From aea254eeeacacdfa97aa26a02612b2cd00c7ffb9 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 20:43:09 +0000 Subject: [PATCH] Agent Flow: draw the sub-agents of IDE Claude Code chats (#459) The IDE reads its chats over ACP, where Claude Code names each tool and the sub-agent that ran it only in _meta.claudeCode (toolName, parentToolUseId). The IDE dropped _meta, so the Agent call was a think step and every tool of a sub-agent was drawn on the main agent: the hangar never launched a UFO. - IDE: keep toolName and parentToolUseId on tool events and transcript cards, and replay them when a chat is seeded. - Core: an Agent/Task tool is a delegate that spawns a sub-agent named after its description; tools (and permissions) stamped with its call id run as that sub-agent. - Tests: core mapper + dataset actors, IDE _meta mapping, feed live and seeded. - Docs: hangar.md (where sub-agents come from), CHANGELOG. --- apps/ide/src/main/agentSessions/acpMapping.ts | 18 ++++++++ .../src/main/agentSessions/agentFlowFeed.ts | 2 + apps/ide/src/shared/agentSession.ts | 12 +++++ apps/ide/test/agentFlowFeed.test.ts | 33 +++++++++++++ apps/ide/test/agentSessions.test.ts | 5 ++ 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 +++++++ 9 files changed, 163 insertions(+), 1 deletion(-) diff --git a/apps/ide/src/main/agentSessions/acpMapping.ts b/apps/ide/src/main/agentSessions/acpMapping.ts index fad6262c..e60c0336 100644 --- a/apps/ide/src/main/agentSessions/acpMapping.ts +++ b/apps/ide/src/main/agentSessions/acpMapping.ts @@ -120,6 +120,23 @@ export function rawInputText(value: unknown): string | undefined { return text.length > MAX_RAW_INPUT ? `${text.slice(0, MAX_RAW_INPUT)}...` : text; } +/** + * What Claude Code's adapter stamps on a tool update's `_meta.claudeCode`: the + * tool's own name (`Agent`, `Read`...) and, for a call a sub-agent made, the + * Agent/Task call it belongs to. Agent Flow needs both to draw sub-agents. + */ +export function claudeCodeToolMeta(meta: unknown): { toolName?: string; parentToolUseId?: string } { + const claudeCode = meta && typeof meta === 'object' ? (meta as Record).claudeCode : undefined; + if (!claudeCode || typeof claudeCode !== 'object') { + return {}; + } + const { toolName, parentToolUseId } = claudeCode as Record; + return { + ...(typeof toolName === 'string' && toolName ? { toolName } : {}), + ...(typeof parentToolUseId === 'string' && parentToolUseId ? { parentToolUseId } : {}), + }; +} + /** A `tool_call` or `tool_call_update` (also the `toolCall` of a permission request) as a `tool` event. */ export function toolEvent(update: ToolCallUpdate, ctx: MappingContext, isNew: boolean): Extract { const status = toolStatusOf(update.status); @@ -133,6 +150,7 @@ export function toolEvent(update: ToolCallUpdate, ctx: MappingContext, isNew: bo ...(update.locations ? { locations: mapLocations(update.locations) } : {}), ...(update.content ? { content: mapToolContent(update.content, ctx) } : {}), ...(rawInput !== undefined ? { rawInput } : {}), + ...claudeCodeToolMeta(update._meta), }; } diff --git a/apps/ide/src/main/agentSessions/agentFlowFeed.ts b/apps/ide/src/main/agentSessions/agentFlowFeed.ts index d5badf9a..a3523004 100644 --- a/apps/ide/src/main/agentSessions/agentFlowFeed.ts +++ b/apps/ide/src/main/agentSessions/agentFlowFeed.ts @@ -103,6 +103,8 @@ export function transcriptAsEvents(items: readonly TranscriptItem[]): { readonly event: { type: 'tool', id: item.id, title: item.title, toolKind: item.toolKind, status: item.status, locations: item.locations, content: item.content, ...(item.rawInput !== undefined ? { rawInput: item.rawInput } : {}), + ...(item.toolName !== undefined ? { toolName: item.toolName } : {}), + ...(item.parentToolUseId !== undefined ? { parentToolUseId: item.parentToolUseId } : {}), }, }); } else if (item.kind === 'permission') { diff --git a/apps/ide/src/shared/agentSession.ts b/apps/ide/src/shared/agentSession.ts index 7ca7cb00..80c8eea9 100644 --- a/apps/ide/src/shared/agentSession.ts +++ b/apps/ide/src/shared/agentSession.ts @@ -204,6 +204,10 @@ export type TranscriptItem = readonly content: readonly ToolContentView[]; /** The raw input as compact JSON, for the expanded card. */ readonly rawInput?: string; + /** Claude Code's own tool name (`Agent`, `Read`...), from the update's `_meta`. */ + readonly toolName?: string; + /** The Agent/Task call a sub-agent's tool belongs to, from the update's `_meta`. */ + readonly parentToolUseId?: string; } | { readonly kind: 'permission'; @@ -459,6 +463,10 @@ export type SessionEvent = /** Replaces the card's content when present (ACP semantics). */ readonly content?: readonly ToolContentView[]; readonly rawInput?: string; + /** Claude Code's own tool name (`_meta.claudeCode.toolName`). */ + readonly toolName?: string; + /** The sub-agent's launching Agent/Task call (`_meta.claudeCode.parentToolUseId`). */ + readonly parentToolUseId?: string; } /** Appends output to one terminal shown in a tool card (the IDE runs the terminal itself). */ | { readonly type: 'terminal-output'; readonly terminalId: string; readonly output: string; readonly exitCode?: number | null; readonly truncated?: boolean } @@ -557,6 +565,8 @@ export function applySessionEvent(state: AgentSessionState, event: SessionEvent, locations: event.locations ?? [], content: event.content ?? [], ...(event.rawInput !== undefined ? { rawInput: event.rawInput } : {}), + ...(event.toolName !== undefined ? { toolName: event.toolName } : {}), + ...(event.parentToolUseId !== undefined ? { parentToolUseId: event.parentToolUseId } : {}), }; return touch({ items: [...items, card] }); } @@ -569,6 +579,8 @@ export function applySessionEvent(state: AgentSessionState, event: SessionEvent, ...(event.locations !== undefined ? { locations: event.locations } : {}), ...(event.content !== undefined ? { content: mergeTerminalOutput(old.content, event.content) } : {}), ...(event.rawInput !== undefined ? { rawInput: event.rawInput } : {}), + ...(event.toolName !== undefined ? { toolName: event.toolName } : {}), + ...(event.parentToolUseId !== undefined ? { parentToolUseId: event.parentToolUseId } : {}), }; const copy = items.slice(); copy[index] = updated; diff --git a/apps/ide/test/agentFlowFeed.test.ts b/apps/ide/test/agentFlowFeed.test.ts index cca66d11..ee522e50 100644 --- a/apps/ide/test/agentFlowFeed.test.ts +++ b/apps/ide/test/agentFlowFeed.test.ts @@ -108,6 +108,39 @@ describe('Agent Flow feed of the IDE chats', () => { assert.equal(listAgentTraceSources(root, { home: temp }).some((candidate) => candidate.id === agentFlowSourceId('s2')), false); }); + it('draws the sub-agents of a Claude Code chat, live and when seeded from its transcript (hangar fix)', () => { + const root = project('subagents'); + const parser = path.join(root, 'src/parser.ts'); + const events: SessionEvent[] = [ + { type: 'user', id: 'u1', text: 'Map the parser', at: '2026-11-28T09:00:02.000Z' }, + { type: 'tool', id: 'agent-1', title: 'Map the parser', toolKind: 'think', status: 'in_progress', toolName: 'Agent', rawInput: '{"description":"Map the parser"}' }, + { type: 'tool', id: 'r1', title: 'Read parser.ts', toolKind: 'read', status: 'completed', toolName: 'Read', parentToolUseId: 'agent-1', locations: [{ path: parser }] }, + { type: 'tool', id: 'agent-1', status: 'completed' }, + { type: 'turn-end', id: 'e1', stopReason: 'end_turn', at: '2026-11-28T09:00:09.000Z' }, + ]; + const check = (followed: AgentTraceEvent[]) => { + const steps = followed.filter((event): event is Extract => event.type === 'step'); + const delegate = steps.find((step) => step.id === 'agent-1'); + assert.equal(delegate?.kind, 'delegate'); + assert.deepEqual(delegate?.spawns, { id: 'agent-1', label: 'Map the parser' }); + assert.equal(steps.find((step) => step.id === 'r1')?.actor, 'agent-1'); + }; + const feed = new AgentFlowFeed(() => 1_000); + const live = { state: state('s-live', root), root, workRoot: root }; + for (const event of events) { + feed.onEvent(live, event); + } + check(follow(agentFlowSourceId('s-live'), root)); + const items: TranscriptItem[] = [ + { kind: 'user', id: 'u1', text: 'Map the parser', at: '2026-11-28T09:00:02.000Z' }, + { kind: 'tool', id: 'agent-1', title: 'Map the parser', toolKind: 'think', status: 'completed', locations: [], content: [], rawInput: '{"description":"Map the parser"}', toolName: 'Agent' }, + { kind: 'tool', id: 'r1', title: 'Read parser.ts', toolKind: 'read', status: 'completed', locations: [{ path: parser }], content: [], toolName: 'Read', parentToolUseId: 'agent-1' }, + ]; + const seeded = transcriptAsEvents(items).map((entry) => entry.event); + assert.deepEqual(seeded.filter((event) => event.type === 'tool').map((event) => [(event as { toolName?: string }).toolName, (event as { parentToolUseId?: string }).parentToolUseId]), [['Agent', undefined], ['Read', 'agent-1']]); + feed.forget('s-live'); + }); + it('moves the paths of a chat that works in a worktree onto the project folder', () => { const root = project('rebased'); const worktree = path.join(temp, 'worktrees', 'rebased-1'); diff --git a/apps/ide/test/agentSessions.test.ts b/apps/ide/test/agentSessions.test.ts index b66d7967..b205baba 100644 --- a/apps/ide/test/agentSessions.test.ts +++ b/apps/ide/test/agentSessions.test.ts @@ -118,6 +118,11 @@ describe('ACP mapping', () => { rawInput: '{"command":"npm test"}', }); assert.deepEqual(mapSessionUpdate({ sessionUpdate: 'tool_call_update', toolCallId: 't1', status: 'failed' }, ctx), [{ type: 'tool', id: 't1', status: 'failed' }]); + // Claude Code names its tool and the sub-agent a call ran in (Agent Flow's hangar needs both). + assert.deepEqual( + mapSessionUpdate({ sessionUpdate: 'tool_call_update', toolCallId: 't2', status: 'completed', _meta: { claudeCode: { toolName: 'Read', parentToolUseId: 'agent-1' } } }, ctx), + [{ type: 'tool', id: 't2', status: 'completed', toolName: 'Read', parentToolUseId: 'agent-1' }], + ); assert.deepEqual(mapSessionUpdate({ sessionUpdate: 'plan', entries: [{ content: 'Test', priority: 'high', status: 'pending' }] }, ctx), [{ type: 'plan', entries: [{ content: 'Test', priority: 'high', status: 'pending' }] }]); assert.deepEqual(mapSessionUpdate({ sessionUpdate: 'available_commands_update', availableCommands: [{ name: 'review', description: 'Review', input: { hint: 'what' } }] }, ctx), [{ type: 'commands', commands: [{ name: 'review', description: 'Review', hint: 'what' }] }]); assert.deepEqual(mapSessionUpdate({ sessionUpdate: 'current_mode_update', currentModeId: 'plan' }, ctx), [{ type: 'modes', current: 'plan' }]); diff --git a/apps/vscode-extension/CHANGELOG.md b/apps/vscode-extension/CHANGELOG.md index 94dc52a6..c47f5213 100644 --- a/apps/vscode-extension/CHANGELOG.md +++ b/apps/vscode-extension/CHANGELOG.md @@ -20,7 +20,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