diff --git a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts index 7815f8d959a2..6f62ad0cdf30 100644 --- a/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts @@ -9501,6 +9501,174 @@ describe("AcpAdapterV2", () => { }).pipe(Effect.provide(layerTest)), ); + // Process-group teardown uses the live clock; observations wait on provider events. + it.live.each(["idle", "adopted"] as const)( + "completes the root and stops child tools while the subagent is %s", + (stopWhile) => + Effect.gen(function* () { + const instanceId = ProviderInstanceId.make("acp-test"); + const threadId = ThreadId.make(`thread-acp-stop-child-${stopWhile}`); + const childSessionId = "mock-child-session-stop-tools"; + const secondPromptEntered = yield* Deferred.make(); + let promptCount = 0; + type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"]; + let sessionUpdateHandler: Parameters[0] | undefined; + const adapter = yield* makeAcpAdapterV2({ + instanceId, + selfInvocation: yield* resolveSelfInvocation(), + flavor: { + driver: ACP_TEST_DRIVER, + capabilities: AcpProviderCapabilitiesV2, + deferFinalizeForBackgroundWork: true, + subagentsOutliveTurn: true, + restartRuntimeAfterInterrupt: true, + terminateRuntimeProcessGroupOnInterrupt: true, + preserveRuntimeOnSettledInterrupt: true, + extractSubagentUpdate: (toolCall) => + toolCall.toolCallId === "tool-call-generic-1" + ? { + nativeTaskId: "task-generic-1", + prompt: "background subagent", + title: "background subagent", + model: null, + status: "running", + childSessionId, + result: null, + } + : undefined, + makeRuntime: makeMockRuntime({ + childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner, + mockAgentPath: yield* bundledMockAgentPath, + environment: { T3_ACP_EMIT_GENERIC_TOOL_PLACEHOLDERS: "1" }, + ownDetachedProcessGroup: true, + wrapRuntime: (runtime) => ({ + ...runtime, + handleSessionUpdate: (handler) => + Effect.sync(() => { + sessionUpdateHandler = handler; + }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))), + prompt: (payload) => + ++promptCount === 2 + ? Deferred.succeed(secondPromptEntered, undefined).pipe( + Effect.andThen(Effect.never), + ) + : runtime.prompt(payload), + }), + }), + }, + }); + const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: "full-access", + interactionMode: "default", + cwd: process.cwd(), + }); + const modelSelection = { instanceId, model: "default" } as const; + const runtime = yield* adapter.openSession({ + threadId, + providerSessionId: ProviderSessionId.make(`session-acp-stop-child-${stopWhile}`), + modelSelection, + runtimePolicy, + }); + const providerThread = yield* runtime.ensureThread({ + threadId, + modelSelection, + runtimePolicy, + }); + const startTurn = (ordinal: number) => + DateTime.now.pipe( + Effect.flatMap((now) => + runtime.startTurn( + makeTurnInput({ + threadId, + providerThread, + instanceId, + runtimePolicy, + now, + ordinal, + }), + ), + ), + ); + yield* startTurn(1); + const rootEvents = yield* runtime.events.pipe( + Stream.takeUntil((event) => event.type === "turn.terminal"), + Stream.runCollect, + ); + const terminal = rootEvents.find((event) => event.type === "turn.terminal"); + assert.isDefined(terminal); + assert.equal(terminal.status, "completed"); + const subagent = rootEvents + .flatMap((event) => + event.type === "turn_item.updated" && event.turnItem.type === "subagent" + ? [event.turnItem] + : [], + ) + .at(-1); + assert.isDefined(subagent); + assert.equal(subagent.status, "running"); + + assert.isDefined(sessionUpdateHandler); + yield* sessionUpdateHandler({ + sessionId: childSessionId, + update: { + sessionUpdate: "tool_call", + toolCallId: "child-tool-call-1", + title: "Child read", + kind: "read", + status: "in_progress", + rawInput: {}, + }, + }); + const isChildTool = (event: ProviderAdapter.ProviderAdapterV2Event) => + event.type === "turn_item.updated" && + event.turnItem.nativeItemRef?.nativeId?.includes("child-tool-call-1") === true; + const idleEvents = yield* runtime.events.pipe( + Stream.takeUntil(isChildTool), + Stream.runCollect, + ); + const childTool = idleEvents.find(isChildTool); + assert.isDefined(childTool); + if (childTool.type !== "turn_item.updated") return yield* Effect.die("Missing child tool"); + assert.notEqual(childTool.turnItem.threadId, threadId); + assert.equal(childTool.turnItem.status, "running"); + + const idAllocator = yield* IdAllocator.IdAllocatorV2; + let stoppedProviderTurnId = terminal.providerTurnId; + if (stopWhile === "adopted") { + yield* startTurn(2); + yield* Deferred.await(secondPromptEntered); + stoppedProviderTurnId = idAllocator.derive.providerTurn({ + driver: ACP_TEST_DRIVER, + nativeTurnId: acpScopedNativeId(instanceId, "mock-session-1:turn:2"), + }); + } + yield* runtime.interruptTurn({ + providerThread, + providerTurnId: stoppedProviderTurnId, + requestRuntimeRestart: true, + }); + const stoppedEvents = yield* runtime.events.pipe( + Stream.takeUntil((event) => + stopWhile === "adopted" + ? event.type === "turn.terminal" && event.providerTurnId === stoppedProviderTurnId + : isChildTool(event) && + event.type === "turn_item.updated" && + event.turnItem.status === "interrupted", + ), + Stream.runCollect, + ); + const childUpdates = [...rootEvents, ...idleEvents, ...stoppedEvents].flatMap((event) => + isChildTool(event) && event.type === "turn_item.updated" ? [event.turnItem] : [], + ); + assert.deepEqual( + childUpdates.map((item) => item.status), + ["running", "interrupted"], + ); + assert.isTrue(childUpdates.every((item) => item.threadId === childTool.turnItem.threadId)); + assert.isTrue(childUpdates.every((item) => item.id === childTool.turnItem.id)); + }).pipe(Effect.provide(layerTest), Effect.scoped), + ); + it.effect("projects completed-root carryover eagerly and drain cannot resurrect it", () => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/grok_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/grok_transcript.ndjson index 92a295504b63..9d9de0fce1f1 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/grok_transcript.ndjson +++ b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/grok_transcript.ndjson @@ -212,6 +212,18 @@ {"type":"emit_inbound","label":"notification:_x.ai/session_notification:subagent_progress","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"subagent_progress","subagent_id":"00000000-0000-4000-8000-000000000002","attempt_id":"at1.bd80fbaa7c40408eb7999e19714ed4e1","parent_session_id":"00000000-0000-4000-8000-000000000001","child_session_id":"00000000-0000-4000-8000-000000000002","duration_ms":10066,"turn_count":1,"tool_call_count":0,"tokens_used":2912,"context_window_tokens":500000,"context_usage_pct":0,"tools_used":[],"error_count":0}}}} {"type":"emit_inbound","label":"notification:_x.ai/task_backgrounded:task_backgrounded","frame":{"kind":"notification","method":"_x.ai/task_backgrounded","params":{"sessionId":"00000000-0000-4000-8000-000000000002","update":{"sessionUpdate":"task_backgrounded","tool_call_id":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","task_id":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","command":"sleep 20","cwd":"","output_file":"/home/grok-replay/.grok/sessions/%3Cworkspace%3E/00000000-0000-4000-8000-000000000002/terminal/call-d5448d70-380d-4a88-a7a0-2968806350ea-0.log","description":"Sleep for 20 seconds as requested"},"_meta":{"eventId":"00000000-0000-4000-8000-000000000002-170","agentTimestampMs":1790321099801}}}} {"type":"emit_inbound","label":"notification:_x.ai/session_notification:background_tasks","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000002","update":{"sessionUpdate":"background_tasks","tasks":[{"task_id":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","command":"sleep 20","description":"Sleep for 20 seconds as requested","cwd":"","kind":"bash","status":"running","started_at":"2026-09-25T07:24:44.780647541+00:00","output_file":"/home/grok-replay/.grok/sessions/%3Cworkspace%3E/00000000-0000-4000-8000-000000000002/terminal/call-d5448d70-380d-4a88-a7a0-2968806350ea-0.log"}]},"_meta":{"eventId":"00000000-0000-4000-8000-000000000002-172","agentTimestampMs":1790321099801}}}} +{"type":"expect_outbound","label":"request:session/prompt:t3-xai-prompt-2","frame":{"kind":"request","method":"session/prompt","params":{"sessionId":"00000000-0000-4000-8000-000000000001","prompt":[{"type":"text","text":"Reply exactly QUEUED_DONE."}],"_meta":{"promptId":"t3-xai-prompt-2","requestId":"t3-xai-prompt-2"}}}} +{"type":"emit_inbound","label":"notification:session/update:agent_message_chunk:t3-xai-prompt-2","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"QUEUED"}},"_meta":{"promptId":"t3-xai-prompt-2","agentTimestampMs":1790321085000,"updateType":"AgentMessageChunk","chunkId":1}}}} +{"type":"emit_inbound","label":"notification:session/update:agent_message_chunk:t3-xai-prompt-2","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"_DONE"}},"_meta":{"promptId":"t3-xai-prompt-2","agentTimestampMs":1790321085001,"updateType":"AgentMessageChunk","chunkId":2}}}} +{"type":"emit_inbound","label":"notification:_x.ai/session_notification:turn_completed:t3-xai-prompt-2","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"turn_completed","prompt_id":"t3-xai-prompt-2","stop_reason":"end_turn"}}}} +{"type":"emit_inbound","label":"notification:_x.ai/session/prompt_complete","frame":{"kind":"notification","method":"_x.ai/session/prompt_complete","params":{"sessionId":"00000000-0000-4000-8000-000000000001","promptId":"t3-xai-prompt-2","stopReason":"end_turn","agentResult":null}}} +{"type":"emit_inbound","label":"response:session/prompt","frame":{"kind":"response","method":"session/prompt","result":{"stopReason":"end_turn","_meta":{"sessionId":"00000000-0000-4000-8000-000000000001","requestId":"t3-xai-prompt-2","promptId":"t3-xai-prompt-2"}}}} +{"type":"expect_outbound","label":"request:session/prompt:t3-xai-prompt-3","frame":{"kind":"request","method":"session/prompt","params":{"sessionId":"00000000-0000-4000-8000-000000000001","prompt":[{"type":"text","text":"Reply exactly STEER_DONE."}],"_meta":{"promptId":"t3-xai-prompt-3","requestId":"t3-xai-prompt-3"}}}} +{"type":"emit_inbound","label":"notification:session/update:agent_message_chunk:t3-xai-prompt-3","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"STEER"}},"_meta":{"promptId":"t3-xai-prompt-3","agentTimestampMs":1790321086000,"updateType":"AgentMessageChunk","chunkId":1}}}} +{"type":"emit_inbound","label":"notification:session/update:agent_message_chunk:t3-xai-prompt-3","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"_DONE"}},"_meta":{"promptId":"t3-xai-prompt-3","agentTimestampMs":1790321086001,"updateType":"AgentMessageChunk","chunkId":2}}}} +{"type":"emit_inbound","label":"notification:_x.ai/session_notification:turn_completed:t3-xai-prompt-3","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"turn_completed","prompt_id":"t3-xai-prompt-3","stop_reason":"end_turn"}}}} +{"type":"emit_inbound","label":"notification:_x.ai/session/prompt_complete","frame":{"kind":"notification","method":"_x.ai/session/prompt_complete","params":{"sessionId":"00000000-0000-4000-8000-000000000001","promptId":"t3-xai-prompt-3","stopReason":"end_turn","agentResult":null}}} +{"type":"emit_inbound","label":"response:session/prompt","frame":{"kind":"response","method":"session/prompt","result":{"stopReason":"end_turn","_meta":{"sessionId":"00000000-0000-4000-8000-000000000001","requestId":"t3-xai-prompt-3","promptId":"t3-xai-prompt-3"}}}} {"type":"emit_inbound","label":"notification:session/update:tool_call_update:01a0d773-bfa9-72a1-b268-790ccebae43f","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000002","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","status":"completed","title":"[bg] sleep 20 (call-d54)","content":[{"type":"content","content":{"type":"text","text":"Command \"Sleep for 20 seconds as requested\" has been automatically moved to background because it exceeded auto-background timeout limit of 15s. Process is still running."}}],"rawOutput":{"type":"BackgroundTaskStarted","task_id":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","task_type":"bash","output_file":"/home/grok-replay/.grok/sessions/%3Cworkspace%3E/00000000-0000-4000-8000-000000000002/terminal/call-d5448d70-380d-4a88-a7a0-2968806350ea-0.log","status":"running","command":"sleep 20","summary":"Command \"Sleep for 20 seconds as requested\" has been automatically moved to background because it exceeded auto-background timeout limit of 15s. Process is still running.","retrieval_hint":"Use get_command_or_subagent_output with task_ids=[\"call-d5448d70-380d-4a88-a7a0-2968806350ea-0\"] when you need the output.","pid":2584853}},"_meta":{"totalTokens":13313,"eventId":"00000000-0000-4000-8000-000000000002-173","agentTimestampMs":1790321099802,"promptId":"01a0d773-bfa9-72a1-b268-790ccebae43f","streamStartMs":1790321082753,"turnStartMs":1790321082313,"updateType":"ToolCallUpdate","updateParams":{"toolCallId":"call-d5448d70-380d-4a88-a7a0-2968806350ea-0","status":"Completed"}}}}} {"type":"emit_inbound","label":"notification:_x.ai/session_notification:subagent_progress","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"subagent_progress","subagent_id":"00000000-0000-4000-8000-000000000002","attempt_id":"at1.bd80fbaa7c40408eb7999e19714ed4e1","parent_session_id":"00000000-0000-4000-8000-000000000001","child_session_id":"00000000-0000-4000-8000-000000000002","duration_ms":18067,"turn_count":1,"tool_call_count":1,"tokens_used":13480,"context_window_tokens":500000,"context_usage_pct":2,"tools_used":["run_terminal_command"],"error_count":0}}}} {"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:01a0d773-bfa9-72a1-b268-790ccebae43f","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000002","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}},"_meta":{"totalTokens":13480,"eventId":"00000000-0000-4000-8000-000000000002-177","agentTimestampMs":1790321101461,"promptId":"01a0d773-bfa9-72a1-b268-790ccebae43f","streamStartMs":1790321100147,"turnStartMs":1790321082313,"updateType":"AgentThoughtChunk","chunkId":1}}}} diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/input.ts b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/input.ts index 98f807dd38d9..b033edf88e5b 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/input.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/input.ts @@ -2,18 +2,28 @@ import type { OrchestratorFixtureInput } from "../shared.ts"; export const GROK_BACKGROUND_SUBAGENT_PROMPT = "Spawn one background subagent (run it in the background, do not wait for it) whose task is: run the shell command 'sleep 20' and then reply exactly SUBAGENT_DONE. As soon as the subagent is launched, end your turn by replying exactly ROOT_DONE without waiting for it."; +export const GROK_BACKGROUND_SUBAGENT_QUEUED_PROMPT = "Reply exactly QUEUED_DONE."; +export const GROK_BACKGROUND_SUBAGENT_STEER_PROMPT = "Reply exactly STEER_DONE."; const GROK_SUBAGENT_WAKE_LABEL = "notification:session/update:agent_message_chunk:subagent-completed-00000000-0000-4000-8000-000000000002"; -// Hold run 1 until subagent_finished and the adapter's settlement debounce. +// The queued and steered exchanges are synthetic, based on the recorded Grok +// 1.0.41 second-prompt exchange in message_steering. export function grokBackgroundSubagentInput(): OrchestratorFixtureInput { return { steps: [ { type: "message", text: GROK_BACKGROUND_SUBAGENT_PROMPT }, - { type: "finish_held_run", targetRunIndex: 1, status: "completed" }, + { type: "queue_message", text: GROK_BACKGROUND_SUBAGENT_QUEUED_PROMPT }, + { type: "capture_shell_snapshot", key: "while-subagent-runs" }, + { + type: "steer", + text: GROK_BACKGROUND_SUBAGENT_STEER_PROMPT, + targetRunIndex: 1, + targetReplyFinished: true, + }, { type: "release_replay_gate", label: GROK_SUBAGENT_WAKE_LABEL }, - { type: "finish_held_run", targetRunIndex: 2, status: "completed" }, + { type: "finish_held_run", targetRunIndex: 4, status: "completed" }, ], }; } diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/output.ts index 0a15fa9eb3dc..08adb890d72c 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/grok_background_subagent/output.ts @@ -9,7 +9,11 @@ import { backgroundNotifications, projectionFor, } from "../shared.ts"; -import { GROK_BACKGROUND_SUBAGENT_PROMPT } from "./input.ts"; +import { + GROK_BACKGROUND_SUBAGENT_PROMPT, + GROK_BACKGROUND_SUBAGENT_QUEUED_PROMPT, + GROK_BACKGROUND_SUBAGENT_STEER_PROMPT, +} from "./input.ts"; function runAssistantTexts( projection: ReturnType, @@ -20,7 +24,6 @@ function runAssistantTexts( ); } -// Only root-session subagent_finished ends background subagents; spawn results only acknowledge launch. export function assertGrokBackgroundSubagentOutput( result: OrchestratorV2ScenarioResult, transcript: ProviderReplayTranscript, @@ -28,13 +31,17 @@ export function assertGrokBackgroundSubagentOutput( assertBaseProjection({ result, transcript, - runCount: 2, - runStatuses: ["completed", "completed"], + runCount: 4, + runStatuses: ["completed", "completed", "completed", "completed"], }); const projection = projectionFor(result, transcript.scenario); assertSemanticProjectionIntegrity(projection); - assertUserMessagesInclude(projection, [GROK_BACKGROUND_SUBAGENT_PROMPT]); - const [rootRun, wakeRun] = projection.runs; + assertUserMessagesInclude(projection, [ + GROK_BACKGROUND_SUBAGENT_PROMPT, + GROK_BACKGROUND_SUBAGENT_QUEUED_PROMPT, + GROK_BACKGROUND_SUBAGENT_STEER_PROMPT, + ]); + const [rootRun, queuedRun, steerRun, wakeRun] = projection.runs; assert.lengthOf(projection.subagents, 1); const subagent = projection.subagents[0]; @@ -45,6 +52,7 @@ export function assertGrokBackgroundSubagentOutput( assert.equal(subagentItem?.status, "completed"); assert.equal(subagentItem?.runId, rootRun?.id); + // Run 1 completes with ROOT_DONE; the running subagent does not hold it. const subagentCompletedAt = result.domainEvents.findIndex( (event) => event.type === "turn-item.updated" && @@ -58,24 +66,46 @@ export function assertGrokBackgroundSubagentOutput( event.payload.status === "completed", ); assert.isAtLeast(subagentCompletedAt, 0); - assert.isAbove(rootCompletedAt, subagentCompletedAt, "run 1 completed before the subagent"); + assert.isAtLeast(rootCompletedAt, 0); + assert.isBelow(rootCompletedAt, subagentCompletedAt, "run 1 waited for the subagent"); assert.include(runAssistantTexts(projection, rootRun?.id), "ROOT_DONE"); - const rootDoneCompletedAt = result.domainEvents.findIndex( + // While the subagent runs, clients see the thread waiting on it, not working. + const waitingShell = result.capturedShellSnapshots + .get("while-subagent-runs") + ?.threads.find((thread) => thread.id === projection.thread.id); + assert.deepEqual( + waitingShell?.pendingBackgroundTasks?.map((task) => [ + task.kind, + task.kind === "subagent" ? task.childThreadId : undefined, + ]), + [["subagent", subagent?.childThreadId]], + "the Waiting strip names the running subagent", + ); + assert.isNull(waitingShell?.activeRunId, "no run is working while only the subagent runs"); + + // The queued message did not wait for the subagent, and the steer started a + // new turn: no finished reply was superseded. + assert.deepEqual(runAssistantTexts(projection, queuedRun?.id), ["QUEUED_DONE"]); + assert.deepEqual(runAssistantTexts(projection, steerRun?.id), ["STEER_DONE"]); + const queuedStartedAt = result.domainEvents.findIndex( (event) => - event.type === "turn-item.updated" && - event.payload.runId === rootRun?.id && - event.payload.type === "assistant_message" && - event.payload.text.trim() === "ROOT_DONE" && - event.payload.status === "completed", + event.type === "run.updated" && + event.payload.id === queuedRun?.id && + event.payload.status === "running", ); - assert.isAtLeast(rootDoneCompletedAt, 0); + assert.isAtLeast(queuedStartedAt, 0); assert.isBelow( - rootDoneCompletedAt, + queuedStartedAt, subagentCompletedAt, - "ROOT_DONE streamed until the subagent finished", + "the queued message waited for the subagent", + ); + assert.deepEqual( + projection.attempts.map((attempt) => attempt.status), + ["completed", "completed", "completed", "completed"], ); + // The subagent's own work, done after run 1 settled, lands in its child thread. if (subagent?.childThreadId == null) { throw new Error("The background subagent is missing its child thread."); } @@ -87,7 +117,16 @@ export function assertGrokBackgroundSubagentOutput( ), "the subagent's reply must land in its child thread", ); - assert.notInclude(runAssistantTexts(projection, rootRun?.id), "SUBAGENT_DONE"); + const childTools = child?.turnItems.filter((item) => item.type === "dynamic_tool") ?? []; + // Its poll of the sleep, made after run 1 completed, still projects. + assert.include( + childTools.map((item) => `${item.title}:${item.status}`), + "sleep 20 (call-d54):completed", + "the subagent's tool calls must land in its child thread", + ); + for (const run of projection.runs) { + assert.notInclude(runAssistantTexts(projection, run.id), "SUBAGENT_DONE"); + } assert.deepEqual(backgroundNotifications(projection), [ { diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/grok_transcript.ndjson b/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/grok_transcript.ndjson index 4e09c1a6039f..dc17bb8effea 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/grok_transcript.ndjson +++ b/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/grok_transcript.ndjson @@ -7,6 +7,8 @@ {"type":"emit_inbound","label":"task.server.started","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-b386-78b3-bc57-dc127c023559","update":{"sessionUpdate":"tool_call","toolCallId":"call-28397a12-0446-478e-a180-b06aefb56734-composer_call_kFjs8","title":"Task","rawInput":{"description":"Explore server architecture","prompt":"Audit the apps/server package and return a structured summary.","subagent_type":"generalPurpose","model":"composer-2.5-fast"}}}}} {"type":"emit_inbound","label":"task.web.started","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-b386-78b3-bc57-dc127c023559","update":{"sessionUpdate":"tool_call","toolCallId":"call-28397a12-0446-478e-a180-b06aefb56734-composer_call_Olv1X","title":"Task","rawInput":{"description":"Explore web client architecture","prompt":"Audit the apps/web package and return a structured summary.","subagent_type":"generalPurpose","model":"composer-2.5-fast"}}}}} {"type":"emit_inbound","label":"child.server.progress","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e192-7c41-9e9d-b406bc3459c8","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"I'll audit `apps/server` by mapping its layout first, then reading entry points, WebSocket/auth/session code, and tests.\n"}}}}} +{"type":"emit_inbound","label":"child.server.tool.started","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e192-7c41-9e9d-b406bc3459c8","update":{"sessionUpdate":"tool_call","toolCallId":"call-child-server-list-files","title":"List apps/server","kind":"execute","status":"in_progress","rawInput":{"command":"ls apps/server/src"}}}}} +{"type":"emit_inbound","label":"child.server.tool.completed","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e192-7c41-9e9d-b406bc3459c8","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-child-server-list-files","status":"completed","rawOutput":{"type":"Text","text":"orchestration-v2\nprovider\nws.ts"}}}}} {"type":"emit_inbound","label":"child.web.progress","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e197-7833-a8e4-ad38f2bd5b4c","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"Auditing `apps/web`: mapping package structure and searching source for state, WebSocket, components, performance, and tests.\n"}}}}} {"type":"emit_inbound","label":"child.server.result","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e192-7c41-9e9d-b406bc3459c8","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"# `apps/server` Audit Summary\n\nLayered Effect architecture with WebSocket RPC and durable provider sessions."}}}}} {"type":"emit_inbound","label":"child.web.result","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"019f0220-e197-7833-a8e4-ad38f2bd5b4c","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"# `apps/web` Audit Summary\n\nHybrid Effect Atom and Zustand state with centralized reconnect handling."}}}}} diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/output.ts b/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/output.ts index 64f9fc79e005..6ee7037ac05d 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/output.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/grok_subagent_lineage/output.ts @@ -6,6 +6,7 @@ import { assertBaseProjection, assertExecutionNodeKinds, assertNoExtraAppRunsForProviderChildren, + assertTurnItemTypeSequence, assertTurnItemTypes, projectionFor, } from "../shared.ts"; @@ -79,7 +80,18 @@ export function assertGrokSubagentLineageOutput( ), "the native child session must fork from the parent provider turn", ); - assertTurnItemTypes(child, ["user_message", "assistant_message"]); + // The server child runs a tool before its Task result names its session: + // that call still lands in the child thread, in order, with its result. + if (expected.sessionId === EXPECTED_CHILDREN[0].sessionId) { + assertTurnItemTypeSequence(child, ["user_message", "assistant_message", "dynamic_tool"]); + const tool = child.turnItems.find((item) => item.type === "dynamic_tool"); + assert.deepEqual(tool?.type === "dynamic_tool" ? tool.input : null, { + command: "ls apps/server/src", + }); + assert.equal(tool?.status, "completed"); + } else { + assertTurnItemTypeSequence(child, ["user_message", "assistant_message"]); + } const assistant = child.messages.find((message) => message.role === "assistant"); assert.isDefined(assistant); assert.include(assistant.text, expected.first); diff --git a/apps/server/src/orchestration-v2/testkit/fixtures/shared.ts b/apps/server/src/orchestration-v2/testkit/fixtures/shared.ts index 125d88a94034..d63e417be711 100644 --- a/apps/server/src/orchestration-v2/testkit/fixtures/shared.ts +++ b/apps/server/src/orchestration-v2/testkit/fixtures/shared.ts @@ -227,6 +227,11 @@ export type OrchestratorFixtureInputStep = readonly text: string; readonly attachments?: ReadonlyArray; readonly targetRunIndex: number; + /** + * The target's reply already finished, as a client that still lists it + * running would send: steer without waiting for it to be steerable. + */ + readonly targetReplyFinished?: true; } | { readonly type: "restart"; @@ -748,11 +753,13 @@ export function materializeFixtureInput(input: { break; case "steer": messageIndex += 1; - steps.push({ - type: "await_run_steerable", - threadId: ids.threadId, - runId: runIdFor(step.targetRunIndex), - }); + if (step.targetReplyFinished !== true) { + steps.push({ + type: "await_run_steerable", + threadId: ids.threadId, + runId: runIdFor(step.targetRunIndex), + }); + } pushDispatch( dispatchMessageCommand({ commandId: yield* idAllocator.allocate.command({ diff --git a/packages/provider-acp/src/server/adapter.ts b/packages/provider-acp/src/server/adapter.ts index 21fe31158890..7a370a7bc2a0 100644 --- a/packages/provider-acp/src/server/adapter.ts +++ b/packages/provider-acp/src/server/adapter.ts @@ -534,6 +534,11 @@ export interface AcpAdapterV2Flavor { cwd: string, ) => Effect.Effect; readonly subagentsIdleOnTurnCompletion?: boolean; + /** + * Subagents run on after the root's reply ends and report their own end: they never hold the + * turn open, and their child threads keep receiving updates once it completes. + */ + readonly subagentsOutliveTurn?: boolean; /** * Subagents the agent goes on running after its prompt, with no update when one ends. Asked * when the prompt ends and when a reply the agent started by itself ends; the native task ids @@ -1946,6 +1951,7 @@ type AcpCarryoverSubagents = { /** The root's subscriber keeps ingesting, so late child terminals can still project. */ readonly projectsLateTerminals: boolean; readonly subagents: ReadonlyArray; + readonly pendingSubagentNotifications: ActiveAcpTurn["pendingSubagentNotifications"]; }; type PendingRuntimeRequest = { @@ -1990,6 +1996,8 @@ const emptySnapshotState = (): SnapshotMessageState => ({ /** How long a Stop waits for the agent to confirm before the user is offered Force stop. */ const ACP_STOP_CONFIRM_TIMEOUT_MS = 10_000; const TOOL_UPDATE_PERSIST_EVERY = 10; +// Bounds a child session's tool calls held until its session is named; its reply text is kept. +const MAX_PENDING_SUBAGENT_TOOL_NOTIFICATIONS = 1_000; // CommandOutputHub reads the last 256 KiB; retain more so its display cut stays unchanged. const MAX_AGENT_TERMINAL_OUTPUT_CHARS = 320 * 1024; @@ -3500,7 +3508,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const projectSubagentNotification = Effect.fnUntraced(function* ( subagent: ActiveAcpSubagent, notification: EffectAcpSchema.SessionNotification, - owner: ActiveAcpTurn, + owner: ActiveAcpTurn | undefined, ) { const update = notification.update; if (update.sessionUpdate === "agent_message_chunk") { @@ -3828,7 +3836,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( context.pendingSubagentNotifications.delete(childSessionId); yield* Effect.forEach( buffered, - (notification) => projectSubagentNotification(subagent, notification, context), + (notification) => projectChildSessionUpdate(subagent, notification, context), { concurrency: 1, discard: true }, ); } @@ -4294,10 +4302,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( const status = toolStatus(tool.status); if (status === "pending" || status === "running") return true; } - // finalizeTurn idles these subagents, so waiting on them would never settle. + // finalizeTurn idles these subagents, or they carry over; waiting on them would never settle. if ( - flavor.subagentsIdleOnTurnCompletion === true && - flavor.subagentsOutlivingPrompt === undefined + (flavor.subagentsIdleOnTurnCompletion === true && + flavor.subagentsOutlivingPrompt === undefined) || + flavor.subagentsOutliveTurn === true ) return false; for (const subagent of context.subagents.values()) { @@ -5913,13 +5922,32 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (yield* Ref.get(stoppedRunQuarantine)) { return; } - const bufferOutcome = yield* bufferPostSettleWake(notification); // Completed roots retain subscribers and can project terminals now; interrupted roots may lack runId ownership. const carryover = yield* Ref.get(carryoverSubagents); + const idleRootSessionId = yield* Ref.get(activeSessionId); const rootTerminalCanStillProject = carryover !== null && - carryover.sessionId === (yield* Ref.get(activeSessionId)) && + carryover.sessionId === idleRootSessionId && carryover.projectsLateTerminals; + if ( + rootTerminalCanStillProject && + flavor.subagentsOutliveTurn === true && + notification.sessionId !== idleRootSessionId + ) { + const subagent = carryover.subagents.find( + (candidate) => candidate.childSessionId === notification.sessionId, + ); + if (subagent !== undefined) { + yield* projectChildSessionUpdate(subagent, notification, undefined); + } else { + bufferUnnamedChildSessionUpdate( + carryover.pendingSubagentNotifications, + notification, + ); + } + return; + } + const bufferOutcome = yield* bufferPostSettleWake(notification); const projectCarryover = rootTerminalCanStillProject; let carryoverTerminalized = false; if ( @@ -6004,61 +6032,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( if (context.finalized) return; if (flavor.extractSubagentUpdate === undefined) return; const subagent = context.subagentsBySessionId.get(notification.sessionId); - if ( - update.sessionUpdate === "tool_call" || - update.sessionUpdate === "tool_call_update" - ) { - if (subagent === undefined) return; - const nativeTaskId = - subagent.task.nativeTaskRef?.nativeId ?? String(subagent.task.id); - for (const event of parseSessionUpdateEvent(notification).events) { - if (event._tag !== "ToolCallUpdated") continue; - const toolCall = flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall; - const subagentUpdate = extractSubagentUpdate(toolCall); - if (subagentUpdate !== undefined) { - if ( - subagentUpdate.nativeTaskId === nativeTaskId || - subagentUpdate.childSessionId === notification.sessionId - ) { - yield* emitSubagent(context, subagentUpdate); - } - continue; - } - yield* projectSubagentTool( - subagent, - notification.sessionId, - toolCall, - undefined, - context, - ); - } - return; - } - if (update.sessionUpdate === "tool_call_content_chunk") { - if (subagent !== undefined) - yield* projectSubagentTool( - subagent, - notification.sessionId, - update, - undefined, - context, - ); - return; - } - const isDisplayableAssistantUpdate = - (update.sessionUpdate === "agent_message_chunk" && - acpContentBlockDisplayText(update.content) !== undefined) || - (update.sessionUpdate === "agent_message" && update.content !== undefined); - if (!isDisplayableAssistantUpdate) { - return; - } if (subagent !== undefined) { - yield* projectSubagentNotification(subagent, notification, context); - return; + yield* projectChildSessionUpdate(subagent, notification, context); + } else { + bufferUnnamedChildSessionUpdate(context.pendingSubagentNotifications, notification); } - const buffered = context.pendingSubagentNotifications.get(notification.sessionId) ?? []; - buffered.push(notification); - context.pendingSubagentNotifications.set(notification.sessionId, buffered); return; } // The activeSessionId yield may let idle/prompt settlement finalize this context. @@ -6852,6 +6830,74 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( return true; }); + /** `owner` is the running turn; without one the subagent was carried past its root. */ + const projectChildSessionUpdate = Effect.fnUntraced(function* ( + subagent: ActiveAcpSubagent, + notification: EffectAcpSchema.SessionNotification, + owner: ActiveAcpTurn | undefined, + ): Effect.fn.Return { + const update = notification.update; + if (update.sessionUpdate !== "tool_call" && update.sessionUpdate !== "tool_call_update") { + yield* projectSubagentNotification(subagent, notification, owner); + return; + } + const nativeTaskId = subagent.task.nativeTaskRef?.nativeId ?? String(subagent.task.id); + for (const event of parseSessionUpdateEvent(notification).events) { + if (event._tag !== "ToolCallUpdated") continue; + const toolCall = flavor.normalizeToolCall?.(event.toolCall) ?? event.toolCall; + const subagentUpdate = extractSubagentUpdate(toolCall); + if (subagentUpdate === undefined) { + yield* projectSubagentTool( + subagent, + notification.sessionId, + toolCall, + undefined, + owner, + ); + continue; + } + if ( + subagentUpdate.nativeTaskId !== nativeTaskId && + subagentUpdate.childSessionId !== notification.sessionId + ) + continue; + if (owner !== undefined) yield* emitSubagent(owner, subagentUpdate); + else + yield* updateCarryoverSubagentStatus( + nativeTaskId, + subagentUpdate.status, + subagentUpdate.result, + ); + } + }); + + const bufferUnnamedChildSessionUpdate = ( + pending: ActiveAcpTurn["pendingSubagentNotifications"], + notification: EffectAcpSchema.SessionNotification, + ) => { + const update = notification.update; + const isTool = + update.sessionUpdate === "tool_call" || + update.sessionUpdate === "tool_call_update" || + update.sessionUpdate === "tool_call_content_chunk"; + const isReply = + (update.sessionUpdate === "agent_message_chunk" && + acpContentBlockDisplayText(update.content) !== undefined) || + (update.sessionUpdate === "agent_message" && update.content !== undefined); + // Grok names a Task's child session only in its result, after the child already ran. + if (!isReply && !(isTool && flavor.subagentsOutliveTurn === true)) return; + const buffered = pending.get(notification.sessionId) ?? []; + const isToolFrame = (frame: EffectAcpSchema.SessionNotification) => + frame.update.sessionUpdate.startsWith("tool_call"); + if ( + isTool && + buffered.filter(isToolFrame).length >= MAX_PENDING_SUBAGENT_TOOL_NOTIFICATIONS + ) + buffered.splice(buffered.findIndex(isToolFrame), 1); + buffered.push(notification); + pending.set(notification.sessionId, buffered); + }; + const finishSubagentFromNotice = Effect.fnUntraced(function* (notice: { readonly childSessionId: string; readonly status: "completed" | "failed" | "cancelled"; @@ -8775,6 +8821,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( sessionId, projectsLateTerminals: settledStatus === "completed" || childTurnsContinue, subagents: subagentsRequiringCarryover, + pendingSubagentNotifications: context.pendingSubagentNotifications, }); } } @@ -9531,6 +9578,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* ( let rehydratedCarryoverSubagents: ReadonlyArray = []; if (carryover !== null && carryover.sessionId === requestedSessionId) { rehydratedCarryoverSubagents = carryover.subagents; + for (const [sessionId, buffered] of carryover.pendingSubagentNotifications) { + context.pendingSubagentNotifications.set(sessionId, buffered); + } for (const subagent of carryover.subagents) { const nativeId = subagent.task.nativeTaskRef?.nativeId ?? null; if (nativeId !== null) { diff --git a/packages/provider-grok/src/server/adapter.ts b/packages/provider-grok/src/server/adapter.ts index db5a136697c1..04537bd830a6 100644 --- a/packages/provider-grok/src/server/adapter.ts +++ b/packages/provider-grok/src/server/adapter.ts @@ -448,6 +448,7 @@ export function makeGrokAcpAdapterFlavor( isPersistentBackgroundTool: isXAiPersistentMonitor, isProviderWakeNotification: isXAiTaskCompletedWakeNotification, deferFinalizeForBackgroundWork: true, + subagentsOutliveTurn: true, enablePostSettleContinuation: true, ...(options.assertComplete === undefined ? {} : { assertComplete: options.assertComplete }), };