Skip to content

grep fails when target directory is outside of the initial workspace (ctx.cwd) #4

Description

@fooofei

Description

When starting pi in a specific directory, the fff tool fails to search for files in a different directory provided in the prompt. This is because the search indexer is currently limited to the initial ctx.cwd.

Steps to Reproduce

  1. Start pi in directory d:\a\b\c:
    d:\a\b\c> pi
    
    
    1. Enter the following prompt:
    
    "please search content hello in d:\d directory"
    
    

Expected Behavior

The tool should be able to index and search files within the specified d:\d directory.

Actual Behavior

The search returns no results.
In src/index.ts, the session_start function initializes ctx.cwd to d:\a\b\c. Because fff only indexes the current working directory, it cannot access or find files located in d:\d.

Suggested Solutions

Solution 1: Broaden Indexing Scope

In session_start, consider indexing from the drive root (e.g., d:) instead of the sub-directory to allow broader file access.

Solution 2: Implement Dynamic Re-indexing

Introduce a tool like fff_chindex_directory to allow the system to dynamically change or add a directory to the index based on the user's requested path.

attachment

diff --git a/package.json b/package.json
index e5ee18d..24ed4ae 100644
--- a/package.json
+++ b/package.json
@@ -32,7 +32,7 @@
     "url": "git+https://github.com/ShpetimA/pi-fff.git"
   },
   "dependencies": {
-    "@ff-labs/fff-node": "^0.5.2",
+    "@ff-labs/fff-node": "^0.6.0",
     "@sinclair/typebox": "^0.34.41",
     "better-result": "^2.8.2"
   },
diff --git a/src/fff-runtime.ts b/src/fff-runtime.ts
index 567bf6a..b1665d9 100644
--- a/src/fff-runtime.ts
+++ b/src/fff-runtime.ts
@@ -304,6 +304,16 @@ export class FffRuntime {
 		return toVoidResult(safeFinderCall("scanFiles", () => finderResult.value.scanFiles()));
 	}
 
+  async chindex(path: string) {
+    const finderResult = await this.ensure();
+		if (finderResult.isErr()) return propagateError(finderResult);
+		return toVoidResult(safeFinderCall("reindex", () => {
+      const r = finderResult.value.reindex(path);
+      this.basePath = isAbsolute(path) ? path : resolve(this.basePath, path);
+      return r;
+    }));
+  }
+
 	async getStatus(): Promise<AppResult<{ state: string; indexedFiles?: number; error?: string }, RuntimeInitializationError>> {
 		const finderResult = await this.ensure();
 		if (finderResult.isErr()) return propagateError(finderResult);
diff --git a/src/register-tools.ts b/src/register-tools.ts
index df446aa..dd4c5c5 100644
--- a/src/register-tools.ts
+++ b/src/register-tools.ts
@@ -98,6 +98,7 @@ export function registerTools(pi: ExtensionAPI, deps: ToolRegistrationDeps): voi
 			"Use path/glob/constraints to narrow scope before trying another grep.",
 			"Use outputMode=files_with_matches when content output is too noisy.",
 			"After one or two good greps, read the best matching file.",
+      "Use fff_chdir tool when given path scope has no matches to change fff index directory to the right one.",
 		],
 		parameters: grepSchema,
 		async execute(toolCallId, params, signal, onUpdate, ctx) {
@@ -205,4 +206,25 @@ export function registerTools(pi: ExtensionAPI, deps: ToolRegistrationDeps): voi
 			});
 		},
 	});
+
+
+  pi.registerTool({
+		name: "fff_chdir",
+		label: "change fff index directory",
+		description: "change fff index directory",
+		promptSnippet: "Explore which files exist for a topic before reading one.",
+		promptGuidelines: ["Use this tool when grep/find_files No matches found."],
+		parameters: Type.Object({
+			path: Type.String({ description: "new index directory" }),
+		}),
+		async execute(_toolCallId, params) {
+   		const guarded = getAgentRuntime(buildFindFilesDetails(undefined, "agentTools"), buildFindFilesDetails());
+			if (guarded.kind !== "ready") return guarded.result;
+			const result = await guarded.runtime.chindex(params.path);
+			return result.match({
+				err: (error) => textResult(`Failed to change fff index directory to ${params.path}: ${error.message}` , null),
+				ok: () => textResult(`FFF index directory changed to ${params.path}` , null),
+			});
+		},
+	});
 }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions