xonecas.io
64875
Mc

mini-coder / commit 2064f1e

fix: guard shell output and update status usage display

xonecas2026-04-06 2064f1ea3faa585c8fd8176ef4b54545aae3b660 parent 2e78982

6 files changed

TODO.md+0−2
@@ -9,8 +9,6 @@ The spec.md and code are the sources of truth, not this file, don't assume anyth
99
1010 ### MUST FIX
1111
12-- [ ] **Status bar context usage percentage is wrong** (`ui.ts`) — the token usage indicator reports a much higher fraction of the model context window than is actually in use (for example, showing roughly half full when usage is still low). That makes the context meter untrustworthy and could mislead users about when compaction is needed. Fix: audit the percentage calculation and ensure it uses the correct token totals against the active model's real context window.
13-
1412 ### High
1513
1614 - [ ] **Uncaught tool handler exceptions** (`agent.ts`) — if a plugin handler (or a built-in on a filesystem error like EPERM) throws, the agent loop crashes. The exception propagates through `submitMessage` (try/finally, no catch) to a `.catch(console.error)` in `handleInput` — invisible in the TUI. The conversation is left inconsistent: `tool_start` emitted but `tool_end` never fires, tool result message never appended. Fix: wrap the `handler()` call in try/catch, produce an error `ToolExe …
spec.md+5−5
@@ -65,7 +65,7 @@ Runs a command in the user's shell. Returns stdout, stderr, and exit code.
6565
6666 Implementation details:
6767
68-- Large outputs are truncated to avoid context explosion caused by reading a massive file or binary by accident. This is a guard at the tool level, and not related to the user-configured verbose setting.
68+- Large outputs are truncated to avoid context explosion caused by reading a massive file or binary by accident. This guard applies to both very tall output (many lines) and very wide output (a few extremely long lines), and is not related to the user-configured verbose setting.
6969 - The truncation threshold is configurable, tuned to keep useful output while staying well within context limits.
7070 - Commands run via `$SHELL -c "<command>"` (falling back to `/bin/sh` if `$SHELL` is unset) with the CWD set to the session's working directory.
7171 - Timeout: no default timeout. The user can interrupt via `Escape`.
@@ -471,8 +471,8 @@ The default theme uses terminal palette colors where semantic accents help (`col
471471 Two lines, four corners:
472472
473473 ```
474-~/src/mini-coder main +3 ~1 ▲ 2
475-anthropic/sonnet-4 · med in:1.2k out:0.8k · 42% · $0.03
474+~/src/mini-coder main +3 ~1 ▲ 2
475+anthropic/sonnet-4 · med in:1.2k out:0.8k · 42.0%/200k · $0.03
476476 ```
477477
478478 **Line 1 — location:**
@@ -483,9 +483,9 @@ anthropic/sonnet-4 · med in:1.2k out:0.8k · 42% · $0.03
483483 **Line 2 — session:**
484484
485485 - Left: `provider/model · effort`. Effort shown as `low`, `med`, `high`, `xhigh`.
486-- Right: `in:input out:output · context% · $cost`. All values are **cumulative for the session**.
486+- Right: `in:input out:output · context%/window · $cost`. `in`, `out`, and `$cost` are **cumulative for the session**. `context%/window` shows the most recently loaded context as a percentage of the active model's context window, e.g. `33.1%/272k`.
487487
488-Token counts use human-friendly units (1.2k, 45k, 1.2M). Context % is computed from cumulative tokens vs `Model.contextWindow`. Cost is a running sum of `AssistantMessage.usage.cost.total` across all messages in the session, accumulated by us (pi-ai tracks per-message only). Reset on `/new`, restored from DB on session resume.
488+Token counts use human-friendly units (1.2k, 45k, 1.2M). Context usage is computed from the most recent assistant message's `usage.totalTokens` vs `Model.contextWindow` — this is the best available approximation of the actual prompt/response footprint without re-tokenizing the conversation locally. The denominator shows the active model's context window in the same compact units. Cost is a running sum of `AssistantMessage.usage.cost.total` across all messages in the session, accumulated by us …
489489
490490 ### Conversation log
491491
src/tools.test.ts+62−9
@@ -229,6 +229,28 @@ describe("shell", () => {
229229 expect(text).toContain("2000");
230230 });
231231
232+ test("truncates huge output even when line count is small", async () => {
233+ const longLine = "x".repeat(20_000);
234+ writeFileSync(join(tmp, "huge-lines.txt"), `${longLine}\n${longLine}\n`);
235+
236+ const result = await executeShell(
237+ {
238+ command:
239+ "python - <<'PY'\nfrom pathlib import Path\nprint(Path('huge-lines.txt').read_text(), end='')\nPY",
240+ },
241+ tmp,
242+ {
243+ maxLines: 100,
244+ maxBytes: 4_000,
245+ },
246+ );
247+
248+ expect(result.isError).toBe(false);
249+ const text = resultText(result);
250+ expect(text).toContain("… truncated");
251+ expect(text.length).toBeLessThan(40_000);
252+ });
253+
232254 test("does not truncate output within the limit", async () => {
233255 const result = await executeShell({ command: "seq 1 10" }, tmp, {
234256 maxLines: 100,
@@ -284,25 +306,54 @@ describe("shell", () => {
284306 // ---------------------------------------------------------------------------
285307
286308 describe("truncateOutput", () => {
287- test("returns input unchanged when within limit", () => {
309+ test("returns input unchanged when within limits", () => {
288310 const input = "line1\nline2\nline3";
289- expect(truncateOutput(input, 10)).toBe(input);
311+ expect(truncateOutput(input, 10, 1_000)).toBe(input);
290312 });
291313
292- test("truncates keeping head and tail with marker", () => {
314+ test("truncates keeping head and tail with marker when line count exceeds the limit", () => {
293315 const lines = Array.from({ length: 100 }, (_, i) => `line${i + 1}`);
294316 const input = lines.join("\n");
295- const result = truncateOutput(input, 20);
317+ const result = truncateOutput(input, 20, 10_000);
296318
297319 expect(result).toContain("line1");
298320 expect(result).toContain("line100");
299321 expect(result).toContain("… truncated");
300322 });
301323
324+ test("truncates keeping head and tail with marker when byte size exceeds the limit", () => {
325+ const head = "A".repeat(6_000);
326+ const tail = "B".repeat(6_000);
327+ const input = `${head}\n${tail}`;
328+ const result = truncateOutput(input, 100, 4_000);
329+
330+ expect(result).toContain("AAAA");
331+ expect(result).toContain("BBBB");
332+ expect(result).toContain("… truncated");
333+ expect(Buffer.byteLength(result, "utf8")).toBeLessThanOrEqual(4_000);
334+ });
335+
336+ test("applies line and byte limits in a single truncation pass", () => {
337+ const lines = Array.from(
338+ { length: 100 },
339+ (_, i) => `${i + 1}:${"x".repeat(300)}`,
340+ );
341+ const input = lines.join("\n");
342+ const result = truncateOutput(input, 20, 2_000);
343+
344+ expect(result).toContain("1:");
345+ expect(result).toContain("100:");
346+ expect(result).toContain("… truncated 80 lines …");
347+ expect(result).not.toContain("… truncated for size …");
348+ expect(result.match(/… truncated/g) ?? []).toHaveLength(1);
349+ expect(result.split("\n").length).toBeLessThanOrEqual(21);
350+ expect(Buffer.byteLength(result, "utf8")).toBeLessThanOrEqual(2_000);
351+ });
352+
302353 test("head and tail do not overlap", () => {
303354 const lines = Array.from({ length: 50 }, (_, i) => `L${i + 1}`);
304355 const input = lines.join("\n");
305- const result = truncateOutput(input, 20);
356+ const result = truncateOutput(input, 20, 10_000);
306357
307358 // Count total lines (head + marker + tail)
308359 const resultLines = result.split("\n");
@@ -311,17 +362,19 @@ describe("truncateOutput", () => {
311362 });
312363
313364 test("handles empty input", () => {
314- expect(truncateOutput("", 10)).toBe("");
365+ expect(truncateOutput("", 10, 1_000)).toBe("");
315366 });
316367
317368 test("handles single-line input", () => {
318- expect(truncateOutput("hello", 10)).toBe("hello");
369+ expect(truncateOutput("hello", 10, 1_000)).toBe("hello");
319370 });
320371
321- test("handles input at exactly the limit", () => {
372+ test("handles input at exactly the line and byte limit", () => {
322373 const lines = Array.from({ length: 10 }, (_, i) => `line${i + 1}`);
323374 const input = lines.join("\n");
324- expect(truncateOutput(input, 10)).toBe(input);
375+ expect(truncateOutput(input, 10, Buffer.byteLength(input, "utf8"))).toBe(
376+ input,
377+ );
325378 });
326379 });
327380
src/tools.ts+152−15
@@ -124,6 +124,8 @@ export interface ShellArgs {
124124 export interface ShellOpts {
125125 /** Maximum output lines before truncation. Default: 1000. */
126126 maxLines?: number;
127+ /** Maximum UTF-8 bytes before truncation. Default: 50_000. */
128+ maxBytes?: number;
127129 /** Abort signal to cancel the command. */
128130 signal?: AbortSignal;
129131 /** Callback for progressive output updates while the command is running. */
@@ -131,6 +133,7 @@ export interface ShellOpts {
131133 }
132134
133135 const DEFAULT_MAX_LINES = 1000;
136+const DEFAULT_MAX_BYTES = 50_000;
134137
135138 /** Format combined stdout/stderr for display in tool results. */
136139 function formatShellOutput(stdout: string, stderr: string): string {
@@ -187,6 +190,7 @@ export async function executeShell(
187190 ): Promise<ToolExecResult> {
188191 const shell = process.env.SHELL || "/bin/sh";
189192 const maxLines = opts?.maxLines ?? DEFAULT_MAX_LINES;
193+ const maxBytes = opts?.maxBytes ?? DEFAULT_MAX_BYTES;
190194
191195 try {
192196 const spawnOpts: Parameters<typeof Bun.spawn>[1] = {
@@ -200,7 +204,11 @@ export async function executeShell(
200204 let stdoutBuf = "";
201205 let stderrBuf = "";
202206 const reportUpdate = (): void => {
203- const output = formatShellOutput(stdoutBuf, stderrBuf);
207+ const output = truncateOutput(
208+ formatShellOutput(stdoutBuf, stderrBuf),
209+ maxLines,
210+ maxBytes,
211+ );
204212 if (!output) {
205213 return;
206214 }
@@ -222,6 +230,7 @@ export async function executeShell(
222230 const output = truncateOutput(
223231 formatShellOutput(stdout.trimEnd(), stderr.trimEnd()),
224232 maxLines,
233+ maxBytes,
225234 );
226235
227236 const isError = exitCode !== 0;
@@ -239,31 +248,159 @@ export async function executeShell(
239248 // Output truncation
240249 // ---------------------------------------------------------------------------
241250
251+/** Build line-limited head/tail segments and their truncation marker. */
252+function buildLineTruncation(
253+ output: string,
254+ maxLines: number,
255+): {
256+ head: string;
257+ tail: string;
258+ marker: string;
259+} | null {
260+ const lines = output.split("\n");
261+ if (lines.length <= maxLines) {
262+ return null;
263+ }
264+
265+ const headCount = Math.ceil(maxLines / 2);
266+ const tailCount = Math.floor(maxLines / 2);
267+ const omitted = lines.length - headCount - tailCount;
268+
269+ return {
270+ head: lines.slice(0, headCount).join("\n"),
271+ tail: lines.slice(lines.length - tailCount).join("\n"),
272+ marker: `\n… truncated ${omitted} lines …\n`,
273+ };
274+}
275+
276+/** Slice the largest UTF-8 prefix that fits within `maxBytes`. */
277+function sliceUtf8Prefix(input: string, maxBytes: number): string {
278+ if (maxBytes <= 0 || input === "") {
279+ return "";
280+ }
281+ let low = 0;
282+ let high = input.length;
283+ while (low < high) {
284+ const mid = Math.ceil((low + high) / 2);
285+ const candidate = input.slice(0, mid);
286+ if (Buffer.byteLength(candidate, "utf8") <= maxBytes) {
287+ low = mid;
288+ } else {
289+ high = mid - 1;
290+ }
291+ }
292+ return input.slice(0, low);
293+}
294+
295+/** Slice the largest UTF-8 suffix that fits within `maxBytes`. */
296+function sliceUtf8Suffix(input: string, maxBytes: number): string {
297+ if (maxBytes <= 0 || input === "") {
298+ return "";
299+ }
300+ let low = 0;
301+ let high = input.length;
302+ while (low < high) {
303+ const mid = Math.ceil((low + high) / 2);
304+ const candidate = input.slice(input.length - mid);
305+ if (Buffer.byteLength(candidate, "utf8") <= maxBytes) {
306+ low = mid;
307+ } else {
308+ high = mid - 1;
309+ }
310+ }
311+ return input.slice(input.length - low);
312+}
313+
314+/** Fit disjoint head/tail segments plus a marker within a UTF-8 byte budget. */
315+function fitSegmentsWithinBytes(
316+ headSource: string,
317+ tailSource: string,
318+ marker: string,
319+ maxBytes: number,
320+): string {
321+ const markerBytes = Buffer.byteLength(marker, "utf8");
322+ if (markerBytes >= maxBytes) {
323+ return sliceUtf8Prefix(headSource, maxBytes);
324+ }
325+
326+ const availableBytes = maxBytes - markerBytes;
327+ const headBudget = Math.ceil(availableBytes / 2);
328+ const tailBudget = Math.floor(availableBytes / 2);
329+
330+ let head = sliceUtf8Prefix(headSource, headBudget);
331+ let tail = sliceUtf8Suffix(tailSource, tailBudget);
332+
333+ const usedBytes =
334+ Buffer.byteLength(head, "utf8") + Buffer.byteLength(tail, "utf8");
335+ let remainingBytes = availableBytes - usedBytes;
336+
337+ if (remainingBytes > 0) {
338+ const expandedHead = sliceUtf8Prefix(
339+ headSource,
340+ headBudget + remainingBytes,
341+ );
342+ remainingBytes -=
343+ Buffer.byteLength(expandedHead, "utf8") - Buffer.byteLength(head, "utf8");
344+ head = expandedHead;
345+ }
346+
347+ if (remainingBytes > 0) {
348+ tail = sliceUtf8Suffix(tailSource, tailBudget + remainingBytes);
349+ }
350+
351+ return head + marker + tail;
352+}
353+
354+/** Truncate output by UTF-8 byte size, preserving head and tail text. */
355+function truncateOutputByBytes(output: string, maxBytes: number): string {
356+ if (Buffer.byteLength(output, "utf8") <= maxBytes) {
357+ return output;
358+ }
359+
360+ return fitSegmentsWithinBytes(
361+ output,
362+ output,
363+ "\n… truncated for size …\n",
364+ maxBytes,
365+ );
366+}
367+
242368 /**
243- * Truncate output to keep head and tail lines with a marker in between.
369+ * Truncate output to keep useful head and tail content within line and byte budgets.
244370 *
245- * If the output has fewer lines than `maxLines`, it is returned unchanged.
246- * Otherwise, keeps roughly half the budget for head lines and half for tail
247- * lines, joined by a truncation marker showing how many lines were omitted.
371+ * The line budget avoids flooding the model with very tall outputs, while the
372+ * byte budget prevents context explosions caused by a small number of very long
373+ * lines.
248374 *
249375 * @param output - The full output string.
250376 * @param maxLines - Maximum number of content lines to keep.
377+ * @param maxBytes - Maximum UTF-8 bytes to keep.
251378 * @returns The (possibly truncated) output string.
252379 */
253-export function truncateOutput(output: string, maxLines: number): string {
380+export function truncateOutput(
381+ output: string,
382+ maxLines: number,
383+ maxBytes: number,
384+): string {
254385 if (!output) return output;
255386
256- const lines = output.split("\n");
257- if (lines.length <= maxLines) return output;
258-
259- const headCount = Math.ceil(maxLines / 2);
260- const tailCount = Math.floor(maxLines / 2);
261- const omitted = lines.length - headCount - tailCount;
387+ const lineTruncation = buildLineTruncation(output, maxLines);
388+ if (!lineTruncation) {
389+ return truncateOutputByBytes(output, maxBytes);
390+ }
262391
263- const head = lines.slice(0, headCount);
264- const tail = lines.slice(lines.length - tailCount);
392+ const lineLimited =
393+ lineTruncation.head + lineTruncation.marker + lineTruncation.tail;
394+ if (Buffer.byteLength(lineLimited, "utf8") <= maxBytes) {
395+ return lineLimited;
396+ }
265397
266- return [...head, `… truncated ${omitted} lines …`, ...tail].join("\n");
398+ return fitSegmentsWithinBytes(
399+ lineTruncation.head,
400+ lineTruncation.tail,
401+ lineTruncation.marker,
402+ maxBytes,
403+ );
267404 }
268405
269406 // ---------------------------------------------------------------------------
src/ui.test.ts+62−0
@@ -111,6 +111,29 @@ function createTestState(): AppState {
111111 };
112112 }
113113
114+function makeAssistantWithUsage(
115+ text: string,
116+ usage: Partial<ReturnType<typeof fauxAssistantMessage>["usage"]>,
117+) {
118+ const message = fauxAssistantMessage(text);
119+ message.usage = {
120+ input: 100,
121+ output: 50,
122+ cacheRead: 0,
123+ cacheWrite: 0,
124+ totalTokens: 150,
125+ cost: {
126+ input: 0.001,
127+ output: 0.002,
128+ cacheRead: 0,
129+ cacheWrite: 0,
130+ total: 0.003,
131+ },
132+ ...usage,
133+ };
134+ return message;
135+}
136+
114137 afterEach(() => {
115138 for (const dir of tempDirs.splice(0)) {
116139 rmSync(dir, { recursive: true, force: true });
@@ -493,6 +516,45 @@ describe("ui rendering", () => {
493516 }
494517 });
495518
519+ test("renderStatusBar uses the latest assistant usage for context percentage", () => {
520+ const faux = registerFauxProvider();
521+ const state = createTestState();
522+ state.model = {
523+ ...faux.getModel(),
524+ contextWindow: 1_000,
525+ };
526+ state.messages = [
527+ { role: "user", content: "first", timestamp: 1 },
528+ makeAssistantWithUsage("First.", {
529+ input: 120,
530+ output: 30,
531+ totalTokens: 150,
532+ }),
533+ { role: "user", content: "second", timestamp: 2 },
534+ makeAssistantWithUsage("Second.", {
535+ input: 200,
536+ output: 50,
537+ totalTokens: 250,
538+ }),
539+ ];
540+ state.stats = {
541+ totalInput: 4_000,
542+ totalOutput: 2_000,
543+ totalCost: 1.23,
544+ };
545+
546+ try {
547+ const node = renderStatusBar(state);
548+ const text = collectText(node);
549+
550+ expect(text).toContain("in:4.0k out:2.0k · 25.0%/1k · $1.23");
551+ expect(text).not.toContain("in:4.0k out:2.0k · 600.0%/1k · $1.23");
552+ } finally {
553+ faux.unregister();
554+ state.db.close();
555+ }
556+ });
557+
496558 test("buildHelpText includes current reasoning and verbose state", () => {
497559 const helpState: HelpRenderState = {
498560 providers: new Map(),
src/ui.ts+20−3
@@ -268,6 +268,11 @@ function formatTokens(n: number): string {
268268 return String(n);
269269 }
270270
271+/** Format a token capacity, trimming unnecessary trailing `.0`. */
272+function formatTokenCapacity(n: number): string {
273+ return formatTokens(n).replace(/\.0([kM])$/, "$1");
274+}
275+
271276 /** Format a dollar cost. */
272277 function formatCost(cost: number): string {
273278 return `$${cost.toFixed(2)}`;
@@ -303,17 +308,29 @@ function formatModelInfo(state: AppState): string {
303308 return `${state.model.provider}/${state.model.id} · ${formatEffort(state.effort)}`;
304309 }
305310
311+/** Get the most recent assistant message's total token usage. */
312+function getLatestContextTokenUsage(state: AppState): number {
313+ for (let i = state.messages.length - 1; i >= 0; i--) {
314+ const message = state.messages[i];
315+ if (message?.role === "assistant") {
316+ return (message as AssistantMessage).usage.totalTokens;
317+ }
318+ }
319+ return 0;
320+}
321+
306322 /** Format usage stats for the status bar right side. */
307323 function formatUsage(state: AppState): string {
308324 if (!state.model) return "";
309325 const inp = formatTokens(state.stats.totalInput);
310326 const out = formatTokens(state.stats.totalOutput);
311- const totalTokens = state.stats.totalInput + state.stats.totalOutput;
327+ const contextTokens = getLatestContextTokenUsage(state);
312328 const ctxPct =
313329 state.model.contextWindow > 0
314- ? Math.round((totalTokens / state.model.contextWindow) * 100)
330+ ? (contextTokens / state.model.contextWindow) * 100
315331 : 0;
316- return `in:${inp} out:${out} · ${ctxPct}% · ${formatCost(state.stats.totalCost)}`;
332+ const ctxWindow = formatTokenCapacity(state.model.contextWindow);
333+ return `in:${inp} out:${out} · ${ctxPct.toFixed(1)}%/${ctxWindow} · ${formatCost(state.stats.totalCost)}`;
317334 }
318335
319336 // ---------------------------------------------------------------------------