Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/core/src/tools/grep-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import fsPromises from 'node:fs/promises';
import { debugLogger } from '../utils/debugLogger.js';
import { MAX_LINE_LENGTH_TEXT_FILE } from '../utils/constants.js';

/**
* Result object for a single grep match
Expand Down Expand Up @@ -198,7 +199,13 @@ export async function formatGrepResults(
// If isContext is undefined, assume it's a match (false)
const separator = match.isContext ? '-' : ':';
// trimEnd to avoid double newlines if line has them, but we want to preserve indentation
llmContent += `L${match.lineNumber}${separator} ${match.line.trimEnd()}\n`;
let lineContent = match.line.trimEnd();
if (lineContent.length > MAX_LINE_LENGTH_TEXT_FILE) {
lineContent =
lineContent.substring(0, MAX_LINE_LENGTH_TEXT_FILE) +
'... [truncated]';
}
llmContent += `L${match.lineNumber}${separator} ${lineContent}\n`;
});
llmContent += '---\n';
}
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/tools/grep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,22 @@ describe('GrepTool', () => {
// Verify context after
expect(result.llmContent).toContain('L60- Line 60');
});

it('should truncate excessively long lines', async () => {
const longString = 'a'.repeat(3000);
await fs.writeFile(
path.join(tempRootDir, 'longline.txt'),
`Target match ${longString}`,
);

const params: GrepToolParams = { pattern: 'Target match' };
const invocation = grepTool.build(params);
const result = await invocation.execute(abortSignal);

// MAX_LINE_LENGTH_TEXT_FILE is 2000. It should be truncated.
expect(result.llmContent).toContain('... [truncated]');
expect(result.llmContent).not.toContain(longString);
});
});

describe('getDescription', () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/core/src/tools/ripGrep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2028,6 +2028,32 @@ describe('RipGrepTool', () => {
expect(result.llmContent).not.toContain('fileB.txt');
expect(result.llmContent).toContain('Copyright 2025 Google LLC');
});

it('should truncate excessively long lines', async () => {
const longString = 'a'.repeat(3000);
mockSpawn.mockImplementation(
createMockSpawn({
outputData:
JSON.stringify({
type: 'match',
data: {
path: { text: 'longline.txt' },
line_number: 1,
lines: { text: `Target match ${longString}\n` },
},
}) + '\n',
exitCode: 0,
}),
);

const params: RipGrepToolParams = { pattern: 'Target match', context: 0 };
const invocation = grepTool.build(params);
const result = await invocation.execute(abortSignal);

// MAX_LINE_LENGTH_TEXT_FILE is 2000. It should be truncated.
expect(result.llmContent).toContain('... [truncated]');
expect(result.llmContent).not.toContain(longString);
});
});
});

Expand Down
Loading