Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(local-file-shell): report real match count for file-scoped context grep (#18101)

Arvin Xu3a0d5f2a6f46
diff --git a/packages/local-file-shell/src/contentSearch/__tests__/enginePathParity.test.ts b/packages/local-file-shell/src/contentSearch/__tests__/enginePathParity.test.ts
index 22320782ce9..9b77d42a79b 100644
--- a/packages/local-file-shell/src/contentSearch/__tests__/enginePathParity.test.ts
+++ b/packages/local-file-shell/src/contentSearch/__tests__/enginePathParity.test.ts
@@ -59,6 +59,12 @@ beforeAll(async () => {
await mkdir(path.join(repo, 'build'), { recursive: true });
await writeFile(path.join(repo, '.gitignore'), 'dist/\n*.log\n');
await writeFile(path.join(repo, 'src', 'a.ts'), 'needle\n');
+ await writeFile(
+ path.join(repo, 'src', 'ctx.ts'),
+ ['one', 'two', 'ctxmark first', 'four', 'five', 'six', 'seven', 'ctxmark second', 'nine'].join(
+ '\n',
+ ) + '\n',
+ );
await writeFile(path.join(repo, 'src', 'noisy.log'), 'needle\n');
await writeFile(path.join(repo, 'dist', 'b.js'), 'needle\n');
await writeFile(path.join(repo, 'dist', 'sub', 'c.js'), 'needle\n');
@@ -146,6 +152,29 @@ describe('content search engine parity', () => {
}
}, 60_000);
+ it('reports the real match count for a file-scoped search with context lines', async () => {
+ // Regression: with `-A`/`-B` the match count comes from a second `-c` run,
+ // which used the raw scope as its `cwd`. A file scope made that spawn throw
+ // and the catch silently returned 0 β€” so the summary said "Found 0 matches"
+ // while the output right below it contained the match lines, and the agent
+ // reading the summary kept retrying with new patterns.
+ const file = path.join(repo, 'src', 'ctx.ts');
+
+ for (const engine of engines) {
+ const r = await search(engine, {
+ '-A': 2,
+ '-B': 2,
+ '-n': true,
+ 'output_mode': 'content',
+ 'pattern': 'ctxmark',
+ 'scope': file,
+ } as GrepContentParams);
+
+ expect(r.total_matches, `${engine} miscounted a file-scoped context search`).toBe(2);
+ expect(r.matches.length, `${engine} should include context lines`).toBeGreaterThan(2);
+ }
+ }, 60_000);
+
it('prefixes the file path when scope names a single file', async () => {
const file = path.join(repo, 'src', 'a.ts');
diff --git a/packages/local-file-shell/src/contentSearch/impl/unix.ts b/packages/local-file-shell/src/contentSearch/impl/unix.ts
index fc5c4466649..a8f66e77a3f 100644
--- a/packages/local-file-shell/src/contentSearch/impl/unix.ts
+++ b/packages/local-file-shell/src/contentSearch/impl/unix.ts
@@ -205,7 +205,12 @@ export abstract class UnixContentSearch extends BaseContentSearch {
matches = lines;
const hasContext = params['-A'] || params['-B'] || params['-C'];
if (hasContext) {
- totalMatches = await this.getActualMatchCount(tool, params);
+ totalMatches = await this.getActualMatchCount(
+ tool,
+ params,
+ searchRoot,
+ this.searchTarget(searchPath, searchRoot),
+ );
} else {
totalMatches = lines.length;
}
@@ -267,19 +272,32 @@ export abstract class UnixContentSearch extends BaseContentSearch {
return searchPath === searchRoot ? '.' : `./${path.basename(searchPath)}`;
}
+ /**
+ * `searchRoot`/`target` must be the same pair the main search ran with: a
+ * file-valued `scope` is legal, but execa's `cwd` must be a directory, so
+ * running the count from the raw scope used to throw and silently return 0 β€”
+ * the summary then said "Found 0 matches" above output that plainly contained
+ * the match lines.
+ */
protected async getActualMatchCount(
tool: 'ag' | 'grep' | 'rg',
params: GrepContentParams,
+ searchRoot: string,
+ target: string,
): Promise<number> {
const countParams = { ...params, '-A': undefined, '-B': undefined, '-C': undefined };
- const args = this.buildGrepArgs(tool, {
- ...countParams,
- output_mode: 'count',
- } as GrepContentParams);
+ const args = this.buildGrepArgs(
+ tool,
+ {
+ ...countParams,
+ output_mode: 'count',
+ } as GrepContentParams,
+ target,
+ );
try {
const { stdout } = await execa(tool, args, {
- cwd: this.resolveSearchPath(params),
+ cwd: searchRoot,
reject: false,
stdin: 'ignore',
});