Skip to content
Open
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
210 changes: 0 additions & 210 deletions package-lock.json

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

159 changes: 159 additions & 0 deletions packages/pi-fff/src/aux-finders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import type { FileFinderApi } from "@ff-labs/fff-node";
import { loadSdk, SCAN_TIMEOUT_MS } from "./sdk";

export const MAX_AUX = 3;
export const IDLE_TTL_MS = 5 * 60 * 1000;

interface AuxPicker {
root: string;
finder: FileFinderApi;
lastUsed: number;
}

export interface AuxOpts {
frecencyDbPath?: string;
historyDbPath?: string;
enableFsRootScanning: boolean;
}

export class AuxFinderPool {
private entries: AuxPicker[] = [];
constructor(private opts: AuxOpts) {}

destroy(): void {
for (const e of this.entries) {
e.finder.destroy();
}

this.entries = [];
}

private sweepIdle(now = Date.now()): void {
const kept: AuxPicker[] = [];
for (const e of this.entries) {
if (now - e.lastUsed > IDLE_TTL_MS) {
if (!e.finder.isDestroyed) e.finder.destroy();
} else {
kept.push(e);
}
}
this.entries = kept;
}

async acquire(
maybeRoot: string,
opts?: { exact?: boolean },
): Promise<{ finder: FileFinderApi; root: string }> {
this.sweepIdle();
let covering: AuxPicker | null = null;
for (const e of this.entries) {
if (e.finder.isDestroyed) continue;
if (opts?.exact ? e.root !== maybeRoot : !rootCovers(e.root, maybeRoot)) continue;
if (!covering || e.root.length > covering.root.length) covering = e;
}

if (covering) {
covering.lastUsed = Date.now();
return { finder: covering.finder, root: covering.root };
}

if (this.entries.length >= MAX_AUX) {
let oldest = this.entries[0];
for (const e of this.entries)
if (e.lastUsed < oldest.lastUsed) oldest = e;
if (!oldest.finder.isDestroyed) oldest.finder.destroy();
this.entries = this.entries.filter((e) => e !== oldest);
}

const { FileFinder } = await loadSdk();
const result = FileFinder.create({
basePath: maybeRoot,
frecencyDbPath: this.opts.frecencyDbPath,
historyDbPath: this.opts.historyDbPath,
aiMode: true,
enableHomeDirScanning: true,
enableFsRootScanning: this.opts.enableFsRootScanning,
});
if (!result.ok)
throw new Error(
`Failed to create aux file finder for ${maybeRoot}: ${result.error}`,
);

await result.value.waitForScan(SCAN_TIMEOUT_MS);
this.entries.push({ root: maybeRoot, finder: result.value, lastUsed: Date.now() });
return { finder: result.value, root: maybeRoot };
}

size(): number {
this.sweepIdle();
return this.entries.length;
}
}

// Split an absolute path into an existing directory (the aux root) and a
// remainder usable as a fuzzy path constraint relative to that root. Glob and
// nonexistent segments both go into the suffix: we walk up to the nearest
// existing ancestor so partially-wrong paths still resolve to a search root.
export function resolveAuxRoot(
absPath: string,
): { root: string; suffix: string } | null {
const trimmed = path.normalize(absPath.trim()).replace(/\/+$/, "") || "/";
if (!path.isAbsolute(trimmed)) return null;
if (trimmed === path.sep) return { root: path.sep, suffix: "" };

const parts = trimmed.split(path.sep);
const firstGlob = parts.findIndex((p) => /[*?[{]/.test(p));
const boundary = firstGlob === -1 ? parts.length : firstGlob;

// Deepest existing non-glob prefix wins; everything below it is suffix.
for (let i = boundary; i > 0; i--) {
const candidate = parts.slice(0, i).join(path.sep) || path.sep;
let stat: fs.Stats;
try {
stat = fs.statSync(candidate);
} catch {
continue;
}
if (stat.isFile()) {
return {
root: parts.slice(0, i - 1).join(path.sep) || path.sep,
suffix: parts.slice(i - 1).join("/"),
};
}
return { root: candidate, suffix: parts.slice(i).join("/") };
}
return null;
}

// Decide whether a `path` parameter should route to the workspace finder or
// to an aux finder. Accepts absolute paths, `~`-prefixed paths, and relative
// paths escaping the workspace (`../other-project`); everything is resolved
// against cwd first. Returns null to signal "no rerouting" (workspace finder).
export function routePathConstraint(
pathConstraint: string | undefined,
cwd: string,
): { root: string; suffix: string } | null {
if (!pathConstraint) return null;
let candidate = pathConstraint.trim();
if (!candidate) return null;
if (candidate === "~" || candidate.startsWith("~/"))
candidate = path.join(os.homedir(), candidate.slice(1));
if (!path.isAbsolute(candidate)) {
// Plain workspace-relative constraints stay on the workspace finder.
if (candidate !== ".." && !candidate.startsWith("../")) return null;
candidate = path.resolve(cwd, candidate);
}
const rel = path.relative(cwd, candidate);
if (rel !== ".." && !rel.startsWith(`..${path.sep}`)) return null;
return resolveAuxRoot(candidate);
}


export function rootCovers(root: string, target: string): boolean {
if (root === target) return true;
const prefix = root.endsWith(path.sep) ? root : root + path.sep;
return target.startsWith(prefix);
}
Loading
Loading