@ff-labs/pi-fff 0.6.5-nightly.bca71ef → 0.6.5-nightly.bcd3c76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +2 -1
  2. package/src/index.ts +488 -269
  3. package/src/query.ts +87 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ff-labs/pi-fff",
3
3
  "public": true,
4
- "version": "0.6.5-nightly.bca71ef",
4
+ "version": "0.6.5-nightly.bcd3c76",
5
5
  "description": "pi extension: FFF-powered fuzzy file and content search",
6
6
  "type": "module",
7
7
  "license": "MIT",
@@ -36,6 +36,7 @@
36
36
  "access": "public"
37
37
  },
38
38
  "scripts": {
39
+ "test": "bun test test/",
39
40
  "typecheck": "tsc --noEmit"
40
41
  },
41
42
  "dependencies": {
package/src/index.ts CHANGED
@@ -6,12 +6,7 @@
6
6
  */
7
7
 
8
8
  import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
9
- import {
10
- CustomEditor,
11
- truncateHead,
12
- DEFAULT_MAX_BYTES,
13
- formatSize,
14
- } from "@mariozechner/pi-coding-agent";
9
+ import { CustomEditor } from "@mariozechner/pi-coding-agent";
15
10
  import {
16
11
  Text,
17
12
  type AutocompleteItem,
@@ -26,13 +21,14 @@ import type {
26
21
  SearchResult,
27
22
  MixedItem,
28
23
  } from "@ff-labs/fff-node";
24
+ import { buildQuery } from "./query";
29
25
 
30
26
  // ---------------------------------------------------------------------------
31
27
  // Constants
32
28
  // ---------------------------------------------------------------------------
33
29
 
34
- const DEFAULT_GREP_LIMIT = 100;
35
- const DEFAULT_FIND_LIMIT = 200;
30
+ const DEFAULT_GREP_LIMIT = 20;
31
+ const DEFAULT_FIND_LIMIT = 30;
36
32
  const GREP_MAX_LINE_LENGTH = 500;
37
33
  const MENTION_MAX_RESULTS = 20;
38
34
 
@@ -82,6 +78,33 @@ function getCursor(id: string): GrepCursor | undefined {
82
78
  return cursorCache.get(id);
83
79
  }
84
80
 
81
+ // Find pagination uses a page-index cursor: native `fileSearch` takes
82
+ // pageIndex/pageSize, so the cursor is just the next page index paired with
83
+ // the query+limit that produced it. Stored tokens are opaque IDs to the agent.
84
+ interface FindCursor {
85
+ query: string;
86
+ pattern: string;
87
+ pageSize: number;
88
+ nextPageIndex: number;
89
+ }
90
+
91
+ const findCursorCache = new Map<string, FindCursor>();
92
+ let findCursorCounter = 0;
93
+
94
+ function storeFindCursor(cursor: FindCursor): string {
95
+ const id = `${++findCursorCounter}`;
96
+ findCursorCache.set(id, cursor);
97
+ if (findCursorCache.size > 200) {
98
+ const first = findCursorCache.keys().next().value;
99
+ if (first) findCursorCache.delete(first);
100
+ }
101
+ return id;
102
+ }
103
+
104
+ function getFindCursor(id: string): FindCursor | undefined {
105
+ return findCursorCache.get(id);
106
+ }
107
+
85
108
  // ---------------------------------------------------------------------------
86
109
  // Output formatting helpers
87
110
  // ---------------------------------------------------------------------------
@@ -91,44 +114,122 @@ function truncateLine(line: string, max = GREP_MAX_LINE_LENGTH): string {
91
114
  return trimmed.length <= max ? trimmed : `${trimmed.slice(0, max)}...`;
92
115
  }
93
116
 
94
- function formatGrepOutput(result: GrepResult, limit: number): string {
95
- const items = result.items.slice(0, limit);
96
- if (items.length === 0) return "No matches found";
117
+ const HOT_FRECENCY = 25;
118
+ const WARM_FRECENCY = 20;
119
+
120
+ // Shared annotation helper for both find-output paths and grep-output file
121
+ // headers. Returns at most ONE tag so output stays scannable. Priority:
122
+ // git-dirty (most actionable — file is changing right now) beats frecency
123
+ // (historically often-touched). Keeping one function ensures the two tools
124
+ // never drift in how they surface git/frecency signal.
125
+ export function fffFileAnnotation(item: {
126
+ gitStatus?: string;
127
+ totalFrecencyScore?: number;
128
+ accessFrecencyScore?: number;
129
+ }): string {
130
+ const git = item.gitStatus;
131
+ if (git && git !== "clean" && git !== "unknown" && git !== "") {
132
+ return ` [${git} in git]`;
133
+ }
134
+
135
+ const frecency = item.totalFrecencyScore ?? item.accessFrecencyScore ?? 0;
136
+ if (frecency >= HOT_FRECENCY) return " [VERY often touched file]";
137
+ if (frecency >= WARM_FRECENCY) return " [often touched file]";
97
138
 
139
+ return "";
140
+ }
141
+
142
+ // fff-core native definition classifier (byte-level scanner in Rust) is enabled
143
+ // via GrepOptions.classifyDefinitions. Each GrepMatch carries isDefinition for
144
+ // downstream consumers; pi-fff does NOT use it to re-sort.
145
+ //
146
+ // Ordering policy: NO CUSTOM SORTING. The engine already returns items in
147
+ // frecency order (most-accessed files first). pi-fff only groups consecutive
148
+ // matches into per-file blocks and preserves whatever order the engine
149
+ // provided — inside a file we keep matches in source-line order because the
150
+ // engine emits them that way.
151
+
152
+ function formatGrepOutput(result: GrepResult): string {
153
+ if (result.items.length === 0) return "No matches found";
154
+
155
+ // Build file-grouped output in the order files first appear in the result.
156
+ // This preserves native frecency ordering across files without re-sorting.
98
157
  const lines: string[] = [];
99
158
  let currentFile = "";
159
+ let shown = 0;
100
160
 
101
- for (const match of items) {
161
+ for (const match of result.items) {
102
162
  if (match.relativePath !== currentFile) {
103
- currentFile = match.relativePath;
104
163
  if (lines.length > 0) lines.push("");
164
+ currentFile = match.relativePath;
165
+ lines.push(`${currentFile}${fffFileAnnotation(match)}`);
105
166
  }
106
167
 
107
168
  match.contextBefore?.forEach((line: string, i: number) => {
108
- lines.push(
109
- `${match.relativePath}-${match.lineNumber - match.contextBefore!.length + i}- ${truncateLine(line)}`,
110
- );
169
+ const lineNum = match.lineNumber - match.contextBefore!.length + i;
170
+ lines.push(` ${lineNum}- ${truncateLine(line)}`);
111
171
  });
112
172
 
113
- lines.push(
114
- `${match.relativePath}:${match.lineNumber}: ${truncateLine(match.lineContent)}`,
115
- );
173
+ lines.push(` ${match.lineNumber}: ${truncateLine(match.lineContent)}`);
174
+ shown++;
116
175
 
117
176
  match.contextAfter?.forEach((line: string, i: number) => {
118
- lines.push(
119
- `${match.relativePath}-${match.lineNumber + 1 + i}- ${truncateLine(line)}`,
120
- );
177
+ const lineNum = match.lineNumber + 1 + i;
178
+ lines.push(` ${lineNum}- ${truncateLine(line)}`);
121
179
  });
122
180
  }
123
181
 
124
182
  return lines.join("\n");
125
183
  }
126
184
 
127
- function formatFindOutput(result: SearchResult, limit: number): string {
128
- const items = result.items.slice(0, limit);
129
- return items.length === 0
130
- ? "No files found matching pattern"
131
- : items.map((i: { relativePath: string }) => i.relativePath).join("\n");
185
+ // Weak-match threshold is derived from the query length, matching the
186
+ // scoring formula in crates/fff-core/src/score.rs: a perfect match scores
187
+ // `len * 16`, so we treat anything below 50% of that as scattered fuzzy noise.
188
+ // When the top score is weak, trim output to a small sample instead of dumping
189
+ // the full limit worth of noise into the agent's context.
190
+ const FIND_WEAK_SAMPLE_SIZE = 5;
191
+
192
+ function weakScoreThreshold(pattern: string): number {
193
+ const perfect = pattern.length * 12;
194
+ return Math.floor((perfect * 50) / 100);
195
+ }
196
+
197
+ interface FormattedFind {
198
+ output: string;
199
+ weak: boolean;
200
+ shownCount: number;
201
+ }
202
+
203
+ function formatFindOutput(
204
+ result: SearchResult,
205
+ limit: number,
206
+ pattern: string,
207
+ ): FormattedFind {
208
+ if (result.items.length === 0) {
209
+ return {
210
+ output: "No files found matching pattern",
211
+ weak: false,
212
+ shownCount: 0,
213
+ };
214
+ }
215
+
216
+ // NO CUSTOM SORTING — trust native frecency order from the engine.
217
+ const reordered = result.items.map((item) => ({ item }));
218
+
219
+ // Peek at the top native score to decide whether results are scattered
220
+ // fuzzy noise (query length-scaled threshold from score.rs).
221
+ const topScore = result.scores[0]?.total ?? 0;
222
+ const weak = topScore < weakScoreThreshold(pattern);
223
+ const effective = weak ? Math.min(FIND_WEAK_SAMPLE_SIZE, limit) : limit;
224
+ const shown = reordered.slice(0, effective);
225
+
226
+ return {
227
+ output: shown
228
+ .map((p) => `${p.item.relativePath}${fffFileAnnotation(p.item)}`)
229
+ .join("\n"),
230
+ weak,
231
+ shownCount: shown.length,
232
+ };
132
233
  }
133
234
 
134
235
  // ---------------------------------------------------------------------------
@@ -155,7 +256,9 @@ function createFffMentionProvider(
155
256
 
156
257
  const query = prefix.startsWith('@"') ? prefix.slice(2) : prefix.slice(1);
157
258
  const items = await getItems(query, options.signal);
158
- return options.signal.aborted || items.length === 0 ? null : { items, prefix };
259
+ return options.signal.aborted || items.length === 0
260
+ ? null
261
+ : { items, prefix };
159
262
  },
160
263
  applyCompletion(_lines, cursorLine, cursorCol, item, prefix) {
161
264
  const currentLine = _lines[cursorLine] || "";
@@ -164,7 +267,11 @@ function createFffMentionProvider(
164
267
  const newLine = before + item.value + after;
165
268
  const newCursorCol = cursorCol - prefix.length + item.value.length;
166
269
  return {
167
- lines: [..._lines.slice(0, cursorLine), newLine, ..._lines.slice(cursorLine + 1)],
270
+ lines: [
271
+ ..._lines.slice(0, cursorLine),
272
+ newLine,
273
+ ..._lines.slice(cursorLine + 1),
274
+ ],
168
275
  cursorLine,
169
276
  cursorCol: newCursorCol,
170
277
  };
@@ -172,68 +279,11 @@ function createFffMentionProvider(
172
279
  };
173
280
  }
174
281
 
175
- // Simple editor wrapper that injects FFF @-mention autocomplete alongside base provider
176
- class FffEditor extends CustomEditor {
177
- private baseProvider: AutocompleteProvider | undefined;
178
- private getMentionItems: (
179
- query: string,
180
- signal: AbortSignal,
181
- ) => Promise<AutocompleteItem[]>;
182
-
183
- constructor(
184
- tui: any,
185
- theme: any,
186
- keybindings: any,
187
- getMentionItems: (query: string, signal: AbortSignal) => Promise<AutocompleteItem[]>,
188
- ) {
189
- super(tui, theme, keybindings);
190
- this.getMentionItems = getMentionItems;
191
- }
192
-
193
- override setAutocompleteProvider(provider: AutocompleteProvider): void {
194
- this.baseProvider = provider;
195
- // Create composite provider that handles @-mentions and falls back to base
196
- const mentionProvider = createFffMentionProvider(this.getMentionItems);
197
- const compositeProvider: AutocompleteProvider = {
198
- getSuggestions: async (lines, cursorLine, cursorCol, options) => {
199
- // Try @-mention first
200
- const mentionResult = await mentionProvider.getSuggestions(
201
- lines,
202
- cursorLine,
203
- cursorCol,
204
- options,
205
- );
206
- if (mentionResult) return mentionResult;
207
- // Fall back to base provider
208
- return (
209
- this.baseProvider?.getSuggestions(lines, cursorLine, cursorCol, options) ?? null
210
- );
211
- },
212
- applyCompletion: (lines, cursorLine, cursorCol, item, prefix) => {
213
- // Let mention provider handle @ completions, base provider for others
214
- if (prefix?.startsWith("@")) {
215
- return mentionProvider.applyCompletion!(
216
- lines,
217
- cursorLine,
218
- cursorCol,
219
- item,
220
- prefix,
221
- );
222
- }
223
- return (
224
- this.baseProvider?.applyCompletion?.(
225
- lines,
226
- cursorLine,
227
- cursorCol,
228
- item,
229
- prefix,
230
- ) ?? { lines, cursorLine, cursorCol }
231
- );
232
- },
233
- };
234
- super.setAutocompleteProvider(compositeProvider);
235
- }
236
- }
282
+ // FffEditor is defined inside fffExtension() so it can capture `getMentionItems`
283
+ // via closure rather than via a 4th constructor parameter. This makes the class
284
+ // safe to subclass via `new SubClass(tui, theme, keybindings)` -- the pattern
285
+ // pi-vim and pi-image-attachments use to compose editors. See:
286
+ // https://github.com/badlogic/pi-mono/issues/3935
237
287
 
238
288
  // ---------------------------------------------------------------------------
239
289
  // Extension
@@ -289,7 +339,8 @@ export default function fffExtension(pi: ExtensionAPI) {
289
339
  aiMode: true,
290
340
  });
291
341
 
292
- if (!result.ok) throw new Error(`Failed to create FFF file finder: ${result.error}`);
342
+ if (!result.ok)
343
+ throw new Error(`Failed to create FFF file finder: ${result.error}`);
293
344
 
294
345
  finder = result.value;
295
346
  finderCwd = cwd;
@@ -316,20 +367,80 @@ export default function fffExtension(pi: ExtensionAPI) {
316
367
  const result = f.mixedSearch(query, { pageSize: MENTION_MAX_RESULTS });
317
368
  if (!result.ok) return [];
318
369
 
319
- return result.value.items.slice(0, MENTION_MAX_RESULTS).map((mixed: MixedItem) => {
320
- if (mixed.type === "directory") {
370
+ return result.value.items
371
+ .slice(0, MENTION_MAX_RESULTS)
372
+ .map((mixed: MixedItem) => {
373
+ if (mixed.type === "directory") {
374
+ return {
375
+ value: buildAtCompletionValue(mixed.item.relativePath),
376
+ label: mixed.item.dirName,
377
+ description: mixed.item.relativePath,
378
+ };
379
+ }
321
380
  return {
322
381
  value: buildAtCompletionValue(mixed.item.relativePath),
323
- label: mixed.item.dirName,
382
+ label: mixed.item.fileName,
324
383
  description: mixed.item.relativePath,
325
384
  };
326
- }
327
- return {
328
- value: buildAtCompletionValue(mixed.item.relativePath),
329
- label: mixed.item.fileName,
330
- description: mixed.item.relativePath,
385
+ });
386
+ }
387
+
388
+ // Editor wrapper that injects FFF @-mention autocomplete alongside base provider.
389
+ // Defined inside fffExtension() so the class methods capture `getMentionItems`
390
+ // via closure. Subclasses constructed as `new Sub(tui, theme, keybindings)` by
391
+ // composability wrappers (pi-vim, pi-image-attachments) still get a working
392
+ // mention provider because the closure binding is preserved across subclassing.
393
+ class FffEditor extends CustomEditor {
394
+ private baseProvider: AutocompleteProvider | undefined;
395
+
396
+ override setAutocompleteProvider(provider: AutocompleteProvider): void {
397
+ this.baseProvider = provider;
398
+ // Create composite provider that handles @-mentions and falls back to base
399
+ const mentionProvider = createFffMentionProvider(getMentionItems);
400
+ const compositeProvider: AutocompleteProvider = {
401
+ getSuggestions: async (lines, cursorLine, cursorCol, options) => {
402
+ // Try @-mention first
403
+ const mentionResult = await mentionProvider.getSuggestions(
404
+ lines,
405
+ cursorLine,
406
+ cursorCol,
407
+ options,
408
+ );
409
+ if (mentionResult) return mentionResult;
410
+ // Fall back to base provider
411
+ return (
412
+ this.baseProvider?.getSuggestions(
413
+ lines,
414
+ cursorLine,
415
+ cursorCol,
416
+ options,
417
+ ) ?? null
418
+ );
419
+ },
420
+ applyCompletion: (lines, cursorLine, cursorCol, item, prefix) => {
421
+ // Let mention provider handle @ completions, base provider for others
422
+ if (prefix?.startsWith("@")) {
423
+ return mentionProvider.applyCompletion!(
424
+ lines,
425
+ cursorLine,
426
+ cursorCol,
427
+ item,
428
+ prefix,
429
+ );
430
+ }
431
+ return (
432
+ this.baseProvider?.applyCompletion?.(
433
+ lines,
434
+ cursorLine,
435
+ cursorCol,
436
+ item,
437
+ prefix,
438
+ ) ?? { lines, cursorLine, cursorCol }
439
+ );
440
+ },
331
441
  };
332
- });
442
+ super.setAutocompleteProvider(compositeProvider);
443
+ }
333
444
  }
334
445
 
335
446
  function applyEditorMode(ctx: {
@@ -344,7 +455,7 @@ export default function fffExtension(pi: ExtensionAPI) {
344
455
  } else {
345
456
  ctx.ui.setEditorComponent(
346
457
  (tui: any, theme: any, keybindings: any) =>
347
- new FffEditor(tui, theme, keybindings, getMentionItems),
458
+ new FffEditor(tui, theme, keybindings),
348
459
  );
349
460
  }
350
461
  }
@@ -357,12 +468,14 @@ export default function fffExtension(pi: ExtensionAPI) {
357
468
  });
358
469
 
359
470
  pi.registerFlag("fff-frecency-db", {
360
- description: "Path to the frecency database (overrides FFF_FRECENCY_DB env)",
471
+ description:
472
+ "Path to the frecency database (overrides FFF_FRECENCY_DB env)",
361
473
  type: "string",
362
474
  });
363
475
 
364
476
  pi.registerFlag("fff-history-db", {
365
- description: "Path to the query history database (overrides FFF_HISTORY_DB env)",
477
+ description:
478
+ "Path to the query history database (overrides FFF_HISTORY_DB env)",
366
479
  type: "string",
367
480
  });
368
481
 
@@ -392,15 +505,20 @@ export default function fffExtension(pi: ExtensionAPI) {
392
505
  context: any,
393
506
  maxLines = 15,
394
507
  ) => {
395
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
396
- const output = result.content?.find((c) => c.type === "text")?.text?.trim() ?? "";
508
+ const text =
509
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
510
+ const output =
511
+ result.content?.find((c) => c.type === "text")?.text?.trim() ?? "";
397
512
  if (!output) {
398
513
  text.setText(theme.fg("muted", "No output"));
399
514
  return text;
400
515
  }
401
516
 
402
517
  const lines = output.split("\n");
403
- const displayLines = lines.slice(0, options.expanded ? lines.length : maxLines);
518
+ const displayLines = lines.slice(
519
+ 0,
520
+ options.expanded ? lines.length : maxLines,
521
+ );
404
522
  let content = `\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
405
523
  if (lines.length > displayLines.length) {
406
524
  content += theme.fg(
@@ -415,44 +533,50 @@ export default function fffExtension(pi: ExtensionAPI) {
415
533
  // --- grep tool ---
416
534
 
417
535
  const grepSchema = Type.Object({
418
- pattern: Type.String({ description: "Search pattern (plain text or regex)" }),
536
+ pattern: Type.String({
537
+ description: "Search pattern (literal text or regex)",
538
+ }),
419
539
  path: Type.Optional(
420
540
  Type.String({
421
541
  description:
422
- "Directory or file constraint, e.g. 'src/' or '*.ts' (default: project root)",
542
+ "Repo-relative path constraint. Directory prefix (src/ or src/foo/), bare filename with extension (main.rs), or glob (*.ts, src/**/*.cc, {src,lib}/**). Applied to the full repo-relative path.",
423
543
  }),
424
544
  ),
425
- literal: Type.Optional(
545
+ exclude: Type.Optional(
546
+ Type.Union([Type.String(), Type.Array(Type.String())], {
547
+ description:
548
+ "Exclude paths (comma/space-separated or array). Same syntax as path: directory prefix ('test/'), filename with extension ('config.json'), or glob ('*.min.js', '**/*.{rs,go}'). A leading '!' is optional and ignored — both 'test/' and '!test/' work. Example: 'test/,*.min.js,!vendor/'.",
549
+ }),
550
+ ),
551
+ caseSensitive: Type.Optional(
426
552
  Type.Boolean({
427
- description: "Treat pattern as literal string instead of regex (default: true)",
553
+ description:
554
+ "Force case-sensitive matching. Default uses smart-case (case-insensitive when pattern is all lowercase).",
428
555
  }),
429
556
  ),
430
557
  context: Type.Optional(
431
- Type.Number({
432
- description: "Number of lines to show before and after each match (default: 0)",
433
- }),
558
+ Type.Number({ description: "Context lines before+after each match" }),
434
559
  ),
435
560
  limit: Type.Optional(
436
561
  Type.Number({
437
- description: `Maximum number of matches to return (default: ${DEFAULT_GREP_LIMIT})`,
562
+ description: `Max matches (default ${DEFAULT_GREP_LIMIT})`,
438
563
  }),
439
564
  ),
440
565
  cursor: Type.Optional(
441
- Type.String({ description: "Cursor from previous result for pagination" }),
566
+ Type.String({ description: "Pagination cursor from previous result" }),
442
567
  ),
443
568
  });
444
569
 
445
570
  pi.registerTool({
446
571
  name: toolNames.grep,
447
572
  label: toolNames.grep,
448
- description: `Search file contents for a pattern using FFF (fast, frecency-ranked, git-aware). Returns matching lines with file paths and line numbers. Respects .gitignore. Supports plain text, regex, and fuzzy search modes. Smart case by default. Output truncated to ${DEFAULT_GREP_LIMIT} matches or ${DEFAULT_MAX_BYTES / 1024}KB.`,
449
- promptSnippet:
450
- "Search file contents for patterns (FFF: frecency-ranked, git-aware, respects .gitignore)",
573
+ description: `Grep file contents. Smart-case, auto-detects regex vs literal, git-aware. Results are ranked by frecency (most-accessed files first); matches within a file stay in source order. Default limit ${DEFAULT_GREP_LIMIT}.`,
574
+ promptSnippet: "Grep contents",
451
575
  promptGuidelines: [
452
- "Search for bare identifiers (e.g. 'InProgressQuote'), not code syntax or multi-token regex.",
453
- "Plain text search is faster and more reliable than regex. Prefer it.",
454
- "After 2 grep calls, read the top result file instead of grepping more.",
455
- "Use the path parameter for file/directory constraints: '*.ts', 'src/'.",
576
+ "Prefer bare identifiers as patterns. Literal queries are most efficient.",
577
+ "Use path for include ('src/', '*.ts') and exclude for noise ('test/,*.min.js').",
578
+ "caseSensitive: true when you need exact case (smart-case otherwise).",
579
+ "After 1-2 greps, read the top match instead of more greps.",
456
580
  ],
457
581
  parameters: grepSchema,
458
582
 
@@ -461,53 +585,109 @@ export default function fffExtension(pi: ExtensionAPI) {
461
585
 
462
586
  const f = await ensureFinder(activeCwd);
463
587
  const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_GREP_LIMIT);
464
- const query = params.path ? `${params.path} ${params.pattern}` : params.pattern;
465
- const mode: GrepMode = params.literal === false ? "regex" : "plain";
588
+ const query = buildQuery(params.path, params.pattern, params.exclude, activeCwd);
589
+ // Auto-detect: regex if the pattern has regex metacharacters AND parses
590
+ // as a valid regex, otherwise plain literal. The fuzzy fallback below
591
+ // only kicks in for plain mode — regex queries are intentional.
592
+ const hasRegexSyntax =
593
+ params.pattern !==
594
+ params.pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
595
+ let mode: GrepMode = hasRegexSyntax ? "regex" : "plain";
596
+ if (mode === "regex") {
597
+ try {
598
+ new RegExp(params.pattern);
599
+ } catch {
600
+ mode = "plain";
601
+ }
602
+ }
603
+
604
+ // Guard: the agent keeps calling grep with '.*' or similar wildcard-only regex
605
+ // to try to read a whole file. That's not what grep is for — return a terse error
606
+ // steering them to a real pattern, preventing dozens of wasted retries.
607
+ const p = params.pattern.trim();
608
+ const isWildcardOnly =
609
+ hasRegexSyntax &&
610
+ /^(?:[.^$]*(?:[.][*+?]|\*|\+)[.^$]*|[.^$\s]*|\.\*\??|\.\*[+?]?|\.\+\??|\.|\*|\?)$/.test(
611
+ p,
612
+ );
613
+
614
+ if (isWildcardOnly) {
615
+ return {
616
+ content: [
617
+ {
618
+ type: "text",
619
+ text: `Pattern '${params.pattern}' matches everything — grep needs a concrete substring or identifier. Example: \`pattern: 'MyClass'\` or \`pattern: 'export function'\`.`,
620
+ },
621
+ ],
622
+ details: { totalMatched: 0, totalFiles: 0 },
623
+ };
624
+ }
625
+
626
+ // caseSensitive override flips smartCase off; omitting it keeps smart-case
627
+ // (case-insensitive when pattern is all lowercase).
628
+ const smartCase = params.caseSensitive !== true;
466
629
 
467
630
  const grepResult = f.grep(query, {
468
631
  mode,
469
- smartCase: true,
632
+ smartCase,
470
633
  maxMatchesPerFile: Math.min(effectiveLimit, 50),
471
634
  cursor: (params.cursor ? getCursor(params.cursor) : null) ?? null,
472
635
  beforeContext: params.context ?? 0,
473
636
  afterContext: params.context ?? 0,
637
+ classifyDefinitions: true,
474
638
  });
475
639
 
476
640
  if (!grepResult.ok) throw new Error(grepResult.error);
477
641
 
478
- const result = grepResult.value;
479
- let output = formatGrepOutput(result, effectiveLimit);
480
- const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
481
- output = truncation.content;
642
+ let result = grepResult.value;
643
+ let fuzzyNotice: string | null = null;
644
+
645
+ // automatic fuzzy fallback allows to broad the queries and find different cases
646
+ if (result.items.length === 0 && !params.cursor && mode !== "regex") {
647
+ const fuzzy = f.grep(params.pattern, {
648
+ mode: "fuzzy",
649
+ smartCase,
650
+ maxMatchesPerFile: Math.min(effectiveLimit, 50),
651
+ cursor: null,
652
+ beforeContext: 0,
653
+ afterContext: 0,
654
+ classifyDefinitions: true,
655
+ });
656
+
657
+ if (fuzzy.ok && fuzzy.value.items.length > 0) {
658
+ fuzzyNotice = `0 exact matches. Maybe you meant this?`;
659
+ result = fuzzy.value;
660
+ }
661
+ }
482
662
 
663
+ let output = formatGrepOutput(result);
483
664
  const notices: string[] = [];
484
- if (result.items.length >= effectiveLimit)
665
+ if (result.regexFallbackError) {
485
666
  notices.push(
486
- `${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more`,
667
+ `Invalid regex: ${result.regexFallbackError}, used literal match`,
487
668
  );
488
- if (truncation.truncated)
489
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
490
- if (result.regexFallbackError)
491
- notices.push(`Regex failed: ${result.regexFallbackError}, used literal match`);
492
- if (result.nextCursor)
669
+ }
670
+ if (result.nextCursor) {
493
671
  notices.push(
494
- `More results available. Use cursor="${storeCursor(result.nextCursor)}" to continue`,
672
+ `Continue with cursor="${storeCursor(result.nextCursor)}"`,
495
673
  );
674
+ }
496
675
 
497
676
  if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
677
+ if (fuzzyNotice) output = `[${fuzzyNotice}]\n${output}`;
498
678
 
499
679
  return {
500
680
  content: [{ type: "text", text: output }],
501
681
  details: {
502
682
  totalMatched: result.totalMatched,
503
683
  totalFiles: result.totalFiles,
504
- truncated: truncation.truncated,
505
684
  },
506
685
  };
507
686
  },
508
687
 
509
688
  renderCall(args, theme, context) {
510
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
689
+ const text =
690
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
511
691
  const pattern = args?.pattern ?? "";
512
692
  const path = args?.path ?? ".";
513
693
  let content =
@@ -532,28 +712,42 @@ export default function fffExtension(pi: ExtensionAPI) {
532
712
  const findSchema = Type.Object({
533
713
  pattern: Type.String({
534
714
  description:
535
- "Fuzzy search query for file names. Supports path prefixes ('src/') and globs ('*.ts').",
715
+ "Fuzzy filename search and glob search. Frecency-ranked, git-aware. Multi-word = narrower (AND) not bound to order, use for multi word related concept search. Prefer this over ls/find/bash as the first exploration step whenever the user names a concept, feature, or symbol — it surfaces the relevant files in one call. Only use ls/read on a directory when you specifically need the alphabetical layout of an unknown repo, or when a concept search returned nothing.",
536
716
  }),
537
717
  path: Type.Optional(
538
- Type.String({ description: "Directory to search in (default: project root)" }),
718
+ Type.String({
719
+ description:
720
+ "Repo-relative path constraint. Directory prefix (src/ or src/foo/), bare filename with extension (main.rs), or glob (*.ts, src/**/*.cc, {src,lib}/**). Applied to the full repo-relative path.",
721
+ }),
722
+ ),
723
+ exclude: Type.Optional(
724
+ Type.Union([Type.String(), Type.Array(Type.String())], {
725
+ description:
726
+ "Exclude paths (comma/space-separated or array). Same syntax as path: directory prefix ('test/'), filename with extension ('config.json'), or glob ('*.min.js', '**/*.{rs,go}'). A leading '!' is optional and ignored — both 'test/' and '!test/' work. Example: 'test/,*.min.js,!vendor/'.",
727
+ }),
539
728
  ),
540
729
  limit: Type.Optional(
541
730
  Type.Number({
542
- description: `Maximum number of results (default: ${DEFAULT_FIND_LIMIT})`,
731
+ description: `Max results per page (default ${DEFAULT_FIND_LIMIT})`,
543
732
  }),
544
733
  ),
734
+ cursor: Type.Optional(
735
+ Type.String({ description: "Pagination cursor from previous result" }),
736
+ ),
545
737
  });
546
738
 
547
739
  pi.registerTool({
548
740
  name: toolNames.find,
549
741
  label: toolNames.find,
550
- description: `Fuzzy file search by name using FFF (fast, frecency-ranked, git-aware). Returns matching file paths relative to project root. Respects .gitignore. Supports fuzzy matching, path prefixes ('src/'), and glob constraints ('*.ts', '**/*.spec.ts'). Output truncated to ${DEFAULT_FIND_LIMIT} results or ${DEFAULT_MAX_BYTES / 1024}KB.`,
551
- promptSnippet:
552
- "Find files by name (FFF: fuzzy, frecency-ranked, git-aware, respects .gitignore)",
742
+ description: `Fuzzy path search and glob search. Matches against the whole repo-relative path, not just the filename. Frecency-ranked, git-aware. Multi-word = narrower (AND). Default limit ${DEFAULT_FIND_LIMIT}.`,
743
+ promptSnippet: "Find files by path or glob",
553
744
  promptGuidelines: [
554
- "Keep queries short -- prefer 1-2 terms max.",
555
- "Multiple words narrow results (waterfall), they are not OR.",
556
- "Use this to find files by name. Use grep to search file contents.",
745
+ "Matches the WHOLE path, not just the filename — `profile` hits `chrome/browser/profiles/x.cc` too.",
746
+ "Keep queries to 1-2 terms; extra words narrow.",
747
+ "Use for paths, not content. Use grep for content.",
748
+ "For exact path matches use a glob in `path` — e.g. path: '**/profile.h' for exact filename, or path: 'src/**/profile.h' scoped to a subtree. Bare patterns are fuzzy.",
749
+ "To list everything inside a directory, pass path: 'dir/**' with an empty or wildcard pattern instead of using pattern alone.",
750
+ "Use exclude: 'test/,*.min.js' to cut noise in large repos.",
557
751
  ],
558
752
  parameters: findSchema,
559
753
 
@@ -561,43 +755,71 @@ export default function fffExtension(pi: ExtensionAPI) {
561
755
  if (signal?.aborted) throw new Error("Operation aborted");
562
756
 
563
757
  const f = await ensureFinder(activeCwd);
564
- const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_FIND_LIMIT);
565
- const query = params.path ? `${params.path} ${params.pattern}` : params.pattern;
566
758
 
567
- const searchResult = f.fileSearch(query, { pageSize: effectiveLimit });
759
+ // Resume from a prior cursor if supplied — cursor owns query+pageSize so
760
+ // the agent can't accidentally mix patterns across pages.
761
+ const resumed = params.cursor ? getFindCursor(params.cursor) : undefined;
762
+ const effectiveLimit = resumed
763
+ ? resumed.pageSize
764
+ : Math.max(1, params.limit ?? DEFAULT_FIND_LIMIT);
765
+ const query = resumed
766
+ ? resumed.query
767
+ : buildQuery(params.path, params.pattern, params.exclude, activeCwd);
768
+ const pattern = resumed ? resumed.pattern : params.pattern;
769
+ const pageIndex = resumed?.nextPageIndex ?? 0;
770
+
771
+ const searchResult = f.fileSearch(query, {
772
+ pageIndex,
773
+ pageSize: effectiveLimit,
774
+ });
568
775
  if (!searchResult.ok) throw new Error(searchResult.error);
569
776
 
570
777
  const result = searchResult.value;
571
- let output = formatFindOutput(result, effectiveLimit);
572
- const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
573
- output = truncation.content;
778
+ const formatted = formatFindOutput(result, effectiveLimit, pattern);
779
+ let output = formatted.output;
780
+
781
+ // Infer hasMore: native fileSearch fills pageSize when more results
782
+ // exist, so if we got a full page AND totalMatched exceeds what we've
783
+ // shown so far there's another page to fetch.
784
+ const shownSoFar = pageIndex * effectiveLimit + result.items.length;
785
+ const hasMore =
786
+ result.items.length >= effectiveLimit &&
787
+ result.totalMatched > shownSoFar;
574
788
 
575
789
  const notices: string[] = [];
576
- if (result.items.length >= effectiveLimit)
790
+ if (formatted.weak && formatted.shownCount > 0)
577
791
  notices.push(
578
- `${effectiveLimit} results limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,
792
+ `Query "${pattern}" produced only weak scattered fuzzy matches. Output capped at ${formatted.shownCount}/${result.totalMatched}.`,
579
793
  );
580
- if (truncation.truncated)
581
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
582
- if (result.totalMatched > result.items.length)
794
+
795
+ if (!formatted.weak && hasMore) {
796
+ const remaining = result.totalMatched - shownSoFar;
797
+ const cursorId = storeFindCursor({
798
+ query,
799
+ pattern,
800
+ pageSize: effectiveLimit,
801
+ nextPageIndex: pageIndex + 1,
802
+ });
583
803
  notices.push(
584
- `${result.totalMatched} total matches (${result.totalFiles} indexed files)`,
804
+ `${remaining} more match${remaining === 1 ? "" : "es"} available. cursor="${cursorId}" to continue`,
585
805
  );
806
+ }
586
807
 
587
808
  if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
588
-
589
809
  return {
590
810
  content: [{ type: "text", text: output }],
591
811
  details: {
592
812
  totalMatched: result.totalMatched,
593
813
  totalFiles: result.totalFiles,
594
- truncated: truncation.truncated,
814
+ pageIndex,
815
+ hasMore,
595
816
  },
596
817
  };
597
818
  },
598
819
 
599
820
  renderCall(args, theme, context) {
600
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
821
+ const text =
822
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
601
823
  const pattern = args?.pattern ?? "";
602
824
  const path = args?.path ?? ".";
603
825
  let content =
@@ -607,6 +829,7 @@ export default function fffExtension(pi: ExtensionAPI) {
607
829
  theme.fg("toolOutput", ` in ${path}`);
608
830
  if (args?.limit !== undefined)
609
831
  content += theme.fg("toolOutput", ` (limit ${args.limit})`);
832
+ if (args?.cursor) content += theme.fg("muted", ` (page)`);
610
833
  text.setText(content);
611
834
  return text;
612
835
  },
@@ -617,121 +840,111 @@ export default function fffExtension(pi: ExtensionAPI) {
617
840
  });
618
841
 
619
842
  // --- multi_grep tool ---
843
+ // My latest tests are showing that the multi grep tool is only harmful, trying to get rid of it
844
+ const enableMultiGrep = process.env.PI_FFF_MULTIGREP === "1";
620
845
 
621
- const multiGrepSchema = Type.Object({
622
- patterns: Type.Array(Type.String(), {
623
- description:
624
- "Patterns to search for (OR logic -- matches lines containing ANY pattern). Include all naming conventions: snake_case, PascalCase, camelCase.",
625
- }),
626
- constraints: Type.Optional(
627
- Type.String({
846
+ if (enableMultiGrep) {
847
+ const multiGrepSchema = Type.Object({
848
+ patterns: Type.Array(Type.String(), {
628
849
  description:
629
- "File constraints, e.g. '*.{ts,tsx} !test/' to filter files. Separate from patterns.",
630
- }),
631
- ),
632
- context: Type.Optional(
633
- Type.Number({
634
- description: "Number of context lines before and after each match (default: 0)",
850
+ "Literal patterns (OR). Include snake_case/camelCase/PascalCase variants.",
635
851
  }),
636
- ),
637
- limit: Type.Optional(
638
- Type.Number({
639
- description: `Maximum number of matches to return (default: ${DEFAULT_GREP_LIMIT})`,
640
- }),
641
- ),
642
- cursor: Type.Optional(
643
- Type.String({ description: "Cursor from previous result for pagination" }),
644
- ),
645
- });
646
-
647
- pi.registerTool({
648
- name: toolNames.multiGrep,
649
- label: toolNames.multiGrep,
650
- description:
651
- "Search file contents for lines matching ANY of multiple patterns (OR logic). Uses SIMD-accelerated Aho-Corasick multi-pattern matching. Faster than regex alternation. Patterns are literal text -- never escape special characters. Use the constraints parameter for file filtering ('*.rs', 'src/', '!test/').",
652
- promptSnippet:
653
- "Multi-pattern OR search across file contents (FFF: SIMD-accelerated, frecency-ranked)",
654
- promptGuidelines: [
655
- `Use ${toolNames.multiGrep} when you need to find multiple identifiers at once (OR logic).`,
656
- "Include all naming conventions: snake_case, PascalCase, camelCase variants.",
657
- "Patterns are literal text. Never escape special characters.",
658
- "Use the constraints parameter for file type/path filtering, not inside patterns.",
659
- ],
660
- parameters: multiGrepSchema,
661
-
662
- async execute(_toolCallId, params, signal) {
663
- if (signal?.aborted) throw new Error("Operation aborted");
664
- if (!params.patterns?.length)
665
- throw new Error("patterns array must have at least 1 element");
666
-
667
- const f = await ensureFinder(activeCwd);
668
- const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_GREP_LIMIT);
669
-
670
- const grepResult = f.multiGrep({
671
- patterns: params.patterns,
672
- constraints: params.constraints,
673
- maxMatchesPerFile: Math.min(effectiveLimit, 50),
674
- smartCase: true,
675
- cursor: (params.cursor ? getCursor(params.cursor) : null) ?? null,
676
- beforeContext: params.context ?? 0,
677
- afterContext: params.context ?? 0,
678
- });
679
-
680
- if (!grepResult.ok) throw new Error(grepResult.error);
681
-
682
- const result = grepResult.value;
683
- let output = formatGrepOutput(result, effectiveLimit);
684
- const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
685
- output = truncation.content;
852
+ constraints: Type.Optional(
853
+ Type.String({ description: "File filter, e.g. '*.{ts,tsx} !test/'" }),
854
+ ),
855
+ context: Type.Optional(
856
+ Type.Number({ description: "Context lines before+after" }),
857
+ ),
858
+ limit: Type.Optional(
859
+ Type.Number({
860
+ description: `Max matches (default ${DEFAULT_GREP_LIMIT})`,
861
+ }),
862
+ ),
863
+ cursor: Type.Optional(Type.String({ description: "Pagination cursor" })),
864
+ });
686
865
 
687
- const notices: string[] = [];
688
- if (result.items.length >= effectiveLimit)
689
- notices.push(
690
- `${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more`,
691
- );
692
- if (truncation.truncated)
693
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
694
- if (result.nextCursor)
695
- notices.push(
696
- `More results available. Use cursor="${storeCursor(result.nextCursor)}" to continue`,
697
- );
866
+ pi.registerTool({
867
+ name: toolNames.multiGrep,
868
+ label: toolNames.multiGrep,
869
+ description:
870
+ "Search file contents for ANY of multiple literal patterns (OR, SIMD Aho-Corasick). Faster than regex alternation.",
871
+ promptSnippet: "Multi-pattern OR content search",
872
+ promptGuidelines: [
873
+ "Use when searching for several identifiers at once.",
874
+ "Include all naming-convention variants (snake/camel/Pascal).",
875
+ "Patterns are literal. Use constraints for file filters.",
876
+ ],
877
+ parameters: multiGrepSchema,
878
+
879
+ async execute(_toolCallId, params, signal) {
880
+ if (signal?.aborted) throw new Error("Operation aborted");
881
+ if (!params.patterns?.length)
882
+ throw new Error("patterns array must have at least 1 element");
883
+
884
+ const f = await ensureFinder(activeCwd);
885
+ const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_GREP_LIMIT);
886
+
887
+ const grepResult = f.multiGrep({
888
+ patterns: params.patterns,
889
+ constraints: params.constraints,
890
+ maxMatchesPerFile: Math.min(effectiveLimit, 50),
891
+ smartCase: true,
892
+ cursor: (params.cursor ? getCursor(params.cursor) : null) ?? null,
893
+ beforeContext: params.context ?? 0,
894
+ afterContext: params.context ?? 0,
895
+ });
896
+
897
+ if (!grepResult.ok) throw new Error(grepResult.error);
898
+
899
+ const result = grepResult.value;
900
+ let output = formatGrepOutput(result);
901
+
902
+ const notices: string[] = [];
903
+ if (result.items.length >= effectiveLimit)
904
+ notices.push(`${effectiveLimit}+ matches (refine patterns)`);
905
+ if (result.nextCursor)
906
+ notices.push(
907
+ `More available. cursor="${storeCursor(result.nextCursor)}" to continue`,
908
+ );
698
909
 
699
- if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
910
+ if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
700
911
 
701
- return {
702
- content: [{ type: "text", text: output }],
703
- details: {
704
- totalMatched: result.totalMatched,
705
- totalFiles: result.totalFiles,
706
- truncated: truncation.truncated,
707
- patterns: params.patterns,
708
- },
709
- };
710
- },
912
+ return {
913
+ content: [{ type: "text", text: output }],
914
+ details: {
915
+ totalMatched: result.totalMatched,
916
+ totalFiles: result.totalFiles,
917
+ patterns: params.patterns,
918
+ },
919
+ };
920
+ },
711
921
 
712
- renderCall(args, theme, context) {
713
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
714
- const patterns = args?.patterns ?? [];
715
- const constraints = args?.constraints;
716
- let content =
717
- theme.fg("toolTitle", theme.bold(toolNames.multiGrep)) +
718
- " " +
719
- theme.fg("accent", patterns.map((p: string) => `"${p}"`).join(", "));
720
- if (constraints) content += theme.fg("toolOutput", ` (${constraints})`);
721
- if (args?.cursor) content += theme.fg("muted", ` (page)`);
722
- text.setText(content);
723
- return text;
724
- },
922
+ renderCall(args, theme, context) {
923
+ const text =
924
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
925
+ const patterns = args?.patterns ?? [];
926
+ const constraints = args?.constraints;
927
+ let content =
928
+ theme.fg("toolTitle", theme.bold(toolNames.multiGrep)) +
929
+ " " +
930
+ theme.fg("accent", patterns.map((p: string) => `"${p}"`).join(", "));
931
+ if (constraints) content += theme.fg("toolOutput", ` (${constraints})`);
932
+ if (args?.cursor) content += theme.fg("muted", ` (page)`);
933
+ text.setText(content);
934
+ return text;
935
+ },
725
936
 
726
- renderResult(result, options, theme, context) {
727
- return renderTextResult(result, options, theme, context, 15);
728
- },
729
- });
937
+ renderResult(result, options, theme, context) {
938
+ return renderTextResult(result, options, theme, context, 15);
939
+ },
940
+ });
941
+ } // end if (enableMultiGrep)
730
942
 
731
943
  // --- commands ---
732
944
 
733
945
  pi.registerCommand("fff-mode", {
734
- description: "Show or set FFF mode: /fff-mode [tools-and-ui | tools-only | override]",
946
+ description:
947
+ "Show or set FFF mode: /fff-mode [tools-and-ui | tools-only | override]",
735
948
  handler: async (args, ctx) => {
736
949
  const arg = (args || "").trim();
737
950
 
@@ -740,13 +953,19 @@ export default function fffExtension(pi: ExtensionAPI) {
740
953
  const mode = getMode();
741
954
  const flag = pi.getFlag("fff-mode") ?? "unset";
742
955
  const env = process.env.PI_FFF_MODE ?? "unset";
743
- ctx.ui.notify(`Current mode: '${mode}'\nFlag: ${flag}, Env: ${env}`, "info");
956
+ ctx.ui.notify(
957
+ `Current mode: '${mode}'\nFlag: ${flag}, Env: ${env}`,
958
+ "info",
959
+ );
744
960
  return;
745
961
  }
746
962
 
747
963
  // Validate and set mode
748
964
  if (!VALID_MODES.includes(arg as FffMode)) {
749
- ctx.ui.notify(`Usage: /fff-mode [${VALID_MODES.join(" | ")}]`, "warning");
965
+ ctx.ui.notify(
966
+ `Usage: /fff-mode [${VALID_MODES.join(" | ")}]`,
967
+ "warning",
968
+ );
750
969
  return;
751
970
  }
752
971
 
package/src/query.ts ADDED
@@ -0,0 +1,87 @@
1
+ import path from "node:path";
2
+
3
+ export function normalizePathConstraint(
4
+ pathConstraint: string,
5
+ cwd = process.cwd(),
6
+ ): string | null {
7
+ let trimmed = pathConstraint.trim();
8
+ if (!trimmed) return trimmed;
9
+
10
+ if (path.isAbsolute(trimmed)) {
11
+ const relative = path.relative(cwd, trimmed).replaceAll(path.sep, "/");
12
+ if (relative === "") return null;
13
+ if (relative.startsWith("../") || relative === ".." || path.isAbsolute(relative)) {
14
+ throw new Error(
15
+ `Path constraint must be relative to the workspace: ${pathConstraint}`,
16
+ );
17
+ }
18
+ trimmed = relative;
19
+ }
20
+
21
+ if (trimmed === "." || trimmed === "./") return null;
22
+ // Strip a leading `./` so `./**/*.rs` and `**/*.rs` behave identically.
23
+ if (trimmed.startsWith("./")) trimmed = trimmed.slice(2);
24
+
25
+ // FFF's glob matcher can treat a hidden directory root glob such as
26
+ // `.agents/**` as empty, while the tool contract says this means "inside
27
+ // this directory". Collapse simple trailing recursive directory globs to the
28
+ // directory-prefix constraint understood by the parser. Keep real file globs
29
+ // such as `src/**/*.ts` unchanged.
30
+ const recursiveDir = trimmed.match(/^(.*)\/\*\*(?:\/\*)?$/);
31
+ if (recursiveDir) {
32
+ const dir = recursiveDir[1];
33
+ if (dir && !/[*?[{]/.test(dir)) return `${dir}/`;
34
+ }
35
+
36
+ // Already signals path-constraint syntax to the parser.
37
+ if (trimmed.startsWith("/") || trimmed.endsWith("/")) return trimmed;
38
+ // Globs (`*.ts`, `src/**/*.cc`, `{src,lib}`) are handled by the parser.
39
+ if (/[*?[{]/.test(trimmed)) return trimmed;
40
+ // Filename with extension (`main.rs`, `config.json`) → FilePath constraint.
41
+ const lastSegment = trimmed.split("/").pop() ?? "";
42
+ if (/\.[a-zA-Z][a-zA-Z0-9]{0,9}$/.test(lastSegment)) return trimmed;
43
+ // Bare directory prefix → append `/` so the parser sees a PathSegment.
44
+ return `${trimmed}/`;
45
+ }
46
+
47
+ // Exclusions are emitted as `!<constraint>` tokens, which the Rust parser
48
+ // understands (crates/fff-query-parser/src/parser.rs). We normalize each one
49
+ // the same way as the include path so bare dirs become PathSegment excludes.
50
+ // Tolerate callers passing already-negated forms like `!src/` by stripping
51
+ // the leading `!` before normalizing so we never double-negate (`!!src/`).
52
+ export function normalizeExcludes(
53
+ exclude: string | string[] | undefined,
54
+ cwd = process.cwd(),
55
+ ): string[] {
56
+ if (!exclude) return [];
57
+ const list = Array.isArray(exclude) ? exclude : [exclude];
58
+ const out: string[] = [];
59
+ for (const raw of list) {
60
+ const parts = raw
61
+ .split(/[,\s]+/)
62
+ .map((s) => s.trim())
63
+ .filter(Boolean);
64
+ for (const p of parts) {
65
+ const stripped = p.startsWith("!") ? p.slice(1) : p;
66
+ const normalized = normalizePathConstraint(stripped, cwd);
67
+ if (normalized) out.push(`!${normalized}`);
68
+ }
69
+ }
70
+ return out;
71
+ }
72
+
73
+ export function buildQuery(
74
+ path: string | undefined,
75
+ pattern: string,
76
+ exclude?: string | string[],
77
+ cwd = process.cwd(),
78
+ ): string {
79
+ const parts: string[] = [];
80
+ if (path) {
81
+ const pathConstraint = normalizePathConstraint(path, cwd);
82
+ if (pathConstraint) parts.push(pathConstraint);
83
+ }
84
+ parts.push(...normalizeExcludes(exclude, cwd));
85
+ parts.push(pattern);
86
+ return parts.join(" ");
87
+ }