diff --git a/src/tests/v1/incomplete-tools.test.ts b/src/tests/v1/incomplete-tools.test.ts index 86d23e29..6143b1d2 100644 --- a/src/tests/v1/incomplete-tools.test.ts +++ b/src/tests/v1/incomplete-tools.test.ts @@ -267,6 +267,12 @@ describe("incomplete foreground tools", () => { await expect(prompt()).resolves.toMatchObject({ stopReason: "end_turn" }); }); + it("does not claim a workflow agent's permission tool as a foreground tool", async () => { + const { prompt } = createTestSession(workflowAgentPermissionMessages); + + await expect(prompt()).resolves.toMatchObject({ stopReason: "end_turn" }); + }); + it("does not treat a request to run in the background as a confirmed handoff", async () => { const { prompt } = createTestSession(unconfirmedBackgroundToolMessages); @@ -1442,6 +1448,44 @@ async function* permissionOnlyToolMessages(input: Pushable, agent: ClaudeAc yield successfulResultMessage(); } +/** A workflow agent asks for permission while the turn runs. The workflow's + * task_started names the Workflow call, not the agent, and the agent's + * tool_use and tool_result never stream on the root query. */ +async function* workflowAgentPermissionMessages(input: Pushable, agent: ClaudeAcpAgent) { + yield* echoNextPrompt(input); + yield toolStart("workflow", null, "Workflow"); + yield toolUse("workflow", null, "Workflow", { script: "await agent('Stamp the date')" }); + yield { + type: "system", + subtype: "task_started", + session_id: sessionId, + task_id: "wzr1bosoo", + tool_use_id: "workflow", + description: "Stamp the date", + task_type: "local_workflow", + }; + yield { + ...toolResult("workflow"), + message: { + role: "user", + content: [ + { + type: "tool_result", + tool_use_id: "workflow", + content: "Workflow launched in background. Task ID: wzr1bosoo", + }, + ], + }, + }; + await agent.canUseTool(sessionId)("Bash", { command: "date > stamp.txt; cat stamp.txt" }, { + toolUseID: toolCallId, + agentID: "a2a57f1ad0803996a", + signal: new AbortController().signal, + suggestions: [], + } as any); + yield successfulResultMessage(); +} + async function* completedToolMessages(input: Pushable) { yield* echoNextPrompt(input); yield toolStart(); diff --git a/src/v1/acp-agent.ts b/src/v1/acp-agent.ts index 9628e13f..a78da842 100644 --- a/src/v1/acp-agent.ts +++ b/src/v1/acp-agent.ts @@ -8686,6 +8686,7 @@ export class ClaudeAcpAgent { params.toolCall.toolCallId, toolInput, parentToolUseId, + agentID, signal, params.sessionId, previewContent, @@ -8780,13 +8781,17 @@ export class ClaudeAcpAgent { * always resolve. Since the streamed path never completes those calls, they * are resolved at tool_result time instead (see `toAcpNotifications`). * `parentToolUseId` attributes a subagent's tool call to the Agent/Task call - * that spawned it, matching the streamed path's `_meta`. */ + * that spawned it, matching the streamed path's `_meta`. `agentID` is + * `canUseTool`'s, set for every subagent's tool call, including a workflow + * agent's, which has no `parentToolUseId`. Such a call is not recorded as + * the turn's foreground work. */ private async ensureToolCallEmitted( sessionId: string, toolName: string, toolCallId: string, toolInput: unknown, parentToolUseId?: string, + agentID?: string, signal?: AbortSignal, notificationSessionId: string = sessionId, previewContent?: ToolCallContent[], @@ -8810,11 +8815,12 @@ export class ClaudeAcpAgent { } session.emittedToolCalls.add(toolCallId); if (parentToolUseId) recordToolCallParent(session, toolCallId, parentToolUseId); - // SandboxNetworkAccess is a permission-only callback, not an executing - // tool: the SDK does not send a tool_result for its synthetic id. + // A subagent's tool call is not the turn's foreground work, even when no + // parent was found for it: a workflow agent's tool_result never streams on + // the root query. SandboxNetworkAccess is a permission-only callback, not + // an executing tool: the SDK does not send a tool_result for its synthetic id. if ( - !parentToolUseId && - notificationSessionId === sessionId && + !agentID && toolName !== "SandboxNetworkAccess" && // Neither does it send one for a question that Claude Code asks on its own. !isConfirmQuestion(toolCallId) @@ -8853,7 +8859,8 @@ export class ClaudeAcpAgent { * emitted tool call and of its requests: the subagent's child session once * it is announced, else the root session. In the rare SDK ordering where * the callback beats the spawning Agent/Task frame, the root keeps it; the - * later frame announces the child with its correct nested lineage. + * later frame announces the child with its correct nested lineage. A + * workflow agent gets no task_started, so the root always keeps its tool use. */ private toolUseOwner( session: Session, @@ -8968,7 +8975,8 @@ export class ClaudeAcpAgent { // defines a subagent's `agent_id` as its task id). Should the consumer // lose the race with task_started, the lookup misses and the request // goes out unattributed; log it so the miss is observable rather than - // silent. + // silent. A workflow agent's request always misses: it has no + // task_started. this.logger.log( `[claude-agent-acp] No parent tool_use recorded for subagent ${agentID}; ` + `sending the ${toolName} permission request unattributed`, @@ -8988,6 +8996,7 @@ export class ClaudeAcpAgent { toolUseID, toolInput, parentToolUseId, + agentID, signal, permissionSessionId, );