The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 23, 01:45 UTC
fix(server): Stop no longer leaves a killed background command showing as running (#18163)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
spiky02plateau committed
Oct 19, 2026 at 22:27 UTC
eb0652067cd143592fcaffbdcf43c3c5c97c6e2c
2 files changed
+131
-31
apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.ts
+107
-27
@@ -38,10 +38,12 @@ const stopEarlierBackgroundWork = ({
38
failedStart = false,
39
stopWithQueue,
40
olderStart = false,
41
+ backgroundWorkAfterStop,
42
stalledRun,
43
workflowMember = false,
44
}: {
45
readonly failedStart?: boolean;
46
+ readonly backgroundWorkAfterStop?: "running" | "unknown";
47
readonly stopWithQueue?: "thread.stop" | "run.interrupt";
48
readonly olderStart?: boolean;
49
readonly workflowMember?: boolean;
@@ -83,6 +85,22 @@ const stopEarlierBackgroundWork = ({
85
lastError: null,
86
},
87
events: Stream.fromQueue(events),
88
+ ...(failedStart
89
+ ? {
90
+ hasPendingBackgroundWorkForThread: (providerThread) =>
91
+ Effect.gen(function* () {
92
+ if (
93
+ !interrupts.some((input) => input.providerThread.id === providerThread.id)
94
+ ) {
95
+ return false;
96
+ }
97
+ if (backgroundWorkAfterStop === "unknown") {
98
+ return yield* Effect.die("Background work probe failed");
99
+ }
100
+ return backgroundWorkAfterStop === "running";
101
+ }),
102
+ }
103
+ : {}),
104
ensureThread: ({ threadId }) =>
105
Effect.succeed({
106
id: ProviderThreadId.make(`provider-thread:codex:${threadId}`),
@@ -412,6 +430,10 @@ const stopEarlierBackgroundWork = ({
430
431
const codexProviderThread = (yield* orchestrator.getThreadProjection(threadId))
432
.providerThreads[0]!;
433
+ const backgroundTasks = [
434
+ { taskId: "sleep", description: "Wait before the recheck", kind: "command" as const },
435
+ ];
436
+ const otherBackgroundTasks = [{ ...backgroundTasks[0]!, taskId: "other-sleep" }];
437
const subagentThread = (name: string) =>
438
Effect.gen(function* () {
439
const subagentThreadId = ThreadId.make(`thread:${name}`);
@@ -735,40 +757,76 @@ const stopEarlierBackgroundWork = ({
757
id: otherProviderThreadId,
758
firstRunOrdinal: 4,
759
lastRunOrdinal: 4,
760
+ ...(failedStart ? { pendingBackgroundTasks: otherBackgroundTasks } : {}),
761
},
762
},
763
...latestRun.events,
764
],
765
});
766
744
- const failedRun = settledRun({ ordinal: 5, providerThreadId: otherProviderThreadId });
767
+ const failedRun = settledRun({ ordinal: 5, providerThreadId: codexProviderThread.id });
768
if (failedStart) {
769
yield* sink.write({
747
- events: failedRun.events.flatMap((event): Array<OrchestrationV2DomainEvent> => {
748
- switch (event.type) {
749
- case "provider-turn.updated":
750
- return [];
751
- case "run.created":
752
- return [{ ...event, payload: { ...event.payload, status: "failed" } }];
753
- case "node.updated":
754
- return [
755
- {
756
- ...event,
757
- payload: { ...event.payload, status: "failed", providerTurnId: null },
758
- },
759
- ];
760
- case "run-attempt.created":
761
- return [
762
- {
763
- ...event,
764
- payload: { ...event.payload, status: "failed", providerTurnId: null },
765
- },
766
- ];
767
- default:
768
- return [event];
769
- }
770
- }),
770
+ events: [
771
+ {
772
+ id: EventId.make("failed-start-roster"),
773
+ type: "provider-thread.updated",
774
+ threadId,
775
+ occurredAt: now,
776
+ payload: {
777
+ ...codexProviderThread,
778
+ lastRunOrdinal: 5,
779
+ pendingBackgroundTasks: backgroundTasks,
780
+ },
781
+ },
782
+ ...failedRun.events.flatMap((event): Array<OrchestrationV2DomainEvent> => {
783
+ switch (event.type) {
784
+ case "provider-turn.updated":
785
+ return [];
786
+ case "run.created":
787
+ return [{ ...event, payload: { ...event.payload, status: "failed" } }];
788
+ case "node.updated":
789
+ return [
790
+ {
791
+ ...event,
792
+ payload: { ...event.payload, status: "failed", providerTurnId: null },
793
+ },
794
+ ];
795
+ case "run-attempt.created":
796
+ return [
797
+ {
798
+ ...event,
799
+ payload: { ...event.payload, status: "failed", providerTurnId: null },
800
+ },
801
+ ];
802
+ default:
803
+ return [event];
804
+ }
805
+ }),
806
+ ],
807
});
808
+ // The older turn reports an empty roster, but the refused run owns the row now.
809
+ const clear = yield* sink.writeIfProviderThreadOwner({
810
+ providerThreadId: codexProviderThread.id,
811
+ runId: reviewerRun.runId,
812
+ activeAttemptId: RunAttemptId.make("attempt:3"),
813
+ expectedLastRunOrdinal: 3,
814
+ events: [
815
+ {
816
+ id: EventId.make("dropped-roster-clear"),
817
+ type: "provider-thread.updated",
818
+ threadId,
819
+ occurredAt: now,
820
+ payload: { ...codexProviderThread, pendingBackgroundTasks: [] },
821
+ },
822
+ ],
823
+ });
824
+ assert.isFalse(clear.committed);
825
+ const beforeStop = (yield* orchestrator.getThreadProjection(
826
+ threadId,
827
+ )).providerThreads.find((thread) => thread.id === codexProviderThread.id)!;
828
+ assert.equal(beforeStop.lastRunOrdinal, 5);
829
+ assert.deepEqual(beforeStop.pendingBackgroundTasks, backgroundTasks);
830
}
831
832
if (stopWithQueue !== undefined) {
@@ -864,7 +922,9 @@ const stopEarlierBackgroundWork = ({
922
assert.sameDeepMembers(
923
interrupts.map((interrupt) => [interrupt.providerThread.id, interrupt.providerTurnId]),
924
[
867
- ...(olderStart ? [] : [[otherProviderThreadId, latestRun.providerTurnId]]),
925
+ ...(olderStart || failedStart
926
+ ? []
927
+ : [[otherProviderThreadId, latestRun.providerTurnId]]),
928
[codexProviderThread.id, reviewerRun.providerTurnId],
929
],
930
);
@@ -906,6 +966,18 @@ const stopEarlierBackgroundWork = ({
966
}
967
const testerThread = yield* orchestrator.getThreadProjection(tester.threadId);
968
assert.equal(testerThread.nodes[0]?.status, "interrupted");
969
+ if (failedStart) {
970
+ assert.deepEqual(
971
+ after.providerThreads.find((candidate) => candidate.id === codexProviderThread.id)
972
+ ?.pendingBackgroundTasks,
973
+ backgroundWorkAfterStop === undefined ? [] : backgroundTasks,
974
+ );
975
+ assert.deepEqual(
976
+ after.providerThreads.find((candidate) => candidate.id === otherProviderThreadId)
977
+ ?.pendingBackgroundTasks,
978
+ otherBackgroundTasks,
979
+ );
980
+ }
981
}).pipe(
982
Effect.provide(
983
ProviderReplayHarness.layerWithRegistry(
@@ -927,10 +999,18 @@ it.effect("Stop settles workflow members without subagent turn items", () =>
999
);
1000
1001
it.effect(
930
- "Stop reaches earlier background work after the newest run fails before provider start",
1002
+ "Stop clears a stale roster after a refused run takes ownership of the provider thread",
1003
() => stopEarlierBackgroundWork({ failedStart: true }),
1004
);
1005
1006
+it.effect("Stop preserves the roster when background work survives the interrupt", () =>
1007
+ stopEarlierBackgroundWork({ failedStart: true, backgroundWorkAfterStop: "running" }),
1008
+);
1009
+
1010
+it.effect("Stop preserves the roster when the background work probe fails", () =>
1011
+ stopEarlierBackgroundWork({ failedStart: true, backgroundWorkAfterStop: "unknown" }),
1012
+);
1013
+
1014
it.effect.each(["thread.stop", "run.interrupt"] as const)(
1015
"%s stops background work when a later message is queued",
1016
(stopType) => stopEarlierBackgroundWork({ stopWithQueue: stopType }),
apps/server/src/orchestration-v2/Orchestrator.ts
+24
-4
@@ -8547,6 +8547,24 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8547
liveness.set(providerThreadId, live);
8548
return live;
8549
});
8550
+ const stillReportsBackgroundWork = Effect.fnUntraced(function* (
8551
+ providerThread: OrchestrationV2ProviderThread,
8552
+ ) {
8553
+ if (providerThread.providerSessionId == null) return false;
8554
+ const session = yield* providerSessions
8555
+ .get(providerThread.providerSessionId)
8556
+ .pipe(Effect.orElseSucceed(() => Option.none()));
8557
+ if (
8558
+ Option.isNone(session) ||
8559
+ session.value.hasPendingBackgroundWorkForThread === undefined
8560
+ ) {
8561
+ return false;
8562
+ }
8563
+ return yield* session.value
8564
+ .hasPendingBackgroundWorkForThread(providerThread)
8565
+ // A failed probe cannot confirm that the work ended.
8566
+ .pipe(Effect.catchCause(() => Effect.succeed(true)));
8567
+ });
8568
for (const item of pendingBackgroundTurnItems({
8569
turnItems: input.projection.turnItems,
8570
runs: input.projection.runs,
@@ -8649,10 +8667,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
8667
}
8668
}
8669
for (const providerThread of input.projection.providerThreads) {
8652
- if (
8653
- (providerThread.pendingBackgroundTasks?.length ?? 0) === 0 ||
8654
- (yield* hasLiveSession(providerThread.id))
8655
- ) {
8670
+ if ((providerThread.pendingBackgroundTasks?.length ?? 0) === 0) continue;
8671
+ if (providerThread.id === input.stoppedProviderThreadId) {
8672
+ // A refused newer start can take ownership and reject the stopped
8673
+ // turn's roster clear. Reconcile it without hiding surviving work.
8674
+ if (yield* stillReportsBackgroundWork(providerThread)) continue;
8675
+ } else if (yield* hasLiveSession(providerThread.id)) {
8676
continue;
8677
}
8678
yield* emitEvent({