Skip to content

Commit aa8bf8b

Browse files
committed
fix(server): the turn after a stopped turn shows its diff again
Resolve a single-turn diff's start from the end checkpoint's parent: the previous ordinal in the same scope. A stopped run either captured no checkpoint, leaving only the next run's baseline with no run or turn count, or kept a non-completed status, so the turn-count lookup rejected both. Fixes #17133
1 parent 0647c48 commit aa8bf8b

4 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎apps/server/src/checkpointing/CheckpointDiffQuery.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function makeProjection(): ProjectionCheckpointContext {
3737
{
3838
scopeId: secondScopeId,
3939
runId: secondRunId,
40+
ordinalWithinScope: 1,
4041
appRunOrdinal: 2,
4142
status: "ready",
4243
ref: secondRef,
@@ -170,6 +171,64 @@ it.effect("excludes ready checkpoints from rolled-back runs", () => {
170171
}).pipe(Effect.provide(layer));
171172
});
172173

174+
// Run 2 was stopped. Run 3 shares the root scope, and its parent checkpoint at
175+
// scope ordinal 2 is either run 2's own capture or the baseline run 3 recorded.
176+
function makeProjectionAfterStoppedRun(stoppedRunCheckpoint: boolean) {
177+
const thirdRunId = RunId.make("run:checkpoint-diff-v2:3");
178+
const checkpoint = (
179+
ordinalWithinScope: number,
180+
runId: RunId | null,
181+
): ProjectionCheckpointContext["checkpoints"][number] => ({
182+
scopeId: firstScopeId,
183+
runId,
184+
ordinalWithinScope,
185+
appRunOrdinal: runId === null ? null : ordinalWithinScope,
186+
status: "ready",
187+
ref: CheckpointRef.make(`refs/t3/test/ordinal/${ordinalWithinScope}`),
188+
});
189+
return {
190+
runs: [
191+
{ id: firstRunId, ordinal: 1, status: "completed" },
192+
{ id: secondRunId, ordinal: 2, status: "interrupted" },
193+
{ id: thirdRunId, ordinal: 3, status: "completed" },
194+
],
195+
checkpointScopes: [{ id: firstScopeId, runId: thirdRunId, kind: "root_run", cwd: "/repo" }],
196+
checkpoints: [
197+
checkpoint(0, null),
198+
checkpoint(1, firstRunId),
199+
checkpoint(2, stoppedRunCheckpoint ? secondRunId : null),
200+
checkpoint(3, thirdRunId),
201+
],
202+
} satisfies ProjectionCheckpointContext;
203+
}
204+
205+
it.effect.each([false, true])(
206+
"diffs the turn after a stopped run from its parent checkpoint (stopped run captured: %s)",
207+
(stoppedRunCheckpoint) => {
208+
const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
209+
Effect.succeed("diff --git a/file b/file"),
210+
);
211+
const layer = layerFor({
212+
projection: Effect.succeed(makeProjectionAfterStoppedRun(stoppedRunCheckpoint)),
213+
diffCheckpoints,
214+
});
215+
216+
return Effect.gen(function* () {
217+
const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
218+
const result = yield* query.getTurnDiff({ threadId, fromTurnCount: 2, toTurnCount: 3 });
219+
220+
assert.strictEqual(result.diff, "diff --git a/file b/file");
221+
assert.deepEqual(
222+
{
223+
from: diffCheckpoints.mock.calls[0]?.[0].fromCheckpointRef,
224+
to: diffCheckpoints.mock.calls[0]?.[0].toCheckpointRef,
225+
},
226+
{ from: "refs/t3/test/ordinal/2", to: "refs/t3/test/ordinal/3" },
227+
);
228+
}).pipe(Effect.provide(layer));
229+
},
230+
);
231+
173232
it.effect("preserves the typed missing-baseline-ref error contract", () => {
174233
const projection = makeProjection();
175234
const layer = layerFor({

‎apps/server/src/checkpointing/CheckpointDiffQuery.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ export const make = Effect.gen(function* () {
157157
});
158158
}
159159

160+
// A single turn starts at the checkpoint its capture diffed against: the
161+
// previous ordinal in the same scope. That checkpoint may belong to a
162+
// stopped run or be the baseline left when the previous run captured none.
163+
const parentCheckpoint =
164+
input.fromTurnCount === input.toTurnCount - 1
165+
? projection.checkpoints.find(
166+
(checkpoint) =>
167+
checkpoint.status === "ready" &&
168+
checkpoint.scopeId === toCheckpoint.scopeId &&
169+
checkpoint.ordinalWithinScope === toCheckpoint.ordinalWithinScope - 1,
170+
)
171+
: undefined;
172+
160173
// The root scope is shared by every run in this thread. Its runId
161174
// tracks the latest owner, while ordinal zero stays the baseline.
162175
const firstScope =
@@ -171,8 +184,12 @@ export const make = Effect.gen(function* () {
171184
scopeId: firstScope.id,
172185
ordinalWithinScope: 0,
173186
}).pipe(Effect.provideService(Crypto.Crypto, crypto))
174-
: readyCheckpoints.find((checkpoint) => checkpoint.appRunOrdinal === input.fromTurnCount)
175-
?.ref;
187+
: (
188+
parentCheckpoint ??
189+
readyCheckpoints.find(
190+
(checkpoint) => checkpoint.appRunOrdinal === input.fromTurnCount,
191+
)
192+
)?.ref;
176193
if (fromCheckpointRef === undefined) {
177194
return yield* new CheckpointRefUnavailableError({
178195
operation,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2885,7 +2885,9 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
28852885
assert.deepEqual(yield* projectionStore.getCheckpointContext(threadId), {
28862886
runs: [{ id: runId, ordinal: 1, status: "completed" }],
28872887
checkpointScopes: [{ id: scopeId, runId, kind: "root_run", cwd: "/repo/worktree" }],
2888-
checkpoints: [{ scopeId, runId, appRunOrdinal: 1, status: "ready", ref }],
2888+
checkpoints: [
2889+
{ scopeId, runId, ordinalWithinScope: 1, appRunOrdinal: 1, status: "ready", ref },
2890+
],
28892891
});
28902892
const missing = yield* projectionStore
28912893
.getCheckpointContext(ThreadId.make("thread:checkpoint-context:missing"))

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ const ProjectionCheckpointContext = Schema.Struct({
208208
),
209209
checkpoints: Schema.Array(
210210
OrchestrationV2CheckpointJsonSchema.mapFields(
211-
({ scopeId, runId, appRunOrdinal, status, ref }) => ({
211+
({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
212212
scopeId,
213213
runId,
214+
ordinalWithinScope,
214215
appRunOrdinal,
215216
status,
216217
ref,
@@ -4480,6 +4481,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
44804481
`,
44814482
sql`
44824483
SELECT scope_id AS "scopeId", run_id AS "runId",
4484+
ordinal_within_scope AS "ordinalWithinScope",
44834485
app_run_ordinal AS "appRunOrdinal", status,
44844486
json_extract(payload_json, '$.ref') AS ref
44854487
FROM orchestration_v2_projection_checkpoints
@@ -6137,9 +6139,10 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
61376139
cwd,
61386140
})),
61396141
checkpoints: projection.checkpoints.map(
6140-
({ scopeId, runId, appRunOrdinal, status, ref }) => ({
6142+
({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
61416143
scopeId,
61426144
runId,
6145+
ordinalWithinScope,
61436146
appRunOrdinal,
61446147
status,
61456148
ref,

0 commit comments

Comments
 (0)