Skip to content

Commit 31eea88

Browse files
Revens2juliusmarmingeclaude
authored
fix(rewind): allow conversation-only rewind in non-git workspaces (#18379)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 2dfbad7 commit 31eea88

15 files changed

Lines changed: 328 additions & 45 deletions

‎apps/server/src/orchestration-v2/CheckpointCaptureService.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,24 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
914914
DateTime.formatIso(cancelledAt),
915915
);
916916
assert.equal(projected.rootNode?.status, "cancelled");
917-
assert.deepEqual([...projected.readyCheckpointOrdinals].toSorted(), [0, 1]);
917+
assert.deepEqual([...projected.preservedCheckpointOrdinals].toSorted(), [0, 1]);
918+
919+
// A rewind leaves the run's checkpoint stale; the next capture must rebaseline it.
920+
yield* projectionStore.apply({
921+
id: EventId.make("event:checkpoint-capture-cancelled:captured-stale"),
922+
type: "checkpoint.captured",
923+
threadId: cancelledThreadId,
924+
runId: cancelledRunId,
925+
nodeId: cancelledRootNodeId,
926+
providerInstanceId,
927+
occurredAt: cancelledAt,
928+
payload: { ...captured, status: "stale" },
929+
});
930+
const afterRewind = yield* projectionStore.getCheckpointCaptureContext(cancelledThreadId, {
931+
runId: cancelledRunId,
932+
scopeId: cancelledScopeId,
933+
});
934+
assert.deepEqual(afterRewind.preservedCheckpointOrdinals, [0]);
918935
}),
919936
);
920937

