fix(server): resume checkpointing after git init (#10078)

This commit is contained in:
Yash Singh authored and GitHub committed 2026-09-05 04:12:01 -05:00
1 parent ab0933a41a
commit c843c19294
2 files changed
+183 -78

No files matched your search

@@ -292,6 +292,7 @@ describe("CheckpointReactor", () => {
async function createHarness(options?: {
readonly hasSession?: boolean;
readonly seedFilesystemCheckpoints?: boolean;
readonly initializeGit?: boolean;
readonly projectWorkspaceRoot?: string;
readonly threadWorktreePath?: string | null;
readonly threadBranch?: string | null;
@@ -303,6 +304,9 @@ describe("CheckpointReactor", () => {
readonly pullRequestRefreshCalls?: Array<string>;
}) {
const cwd = createGitRepository();
if (options?.initializeGit === false) {
NodeFS.rmSync(NodePath.join(cwd, ".git"), { recursive: true });
}
tempDirs.push(cwd);
const provider = createProviderServiceHarness(
cwd,
@@ -1158,52 +1162,148 @@ describe("CheckpointReactor", () => {
).toBe(true);
});
it("appends capture failure activity when turn diff summary cannot be derived", async () => {
const harness = await createHarness({ seedFilesystemCheckpoints: false });
const createdAt = "2026-01-01T00:00:00.000Z";
await Effect.runPromise(
harness.engine.dispatch({
type: "thread.session.set",
commandId: CommandId.make("cmd-session-set-missing-baseline-diff"),
effectIt.effect("captures a checkpoint without a summary when the baseline is missing", () =>
Effect.gen(function* () {
const harness = yield* Effect.promise(() =>
createHarness({ seedFilesystemCheckpoints: false }),
);
harness.provider.emit({
type: "turn.completed",
eventId: EventId.make("evt-turn-completed-missing-baseline"),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:00.000Z",
threadId: ThreadId.make("thread-1"),
session: {
threadId: ThreadId.make("thread-1"),
status: "ready",
providerName: "codex",
turnId: asTurnId("turn-missing-baseline"),
payload: { state: "completed" },
});
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.diff.finalized",
checkpointTurnCount: 1,
});
yield* Effect.promise(harness.drain);
const thread = (yield* Effect.promise(harness.readModel)).threads[0];
expect(thread?.checkpoints[0]).toMatchObject({
status: "ready",
checkpointTurnCount: 1,
files: [],
});
expect(
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 1)),
).toBe(true);
expect(
thread?.activities.some((activity) => activity.kind === "checkpoint.capture.failed"),
).toBe(false);
}),
);
effectIt.effect.each([
{ timing: "between turns", commit: false },
{ timing: "between turns", commit: true },
{ timing: "during a turn", commit: false },
{ timing: "during a turn", commit: true },
])("resumes checkpointing after git init $timing (commit: $commit)", ({ timing, commit }) =>
Effect.gen(function* () {
const harness = yield* Effect.promise(() =>
createHarness({ initializeGit: false, seedFilesystemCheckpoints: false }),
);
const threadId = ThreadId.make("thread-1");
const createdAt = "2026-01-01T00:00:00.000Z";
const emit = (type: "turn.started" | "turn.completed", turn: number) =>
harness.provider.emit({
type,
eventId: EventId.make(`${type}-${turn}`),
provider: ProviderDriverKind.make("codex"),
createdAt,
threadId,
turnId: asTurnId(`turn-${turn}`),
...(type === "turn.completed" ? { payload: { state: "completed" } } : {}),
});
emit("turn.started", 1);
yield* Effect.promise(harness.drain);
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "before git\n");
emit("turn.completed", 1);
yield* Effect.promise(harness.drain);
expect((yield* Effect.promise(harness.readModel)).threads[0]?.checkpoints).toEqual([]);
if (timing === "during a turn") {
emit("turn.started", 2);
yield* Effect.promise(harness.drain);
}
runGit(harness.cwd, ["init", "--initial-branch=main"]);
if (commit) {
runGit(harness.cwd, ["add", "."]);
runGit(harness.cwd, [
"-c",
"user.name=Test",
"-c",
"[email protected]",
"commit",
"-m",
"Initial",
]);
}
if (timing === "between turns") {
// Exercise the domain entry point as well as the provider turn-start event.
yield* harness.engine.dispatch({
type: "thread.turn.start",
commandId: CommandId.make("cmd-after-git-init"),
threadId,
message: {
messageId: MessageId.make("message-after-git-init"),
role: "user",
text: "continue",
attachments: [],
},
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
runtimeMode: "approval-required",
activeTurnId: null,
lastError: null,
updatedAt: createdAt,
},
createdAt,
}),
);
createdAt,
});
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.baseline.captured",
checkpointTurnCount: 0,
});
emit("turn.started", 2);
yield* Effect.promise(harness.drain);
}
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "after git\n");
emit("turn.completed", 2);
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.diff.finalized",
checkpointTurnCount: 1,
});
expect(yield* harness.nextReceipt).toMatchObject({ type: "turn.processing.quiesced" });
yield* Effect.promise(harness.drain);
const firstCheckpoint = (yield* Effect.promise(harness.readModel)).threads[0]?.checkpoints[0];
expect(firstCheckpoint?.files).toEqual(
timing === "between turns"
? [{ path: "README.md", kind: "modified", additions: 1, deletions: 1 }]
: [],
);
expect(
gitShowFileAtRef(harness.cwd, checkpointRefForThreadTurn(threadId, 1), "README.md"),
).toBe("after git\n");
expect(gitRefExists(harness.cwd, checkpointRefForThreadTurn(threadId, 0))).toBe(
timing === "between turns",
);
harness.provider.emit({
type: "turn.completed",
eventId: EventId.make("evt-turn-completed-missing-baseline"),
provider: ProviderDriverKind.make("codex"),
createdAt: "2026-01-01T00:00:00.000Z",
threadId: ThreadId.make("thread-1"),
turnId: asTurnId("turn-missing-baseline"),
payload: { state: "completed" },
});
await waitForEvent(harness.engine, (event) => event.type === "thread.turn-diff-completed");
const thread = await waitForThread(
harness.readModel,
(entry) =>
entry.checkpoints.length === 1 &&
entry.activities.some((activity) => activity.kind === "checkpoint.capture.failed"),
);
expect(thread.checkpoints[0]?.checkpointTurnCount).toBe(1);
expect(
thread.activities.some((activity) => activity.kind === "checkpoint.capture.failed"),
).toBe(true);
});
emit("turn.started", 3);
yield* Effect.promise(harness.drain);
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "next turn\n");
emit("turn.completed", 3);
expect(yield* harness.nextReceipt).toMatchObject({
type: "checkpoint.diff.finalized",
checkpointTurnCount: 2,
});
yield* Effect.promise(harness.drain);
const thread = (yield* Effect.promise(harness.readModel)).threads[0];
expect(thread?.checkpoints[1]?.files).toEqual([
{ path: "README.md", kind: "modified", additions: 1, deletions: 1 },
]);
expect(
thread?.activities.some((activity) => activity.kind === "checkpoint.capture.failed"),
).toBe(false);
}),
);
it("captures pre-turn baseline from project workspace root when thread worktree is unset", async () => {
const harness = await createHarness({
@@ -258,41 +258,46 @@ const make = Effect.gen(function* () {
// reflects files created or deleted during this turn.
yield* workspaceEntries.refresh(input.cwd);
const files = yield* checkpointStore
.diffCheckpoints({
cwd: input.cwd,
fromCheckpointRef,
toCheckpointRef: targetCheckpointRef,
fallbackFromToHead: false,
ignoreWhitespace: false,
format: "numstat",
})
.pipe(
Effect.map((diff) =>
parseTurnDiffFilesFromNumstat(diff).map((file) => ({
path: file.path,
kind: "modified" as const,
additions: file.additions,
deletions: file.deletions,
})),
),
Effect.tapError((error) =>
appendCaptureFailureActivity({
threadId: input.threadId,
turnId: input.turnId,
detail: `Checkpoint captured, but turn diff summary is unavailable: ${error.message}`,
createdAt: input.createdAt,
}),
),
Effect.catch((error) =>
Effect.logWarning("failed to derive checkpoint file summary", {
threadId: input.threadId,
turnId: input.turnId,
turnCount: input.turnCount,
detail: error.message,
}).pipe(Effect.as([])),
),
);
// Git may have been initialized during this turn, leaving no pre-turn
// snapshot. Keep the completion checkpoint for future turns, but do not
// invent a baseline or attempt a diff against a ref that does not exist.
const files = yield* (
fromCheckpointExists
? checkpointStore.diffCheckpoints({
cwd: input.cwd,
fromCheckpointRef,
toCheckpointRef: targetCheckpointRef,
fallbackFromToHead: false,
ignoreWhitespace: false,
format: "numstat",
})
: Effect.succeed("")
).pipe(
Effect.map((diff) =>
parseTurnDiffFilesFromNumstat(diff).map((file) => ({
path: file.path,
kind: "modified" as const,
additions: file.additions,
deletions: file.deletions,
})),
),
Effect.tapError((error) =>
appendCaptureFailureActivity({
threadId: input.threadId,
turnId: input.turnId,
detail: `Checkpoint captured, but turn diff summary is unavailable: ${error.message}`,
createdAt: input.createdAt,
}),
),
Effect.catch((error) =>
Effect.logWarning("failed to derive checkpoint file summary", {
threadId: input.threadId,
turnId: input.turnId,
turnCount: input.turnCount,
detail: error.message,
}).pipe(Effect.as([])),
),
);
const assistantMessageId =
input.assistantMessageId ??