Skip to content

Commit fd5553f

Browse files
authored
fix: restore provider history and prompts when rewinding (pingdotgg#11338)
Co-authored-by: maria-rcks <[email protected]>
1 parent cf1ba3d commit fd5553f

31 files changed

Lines changed: 1517 additions & 190 deletions

‎apps/server/integration/orchestrationEngine.integration.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ it.live("reverts to an earlier checkpoint and trims checkpoint projections + git
888888
);
889889

890890
it.live(
891-
"appends checkpoint.revert.failed activity when revert is requested without an active session",
891+
"appends checkpoint.revert.failed activity when revert is requested without a provider binding",
892892
() =>
893893
withHarness((harness) =>
894894
Effect.gen(function* () {
@@ -917,7 +917,7 @@ it.live(
917917
assert.equal(
918918
String(
919919
(failureActivity?.payload as { readonly detail?: string } | undefined)?.detail,
920-
).includes("No active provider session"),
920+
).includes("no persisted provider binding exists"),
921921
true,
922922
);
923923
}),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { forkSession, getSessionMessages } from "@anthropic-ai/claude-agent-sdk";
2+
import * as Schema from "effect/Schema";
3+
4+
// A separate process gives SDK history helpers the provider's environment without
5+
// mutating the server's environment. This entry is bundled alongside the server.
6+
const [method, sessionId, rawOptions] = process.argv.slice(2);
7+
const options = Schema.decodeSync(
8+
Schema.fromJsonString(
9+
Schema.Struct({
10+
dir: Schema.optionalKey(Schema.String),
11+
includeSystemMessages: Schema.optionalKey(Schema.Boolean),
12+
upToMessageId: Schema.optionalKey(Schema.String),
13+
}),
14+
),
15+
)(rawOptions ?? "{}");
16+
if (!sessionId) throw new Error("Claude history session id is required.");
17+
const result =
18+
method === "getSessionMessages"
19+
? await getSessionMessages(sessionId, options)
20+
: method === "forkSession"
21+
? await forkSession(sessionId, options)
22+
: (() => {
23+
throw new Error("Unknown Claude history operation.");
24+
})();
25+
process.stdout.write(JSON.stringify(result));

‎apps/server/src/orchestration/Layers/CheckpointReactor.test.ts‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,27 +1916,45 @@ describe("CheckpointReactor", () => {
19161916
});
19171917
});
19181918

1919-
it("appends an error activity when revert is requested without an active session", async () => {
1920-
const harness = await createHarness({ hasSession: false });
1921-
const createdAt = "2026-01-01T00:00:00.000Z";
1922-
1923-
await Effect.runPromise(
1924-
harness.engine.dispatch({
1925-
type: "thread.checkpoint.revert",
1926-
commandId: CommandId.make("cmd-revert-no-session"),
1927-
threadId: ThreadId.make("thread-1"),
1928-
turnCount: 1,
1929-
createdAt,
1930-
}),
1931-
);
1919+
it.each([false, true])(
1920+
"reverts without an active session using project cwd fallback: %s",
1921+
async (useProjectCwd) => {
1922+
const harness = await createHarness({
1923+
hasSession: false,
1924+
...(useProjectCwd ? { threadWorktreePath: null } : {}),
1925+
});
1926+
const createdAt = "2026-01-01T00:00:00.000Z";
19321927

1933-
const thread = await waitForThread(harness.readModel, (entry) =>
1934-
entry.activities.some((activity) => activity.kind === "checkpoint.revert.failed"),
1935-
);
1928+
await Effect.runPromise(
1929+
harness.engine.dispatch({
1930+
type: "thread.turn.diff.complete",
1931+
commandId: CommandId.make("cmd-diff-before-session-recovery"),
1932+
threadId: ThreadId.make("thread-1"),
1933+
turnId: asTurnId("turn-1"),
1934+
completedAt: createdAt,
1935+
checkpointRef: checkpointRefForThreadTurn(ThreadId.make("thread-1"), 1),
1936+
status: "ready",
1937+
files: [],
1938+
checkpointTurnCount: 1,
1939+
createdAt,
1940+
}),
1941+
);
1942+
await Effect.runPromise(
1943+
harness.engine.dispatch({
1944+
type: "thread.checkpoint.revert",
1945+
commandId: CommandId.make("cmd-revert-no-session"),
1946+
threadId: ThreadId.make("thread-1"),
1947+
turnCount: 0,
1948+
createdAt,
1949+
}),
1950+
);
19361951

1937-
expect(thread.activities.some((activity) => activity.kind === "checkpoint.revert.failed")).toBe(
1938-
true,
1939-
);
1940-
expect(harness.provider.rollbackConversation).not.toHaveBeenCalled();
1941-
});
1952+
await waitForEvent(harness.engine, (event) => event.type === "thread.reverted");
1953+
expect(harness.provider.rollbackConversation).toHaveBeenCalledWith({
1954+
threadId: ThreadId.make("thread-1"),
1955+
numTurns: 1,
1956+
});
1957+
expect(NodeFS.readFileSync(NodePath.join(harness.cwd, "README.md"), "utf8")).toBe("v1\n");
1958+
},
1959+
);
19421960
});

