xonecas.io
64875
Mc

mini-coder / commit 7403923

fix: keep streamed shell output and summaries on separate lines

xonecas2026-03-14 7403923b8f41cc24434d76a74074eac24e29004f parent dd4d034

2 files changed

src/tools/shell.test.ts+30−2
@@ -1,7 +1,35 @@
1-import { describe, expect, it } from "bun:test";
1+import { describe, expect, it, mock } from "bun:test";
22 import { shellTool } from "./shell.ts";
33
4-describe.skipIf(!process.stdin.isTTY)("shellTool", () => {
4+describe("shellTool", () => {
5+ it("appends a newline to streamed output when the command does not print one", async () => {
6+ const onOutput = mock(() => {});
7+
8+ const result = await shellTool.execute({
9+ command: "printf ok",
10+ timeout: 30_000,
11+ onOutput,
12+ });
13+
14+ expect(result.stdout).toBe("ok");
15+ expect(onOutput.mock.calls.map(([chunk]) => chunk).join("")).toBe("ok\n");
16+ });
17+
18+ it("does not duplicate a trailing newline in streamed output", async () => {
19+ const onOutput = mock(() => {});
20+
21+ const result = await shellTool.execute({
22+ command: "printf 'ok\\n'",
23+ timeout: 30_000,
24+ onOutput,
25+ });
26+
27+ expect(result.stdout).toBe("ok");
28+ expect(onOutput.mock.calls.map(([chunk]) => chunk).join("")).toBe("ok\n");
29+ });
30+});
31+
32+describe.skipIf(!process.stdin.isTTY)("shellTool raw mode", () => {
533 it("preserves stdin raw mode if it was enabled", async () => {
634 // Set raw mode to true initially
735 process.stdin.setRawMode(true);
src/tools/shell.ts+13−2
@@ -121,12 +121,23 @@ export const shellTool: ToolDef<ShellInput, ShellOutput> = {
121121 let stdout = "";
122122 let stderr = "";
123123 let exitCode = 1;
124+ let streamedAnyOutput = false;
125+ let streamedEndsWithNewline = true;
126+ const emitOutput = (chunk: string): void => {
127+ if (!chunk) return;
128+ streamedAnyOutput = true;
129+ streamedEndsWithNewline = /[\r\n]$/.test(chunk);
130+ input.onOutput?.(chunk);
131+ };
124132
125133 try {
126134 [stdout, stderr] = await Promise.all([
127- collectStream(proc.stdout, input.onOutput),
128- collectStream(proc.stderr, input.onOutput),
135+ collectStream(proc.stdout, emitOutput),
136+ collectStream(proc.stderr, emitOutput),
129137 ]);
138+ if (streamedAnyOutput && !streamedEndsWithNewline) {
139+ emitOutput("\n");
140+ }
130141
131142 exitCode = await proc.exited;
132143 } finally {