fix: context length fixes, truncation, etc
This commit is contained in:
parent
b426b196c9
commit
613ffb8ca9
3 changed files with 303 additions and 24 deletions
|
|
@ -10,6 +10,7 @@ import {
|
|||
isGitCommitCommand,
|
||||
isPullRequestCommand,
|
||||
} from "../telemetry/utils.js";
|
||||
import { truncateOutputFromStart } from "../util/truncateOutput.js";
|
||||
|
||||
import { Tool } from "./types.js";
|
||||
|
||||
|
|
@ -120,18 +121,7 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed
|
|||
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);
|
||||
resolve(truncateOutputFromStart(output).output);
|
||||
}, TIMEOUT_MS);
|
||||
};
|
||||
|
||||
|
|
@ -176,18 +166,7 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed
|
|||
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(output);
|
||||
resolve(truncateOutputFromStart(output).output);
|
||||
});
|
||||
|
||||
child.on("error", (error) => {
|
||||
|
|
|
|||
219
extensions/cli/src/util/truncateOutput.test.ts
Normal file
219
extensions/cli/src/util/truncateOutput.test.ts
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
import {
|
||||
truncateOutputFromStart,
|
||||
TRUNCATION_MAX_CHARACTERS,
|
||||
TRUNCATION_MAX_LINES,
|
||||
} from "./truncateOutput.js";
|
||||
|
||||
describe("truncateOutputFromStart", () => {
|
||||
describe("no truncation needed", () => {
|
||||
it("should return empty string unchanged", () => {
|
||||
const result = truncateOutputFromStart("");
|
||||
expect(result.output).toBe("");
|
||||
expect(result.wasTruncated).toBe(false);
|
||||
});
|
||||
|
||||
it("should return short output unchanged", () => {
|
||||
const input = "hello world\nline 2\nline 3";
|
||||
const result = truncateOutputFromStart(input);
|
||||
expect(result.output).toBe(input);
|
||||
expect(result.wasTruncated).toBe(false);
|
||||
});
|
||||
|
||||
it("should return output at exactly max lines unchanged", () => {
|
||||
const lines = Array.from(
|
||||
{ length: TRUNCATION_MAX_LINES },
|
||||
(_, i) => `line ${i + 1}`,
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
const result = truncateOutputFromStart(input);
|
||||
expect(result.output).toBe(input);
|
||||
expect(result.wasTruncated).toBe(false);
|
||||
});
|
||||
|
||||
it("should return output at exactly max characters unchanged", () => {
|
||||
const input = "a".repeat(TRUNCATION_MAX_CHARACTERS);
|
||||
const result = truncateOutputFromStart(input);
|
||||
expect(result.output).toBe(input);
|
||||
expect(result.wasTruncated).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("truncation by line count", () => {
|
||||
it("should truncate when exceeding max lines", () => {
|
||||
const totalLines = TRUNCATION_MAX_LINES + 500;
|
||||
const lines = Array.from(
|
||||
{ length: totalLines },
|
||||
(_, i) => `line ${i + 1}`,
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain("(previous 500 lines truncated)");
|
||||
expect(result.output).toContain("line 501");
|
||||
expect(result.output).toContain(`line ${totalLines}`);
|
||||
expect(result.output).not.toContain("line 500\n");
|
||||
});
|
||||
|
||||
it("should preserve the last max lines when truncating", () => {
|
||||
const totalLines = TRUNCATION_MAX_LINES * 2;
|
||||
const lines = Array.from(
|
||||
{ length: totalLines },
|
||||
(_, i) => `line ${i + 1}`,
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain(
|
||||
`(previous ${TRUNCATION_MAX_LINES} lines truncated)`,
|
||||
);
|
||||
|
||||
// Check that we have lines (MAX_LINES+1) to (MAX_LINES*2)
|
||||
const outputLines = result.output.split("\n");
|
||||
// First line is the truncation message, second is empty, then content
|
||||
expect(outputLines[2]).toBe(`line ${TRUNCATION_MAX_LINES + 1}`);
|
||||
expect(outputLines[outputLines.length - 1]).toBe(`line ${totalLines}`);
|
||||
});
|
||||
|
||||
it("should handle exactly max lines + 1", () => {
|
||||
const totalLines = TRUNCATION_MAX_LINES + 1;
|
||||
const lines = Array.from(
|
||||
{ length: totalLines },
|
||||
(_, i) => `line ${i + 1}`,
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain("(previous 1 lines truncated)");
|
||||
expect(result.output).toContain("line 2");
|
||||
expect(result.output).toContain(`line ${totalLines}`);
|
||||
expect(result.output).not.toContain("\nline 1\n");
|
||||
});
|
||||
});
|
||||
|
||||
describe("truncation by character count", () => {
|
||||
it("should truncate when exceeding max characters", () => {
|
||||
// Create output that exceeds character limit but not line limit
|
||||
const input = "a".repeat(TRUNCATION_MAX_CHARACTERS + 10000);
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output.length).toBeLessThanOrEqual(
|
||||
TRUNCATION_MAX_CHARACTERS + 100 /* header allowance */,
|
||||
);
|
||||
expect(result.output).toContain("characters truncated");
|
||||
});
|
||||
|
||||
it("should truncate at line boundary when possible", () => {
|
||||
// Create lines that exceed character limit
|
||||
const line = "x".repeat(100) + "\n";
|
||||
const input = line.repeat(600); // 60600 characters, 600 lines
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
// Should start at a line boundary (after truncation header)
|
||||
const contentStart = result.output.indexOf("\n\n") + 2;
|
||||
const content = result.output.slice(contentStart);
|
||||
// Content should start with 'x' (beginning of a line, not mid-line)
|
||||
expect(content[0]).toBe("x");
|
||||
});
|
||||
});
|
||||
|
||||
describe("combined truncation", () => {
|
||||
it("should apply both line and character truncation with single message", () => {
|
||||
// Create lines that exceed both limits
|
||||
// 2x max lines, each 100 chars = way more than max characters
|
||||
const line = "y".repeat(100);
|
||||
const lines = Array.from(
|
||||
{ length: TRUNCATION_MAX_LINES * 2 },
|
||||
() => line,
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
// Should be truncated by lines first, then by characters
|
||||
expect(result.output.length).toBeLessThanOrEqual(
|
||||
TRUNCATION_MAX_CHARACTERS + 100 /* header allowance */,
|
||||
);
|
||||
|
||||
// Should have a single combined message, not duplicate notes
|
||||
expect(result.output).toContain("previous output truncated:");
|
||||
expect(result.output).toContain(`${TRUNCATION_MAX_LINES} lines`);
|
||||
expect(result.output).toContain("characters removed");
|
||||
|
||||
// Should NOT have separate line truncation message
|
||||
const truncationNoteCount = (result.output.match(/\(previous/g) || [])
|
||||
.length;
|
||||
expect(truncationNoteCount).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("should handle single very long line without newlines", () => {
|
||||
const input = "z".repeat(TRUNCATION_MAX_CHARACTERS + 10000);
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain("characters truncated");
|
||||
// Should keep approximately max chars (plus header)
|
||||
expect(result.output.length).toBeLessThanOrEqual(
|
||||
TRUNCATION_MAX_CHARACTERS + 100,
|
||||
);
|
||||
expect(result.output.length).toBeGreaterThan(TRUNCATION_MAX_CHARACTERS);
|
||||
});
|
||||
|
||||
it("should not snap to line boundary if newline is too far into the text", () => {
|
||||
// Create a massive line followed by a newline far from the truncation point
|
||||
const leadingChars = TRUNCATION_MAX_CHARACTERS + 5000;
|
||||
const input = "a".repeat(leadingChars) + "\n" + "b".repeat(10);
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
// Should cut mid-line since newline is beyond snap threshold into kept text
|
||||
// Result should contain 'a' characters (cut mid-line)
|
||||
const contentStart = result.output.indexOf("\n\n") + 2;
|
||||
const content = result.output.slice(contentStart);
|
||||
expect(content[0]).toBe("a");
|
||||
});
|
||||
|
||||
it("should snap to line boundary if newline is within snap threshold", () => {
|
||||
// Create text where truncation point lands within snap threshold of a newline
|
||||
const charsBeforeNewline = TRUNCATION_MAX_CHARACTERS / 5; // Well under max
|
||||
const input =
|
||||
"a".repeat(charsBeforeNewline) +
|
||||
"\n" +
|
||||
"b".repeat(TRUNCATION_MAX_CHARACTERS);
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
// Newline should be within snap threshold, so should start with 'b'
|
||||
const contentStart = result.output.indexOf("\n\n") + 2;
|
||||
const content = result.output.slice(contentStart);
|
||||
expect(content[0]).toBe("b");
|
||||
});
|
||||
|
||||
it("should handle output with only newlines", () => {
|
||||
const input = "\n".repeat(TRUNCATION_MAX_LINES + 500);
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain("lines truncated)");
|
||||
});
|
||||
|
||||
it("should handle mixed content with empty lines", () => {
|
||||
const totalLines = TRUNCATION_MAX_LINES + 200;
|
||||
const lines = Array.from({ length: totalLines }, (_, i) =>
|
||||
i % 2 === 0 ? `content line ${i}` : "",
|
||||
);
|
||||
const input = lines.join("\n");
|
||||
const result = truncateOutputFromStart(input);
|
||||
|
||||
expect(result.wasTruncated).toBe(true);
|
||||
expect(result.output).toContain("(previous 200 lines truncated)");
|
||||
});
|
||||
});
|
||||
});
|
||||
81
extensions/cli/src/util/truncateOutput.ts
Normal file
81
extensions/cli/src/util/truncateOutput.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
export const TRUNCATION_MAX_CHARACTERS = 50000;
|
||||
export const TRUNCATION_MAX_LINES = 1000;
|
||||
export const TRUNCATION_LINE_SNAP_THRESHOLD = 1000;
|
||||
|
||||
interface TruncationResult {
|
||||
output: string;
|
||||
wasTruncated: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates output from the beginning to fit within limits, preserving the end.
|
||||
*
|
||||
* Limits: max 50000 characters OR 1000 lines, whichever is smaller.
|
||||
* When truncated, removes content from the beginning and adds a "(previous output truncated)" note.
|
||||
*/
|
||||
export function truncateOutputFromStart(output: string): TruncationResult {
|
||||
if (!output) {
|
||||
return { output, wasTruncated: false };
|
||||
}
|
||||
|
||||
const lines = output.split("\n");
|
||||
|
||||
// Check if we need to truncate by lines first
|
||||
if (lines.length > TRUNCATION_MAX_LINES) {
|
||||
const linesTruncated = lines.length - TRUNCATION_MAX_LINES;
|
||||
const preservedLines = lines.slice(-TRUNCATION_MAX_LINES);
|
||||
const contentAfterLineTruncation = preservedLines.join("\n");
|
||||
|
||||
// After line truncation, check character limit
|
||||
if (contentAfterLineTruncation.length > TRUNCATION_MAX_CHARACTERS) {
|
||||
return truncateCharactersFromStart(
|
||||
contentAfterLineTruncation,
|
||||
linesTruncated,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
output: `(previous ${linesTruncated} lines truncated)\n\n${contentAfterLineTruncation}`,
|
||||
wasTruncated: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Check character limit
|
||||
if (output.length > TRUNCATION_MAX_CHARACTERS) {
|
||||
return truncateCharactersFromStart(output, 0);
|
||||
}
|
||||
|
||||
return { output, wasTruncated: false };
|
||||
}
|
||||
|
||||
function truncateCharactersFromStart(
|
||||
text: string,
|
||||
linesTruncated: number,
|
||||
): TruncationResult {
|
||||
// Remove characters from the beginning, keeping the end
|
||||
const truncationPoint = text.length - TRUNCATION_MAX_CHARACTERS;
|
||||
const textToKeep = text.slice(truncationPoint);
|
||||
|
||||
// Try to start at a clean line boundary, but only if there's a newline
|
||||
// within a reasonable distance. Otherwise just cut mid-line.
|
||||
const firstNewline = textToKeep.indexOf("\n");
|
||||
const shouldSnapToLine =
|
||||
firstNewline !== -1 && firstNewline < TRUNCATION_LINE_SNAP_THRESHOLD;
|
||||
const cleanText = shouldSnapToLine
|
||||
? textToKeep.slice(firstNewline + 1)
|
||||
: textToKeep;
|
||||
|
||||
const charsRemoved = text.length - cleanText.length;
|
||||
|
||||
let prefix: string;
|
||||
if (linesTruncated > 0) {
|
||||
prefix = `(previous output truncated: ${linesTruncated} lines and ${charsRemoved} characters removed)\n\n`;
|
||||
} else {
|
||||
prefix = `(previous ${charsRemoved} characters truncated)\n\n`;
|
||||
}
|
||||
|
||||
return {
|
||||
output: prefix + cleanText,
|
||||
wasTruncated: true,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue