Compare commits

...

2 Commits

Author SHA1 Message Date
Luke Parker 0b31861db2 refactor(core): use scoped search effects 2026-08-13 02:16:17 +00:00
Luke Parker fbde9dd414 fix(core): refresh fallback file search 2026-08-13 02:09:27 +00:00
2 changed files with 89 additions and 20 deletions
+36 -16
View File
@@ -2,7 +2,7 @@ export * as FileSystemSearch from "./search.js"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
import path from "path"
import { Context, Effect, Layer, Schema, Scope } from "effect"
import { Context, Duration, Effect, Layer, Schema, Stream } from "effect"
import { Fff } from "#fff"
import fuzzysort from "fuzzysort"
import { FileSystem } from "../filesystem.js"
@@ -10,6 +10,7 @@ import { Location } from "../location.js"
import { Ripgrep } from "../ripgrep.js"
import { RelativePath } from "../schema.js"
import { Protected } from "./protected.js"
import { Watcher } from "./watcher.js"
export interface Interface {
readonly find: (input: FileSystem.FindInput) => Effect.Effect<FileSystem.Entry[]>
@@ -27,33 +28,48 @@ export const ripgrepLayer = Layer.effect(
Effect.gen(function* () {
const location = yield* Location.Service
const ripgrep = yield* Ripgrep.Service
const scope = yield* Scope.Scope
const files: string[] = []
const directories = new Set<string>()
const watcher = yield* Watcher.Service
const home = Protected.isHome(location.directory)
yield* ripgrep
const scan = ripgrep
.find({
cwd: location.directory,
pattern: "*",
limit: location.vcs && !home ? Number.MAX_SAFE_INTEGER : 100_000,
exclude: home ? [...Protected.names()].map((name) => `${name}/**`) : undefined,
onEntry: (entry) =>
Effect.sync(() => {
files.push(entry.path)
const parts = entry.path.split("/")
parts.slice(0, -1).forEach((_, index) => directories.add(parts.slice(0, index + 1).join("/") + path.sep))
}),
})
.pipe(Effect.orDie, Effect.asVoid, Effect.forkIn(scope))
.pipe(
Effect.orDie,
Effect.map((entries) => {
const files = entries.map((entry) => entry.path)
return {
files,
directories: new Set(
files.flatMap((file) => {
const parts = file.split("/")
return parts.slice(0, -1).map((_, index) => parts.slice(0, index + 1).join("/") + path.sep)
}),
),
}
}),
)
const [snapshot, invalidate] = yield* Effect.cachedInvalidateWithTTL(scan, Duration.infinity)
const updates = yield* watcher.subscribe({ path: location.directory, type: "directory" })
yield* updates.pipe(
Stream.runForEach(() => invalidate),
Effect.forkScoped,
)
yield* Effect.yieldNow
yield* snapshot.pipe(Effect.forkScoped)
return Service.of({
find: (input) =>
Effect.gen(function* () {
const index = yield* snapshot
const items =
input.type === "file"
? files
? index.files
: input.type === "directory"
? Array.from(directories)
: [...files, ...directories]
? Array.from(index.directories)
: [...index.files, ...index.directories]
return fuzzysort.go(input.query, items, { limit: input.limit ?? 50 }).map((item) => {
const relative = item.target
const type = relative.endsWith(path.sep) ? ("directory" as const) : ("file" as const)
@@ -147,7 +163,11 @@ export const layer = (options?: Options) =>
)
export function configured(options?: Options) {
return makeLocationNode({ service: Service, layer: layer(options), deps: [Location.node, Ripgrep.node] })
return makeLocationNode({
service: Service,
layer: layer(options),
deps: [Location.node, Ripgrep.node, Watcher.node],
})
}
export const node = configured()
+53 -4
View File
@@ -1,11 +1,12 @@
import { describe, expect, test } from "bun:test"
import os from "os"
import path from "path"
import { Effect, Layer } from "effect"
import { Effect, Layer, PubSub, Stream } from "effect"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { FileSystem } from "@opencode-ai/core/filesystem"
import { Protected } from "@opencode-ai/core/filesystem/protected"
import { FileSystemSearch } from "@opencode-ai/core/filesystem/search"
import { Watcher } from "@opencode-ai/core/filesystem/watcher"
import { Location } from "@opencode-ai/core/location"
import { Ripgrep } from "@opencode-ai/core/ripgrep"
import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
@@ -33,15 +34,14 @@ describe("FileSystemSearch", () => {
find: (input) =>
Effect.gen(function* () {
observed = input
if (input.onEntry)
yield* input.onEntry(FileSystem.Entry.make({ path: RelativePath.make("src/index.ts"), type: "file" }))
return []
return [FileSystem.Entry.make({ path: RelativePath.make("src/index.ts"), type: "file" })]
}),
glob: () => Effect.succeed([]),
grep: () => Effect.succeed([]),
}),
),
],
[Watcher.node, Watcher.testLayer],
])
await Effect.runPromise(
@@ -56,4 +56,53 @@ describe("FileSystemSearch", () => {
}).pipe(Effect.provide(layer), Effect.scoped),
)
})
test("refreshes the ripgrep index after files change", async () => {
const root = AbsolutePath.make(path.join(os.tmpdir(), "opencode-search-refresh"))
let scans = 0
const updates = Effect.runSync(PubSub.unbounded<Watcher.Update>())
const layer = AppNodeBuilder.build(FileSystemSearch.node, [
[Location.node, Layer.succeed(Location.Service, Location.Service.of(location({ directory: root })))],
[
Ripgrep.node,
Layer.succeed(
Ripgrep.Service,
Ripgrep.Service.of({
find: () =>
Effect.sync(() => {
scans++
return [
FileSystem.Entry.make({ path: RelativePath.make("src/old.ts"), type: "file" }),
...(scans > 1
? [FileSystem.Entry.make({ path: RelativePath.make("src/new.ts"), type: "file" })]
: []),
]
}),
glob: () => Effect.succeed([]),
grep: () => Effect.succeed([]),
}),
),
],
[
Watcher.node,
Layer.succeed(
Watcher.Service,
Watcher.Service.of({ subscribe: () => Effect.succeed(Stream.fromPubSub(updates)) }),
),
],
])
await Effect.runPromise(
Effect.gen(function* () {
const search = yield* FileSystemSearch.Service
expect((yield* search.find({ query: "new", type: "file" })).length).toBe(0)
yield* PubSub.publish(
updates,
{ type: "create", path: path.join(root, "src/new.ts") } satisfies Watcher.Update,
)
yield* Effect.sleep("100 millis")
expect((yield* search.find({ query: "new", type: "file" }))[0]?.path).toBe(RelativePath.make("src/new.ts"))
}).pipe(Effect.provide(layer), Effect.scoped),
)
})
})