batch3 T4 review fixes: harmonize find_files cap; delegate to file_ops
- file_ops.MAX_FIND_RESULTS: 1000 -> 200 to match existing tool cap and preserve LLM behavior - tools.find_files now delegates to file_ops.findFiles (parallels how grep already delegates); drops ~50 LOC of duplicated path resolution and rg subprocess - Drop unused basename import in file_ops Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
import { readFile, readdir, stat } from 'node:fs/promises';
|
||||
import { resolve, basename, relative } from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { z } from 'zod';
|
||||
import { pathGuard, PathScopeError } from './path_guard.js';
|
||||
import { grep as fileOpsGrep } from './file_ops.js';
|
||||
import { grep as fileOpsGrep, findFiles as fileOpsFindFiles } from './file_ops.js';
|
||||
|
||||
const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
||||
const DEFAULT_VIEW_LINES = 200;
|
||||
@@ -249,55 +248,21 @@ export const findFiles: ToolDef<FindFilesInputT> = {
|
||||
},
|
||||
},
|
||||
async execute(input, projectRoot) {
|
||||
const target = await pathGuard(projectRoot, input.path ?? projectRoot);
|
||||
const limit = Math.min(
|
||||
Math.max(input.max_results ?? DEFAULT_FIND_RESULTS, 1),
|
||||
MAX_FIND_RESULTS
|
||||
);
|
||||
return await new Promise((resolveP, rejectP) => {
|
||||
const args = ['--files', '--glob', input.pattern, target];
|
||||
const child = spawn('rg', args, { cwd: projectRoot });
|
||||
const paths: string[] = [];
|
||||
let total = 0;
|
||||
let buf = '';
|
||||
let stderr = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk: string) => {
|
||||
buf += chunk;
|
||||
let idx;
|
||||
while ((idx = buf.indexOf('\n')) >= 0) {
|
||||
const line = buf.slice(0, idx);
|
||||
buf = buf.slice(idx + 1);
|
||||
if (!line) continue;
|
||||
total++;
|
||||
if (paths.length < limit) {
|
||||
paths.push(relative(projectRoot, line) || line);
|
||||
}
|
||||
}
|
||||
});
|
||||
child.stderr.on('data', (chunk: string) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
child.on('error', (err) => rejectP(err));
|
||||
child.on('close', (code) => {
|
||||
if (code === 2) {
|
||||
rejectP(new Error(`ripgrep failed: ${stderr.slice(0, 300)}`));
|
||||
return;
|
||||
}
|
||||
if (buf.length > 0) {
|
||||
total++;
|
||||
if (paths.length < limit) {
|
||||
paths.push(relative(projectRoot, buf) || buf);
|
||||
}
|
||||
}
|
||||
resolveP({
|
||||
paths,
|
||||
total,
|
||||
truncated: total > paths.length,
|
||||
});
|
||||
});
|
||||
// Delegate to file_ops.findFiles; reshape { files, total, truncated } to
|
||||
// preserve the LLM-visible output format { paths, total, truncated }
|
||||
const result = await fileOpsFindFiles(projectRoot, input.pattern, {
|
||||
path: input.path,
|
||||
max_results: limit,
|
||||
});
|
||||
return {
|
||||
paths: result.files,
|
||||
total: result.total,
|
||||
truncated: result.truncated,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user