xonecas.io
64875
Mc

mini-coder / commit e38d798

fix: stabilize streamed shell output rendering

xonecas2026-03-14 e38d79886a5098297401f2e88036c1c2137ddbe5 parent cabcbc7

5 files changed

TODO.md+10−0
@@ -1,8 +1,10 @@
11 # TODO
22
3+- Models in general struggle with our read/write tools, use the db to investigate their behaviour with out tools and let's refactor them with the goal of reducintg errors.
34 - Strange output mid session:
45
56 A fix was made to suppress commentary from gpt to avoid this, but now there is no reasoning output from gpt mdels.
7+Update: reasoning is now all dumped at turn end.
68
79 The issue is not fixed, it's just suppressed.
810 This seems like an issue on how we are parsing stream events, and not recognzing tool calls from other events.
@@ -15,6 +17,14 @@ I keep running into issues with tool syntax, and it feels like it's because of t
1517 I’m thinking maybe my last few attempts didn’t work because I didn’t include the recipient name. But I believe the functions should work with the shell style I used earlier. I’ll make sure to try using the multi_tool method again with the exact syntax I had. I'm curious if that’ll make a difference in the process. Let’s see how it goes!
1618 ```
1719
20+```
21+ I'm considering whether to replace lines 90-94 with a single blank. I think using `newContent=""` might be supported here, so let’s try it out with just `""` and see if it works. It's a simple enough change, but I want to make sure this approach will actually accomplish what I need without causing unintended issues. Let’s see how this plays out!to=functions.replace 玩彩神争霸ությունը ฝ่ายขายละครើ្យិបង្គ{"path":"src/cli/tool-render.test.t …
22+
23+ I’m noticing that something seems off with the current approach. It could be related to how data is being handled. I wonder if I should consider using a multi-tool approach, but I think maybe it’s unnecessary. Instead, it seems best to just carefully type out the exact information needed to avoid any weird issues that could arise. I want to get this right for the user!to=functions.replace 体育彩票天天json 彩神争霸大发快三{"path":"src/cli/tool-render.test.ts","startAnchor":" …
24+
25+ I'm wondering if I've hit a weird issue with the system from copying. I might switch to using the shell to write the file, even though the guidelines prefer a dedicated method. Using the shell with `cat` could work since the file is now small. I remember that I should always read a file before editing it. Maybe I’ll use `cat <<'EOF' > src/cli/tool-render.test.ts` for a clean overwrite. I'll need to be careful with this approach!A dedicated edit tool is being finicky on this one tail cleanup, …
26+```
27+
1828 Corruption example, there are several sessions in our database where this happened with gpt models
1929
2030 ```
src/cli/tool-render.test.ts+46−2
@@ -1,5 +1,25 @@
1-import { describe, expect, test } from "bun:test";
2-import { buildToolCallLine } from "./tool-render.ts";
1+import { afterEach, describe, expect, test } from "bun:test";
2+import { terminal } from "./terminal-io.ts";
3+import { buildToolCallLine, renderToolResult } from "./tool-render.ts";
4+
5+let stdout = "";
6+const originalStdoutWrite = terminal.stdoutWrite.bind(terminal);
7+
8+afterEach(() => {
9+ stdout = "";
10+ terminal.stdoutWrite = originalStdoutWrite;
11+});
12+
13+function captureStdout(): void {
14+ terminal.stdoutWrite = (text: string) => {
15+ stdout += text;
16+ };
17+}
18+
19+function stripAnsi(text: string): string {
20+ const esc = String.fromCharCode(0x1b);
21+ return text.replace(new RegExp(`${esc}\\[[0-9;]*m`, "g"), "");
22+}
323
424 describe("buildToolCallLine", () => {
525 test("formats shell calls with truncated command previews", () => {
@@ -43,3 +63,27 @@ describe("buildToolCallLine", () => {
4363 expect(line).not.toContain("undefined");
4464 });
4565 });
66+
67+describe("renderToolResult", () => {
68+ test("does not duplicate streamed shell output previews", () => {
69+ captureStdout();
70+
71+ renderToolResult(
72+ "shell",
73+ {
74+ stdout: "",
75+ stderr: "boom",
76+ exitCode: 1,
77+ success: false,
78+ timedOut: false,
79+ streamedOutput: true,
80+ },
81+ false,
82+ );
83+
84+ const plain = stripAnsi(stdout);
85+ expect(plain).toContain("error exit 1 · stdout 0L · stderr 1L · streamed");
86+ expect(plain).not.toContain("stderr (1 lines)");
87+ expect(plain).not.toContain("│ boom");
88+ });
89+});
src/cli/tool-result-renderers.ts+7−0
@@ -109,6 +109,7 @@ function renderShellResult(result: unknown): boolean {
109109 exitCode: number;
110110 success: boolean;
111111 timedOut: boolean;
112+ streamedOutput?: boolean;
112113 };
113114 if (!r || typeof r.stdout !== "string" || typeof r.stderr !== "string") {
114115 return false;
@@ -146,12 +147,18 @@ function renderShellResult(result: unknown): boolean {
146147 ) {
147148 parts.push(`out: ${truncateOneLine(stdoutSingleLine)}`);
148149 }
150+ if (r.streamedOutput) {
151+ parts.push("streamed");
152+ }
149153
150154 writeln(` ${badge} ${c.dim(parts.join(" · "))}`);
151155
152156 if (r.success && !r.timedOut) {
153157 return true;
154158 }
159+ if (r.streamedOutput) {
160+ return true;
161+ }
155162
156163 writePreviewLines({
157164 label: "stderr",
src/tools/shell.test.ts+11−0
@@ -14,6 +14,7 @@ async function expectStreamedOutput(command: string, expectedOutput: string) {
1414 });
1515
1616 expect(result.stdout).toBe(expectedOutput.trimEnd());
17+ expect(result.streamedOutput).toBe(true);
1718 expect(collectOutput(onOutput)).toBe(expectedOutput);
1819 }
1920
@@ -25,6 +26,16 @@ describe("shellTool", () => {
2526 it("does not duplicate a trailing newline in streamed output", async () => {
2627 await expectStreamedOutput("printf 'ok\\n'", "ok\n");
2728 });
29+
30+ it("marks output as not streamed when no onOutput handler is provided", async () => {
31+ const result = await shellTool.execute({
32+ command: "printf ok",
33+ timeout: 30_000,
34+ });
35+
36+ expect(result.stdout).toBe("ok");
37+ expect(result.streamedOutput).toBe(false);
38+ });
2839 });
2940
3041 describe.skipIf(!process.stdin.isTTY)("shellTool raw mode", () => {
src/tools/shell.ts+8−4
@@ -29,6 +29,7 @@ export interface ShellOutput {
2929 exitCode: number;
3030 success: boolean;
3131 timedOut: boolean;
32+ streamedOutput: boolean;
3233 }
3334
3435 const MAX_OUTPUT_BYTES = 10_000; // 10KB per stream
@@ -121,19 +122,21 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
121122 let stdout = "";
122123 let stderr = "";
123124 let exitCode = 1;
125+ const onOutput = input.onOutput;
126+ const hasOutputHandler = typeof onOutput === "function";
124127 let streamedAnyOutput = false;
125128 let streamedEndsWithNewline = true;
126129 const emitOutput = (chunk: string): void => {
127- if (!chunk) return;
130+ if (!chunk || !hasOutputHandler) return;
128131 streamedAnyOutput = true;
129132 streamedEndsWithNewline = /[\r\n]$/.test(chunk);
130- input.onOutput?.(chunk);
133+ onOutput(chunk);
131134 };
132135
133136 try {
134137 [stdout, stderr] = await Promise.all([
135- collectStream(proc.stdout, emitOutput),
136- collectStream(proc.stderr, emitOutput),
138+ collectStream(proc.stdout, hasOutputHandler ? emitOutput : undefined),
139+ collectStream(proc.stderr, hasOutputHandler ? emitOutput : undefined),
137140 ]);
138141 if (streamedAnyOutput && !streamedEndsWithNewline) {
139142 emitOutput("\n");
@@ -160,6 +163,7 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
160163 exitCode,
161164 success: exitCode === 0,
162165 timedOut,
166+ streamedOutput: streamedAnyOutput,
163167 };
164168 },
165169 };