Compare commits

...

2 Commits

Author SHA1 Message Date
Saoud Rizwan 5ebac38628 fix: make search output truncation byte accurate 2026-05-24 18:13:00 -07:00
Saoud Rizwan ae97026a17 fix: cap search_codebase output 2026-05-24 18:04:42 -07:00
@@ -44,6 +44,12 @@ export interface SearchExecutorOptions {
* @default 20
*/
maxDepth?: number;
/**
* Maximum output size in bytes
* @default 1_000_000 (1MB)
*/
maxOutputBytes?: number;
}
const DEFAULT_INCLUDE_EXTENSIONS = [
@@ -119,6 +125,19 @@ interface SearchMatch {
context: string[];
}
function truncateOutput(output: string, maxOutputBytes: number): string {
const outputSize = Buffer.byteLength(output);
if (outputSize <= maxOutputBytes) {
return output;
}
const truncated = Buffer.from(output, "utf8")
.subarray(0, maxOutputBytes)
.toString("utf8")
.replace(/\uFFFD$/u, "");
return `${truncated}\n\n[Output truncated: ${outputSize} bytes total, showing first ${maxOutputBytes} bytes]`;
}
let rgAvailable: boolean | null = null;
function checkRipgrepAvailable(): Promise<boolean> {
@@ -311,6 +330,7 @@ export function createSearchExecutor(
maxResults = 100,
contextLines = 2,
maxDepth = 20,
maxOutputBytes = 1_000_000,
} = options;
const excludeDirsSet = new Set(excludeDirs);
const includeExtensionsSet = new Set(
@@ -359,7 +379,7 @@ export function createSearchExecutor(
);
}
return resultLines.join("\n");
return truncateOutput(resultLines.join("\n"), maxOutputBytes);
}
// Fallback to manual regex search
@@ -445,7 +465,10 @@ export function createSearchExecutor(
// Format results
if (matches.length === 0) {
return `No results found for pattern: ${query}\nSearched ${totalFilesSearched} files.`;
return truncateOutput(
`No results found for pattern: ${query}\nSearched ${totalFilesSearched} files.`,
maxOutputBytes,
);
}
const resultLines: string[] = [
@@ -466,6 +489,6 @@ export function createSearchExecutor(
);
}
return resultLines.join("\n");
return truncateOutput(resultLines.join("\n"), maxOutputBytes);
};
}