Heuristic:20000419 Fauxnix Output Budget Management
| Knowledge Sources | |
|---|---|
| Domains | Performance, Output_Normalization, MCP |
| Last Updated | 2026-10-26 00:00 GMT |
Overview
MCP tool calls cap stdout at 256 KiB and stderr at 64 KiB with UTF-8 codepoint-safe clipping, redirecting larger artifacts to files instead of truncating mid-surrogate-pair.
Description
The MCP stdio transport has a practical limit (~10 MiB) for JSON responses, and tool output appears in both text and structuredContent fields with JSON escaping overhead. Fauxnix enforces a 256 KiB stdout budget and 64 KiB stderr budget for all MCP bash and bash_batch tool calls. When output exceeds the budget, `clipUtf8` trims the longest whole-codepoint prefix that fits — it never splits a UTF-8 surrogate pair. The clipping uses a backward-trim path when the overshoot is small (the common case, avoiding a second full-buffer allocation) or a forward iterator for large overshoots. The result includes a `truncated: true` flag so the agent knows data was cut. For larger artifacts, the tool description directs agents to redirect to files and verify with `wc -c` or `stat -c %s` rather than inferring byte counts from displayed text.
Usage
Use this heuristic when returning process output through a bounded transport (MCP stdio, HTTP, any JSON-framed channel). Set explicit byte budgets that leave room for transport overhead, clip at codepoint boundaries, and tell the caller when truncation happened. For data larger than the budget, redirect to files — never rely on truncated output for correctness.
The Insight (Rule of Thumb)
- Action: Set `MCP_STDOUT_LIMIT = 262_144` (256 KiB) and `MCP_STDERR_LIMIT = 65_536` (64 KiB); pass these as `stdoutLimit`/`stderrLimit` to the executor.
- Value: 256 KiB / 64 KiB — leaves room for worst-case JSON escaping below the 10 MiB stdio client limit.
- Trade-off: Large outputs are truncated; agents must redirect to files for full data.
- Compatibility: `clipUtf8` never splits a surrogate pair and avoids a second full UTF-8 buffer allocation in the common small-overshoot case.
Reasoning
MCP stdio clients have a ~10 MiB limit, and tool output appears in both `content` (text) and `structuredContent` (JSON) fields — effectively doubling the payload. JSON escaping can expand non-ASCII further. A 256 KiB stdout budget leaves comfortable headroom while capturing the vast majority of real command output. The backward-trim clip path handles the common case (output barely exceeds the budget) by trimming from the end without allocating a second full buffer — it walks backward, subtracting codepoint byte sizes, and never splits a surrogate pair. The `truncated` flag lets agents detect truncation and fall back to file-based verification.
Code Evidence
MCP output budget constants from `src/mcp.ts:22-23`:
const MCP_STDOUT_LIMIT = 262_144;
const MCP_STDERR_LIMIT = 65_536;
Tool description guidance from `src/mcp.ts:56`:
MCP capture is bounded to 256 KiB stdout and 64 KiB stderr across the complete command list;
larger output is marked truncated. Redirect larger artifacts to files.
clipUtf8 backward-trim path from `src/encoding.ts:68-104`:
export function clipUtf8(text: string, limit: number): { text: string; truncated: boolean } {
const total = Buffer.byteLength(text, 'utf8');
if (total <= limit) return { text, truncated: false };
if (total - limit <= limit) {
let used = total;
let end = text.length;
while (used > limit && end > 0) {
const last = text.charCodeAt(end - 1);
const previous = end > 1 ? text.charCodeAt(end - 2) : 0;
if (last >= 0xdc00 && last <= 0xdfff && previous >= 0xd800 && previous <= 0xdbff) {
used -= 4;
end -= 2; // never split a surrogate pair
} else {
used -= last < 0x80 ? 1 : last < 0x800 ? 2 : 3;
end--;
}
}
return { text: text.slice(0, end), truncated: true };
}
// forward iterator for large overshoots...
}