From c02931621a4a3a760cd7f7d51f543fd5910150ca Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 20:45:39 +0000 Subject: [PATCH] Agent Flow: ACP chats of Claude Code draw their sub-agents (#459) Claude Code names each tool and the sub-agent that ran it only in _meta.claudeCode (toolName, parentToolUseId) on ACP updates. The shared core mapper now reads both: an Agent/Task tool is a delegate that spawns a sub-agent named after its description, and tools (and permissions) stamped with its call id run as that sub-agent, so the hangar launches their UFOs. The IDE side that carries _meta into these events lands on the ide branch. - Tests: core mapper + dataset actors. - Docs: hangar.md (where sub-agents come from), CHANGELOG. --- apps/vscode-extension/CHANGELOG.md | 2 +- .../src/core/agent-flow/acpSessionEvents.ts | 46 +++++++++++++++++++ .../test/analysis/agentFlowCore.test.cjs | 30 ++++++++++++ docs/agent-flow/hangar.md | 16 +++++++ 4 files changed, 93 insertions(+), 1 deletion(-) diff --git a/apps/vscode-extension/CHANGELOG.md b/apps/vscode-extension/CHANGELOG.md index 744d7007..6741168a 100644 --- a/apps/vscode-extension/CHANGELOG.md +++ b/apps/vscode-extension/CHANGELOG.md @@ -24,7 +24,7 @@ - A function the agent writes from scratch gets a new floor, built in front of you when the step plays. - Resting the pointer on a floor names its function, its lines and how many times it was read and written. - Line data comes from Claude Code's edit and read results and from the Code-XR IDE's agent locations; a step without it lights the whole building, as before. - - Sub-agent hangar: when the agent delegates, its UFO docks on the control tower and coordinates; each sub-agent waits in a hangar beside it, takes off on its first step, stays linked to the tower by a radio line and lands back in its bay when done. The hangar and the tower have their own pinnable cards. + - Sub-agent hangar: when the agent delegates, its UFO docks on the control tower and coordinates; each sub-agent waits in a hangar beside it, takes off on its first step, stays linked to the tower by a radio line and lands back in its bay when done. The hangar and the tower have their own pinnable cards. In the IDE, Claude Code chats draw their sub-agents too (the Agent call and the tools each sub-agent runs are read from the chat itself). - The timeline is time travel: at any step the city shows the project as it was then, and playing forward builds each change. - Files created, deleted, moved or renamed appear and disappear in their step, whatever tool did it: shell commands (`rm`, `git rm`, `mv`, `git mv`, `cp`, `touch`, `> file`, `tee`, `sed -i` and their PowerShell and cmd forms), Codex patches (including moves) and the IDE's agents. - A function the agent deletes keeps its floor until that step, then the floor comes down. diff --git a/apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts b/apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts index bef4c351..be442837 100644 --- a/apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts +++ b/apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts @@ -31,6 +31,10 @@ export type AcpSessionEventLike = readonly locations?: readonly { readonly path: string; readonly line?: number }[]; readonly content?: readonly AcpToolContent[]; readonly rawInput?: string; + /** The agent's own tool name (Claude Code's `_meta.claudeCode.toolName`: `Agent`, `Task`, `Read`...). */ + readonly toolName?: string; + /** The Agent/Task call a sub-agent's tool belongs to (Claude Code's `_meta.claudeCode.parentToolUseId`). */ + readonly parentToolUseId?: string; } | { readonly type: 'terminal-output'; readonly terminalId: string; readonly exitCode?: number | null } /** The agent asks the person before a tool runs; resolved by `permission-resolved`. */ @@ -96,6 +100,25 @@ function change(changes: readonly { metric: string; before: number | null; after } type StepEvent = Extract; + +/** Claude Code's tools that launch a sub-agent. */ +const DELEGATE_TOOLS: ReadonlySet = new Set(['Agent', 'Task']); + +/** The name a delegate step gives its sub-agent: the task's description, else its type, else the card title. */ +function spawnLabel(rawInput: string | undefined, title: string | undefined): string | undefined { + if (rawInput) { + try { + const parsed = JSON.parse(rawInput) as Record; + for (const key of ['description', 'subagent_type']) { + const value = parsed[key]; + if (typeof value === 'string' && value.trim()) { return value.trim(); } + } + } catch { + // A capped or partial input: fall back to the title. + } + } + return title?.trim() ? title.trim() : undefined; +} type AcpEventOf = Extract; function metricDeltaEvent(event: AcpEventOf<'metric-delta'>, at: number): AgentTraceEvent { @@ -192,6 +215,10 @@ export class AcpSessionEventMapper { private readonly terminals = new Map(); /** Where each tool works, so a permission asked for it waits at the same building. */ private readonly locations = new Map(); + /** Claude Code's tool name per tool, kept from the call for updates that omit it. */ + private readonly toolNames = new Map(); + /** The sub-agent (its Agent/Task call id) each tool ran in. */ + private readonly owners = new Map(); private turnOpen = false; public map(event: AcpSessionEventLike, at: number): AgentTraceEvent[] { @@ -230,6 +257,8 @@ export class AcpSessionEventMapper { }; const locations = ask.toolCallId ? this.locations.get(ask.toolCallId) : undefined; if (locations && locations.length > 0) { step.locations = locations; } + const owner = ask.toolCallId ? this.owners.get(ask.toolCallId) : undefined; + if (owner) { step.actor = owner; step.subagent = true; } return step; } @@ -259,6 +288,23 @@ export class AcpSessionEventMapper { } private tool(event: AcpEventOf<'tool'>, at: number): AgentTraceEvent { + if (event.toolName) { this.toolNames.set(event.id, event.toolName); } + if (event.parentToolUseId) { this.owners.set(event.id, event.parentToolUseId); } + const toolName = this.toolNames.get(event.id); + const step = this.toolBody(event, at); + if (toolName && DELEGATE_TOOLS.has(toolName)) { + // Claude Code reports its Agent/Task call as a `think` tool: it launches a sub-agent. + step.kind = 'delegate'; + this.kinds.set(event.id, 'delegate'); + const label = spawnLabel(event.rawInput, event.title); + step.spawns = label ? { id: event.id, label } : { id: event.id }; + } + const owner = this.owners.get(event.id); + if (owner) { step.actor = owner; step.subagent = true; } + return step; + } + + private toolBody(event: AcpEventOf<'tool'>, at: number): StepEvent { const initialKind = event.toolKind ? acpKindToStep(event.toolKind) : (this.kinds.get(event.id) ?? 'other'); const locations: AgentTraceLocation[] = (event.locations ?? []).map((loc) => (loc.line ? { path: loc.path, line: loc.line } : { path: loc.path })); const content = this.readContent(event, initialKind, locations); diff --git a/apps/vscode-extension/test/analysis/agentFlowCore.test.cjs b/apps/vscode-extension/test/analysis/agentFlowCore.test.cjs index 27bfe8c2..9c5e490e 100644 --- a/apps/vscode-extension/test/analysis/agentFlowCore.test.cjs +++ b/apps/vscode-extension/test/analysis/agentFlowCore.test.cjs @@ -156,6 +156,36 @@ test('ACP chat events map to trace events: kinds kept across updates, diffs as w assert.equal(all.length, 8); }); +test('ACP chats of Claude Code draw their sub-agents: the Agent call delegates and the tools stamped with it run in the sub-agent', () => { + const mapper = new AcpSessionEventMapper(); + const all = []; + const push = (event, at) => all.push(...mapper.map(event, at)); + const sub = { parentToolUseId: 'agent-1' }; + push({ type: 'user', text: 'Map the parser' }, 1); + // Claude Code's adapter reports the Agent tool as a `think` card; its `_meta` names the tool. + push({ type: 'tool', id: 'agent-1', title: 'Map the parser', toolKind: 'think', status: 'pending', toolName: 'Agent', rawInput: JSON.stringify({ description: 'Map the parser', subagent_type: 'Explore', prompt: 'p' }) }, 2); + push({ type: 'tool', id: 'agent-1', status: 'in_progress' }, 3); + push({ type: 'tool', id: 'r1', title: 'Read parser.ts', toolKind: 'read', status: 'in_progress', toolName: 'Read', locations: [{ path: `${ROOT}/src/parser.ts` }], ...sub }, 4); + push({ type: 'tool', id: 'r1', status: 'completed' }, 5); + push({ type: 'tool', id: 'x1', title: 'npm test', toolKind: 'execute', status: 'in_progress', toolName: 'Bash', rawInput: JSON.stringify({ command: 'npm test' }), ...sub }, 6); + push({ type: 'permission', id: 'p1', toolCallId: 'x1', title: 'Run npm test?' }, 7); + push({ type: 'tool', id: 'agent-1', status: 'completed' }, 8); + push({ type: 'turn-end', stopReason: 'end_turn' }, 9); + const steps = all.filter((event) => event.type === 'step'); + assert.deepEqual(steps[0].spawns, { id: 'agent-1', label: 'Map the parser' }); + assert.equal(steps[0].kind, 'delegate'); + assert.equal(steps[0].actor, undefined, 'the main agent made the Agent call'); + assert.equal(steps[1].kind, 'delegate', 'an update without the tool name stays a delegate'); + assert.deepEqual(steps.slice(2, 4).map((step) => [step.id, step.actor, step.subagent]), [['r1', 'agent-1', true], ['r1', 'agent-1', true]]); + assert.equal(steps[4].actor, 'agent-1'); + assert.deepEqual([steps[5].kind, steps[5].actor], ['wait', 'agent-1'], 'a permission asked inside the sub-agent waits there'); + const { dataset } = buildAgentFlowDataset({ root: ROOT, session: session(), events: all, baseMetrics: [{ path: 'src/parser.ts', loc: 40 }], revision: 1 }); + assert.deepEqual(dataset.actors.map((actor) => [actor.id, actor.kind, actor.label, actor.spawnStep, actor.steps]), [ + ['main', 'main', 'Test', null, 1], + ['agent-1', 'subagent', 'Map the parser', 0, 3], + ]); +}); + // ---- layout ----------------------------------------------------------------- function overlaps(a, b) { diff --git a/docs/agent-flow/hangar.md b/docs/agent-flow/hangar.md index ebc98586..9a3c7d93 100644 --- a/docs/agent-flow/hangar.md +++ b/docs/agent-flow/hangar.md @@ -64,6 +64,22 @@ saucers are placed directly, the doors snap and no packets are sent. The runtime is shared, so the VS Code extension and the Code-XR IDE get it alike; the core and the server are unchanged. +### Where sub-agents come from + +The hangar only has something to launch when the trace names sub-agents +(`actor` on a step, `spawns` on its delegate): + +- Claude Code transcripts (the extension, and IDE chats followed by their + transcript): `claudeCodeTranscript.ts` reads the Agent/Task call and each + `subagents/agent-.jsonl`. +- IDE chats over ACP (#459): Claude Code's adapter names each tool and its + sub-agent only in `_meta.claudeCode` (`toolName`, `parentToolUseId`). The + IDE keeps both on its tool events and transcript cards (`acpMapping.ts`, + `agentSession.ts`, `agentFlowFeed.ts`), and `acpSessionEvents.ts` turns an + `Agent`/`Task` tool into a `delegate` that spawns a sub-agent and runs every + tool stamped with its call id as that sub-agent. Chats stored before #459 + have no `_meta` and show the main agent only. + ## Tests - `test/analysis/agentFlowRuntime.test.cjs`: docked/controlling, bay plan