Skip to content

Commit 66fa3fe

Browse files
navidnadaliclaude
andauthored
fix(server): a late checkpoint keeps the stopped run's baselines (pingdotgg#30830)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 3ca53a9 commit 66fa3fe

4 files changed

Lines changed: 217 additions & 12 deletions

File tree

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

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,204 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
477477
}),
478478
);
479479

480+
// A stopped run is captured after it ends; by then the next root run, dispatched or queued, may
481+
// have re-created the thread's one root scope with its own node and time.
482+
it.effect.each([1, 2])(
483+
"attributes the baselines a late capture materializes to the stopped run %i",
484+
(ordinal) =>
485+
Effect.gen(function* () {
486+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
487+
const requestedAt = yield* DateTime.now;
488+
const stoppedAt = DateTime.add(requestedAt, { seconds: 1 });
489+
const rescopedAt = DateTime.add(requestedAt, { seconds: 2 });
490+
const lateThreadId = ThreadId.make(`thread:checkpoint-capture-late-${ordinal}`);
491+
const lateRunId = RunId.make(`run:checkpoint-capture-late-${ordinal}`);
492+
const lateRootNodeId = NodeId.make(`node:checkpoint-capture-late-${ordinal}-root`);
493+
const lateScopeId = CheckpointScopeId.make(`scope:checkpoint-capture-late-${ordinal}`);
494+
const lateProviderThreadId = ProviderThreadId.make(
495+
`provider-thread:checkpoint-capture-late-${ordinal}`,
496+
);
497+
yield* projectionStore.apply({
498+
id: EventId.make(`event:checkpoint-capture-late-${ordinal}:thread`),
499+
type: "thread.created",
500+
threadId: lateThreadId,
501+
occurredAt: requestedAt,
502+
payload: {
503+
createdBy: "user",
504+
creationSource: "web",
505+
id: lateThreadId,
506+
projectId,
507+
title: "Checkpoint capture after the next run re-created the scope",
508+
providerInstanceId,
509+
modelSelection,
510+
runtimeMode: "full-access",
511+
interactionMode: "default",
512+
branch: null,
513+
worktreePath: null,
514+
activeProviderThreadId: null,
515+
lineage: {
516+
parentThreadId: null,
517+
relationshipToParent: null,
518+
rootThreadId: lateThreadId,
519+
},
520+
forkedFrom: null,
521+
createdAt: requestedAt,
522+
updatedAt: requestedAt,
523+
archivedAt: null,
524+
settledOverride: null,
525+
settledAt: null,
526+
lastVisitedAt: null,
527+
deletedAt: null,
528+
},
529+
});
530+
yield* projectionStore.apply({
531+
id: EventId.make(`event:checkpoint-capture-late-${ordinal}:run`),
532+
type: "run.updated",
533+
threadId: lateThreadId,
534+
runId: lateRunId,
535+
nodeId: lateRootNodeId,
536+
providerInstanceId,
537+
occurredAt: stoppedAt,
538+
payload: {
539+
id: lateRunId,
540+
threadId: lateThreadId,
541+
ordinal,
542+
providerInstanceId,
543+
modelSelection,
544+
providerThreadId: lateProviderThreadId,
545+
userMessageId: MessageId.make(`message:checkpoint-capture-late-${ordinal}`),
546+
rootNodeId: lateRootNodeId,
547+
activeAttemptId: null,
548+
status: "interrupted",
549+
requestedAt,
550+
startedAt: requestedAt,
551+
completedAt: stoppedAt,
552+
checkpointId: null,
553+
contextHandoffId: null,
554+
},
555+
});
556+
yield* projectionStore.apply({
557+
id: EventId.make(`event:checkpoint-capture-late-${ordinal}:node`),
558+
type: "node.updated",
559+
threadId: lateThreadId,
560+
runId: lateRunId,
561+
nodeId: lateRootNodeId,
562+
providerInstanceId,
563+
occurredAt: stoppedAt,
564+
payload: {
565+
id: lateRootNodeId,
566+
threadId: lateThreadId,
567+
runId: lateRunId,
568+
parentNodeId: null,
569+
rootNodeId: lateRootNodeId,
570+
kind: "root_turn",
571+
status: "interrupted",
572+
countsForRun: true,
573+
providerThreadId: lateProviderThreadId,
574+
providerTurnId: null,
575+
nativeItemRef: null,
576+
runtimeRequestId: null,
577+
checkpointScopeId: lateScopeId,
578+
startedAt: requestedAt,
579+
completedAt: stoppedAt,
580+
},
581+
});
582+
yield* projectionStore.apply({
583+
id: EventId.make(`event:checkpoint-capture-late-${ordinal}:provider-thread`),
584+
type: "provider-thread.updated",
585+
threadId: lateThreadId,
586+
nodeId: lateRootNodeId,
587+
driver,
588+
providerInstanceId,
589+
occurredAt: requestedAt,
590+
payload: {
591+
id: lateProviderThreadId,
592+
driver,
593+
providerInstanceId,
594+
providerSessionId: null,
595+
appThreadId: lateThreadId,
596+
ownerNodeId: lateRootNodeId,
597+
nativeThreadRef: null,
598+
nativeConversationHeadRef: null,
599+
status: "idle",
600+
firstRunOrdinal: 1,
601+
lastRunOrdinal: ordinal,
602+
handoffIds: [],
603+
forkedFrom: null,
604+
createdAt: requestedAt,
605+
updatedAt: requestedAt,
606+
},
607+
});
608+
const nextRunId = RunId.make(`run:checkpoint-capture-late-${ordinal}-next`);
609+
const nextRootNodeId = NodeId.make(`node:checkpoint-capture-late-${ordinal}-next-root`);
610+
yield* projectionStore.apply({
611+
id: EventId.make(`event:checkpoint-capture-late-${ordinal}:scope-of-next-run`),
612+
type: "checkpoint-scope.created",
613+
threadId: lateThreadId,
614+
runId: nextRunId,
615+
nodeId: nextRootNodeId,
616+
occurredAt: rescopedAt,
617+
payload: {
618+
id: lateScopeId,
619+
threadId: lateThreadId,
620+
runId: nextRunId,
621+
nodeId: nextRootNodeId,
622+
parentScopeId: null,
623+
providerThreadId: lateProviderThreadId,
624+
kind: "root_run",
625+
ordinalWithinParent: 0,
626+
advancesAppRunCount: true,
627+
cwd: "/repo",
628+
createdAt: rescopedAt,
629+
},
630+
});
631+
632+
const committed = yield* Ref.make<ReadonlyArray<OrchestrationV2DomainEvent>>([]);
633+
const layerCapture = CheckpointCaptureService.layer.pipe(
634+
Layer.provide(
635+
Layer.mergeAll(
636+
IdAllocator.layer,
637+
CheckpointService.layer.pipe(
638+
Layer.provide(
639+
Layer.mergeAll(
640+
IdAllocator.layer,
641+
NodeCrypto.layer,
642+
Layer.mock(CheckpointStore.CheckpointStore)({
643+
isGitRepository: () => Effect.succeed(true),
644+
hasCheckpointRef: () => Effect.succeed(true),
645+
captureCheckpoint: () => Effect.void,
646+
diffCheckpoints: () => Effect.succeed(""),
647+
listAuthoredPaths: () => Effect.succeed(null),
648+
}),
649+
),
650+
),
651+
),
652+
Layer.mock(EventSink.EventSinkV2)({
653+
commitCommand: (input) =>
654+
Ref.set(committed, input.events).pipe(Effect.as({ committed: true } as never)),
655+
}),
656+
),
657+
),
658+
);
659+
yield* CheckpointCaptureService.CheckpointCaptureServiceV2.use((service) =>
660+
service.execute({ threadId: lateThreadId, runId: lateRunId, scopeId: lateScopeId }),
661+
).pipe(Effect.provide(layerCapture));
662+
663+
const baselines = (yield* Ref.get(committed)).flatMap((event) =>
664+
event.type === "checkpoint.captured" && event.payload.runId === null ? [event] : [],
665+
);
666+
assert.deepEqual(
667+
baselines.map((event) => event.payload.ordinalWithinScope),
668+
ordinal === 1 ? [0] : [0, 1],
669+
);
670+
for (const baseline of baselines) {
671+
assert.equal(baseline.nodeId, lateRootNodeId);
672+
assert.equal(baseline.payload.nodeId, lateRootNodeId);
673+
assert.deepEqual(baseline.payload.capturedAt, requestedAt);
674+
}
675+
}),
676+
);
677+
480678
it.effect("records the checkpoint of a cancelled run and keeps it cancelled", () =>
481679
Effect.gen(function* () {
482680
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,23 @@ export const layer: Layer.Layer<
103103
const baselineOrdinalWithinScope = Math.max(0, run.ordinal - 1);
104104
const hasReadyCheckpoint = (ordinalWithinScope: number) =>
105105
readyCheckpointOrdinals.includes(ordinalWithinScope);
106+
// The baselines this capture materializes are attributed to the run materializing them. The
107+
// thread's scope may already be a later run's: one queued meanwhile, or sent after Stop.
108+
const materializeBaseline = (ordinalWithinScope: number) =>
109+
checkpoints.materializeBaselineCheckpoint({
110+
scope,
111+
ordinalWithinScope,
112+
nodeId: rootNode.id,
113+
capturedAt: run.requestedAt,
114+
forceMissing: sideChat,
115+
});
106116
const threadStartCheckpoint =
107117
baselineOrdinalWithinScope === 0 || hasReadyCheckpoint(0)
108118
? null
109-
: yield* checkpoints.materializeBaselineCheckpoint({
110-
scope,
111-
ordinalWithinScope: 0,
112-
forceMissing: sideChat,
113-
});
119+
: yield* materializeBaseline(0);
114120
const baselineCheckpoint = hasReadyCheckpoint(baselineOrdinalWithinScope)
115121
? null
116-
: yield* checkpoints.materializeBaselineCheckpoint({
117-
scope,
118-
ordinalWithinScope: baselineOrdinalWithinScope,
119-
forceMissing: sideChat,
120-
});
122+
: yield* materializeBaseline(baselineOrdinalWithinScope);
121123
const checkpoint = yield* checkpoints.capture({
122124
scope,
123125
runId: run.id,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ it.effect(
8787
const materialized = yield* checkpoints.materializeBaselineCheckpoint({
8888
scope,
8989
ordinalWithinScope: 0,
90+
nodeId: scope.nodeId,
91+
capturedAt: scope.createdAt,
9092
});
9193
assert.strictEqual(materialized.status, "missing");
9294
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export interface CheckpointServiceV2Shape {
118118
readonly materializeBaselineCheckpoint: (input: {
119119
readonly scope: OrchestrationV2CheckpointScope;
120120
readonly ordinalWithinScope: number;
121+
/** The run materializing the baseline: its root node, and when it was requested. */
122+
readonly nodeId: NodeId;
123+
readonly capturedAt: DateTime.Utc;
121124
/** Records a missing checkpoint without touching git, for side chats sharing a parent's worktree. */
122125
readonly forceMissing?: boolean;
123126
}) => Effect.Effect<OrchestrationV2Checkpoint, CheckpointServiceV2Error>;
@@ -324,14 +327,14 @@ export const layer: Layer.Layer<
324327
id: checkpointId,
325328
scope: input.scope,
326329
runId: null,
327-
nodeId: input.scope.nodeId,
330+
nodeId: input.nodeId,
328331
parentCheckpointId: null,
329332
ordinalWithinScope: input.ordinalWithinScope,
330333
appRunOrdinal: null,
331334
ref: checkpointRef,
332335
status: available ? "ready" : "missing",
333336
files: [],
334-
capturedAt: input.scope.createdAt,
337+
capturedAt: input.capturedAt,
335338
});
336339
}),
337340
).pipe(

0 commit comments

Comments
 (0)