Skip to content

Commit d20d535

Browse files
fix(server): show self-resumed Claude subagents as running (pingdotgg#23831)
1 parent 8b23382 commit d20d535

2 files changed

Lines changed: 232 additions & 19 deletions

File tree

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

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16478,6 +16478,208 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1647816478
),
1647916479
);
1648016480

16481+
// Claude reports a background subagent `completed` when its turn ends while
16482+
// its own background shell still runs. When that shell finishes, Claude
16483+
// resumes the subagent with a new task_started for the same task and launch
16484+
// tool_use_id while the root is idle. Frame order mirrors a live capture.
16485+
it.effect("reopens a completed background subagent that resumes while the root is idle", () =>
16486+
Effect.scoped(
16487+
Effect.gen(function* () {
16488+
const TASK_ID = "ae3f32459af762a5f";
16489+
const LAUNCH = "toolu_01LcidmwEK1Y6GC6LpX3kvxX";
16490+
const OWN_SHELL = "toolu_01QAahgijM2KRBBSkWxnui4g";
16491+
const RESUMED_BASH = "toolu_01CkA1juicPNCtQrqKL8h6Rz";
16492+
const harness = yield* makeWakeHarness;
16493+
const now = yield* DateTime.now;
16494+
let sequence = 0;
16495+
const system = (frame: object) =>
16496+
claudeSdkFrame({
16497+
type: "system",
16498+
...frame,
16499+
uuid: `00000000-0000-4000-8000-0000000162${String(++sequence).padStart(2, "0")}`,
16500+
session_id: WAKE_NATIVE_SESSION,
16501+
});
16502+
const agentEntry = (runId: string) => ({
16503+
task_id: TASK_ID,
16504+
run_id: runId,
16505+
task_type: "local_agent",
16506+
subagent_type: "general-purpose",
16507+
description: "Audit recent commits",
16508+
});
16509+
const shellEntry = (taskId: string) => ({
16510+
task_id: taskId,
16511+
run_id: `run-${taskId}`,
16512+
task_type: "local_bash",
16513+
description: "Sleep",
16514+
parent_task_id: TASK_ID,
16515+
});
16516+
const roster = (tasks: ReadonlyArray<object>) =>
16517+
system({ subtype: "background_tasks_changed", tasks });
16518+
const shellStarted = (taskId: string, toolUseId: string) =>
16519+
system({
16520+
subtype: "task_started",
16521+
task_id: taskId,
16522+
tool_use_id: toolUseId,
16523+
task_type: "local_bash",
16524+
description: "Sleep",
16525+
});
16526+
const startTurn = (ordinal: number, continuation: boolean) =>
16527+
harness.runtime.startTurn(
16528+
makeClaudeTestTurnInput({
16529+
threadId: harness.threadId,
16530+
providerThread: harness.providerThread,
16531+
now,
16532+
attemptId: RunAttemptId.make(`attempt-claude-self-resume-${ordinal}`),
16533+
text: continuation ? "Background task completed." : "Launch a background agent.",
16534+
attachments: [],
16535+
providerTurnOrdinal: ordinal,
16536+
...(continuation
16537+
? {
16538+
messageCreatedBy: "agent" as const,
16539+
messageCreationSource: "provider" as const,
16540+
}
16541+
: {}),
16542+
}),
16543+
);
16544+
const subagentUpdates = () =>
16545+
harness.events.flatMap((event) =>
16546+
event.type === "subagent.updated" ? [event.subagent] : [],
16547+
);
16548+
16549+
yield* startTurn(1, false);
16550+
for (const frame of [
16551+
roster([agentEntry("run-1")]),
16552+
makeSubagentTaskStartedFrame({
16553+
taskId: TASK_ID,
16554+
toolUseId: LAUNCH,
16555+
uuid: "00000000-0000-4000-8000-000000016101",
16556+
}),
16557+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000016102", result: "LAUNCHED" }),
16558+
]) {
16559+
yield* harness.offerAndWait(frame);
16560+
}
16561+
yield* Queue.take(harness.terminalReceipts);
16562+
16563+
// The subagent starts its own background shell, ends its turn, and
16564+
// Claude reports it completed; the parent wakes, answers, goes idle.
16565+
for (const frame of [
16566+
...makeSubagentAssistantFrames({
16567+
parentToolUseId: LAUNCH,
16568+
uuid: "00000000-0000-4000-8000-000000016103",
16569+
bashToolUseId: OWN_SHELL,
16570+
}),
16571+
roster([agentEntry("run-1"), shellEntry("b455x9pq5")]),
16572+
shellStarted("b455x9pq5", OWN_SHELL),
16573+
makeSubagentToolResultFrame({
16574+
parentToolUseId: LAUNCH,
16575+
uuid: "00000000-0000-4000-8000-000000016104",
16576+
toolUseId: OWN_SHELL,
16577+
}),
16578+
makeSubagentNotificationFrame({
16579+
taskId: TASK_ID,
16580+
toolUseId: LAUNCH,
16581+
summary: "WAITING",
16582+
uuid: "00000000-0000-4000-8000-000000016105",
16583+
}),
16584+
roster([shellEntry("b455x9pq5")]),
16585+
wakeTurnInit,
16586+
]) {
16587+
yield* harness.offerAndWait(frame);
16588+
}
16589+
yield* Queue.take(harness.continuationReceipts);
16590+
yield* startTurn(2, true);
16591+
yield* harness.offerAndWait(
16592+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000016106", result: "Noted." }),
16593+
);
16594+
yield* Queue.take(harness.terminalReceipts);
16595+
16596+
// Its shell finishes and Claude resumes it under a new native run.
16597+
for (const frame of [
16598+
system({
16599+
subtype: "task_notification",
16600+
task_id: "b455x9pq5",
16601+
tool_use_id: OWN_SHELL,
16602+
status: "completed",
16603+
summary: "Background command completed (exit code 0)",
16604+
output_file: "/tmp/b455x9pq5.output",
16605+
}),
16606+
roster([]),
16607+
roster([agentEntry("run-2")]),
16608+
makeSubagentTaskStartedFrame({
16609+
taskId: TASK_ID,
16610+
toolUseId: LAUNCH,
16611+
uuid: "00000000-0000-4000-8000-000000016107",
16612+
}),
16613+
]) {
16614+
yield* harness.offerAndWait(frame);
16615+
}
16616+
// Events reach the app only through a run, and the launching run ended
16617+
// with the interim completion.
16618+
assert.equal(harness.continuationRequests.length, 2);
16619+
16620+
yield* Queue.take(harness.continuationReceipts);
16621+
yield* startTurn(3, true);
16622+
// Claude does not wake the root for this, so the root must not stay busy.
16623+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
16624+
yield* startTurn(4, false);
16625+
yield* harness.offerAndWait(
16626+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000016110", result: "PONG" }),
16627+
);
16628+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
16629+
for (const frame of [
16630+
...makeSubagentAssistantFrames({
16631+
parentToolUseId: LAUNCH,
16632+
uuid: "00000000-0000-4000-8000-000000016108",
16633+
bashToolUseId: RESUMED_BASH,
16634+
}),
16635+
shellStarted("beyhxkdpc", RESUMED_BASH),
16636+
roster([agentEntry("run-2"), shellEntry("beyhxkdpc")]),
16637+
]) {
16638+
yield* harness.offerAndWait(frame);
16639+
}
16640+
// Events are ordered: once this roster is recorded, so is every earlier event.
16641+
yield* harness.until(() =>
16642+
harness.events.some(
16643+
(event) =>
16644+
event.type === "provider_thread.updated" &&
16645+
event.providerThread.pendingBackgroundTasks?.some(
16646+
(task) => task.taskId === "beyhxkdpc",
16647+
) === true,
16648+
),
16649+
);
16650+
const resumed = subagentUpdates().at(-1);
16651+
assert.equal(resumed?.status, "running");
16652+
assert.isNull(resumed?.completedAt);
16653+
assert.equal(resumed?.runId, "run-attempt-claude-self-resume-3");
16654+
const routing = subagentRouting(harness.events, [RESUMED_BASH]);
16655+
assert.deepEqual(
16656+
[...(routing.toolThreadIds.get(RESUMED_BASH) ?? [])],
16657+
[routing.childThreadId],
16658+
);
16659+
16660+
yield* harness.offerAndWait(
16661+
makeSubagentNotificationFrame({
16662+
taskId: TASK_ID,
16663+
toolUseId: LAUNCH,
16664+
summary: "DONE",
16665+
uuid: "00000000-0000-4000-8000-000000016109",
16666+
}),
16667+
);
16668+
yield* harness.until(() => subagentUpdates().at(-1)?.result === "DONE");
16669+
assert.equal(subagentUpdates().at(-1)?.status, "completed");
16670+
}).pipe(
16671+
Effect.provide(
16672+
Layer.mergeAll(
16673+
IdAllocator.layer,
16674+
McpProviderSessions.layer,
16675+
NodeServices.layer,
16676+
NodeCrypto.layer,
16677+
),
16678+
),
16679+
),
16680+
),
16681+
);
16682+
1648116683
it.effect("keeps a nested subagent's launch in order while the root turn is idle", () =>
1648216684
Effect.scoped(
1648316685
Effect.gen(function* () {

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9234,31 +9234,41 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92349234
message.type === "system" &&
92359235
message.subtype === "task_started" &&
92369236
(yield* Ref.get(sessionSubagentsByTaskId)).has(message.task_id);
9237+
// A completed subagent that Claude resumes on its own (under its launch
9238+
// call, unlike SendMessage) has no live run left to show it while the
9239+
// root is idle; only a continuation does.
9240+
let isResumedSubagentTaskStarted = false;
92379241
if (
92389242
isKnownSubagentTaskStarted &&
92399243
message.type === "system" &&
92409244
message.subtype === "task_started"
92419245
) {
92429246
const now = yield* DateTime.now;
9243-
yield* Ref.update(sessionSubagentsByTaskId, (current) => {
9244-
const registered = current.get(message.task_id);
9245-
if (registered === undefined || registered.task.status === "running") {
9246-
return current;
9247-
}
9248-
const { progress: _staleProgress, ...priorTask } = registered.task;
9249-
return new Map(current).set(message.task_id, {
9250-
...registered,
9251-
confirmedOutcome: undefined,
9252-
task: {
9253-
...priorTask,
9254-
status: "running",
9255-
result: null,
9256-
startedAt: now,
9257-
completedAt: null,
9258-
updatedAt: now,
9259-
},
9260-
});
9261-
});
9247+
isResumedSubagentTaskStarted = yield* Ref.modify(
9248+
sessionSubagentsByTaskId,
9249+
(current) => {
9250+
const registered = current.get(message.task_id);
9251+
if (registered === undefined || registered.task.status === "running") {
9252+
return [false, current] as const;
9253+
}
9254+
const { progress: _staleProgress, ...priorTask } = registered.task;
9255+
return [
9256+
registered.runToolUseId === message.tool_use_id,
9257+
new Map(current).set(message.task_id, {
9258+
...registered,
9259+
confirmedOutcome: undefined,
9260+
task: {
9261+
...priorTask,
9262+
status: "running",
9263+
result: null,
9264+
startedAt: now,
9265+
completedAt: null,
9266+
updatedAt: now,
9267+
},
9268+
}),
9269+
] as const;
9270+
},
9271+
);
92629272
}
92639273
const isWakeTurnStart = isClaudeTurnStartMessage(message);
92649274
const isWakeEvidence =
@@ -9373,6 +9383,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93739383
parentToolUseIdFromSdkMessage(message) === null;
93749384
if (
93759385
!isPendingSubagentNotification &&
9386+
!isResumedSubagentTaskStarted &&
93769387
!isNativeOpaqueWakeFrame &&
93779388
!isWakeTurnStart &&
93789389
message.type !== "result"

0 commit comments

Comments
 (0)