xonecas.io
64875
Mc

mini-coder / commit 76515a3

chore: record deferred subagent buffering fix

xonecas2026-03-11 76515a32f1901f80b306223282c41cc8f0af4b93 parent 04ed927

3 files changed

TODO.md+1−0
@@ -17,3 +17,4 @@ We need to do research first, but maybe we can leverage the hooks feature to ach
1717 ## Deferred fixes
1818
1919 - model-info: in `resolveFromProviderRow`, when canonical capability exists but `contextWindow` is null, fall back to provider row context (`capability.contextWindow ?? row.contextWindow`).
20+- subagent-runner: avoid unconditional full buffering of child `stdout`/`stderr` in `runSubagent`; capture diagnostics only on failure or via a bounded tail buffer to prevent latency/memory regressions.
src/agent/subagent-runner.ts+20−6
@@ -30,6 +30,15 @@ function getMcCommand(): string[] {
3030 // without an in-process depth parameter. Generous enough for real use-cases.
3131 const MAX_SUBAGENT_DEPTH = 10;
3232
33+function formatSubagentDiagnostics(stdout: string, stderr: string): string {
34+ const lines = [...stderr.split("\n"), ...stdout.split("\n")]
35+ .map((line) => line.trim())
36+ .filter(Boolean);
37+ if (lines.length === 0) return "";
38+ const tail = lines.slice(-6).join(" | ");
39+ return ` (diagnostics: ${tail.slice(0, 300)})`;
40+}
41+
3342 export function createSubagentRunner(
3443 cwd: string,
3544 getCurrentModel: () => string,
@@ -75,21 +84,26 @@ export function createSubagentRunner(
7584 ...process.env,
7685 MC_SUBAGENT_DEPTH: String(subagentDepth + 1),
7786 },
78- stdio: ["ignore", "inherit", "inherit", "pipe"],
87+ // Keep subagent stdout/stderr isolated so transient tool errors or warnings
88+ // don't leak into the parent UI. We only surface these streams on failure.
89+ stdio: ["ignore", "pipe", "pipe", "pipe"],
7990 });
8091
8192 activeProcs.add(proc);
8293
8394 try {
84- const [text] = await Promise.all([
95+ const [text, stdout, stderr] = await Promise.all([
8596 Bun.file(proc.stdio[3] as number).text(),
97+ new Response(proc.stdout).text(),
98+ new Response(proc.stderr).text(),
8699 proc.exited,
87100 ]);
101+ const diagnostics = formatSubagentDiagnostics(stdout, stderr);
88102
89103 const trimmed = text.trim();
90104 if (!trimmed) {
91105 throw new Error(
92- `Subagent subprocess produced no output (exit code ${String(proc.exitCode)})`,
106+ `Subagent subprocess produced no output (exit code ${String(proc.exitCode)})${diagnostics}`,
93107 );
94108 }
95109
@@ -98,16 +112,16 @@ export function createSubagentRunner(
98112 parsed = JSON.parse(trimmed) as SubagentSummary & { error?: string };
99113 } catch {
100114 throw new Error(
101- `Subagent subprocess wrote non-JSON output (exit code ${String(proc.exitCode)}): ${trimmed.slice(0, 200)}`,
115+ `Subagent subprocess wrote non-JSON output (exit code ${String(proc.exitCode)}): ${trimmed.slice(0, 200)}${diagnostics}`,
102116 );
103117 }
104118
105119 if (parsed.error) {
106- throw new Error(`Subagent failed: ${parsed.error}`);
120+ throw new Error(`Subagent failed: ${parsed.error}${diagnostics}`);
107121 }
108122 if (proc.exitCode !== 0) {
109123 throw new Error(
110- `Subagent process exited with code ${String(proc.exitCode)}`,
124+ `Subagent process exited with code ${String(proc.exitCode)}${diagnostics}`,
111125 );
112126 }
113127
src/agent/system-prompt.ts+51−51
@@ -4,28 +4,28 @@ import { join } from "node:path";
44 import { tildePath } from "../cli/output.ts";
55
66 function tryReadFile(p: string): string | null {
7- if (!existsSync(p)) return null;
8- try {
9- return readFileSync(p, "utf-8");
10- } catch {
11- return null;
12- }
7+ if (!existsSync(p)) return null;
8+ try {
9+ return readFileSync(p, "utf-8");
10+ } catch {
11+ return null;
12+ }
1313 }
1414
1515 function loadGlobalContextFile(homeDir: string): string | null {
16- const globalDir = join(homeDir, ".agents");
17- return (
18- tryReadFile(join(globalDir, "AGENTS.md")) ??
19- tryReadFile(join(globalDir, "CLAUDE.md"))
20- );
16+ const globalDir = join(homeDir, ".agents");
17+ return (
18+ tryReadFile(join(globalDir, "AGENTS.md")) ??
19+ tryReadFile(join(globalDir, "CLAUDE.md"))
20+ );
2121 }
2222
2323 export function loadLocalContextFile(cwd: string): string | null {
24- return (
25- tryReadFile(join(cwd, ".agents", "AGENTS.md")) ??
26- tryReadFile(join(cwd, "CLAUDE.md")) ??
27- tryReadFile(join(cwd, "AGENTS.md"))
28- );
24+ return (
25+ tryReadFile(join(cwd, ".agents", "AGENTS.md")) ??
26+ tryReadFile(join(cwd, "CLAUDE.md")) ??
27+ tryReadFile(join(cwd, "AGENTS.md"))
28+ );
2929 }
3030
3131 const AUTONOMY = `
@@ -73,18 +73,18 @@ const FINAL_MESSAGE = `
7373 const SUBAGENT_DELEGATION = `You are running as a subagent. Complete the task you have been given directly using your tools. Do not spawn further subagents unless the subtask is unambiguously separable and self-contained.`;
7474
7575 export function buildSystemPrompt(
76- cwd: string,
77- extraSystemPrompt?: string,
78- isSubagent?: boolean,
79- /** Override home directory — used in tests to isolate global context loading. */
80- homeDir?: string,
76+ cwd: string,
77+ extraSystemPrompt?: string,
78+ isSubagent?: boolean,
79+ /** Override home directory — used in tests to isolate global context loading. */
80+ homeDir?: string,
8181 ): string {
82- const globalContext = loadGlobalContextFile(homeDir ?? homedir());
83- const localContext = loadLocalContextFile(cwd);
84- const cwdDisplay = tildePath(cwd);
85- const now = new Date().toLocaleString(undefined, { hour12: false });
82+ const globalContext = loadGlobalContextFile(homeDir ?? homedir());
83+ const localContext = loadLocalContextFile(cwd);
84+ const cwdDisplay = tildePath(cwd);
85+ const now = new Date().toLocaleString(undefined, { hour12: false });
8686
87- let prompt = `You are mini-coder, a small and fast CLI coding agent.
87+ let prompt = `You are mini-coder, a small and fast CLI coding agent.
8888 You have access to tools to read files, search code, make edits, run shell commands, and spawn subagents.
8989
9090 Current working directory: ${cwdDisplay}
@@ -100,29 +100,29 @@ Guidelines:
100100 # Tool output format
101101 \`read\` and \`grep\` prefix every line with \`line:hash|\` (e.g. \`11:a3| code here\`). This prefix is metadata — never include it in file content you write. Use the \`line:hash\` values as anchors for \`replace\` and \`insert\`.`;
102102
103- prompt += AUTONOMY;
104- prompt += SAFETY;
105- prompt += WORKSPACE_GUARDRAILS;
106- prompt += STATUS_UPDATES;
107- prompt += FINAL_MESSAGE;
108-
109- if (globalContext || localContext) {
110- prompt += "\n\n# Project context";
111- if (globalContext) {
112- prompt += `\n\n${globalContext}`;
113- }
114- if (localContext) {
115- prompt += `\n\n${localContext}`;
116- }
117- }
118-
119- if (isSubagent) {
120- prompt += `\n\n${SUBAGENT_DELEGATION}`;
121- }
122-
123- if (extraSystemPrompt) {
124- prompt += `\n\n${extraSystemPrompt}`;
125- }
126-
127- return prompt;
103+ prompt += AUTONOMY;
104+ prompt += SAFETY;
105+ prompt += WORKSPACE_GUARDRAILS;
106+ prompt += STATUS_UPDATES;
107+ prompt += FINAL_MESSAGE;
108+
109+ if (globalContext || localContext) {
110+ prompt += "\n\n# Project context";
111+ if (globalContext) {
112+ prompt += `\n\n${globalContext}`;
113+ }
114+ if (localContext) {
115+ prompt += `\n\n${localContext}`;
116+ }
117+ }
118+
119+ if (isSubagent) {
120+ prompt += `\n\n${SUBAGENT_DELEGATION}`;
121+ }
122+
123+ if (extraSystemPrompt) {
124+ prompt += `\n\n${extraSystemPrompt}`;
125+ }
126+
127+ return prompt;
128128 }