Skip to content

Commit 9a5a793

Browse files
vitalyiegorovclaude
andcommitted
fix(server): Stop reaches background work after a run fails before its provider starts
When the newest run failed before its provider started, it had no provider turn, so run.interrupt rejected it as not interruptible. Stop then could not end the background work the thread still showed. The interrupt now falls back to the provider thread's latest turn, whose session owns that work. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8fb068c commit 9a5a793

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ const instanceId = ProviderInstanceId.make("codex");
3939
const modelSelection = { instanceId, model: "test-model" };
4040

4141
// Codex turns leave commands running, then the thread moves to another
42-
// provider thread (a provider switch). Stop on the newer, settled run must
43-
// reach both provider threads and end all of the Codex work.
42+
// provider thread (a provider switch), and its newest run fails before its
43+
// provider starts. Stop on that run must reach both provider threads and end
44+
// all of the Codex work.
4445
it.effect("Stop reaches background work an earlier provider thread still runs", () =>
4546
Effect.scoped(
4647
Effect.gen(function* () {
@@ -387,6 +388,17 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
387388
});
388389
const otherProviderThreadId = ProviderThreadId.make("provider-thread:other");
389390
const latestRun = settledRun({ ordinal: 4, providerThreadId: otherProviderThreadId });
391+
// The newest run failed before its provider started, so it has no
392+
// provider turn of its own.
393+
const failedRun = settledRun({ ordinal: 5, providerThreadId: otherProviderThreadId });
394+
const failedRunEvents = failedRun.events.flatMap(
395+
(event): Array<OrchestrationV2DomainEvent> =>
396+
event.type === "provider-turn.updated"
397+
? []
398+
: event.type === "run.created"
399+
? [{ ...event, payload: { ...event.payload, status: "failed" } }]
400+
: [event],
401+
);
390402
yield* sink.write({
391403
events: [
392404
...watcherRun.events,
@@ -404,18 +416,20 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
404416
},
405417
},
406418
...latestRun.events,
419+
...failedRunEvents,
407420
],
408421
});
409422

410423
yield* orchestrator.dispatch({
411424
type: "run.interrupt",
412425
commandId: CommandId.make("stop-background-work"),
413426
threadId,
414-
runId: latestRun.runId,
427+
runId: failedRun.runId,
415428
});
416429
yield* worker.drain();
417430

418-
// Stop reaches both provider threads. The Codex one is interrupted at
431+
// Stop reaches both provider threads. The other one is interrupted at
432+
// its latest turn, since the failed run has none. The Codex one is at
419433
// its latest pending work, the subagent's parent turn, so its settle
420434
// covers all three Codex runs.
421435
assert.sameDeepMembers(

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8035,11 +8035,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
80358035
activeProviderThreadId: projection.thread.activeProviderThreadId,
80368036
runs: projection.runs,
80378037
}).length > 0;
8038-
const providerTurn = projection.providerTurns.findLast(
8039-
(candidate) =>
8040-
candidate.runAttemptId === run?.activeAttemptId &&
8041-
(candidate.status === "running" || hasBackgroundWork),
8042-
);
8038+
// A run that failed before its provider started has no turn of its own.
8039+
// Stopping its thread's background work goes through the provider
8040+
// thread's latest turn instead, whose session owns that work.
8041+
const providerTurn =
8042+
projection.providerTurns.findLast(
8043+
(candidate) =>
8044+
candidate.runAttemptId === run?.activeAttemptId &&
8045+
(candidate.status === "running" || hasBackgroundWork),
8046+
) ??
8047+
(hasBackgroundWork
8048+
? projection.providerTurns.findLast(
8049+
(candidate) => candidate.providerThreadId === run?.providerThreadId,
8050+
)
8051+
: undefined);
80438052
if (run === undefined || rootNode === undefined || providerThread === undefined) {
80448053
return yield* new OrchestratorDispatchError({
80458054
commandId: command.commandId,

0 commit comments

Comments
 (0)