fix(coding-agent): harden find cancellation and grep match formatting closes #3148

This commit is contained in:
Mario Zechner
2026-04-16 13:15:58 +02:00
parent b5007640d9
commit e9ba9e2ebc
4 changed files with 176 additions and 79 deletions

View File

@@ -267,7 +267,7 @@ export function createGrepToolDefinition(
};
// Collect matches during streaming, then format them after rg exits.
const matches: Array<{ filePath: string; lineNumber: number }> = [];
const matches: Array<{ filePath: string; lineNumber: number; lineText?: string }> = [];
rl.on("line", (line) => {
if (!line.trim() || matchCount >= effectiveLimit) return;
let event: any;
@@ -280,7 +280,9 @@ export function createGrepToolDefinition(
matchCount++;
const filePath = event.data?.path?.text;
const lineNumber = event.data?.line_number;
if (filePath && typeof lineNumber === "number") matches.push({ filePath, lineNumber });
const lineText = event.data?.lines?.text;
if (filePath && typeof lineNumber === "number")
matches.push({ filePath, lineNumber, lineText });
if (matchCount >= effectiveLimit) {
matchLimitReached = true;
stopChild(true);
@@ -312,8 +314,19 @@ export function createGrepToolDefinition(
// Format matches after streaming finishes so custom readFile() backends can be async.
for (const match of matches) {
const block = await formatBlock(match.filePath, match.lineNumber);
outputLines.push(...block);
if (contextValue === 0 && match.lineText !== undefined) {
const relativePath = formatPath(match.filePath);
const sanitized = match.lineText
.replace(/\r\n/g, "\n")
.replace(/\r/g, "")
.replace(/\n$/, "");
const { text: truncatedText, wasTruncated } = truncateLine(sanitized);
if (wasTruncated) linesTruncated = true;
outputLines.push(`${relativePath}:${match.lineNumber}: ${truncatedText}`);
} else {
const block = await formatBlock(match.filePath, match.lineNumber);
outputLines.push(...block);
}
}
const rawOutput = outputLines.join("\n");