Skip to content

Commit cb816c9

Browse files
jieyuexingclaude
andcommitted
fix(server): Claude background subagents end when their wake can't run on Claude
A Claude wake buffered after the thread switched providers was dispatched as an ordinary continuation and ran on the thread's current provider, which cannot replay Claude's buffer. The adapter's sticky continuation flag then suppressed every later wake, so the reported subagents stayed running until a server restart (#17099). The continuation worker now drops an adapter-buffered wake when the provider instance that buffered it is no longer the thread's current instance, as it already did for archived threads. ClaudeAdapterV2 implements clearIfCurrent and dispatchIfCurrent: a dropped or failed continuation clears the sticky flag, ends the buffered subagents with the status their task_notification reports through the turn that settled last, and discards the buffer. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 30cc788 commit cb816c9

4 files changed

Lines changed: 220 additions & 12 deletions

File tree

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7986,6 +7986,99 @@ describe("ClaudeAdapterV2 background wake turns", () => {
79867986
),
79877987
);
79887988

7989+
it.effect("ends buffered subagents when the worker drops their wake continuation", () =>
7990+
Effect.scoped(
7991+
Effect.gen(function* () {
7992+
const subagentFrames = (name: string, ordinal: number) => ({
7993+
started: claudeSdkFrame({
7994+
type: "system",
7995+
subtype: "task_started",
7996+
task_id: `task-dropped-wake-${name}`,
7997+
tool_use_id: `toolu-dropped-wake-${name}`,
7998+
description: `Background research ${name}`,
7999+
subagent_type: "general-purpose",
8000+
task_type: "local_agent",
8001+
prompt: "Investigate the flaky test.",
8002+
uuid: `00000000-0000-4000-8000-00000000d1${ordinal}0`,
8003+
session_id: WAKE_NATIVE_SESSION,
8004+
}),
8005+
failed: claudeSdkFrame({
8006+
type: "system",
8007+
subtype: "task_notification",
8008+
task_id: `task-dropped-wake-${name}`,
8009+
tool_use_id: `toolu-dropped-wake-${name}`,
8010+
status: "failed",
8011+
output_file: `/tmp/task-dropped-wake-${name}.output`,
8012+
summary: "You've hit your limit.",
8013+
uuid: `00000000-0000-4000-8000-00000000d1${ordinal}1`,
8014+
session_id: WAKE_NATIVE_SESSION,
8015+
}),
8016+
});
8017+
const first = subagentFrames("first", 1);
8018+
const second = subagentFrames("second", 2);
8019+
8020+
const harness = yield* makeWakeHarness;
8021+
const now = yield* DateTime.now;
8022+
const lastStatus = (taskId: string) =>
8023+
harness.events.findLast(
8024+
(event): event is Extract<ProviderAdapterV2Event, { type: "subagent.updated" }> =>
8025+
event.type === "subagent.updated" &&
8026+
event.subagent.nativeTaskRef?.nativeId === taskId,
8027+
)?.subagent.status;
8028+
8029+
yield* harness.runtime.startTurn(
8030+
makeClaudeTestTurnInput({
8031+
threadId: harness.threadId,
8032+
providerThread: harness.providerThread,
8033+
now,
8034+
attemptId: RunAttemptId.make("attempt-claude-dropped-wake"),
8035+
text: "Spawn two background subagents and stop.",
8036+
attachments: [],
8037+
}),
8038+
);
8039+
yield* Queue.offer(harness.sdkMessages, first.started);
8040+
yield* Queue.offer(harness.sdkMessages, second.started);
8041+
yield* awaitUntil(
8042+
() => lastStatus("task-dropped-wake-second") === "running",
8043+
"subagents running",
8044+
);
8045+
yield* Queue.offer(
8046+
harness.sdkMessages,
8047+
makeResultFrame({
8048+
uuid: "00000000-0000-4000-8000-00000000d100",
8049+
result: "Spawned two subagents in the background.",
8050+
}),
8051+
);
8052+
yield* awaitUntil(() => harness.terminalEvents().length === 1, "first turn terminal");
8053+
8054+
// The thread moved to another provider, so the worker drops this wake
8055+
// instead of starting a Claude continuation.
8056+
yield* Queue.offer(harness.sdkMessages, first.failed);
8057+
yield* awaitUntil(() => harness.continuationRequests.length === 1, "first wake");
8058+
const firstDrop = harness.continuationRequests[0]?.clearIfCurrent;
8059+
assert.isDefined(firstDrop);
8060+
yield* firstDrop!();
8061+
yield* awaitUntil(
8062+
() => lastStatus("task-dropped-wake-first") === "failed",
8063+
"first subagent failed",
8064+
);
8065+
8066+
// The sticky offer is gone, so the next wake asks again; a failed
8067+
// dispatch drops it the same way.
8068+
yield* Queue.offer(harness.sdkMessages, second.failed);
8069+
yield* awaitUntil(() => harness.continuationRequests.length === 2, "second wake");
8070+
const secondDispatch = harness.continuationRequests[1]?.dispatchIfCurrent;
8071+
assert.isDefined(secondDispatch);
8072+
yield* Effect.exit(secondDispatch!(Effect.fail("dispatch failed")));
8073+
yield* awaitUntil(
8074+
() => lastStatus("task-dropped-wake-second") === "failed",
8075+
"second subagent failed",
8076+
);
8077+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
8078+
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))),
8079+
),
8080+
);
8081+
79898082
it.effect("re-opens a resumed subagent whose task_started races past settle", () =>
79908083
Effect.scoped(
79918084
Effect.gen(function* () {

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

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,11 @@ function isClaudeNonSubagentTask(message: SDKMessage): boolean {
18101810
return isClaudeOpaqueBackgroundTaskType(claudeTaskTypeFromSdkMessage(message));
18111811
}
18121812

1813+
/** The subagent status a `task_notification` ends it with. */
1814+
function claudeSubagentEndStatus(status: "completed" | "failed" | "stopped") {
1815+
return status === "completed" ? "completed" : status === "stopped" ? "cancelled" : "failed";
1816+
}
1817+
18131818
function isClaudeBackgroundTasksChangedMessage(message: SDKMessage): boolean {
18141819
return (
18151820
message.type === "system" &&
@@ -3302,6 +3307,9 @@ export function makeClaudeAdapterV2(
33023307
}
33033308
});
33043309
const requestedContinuations = yield* Ref.make(new Set<string>());
3310+
// Counts continuation offers per native thread, so a request the
3311+
// worker drops late cannot clear a newer offer's buffer.
3312+
const continuationGenerations = new Map<string, number>();
33053313
// ExitPlanMode plans whose permission callback fired while the tool's
33063314
// root frames were held for a prompt echo. Each projects when its
33073315
// tool_use frame is handled, in whichever run that frame is routed
@@ -5305,6 +5313,51 @@ export function makeClaudeAdapterV2(
53055313
}
53065314
});
53075315

5316+
// The worker drops a continuation it will not start: the thread was
5317+
// archived, it now runs another provider, or the dispatch failed. No
5318+
// Claude turn will drain the buffer, so the subagents it reports end
5319+
// here, through the turn that settled last, and the next wake can
5320+
// request a continuation again. Claude's own session keeps the wake.
5321+
const dropBufferedWake = Effect.fnUntraced(function* (
5322+
nativeThreadId: string,
5323+
generation: number,
5324+
) {
5325+
if (continuationGenerations.get(nativeThreadId) !== generation) return;
5326+
const requested = yield* Ref.modify(requestedContinuations, (current) => {
5327+
if (!current.has(nativeThreadId)) return [false, current] as const;
5328+
const updated = new Set(current);
5329+
updated.delete(nativeThreadId);
5330+
return [true, updated] as const;
5331+
});
5332+
if (!requested) return;
5333+
const buffered = yield* Ref.modify(wakeBuffers, (current) => {
5334+
const entry = current.get(nativeThreadId);
5335+
if (entry === undefined) return [[] as ReadonlyArray<SDKMessage>, current] as const;
5336+
const updated = new Map(current);
5337+
updated.delete(nativeThreadId);
5338+
return [entry.messages, updated] as const;
5339+
});
5340+
const settled = settledTurnByNativeThread.get(nativeThreadId);
5341+
if (settled === undefined) return;
5342+
const subagents = yield* Ref.get(sessionSubagentsByTaskId);
5343+
for (const message of buffered) {
5344+
if (
5345+
message.type !== "system" ||
5346+
message.subtype !== "task_notification" ||
5347+
!subagents.has(message.task_id)
5348+
) {
5349+
continue;
5350+
}
5351+
yield* updateClaudeSubagentNode({
5352+
context: settled,
5353+
taskId: message.task_id,
5354+
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
5355+
result: message.summary,
5356+
status: claudeSubagentEndStatus(message.status),
5357+
});
5358+
}
5359+
});
5360+
53085361
const bufferWakeMessage = Effect.fnUntraced(function* (wakeInput: {
53095362
readonly nativeThreadId: string;
53105363
readonly message: SDKMessage;
@@ -5511,12 +5564,21 @@ export function makeClaudeAdapterV2(
55115564
threadId: route.threadId,
55125565
providerThreadId: route.providerThreadId,
55135566
});
5567+
const generation = (continuationGenerations.get(wakeInput.nativeThreadId) ?? 0) + 1;
5568+
continuationGenerations.set(wakeInput.nativeThreadId, generation);
5569+
const drop = dropBufferedWake(wakeInput.nativeThreadId, generation);
55145570
yield* continuationRequests.offer({
55155571
threadId: route.threadId,
55165572
providerThreadId: route.providerThreadId,
55175573
driver: CLAUDE_PROVIDER,
55185574
detail,
55195575
...(notification === null ? {} : { notification }),
5576+
clearIfCurrent: () => drop,
5577+
dispatchIfCurrent: (dispatch) =>
5578+
dispatch.pipe(
5579+
Effect.map(Option.some),
5580+
Effect.tapCause(() => drop),
5581+
),
55205582
});
55215583
});
55225584

@@ -6304,12 +6366,7 @@ export function makeClaudeAdapterV2(
63046366
taskId: message.task_id,
63056367
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
63066368
result: message.summary,
6307-
status:
6308-
message.status === "completed"
6309-
? "completed"
6310-
: message.status === "stopped"
6311-
? "cancelled"
6312-
: "failed",
6369+
status: claudeSubagentEndStatus(message.status),
63136370
});
63146371
}
63156372
// Replay tombstone only needs to outlive buffering until this

