Skip to content

Commit efecd3c

Browse files
authored
fix(server): Claude threads no longer get stuck after background commands (#15770)
1 parent 2188bdd commit efecd3c

9 files changed

Lines changed: 291 additions & 16 deletions

‎apps/server/src/claudeModelOptions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function compileClaudeModelSelection(
3737
const resolvedEffort = resolveClaudeCatalogEffort(catalog, selection.model, rawEffort);
3838
const effort = normalizeClaudeCatalogEffort(catalog, resolvedEffort, selection.model);
3939
const fastMode = supportsBoolean("fastMode")
40-
? getModelSelectionBooleanOptionValue(selection, "fastMode")
40+
? (getModelSelectionBooleanOptionValue(selection, "fastMode") ?? false)
4141
: undefined;
4242
const thinking = supportsBoolean("thinking")
4343
? getModelSelectionBooleanOptionValue(selection, "thinking")

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ function makeClaudeTestTurnInput(input: {
126126
readonly text: string;
127127
readonly attachments: ProviderAdapterV2TurnInput["message"]["attachments"];
128128
readonly providerTurnOrdinal?: number;
129+
readonly nativeThreadHasTurns?: boolean;
129130
readonly messageCreatedBy?: ProviderAdapterV2TurnInput["message"]["createdBy"];
130131
readonly messageCreationSource?: ProviderAdapterV2TurnInput["message"]["creationSource"];
131132
readonly modelSelection?: ModelSelection;
@@ -137,6 +138,9 @@ function makeClaudeTestTurnInput(input: {
137138
runId: RunId.make(`run-${input.attemptId}`),
138139
runOrdinal: 1,
139140
providerTurnOrdinal: input.providerTurnOrdinal ?? 1,
141+
...(input.nativeThreadHasTurns === undefined
142+
? {}
143+
: { nativeThreadHasTurns: input.nativeThreadHasTurns }),
140144
attemptId: input.attemptId,
141145
rootNodeId: NodeId.make(`node-${input.attemptId}`),
142146
providerThread: input.providerThread,
@@ -1783,7 +1787,7 @@ describe("ClaudeAdapterV2 native fork", () => {
17831787
});
17841788

17851789
describe("ClaudeAdapterV2 native session identity", () => {
1786-
const openTurnWithOrdinal = (providerTurnOrdinal: number) =>
1790+
const openTurnWithOrdinal = (providerTurnOrdinal: number, nativeThreadHasTurns?: boolean) =>
17871791
Effect.scoped(
17881792
Effect.gen(function* () {
17891793
const fileSystem = yield* FileSystem.FileSystem;
@@ -1842,6 +1846,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
18421846
text: "Respond with identity ok",
18431847
attachments: [],
18441848
providerTurnOrdinal,
1849+
...(nativeThreadHasTurns === undefined ? {} : { nativeThreadHasTurns }),
18451850
}),
18461851
);
18471852
return openedQueries;
@@ -1867,6 +1872,14 @@ describe("ClaudeAdapterV2 native session identity", () => {
18671872
assert.equal(openedQueries[0]?.options.sessionId, undefined);
18681873
}),
18691874
);
1875+
1876+
it.effect("creates a fresh native session despite earlier provider-thread turns", () =>
1877+
Effect.gen(function* () {
1878+
const openedQueries = yield* openTurnWithOrdinal(4, false);
1879+
assert.equal(openedQueries[0]?.options.sessionId, "native-session-identity");
1880+
assert.equal(openedQueries[0]?.options.resume, undefined);
1881+
}),
1882+
);
18701883
});
18711884

18721885
const encodeJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
@@ -2182,6 +2195,68 @@ describe("ClaudeAdapterV2 background wake turns", () => {
21822195
});
21832196
const makeWakeHarness = makeWakeHarnessWithOptions();
21842197

2198+
it.effect(
2199+
"reuses a background shell's query for omitted and explicit Normal, but blocks Fast",
2200+
() =>
2201+
Effect.scoped(
2202+
Effect.gen(function* () {
2203+
const harness = yield* makeWakeHarness;
2204+
const now = yield* DateTime.now;
2205+
const normal = {
2206+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
2207+
model: "claude-opus-5-5",
2208+
} satisfies ModelSelection;
2209+
const turn = (ordinal: number, modelSelection: ModelSelection) =>
2210+
makeClaudeTestTurnInput({
2211+
threadId: harness.threadId,
2212+
providerThread: harness.providerThread,
2213+
now,
2214+
attemptId: RunAttemptId.make(`attempt-normal-background:${ordinal}`),
2215+
text: `Request ${ordinal}`,
2216+
attachments: [],
2217+
providerTurnOrdinal: ordinal,
2218+
modelSelection,
2219+
});
2220+
yield* harness.runtime.startTurn(turn(1, normal));
2221+
const originalOptions = harness.getOpenedOptions();
2222+
yield* harness.offerAndWait(wakeTaskStarted);
2223+
yield* harness.offerAndWait(turnOneResult);
2224+
yield* Queue.take(harness.terminalReceipts);
2225+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
2226+
2227+
yield* harness.runtime.startTurn(
2228+
turn(2, {
2229+
...normal,
2230+
options: [{ id: "fastMode", value: false }],
2231+
}),
2232+
);
2233+
assert.strictEqual(harness.getOpenedOptions(), originalOptions);
2234+
assert.lengthOf(harness.offeredMessages, 2);
2235+
yield* harness.offerAndWait(turnOneResult);
2236+
yield* Queue.take(harness.terminalReceipts);
2237+
2238+
const refused = yield* harness.runtime
2239+
.startTurn(
2240+
turn(3, {
2241+
...normal,
2242+
options: [{ id: "fastMode", value: true }],
2243+
}),
2244+
)
2245+
.pipe(Effect.result);
2246+
assert.equal(refused._tag, "Failure");
2247+
if (refused._tag === "Failure") {
2248+
assert.instanceOf(
2249+
refused.failure.cause,
2250+
ClaudeAdapterV2.ClaudeBackgroundWorkBlocksQueryReplacementError,
2251+
);
2252+
}
2253+
assert.strictEqual(harness.getOpenedOptions(), originalOptions);
2254+
assert.lengthOf(harness.offeredMessages, 2);
2255+
assert.isTrue(yield* harness.hasPendingBackgroundWork);
2256+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
2257+
),
2258+
);
2259+
21852260
it.effect.each(["completed", "interrupted"] as const)(
21862261
"projects Claude thinking blocks when %s",
21872262
(status) =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6969,11 +6969,12 @@ export function makeClaudeAdapterV2(
69696969

69706970
const openedWithResume = (yield* Ref.get(openedNativeThreads)).has(nativeThreadId);
69716971
// openedNativeThreads is per session instance and is lost when the
6972-
// provider session is idle-released. A prior persisted provider turn
6973-
// proves the native session already exists, so the query must resume
6974-
// it; reopening with a fixed session id makes the CLI fail fast with
6975-
// "Session ID ... is already in use".
6976-
const hasPersistedProviderTurn = turnInput.providerTurnOrdinal > 1;
6972+
// provider session is idle-released. A prior turn on this native id
6973+
// requires resume; sessionId would fail with "already in use".
6974+
// A fresh-session fallback keeps provider-thread history but binds
6975+
// a new native id, which must be created before it can be resumed.
6976+
const hasPersistedProviderTurn =
6977+
turnInput.nativeThreadHasTurns ?? turnInput.providerTurnOrdinal > 1;
69776978
const shouldResume =
69786979
resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn;
69796980
const queryOptions = makeClaudeQueryOptions({

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const modelSelection = { instanceId, model: "test-model" };
4141
// Codex turns leave commands running, then the thread moves to another
4242
// provider thread (a provider switch). Stop on the newer, settled run must
4343
// reach both provider threads and end all of the Codex work.
44-
it.effect("Stop reaches background work an earlier provider thread still runs", () =>
44+
const stopEarlierBackgroundWork = (failedStart: boolean) =>
4545
Effect.scoped(
4646
Effect.gen(function* () {
4747
const cwd = yield* checkpointWorkspace("background-work-stop");
@@ -233,7 +233,10 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
233233
const settledRun = (input: {
234234
readonly ordinal: number;
235235
readonly providerThreadId: ProviderThreadId;
236-
readonly runningItem?: { readonly id: TurnItemId; readonly kind: "command" | "subagent" };
236+
readonly runningItem?: {
237+
readonly id: TurnItemId;
238+
readonly kind: "command" | "subagent";
239+
};
237240
}) => {
238241
const runId = RunId.make(`run:${input.ordinal}`);
239242
const attemptId = RunAttemptId.make(`attempt:${input.ordinal}`);
@@ -407,11 +410,41 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
407410
],
408411
});
409412

413+
const failedRun = settledRun({ ordinal: 5, providerThreadId: otherProviderThreadId });
414+
if (failedStart) {
415+
yield* sink.write({
416+
events: failedRun.events.flatMap((event): Array<OrchestrationV2DomainEvent> => {
417+
switch (event.type) {
418+
case "provider-turn.updated":
419+
return [];
420+
case "run.created":
421+
return [{ ...event, payload: { ...event.payload, status: "failed" } }];
422+
case "node.updated":
423+
return [
424+
{
425+
...event,
426+
payload: { ...event.payload, status: "failed", providerTurnId: null },
427+
},
428+
];
429+
case "run-attempt.created":
430+
return [
431+
{
432+
...event,
433+
payload: { ...event.payload, status: "failed", providerTurnId: null },
434+
},
435+
];
436+
default:
437+
return [event];
438+
}
439+
}),
440+
});
441+
}
442+
410443
yield* orchestrator.dispatch({
411444
type: "run.interrupt",
412445
commandId: CommandId.make("stop-background-work"),
413446
threadId,
414-
runId: latestRun.runId,
447+
runId: failedStart ? failedRun.runId : latestRun.runId,
415448
});
416449
yield* worker.drain();
417450

@@ -442,5 +475,13 @@ it.effect("Stop reaches background work an earlier provider thread still runs",
442475
),
443476
);
444477
}),
445-
),
478+
);
479+
480+
it.effect("Stop reaches background work an earlier provider thread still runs", () =>
481+
stopEarlierBackgroundWork(false),
482+
);
483+
484+
it.effect(
485+
"Stop reaches earlier background work after the newest run fails before provider start",
486+
() => stopEarlierBackgroundWork(true),
446487
);

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8040,11 +8040,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
80408040
activeProviderThreadId: projection.thread.activeProviderThreadId,
80418041
runs: projection.runs,
80428042
}).length > 0;
8043-
const providerTurn = projection.providerTurns.findLast(
8044-
(candidate) =>
8045-
candidate.runAttemptId === run?.activeAttemptId &&
8046-
(candidate.status === "running" || hasBackgroundWork),
8047-
);
8043+
// A failed start has no provider turn. Background work still belongs
8044+
// to the provider thread, so Stop reaches its latest accepted turn.
8045+
const providerTurn =
8046+
projection.providerTurns.findLast(
8047+
(candidate) =>
8048+
candidate.runAttemptId === run?.activeAttemptId &&
8049+
(candidate.status === "running" || hasBackgroundWork),
8050+
) ??
8051+
(hasBackgroundWork
8052+
? projection.providerTurns.findLast(
8053+
(candidate) => candidate.providerThreadId === run?.providerThreadId,
8054+
)
8055+
: undefined);
80488056
if (run === undefined || rootNode === undefined || providerThread === undefined) {
80498057
return yield* new OrchestratorDispatchError({
80508058
commandId: command.commandId,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ export interface ProviderAdapterV2TurnInput {
398398
readonly threadId: ThreadId;
399399
readonly runId: RunId;
400400
readonly runOrdinal: number;
401+
/** Whether the current native session has an accepted turn; omitted when unknown. */
402+
readonly nativeThreadHasTurns?: boolean;
401403
readonly providerTurnOrdinal: number;
402404
readonly restartContinuationOfRunId?: RunId;
403405
readonly attemptId: RunAttemptId;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,13 @@ export const layer: Layer.Layer<
12311231
.filter((turn) => turn.providerThreadId === providerThread.id)
12321232
.map((turn) => turn.ordinal),
12331233
) + 1,
1234+
// Legacy accepted attempts have no native id. They count only before
1235+
// a replacement, while no accepted attempt records a native identity.
1236+
nativeThreadHasTurns:
1237+
nativeInputRunIds.size > 0 ||
1238+
(legacyInputRunIds.size > 0 &&
1239+
sameNativeThread &&
1240+
!acceptedAttempts.some((source) => source.nativeThreadId !== undefined)),
12341241
shouldStartProviderTurn: runControls.shouldStartProviderTurn,
12351242
shouldFinalizeRun: runControls.shouldFinalizeRun,
12361243
hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ export interface RunExecutionServiceV2StartRootRunInput {
507507
readonly attempt: OrchestrationV2RunAttempt;
508508
readonly attemptId: RunAttemptId;
509509
readonly providerTurnOrdinal: number;
510+
readonly nativeThreadHasTurns?: boolean;
510511
readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect<
511512
ReadonlyArray<InheritedBackgroundTurnItemRoute>,
512513
unknown
@@ -1348,6 +1349,9 @@ export const layer: Layer.Layer<
13481349
runId: input.run.id,
13491350
runOrdinal: input.run.ordinal,
13501351
providerTurnOrdinal: input.providerTurnOrdinal,
1352+
...(input.nativeThreadHasTurns === undefined
1353+
? {}
1354+
: { nativeThreadHasTurns: input.nativeThreadHasTurns }),
13511355
...(input.run.restartContinuationOfRunId === undefined
13521356
? {}
13531357
: {

0 commit comments

Comments
 (0)