Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/server/src/checkpointing/CheckpointStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";

import type { CheckpointStoreError } from "./Errors.ts";
import type { VcsCheckpointOps } from "../vcs/VcsDriver.ts";
import type { VcsCheckpointOps, VcsCheckpointRange } from "../vcs/VcsDriver.ts";
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";

export interface CaptureCheckpointInput {
Expand All @@ -16,6 +16,8 @@ export interface CaptureCheckpointInput {
readonly excludePaths?: ReadonlyArray<string>;
}

export type CheckpointRange = VcsCheckpointRange;

export interface CaptureHeadCheckpointInput {
readonly cwd: string;
readonly checkpointRef: CheckpointRef;
Expand Down Expand Up @@ -49,12 +51,16 @@ export interface DiffCheckpointsInput {
readonly filePaths?: ReadonlyArray<string>;
/** Folders below `cwd` left out of the diff, as in a capture. */
readonly excludePaths?: ReadonlyArray<string>;
/** The refs' range as their capture returned it; the refs are then not read. */
readonly range?: VcsCheckpointRange;
}

export interface ListAuthoredPathsInput {
readonly cwd: string;
readonly fromCheckpointRef: CheckpointRef;
readonly toCheckpointRef: CheckpointRef;
/** The refs' range as their capture returned it; the refs are then not read. */
readonly range?: VcsCheckpointRange;
}

export interface DeleteCheckpointRefsInput {
Expand All @@ -67,9 +73,10 @@ export class CheckpointStore extends Context.Service<
{
readonly isGitRepository: (cwd: string) => Effect.Effect<boolean, CheckpointStoreError>;

/** The range from `previousCheckpointRef`, or null when there is none to compare with. */
readonly captureCheckpoint: (
input: CaptureCheckpointInput,
) => Effect.Effect<void, CheckpointStoreError>;
) => Effect.Effect<VcsCheckpointRange | null, CheckpointStoreError>;

/**
* HEAD's commit when the folder matches it exactly: nothing staged, modified or untracked.
Expand Down
96 changes: 93 additions & 3 deletions apps/server/src/orchestration-v2/CheckpointCaptureService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import * as ThreadManagement from "./ThreadManagementService.ts";
import * as WorkspaceFolders from "../workspace/WorkspaceFolders.ts";

// `git -C <cwd> -c <config> log ...` reads as `log`.
const gitSubcommand = (args: ReadonlyArray<string>) => {
let index = 0;
while (args[index] === "-C" || args[index] === "-c") index += 2;
return args[index] ?? "";
};

const layerProjectionStoreTest = Layer.mergeAll(
ThreadCommandExecutor.layer,
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
Expand Down Expand Up @@ -298,7 +305,7 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
Layer.mock(CheckpointStore.CheckpointStore)({
listAuthoredPaths: () => Effect.succeed(null),
isGitRepository: () => Effect.succeed(true),
captureCheckpoint: () => Effect.void,
captureCheckpoint: () => Effect.succeed(null),
hasCheckpointRef: () =>
Effect.fail(
new VcsProcessTimeoutError({
Expand Down Expand Up @@ -914,7 +921,7 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
Layer.mock(CheckpointStore.CheckpointStore)({
isGitRepository: () => Effect.succeed(true),
hasCheckpointRef: () => Effect.succeed(true),
captureCheckpoint: () => Effect.void,
captureCheckpoint: () => Effect.succeed(null),
diffCheckpoints: () => Effect.succeed(""),
listAuthoredPaths: () => Effect.succeed(null),
}),
Expand Down Expand Up @@ -1448,6 +1455,8 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
readonly threadId: ThreadId;
readonly ids: IdAllocator.IdAllocatorV2["Service"];
readonly diffs: CheckpointDiffQuery.CheckpointDiffQuery["Service"];
/** The git commands run since the last call, each as its subcommand and arguments. */
readonly takeGitCommands: Effect.Effect<ReadonlyArray<ReadonlyArray<string>>>;
}) => Effect.Effect<A, E, R>,
) =>
Effect.gen(function* () {
Expand All @@ -1461,7 +1470,19 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
const labels = new Map<string, string>([[repositoryRootScopeId, "root"]]);
const committed = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
const layerNode = Layer.mergeAll(NodeServices.layer, NodeCrypto.layer);
const layerVcsProcess = VcsProcess.layer.pipe(Layer.provide(layerNode));
let gitCommands: Array<ReadonlyArray<string>> = [];
const layerVcsProcess = Layer.effect(
VcsProcess.VcsProcess,
Effect.map(VcsProcess.VcsProcess, (inner) =>
VcsProcess.VcsProcess.of({
run: (input) =>
Effect.suspend(() => {
gitCommands.push(input.args);
return inner.run(input);
}),
}),
),
).pipe(Layer.provide(VcsProcess.layer), Layer.provide(layerNode));
const layerCheckpointStore = CheckpointStore.layer.pipe(
Layer.provide(VcsDriverRegistry.layer.pipe(Layer.provide(layerVcsProcess))),
Layer.provide(layerNode),
Expand Down Expand Up @@ -1728,10 +1749,79 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
threadId: repositoryThreadId,
ids: yield* IdAllocator.IdAllocatorV2,
diffs: yield* CheckpointDiffQuery.CheckpointDiffQuery,
takeGitCommands: Effect.sync(() => {
const taken = gitCommands;
gitCommands = [];
return taken;
}),
});
}).pipe(Effect.provide(layer), Effect.scoped);
});

