bug(core): fix `contentRangeTruncated` calculation. (#5329)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
joshualitt 2025-08-05 11:52:39 -07:00 committed by GitHub
parent 43d5aaa798
commit 08f1431946
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -426,6 +426,29 @@ describe('fileUtils', () => {
expect(result.linesShown).toEqual([6, 10]);
});
it('should identify truncation when reading the end of a file', async () => {
const lines = Array.from({ length: 20 }, (_, i) => `Line ${i + 1}`);
actualNodeFs.writeFileSync(testTextFilePath, lines.join('\n'));
// Read from line 11 to 20. The start is not 0, so it's truncated.
const result = await processSingleFileContent(
testTextFilePath,
tempRootDir,
10,
10,
);
const expectedContent = lines.slice(10, 20).join('\n');
expect(result.llmContent).toContain(expectedContent);
expect(result.llmContent).toContain(
'[File content truncated: showing lines 11-20 of 20 total lines. Use offset/limit parameters to view more.]',
);
expect(result.returnDisplay).toBe('Read lines 11-20 of 20 from test.txt');
expect(result.isTruncated).toBe(true); // This is the key check for the bug
expect(result.originalLineCount).toBe(20);
expect(result.linesShown).toEqual([11, 20]);
});
it('should handle limit exceeding file length', async () => {
const lines = ['Line 1', 'Line 2'];
actualNodeFs.writeFileSync(testTextFilePath, lines.join('\n'));

View File

@ -299,7 +299,8 @@ export async function processSingleFileContent(
return line;
});
const contentRangeTruncated = endLine < originalLineCount;
const contentRangeTruncated =
startLine > 0 || endLine < originalLineCount;
const isTruncated = contentRangeTruncated || linesWereTruncatedInLength;
let llmTextContent = '';