Skip to content

Commit d3ff1ef

Browse files
fix(server): a restart into a turn that ended before its run settled starts as a new turn
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 14cd7cd commit d3ff1ef

2 files changed

Lines changed: 63 additions & 22 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6079,7 +6079,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
60796079
(dispatchMode.type === "restart_active" && !isActiveRun(target)) ||
60806080
((target.status === "running" || target.status === "waiting") &&
60816081
turn !== undefined &&
6082-
turn.status === "completed"))
6082+
(turn.status === "completed" ||
6083+
(dispatchMode.type === "restart_active" &&
6084+
turn.status !== "pending" &&
6085+
turn.status !== "running"))))
60836086
) {
60846087
dispatchMode = { type: "start_immediately" };
60856088
}

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,48 +1816,70 @@ it.effect("starts a restart sent into a run that Stop reached once the stop sett
18161816
);
18171817

18181818
// regression: #32891 a restart that arrives after the turn ended is lost
1819-
it.effect.each(["completed", "interrupted"] as const)(
1820-
"starts a restart that missed the turn (%s) as a new turn",
1821-
(status) =>
1819+
it.effect.each([
1820+
{ status: "completed", settled: true, starts: true },
1821+
{ status: "interrupted", settled: true, starts: true },
1822+
{ status: "interrupted", settled: false, starts: true },
1823+
// A provider failure holds the queue, so the restart waits like any queued message.
1824+
{ status: "failed", settled: false, starts: false },
1825+
{ status: "cancelled", settled: false, starts: true },
1826+
] as const)(
1827+
"keeps a restart that missed the turn ($status, run settled: $settled) as a new turn",
1828+
({ status, settled: runSettled, starts }) =>
18221829
Effect.scoped(
18231830
Effect.gen(function* () {
18241831
const { events, started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
1825-
`restart-after-${status}`,
1832+
`restart-after-${status}-${runSettled ? "settled" : "unsettled"}`,
18261833
);
18271834
yield* Effect.gen(function* () {
18281835
const orchestrator = yield* Orchestrator.OrchestratorV2;
18291836
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
18301837
const threadId = yield* startFirstTurn;
18311838
const first = started[0]!;
1832-
const settled = yield* orchestrator.streamDomainEvents.pipe(
1839+
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
1840+
const turnEnded = yield* orchestrator.streamDomainEvents.pipe(
18331841
Stream.filter(
18341842
(event) =>
1835-
event.type === "run.updated" &&
1836-
event.payload.id === first.runId &&
1837-
event.payload.status === (status === "completed" ? "waiting" : "interrupted"),
1843+
event.type === "provider-turn.updated" &&
1844+
event.payload.id === turn.id &&
1845+
event.payload.status === status,
18381846
),
18391847
Stream.take(1),
18401848
Stream.runDrain,
18411849
Effect.forkScoped,
18421850
);
1843-
const turn = (yield* orchestrator.getThreadProjection(threadId)).providerTurns[0]!;
18441851
yield* Queue.offer(events, {
18451852
type: "provider_turn.updated",
18461853
driver,
18471854
providerTurn: { ...turn, status, completedAt: yield* DateTime.now },
18481855
});
1849-
yield* Queue.offer(events, {
1850-
type: "turn.terminal",
1851-
driver,
1852-
providerThreadId: turn.providerThreadId,
1853-
providerTurnId: turn.id,
1854-
runOrdinal: first.runOrdinal,
1855-
status,
1856-
failure: null,
1857-
threadDisposition: "reusable",
1856+
yield* Fiber.join(turnEnded);
1857+
const settleRun = Effect.gen(function* () {
1858+
const settled = yield* orchestrator.streamDomainEvents.pipe(
1859+
Stream.filter(
1860+
(event) =>
1861+
event.type === "run.updated" &&
1862+
event.payload.id === first.runId &&
1863+
event.payload.status !== "running",
1864+
),
1865+
Stream.take(1),
1866+
Stream.runDrain,
1867+
Effect.forkScoped,
1868+
);
1869+
yield* Queue.offer(events, {
1870+
type: "turn.terminal",
1871+
driver,
1872+
providerThreadId: turn.providerThreadId,
1873+
providerTurnId: turn.id,
1874+
runOrdinal: first.runOrdinal,
1875+
status,
1876+
failure: null,
1877+
threadDisposition: "reusable",
1878+
});
1879+
yield* Fiber.join(settled);
1880+
yield* worker.drain();
18581881
});
1859-
yield* Fiber.join(settled);
1860-
yield* worker.drain();
1882+
if (runSettled) yield* settleRun;
18611883

18621884
yield* orchestrator.dispatch({
18631885
type: "message.dispatch",
@@ -1872,11 +1894,27 @@ it.effect.each(["completed", "interrupted"] as const)(
18721894
creationSource: "web",
18731895
});
18741896
yield* worker.drain();
1897+
if (!runSettled) {
1898+
const restartLeftQueue = yield* orchestrator.streamDomainEvents.pipe(
1899+
Stream.filter(
1900+
(event) =>
1901+
event.type === "run.updated" &&
1902+
event.payload.userMessageId === "message:restart" &&
1903+
(starts ? event.payload.status !== "queued" : event.payload.queueHeld === true),
1904+
),
1905+
Stream.take(1),
1906+
Stream.runDrain,
1907+
Effect.forkScoped,
1908+
);
1909+
yield* settleRun;
1910+
yield* Fiber.join(restartLeftQueue);
1911+
yield* worker.drain();
1912+
}
18751913

18761914
assert.deepEqual(steered, []);
18771915
assert.deepEqual(
18781916
started.map((turn) => turn.message.text),
1879-
["first", "restart"],
1917+
starts ? ["first", "restart"] : ["first"],
18801918
);
18811919
}).pipe(Effect.provide(layer));
18821920
}),

0 commit comments

Comments
 (0)