‎apps/server/src/orchestration/Layers/CheckpointReactor.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -699,21 +699,17 @@ const make = Effect.gen(function* () {
699699
return;
700700
}
701701

702-
const sessionRuntime = yield* resolveSessionRuntimeForThread(event.payload.threadId);
703-
if (Option.isNone(sessionRuntime)) {
704-
yield* appendRevertFailureActivity({
705-
threadId: event.payload.threadId,
706-
turnCount: event.payload.turnCount,
707-
detail: "No active provider session with workspace cwd is bound to this thread.",
708-
createdAt: now,
709-
}).pipe(Effect.catch(() => Effect.void));
710-
return;
711-
}
712-
if (!(yield* checkpointStore.isGitRepository(sessionRuntime.value.cwd))) {
702+
const checkpointCwd = yield* resolveCheckpointCwd({
703+
threadId: event.payload.threadId,
704+
thread,
705+
projects: yield* resolveThreadProjects(thread.projectId),
706+
preferSessionRuntime: true,
707+
});
708+
if (!checkpointCwd) {
713709
yield* appendRevertFailureActivity({
714710
threadId: event.payload.threadId,
715711
turnCount: event.payload.turnCount,
716-
detail: "Checkpoints are unavailable because this project is not a git repository.",
712+
detail: "Checkpoint workspace is unavailable or is not a git repository.",
717713
createdAt: now,
718714
}).pipe(Effect.catch(() => Effect.void));
719715
return;
@@ -754,7 +750,7 @@ const make = Effect.gen(function* () {
754750
yield* providerService.assertConversationRollbackSupported(event.payload.threadId);
755751

756752
const restored = yield* checkpointStore.restoreCheckpoint({
757-
cwd: sessionRuntime.value.cwd,
753+
cwd: checkpointCwd,
758754
checkpointRef: targetCheckpointRef,
759755
fallbackToHead: event.payload.turnCount === 0,
760756
});
@@ -770,12 +766,12 @@ const make = Effect.gen(function* () {
770766

771767
// Refresh the workspace entry index so the @-mention file picker
772768
// reflects the reverted filesystem state.
773-
yield* workspaceEntries.refresh(sessionRuntime.value.cwd);
769+
yield* workspaceEntries.refresh(checkpointCwd);
774770

775771
const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount);
776772
if (rolledBackTurns > 0) {
777773
yield* providerService.rollbackConversation({
778-
threadId: sessionRuntime.value.threadId,
774+
threadId: event.payload.threadId,
779775
numTurns: rolledBackTurns,
780776
});
781777
}
@@ -789,7 +785,7 @@ const make = Effect.gen(function* () {
789785

790786
if (staleCheckpointRefs.length > 0) {
791787
yield* checkpointStore.deleteCheckpointRefs({
792-
cwd: sessionRuntime.value.cwd,
788+
cwd: checkpointCwd,
793789
checkpointRefs: staleCheckpointRefs,
794790
});
795791
}

0 commit comments

Comments
 (0)