@ff-labs/pi-fff 0.6.4 → 0.6.5-nightly.287d7b7

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 +435 -209
  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.4",
4
+ "version": "0.6.5-nightly.287d7b7",
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
  };
@@ -184,7 +291,10 @@ class FffEditor extends CustomEditor {
184
291
  tui: any,
185
292
  theme: any,
186
293
  keybindings: any,
187
- getMentionItems: (query: string, signal: AbortSignal) => Promise<AutocompleteItem[]>,
294
+ getMentionItems: (
295
+ query: string,
296
+ signal: AbortSignal,
297
+ ) => Promise<AutocompleteItem[]>,
188
298
  ) {
189
299
  super(tui, theme, keybindings);
190
300
  this.getMentionItems = getMentionItems;
@@ -206,7 +316,12 @@ class FffEditor extends CustomEditor {
206
316
  if (mentionResult) return mentionResult;
207
317
  // Fall back to base provider
208
318
  return (
209
- this.baseProvider?.getSuggestions(lines, cursorLine, cursorCol, options) ?? null
319
+ this.baseProvider?.getSuggestions(
320
+ lines,
321
+ cursorLine,
322
+ cursorCol,
323
+ options,
324
+ ) ?? null
210
325
  );
211
326
  },
212
327
  applyCompletion: (lines, cursorLine, cursorCol, item, prefix) => {
@@ -289,7 +404,8 @@ export default function fffExtension(pi: ExtensionAPI) {
289
404
  aiMode: true,
290
405
  });
291
406
 
292
- if (!result.ok) throw new Error(`Failed to create FFF file finder: ${result.error}`);
407
+ if (!result.ok)
408
+ throw new Error(`Failed to create FFF file finder: ${result.error}`);
293
409
 
294
410
  finder = result.value;
295
411
  finderCwd = cwd;
@@ -316,20 +432,22 @@ export default function fffExtension(pi: ExtensionAPI) {
316
432
  const result = f.mixedSearch(query, { pageSize: MENTION_MAX_RESULTS });
317
433
  if (!result.ok) return [];
318
434
 
319
- return result.value.items.slice(0, MENTION_MAX_RESULTS).map((mixed: MixedItem) => {
320
- if (mixed.type === "directory") {
435
+ return result.value.items
436
+ .slice(0, MENTION_MAX_RESULTS)
437
+ .map((mixed: MixedItem) => {
438
+ if (mixed.type === "directory") {
439
+ return {
440
+ value: buildAtCompletionValue(mixed.item.relativePath),
441
+ label: mixed.item.dirName,
442
+ description: mixed.item.relativePath,
443
+ };
444
+ }
321
445
  return {
322
446
  value: buildAtCompletionValue(mixed.item.relativePath),
323
- label: mixed.item.dirName,
447
+ label: mixed.item.fileName,
324
448
  description: mixed.item.relativePath,
325
449
  };
326
- }
327
- return {
328
- value: buildAtCompletionValue(mixed.item.relativePath),
329
- label: mixed.item.fileName,
330
- description: mixed.item.relativePath,
331
- };
332
- });
450
+ });
333
451
  }
334
452
 
335
453
  function applyEditorMode(ctx: {
@@ -357,12 +475,14 @@ export default function fffExtension(pi: ExtensionAPI) {
357
475
  });
358
476
 
359
477
  pi.registerFlag("fff-frecency-db", {
360
- description: "Path to the frecency database (overrides FFF_FRECENCY_DB env)",
478
+ description:
479
+ "Path to the frecency database (overrides FFF_FRECENCY_DB env)",
361
480
  type: "string",
362
481
  });
363
482
 
364
483
  pi.registerFlag("fff-history-db", {
365
- description: "Path to the query history database (overrides FFF_HISTORY_DB env)",
484
+ description:
485
+ "Path to the query history database (overrides FFF_HISTORY_DB env)",
366
486
  type: "string",
367
487
  });
368
488
 
@@ -392,15 +512,20 @@ export default function fffExtension(pi: ExtensionAPI) {
392
512
  context: any,
393
513
  maxLines = 15,
394
514
  ) => {
395
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
396
- const output = result.content?.find((c) => c.type === "text")?.text?.trim() ?? "";
515
+ const text =
516
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
517
+ const output =
518
+ result.content?.find((c) => c.type === "text")?.text?.trim() ?? "";
397
519
  if (!output) {
398
520
  text.setText(theme.fg("muted", "No output"));
399
521
  return text;
400
522
  }
401
523
 
402
524
  const lines = output.split("\n");
403
- const displayLines = lines.slice(0, options.expanded ? lines.length : maxLines);
525
+ const displayLines = lines.slice(
526
+ 0,
527
+ options.expanded ? lines.length : maxLines,
528
+ );
404
529
  let content = `\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
405
530
  if (lines.length > displayLines.length) {
406
531
  content += theme.fg(
@@ -415,44 +540,50 @@ export default function fffExtension(pi: ExtensionAPI) {
415
540
  // --- grep tool ---
416
541
 
417
542
  const grepSchema = Type.Object({
418
- pattern: Type.String({ description: "Search pattern (plain text or regex)" }),
543
+ pattern: Type.String({
544
+ description: "Search pattern (literal text or regex)",
545
+ }),
419
546
  path: Type.Optional(
420
547
  Type.String({
421
548
  description:
422
- "Directory or file constraint, e.g. 'src/' or '*.ts' (default: project root)",
549
+ "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.",
550
+ }),
551
+ ),
552
+ exclude: Type.Optional(
553
+ Type.Union([Type.String(), Type.Array(Type.String())], {
554
+ description:
555
+ "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/'.",
423
556
  }),
424
557
  ),
425
- literal: Type.Optional(
558
+ caseSensitive: Type.Optional(
426
559
  Type.Boolean({
427
- description: "Treat pattern as literal string instead of regex (default: true)",
560
+ description:
561
+ "Force case-sensitive matching. Default uses smart-case (case-insensitive when pattern is all lowercase).",
428
562
  }),
429
563
  ),
430
564
  context: Type.Optional(
431
- Type.Number({
432
- description: "Number of lines to show before and after each match (default: 0)",
433
- }),
565
+ Type.Number({ description: "Context lines before+after each match" }),
434
566
  ),
435
567
  limit: Type.Optional(
436
568
  Type.Number({
437
- description: `Maximum number of matches to return (default: ${DEFAULT_GREP_LIMIT})`,
569
+ description: `Max matches (default ${DEFAULT_GREP_LIMIT})`,
438
570
  }),
439
571
  ),
440
572
  cursor: Type.Optional(
441
- Type.String({ description: "Cursor from previous result for pagination" }),
573
+ Type.String({ description: "Pagination cursor from previous result" }),
442
574
  ),
443
575
  });
444
576
 
445
577
  pi.registerTool({
446
578
  name: toolNames.grep,
447
579
  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)",
580
+ 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}.`,
581
+ promptSnippet: "Grep contents",
451
582
  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/'.",
583
+ "Prefer bare identifiers as patterns. Literal queries are most efficient.",
584
+ "Use path for include ('src/', '*.ts') and exclude for noise ('test/,*.min.js').",
585
+ "caseSensitive: true when you need exact case (smart-case otherwise).",
586
+ "After 1-2 greps, read the top match instead of more greps.",
456
587
  ],
457
588
  parameters: grepSchema,
458
589
 
@@ -461,53 +592,109 @@ export default function fffExtension(pi: ExtensionAPI) {
461
592
 
462
593
  const f = await ensureFinder(activeCwd);
463
594
  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";
595
+ const query = buildQuery(params.path, params.pattern, params.exclude, activeCwd);
596
+ // Auto-detect: regex if the pattern has regex metacharacters AND parses
597
+ // as a valid regex, otherwise plain literal. The fuzzy fallback below
598
+ // only kicks in for plain mode — regex queries are intentional.
599
+ const hasRegexSyntax =
600
+ params.pattern !==
601
+ params.pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
602
+ let mode: GrepMode = hasRegexSyntax ? "regex" : "plain";
603
+ if (mode === "regex") {
604
+ try {
605
+ new RegExp(params.pattern);
606
+ } catch {
607
+ mode = "plain";
608
+ }
609
+ }
610
+
611
+ // Guard: the agent keeps calling grep with '.*' or similar wildcard-only regex
612
+ // to try to read a whole file. That's not what grep is for — return a terse error
613
+ // steering them to a real pattern, preventing dozens of wasted retries.
614
+ const p = params.pattern.trim();
615
+ const isWildcardOnly =
616
+ hasRegexSyntax &&
617
+ /^(?:[.^$]*(?:[.][*+?]|\*|\+)[.^$]*|[.^$\s]*|\.\*\??|\.\*[+?]?|\.\+\??|\.|\*|\?)$/.test(
618
+ p,
619
+ );
620
+
621
+ if (isWildcardOnly) {
622
+ return {
623
+ content: [
624
+ {
625
+ type: "text",
626
+ text: `Pattern '${params.pattern}' matches everything — grep needs a concrete substring or identifier. Example: \`pattern: 'MyClass'\` or \`pattern: 'export function'\`.`,
627
+ },
628
+ ],
629
+ details: { totalMatched: 0, totalFiles: 0 },
630
+ };
631
+ }
632
+
633
+ // caseSensitive override flips smartCase off; omitting it keeps smart-case
634
+ // (case-insensitive when pattern is all lowercase).
635
+ const smartCase = params.caseSensitive !== true;
466
636
 
467
637
  const grepResult = f.grep(query, {
468
638
  mode,
469
- smartCase: true,
639
+ smartCase,
470
640
  maxMatchesPerFile: Math.min(effectiveLimit, 50),
471
641
  cursor: (params.cursor ? getCursor(params.cursor) : null) ?? null,
472
642
  beforeContext: params.context ?? 0,
473
643
  afterContext: params.context ?? 0,
644
+ classifyDefinitions: true,
474
645
  });
475
646
 
476
647
  if (!grepResult.ok) throw new Error(grepResult.error);
477
648
 
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;
649
+ let result = grepResult.value;
650
+ let fuzzyNotice: string | null = null;
651
+
652
+ // automatic fuzzy fallback allows to broad the queries and find different cases
653
+ if (result.items.length === 0 && !params.cursor && mode !== "regex") {
654
+ const fuzzy = f.grep(params.pattern, {
655
+ mode: "fuzzy",
656
+ smartCase,
657
+ maxMatchesPerFile: Math.min(effectiveLimit, 50),
658
+ cursor: null,
659
+ beforeContext: 0,
660
+ afterContext: 0,
661
+ classifyDefinitions: true,
662
+ });
663
+
664
+ if (fuzzy.ok && fuzzy.value.items.length > 0) {
665
+ fuzzyNotice = `0 exact matches. Maybe you meant this?`;
666
+ result = fuzzy.value;
667
+ }
668
+ }
482
669
 
670
+ let output = formatGrepOutput(result);
483
671
  const notices: string[] = [];
484
- if (result.items.length >= effectiveLimit)
672
+ if (result.regexFallbackError) {
485
673
  notices.push(
486
- `${effectiveLimit} matches limit reached. Use limit=${effectiveLimit * 2} for more`,
674
+ `Invalid regex: ${result.regexFallbackError}, used literal match`,
487
675
  );
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)
676
+ }
677
+ if (result.nextCursor) {
493
678
  notices.push(
494
- `More results available. Use cursor="${storeCursor(result.nextCursor)}" to continue`,
679
+ `Continue with cursor="${storeCursor(result.nextCursor)}"`,
495
680
  );
681
+ }
496
682
 
497
683
  if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
684
+ if (fuzzyNotice) output = `[${fuzzyNotice}]\n${output}`;
498
685
 
499
686
  return {
500
687
  content: [{ type: "text", text: output }],
501
688
  details: {
502
689
  totalMatched: result.totalMatched,
503
690
  totalFiles: result.totalFiles,
504
- truncated: truncation.truncated,
505
691
  },
506
692
  };
507
693
  },
508
694
 
509
695
  renderCall(args, theme, context) {
510
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
696
+ const text =
697
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
511
698
  const pattern = args?.pattern ?? "";
512
699
  const path = args?.path ?? ".";
513
700
  let content =
@@ -532,28 +719,42 @@ export default function fffExtension(pi: ExtensionAPI) {
532
719
  const findSchema = Type.Object({
533
720
  pattern: Type.String({
534
721
  description:
535
- "Fuzzy search query for file names. Supports path prefixes ('src/') and globs ('*.ts').",
722
+ "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
723
  }),
537
724
  path: Type.Optional(
538
- Type.String({ description: "Directory to search in (default: project root)" }),
725
+ Type.String({
726
+ description:
727
+ "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.",
728
+ }),
729
+ ),
730
+ exclude: Type.Optional(
731
+ Type.Union([Type.String(), Type.Array(Type.String())], {
732
+ description:
733
+ "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/'.",
734
+ }),
539
735
  ),
540
736
  limit: Type.Optional(
541
737
  Type.Number({
542
- description: `Maximum number of results (default: ${DEFAULT_FIND_LIMIT})`,
738
+ description: `Max results per page (default ${DEFAULT_FIND_LIMIT})`,
543
739
  }),
544
740
  ),
741
+ cursor: Type.Optional(
742
+ Type.String({ description: "Pagination cursor from previous result" }),
743
+ ),
545
744
  });
546
745
 
547
746
  pi.registerTool({
548
747
  name: toolNames.find,
549
748
  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)",
749
+ 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}.`,
750
+ promptSnippet: "Find files by path or glob",
553
751
  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.",
752
+ "Matches the WHOLE path, not just the filename — `profile` hits `chrome/browser/profiles/x.cc` too.",
753
+ "Keep queries to 1-2 terms; extra words narrow.",
754
+ "Use for paths, not content. Use grep for content.",
755
+ "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.",
756
+ "To list everything inside a directory, pass path: 'dir/**' with an empty or wildcard pattern instead of using pattern alone.",
757
+ "Use exclude: 'test/,*.min.js' to cut noise in large repos.",
557
758
  ],
558
759
  parameters: findSchema,
559
760
 
@@ -561,43 +762,71 @@ export default function fffExtension(pi: ExtensionAPI) {
561
762
  if (signal?.aborted) throw new Error("Operation aborted");
562
763
 
563
764
  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
765
 
567
- const searchResult = f.fileSearch(query, { pageSize: effectiveLimit });
766
+ // Resume from a prior cursor if supplied — cursor owns query+pageSize so
767
+ // the agent can't accidentally mix patterns across pages.
768
+ const resumed = params.cursor ? getFindCursor(params.cursor) : undefined;
769
+ const effectiveLimit = resumed
770
+ ? resumed.pageSize
771
+ : Math.max(1, params.limit ?? DEFAULT_FIND_LIMIT);
772
+ const query = resumed
773
+ ? resumed.query
774
+ : buildQuery(params.path, params.pattern, params.exclude, activeCwd);
775
+ const pattern = resumed ? resumed.pattern : params.pattern;
776
+ const pageIndex = resumed?.nextPageIndex ?? 0;
777
+
778
+ const searchResult = f.fileSearch(query, {
779
+ pageIndex,
780
+ pageSize: effectiveLimit,
781
+ });
568
782
  if (!searchResult.ok) throw new Error(searchResult.error);
569
783
 
570
784
  const result = searchResult.value;
571
- let output = formatFindOutput(result, effectiveLimit);
572
- const truncation = truncateHead(output, { maxLines: Number.MAX_SAFE_INTEGER });
573
- output = truncation.content;
785
+ const formatted = formatFindOutput(result, effectiveLimit, pattern);
786
+ let output = formatted.output;
787
+
788
+ // Infer hasMore: native fileSearch fills pageSize when more results
789
+ // exist, so if we got a full page AND totalMatched exceeds what we've
790
+ // shown so far there's another page to fetch.
791
+ const shownSoFar = pageIndex * effectiveLimit + result.items.length;
792
+ const hasMore =
793
+ result.items.length >= effectiveLimit &&
794
+ result.totalMatched > shownSoFar;
574
795
 
575
796
  const notices: string[] = [];
576
- if (result.items.length >= effectiveLimit)
797
+ if (formatted.weak && formatted.shownCount > 0)
577
798
  notices.push(
578
- `${effectiveLimit} results limit reached. Use limit=${effectiveLimit * 2} for more, or refine pattern`,
799
+ `Query "${pattern}" produced only weak scattered fuzzy matches. Output capped at ${formatted.shownCount}/${result.totalMatched}.`,
579
800
  );
580
- if (truncation.truncated)
581
- notices.push(`${formatSize(DEFAULT_MAX_BYTES)} limit reached`);
582
- if (result.totalMatched > result.items.length)
801
+
802
+ if (!formatted.weak && hasMore) {
803
+ const remaining = result.totalMatched - shownSoFar;
804
+ const cursorId = storeFindCursor({
805
+ query,
806
+ pattern,
807
+ pageSize: effectiveLimit,
808
+ nextPageIndex: pageIndex + 1,
809
+ });
583
810
  notices.push(
584
- `${result.totalMatched} total matches (${result.totalFiles} indexed files)`,
811
+ `${remaining} more match${remaining === 1 ? "" : "es"} available. cursor="${cursorId}" to continue`,
585
812
  );
813
+ }
586
814
 
587
815
  if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
588
-
589
816
  return {
590
817
  content: [{ type: "text", text: output }],
591
818
  details: {
592
819
  totalMatched: result.totalMatched,
593
820
  totalFiles: result.totalFiles,
594
- truncated: truncation.truncated,
821
+ pageIndex,
822
+ hasMore,
595
823
  },
596
824
  };
597
825
  },
598
826
 
599
827
  renderCall(args, theme, context) {
600
- const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
828
+ const text =
829
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
601
830
  const pattern = args?.pattern ?? "";
602
831
  const path = args?.path ?? ".";
603
832
  let content =
@@ -607,6 +836,7 @@ export default function fffExtension(pi: ExtensionAPI) {
607
836
  theme.fg("toolOutput", ` in ${path}`);
608
837
  if (args?.limit !== undefined)
609
838
  content += theme.fg("toolOutput", ` (limit ${args.limit})`);
839
+ if (args?.cursor) content += theme.fg("muted", ` (page)`);
610
840
  text.setText(content);
611
841
  return text;
612
842
  },
@@ -617,121 +847,111 @@ export default function fffExtension(pi: ExtensionAPI) {
617
847
  });
618
848
 
619
849
  // --- multi_grep tool ---
850
+ // My latest tests are showing that the multi grep tool is only harmful, trying to get rid of it
851
+ const enableMultiGrep = process.env.PI_FFF_MULTIGREP === "1";
620
852
 
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({
853
+ if (enableMultiGrep) {
854
+ const multiGrepSchema = Type.Object({
855
+ patterns: Type.Array(Type.String(), {
628
856
  description:
629
- "File constraints, e.g. '*.{ts,tsx} !test/' to filter files. Separate from patterns.",
857
+ "Literal patterns (OR). Include snake_case/camelCase/PascalCase variants.",
630
858
  }),
631
- ),
632
- context: Type.Optional(
633
- Type.Number({
634
- description: "Number of context lines before and after each match (default: 0)",
635
- }),
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;
859
+ constraints: Type.Optional(
860
+ Type.String({ description: "File filter, e.g. '*.{ts,tsx} !test/'" }),
861
+ ),
862
+ context: Type.Optional(
863
+ Type.Number({ description: "Context lines before+after" }),
864
+ ),
865
+ limit: Type.Optional(
866
+ Type.Number({
867
+ description: `Max matches (default ${DEFAULT_GREP_LIMIT})`,
868
+ }),
869
+ ),
870
+ cursor: Type.Optional(Type.String({ description: "Pagination cursor" })),
871
+ });
686
872
 
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
- );
873
+ pi.registerTool({
874
+ name: toolNames.multiGrep,
875
+ label: toolNames.multiGrep,
876
+ description:
877
+ "Search file contents for ANY of multiple literal patterns (OR, SIMD Aho-Corasick). Faster than regex alternation.",
878
+ promptSnippet: "Multi-pattern OR content search",
879
+ promptGuidelines: [
880
+ "Use when searching for several identifiers at once.",
881
+ "Include all naming-convention variants (snake/camel/Pascal).",
882
+ "Patterns are literal. Use constraints for file filters.",
883
+ ],
884
+ parameters: multiGrepSchema,
885
+
886
+ async execute(_toolCallId, params, signal) {
887
+ if (signal?.aborted) throw new Error("Operation aborted");
888
+ if (!params.patterns?.length)
889
+ throw new Error("patterns array must have at least 1 element");
890
+
891
+ const f = await ensureFinder(activeCwd);
892
+ const effectiveLimit = Math.max(1, params.limit ?? DEFAULT_GREP_LIMIT);
893
+
894
+ const grepResult = f.multiGrep({
895
+ patterns: params.patterns,
896
+ constraints: params.constraints,
897
+ maxMatchesPerFile: Math.min(effectiveLimit, 50),
898
+ smartCase: true,
899
+ cursor: (params.cursor ? getCursor(params.cursor) : null) ?? null,
900
+ beforeContext: params.context ?? 0,
901
+ afterContext: params.context ?? 0,
902
+ });
903
+
904
+ if (!grepResult.ok) throw new Error(grepResult.error);
905
+
906
+ const result = grepResult.value;
907
+ let output = formatGrepOutput(result);
908
+
909
+ const notices: string[] = [];
910
+ if (result.items.length >= effectiveLimit)
911
+ notices.push(`${effectiveLimit}+ matches (refine patterns)`);
912
+ if (result.nextCursor)
913
+ notices.push(
914
+ `More available. cursor="${storeCursor(result.nextCursor)}" to continue`,
915
+ );
698
916
 
699
- if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
917
+ if (notices.length > 0) output += `\n\n[${notices.join(". ")}]`;
700
918
 
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
- },
919
+ return {
920
+ content: [{ type: "text", text: output }],
921
+ details: {
922
+ totalMatched: result.totalMatched,
923
+ totalFiles: result.totalFiles,
924
+ patterns: params.patterns,
925
+ },
926
+ };
927
+ },
711
928
 
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
- },
929
+ renderCall(args, theme, context) {
930
+ const text =
931
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
932
+ const patterns = args?.patterns ?? [];
933
+ const constraints = args?.constraints;
934
+ let content =
935
+ theme.fg("toolTitle", theme.bold(toolNames.multiGrep)) +
936
+ " " +
937
+ theme.fg("accent", patterns.map((p: string) => `"${p}"`).join(", "));
938
+ if (constraints) content += theme.fg("toolOutput", ` (${constraints})`);
939
+ if (args?.cursor) content += theme.fg("muted", ` (page)`);
940
+ text.setText(content);
941
+ return text;
942
+ },
725
943
 
726
- renderResult(result, options, theme, context) {
727
- return renderTextResult(result, options, theme, context, 15);
728
- },
729
- });
944
+ renderResult(result, options, theme, context) {
945
+ return renderTextResult(result, options, theme, context, 15);
946
+ },
947
+ });
948
+ } // end if (enableMultiGrep)
730
949
 
731
950
  // --- commands ---
732
951
 
733
952
  pi.registerCommand("fff-mode", {
734
- description: "Show or set FFF mode: /fff-mode [tools-and-ui | tools-only | override]",
953
+ description:
954
+ "Show or set FFF mode: /fff-mode [tools-and-ui | tools-only | override]",
735
955
  handler: async (args, ctx) => {
736
956
  const arg = (args || "").trim();
737
957
 
@@ -740,13 +960,19 @@ export default function fffExtension(pi: ExtensionAPI) {
740
960
  const mode = getMode();
741
961
  const flag = pi.getFlag("fff-mode") ?? "unset";
742
962
  const env = process.env.PI_FFF_MODE ?? "unset";
743
- ctx.ui.notify(`Current mode: '${mode}'\nFlag: ${flag}, Env: ${env}`, "info");
963
+ ctx.ui.notify(
964
+ `Current mode: '${mode}'\nFlag: ${flag}, Env: ${env}`,
965
+ "info",
966
+ );
744
967
  return;
745
968
  }
746
969
 
747
970
  // Validate and set mode
748
971
  if (!VALID_MODES.includes(arg as FffMode)) {
749
- ctx.ui.notify(`Usage: /fff-mode [${VALID_MODES.join(" | ")}]`, "warning");
972
+ ctx.ui.notify(
973
+ `Usage: /fff-mode [${VALID_MODES.join(" | ")}]`,
974
+ "warning",
975
+ );
750
976
  return;
751
977
  }
752
978
 
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
+ }