fix(core): start fff search lazily on first find (#58959)

This commit is contained in:
Kit Langton
2026-10-25 08:08:37 -07:00
committed by GitHub
Unverified
parent b6cbbd7646
commit 9cc9e58c73
3 changed files with 245 additions and 67 deletions
+1
View File
@@ -44,6 +44,7 @@ export interface MixedSearch {
export interface Picker {
destroy(): void
isScanning(): boolean
fileSearch(query: string, options?: SearchOptions): Result<Search>
directorySearch(query: string, options?: SearchOptions): Result<DirSearch>
mixedSearch(query: string, options?: SearchOptions): Result<MixedSearch>
+106 -66
View File
@@ -112,73 +112,113 @@ const ripgrepLayer = Layer.effect(
}),
)
export const fffLayer = Layer.effect(
Service,
Effect.gen(function* () {
const location = yield* Location.Service
const result = yield* Effect.try({
try: () =>
Fff.create({
basePath: location.directory,
aiMode: true,
disableMmapCache: true,
disableContentIndexing: true,
}),
catch: (cause) => cause,
}).pipe(
Effect.catch((error) => Effect.logWarning("failed to initialize fff", { error }).pipe(Effect.as(undefined))),
)
if (!result?.ok) {
if (result) yield* Effect.logWarning("failed to initialize fff", { error: result.error })
return Service.of({
find: () => Effect.succeed([]),
})
}
yield* Effect.addFinalizer(() => Effect.sync(() => result.value.destroy()))
return Service.of({
find: (input) =>
Effect.sync(() => {
const options = { pageIndex: 0, pageSize: input.limit ?? 50 }
const items = (() => {
if (input.type === "file") {
const found = result.value.fileSearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.relativePath,
type: "file" as const,
score: found.value.scores[index]?.total ?? 0,
}))
}
if (input.type === "directory") {
const found = result.value.directorySearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.relativePath,
type: "directory" as const,
score: found.value.scores[index]?.total ?? 0,
}))
}
const found = result.value.mixedSearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.item.relativePath,
type: item.type,
score: found.value.scores[index]?.total ?? 0,
}))
})()
return items
.sort((a, b) => b.score - a.score || a.path.length - b.path.length)
.map((item) => {
const relative = item.path.replaceAll("\\", "/").replace(/\/$/, "")
return FileSystem.Entry.make({
path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")),
type: item.type,
})
})
}),
const FFF_SCAN_WAIT = Duration.seconds(3)
type CreatePicker = (options: Fff.Init) => Fff.Result<Fff.Picker>
function createPicker(
create: CreatePicker,
directory: string,
): { ok: true; value: Fff.Picker } | { ok: false; error: unknown } {
try {
return create({
basePath: directory,
aiMode: true,
disableMmapCache: true,
disableContentIndexing: true,
})
}),
)
} catch (error) {
return { ok: false, error }
}
}
// Each fff picker owns its own scanner, watcher and git threads, so it starts on the first search
// instead of when the Location boots.
export const makeFffLayer = (create: CreatePicker) =>
Layer.effect(
Service,
Effect.gen(function* () {
const location = yield* Location.Service
let state: { readonly result: ReturnType<typeof createPicker>; scanned: boolean } | "closed" | undefined
// The layer owns the picker before it exists: creating and publishing it is one synchronous,
// uninterruptible step, so an interrupted search can't leave a native picker without this finalizer.
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
if (typeof state === "object" && state.result.ok) state.result.value.destroy()
state = "closed"
}),
)
const acquire = Effect.uninterruptible(
Effect.sync(() => {
if (state !== undefined) return { current: state, created: false }
state = { result: createPicker(create, location.directory), scanned: false }
return { current: state, created: true }
}),
)
const picker = Effect.gen(function* () {
const { current, created } = yield* acquire
if (current === "closed") return undefined
if (!current.result.ok) {
if (created) yield* Effect.logWarning("failed to initialize fff", { error: current.result.error })
return undefined
}
if (!current.scanned) {
const value = current.result.value
yield* Effect.gen(function* () {
while (value.isScanning()) yield* Effect.sleep("20 millis")
}).pipe(Effect.timeoutOption(FFF_SCAN_WAIT))
current.scanned = true
}
return current.result.value
})
return Service.of({
find: (input) =>
Effect.gen(function* () {
const finder = yield* picker
if (!finder) return []
const options = { pageIndex: 0, pageSize: input.limit ?? 50 }
const items = (() => {
if (input.type === "file") {
const found = finder.fileSearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.relativePath,
type: "file" as const,
score: found.value.scores[index]?.total ?? 0,
}))
}
if (input.type === "directory") {
const found = finder.directorySearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.relativePath,
type: "directory" as const,
score: found.value.scores[index]?.total ?? 0,
}))
}
const found = finder.mixedSearch(input.query.trim(), options)
if (!found.ok) throw found.error
return found.value.items.map((item, index) => ({
path: item.item.relativePath,
type: item.type,
score: found.value.scores[index]?.total ?? 0,
}))
})()
return items
.sort((a, b) => b.score - a.score || a.path.length - b.path.length)
.map((item) => {
const relative = item.path.replaceAll("\\", "/").replace(/\/$/, "")
return FileSystem.Entry.make({
path: RelativePath.make(relative + (item.type === "directory" ? path.sep : "")),
type: item.type,
})
})
}),
})
}),
)
export const fffLayer = makeFffLayer(Fff.create)
const layer = (options?: Options) =>
Layer.unwrap(
+138 -1
View File
@@ -3,7 +3,7 @@ import fuzzysort from "fuzzysort"
import { mkdir } from "node:fs/promises"
import os from "os"
import path from "path"
import { Deferred, Effect, Layer } from "effect"
import { Context, Deferred, Effect, Exit, Fiber, Layer, Scope } from "effect"
import { TestClock } from "effect/testing"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { FileSystem } from "@opencode/core/filesystem"
@@ -33,7 +33,117 @@ const ripgrepStub = (entry: string, onFind: (input: Ripgrep.FindInput) => void)
}),
)
const fakeFff = () => {
const state = { scanning: true, files: [] as string[], created: 0, destroyed: 0 }
const create: Parameters<typeof FileSystemSearch.makeFffLayer>[0] = () => {
state.created++
const search = () => ({
ok: true as const,
value: {
items: state.files.map((relativePath) => ({ relativePath })),
scores: state.files.map(() => ({ total: 1 })),
},
})
return {
ok: true,
value: {
destroy: () => void state.destroyed++,
isScanning: () => state.scanning,
fileSearch: search,
directorySearch: () => ({ ok: true, value: { items: [], scores: [] } }),
mixedSearch: () => ({ ok: true, value: { items: [], scores: [] } }),
},
}
}
const layer = FileSystemSearch.makeFffLayer(create).pipe(
Layer.provide(
Layer.succeed(
Location.Service,
Location.Service.of(location({ directory: AbsolutePath.make(path.join(os.tmpdir(), "opencode-fff-fake")) })),
),
),
)
return { state, layer }
}
const buildSearch = (layer: Layer.Layer<FileSystemSearch.Service>) =>
Effect.gen(function* () {
const scope = yield* Scope.make()
const context = yield* Layer.buildWithScope(layer, scope)
return { scope, search: Context.get(context, FileSystemSearch.Service) }
})
const findFiles = (search: FileSystemSearch.Interface) =>
search.find({ query: "src", type: "file" }).pipe(Effect.map((entries) => entries.map((entry) => entry.path)))
describe("FileSystemSearch", () => {
it.effect("creates the fff picker on first search and waits for its initial scan", () =>
Effect.gen(function* () {
const fff = fakeFff()
const { scope, search } = yield* buildSearch(fff.layer)
expect(fff.state.created).toBe(0)
const pending = yield* Effect.forkChild(findFiles(search))
yield* TestClock.adjust("100 millis")
expect(fff.state.created).toBe(1)
expect(pending.pollUnsafe()).toBeUndefined()
fff.state.files = ["src/a.ts", "src/b.ts"]
fff.state.scanning = false
yield* TestClock.adjust("20 millis")
expect(yield* Fiber.join(pending)).toEqual([RelativePath.make("src/a.ts"), RelativePath.make("src/b.ts")])
expect(fff.state.created).toBe(1)
yield* Scope.close(scope, Exit.void)
expect(fff.state.destroyed).toBe(1)
}),
)
it.effect("returns partial fff results once the initial scan wait times out", () =>
Effect.gen(function* () {
const fff = fakeFff()
fff.state.files = ["src/a.ts"]
const { scope, search } = yield* buildSearch(fff.layer)
const pending = yield* Effect.forkChild(findFiles(search))
yield* TestClock.adjust("2900 millis")
expect(pending.pollUnsafe()).toBeUndefined()
yield* TestClock.adjust("100 millis")
expect(yield* Fiber.join(pending)).toEqual([RelativePath.make("src/a.ts")])
// The picker is still scanning, but later searches no longer wait for it.
const next = yield* Effect.forkChild(findFiles(search))
yield* TestClock.adjust("1 millis")
expect(next.pollUnsafe()).toBeDefined()
yield* Scope.close(scope, Exit.void)
}),
)
it.effect("keeps the fff picker owned by the layer when the first search is interrupted", () =>
Effect.gen(function* () {
const fff = fakeFff()
const { scope, search } = yield* buildSearch(fff.layer)
const pending = yield* Effect.forkChild(findFiles(search))
yield* TestClock.adjust("100 millis")
yield* Fiber.interrupt(pending)
expect(fff.state.created).toBe(1)
expect(fff.state.destroyed).toBe(0)
fff.state.files = ["src/a.ts"]
fff.state.scanning = false
const next = yield* Effect.forkChild(findFiles(search))
yield* TestClock.adjust("20 millis")
expect(yield* Fiber.join(next)).toEqual([RelativePath.make("src/a.ts")])
expect(fff.state.created).toBe(1)
yield* Scope.close(scope, Exit.void)
expect(fff.state.destroyed).toBe(1)
expect(yield* findFiles(search)).toEqual([])
expect(fff.state.created).toBe(1)
}),
)
it.live("honors wildcard directory rules from .gitignore", () =>
Effect.gen(function* () {
const directory = (yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-fff-ignore-")))).path
@@ -63,6 +173,33 @@ describe("FileSystemSearch", () => {
}),
)
it.live("real fff finds files on first search", () =>
Effect.gen(function* () {
const directory = (yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-fff-lazy-")))).path
yield* Effect.promise(() => Bun.write(path.join(directory, "src/existing-alpha.ts"), "alpha"))
expect(Bun.spawnSync(["git", "init", "-q"], { cwd: directory }).exitCode).toBe(0)
const ref = Location.Ref.make({ directory: AbsolutePath.make(directory) })
const layer = FileSystemSearch.fffLayer.pipe(
Layer.provide(
Layer.succeed(
Location.Service,
Location.Service.of(
location(ref, { vcs: { type: "git", store: AbsolutePath.make(path.join(directory, ".git")) } }),
),
),
),
)
yield* Effect.gen(function* () {
const search = yield* FileSystemSearch.Service
const found = yield* search
.find({ query: "existing-alpha", type: "file" })
.pipe(Effect.map((entries) => entries.map((entry) => entry.path)))
expect(found).toContain(RelativePath.make("src/existing-alpha.ts"))
}).pipe(Effect.provide(layer))
}),
)
it.live("selects the ripgrep layer for workspace-backed locations even when vcs would pick fff", () =>
Effect.gen(function* () {
const directory = (yield* Effect.acquireDisposable(Effect.promise(() => tmpdir("opencode-search-workspace-"))))