fix: fail closed when revert snapshots are missing

This commit is contained in:
Rishin Georgy Anil authored and Julius Marminge committed 2026-07-20 17:45:27 +02:00
1 parent 21e11b0d65
commit c1e0025199
3 files changed
+89 -27

No files matched your search

@@ -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(
@@ -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";
@@ -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({