it.effect("captures a turn without re-reading the previous checkpoint", () =>
withTrackedRepositories("capture-git-commands", (thread) =>
Effect.gen(function* () {
const { root } = thread;
yield* thread.initRepository(root);
yield* thread.turn(1, thread.write(`${root}/README.md`, "# one\n"));
// Counted from after the turn's edits, so only the capture itself.
const capture = (ordinal: number, edits: Effect.Effect<void>) =>
Effect.gen(function* () {
const recorded = yield* thread.turn(
ordinal,
edits.pipe(Effect.andThen(thread.takeGitCommands)),
);
return {
files: recorded.flatMap((entry) => entry.files ?? []),
commands: (yield* thread.takeGitCommands).map(gitSubcommand),
};
});
const snapshot = [
"rev-parse",
"log",
"config",
"read-tree",
"ls-files",
"add",
"write-tree",
"log",
"commit-tree",
"update-ref",
];

assert.deepEqual(yield* capture(2, thread.write(`${root}/README.md`, "# two\n")), {
files: ["README.md"],
commands: [...snapshot, "diff"],
});
// Nothing changed, so there is nothing to diff.
assert.deepEqual(yield* capture(3, Effect.void), { files: [], commands: snapshot });
// A commit moves HEAD; its paths are read without resolving the range again.
const committed = yield* capture(
4,
thread
.write(`${root}/README.md`, "# four\n")
.pipe(Effect.andThen(thread.git(root, "commit", "-am", "four")), Effect.asVoid),
);
assert.deepEqual(committed.files, ["README.md"]);
// The summary's reads run side by side, so only which commands ran is fixed.
assert.deepEqual(
committed.commands.toSorted(),
[
...snapshot,
"diff",
"version",
"rev-list",
"diff",
"diff",
"log",
"log",
"log",
].toSorted(),
);
}),
),
);

it.effect("checkpoints the repositories a folder that is not a repository holds", () =>
withTrackedRepositories("capture-wrapper", (thread) =>
Effect.gen(function* () {
Expand Down
8 changes: 6 additions & 2 deletions apps/server/src/orchestration-v2/CheckpointService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ it.effect(
yield* Deferred.succeed(firstSnapshotStarted, undefined);
yield* Effect.sleep("10 seconds");
yield* Ref.update(refs, (current) => new Set([...current, checkpointRef]));
return null;
}),
}),
),
Expand Down Expand Up @@ -482,7 +483,9 @@ it.effect("backs off only baseline retries after repeated workspace capture time
captureCheckpoint: () =>
Effect.suspend(() => {
attempts++;
return healthy ? Effect.void : Effect.fail(nonTimeoutFailure ? failure : timeout);
return healthy
? Effect.succeed(null)
: Effect.fail(nonTimeoutFailure ? failure : timeout);
}),
}),
),
Expand Down Expand Up @@ -586,7 +589,7 @@ it.effect("skips post-turn captures that waited out another capture's timeout in
yield* Effect.sleep("30 seconds");
const [next = "timeout", ...rest] = yield* Ref.get(script);
yield* Ref.set(script, rest);
if (next === "ready") return;
if (next === "ready") return null;
return yield* new VcsProcessTimeoutError({
operation: "GitVcsDriver.checkpoints.captureCheckpoint",
command: "git",
Expand Down Expand Up @@ -737,6 +740,7 @@ it.effect("keeps the pre-rewind snapshot of a folder a failed rewind could not p
captured.push(input.cwd);
existing.add(input.checkpointRef);
preRewindRefs.set(input.cwd, input.checkpointRef);
return null;
}),
// The second folder's rewind fails, and then so does putting the first one back.
restoreCheckpoint: (input) =>
Expand Down
Loading
Loading