Skip to content

Commit efccda9

Browse files
authored
feat: rewind conversations while keeping file changes (pingdotgg#11358)
Co-authored-by: maria-rcks <[email protected]>
1 parent 25da986 commit efccda9

10 files changed

Lines changed: 475 additions & 161 deletions

File tree

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

Lines changed: 120 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,86 +1683,132 @@ describe("CheckpointReactor", () => {
16831683
}),
16841684
);
16851685

1686-
it("executes provider revert and emits thread.reverted for checkpoint revert requests", async () => {
1687-
const harness = await createHarness();
1688-
const createdAt = "2026-01-01T00:00:00.000Z";
1686+
it.each([
1687+
{ commandType: "thread.checkpoint.revert", initializeGit: true },
1688+
{ commandType: "thread.conversation.revert", initializeGit: true },
1689+
{ commandType: "thread.conversation.revert", initializeGit: false },
1690+
] as const)(
1691+
"$commandType rewinds history with the requested filesystem behavior (git: $initializeGit)",
1692+
async ({ commandType, initializeGit }) => {
1693+
const harness = await createHarness({
1694+
initializeGit,
1695+
seedFilesystemCheckpoints: initializeGit,
1696+
});
1697+
const createdAt = "2026-01-01T00:00:00.000Z";
16891698

1690-
await Effect.runPromise(
1691-
harness.engine.dispatch({
1692-
type: "thread.session.set",
1693-
commandId: CommandId.make("cmd-session-set"),
1694-
threadId: ThreadId.make("thread-1"),
1695-
session: {
1699+
await Effect.runPromise(
1700+
harness.engine.dispatch({
1701+
type: "thread.session.set",
1702+
commandId: CommandId.make("cmd-session-set"),
16961703
threadId: ThreadId.make("thread-1"),
1697-
status: "ready",
1698-
providerName: "codex",
1699-
runtimeMode: "approval-required",
1700-
activeTurnId: null,
1701-
lastError: null,
1702-
updatedAt: createdAt,
1703-
},
1704-
createdAt,
1705-
}),
1706-
);
1704+
session: {
1705+
threadId: ThreadId.make("thread-1"),
1706+
status: "ready",
1707+
providerName: "codex",
1708+
runtimeMode: "approval-required",
1709+
activeTurnId: null,
1710+
lastError: null,
1711+
updatedAt: createdAt,
1712+
},
1713+
createdAt,
1714+
}),
1715+
);
17071716

1708-
await Effect.runPromise(
1709-
harness.engine.dispatch({
1710-
type: "thread.turn.diff.complete",
1711-
commandId: CommandId.make("cmd-diff-1"),
1712-
threadId: ThreadId.make("thread-1"),
1713-
turnId: asTurnId("turn-1"),
1714-
completedAt: createdAt,
1715-
checkpointRef: checkpointRefForThreadTurn(ThreadId.make("thread-1"), 1),
1716-
status: "ready",
1717-
files: [],
1718-
checkpointTurnCount: 1,
1719-
createdAt,
1720-
}),
1721-
);
1722-
await Effect.runPromise(
1723-
harness.engine.dispatch({
1724-
type: "thread.turn.diff.complete",
1725-
commandId: CommandId.make("cmd-diff-2"),
1726-
threadId: ThreadId.make("thread-1"),
1727-
turnId: asTurnId("turn-2"),
1728-
completedAt: createdAt,
1729-
checkpointRef: checkpointRefForThreadTurn(ThreadId.make("thread-1"), 2),
1730-
status: "ready",
1731-
files: [],
1732-
checkpointTurnCount: 2,
1733-
createdAt,
1734-
}),
1735-
);
1717+
await Effect.runPromise(
1718+
harness.engine.dispatch({
1719+
type: "thread.turn.diff.complete",
1720+
commandId: CommandId.make("cmd-diff-1"),
1721+
threadId: ThreadId.make("thread-1"),
1722+
turnId: asTurnId("turn-1"),
1723+
completedAt: createdAt,
1724+
checkpointRef: initializeGit
1725+
? checkpointRefForThreadTurn(ThreadId.make("thread-1"), 1)
1726+
: CheckpointRef.make("provider-diff:thread-1:turn-1"),
1727+
status: initializeGit ? "ready" : "missing",
1728+
files: [],
1729+
checkpointTurnCount: 1,
1730+
createdAt,
1731+
}),
1732+
);
1733+
await Effect.runPromise(
1734+
harness.engine.dispatch({
1735+
type: "thread.turn.diff.complete",
1736+
commandId: CommandId.make("cmd-diff-2"),
1737+
threadId: ThreadId.make("thread-1"),
1738+
turnId: asTurnId("turn-2"),
1739+
completedAt: createdAt,
1740+
checkpointRef: initializeGit
1741+
? checkpointRefForThreadTurn(ThreadId.make("thread-1"), 2)
1742+
: CheckpointRef.make("provider-diff:thread-1:turn-2"),
1743+
status: initializeGit ? "ready" : "missing",
1744+
files: [],
1745+
checkpointTurnCount: 2,
1746+
createdAt,
1747+
}),
1748+
);
17361749

1737-
await Effect.runPromise(
1738-
harness.engine.dispatch({
1739-
type: "thread.checkpoint.revert",
1740-
commandId: CommandId.make("cmd-revert-request"),
1741-
threadId: ThreadId.make("thread-1"),
1742-
turnCount: 1,
1743-
createdAt,
1744-
}),
1745-
);
1750+
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "staged edit\n");
1751+
if (initializeGit) {
1752+
NodeChildProcess.execFileSync("git", ["add", "README.md"], { cwd: harness.cwd });
1753+
}
1754+
NodeFS.writeFileSync(NodePath.join(harness.cwd, "README.md"), "unstaged edit\n");
1755+
NodeFS.writeFileSync(NodePath.join(harness.cwd, "scratch.txt"), "untracked edit\n");
1756+
const indexBefore = initializeGit
1757+
? NodeChildProcess.execFileSync("git", ["ls-files", "--stage"], {
1758+
cwd: harness.cwd,
1759+
encoding: "utf8",
1760+
})
1761+
: undefined;
17461762

1747-
await waitForEvent(harness.engine, (event) => event.type === "thread.reverted");
1748-
const thread = await waitForThread(
1749-
harness.readModel,
1750-
(entry) => entry.checkpoints.length === 1,
1751-
);
1763+
await Effect.runPromise(
1764+
harness.engine.dispatch({
1765+
type: commandType,
1766+
commandId: CommandId.make("cmd-revert-request"),
1767+
threadId: ThreadId.make("thread-1"),
1768+
turnCount: 1,
1769+
createdAt,
1770+
}),
1771+
);
17521772

1753-
expect(thread.latestTurn?.turnId).toBe("turn-1");
1754-
expect(thread.checkpoints).toHaveLength(1);
1755-
expect(thread.checkpoints[0]?.checkpointTurnCount).toBe(1);
1756-
expect(harness.provider.rollbackConversation).toHaveBeenCalledTimes(1);
1757-
expect(harness.provider.rollbackConversation).toHaveBeenCalledWith({
1758-
threadId: ThreadId.make("thread-1"),
1759-
numTurns: 1,
1760-
});
1761-
expect(NodeFS.readFileSync(NodePath.join(harness.cwd, "README.md"), "utf8")).toBe("v2\n");
1762-
expect(
1763-
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 2)),
1764-
).toBe(false);
1765-
});
1773+
await waitForEvent(harness.engine, (event) => event.type === "thread.reverted");
1774+
const thread = await waitForThread(
1775+
harness.readModel,
1776+
(entry) => entry.checkpoints.length === 1,
1777+
);
1778+
1779+
expect(thread.latestTurn?.turnId).toBe("turn-1");
1780+
expect(thread.checkpoints).toHaveLength(1);
1781+
expect(thread.checkpoints[0]?.checkpointTurnCount).toBe(1);
1782+
expect(harness.provider.rollbackConversation).toHaveBeenCalledTimes(1);
1783+
expect(harness.provider.rollbackConversation).toHaveBeenCalledWith({
1784+
threadId: ThreadId.make("thread-1"),
1785+
numTurns: 1,
1786+
});
1787+
expect(NodeFS.readFileSync(NodePath.join(harness.cwd, "README.md"), "utf8")).toBe(
1788+
commandType === "thread.conversation.revert" ? "unstaged edit\n" : "v2\n",
1789+
);
1790+
if (commandType === "thread.conversation.revert") {
1791+
expect(NodeFS.readFileSync(NodePath.join(harness.cwd, "scratch.txt"), "utf8")).toBe(
1792+
"untracked edit\n",
1793+
);
1794+
if (initializeGit) {
1795+
expect(
1796+
NodeChildProcess.execFileSync("git", ["ls-files", "--stage"], {
1797+
cwd: harness.cwd,
1798+
encoding: "utf8",
1799+
}),
1800+
).toBe(indexBefore);
1801+
}
1802+
}
1803+
if (initializeGit) {
1804+
expect(
1805+
gitRefExists(harness.cwd, checkpointRefForThreadTurn(ThreadId.make("thread-1"), 2)),
1806+
).toBe(false);
1807+
} else {
1808+
expect(NodeFS.existsSync(NodePath.join(harness.cwd, ".git"))).toBe(false);
1809+
}
1810+
},
1811+
);
17661812

