Skip to content

Commit cae205a

Browse files
fix(core): bound fuzzy search memory (anomalyco#42741)
Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com>
1 parent d35c6f0 commit cae205a

2 files changed

Lines changed: 92 additions & 21 deletions

File tree

‎packages/core/src/filesystem/search.ts‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ export type Options = typeof Options.Type
2323
export class Service extends Context.Service<Service, Interface>()("@opencode/FileSystem/Search") {}
2424

2525
const REFRESH_INTERVAL = Duration.toMillis("10 seconds")
26+
type Prepared = ReturnType<typeof fuzzysort.prepare>
27+
28+
function emptyIndex() {
29+
return { files: new Map<string, Prepared>(), directories: new Map<string, Prepared>() }
30+
}
31+
32+
function search(index: ReturnType<typeof emptyIndex>, input: FileSystem.FindInput) {
33+
const items =
34+
input.type === "file"
35+
? Array.from(index.files.values())
36+
: input.type === "directory"
37+
? Array.from(index.directories.values())
38+
: [...index.files.values(), ...index.directories.values()]
39+
const result = fuzzysort.go(input.query, items, { limit: input.limit ?? 50 })
40+
// Targets are owned by the current location index. The only global fuzzysort
41+
// state left is its query cache, which must not retain every query forever.
42+
fuzzysort.cleanup()
43+
return result.map((item) => {
44+
const relative = item.target
45+
const type = relative.endsWith(path.sep) ? ("directory" as const) : ("file" as const)
46+
return FileSystem.Entry.make({
47+
path: RelativePath.make(relative),
48+
type,
49+
})
50+
})
51+
}
2652

2753
export const ripgrepLayer = Layer.effect(
2854
Service,
@@ -32,12 +58,13 @@ export const ripgrepLayer = Layer.effect(
3258
const scope = yield* Scope.Scope
3359
const clock = yield* Clock.Clock
3460
const home = Protected.isHome(location.directory)
35-
let index = { files: [] as string[], directories: new Set<string>() }
61+
let index = emptyIndex()
3662
let initialized = false
3763
let settledAt = Number.NEGATIVE_INFINITY
3864
let refreshing = false
3965
const scan = Effect.gen(function* () {
40-
const next = { files: [] as string[], directories: new Set<string>() }
66+
const next = emptyIndex()
67+
const previous = index
4168
if (!initialized) index = next
4269
yield* ripgrep.find({
4370
cwd: location.directory,
@@ -46,11 +73,13 @@ export const ripgrepLayer = Layer.effect(
4673
exclude: home ? [...Protected.names()].map((name) => `${name}/**`) : undefined,
4774
onEntry: (entry) =>
4875
Effect.sync(() => {
49-
next.files.push(entry.path)
76+
next.files.set(entry.path, previous.files.get(entry.path) ?? fuzzysort.prepare(entry.path))
5077
const parts = entry.path.split("/")
51-
parts
52-
.slice(0, -1)
53-
.forEach((_, offset) => next.directories.add(parts.slice(0, offset + 1).join("/") + path.sep))
78+
parts.slice(0, -1).forEach((_, offset) => {
79+
const directory = parts.slice(0, offset + 1).join("/") + path.sep
80+
if (!next.directories.has(directory))
81+
next.directories.set(directory, previous.directories.get(directory) ?? fuzzysort.prepare(directory))
82+
})
5483
}),
5584
})
5685
index = next
@@ -74,20 +103,7 @@ export const ripgrepLayer = Layer.effect(
74103
find: (input) =>
75104
Effect.gen(function* () {
76105
yield* refresh
77-
const items =
78-
input.type === "file"
79-
? index.files
80-
: input.type === "directory"
81-
? Array.from(index.directories)
82-
: [...index.files, ...index.directories]
83-
return fuzzysort.go(input.query, items, { limit: input.limit ?? 50 }).map((item) => {
84-
const relative = item.target
85-
const type = relative.endsWith(path.sep) ? ("directory" as const) : ("file" as const)
86-
return FileSystem.Entry.make({
87-
path: RelativePath.make(relative),
88-
type,
89-
})
90-
})
106+
return search(index, input)
91107
}),
92108
})
93109
}),

‎packages/core/test/filesystem/search.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { describe, expect, test } from "bun:test"
1+
import { describe, expect, spyOn, test } from "bun:test"
2+
import fuzzysort from "fuzzysort"
23
import os from "os"
34
import path from "path"
45
import { Deferred, Effect, Layer } from "effect"
@@ -123,4 +124,58 @@ describe("FileSystemSearch", () => {
123124
}).pipe(Effect.provide(layer), Effect.provide(TestClock.layer()), Effect.scoped),
124125
)
125126
})
127+
128+
test("reuses location-owned fuzzy targets across index refreshes", async () => {
129+
let scans = 0
130+
const first = Effect.runSync(Deferred.make<void>())
131+
const second = Effect.runSync(Deferred.make<void>())
132+
const prepare = spyOn(fuzzysort, "prepare")
133+
const cleanup = spyOn(fuzzysort, "cleanup")
134+
const layer = AppNodeBuilder.build(FileSystemSearch.node, [
135+
[
136+
Location.node,
137+
Layer.succeed(
138+
Location.Service,
139+
Location.Service.of(
140+
location({ directory: AbsolutePath.make(path.join(os.tmpdir(), "opencode-search-cache")) }),
141+
),
142+
),
143+
],
144+
[
145+
Ripgrep.node,
146+
Layer.succeed(
147+
Ripgrep.Service,
148+
Ripgrep.Service.of({
149+
find: (input) =>
150+
Effect.gen(function* () {
151+
scans++
152+
const entry = FileSystem.Entry.make({ path: RelativePath.make("src/index.ts"), type: "file" })
153+
if (input.onEntry) yield* input.onEntry(entry)
154+
yield* Deferred.succeed(scans === 1 ? first : second, undefined)
155+
return [entry]
156+
}),
157+
glob: () => Effect.succeed([]),
158+
grep: () => Effect.succeed([]),
159+
}),
160+
),
161+
],
162+
])
163+
164+
await Effect.runPromise(
165+
Effect.gen(function* () {
166+
const search = yield* FileSystemSearch.Service
167+
yield* Deferred.await(first)
168+
yield* search.find({ query: "index", type: "file" })
169+
yield* TestClock.adjust("10 seconds")
170+
yield* search.find({ query: "index", type: "file" })
171+
yield* Deferred.await(second)
172+
yield* search.find({ query: "index", type: "file" })
173+
174+
expect(prepare).toHaveBeenCalledTimes(2)
175+
expect(cleanup).toHaveBeenCalledTimes(3)
176+
}).pipe(Effect.provide(layer), Effect.provide(TestClock.layer()), Effect.scoped),
177+
)
178+
prepare.mockRestore()
179+
cleanup.mockRestore()
180+
})
126181
})

0 commit comments

Comments
 (0)