‎apps/server/src/orchestration-v2/ProviderContinuationService.test.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const delegatedMessageId = MessageId.make("message-provider-continuation-delegat
3131
const projection = {
3232
thread: { archivedAt: null, deletedAt: null },
3333
messages: [],
34+
providerThreads: [],
3435
} as unknown as OrchestrationV2ThreadProjection;
3536

3637
const request = (
@@ -786,6 +787,46 @@ describe("ProviderContinuationService", () => {
786787
});
787788
});
788789

790+
it.effect("drops a buffered wake once the thread runs another provider instance", () => {
791+
return Effect.gen(function* () {
792+
const onProvider = (instanceId: string) =>
793+
({
794+
...projection,
795+
thread: { ...projection.thread, modelSelection: { instanceId } },
796+
providerThreads: [{ id: providerThreadId, providerInstanceId: "claude-work" }],
797+
}) as unknown as OrchestrationV2ThreadProjection;
798+
for (const [instanceId, dispatches] of [
799+
["codex", false],
800+
// Another account of the same driver cannot replay this buffer either.
801+
["claude-personal", false],
802+
["claude-work", true],
803+
] as const) {
804+
const dispatched = yield* Queue.unbounded<unknown>();
805+
const cleared = yield* Deferred.make<void>();
806+
yield* Effect.gen(function* () {
807+
const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
808+
yield* requests.offer({
809+
...request(),
810+
clearIfCurrent: () => Deferred.succeed(cleared, undefined),
811+
});
812+
const outcome = yield* Effect.raceFirst(
813+
Queue.take(dispatched).pipe(Effect.as("dispatched")),
814+
Deferred.await(cleared).pipe(Effect.as("dropped")),
815+
);
816+
assert.equal(outcome, dispatches ? "dispatched" : "dropped", instanceId);
817+
}).pipe(
818+
Effect.provide(
819+
layerTest({
820+
dispatched,
821+
getThreadRecords: () => Effect.succeed(onProvider(instanceId)),
822+
}),
823+
),
824+
Effect.scoped,
825+
);
826+
}
827+
});
828+
});
829+
789830
it.effect("clears a sticky continuation offer when its thread is archived", () => {
790831
return Effect.gen(function* () {
791832
const dispatched = yield* Queue.unbounded<unknown>();

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,36 @@ export const layer = Layer.effectDiscard(
8484
function* (request: ProviderContinuationRequests.ProviderContinuationRequest) {
8585
const projection = yield* threads.getThreadRecords(
8686
request.threadId,
87-
["messages", "runs", "providerTurns"],
87+
["messages", "runs", "providerTurns", "providerThreads"],
8888
{
8989
messageIds:
9090
request.delegatedCompletion === undefined
9191
? []
9292
: [request.delegatedCompletion.messageId],
9393
},
9494
);
95-
if (projection.thread.archivedAt !== null || projection.thread.deletedAt !== null) {
96-
yield* Effect.logInfo("orchestration-v2.provider-continuation.thread-archived", {
97-
threadId: request.threadId,
98-
providerThreadId: request.providerThreadId,
99-
});
95+
const archived =
96+
projection.thread.archivedAt !== null || projection.thread.deletedAt !== null;
97+
// A buffered wake holds output only the provider that buffered it can
98+
// replay. Another provider would get the notification text as a prompt.
99+
const bufferedBy =
100+
request.delegatedCompletion === undefined && request.delivery !== "message_text"
101+
? projection.providerThreads.find(
102+
(providerThread) => providerThread.id === request.providerThreadId,
103+
)?.providerInstanceId
104+
: undefined;
105+
const providerChanged =
106+
bufferedBy !== undefined && bufferedBy !== projection.thread.modelSelection.instanceId;
107+
if (archived || providerChanged) {
108+
yield* Effect.logInfo(
109+
archived
110+
? "orchestration-v2.provider-continuation.thread-archived"
111+
: "orchestration-v2.provider-continuation.provider-changed",
112+
{
113+
threadId: request.threadId,
114+
providerThreadId: request.providerThreadId,
115+
},
116+
);
100117
if (request.delegatedCompletion !== undefined) {
101118
yield* clearRetryAttempt(
102119
delegatedCompletionRetryKey(request, request.delegatedCompletion),

0 commit comments

Comments
 (0)