Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/ide/src/main/agentSessions/acpMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>).claudeCode : undefined;
if (!claudeCode || typeof claudeCode !== 'object') {
return {};
}
const { toolName, parentToolUseId } = claudeCode as Record<string, unknown>;
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<SessionEvent, { type: 'tool' }> {
const status = toolStatusOf(update.status);
Expand All @@ -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),
};
}

Expand Down
2 changes: 2 additions & 0 deletions apps/ide/src/main/agentSessions/agentFlowFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
12 changes: 12 additions & 0 deletions apps/ide/src/shared/agentSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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] });
}
Expand All @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions apps/ide/test/agentFlowFeed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AgentTraceEvent, { type: 'step' }> => 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');
Expand Down
5 changes: 5 additions & 0 deletions apps/ide/test/agentSessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' }]);
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`. */
Expand Down Expand Up @@ -96,6 +100,25 @@ function change(changes: readonly { metric: string; before: number | null; after
}

type StepEvent = Extract<AgentTraceEvent, { type: 'step' }>;

/** Claude Code's tools that launch a sub-agent. */
const DELEGATE_TOOLS: ReadonlySet<string> = 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<string, unknown>;
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<T extends string> = Extract<AcpSessionEventLike, { type: T }>;

function metricDeltaEvent(event: AcpEventOf<'metric-delta'>, at: number): AgentTraceEvent {
Expand Down Expand Up @@ -192,6 +215,10 @@ export class AcpSessionEventMapper {
private readonly terminals = new Map<string, string>();
/** Where each tool works, so a permission asked for it waits at the same building. */
private readonly locations = new Map<string, AgentTraceLocation[]>();
/** Claude Code's tool name per tool, kept from the call for updates that omit it. */
private readonly toolNames = new Map<string, string>();
/** The sub-agent (its Agent/Task call id) each tool ran in. */
private readonly owners = new Map<string, string>();
private turnOpen = false;

public map(event: AcpSessionEventLike, at: number): AgentTraceEvent[] {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
30 changes: 30 additions & 0 deletions apps/vscode-extension/test/analysis/agentFlowCore.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions docs/agent-flow/hangar.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-<id>.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
Expand Down
Loading