Skip to content

Commit 8f72a73

Browse files
fix(server): the turn after a stopped turn shows its diff again (pingdotgg#17150)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 4c18faf commit 8f72a73

4 files changed

Lines changed: 130 additions & 17 deletions

File tree

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

Lines changed: 85 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,
@@ -200,6 +201,90 @@ it.effect("excludes ready checkpoints from rolled-back runs", () => {
200201
}).pipe(Effect.provide(layer));
201202
});
202203

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+
203288
it.effect("preserves the typed missing-baseline-ref error contract", () => {
204289
const projection = makeProjection();
205290
const layer = layerFor({

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ export const make = Effect.gen(function* () {
161161
});
162162
}
163163

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+
164176
const firstScope =
165177
input.fromTurnCount === 0
166178
? projection.checkpointScopes.find((scope) => scope.kind === "root_run")
@@ -173,8 +185,12 @@ export const make = Effect.gen(function* () {
173185
scopeId: firstScope.id,
174186
ordinalWithinScope: 0,
175187
}).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;
178194
if (fromCheckpointRef === undefined) {
179195
return yield* new CheckpointRefUnavailableError({
180196
operation,

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4184,6 +4184,11 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
41844184
() =>
41854185
Effect.gen(function* () {
41864186
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)]);
41874192
const sql = yield* SqlClient.SqlClient;
41884193
const now = yield* DateTime.now;
41894194
const threadId = ThreadId.make("thread:checkpoint-context");
@@ -4192,7 +4197,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
41924197
const scopeId = CheckpointScopeId.make("scope:checkpoint-context");
41934198
const checkpointId = CheckpointId.make("checkpoint:checkpoint-context");
41944199
const ref = CheckpointRef.make("refs/t3/checkpoint-context/1");
4195-
yield* projectionStore.apply({
4200+
yield* apply({
41964201
id: EventId.make("event:checkpoint-context:thread"),
41974202
type: "thread.created",
41984203
threadId,
@@ -4221,7 +4226,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
42214226
deletedAt: null,
42224227
},
42234228
});
4224-
yield* projectionStore.apply({
4229+
yield* apply({
42254230
id: EventId.make("event:checkpoint-context:run"),
42264231
type: "run.created",
42274232
threadId,
@@ -4244,7 +4249,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
42444249
contextHandoffId: null,
42454250
},
42464251
});
4247-
yield* projectionStore.apply({
4252+
yield* apply({
42484253
id: EventId.make("event:checkpoint-context:scope"),
42494254
type: "checkpoint-scope.created",
42504255
threadId,
@@ -4263,7 +4268,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
42634268
createdAt: now,
42644269
},
42654270
});
4266-
yield* projectionStore.apply({
4271+
yield* apply({
42674272
id: EventId.make("event:checkpoint-context:checkpoint"),
42684273
type: "checkpoint.captured",
42694274
threadId,
@@ -4302,15 +4307,19 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
43024307
SET payload_json = json_set(payload_json, '$.files', 'obsolete file summary')
43034308
WHERE checkpoint_id = ${checkpointId}
43044309
`;
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+
}
43144323
}),
43154324
);
43164325

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ const ProjectionCheckpointContext = Schema.Struct({
220220
),
221221
checkpoints: Schema.Array(
222222
OrchestrationV2CheckpointJsonSchema.mapFields(
223-
({ scopeId, runId, appRunOrdinal, status, ref }) => ({
223+
({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
224224
scopeId,
225225
runId,
226+
ordinalWithinScope,
226227
appRunOrdinal,
227228
status,
228229
ref,
@@ -4732,6 +4733,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
47324733
`,
47334734
sql`
47344735
SELECT scope_id AS "scopeId", run_id AS "runId",
4736+
ordinal_within_scope AS "ordinalWithinScope",
47354737
app_run_ordinal AS "appRunOrdinal", status,
47364738
json_extract(payload_json, '$.ref') AS ref
47374739
FROM orchestration_v2_projection_checkpoints
@@ -6594,9 +6596,10 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
65946596
cwd,
65956597
})),
65966598
checkpoints: projection.checkpoints.map(
6597-
({ scopeId, runId, appRunOrdinal, status, ref }) => ({
6599+
({ scopeId, runId, ordinalWithinScope, appRunOrdinal, status, ref }) => ({
65986600
scopeId,
65996601
runId,
6602+
ordinalWithinScope,
66006603
appRunOrdinal,
66016604
status,
66026605
ref,

0 commit comments

Comments
 (0)