From c1e00251991eae18ffa2d6c7c7019f7452c67a07 Mon Sep 17 00:00:00 2001 From: Rishin Georgy Anil Date: Sun, 19 Jul 2026 18:36:14 +0100 Subject: [PATCH] fix: fail closed when revert snapshots are missing --- .../src/checkpointing/CheckpointStore.ts | 3 + .../Layers/CheckpointReactor.test.ts | 63 +++++++++++++++++++ .../orchestration/Layers/CheckpointReactor.ts | 50 +++++++-------- 3 files changed, 89 insertions(+), 27 deletions(-) diff --git a/apps/server/src/checkpointing/CheckpointStore.ts b/apps/server/src/checkpointing/CheckpointStore.ts index 0e4c3b8c7e..f5287596d4 100644 --- a/apps/server/src/checkpointing/CheckpointStore.ts +++ b/apps/server/src/checkpointing/CheckpointStore.ts @@ -217,6 +217,9 @@ export const make = Effect.gen(function* () { restoreCheckpoint: Effect.fn("CheckpointStore.shadow.restoreCheckpoint")(function* (input) { const commit = yield* resolveShadowCommit(input); if (!commit) { + // Shadow repositories intentionally have no HEAD: every restorable + // state must be addressed by its explicit checkpoint ref. Fail closed + // so callers never roll conversation history back without matching files. return false; } const tempIndexPath = NodePath.join( diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts index 1b8a338b1a..dfe072dd35 100644 --- a/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts +++ b/apps/server/src/orchestration/Layers/CheckpointReactor.test.ts @@ -1066,6 +1066,69 @@ describe("CheckpointReactor", () => { }); }); + it("does not rewind conversation when a shadow filesystem checkpoint is missing", async () => { + const workspace = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-shadow-revert-")); + tempDirs.push(workspace); + NodeFS.writeFileSync(NodePath.join(workspace, "keep.txt"), "unchanged\n", "utf8"); + const harness = await createHarness({ + seedFilesystemCheckpoints: false, + projectWorkspaceRoot: workspace, + threadWorktreePath: workspace, + providerSessionCwd: workspace, + }); + const threadId = ThreadId.make("thread-1"); + const createdAt = "2026-01-01T00:00:00.000Z"; + + await harness.runPromise( + harness.engine.dispatch({ + type: "thread.session.set", + commandId: CommandId.make("cmd-shadow-missing-session-set"), + threadId, + session: { + threadId, + status: "ready", + providerName: "codex", + runtimeMode: "approval-required", + activeTurnId: null, + lastError: null, + updatedAt: createdAt, + }, + createdAt, + }), + ); + await harness.runPromise( + harness.engine.dispatch({ + type: "thread.turn.diff.complete", + commandId: CommandId.make("cmd-shadow-missing-checkpoint-summary"), + threadId, + turnId: asTurnId("shadow-missing-turn"), + completedAt: createdAt, + checkpointRef: checkpointRefForThreadTurn(threadId, 1), + status: "ready", + files: [], + checkpointTurnCount: 1, + createdAt, + }), + ); + + await harness.runPromise( + harness.engine.dispatch({ + type: "thread.checkpoint.revert", + commandId: CommandId.make("cmd-shadow-missing-revert"), + threadId, + turnCount: 0, + createdAt, + }), + ); + + const thread = await waitForThread(harness.readModel, (entry) => + entry.activities.some((activity) => activity.kind === "checkpoint.revert.failed"), + ); + expect(thread.checkpoints).toHaveLength(1); + expect(harness.provider.rollbackConversation).not.toHaveBeenCalled(); + expect(NodeFS.readFileSync(NodePath.join(workspace, "keep.txt"), "utf8")).toBe("unchanged\n"); + }); + it("executes provider revert and emits thread.reverted for claude sessions", async () => { const harness = await createHarness({ providerName: ProviderDriverKind.make("claudeAgent") }); const createdAt = "2026-01-01T00:00:00.000Z"; diff --git a/apps/server/src/orchestration/Layers/CheckpointReactor.ts b/apps/server/src/orchestration/Layers/CheckpointReactor.ts index e7fbe036b2..3dea5613f4 100644 --- a/apps/server/src/orchestration/Layers/CheckpointReactor.ts +++ b/apps/server/src/orchestration/Layers/CheckpointReactor.ts @@ -655,35 +655,31 @@ const make = Effect.gen(function* () { return; } - if (checkpointTurnCount > 0) { - const summarizedCheckpoint = thread.checkpoints.find( - (checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount, - ); - const targetCheckpointRef = - summarizedCheckpoint?.checkpointRef ?? - checkpointRefForThreadTurn(event.payload.threadId, event.payload.turnCount); - const restored = yield* checkpointStore.restoreCheckpoint({ - cwd: sessionRuntime.value.cwd, - checkpointRef: targetCheckpointRef, - fallbackToHead: event.payload.turnCount === 0, - }); - if (!restored && summarizedCheckpoint) { - yield* appendRevertFailureActivity({ - threadId: event.payload.threadId, - turnCount: event.payload.turnCount, - detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}.`, - createdAt: now, - }).pipe(Effect.catch(() => Effect.void)); - return; - } - - if (restored) { - // Refresh the workspace entry index so the @-mention file picker - // reflects the reverted filesystem state. - yield* workspaceEntries.refresh(sessionRuntime.value.cwd); - } + const summarizedCheckpoint = thread.checkpoints.find( + (checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount, + ); + const targetCheckpointRef = + summarizedCheckpoint?.checkpointRef ?? + checkpointRefForThreadTurn(event.payload.threadId, event.payload.turnCount); + const restored = yield* checkpointStore.restoreCheckpoint({ + cwd: sessionRuntime.value.cwd, + checkpointRef: targetCheckpointRef, + fallbackToHead: event.payload.turnCount === 0, + }); + if (!restored) { + yield* appendRevertFailureActivity({ + threadId: event.payload.threadId, + turnCount: event.payload.turnCount, + detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}. Conversation history was not changed.`, + createdAt: now, + }).pipe(Effect.catch(() => Effect.void)); + return; } + // Refresh the workspace entry index so the @-mention file picker reflects + // the reverted filesystem state before conversation history is rewound. + yield* workspaceEntries.refresh(sessionRuntime.value.cwd); + const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount); if (rolledBackTurns > 0) { yield* providerService.rollbackConversation({