xonecas.io
64875
Mc

mini-coder / commit 1d8ed9c

refactor: simplify subagent worktree rendering and paths

xonecas2026-03-08 1d8ed9c107af123c02dd28f9b7f18f683219e80d parent 45cc9a6

4 files changed

TODO.md+13−7
@@ -1,12 +1,18 @@
11 # TODO
22
3----
4-
5-## Subagents id/number/lane could be made more robust
6-
7-- Currently we use an incremental integer for this, but in a long session with several conversations (user uses /new) it can become confusing (seeing subagent number 16 after /new). Let's think of a better way to id subagents and their lanes/worktrees. Maybe with a UUID?
8-
9----
3+Agents keep having to resort to rg, something is wrong with our grep implemenation:
4+
5+```
6+ ? grep formatSubagentLabel *.ts i
7+ · no matches
8+ $ rg formatSubagentLabel src/
9+ ✔ 0
10+ │ src/agent/subagent-runner.ts:import { formatSubagentLabel } from "../cli/output.ts";
11+ │ src/agent/subagent-runner.ts: const laneLabel = formatSubagentLabel(laneId, parentLabel);
12+ │ src/cli/tool-render.ts:export function formatSubagentLabel(
13+ │ src/cli/tool-render.ts: const labelStr = formatSubagentLabel(laneId, parentLabel, worktreeBranch);
14+ │ src/cli/output.ts: formatSubagentLabel,
15+```
1016
1117 ## Write blog posts
1218
src/agent/reporter.ts+3−3
@@ -23,10 +23,10 @@ export interface StatusBarData {
2323 }
2424
2525 export interface SubagentEventData {
26- laneId: number;
26+ laneId: string;
2727 parentLabel?: string;
28- worktreeBranch?: string;
29- activeLanes: Set<number>;
28+ hasWorktree?: boolean;
29+ activeLanes: Set<string>;
3030 }
3131
3232 export interface AgentReporter {
src/agent/subagent-runner.ts+7−9
@@ -21,13 +21,12 @@ import { buildToolSet } from "./tools.ts";
2121
2222 import type { ThinkingEffort } from "../llm-api/providers.ts";
2323
24-function makeWorktreeBranch(laneId: number): string {
25- return `mc-sub-${laneId}-${Date.now()}`;
24+function makeWorktreeBranch(laneId: string): string {
25+ return `mc-sub-${laneId}`;
2626 }
2727
28-function makeWorktreePath(laneId: number): string {
29- const suffix = crypto.randomUUID().replace(/-/g, "").slice(0, 10);
30- return join(tmpdir(), `mc-wt-${laneId}-${suffix}`);
28+function makeWorktreePath(laneId: string): string {
29+ return join(tmpdir(), `mc-wt-${laneId}`);
3130 }
3231
3332 export function createSubagentRunner(
@@ -36,8 +35,7 @@ export function createSubagentRunner(
3635 getCurrentModel: () => string,
3736 getThinkingEffort: () => ThinkingEffort | null,
3837 ) {
39- let nextLaneId = 1;
40- const activeLanes = new Set<number>();
38+ const activeLanes = new Set<string>();
4139 const worktreesEnabledPromise = isGitRepo(cwd);
4240
4341 let mergeLock: Promise<void> = Promise.resolve();
@@ -57,7 +55,7 @@ export function createSubagentRunner(
5755 modelOverride?: string,
5856 parentLabel?: string,
5957 ): Promise<SubagentOutput> => {
60- const laneId = nextLaneId++;
58+ const laneId = crypto.randomUUID().split("-")[0] as string;
6159 activeLanes.add(laneId);
6260
6361 let subagentCwd = cwd;
@@ -124,7 +122,7 @@ export function createSubagentRunner(
124122 reporter.renderSubagentEvent(event, {
125123 laneId,
126124 ...(parentLabel ? { parentLabel } : {}),
127- ...(worktreeBranch ? { worktreeBranch } : {}),
125+ hasWorktree: !!worktreeBranch,
128126 activeLanes,
129127 });
130128 reporter.startSpinner("thinking");
src/cli/tool-render.ts+9−19
@@ -510,40 +510,30 @@ function normalizeParentLaneLabel(parentLabel: string): string {
510510 return lanePath || inner;
511511 }
512512
513-function shortWorktreeBranch(branch: string): string {
514- const match = branch.match(/^(mc-sub-\d+)-\d+$/);
515- return match?.[1] ?? branch;
516-}
517-
518513 export function formatSubagentLabel(
519- laneId: number,
514+ laneId: string,
520515 parentLabel?: string,
521- worktreeBranch?: string,
522516 ): string {
523517 const parent = parentLabel ? normalizeParentLaneLabel(parentLabel) : "";
524518 const numStr = parent ? `${parent}.${laneId}` : `${laneId}`;
525- const branchHint = worktreeBranch
526- ? `·${shortWorktreeBranch(worktreeBranch)}`
527- : "";
528- return c.dim(c.cyan(`[${numStr}${branchHint}]`));
519+ return c.dim(c.cyan(`[${numStr}]`));
529520 }
530521
531-const laneBuffers = new Map<number, string>();
522+const laneBuffers = new Map<string, string>();
532523
533524 export function renderSubagentEvent(
534525 event: TurnEvent,
535526 opts: {
536- laneId: number;
527+ laneId: string;
537528 parentLabel?: string | undefined;
538- worktreeBranch?: string;
539- activeLanes: Set<number>;
529+ hasWorktree?: boolean;
530+ activeLanes: Set<string>;
540531 },
541532 ): void {
542- const { laneId, parentLabel, worktreeBranch, activeLanes } = opts;
543-
544- const labelStr = formatSubagentLabel(laneId, parentLabel, worktreeBranch);
545- const prefix = activeLanes.size > 1 || worktreeBranch ? `${labelStr} ` : "";
533+ const { laneId, parentLabel, hasWorktree, activeLanes } = opts;
546534
535+ const labelStr = formatSubagentLabel(laneId, parentLabel);
536+ const prefix = activeLanes.size > 1 || hasWorktree ? `${labelStr} ` : "";
547537 if (event.type === "text-delta") {
548538 const buf = (laneBuffers.get(laneId) ?? "") + event.delta;
549539 const lines = buf.split("\n");