Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/pi-fff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@
"peerDependencies": {
"@earendil-works/pi-coding-agent": "*",
"@earendil-works/pi-tui": "*",
"@ff-labs/fff-bun": "*",
"@sinclair/typebox": "*"
},
"peerDependenciesMeta": {
"@ff-labs/fff-bun": {
"optional": true
}
},
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.0.0"
Expand Down
42 changes: 38 additions & 4 deletions packages/pi-fff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,49 @@ import {
Text,
} from "@earendil-works/pi-tui";
import type {
FileFinderApi,
GrepCursor,
GrepMode,
GrepResult,
InitOptions,
MixedItem,
Result,
SearchResult,
} from "@ff-labs/fff-node";
import { FileFinder } from "@ff-labs/fff-node";
import { Type } from "@sinclair/typebox";
import { buildQuery } from "./query";

// Isomorphic SDK loading. pi hosts run under either bun (omp / oh-my-pi) or
// node; the two SDKs implement the same FileFinderApi. Static imports of
// @ff-labs/fff-node pull ffi-rs + optional native binaries into the module
// graph, which trips oh-my-pi's static extension validator (issue #668). We
// defeat static resolution with a variable-name dynamic import.
type FileFinderStatic = {
create(options: InitOptions): Result<FileFinderApi>;
};

let sdkPromise: Promise<{ FileFinder: FileFinderStatic }> | null = null;

function detectRuntime(): "bun" | "node" {
if (typeof (globalThis as { Bun?: unknown }).Bun !== "undefined") return "bun";
if (
typeof process !== "undefined" &&
(process as { versions?: { bun?: string } }).versions?.bun
)
return "bun";
return "node";
}

function loadSdk(): Promise<{ FileFinder: FileFinderStatic }> {
if (sdkPromise) return sdkPromise;
// Fail loud on wrong-runtime SDK rather than falling back — a fallback would
// re-introduce the ffi-rs cost on bun (the whole point of the bun SDK is to
// avoid it) and mask packaging bugs where the correct SDK wasn't installed.
const pkg = detectRuntime() === "bun" ? "@ff-labs/fff-bun" : "@ff-labs/fff-node";
sdkPromise = import(pkg) as Promise<{ FileFinder: FileFinderStatic }>;
return sdkPromise;
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -277,13 +310,13 @@ function createFffMentionProvider(
// ---------------------------------------------------------------------------

export default function fffExtension(pi: ExtensionAPI) {
let finder: FileFinder | null = null;
let finder: FileFinderApi | null = null;
let finderCwd: string | null = null;
// Concurrent ensureFinder() callers share the same in-flight promise so
// FileFinder.create() (which takes native DB locks) runs at most once per
// base path at a time — otherwise parallel tool calls would race and
// deadlock at the native layer (issue #403).
let finderPromise: Promise<FileFinder> | null = null;
let finderPromise: Promise<FileFinderApi> | null = null;
let activeCwd = process.cwd();

// Mode resolution: flag > env > default
Expand Down Expand Up @@ -331,7 +364,7 @@ export default function fffExtension(pi: ExtensionAPI) {
return currentMode !== "tools-only";
}

function ensureFinder(cwd: string): Promise<FileFinder> {
function ensureFinder(cwd: string): Promise<FileFinderApi> {
if (finder && !finder.isDestroyed && finderCwd === cwd)
return Promise.resolve(finder);
if (finderPromise) return finderPromise;
Expand All @@ -343,6 +376,7 @@ export default function fffExtension(pi: ExtensionAPI) {
finderCwd = null;
}

const { FileFinder } = await loadSdk();
const result = FileFinder.create({
basePath: cwd,
frecencyDbPath,
Expand Down
7 changes: 5 additions & 2 deletions packages/pi-fff/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createMockFinder(): MockFinder {
};
}

mock.module("@ff-labs/fff-node", () => ({
const finderModule = {
FileFinder: {
create: mock((options: unknown) => {
createCalls.push(options);
Expand All @@ -43,7 +43,10 @@ mock.module("@ff-labs/fff-node", () => ({
return { ok: true, value: finder };
}),
},
}));
};

mock.module("@ff-labs/fff-node", () => finderModule);
mock.module("@ff-labs/fff-bun", () => finderModule);

mock.module("@earendil-works/pi-tui", () => ({
Text: class Text {
Expand Down
Loading