xonecas.io
64875
Mc

mini-coder / commit 1cf85eb

feat: subagent activity tree view in UI

xonecas2026-02-21 1cf85eb36cdfef039da3ee170e2194caa68ff5d8 parent d87809b

6 files changed

TODO.md+0−13
@@ -1,16 +1,3 @@
1-# ~~No way to interrupt the turn~~ ✓
2-
3-- Ctrl-c during a turn now aborts the LLM request and returns to the prompt.
4-- The global SIGINT handler only exits when no other listener is registered.
5-
61 # Undo needs to restore working directory
72
83 Besides undoing the conversation turn, it should restore some snapshot, using git?
9-
10----
11-
12-# Tools output
13-
14-Subagent needs visibity on the ui somehow... that accounts for multiple in parallel... Needs planning, maybe a tree view showing the subagent's tool calls before the output.
15-
16----
src/agent/agent.ts+24−2
@@ -172,7 +172,7 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
172172 const runSubagent = async (
173173 prompt: string,
174174 depth = 0,
175- ): Promise<{ result: string; inputTokens: number; outputTokens: number }> => {
175+ ): Promise<import("../tools/subagent.ts").SubagentOutput> => {
176176 const subMessages: CoreMessage[] = [{ role: "user", content: prompt }];
177177 const subTools = buildToolSet({
178178 cwd,
@@ -188,6 +188,10 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
188188 let result = "";
189189 let inputTokens = 0;
190190 let outputTokens = 0;
191+ const activity: import("../tools/subagent.ts").SubagentToolEntry[] = [];
192+
193+ // Track pending tool call so we can pair it with its result
194+ const pendingCalls = new Map<string, { toolName: string; args: unknown }>();
191195
192196 const events = runTurn({
193197 model: subLlm,
@@ -198,13 +202,31 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
198202
199203 for await (const event of events) {
200204 if (event.type === "text-delta") result += event.delta;
205+ if (event.type === "tool-call-start") {
206+ pendingCalls.set(event.toolCallId, {
207+ toolName: event.toolName,
208+ args: event.args,
209+ });
210+ }
211+ if (event.type === "tool-result") {
212+ const pending = pendingCalls.get(event.toolCallId);
213+ if (pending) {
214+ pendingCalls.delete(event.toolCallId);
215+ activity.push({
216+ toolName: pending.toolName,
217+ args: pending.args,
218+ result: event.result,
219+ isError: event.isError,
220+ });
221+ }
222+ }
201223 if (event.type === "turn-complete") {
202224 inputTokens = event.inputTokens;
203225 outputTokens = event.outputTokens;
204226 }
205227 }
206228
207- return { result, inputTokens, outputTokens };
229+ return { result, inputTokens, outputTokens, activity };
208230 };
209231
210232 // ── MCP: load persisted servers and connect them ───────────────────────────
src/agent/tools.ts+2−5
@@ -100,11 +100,8 @@ export function buildToolSet(opts: {
100100 runSubagent: (
101101 prompt: string,
102102 depth?: number,
103- ) => Promise<{
104- result: string;
105- inputTokens: number;
106- outputTokens: number;
107- }>;
103+ ) => Promise<import("../tools/subagent.ts").SubagentOutput>;
104+
108105 onHook: (toolName: string, scriptPath: string, success: boolean) => void;
109106 }): ToolDef[] {
110107 const { cwd, onHook } = opts;
src/cli/commands.ts+1−1
@@ -24,7 +24,7 @@ export interface CommandContext {
2424 connectMcpServer: (name: string) => Promise<void>;
2525 runSubagent: (
2626 prompt: string,
27- ) => Promise<{ result: string; inputTokens: number; outputTokens: number }>;
27+ ) => Promise<import("../tools/subagent.ts").SubagentOutput>;
2828
2929 cwd: string;
3030 }
src/cli/output.ts+144−7
@@ -229,6 +229,142 @@ export function renderHook(
229229
230230 // ─── Tool result — compact data ───────────────────────────────────────────────
231231
232+/**
233+ * Render a compact one-liner status for a tool result inside a subagent tree.
234+ * `indent` is the prefix string (spaces) for the current tree depth.
235+ */
236+function renderToolResultInline(
237+ toolName: string,
238+ result: unknown,
239+ isError: boolean,
240+ indent: string,
241+): void {
242+ if (isError) {
243+ const msg =
244+ typeof result === "string"
245+ ? result
246+ : result instanceof Error
247+ ? result.message
248+ : JSON.stringify(result);
249+ const oneLiner = msg.split("\n")[0] ?? msg;
250+ writeln(`${indent}${G.err} ${c.red(oneLiner)}`);
251+ return;
252+ }
253+
254+ if (toolName === "glob") {
255+ const r = result as { files: string[]; truncated: boolean };
256+ const n = r.files.length;
257+ writeln(
258+ `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} file${n === 1 ? "" : "s"}${r.truncated ? " (capped)" : ""}`)}`,
259+ );
260+ return;
261+ }
262+
263+ if (toolName === "grep") {
264+ const r = result as { matches: unknown[]; truncated: boolean };
265+ const n = r.matches.length;
266+ writeln(
267+ `${indent}${G.info} ${c.dim(n === 0 ? "no matches" : `${n} match${n === 1 ? "" : "es"}${r.truncated ? " (capped)" : ""}`)}`,
268+ );
269+ return;
270+ }
271+
272+ if (toolName === "read") {
273+ const r = result as { totalLines: number; truncated: boolean };
274+ writeln(
275+ `${indent}${G.info} ${c.dim(`${r.totalLines} lines${r.truncated ? " (truncated)" : ""}`)}`,
276+ );
277+ return;
278+ }
279+
280+ if (toolName === "create") {
281+ const r = result as { path: string; created: boolean };
282+ const verb = r.created ? c.green("created") : c.dim("overwritten");
283+ writeln(`${indent}${G.ok} ${verb} ${r.path}`);
284+ return;
285+ }
286+
287+ if (toolName === "replace" || toolName === "insert") {
288+ const r = result as { path: string; deleted?: boolean };
289+ const verb =
290+ toolName === "insert" ? "inserted" : r.deleted ? "deleted" : "replaced";
291+ writeln(`${indent}${G.ok} ${c.dim(verb)} ${r.path}`);
292+ return;
293+ }
294+
295+ if (toolName === "shell") {
296+ const r = result as {
297+ exitCode: number;
298+ success: boolean;
299+ timedOut: boolean;
300+ };
301+ const badge = r.timedOut
302+ ? c.yellow("timeout")
303+ : r.success
304+ ? c.green(`✔ ${r.exitCode}`)
305+ : c.red(`✖ ${r.exitCode}`);
306+ writeln(`${indent}${badge}`);
307+ return;
308+ }
309+
310+ if (toolName === "subagent") {
311+ const r = result as { inputTokens?: number; outputTokens?: number };
312+ if (r.inputTokens || r.outputTokens) {
313+ writeln(
314+ `${indent}${G.ok} ${c.dim(`↑${r.inputTokens ?? 0} ↓${r.outputTokens ?? 0}`)}`,
315+ );
316+ }
317+ return;
318+ }
319+
320+ if (toolName.startsWith("mcp_")) {
321+ const content = Array.isArray(result) ? result : [result];
322+ const first = (content as Array<{ type?: string; text?: string }>)[0];
323+ if (first?.type === "text" && first.text) {
324+ const oneLiner = first.text.split("\n")[0] ?? "";
325+ if (oneLiner)
326+ writeln(
327+ `${indent}${G.info} ${c.dim(oneLiner.length > 80 ? `${oneLiner.slice(0, 77)}…` : oneLiner)}`,
328+ );
329+ }
330+ return;
331+ }
332+
333+ // Generic: one-line JSON
334+ const text = JSON.stringify(result);
335+ writeln(
336+ `${indent}${G.info} ${c.dim(text.length > 80 ? `${text.slice(0, 77)}…` : text)}`,
337+ );
338+}
339+
340+/** Render a subagent's activity tree, indented under its parent tool-call line. */
341+function renderSubagentActivity(
342+ activity: import("../tools/subagent.ts").SubagentToolEntry[],
343+ indent: string,
344+ maxDepth: number,
345+): void {
346+ for (const entry of activity) {
347+ writeln(`${indent}${toolCallLine(entry.toolName, entry.args)}`);
348+
349+ // For nested subagents, recurse one more level (capped at maxDepth)
350+ if (entry.toolName === "subagent" && maxDepth > 0) {
351+ const nested = entry.result as {
352+ activity?: import("../tools/subagent.ts").SubagentToolEntry[];
353+ };
354+ if (nested?.activity?.length) {
355+ renderSubagentActivity(nested.activity, `${indent} `, maxDepth - 1);
356+ }
357+ }
358+
359+ renderToolResultInline(
360+ entry.toolName,
361+ entry.result,
362+ entry.isError,
363+ `${indent} `,
364+ );
365+ }
366+}
367+
232368 export function renderToolResult(
233369 toolName: string,
234370 result: unknown,
@@ -383,12 +519,13 @@ export function renderToolResult(
383519 }
384520
385521 if (toolName === "subagent") {
386- // subagent result: { result: string, inputTokens, outputTokens }
387- const r = result as {
388- result?: string;
389- inputTokens?: number;
390- outputTokens?: number;
391- };
522+ const r = result as import("../tools/subagent.ts").SubagentOutput;
523+
524+ // Render the tool call tree before showing the result text
525+ if (r.activity?.length) {
526+ renderSubagentActivity(r.activity, " ", 1);
527+ }
528+
392529 if (r.result) {
393530 const lines = r.result.split("\n");
394531 const preview = lines.slice(0, 8);
@@ -397,7 +534,6 @@ export function renderToolResult(
397534 writeln(` ${c.dim(`│ … +${lines.length - 8} lines`)}`);
398535 }
399536 if (r.inputTokens || r.outputTokens) {
400- // TODO: missing total unique context size in tokens
401537 writeln(` ${c.dim(`↑${r.inputTokens ?? 0} ↓${r.outputTokens ?? 0}`)}`);
402538 }
403539 return;
@@ -485,6 +621,7 @@ export async function renderTurn(
485621 if (printPos < printQueue.length) {
486622 process.stdout.write(printQueue[printPos] as string);
487623 printPos++;
624+ // TODO: Tick is too fast, print is almost instant
488625 scheduleTick();
489626 } else {
490627 // Queue fully drained — reclaim memory.
src/tools/subagent.ts+9−3
@@ -7,10 +7,18 @@ const SubagentInput = z.object({
77
88 type SubagentInput = z.infer<typeof SubagentInput>;
99
10+export interface SubagentToolEntry {
11+ toolName: string;
12+ args: unknown;
13+ result: unknown;
14+ isError: boolean;
15+}
16+
1017 export interface SubagentOutput {
1118 result: string;
1219 inputTokens: number;
1320 outputTokens: number;
21+ activity: SubagentToolEntry[];
1422 }
1523
1624 /**
@@ -20,9 +28,7 @@ export interface SubagentOutput {
2028 * the agent without creating a circular import.
2129 */
2230 export function createSubagentTool(
23- runSubagent: (
24- prompt: string,
25- ) => Promise<{ result: string; inputTokens: number; outputTokens: number }>,
31+ runSubagent: (prompt: string) => Promise<SubagentOutput>,
2632 ): ToolDef<SubagentInput, SubagentOutput> {
2733 return {
2834 name: "subagent",