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
2 changes: 1 addition & 1 deletion apps/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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