‎apps/server/src/orchestration-v2/CheckpointCaptureService.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const layer: Layer.Layer<
6565
readonly runId: RunId;
6666
readonly scopeId: CheckpointScopeId;
6767
}) {
68-
const { run, rootNode, scope, providerThread, readyCheckpointOrdinals, sideChat } =
68+
const { run, rootNode, scope, providerThread, preservedCheckpointOrdinals, sideChat } =
6969
yield* projections.getCheckpointCaptureContext(input.threadId, input);
7070
// Stopped runs remain terminal; their checkpoint is the next message's rollback point.
7171
const stopped = run?.status === "interrupted" || run?.status === "cancelled";
@@ -101,8 +101,8 @@ export const layer: Layer.Layer<
101101

102102
const capturedAt = yield* DateTime.now;
103103
const baselineOrdinalWithinScope = Math.max(0, run.ordinal - 1);
104-
const hasReadyCheckpoint = (ordinalWithinScope: number) =>
105-
readyCheckpointOrdinals.includes(ordinalWithinScope);
104+
const isPreserved = (ordinalWithinScope: number) =>
105+
preservedCheckpointOrdinals.includes(ordinalWithinScope);
106106
// The baselines this capture materializes are attributed to the run materializing them. The
107107
// thread's scope may already be a later run's: one queued meanwhile, or sent after Stop.
108108
const materializeBaseline = (ordinalWithinScope: number) =>
@@ -114,10 +114,8 @@ export const layer: Layer.Layer<
114114
forceMissing: sideChat,
115115
});
116116
const threadStartCheckpoint =
117-
baselineOrdinalWithinScope === 0 || hasReadyCheckpoint(0)
118-
? null
119-
: yield* materializeBaseline(0);
120-
const baselineCheckpoint = hasReadyCheckpoint(baselineOrdinalWithinScope)
117+
baselineOrdinalWithinScope === 0 || isPreserved(0) ? null : yield* materializeBaseline(0);
118+
const baselineCheckpoint = isPreserved(baselineOrdinalWithinScope)
121119
? null
122120
: yield* materializeBaseline(baselineOrdinalWithinScope);
123121
const checkpoint = yield* checkpoints.capture({

‎apps/server/src/orchestration-v2/CheckpointRollbackService.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ export const layer: Layer.Layer<
152152
checkpoint === undefined ||
153153
scope === undefined ||
154154
checkpoint.scopeId !== scope.id ||
155-
checkpoint.status !== "ready"
155+
// Missing snapshots support conversation rewind only.
156+
(checkpoint.status !== "ready" &&
157+
!(input.restoreFiles === false && checkpoint.status === "missing"))
156158
) {
157159
return yield* new CheckpointRollbackExecutionError({
158160
reason: "rollback-target-invalid",

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12749,7 +12749,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1274912749
cause: `Checkpoint ${command.checkpointId} was not found.`,
1275012750
});
1275112751
}
12752-
if (targetCheckpoint.status !== "ready") {
12752+
// Missing snapshots support conversation rewind only.
12753+
if (
12754+
targetCheckpoint.status !== "ready" &&
12755+
!(command.restoreFiles === false && targetCheckpoint.status === "missing")
12756+
) {
1275312757
return yield* new OrchestratorDispatchError({
1275412758
commandId: command.commandId,
1275512759
commandType: command.type,
@@ -12802,7 +12806,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1280212806
const targetRun = projection.runs.find((run) => run.ordinal === targetOrdinal);
1280312807
const targetProviderTurn =
1280412808
targetRun === undefined ? undefined : providerTurnForRun(projection, targetRun);
12805-
if (targetRun === undefined || targetProviderTurn === undefined) {
12809+
if (
12810+
targetRun === undefined ||
12811+
targetRun.status === "rolled_back" ||
12812+
targetProviderTurn === undefined
12813+
) {
1280612814
return yield* new OrchestratorDispatchError({
1280712815
commandId: command.commandId,
1280812816
commandType: command.type,

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ export interface ProjectionCheckpointCaptureContext {
262262
readonly rootNode: OrchestrationV2ExecutionNode | undefined;
263263
readonly scope: OrchestrationV2CheckpointScope | undefined;
264264
readonly providerThread: OrchestrationV2ProviderThread | undefined;
265-
readonly readyCheckpointOrdinals: ReadonlyArray<number>;
265+
/**
266+
* Ordinals a runless baseline must not overwrite: ready snapshots, and missing checkpoints pinning
267+
* a run. Without file snapshots that run link is what rewind uses. Stale rows are rebaselined.
268+
*/
269+
readonly preservedCheckpointOrdinals: ReadonlyArray<number>;
266270
/** Side chats share their parent's worktree and never snapshot it. */
267271
readonly sideChat: boolean;
268272
}
@@ -5427,7 +5431,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54275431
runRows[0] === undefined
54285432
? undefined
54295433
: yield* decodeRunPayload(runRows[0].payload_json);
5430-
const [nodeRows, scopeRows, providerRows, readyRows] = yield* Effect.all([
5434+
const [nodeRows, scopeRows, providerRows, preservedRows] = yield* Effect.all([
54315435
sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_nodes
54325436
WHERE thread_id = ${threadId} AND node_id = ${run?.rootNodeId ?? null}`,
54335437
sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_checkpoint_scopes
@@ -5439,7 +5443,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54395443
OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}))`,
54405444
sql<{ readonly ordinal: number }>`SELECT ordinal_within_scope AS ordinal
54415445
FROM orchestration_v2_projection_checkpoints
5442-
WHERE thread_id = ${threadId} AND scope_id = ${target.scopeId} AND status = 'ready'`,
5446+
WHERE thread_id = ${threadId} AND scope_id = ${target.scopeId}
5447+
AND (status = 'ready' OR (status = 'missing' AND run_id IS NOT NULL))`,
54435448
]);
54445449
return {
54455450
run,
@@ -5455,7 +5460,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
54555460
providerRows[0] === undefined
54565461
? undefined
54575462
: yield* decodeProviderThreadPayload(providerRows[0].payload_json),
5458-
readyCheckpointOrdinals: readyRows.map(({ ordinal }) => ordinal),
5463+
preservedCheckpointOrdinals: preservedRows.map(({ ordinal }) => ordinal),
54595464
sideChat: threadRows[0].side_chat === 1,
54605465
};
54615466
}),
@@ -5512,7 +5517,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55125517
checkpoint === undefined ||
55135518
scope === undefined ||
55145519
checkpoint.scopeId !== scope.id ||
5515-
checkpoint.status !== "ready" ||
5520+
(checkpoint.status !== "ready" && checkpoint.status !== "missing") ||
55165521
providerThread.id !== thread.activeProviderThreadId ||
55175522
providerThread.providerInstanceId !== thread.modelSelection.instanceId
55185523
)
@@ -5526,7 +5531,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55265531
JOIN orchestration_v2_projection_runs AS run
55275532
ON attempt.attempt_id = json_extract(run.payload_json, '$.activeAttemptId')
55285533
WHERE run.thread_id = ${threadId} AND run.ordinal = ${targetOrdinal}
5529-
AND attempt.thread_id = ${threadId}`,
5534+
AND run.status != 'rolled_back' AND attempt.thread_id = ${threadId}`,
55305535
sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_runs
55315536
WHERE thread_id = ${threadId} AND ordinal > ${targetOrdinal}
55325537
AND status IN ('completed', 'interrupted', 'failed', 'cancelled')

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import * as Effect from "effect/Effect";
4040
import * as DateTime from "effect/DateTime";
4141
import * as Deferred from "effect/Deferred";
4242
import * as Fiber from "effect/Fiber";
43+
import * as FileSystem from "effect/FileSystem";
4344
import * as Layer from "effect/Layer";
4445
import * as Option from "effect/Option";
4546
import * as Path from "effect/Path";
@@ -1011,6 +1012,228 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
10111012
}),
10121013
);
10131014

1015+
it.effect("captures and repeatedly rewinds non-Git conversations without losing run links", () =>
1016+
Effect.gen(function* () {
1017+
const orchestrator = yield* Orchestrator.OrchestratorV2;
1018+
const eventSink = yield* EventSink.EventSinkV2;
1019+
const outbox = yield* EffectOutbox.EffectOutboxV2;
1020+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1021+
const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
1022+
const fileSystem = yield* FileSystem.FileSystem;
1023+
const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-non-git-rewind-" });
1024+
const threadId = ThreadId.make("runtime-non-git-rewind");
1025+
const rollbackThread = vi.fn(
1026+
({ providerThread }: ProviderAdapter.ProviderAdapterV2RollbackThreadInput) =>
1027+
Effect.succeed({
1028+
providerThread: {
1029+
...providerThread,
1030+
nativeThreadRef: {
1031+
driver,
1032+
nativeId: "rewound-native-thread",
1033+
strength: "strong" as const,
1034+
},
1035+
},
1036+
}),
1037+
);
1038+
const sessionSpy = vi.spyOn(sessions, "open").mockReturnValue(
1039+
Effect.succeed({
1040+
rollbackThread,
1041+
} as unknown as ProviderAdapter.ProviderAdapterV2SessionRuntime),
1042+
);
1043+
yield* Effect.addFinalizer(() => Effect.sync(() => sessionSpy.mockRestore()));
1044+
yield* orchestrator.dispatch({
1045+
type: "thread.create",
1046+
commandId: CommandId.make("non-git-create"),
1047+
createdBy: "user",
1048+
creationSource: "web",
1049+
threadId,
1050+
projectId: ProjectId.make("non-git-project"),
1051+
title: "Non-Git rewind",
1052+
modelSelection,
1053+
runtimeMode: "full-access",
1054+
interactionMode: "default",
1055+
branch: null,
1056+
worktreePath: cwd,
1057+
});
1058+
1059+
const completeMessage = (name: string) =>
1060+
Effect.gen(function* () {
1061+
yield* orchestrator.dispatch({
1062+
type: "message.dispatch",
1063+
commandId: CommandId.make(name),
1064+
createdBy: "user",
1065+
creationSource: "web",
1066+
threadId,
1067+
messageId: MessageId.make(name),
1068+
text: name,
1069+
attachments: [],
1070+
dispatchMode: { type: "start_immediately" },
1071+
});
1072+
const projection = yield* orchestrator.getThreadProjection(threadId);
1073+
const run = projection.runs.find((candidate) => candidate.userMessageId === name)!;
1074+
const node = projection.nodes.find((candidate) => candidate.id === run.rootNodeId)!;
1075+
const providerThread = projection.providerThreads.find(
1076+
(candidate) => candidate.id === run.providerThreadId,
1077+
)!;
1078+
const now = yield* DateTime.now;
1079+
const commandId = CommandId.make(`${name}-complete`);
1080+
// The provider completion is the fixture boundary; capture and rollback use real effects.
1081+
yield* outbox.cancelUnsettled({
1082+
threadId,
1083+
effectTypes: ["provider-turn.start"],
1084+
reason: "provider completion supplied by fixture",
1085+
});
1086+
yield* eventSink.writeWithEffects({
1087+
commandId,
1088+
events: [
1089+
{
1090+
id: EventId.make(`${name}-run`),
1091+
type: "run.updated",
1092+
threadId,
1093+
runId: run.id,
1094+
occurredAt: now,
1095+
payload: { ...run, status: "waiting", startedAt: now },
1096+
},
1097+
{
1098+
id: EventId.make(`${name}-node`),
1099+
type: "node.updated",
1100+
threadId,
1101+
runId: run.id,
1102+
nodeId: node.id,
1103+
occurredAt: now,
1104+
payload: { ...node, status: "completed", completedAt: now },
1105+
},
1106+
{
1107+
id: EventId.make(`${name}-turn`),
1108+
type: "provider-turn.updated",
1109+
threadId,
1110+
runId: run.id,
1111+
occurredAt: now,
1112+
payload: {
1113+
id: ProviderTurnId.make(`${name}-turn`),
1114+
providerThreadId: providerThread.id,
1115+
nodeId: node.id,
1116+
runAttemptId: run.activeAttemptId,
1117+
nativeTurnRef: null,
1118+
ordinal: run.ordinal,
1119+
status: "completed",
1120+
startedAt: now,
1121+
completedAt: now,
1122+
},
1123+
},
1124+
],
1125+
effects: [
1126+
{
1127+
id: `${name}-capture`,
1128+
commandId,
1129+
threadId,
1130+
request: {
1131+
type: "checkpoint.capture",
1132+
runId: run.id,
1133+
scopeId: node.checkpointScopeId!,
1134+
},
1135+
},
1136+
],
1137+
});
1138+
yield* worker.drain();
1139+
const captured = yield* orchestrator.getThreadProjection(threadId);
1140+
assert.equal(
1141+
captured.runs.find((candidate) => candidate.id === run.id)?.status,
1142+
"completed",
1143+
);
1144+
const checkpoint = captured.checkpoints.find((candidate) => candidate.runId === run.id)!;
1145+
assert.equal(checkpoint.status, "missing");
1146+
return { run, checkpoint };
1147+
});
1148+
1149+
const first = yield* completeMessage("non-git-first");
1150+
const second = yield* completeMessage("non-git-second");
1151+
const before = yield* orchestrator.getThreadProjection(threadId);
1152+
assert.equal(
1153+
before.checkpoints.find((checkpoint) => checkpoint.id === first.checkpoint.id)?.runId,
1154+
first.run.id,
1155+
"capturing a later run must preserve the earlier checkpoint's run link",
1156+
);
1157+
const baseline = before.checkpoints.find(
1158+
(checkpoint) => checkpoint.ordinalWithinScope === 0,
1159+
)!;
1160+
assert.equal(baseline.status, "missing");
1161+
for (const checkpoint of [baseline, first.checkpoint]) {
1162+
for (const restoreFiles of [undefined, true]) {
1163+
const commandId = CommandId.make(`non-git-files-${checkpoint.id}-${restoreFiles}`);
1164+
yield* orchestrator
1165+
.dispatch({
1166+
type: "checkpoint.rollback",
1167+
commandId,
1168+
threadId,
1169+
checkpointId: checkpoint.id,
1170+
scopeId: checkpoint.scopeId,
1171+
...(restoreFiles === undefined ? {} : { restoreFiles }),
1172+
})
1173+
.pipe(Effect.flip);
1174+
assert.deepEqual(yield* outbox.listByCommandId(commandId), []);
1175+
}
1176+
}
1177+
assert.equal(rollbackThread.mock.calls.length, 0);
1178+
1179+
const rewind = (name: string) =>
1180+
Effect.gen(function* () {
1181+
const commandId = CommandId.make(name);
1182+
yield* orchestrator.dispatch({
1183+
type: "checkpoint.rollback",
1184+
commandId,
1185+
threadId,
1186+
checkpointId: baseline.id,
1187+
scopeId: baseline.scopeId,
1188+
restoreFiles: false,
1189+
});
1190+
yield* worker.drain();
1191+
assert.deepEqual(
1192+
(yield* outbox.listByCommandId(commandId)).map((effect) => effect.status),
1193+
["succeeded"],
1194+
);
1195+
const projection = yield* orchestrator.getThreadProjection(threadId);
1196+
assert.ok(projection.runs.every((run) => run.status === "rolled_back"));
1197+
assert.isNull(projection.providerThreads[0]!.lastRunOrdinal);
1198+
assert.equal(
1199+
projection.providerThreads[0]!.nativeThreadRef?.nativeId,
1200+
"rewound-native-thread",
1201+
);
1202+
assert.isNull(projection.thread.rollbackFailure);
1203+
});
1204+
1205+
yield* rewind("non-git-rewind-first");
1206+
assert.equal(rollbackThread.mock.calls[0]![0].target.type, "thread_start");
1207+
assert.deepEqual(
1208+
rollbackThread.mock.calls[0]![0].providerThreadTurns.map((turn) => turn.id),
1209+
[ProviderTurnId.make("non-git-first-turn"), ProviderTurnId.make("non-git-second-turn")],
1210+
);
1211+
const discardedCommandId = CommandId.make("non-git-discarded-target");
1212+
yield* orchestrator
1213+
.dispatch({
1214+
type: "checkpoint.rollback",
1215+
commandId: discardedCommandId,
1216+
threadId,
1217+
checkpointId: second.checkpoint.id,
1218+
scopeId: second.checkpoint.scopeId,
1219+
restoreFiles: false,
1220+
})
1221+
.pipe(Effect.flip);
1222+
assert.deepEqual(yield* outbox.listByCommandId(discardedCommandId), []);
1223+
assert.equal(rollbackThread.mock.calls.length, 1);
1224+
1225+
const resent = yield* completeMessage("non-git-resent");
1226+
assert.isAbove(resent.run.ordinal, second.run.ordinal);
1227+
yield* rewind("non-git-rewind-again");
1228+
assert.equal(rollbackThread.mock.calls.length, 2);
1229+
assert.equal(rollbackThread.mock.calls[1]![0].target.type, "thread_start");
1230+
assert.deepEqual(
1231+
rollbackThread.mock.calls[1]![0].providerThreadTurns.map((turn) => turn.id),
1232+
[ProviderTurnId.make("non-git-resent-turn")],
1233+
);
1234+
}).pipe(Effect.provide(Layer.merge(Layer.fresh(layerTest), NodeServices.layer))),
1235+
);
1236+
10141237
it.effect("rejects non-ready rollback targets before persisting events or effects", () =>
10151238
Effect.gen(function* () {
10161239
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)