Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions apps/server/src/orchestration-v2/CheckpointCaptureService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ export const layer: Layer.Layer<
) {
return;
}
// A Stop accepted once the provider's turn had ended stored only its request: the
// terminal already wrote `waiting`. That Stop still wins, and gets its result here.
const stopSignals = stoppedNow
// A Stop accepted before the turn ended can lose the race to it: the terminal wrote
// `completed` (or legacy `waiting`) with only the request stored. That Stop still wins here.
const stopped = currentRun.status === "interrupted" || currentRun.status === "cancelled";
const stopSignals = stopped
? []
: (yield* projections.getThreadRecords(input.threadId, ["turnItems"], {
turnItemTypes: ["run_interrupt_request", "run_interrupt_result"],
Expand All @@ -274,6 +275,9 @@ export const layer: Layer.Layer<
: stopSignals.find((item) => item.type === "run_interrupt_request");
const settledStatus =
stopRequest === undefined ? ("completed" as const) : ("interrupted" as const);
const settles = legacyWaiting || stopRequest !== undefined;
// Keep the turn-end time on a settled run; only legacy `waiting` rows settle at capture.
const settledAt = legacyWaiting ? capturedAt : (currentRun.completedAt ?? capturedAt);
// Omit delegatedCompletion to preserve cohort writes made after capture loaded the run.
const { delegatedCompletion: _delegatedCompletion, ...runWithoutDelegatedCompletion } =
currentRun;
Expand Down Expand Up @@ -372,16 +376,16 @@ export const layer: Layer.Layer<
nodeId: rootNode.id,
providerInstanceId: run.providerInstanceId,
occurredAt: capturedAt,
payload: legacyWaiting
payload: settles
? {
...runWithoutDelegatedCompletion,
status: settledStatus,
completedAt: capturedAt,
completedAt: settledAt,
checkpointId: checkpoint.id,
}
: { ...runWithoutDelegatedCompletion, checkpointId: checkpoint.id },
},
...(!legacyWaiting
...(!settles
? []
: [
{
Expand All @@ -395,7 +399,7 @@ export const layer: Layer.Layer<
payload: {
...rootNode,
status: settledStatus,
completedAt: capturedAt,
completedAt: settledAt,
checkpointScopeId: scope.id,
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3473,12 +3473,12 @@ it.effect(
});
const draining = yield* worker.drain().pipe(Effect.forkScoped);
yield* Deferred.await(interruptReached);
const waiting = yield* orchestrator.streamDomainEvents.pipe(
const completed = yield* orchestrator.streamDomainEvents.pipe(
Stream.filter(
(event) =>
event.type === "run.updated" &&
event.payload.id === first.runId &&
event.payload.status === "waiting",
event.payload.status === "completed",
),
Stream.take(1),
Stream.runDrain,
Expand All @@ -3500,7 +3500,7 @@ it.effect(
failure: null,
threadDisposition: "reusable",
});
yield* Fiber.join(waiting);
yield* Fiber.join(completed);
yield* Deferred.succeed(releaseInterrupt, undefined);
yield* Fiber.join(draining);
yield* worker.drain();
Expand Down
Loading