The best way to control Claude Code, Codex and more from any computer or phone
fix(server): the turn after a stopped turn shows its diff again (#17150)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Ibodullayev Ruslan committed
Oct 20, 2026 at 01:25 UTC
8f72a73b6e4136a5288ed036642ffb6465fd3a78
4 files changed
+130
-17
apps/server/src/checkpointing/CheckpointDiffQuery.test.ts
+85
@@ -37,6 +37,7 @@ function makeProjection(): ProjectionCheckpointContext {
37
{
38
scopeId: secondScopeId,
39
runId: secondRunId,
40
+ ordinalWithinScope: 1,
41
appRunOrdinal: 2,
42
status: "ready",
43
ref: secondRef,
@@ -200,6 +201,90 @@ it.effect("excludes ready checkpoints from rolled-back runs", () => {
201
}).pipe(Effect.provide(layer));
202
});
203
204
+function makeProjectionAfterStoppedRun(stoppedRunCheckpoint: boolean) {
205
+ const thirdRunId = RunId.make("run:checkpoint-diff-v2:3");
206
+ const checkpoint = (
207
+ ordinalWithinScope: number,
208
+ runId: RunId | null,
209
+ ): ProjectionCheckpointContext["checkpoints"][number] => ({
210
+ scopeId: firstScopeId,
211
+ runId,
212
+ ordinalWithinScope,
213
+ appRunOrdinal: runId === null ? null : ordinalWithinScope,
214
+ status: "ready",
215
+ ref: CheckpointRef.make(`refs/t3/test/ordinal/${ordinalWithinScope}`),
216
+ });
217
+ return {
218
+ runs: [
219
+ { id: firstRunId, ordinal: 1, status: "completed" },
220
+ { id: secondRunId, ordinal: 2, status: "interrupted" },
221
+ { id: thirdRunId, ordinal: 3, status: "completed" },
222
+ ],
223
+ checkpointScopes: [
224
+ { id: firstScopeId, runId: thirdRunId, kind: "root_run", cwd: "/repo" },
225
+ { id: secondScopeId, runId: secondRunId, kind: "tool", cwd: "/repo" },
226
+ ],
227
+ checkpoints: [
228
+ {
229
+ ...checkpoint(2, null),
230
+ scopeId: secondScopeId,
231
+ ref: CheckpointRef.make("refs/t3/test/tool/2"),
232
+ },
233
+ checkpoint(0, null),
234
+ checkpoint(1, firstRunId),
235
+ checkpoint(2, stoppedRunCheckpoint ? secondRunId : null),
236
+ checkpoint(3, thirdRunId),
237
+ ],
238
+ } satisfies ProjectionCheckpointContext;
239
+}
240
+
241
+it.effect.each([false, true])(
242
+ "diffs the turn after a stopped run from its parent checkpoint (stopped run captured: %s)",
243
+ (stoppedRunCheckpoint) => {
244
+ const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
245
+ Effect.succeed("diff --git a/file b/file"),
246
+ );
247
+ const layer = layerFor({
248
+ projection: Effect.succeed(makeProjectionAfterStoppedRun(stoppedRunCheckpoint)),
249
+ diffCheckpoints,
250
+ });
251
+
252
+ return Effect.gen(function* () {
253
+ const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
254
+ const result = yield* query.getTurnDiff({ threadId, fromTurnCount: 2, toTurnCount: 3 });
255
+
256
+ assert.strictEqual(result.diff, "diff --git a/file b/file");
257
+ yield* query.getTurnDiff({ threadId, fromTurnCount: 1, toTurnCount: 3 });
258
+ yield* query.getFullThreadDiff({ threadId, toTurnCount: 3 });
259
+ assert.deepEqual(
260
+ diffCheckpoints.mock.calls.map(([input]) => ({
261
+ from: input.fromCheckpointRef,
262
+ to: input.toCheckpointRef,
263
+ })),
264
+ [
265
+ { from: "refs/t3/test/ordinal/2", to: "refs/t3/test/ordinal/3" },
266
+ { from: "refs/t3/test/ordinal/1", to: "refs/t3/test/ordinal/3" },
267
+ {
268
+ from: yield* checkpointRefForScopeOrdinal({
269
+ scopeId: firstScopeId,
270
+ ordinalWithinScope: 0,
271
+ }),
272
+ to: "refs/t3/test/ordinal/3",
273
+ },
274
+ ],
275
+ );
276
+ const stoppedTurnError = yield* query
277
+ .getTurnDiff({ threadId, fromTurnCount: 1, toTurnCount: 2 })
278
+ .pipe(Effect.flip);
279
+ assert.instanceOf(stoppedTurnError, CheckpointRefUnavailableError);
280
+ assert.deepEqual(
281
+ { checkpoint: stoppedTurnError.checkpoint, turnCount: stoppedTurnError.turnCount },
282
+ { checkpoint: "to", turnCount: 2 },
283
+ );
284
+ }).pipe(Effect.provide(layer));
285
+ },
286
+);
287
+
288
it.effect("preserves the typed missing-baseline-ref error contract", () => {
289
const projection = makeProjection();
290
const layer = layerFor({
apps/server/src/checkpointing/CheckpointDiffQuery.ts
+18
-2
@@ -161,6 +161,18 @@ export const make = Effect.gen(function* () {
161
});
162
}
163
164
+ // Capture compares against the preceding scope ordinal, even when that
165
+ // checkpoint belongs to a stopped run or has no run.
166
+ const parentCheckpoint =
167
+ input.fromTurnCount === input.toTurnCount - 1
168
+ ? projection.checkpoints.find(
169
+ (checkpoint) =>
170
+ checkpoint.status === "ready" &&
171
+ checkpoint.scopeId === toCheckpoint.scopeId &&
172
+ checkpoint.ordinalWithinScope === toCheckpoint.ordinalWithinScope - 1,
173
+ )
174
+ : undefined;
175
+
176
const firstScope =
177
input.fromTurnCount === 0
178
? projection.checkpointScopes.find((scope) => scope.kind === "root_run")
@@ -173,8 +185,12 @@ export const make = Effect.gen(function* () {
185
scopeId: firstScope.id,
186
ordinalWithinScope: 0,
187
}).pipe(Effect.provideService(Crypto.Crypto, crypto))
176
- : readyCheckpoints.find((checkpoint) => checkpoint.appRunOrdinal === input.fromTurnCount)
177
- ?.ref;
188
+ : (
189
+ parentCheckpoint ??
190
+ readyCheckpoints.find(
191
+ (checkpoint) => checkpoint.appRunOrdinal === input.fromTurnCount,
192
+ )
193
+ )?.ref;
194
if (fromCheckpointRef === undefined) {
195
return yield* new CheckpointRefUnavailableError({
196
operation,
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+22
-13
@@ -4184,6 +4184,11 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4184
() =>
4185
Effect.gen(function* () {
4186
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
4187
+ const memoryStore = yield* ProjectionStore.ProjectionStoreV2.pipe(
4188
+ Effect.provide(ProjectionStore.layerMemory),
4189
+ );
4190
+ const apply = (event: Parameters<typeof projectionStore.apply>[0]) =>
4191
+ Effect.all([projectionStore.apply(event), memoryStore.apply(event)]);
4192
const sql = yield* SqlClient.SqlClient;
4193
const now = yield* DateTime.now;
4194
const threadId = ThreadId.make("thread:checkpoint-context");
@@ -4192,7 +4197,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4197
const scopeId = CheckpointScopeId.make("scope:checkpoint-context");
4198
const checkpointId = CheckpointId.make("checkpoint:checkpoint-context");
4199
const ref = CheckpointRef.make("refs/t3/checkpoint-context/1");
4195
- yield* projectionStore.apply({
4200
+ yield* apply({
4201
id: EventId.make("event:checkpoint-context:thread"),
4202
type: "thread.created",
4203
threadId,
@@ -4221,7 +4226,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4226
deletedAt: null,
4227
},
4228
});
4224
- yield* projectionStore.apply({
4229
+ yield* apply({
4230
id: EventId.make("event:checkpoint-context:run"),
4231
type: "run.created",
4232
threadId,
@@ -4244,7 +4249,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4249
contextHandoffId: null,
4250
},
4251
});
4247
- yield* projectionStore.apply({
4252
+ yield* apply({
4253
id: EventId.make("event:checkpoint-context:scope"),
4254
type: "checkpoint-scope.created",
4255
threadId,
@@ -4263,7 +4268,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4268
createdAt: now,
4269
},
4270
});
4266
- yield* projectionStore.apply({
4271
+ yield* apply({
4272
id: EventId.make("event:checkpoint-context:checkpoint"),
4273
type: "checkpoint.captured",
4274
threadId,
@@ -4302,15 +4307,19 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4307
SET payload_json = json_set(payload_json, '$.files', 'obsolete file summary')
4308
WHERE checkpoint_id = ${checkpointId}
4309
`;
4305
- assert.deepEqual(yield* projectionStore.getCheckpointContext(threadId), {
4306
- runs: [{ id: runId, ordinal: 1, status: "completed" }],
4307
- checkpointScopes: [{ id: scopeId, runId, kind: "root_run", cwd: "/repo/worktree" }],
4308
- checkpoints: [{ scopeId, runId, appRunOrdinal: 1, status: "ready", ref }],
4309
- });
4310
- const missing = yield* projectionStore
4311
- .getCheckpointContext(ThreadId.make("thread:checkpoint-context:missing"))
4312
- .pipe(Effect.flip);
4313
- assert.instanceOf(missing, ProjectionStore.ProjectionStoreThreadNotFoundError);
4310
+ for (const store of [projectionStore, memoryStore]) {
4311
+ assert.deepEqual(yield* store.getCheckpointContext(threadId), {
4312
+ runs: [{ id: runId, ordinal: 1, status: "completed" }],
4313
+ checkpointScopes: [{ id: scopeId, runId, kind: "root_run", cwd: "/repo/worktree" }],
4314
+ checkpoints: [
4315
+ { scopeId, runId, ordinalWithinScope: 1, appRunOrdinal: 1, status: "ready", ref },
4316
+ ],
4317
+ });
4318
+ const missing = yield* store
4319
+ .getCheckpointContext(ThreadId.make("thread:checkpoint-context:missing"))
4320
+ .pipe(Effect.flip);
4321
+ assert.instanceOf(missing, ProjectionStore.ProjectionStoreThreadNotFoundError);
4322
+ }
4323
}),
4324
);
4325
apps/server/src/orchestration-v2/ProjectionStore.ts
+5
-2
@@ -220,9 +220,10 @@ const ProjectionCheckpointContext = Schema.Struct({
220
),
221
checkpoints: Schema.Array(
222
OrchestrationV2CheckpointJsonSchema.mapFields(
223
- ({ scopeId, runId, appRunOrdinal, status, ref }) => ({
223
+ ({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
224
scopeId,
225
runId,
226
+ ordinalWithinScope,
227
appRunOrdinal,
228
status,
229
ref,
@@ -4732,6 +4733,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4733
`,
4734
sql`
4735
SELECT scope_id AS "scopeId", run_id AS "runId",
4736
+ ordinal_within_scope AS "ordinalWithinScope",
4737
app_run_ordinal AS "appRunOrdinal", status,
4738
json_extract(payload_json, '$.ref') AS ref
4739
FROM orchestration_v2_projection_checkpoints
@@ -6594,9 +6596,10 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
6596
cwd,
6597
})),
6598
checkpoints: projection.checkpoints.map(
6597
- ({ scopeId, runId, appRunOrdinal, status, ref }) => ({
6599
+ ({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
6600
scopeId,
6601
runId,
6602
+ ordinalWithinScope,
6603
appRunOrdinal,
6604
status,
6605
ref,