@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
95 lines • 4.33 kB
JavaScript
import { realpath, stat } from "node:fs/promises";
import { basename, dirname, relative, resolve } from "node:path";
import { GrepEngineError } from "../engine.js";
import { argvBudget, argvCost, packArgv, roomAfter } from "./argv.js";
import { pathOrder, slashPath } from "./paths.js";
import { MAX_FILE_BYTES } from "./prefix-pass.js";
export async function resolveRoots(request, result) {
const roots = [];
for (const path of new Set(request.paths)) {
try {
const [info, canonical] = await Promise.all([stat(path), realpath(path)]);
// Anchored rg globs use the OS cwd (canonical even for /var aliases on macOS).
roots.push({
path: canonical,
alias: path,
cwd: info.isDirectory() ? canonical : dirname(canonical),
file: info.isFile(),
});
}
catch (error) {
if (error.code !== "ENOENT")
throw error;
result.missingPaths.push(path);
}
}
if (roots.length === 0)
throw new GrepEngineError("PATH_NOT_FOUND", `No search paths exist: ${request.paths.join(", ")}`);
return roots;
}
export async function enumerateCandidates({ roots, cwd, walk, run, check, result, budget = argvBudget(), }) {
const candidates = new Map();
for (const root of roots) {
const listed = root.file
? [root.path]
: (await run(["--files", "--null", "--sort", "path", ...walk, "--", root.path], root.cwd))
.toString("utf8")
.split("\0")
.filter(Boolean);
for (const path of listed) {
check();
const absolute = resolve(root.cwd, path);
const [canonical, info] = await Promise.all([realpath(absolute), stat(absolute)]).catch((error) => {
// A named file that vanished after root resolution fails like rg's own listing did.
if (root.file && error.code === "ENOENT")
throw new GrepEngineError("ENGINE_UNAVAILABLE", `${root.path}: No such file or directory`);
throw error;
});
const alias = resolve(root.alias, relative(root.path, absolute));
const display = slashPath(relative(cwd, alias)) || basename(alias);
const existing = candidates.get(canonical);
if (!existing || pathOrder(display, existing.display) < 0)
candidates.set(canonical, { absolute, canonical, display, root, size: info.size });
}
}
const binaryPaths = new Set();
// rg ignores --max-filesize for explicit file roots (the facade's second phase).
// Oversized roots must be classified solely by the bounded prefix pass.
const scanned = roots.filter((root) => (candidates.get(root.path)?.size ?? 0) <= MAX_FILE_BYTES);
const files = scanned.filter((root) => root.file);
// Explicit files share one scan per batch; a directory root keeps its own anchored walk.
const nulScan = [
"-a",
"-l",
"--null",
"--sort",
"path",
"--encoding",
"none",
"--max-filesize",
String(MAX_FILE_BYTES),
...walk,
"-e",
"\\x00",
"--",
];
const room = roomAfter(nulScan, budget);
const scans = [
...packArgv(files, (root) => argvCost(root.path), room).map((batch) => ({
cwd: batch[0].cwd,
paths: batch.map((root) => root.path),
})),
...scanned.filter((root) => !root.file).map((root) => ({ cwd: root.cwd, paths: [root.path] })),
];
// Scan the entire normal-size file, including NULs after rg's first search buffer.
for (const scan of scans) {
const output = await run([...nulScan, ...scan.paths], scan.cwd);
for (const path of output.toString("utf8").split("\0").filter(Boolean))
binaryPaths.add(await realpath(resolve(scan.cwd, path)));
}
result.skippedBinary = [...binaryPaths].filter((path) => candidates.has(path)).length;
return [...candidates.values()]
.sort((a, b) => pathOrder(a.display, b.display))
.map((candidate, index) => ({ ...candidate, ordinal: index + 1, binary: binaryPaths.has(candidate.canonical) }));
}
//# sourceMappingURL=enumerate.js.map