Skip to content

Commit 681afcd

Browse files
test(server): pin when a background subagent's idle work is stored
The after-root replay now requires every subagent message, completed command and progress update to be stored before the wake's continuation run starts, and nothing in the child thread after it. The idle adapter test also checks that the call spanning the turn end keeps its input. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 8d76c39 commit 681afcd

3 files changed

Lines changed: 72 additions & 26 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6347,14 +6347,15 @@ describe("ClaudeAdapterV2 background wake turns", () => {
63476347
);
63486348
const routing = subagentRouting(harness.events, [STRADDLING_BASH, IDLE_BASH]);
63496349
assert.deepEqual(routing.assistantTexts(routing.childThreadId), ["Still auditing."]);
6350+
// The call that spans the turn end keeps the type and input its
6351+
// tool_use gave it during the open turn.
63506352
const straddling = latestToolItem(STRADDLING_BASH);
6353+
const straddlingItem =
6354+
straddling?.type === "turn_item.updated" ? straddling.turnItem : undefined;
6355+
assert.equal(straddlingItem?.status, "completed");
63516356
assert.equal(
6352-
straddling?.type === "turn_item.updated" ? straddling.turnItem.type : undefined,
6353-
"command_execution",
6354-
);
6355-
assert.equal(
6356-
straddling?.type === "turn_item.updated" ? straddling.turnItem.status : undefined,
6357-
"completed",
6357+
straddlingItem?.type === "command_execution" ? straddlingItem.input : undefined,
6358+
"git log -5",
63586359
);
63596360
assert.equal(harness.continuationRequests.length, 0);
63606361

‎apps/server/src/orchestration-v2/testkit/fixtures/claude_background_subagent_after_root/input.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export function claudeBackgroundSubagentAfterRootInput(): OrchestratorFixtureInp
1414
return {
1515
steps: [
1616
{ type: "message", text: CLAUDE_BACKGROUND_SUBAGENT_AFTER_ROOT_PROMPT },
17-
// The subagent's frames all arrive after the root settles, so they wait
18-
// in the wake buffer; its completion starts continuation run 2, which
19-
// drains them. The wake result is held until run 2 has drained the
20-
// buffered wake reply, so the recording cannot end the session first.
17+
// The subagent's frames all arrive after the root settles. They are
18+
// stored as they arrive; its completion starts continuation run 2,
19+
// which drains the wake reply. The wake result is held until run 2 has
20+
// drained that reply, so the recording cannot end the session first.
2121
{
2222
type: "await_run_status",
2323
targetRunIndex: 2,

‎apps/server/src/orchestration-v2/testkit/fixtures/claude_background_subagent_after_root/output.ts‎

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { assert } from "@effect/vitest";
2-
import type { OrchestrationV2ThreadProjection, ProviderReplayTranscript } from "@t3tools/contracts";
2+
import type {
3+
OrchestrationV2DomainEvent,
4+
OrchestrationV2ThreadProjection,
5+
ProviderReplayTranscript,
6+
} from "@t3tools/contracts";
37

48
import type { OrchestratorV2ScenarioResult } from "../../OrchestratorScenario.ts";
59
import {
@@ -12,6 +16,11 @@ import { CLAUDE_BACKGROUND_SUBAGENT_AFTER_ROOT_PROMPT } from "./input.ts";
1216

1317
const SUBAGENT_TEXTS = ["SUB_STEP_1", "SUB_STEP_2", "SUB_FINAL_REPORT"];
1418
const SUBAGENT_COMMANDS = ["sleep 3 && echo SUB_DONE_1", "sleep 3 && echo SUB_DONE_2"];
19+
// The subagent's own task_progress, one per step, as the recording reports it.
20+
const SUBAGENT_PROGRESS = [
21+
"Running Sleep 3 seconds then echo SUB_DONE_1",
22+
"Running Sleep 3 seconds then echo SUB_DONE_2",
23+
];
1524

1625
function assistantTexts(projection: OrchestrationV2ThreadProjection): ReadonlyArray<string> {
1726
return projection.turnItems.flatMap((item) =>
@@ -97,26 +106,62 @@ export function assertClaudeBackgroundSubagentAfterRootOutput(
97106
`subagent command ${command} must be in the child thread`,
98107
);
99108
}
100-
// The continuation is queued as soon as the wake arrives; it drains the
101-
// wake buffer when it starts.
109+
// The subagent works while the root is idle, so its steps and progress are
110+
// stored as they happen. Its end starts continuation run 2, which drains
111+
// the wake buffer; none of the subagent's work may wait there for it.
102112
const continuationStartIndex = result.domainEvents.findIndex(
103113
(event) =>
104114
event.type === "run.updated" &&
105115
event.payload.id === projection.runs[1]?.id &&
106116
event.payload.status === "starting",
107117
);
108-
const firstCommandDoneIndex = result.domainEvents.findIndex(
109-
(event) =>
110-
event.type === "turn-item.updated" &&
111-
event.payload.threadId === subagent.childThreadId &&
112-
event.payload.type === "command_execution" &&
113-
event.payload.status === "completed" &&
114-
JSON.stringify(event.payload.input).includes(SUBAGENT_COMMANDS[0] ?? ""),
115-
);
116-
assert.isAtLeast(firstCommandDoneIndex, 0);
117-
assert.isBelow(
118-
firstCommandDoneIndex,
119-
continuationStartIndex,
120-
"the subagent's work is stored while the root is idle, not when its end wakes the root",
118+
assert.isAtLeast(continuationStartIndex, 0);
119+
const assertStoredWhileIdle = (
120+
label: string,
121+
matches: (event: OrchestrationV2DomainEvent) => boolean,
122+
) => {
123+
const index = result.domainEvents.findIndex(matches);
124+
assert.isAtLeast(index, 0, `${label} is stored`);
125+
assert.isBelow(index, continuationStartIndex, `${label} is stored before the root wakes`);
126+
};
127+
for (const text of SUBAGENT_TEXTS) {
128+
assertStoredWhileIdle(
129+
`subagent text ${text}`,
130+
(event) =>
131+
event.type === "message.updated" &&
132+
event.payload.threadId === subagent.childThreadId &&
133+
event.payload.text.trim() === text,
134+
);
135+
}
136+
for (const command of SUBAGENT_COMMANDS) {
137+
assertStoredWhileIdle(
138+
`completed subagent command ${command}`,
139+
(event) =>
140+
event.type === "turn-item.updated" &&
141+
event.payload.threadId === subagent.childThreadId &&
142+
event.payload.type === "command_execution" &&
143+
event.payload.status === "completed" &&
144+
event.payload.input.includes(command),
145+
);
146+
}
147+
for (const progress of SUBAGENT_PROGRESS) {
148+
assertStoredWhileIdle(
149+
`subagent progress "${progress}"`,
150+
(event) =>
151+
event.type === "subagent.updated" &&
152+
event.payload.id === subagent.id &&
153+
event.payload.progress === progress,
154+
);
155+
}
156+
// The drain stores nothing in the child thread, so nothing is stored twice.
157+
assert.isFalse(
158+
result.domainEvents
159+
.slice(continuationStartIndex)
160+
.some(
161+
(event) =>
162+
(event.type === "message.updated" || event.type === "turn-item.updated") &&
163+
event.payload.threadId === subagent.childThreadId,
164+
),
165+
"the continuation run stores none of the subagent's work",
121166
);
122167
}

0 commit comments

Comments
 (0)