17671813
it("executes provider revert and emits thread.reverted for claude sessions", async () => {
17681814
const harness = await createHarness({ providerName: ProviderDriverKind.make("claudeAgent") });

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

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,11 @@ const make = Effect.gen(function* () {
704704
thread,
705705
projects: yield* resolveThreadProjects(thread.projectId),
706706
preferSessionRuntime: true,
707-
});
708-
if (!checkpointCwd) {
709-
yield* appendRevertFailureActivity({
710-
threadId: event.payload.threadId,
711-
turnCount: event.payload.turnCount,
712-
detail: "Checkpoint workspace is unavailable or is not a git repository.",
713-
createdAt: now,
714-
}).pipe(Effect.catch(() => Effect.void));
715-
return;
716-
}
707+
}).pipe(
708+
Effect.catch((error) =>
709+
event.payload.restoreFiles === false ? Effect.succeed(undefined) : Effect.fail(error),
710+
),
711+
);
717712

718713
const currentTurnCount = thread.checkpoints.reduce(
719714
(maxTurnCount, checkpoint) => Math.max(maxTurnCount, checkpoint.checkpointTurnCount),
@@ -730,43 +725,55 @@ const make = Effect.gen(function* () {
730725
return;
731726
}
732727

733-
const targetCheckpointRef =
734-
event.payload.turnCount === 0
735-
? checkpointRefForThreadTurn(event.payload.threadId, 0)
736-
: thread.checkpoints.find(
737-
(checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount,
738-
)?.checkpointRef;
728+
yield* providerService.assertConversationRollbackSupported(event.payload.threadId);
739729

740-
if (!targetCheckpointRef) {
741-
yield* appendRevertFailureActivity({
742-
threadId: event.payload.threadId,
743-
turnCount: event.payload.turnCount,
744-
detail: `Checkpoint ref for turn ${event.payload.turnCount} is unavailable in read model.`,
745-
createdAt: now,
746-
}).pipe(Effect.catch(() => Effect.void));
747-
return;
748-
}
730+
if (event.payload.restoreFiles !== false) {
731+
if (!checkpointCwd) {
732+
yield* appendRevertFailureActivity({
733+
threadId: event.payload.threadId,
734+
turnCount: event.payload.turnCount,
735+
detail: "Checkpoint workspace is unavailable or is not a git repository.",
736+
createdAt: now,
737+
}).pipe(Effect.catch(() => Effect.void));
738+
return;
739+
}
749740

750-
yield* providerService.assertConversationRollbackSupported(event.payload.threadId);
741+
const targetCheckpointRef =
742+
event.payload.turnCount === 0
743+
? checkpointRefForThreadTurn(event.payload.threadId, 0)
744+
: thread.checkpoints.find(
745+
(checkpoint) => checkpoint.checkpointTurnCount === event.payload.turnCount,
746+
)?.checkpointRef;
747+
748+
if (!targetCheckpointRef) {
749+
yield* appendRevertFailureActivity({
750+
threadId: event.payload.threadId,
751+
turnCount: event.payload.turnCount,
752+
detail: `Checkpoint ref for turn ${event.payload.turnCount} is unavailable in read model.`,
753+
createdAt: now,
754+
}).pipe(Effect.catch(() => Effect.void));
755+
return;
756+
}
751757

752-
const restored = yield* checkpointStore.restoreCheckpoint({
753-
cwd: checkpointCwd,
754-
checkpointRef: targetCheckpointRef,
755-
fallbackToHead: event.payload.turnCount === 0,
756-
});
757-
if (!restored) {
758-
yield* appendRevertFailureActivity({
759-
threadId: event.payload.threadId,
760-
turnCount: event.payload.turnCount,
761-
detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}.`,
762-
createdAt: now,
763-
}).pipe(Effect.catch(() => Effect.void));
764-
return;
765-
}
758+
const restored = yield* checkpointStore.restoreCheckpoint({
759+
cwd: checkpointCwd,
760+
checkpointRef: targetCheckpointRef,
761+
fallbackToHead: event.payload.turnCount === 0,
762+
});
763+
if (!restored) {
764+
yield* appendRevertFailureActivity({
765+
threadId: event.payload.threadId,
766+
turnCount: event.payload.turnCount,
767+
detail: `Filesystem checkpoint is unavailable for turn ${event.payload.turnCount}.`,
768+
createdAt: now,
769+
}).pipe(Effect.catch(() => Effect.void));
770+
return;
771+
}
766772

767-
// Refresh the workspace entry index so the @-mention file picker
768-
// reflects the reverted filesystem state.
769-
yield* workspaceEntries.refresh(checkpointCwd);
773+
// Refresh the workspace entry index so the @-mention file picker
774+
// reflects the reverted filesystem state.
775+
yield* workspaceEntries.refresh(checkpointCwd);
776+
}
770777

771778
const rolledBackTurns = Math.max(0, currentTurnCount - event.payload.turnCount);
772779
if (rolledBackTurns > 0) {
@@ -783,7 +790,7 @@ const make = Effect.gen(function* () {
783790
}
784791
}
785792

786-
if (staleCheckpointRefs.length > 0) {
793+
if (checkpointCwd && staleCheckpointRefs.length > 0) {
787794
yield* checkpointStore.deleteCheckpointRefs({
788795
cwd: checkpointCwd,
789796
checkpointRefs: staleCheckpointRefs,

‎apps/server/src/orchestration/decider.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,6 +1640,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
16401640
};
16411641
}
16421642

1643+
case "thread.conversation.revert":
16431644
case "thread.checkpoint.revert": {
16441645
yield* requireThread({
16451646
readModel,
@@ -1657,6 +1658,7 @@ export const decideOrchestrationCommand = Effect.fn("decideOrchestrationCommand"
16571658
payload: {
16581659
threadId: command.threadId,
16591660
turnCount: command.turnCount,
1661+
...(command.type === "thread.conversation.revert" ? { restoreFiles: false } : {}),
16601662
createdAt: command.createdAt,
16611663
},
16621664
};

0 commit comments

Comments
 (0)