From 51f3df5411dfc19bbeb10ad104e3789919c510ec Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 26 Aug 2025 15:22:52 -0700 Subject: [PATCH] feat: truncate Bash output to 5k lines --- .../cli/src/tools/runTerminalCommand.test.ts | 23 ++++++++++++ .../cli/src/tools/runTerminalCommand.ts | 36 +++++++++++++++---- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/extensions/cli/src/tools/runTerminalCommand.test.ts b/extensions/cli/src/tools/runTerminalCommand.test.ts index d828ded2e..68cdc12aa 100644 --- a/extensions/cli/src/tools/runTerminalCommand.test.ts +++ b/extensions/cli/src/tools/runTerminalCommand.test.ts @@ -184,6 +184,29 @@ describe("runTerminalCommandTool", () => { const lines = result.trim().split("\n"); expect(lines).toHaveLength(1000); }); + + it("should truncate output when it exceeds 5000 lines", async () => { + // Generate a command that produces more than 5000 lines + const result = await runTerminalCommandTool.run({ + command: "seq 1 6000", + }); + + // Should contain the truncation message + expect(result).toContain( + "[Output truncated to first 5000 lines of 6001 total]", + ); + + // Should contain first line + expect(result).toContain("1\n"); + + // Should contain line 5000 but not line 6000 + expect(result).toContain("5000"); + expect(result).not.toContain("6000"); + + // Count lines in the result - should be 5000 content lines + 2 truncation message lines + const lines = result.split("\n"); + expect(lines).toHaveLength(5002); // 5000 content lines + empty line + truncation message + }); }); describe("command types", () => { diff --git a/extensions/cli/src/tools/runTerminalCommand.ts b/extensions/cli/src/tools/runTerminalCommand.ts index 304fe7ba5..c4866e7f0 100644 --- a/extensions/cli/src/tools/runTerminalCommand.ts +++ b/extensions/cli/src/tools/runTerminalCommand.ts @@ -62,11 +62,21 @@ The command will be executed from the current working directory: ${process.cwd() if (isResolved) return; isResolved = true; child.kill(); - const output = stdout + (stderr ? `\nStderr: ${stderr}` : ""); - resolve( - output + - `\n\n[Command timed out after ${TIMEOUT_MS / 1000} seconds of no output]`, - ); + let output = stdout + (stderr ? `\nStderr: ${stderr}` : ""); + output += `\n\n[Command timed out after ${TIMEOUT_MS / 1000} seconds of no output]`; + + // Truncate output if it has too many lines + const lines = output.split("\n"); + if (lines.length > 5000) { + const truncatedOutput = lines.slice(0, 5000).join("\n"); + resolve( + truncatedOutput + + `\n\n[Output truncated to first 5000 lines of ${lines.length} total]`, + ); + return; + } + + resolve(output); }, TIMEOUT_MS); }; @@ -106,11 +116,23 @@ The command will be executed from the current working directory: ${process.cwd() } } + let output = stdout; if (stderr) { - resolve(stdout + `\nStderr: ${stderr}`); + output = stdout + `\nStderr: ${stderr}`; + } + + // Truncate output if it has too many lines + const lines = output.split("\n"); + if (lines.length > 5000) { + const truncatedOutput = lines.slice(0, 5000).join("\n"); + resolve( + truncatedOutput + + `\n\n[Output truncated to first 5000 lines of ${lines.length} total]`, + ); return; } - resolve(stdout); + + resolve(output); }); child.on("error", (error) => {