Skip to content

Commit e5ef00b

Browse files
authored
fix(core): bound project filesystem watches (#41096)
1 parent 917d644 commit e5ef00b

5 files changed

Lines changed: 239 additions & 220 deletions

File tree

‎packages/app/src/pages/session.tsx‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ export default function Page() {
688688
return {
689689
queryKey: [...vcsKey(), mode] as const,
690690
enabled,
691+
refetchOnMount: "always" as const,
692+
refetchOnWindowFocus: true,
691693
queryFn: mode
692694
? () =>
693695
sdk()
@@ -701,6 +703,16 @@ export default function Page() {
701703
}
702704
})
703705
const refreshVcs = debounce(() => void queryClient.invalidateQueries({ queryKey: vcsKey() }), 100)
706+
createEffect(
707+
on(
708+
() => desktopReviewOpen() || mobileChanges(),
709+
(open, previous) => {
710+
if (!open || previous || !desktopFileTreeOpen() || vcsQuery.isFetching) return
711+
refreshVcs()
712+
},
713+
{ defer: true },
714+
),
715+
)
704716
const reviewDiffs = () => {
705717
if (reviewMode() === "git" || reviewMode() === "branch")
706718
// avoids suspense
@@ -947,19 +959,6 @@ export default function Page() {
947959
),
948960
)
949961

950-
const stopVcs = sdk().event.listen((evt) => {
951-
const details = evt.details as { type: string; properties?: unknown }
952-
if (details.type !== "file.watcher.updated" && details.type !== "filesystem.changed") return
953-
const props =
954-
typeof details.properties === "object" && details.properties
955-
? (details.properties as Record<string, unknown>)
956-
: undefined
957-
const file = typeof props?.file === "string" ? props.file : undefined
958-
if (!file || file.startsWith(".git/")) return
959-
refreshVcs()
960-
})
961-
onCleanup(stopVcs)
962-
963962
createEffect(
964963
on(
965964
() => sdk().directory,

‎packages/core/src/filesystem/location-watcher.ts‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ import { FSUtil } from "@opencode-ai/util/fs-util"
1111
import { Git } from "../git"
1212
import { Location } from "../location"
1313
import { Watcher } from "./watcher"
14-
import { Ignore } from "./ignore"
15-
import { Protected } from "./protected"
16-
17-
function protecteds(dir: string) {
18-
return Protected.paths().filter((item) => {
19-
const relative = path.relative(dir, item)
20-
return relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative)
21-
})
22-
}
2314

2415
export interface Interface {}
2516

@@ -44,30 +35,14 @@ const layer = Layer.effect(
4435
const config = (yield* configService.entries())
4536
.filter((entry): entry is Document => entry.type === "document")
4637
.flatMap((item) => item.info.watcher?.ignore ?? [])
47-
const home = Protected.isHome(location.directory)
48-
49-
if (!home && location.vcs) {
50-
const updates = yield* watcher.subscribe({
51-
path: location.directory,
52-
type: "directory",
53-
ignore: [...Ignore.PATTERNS, ...config, ...protecteds(location.directory)],
54-
})
55-
yield* updates.pipe(Stream.runForEach(publish), Effect.forkScoped)
56-
}
57-
if (home) {
58-
yield* Effect.logInfo("location watcher skipped home directory", { directory: location.directory })
59-
}
6038

6139
if (location.vcs?.type === "git") {
6240
const resolved = (yield* git.repo.discover(location.directory))?.gitDirectory
6341
const vcs = resolved
6442
? yield* fs.realPath(resolved).pipe(Effect.catch(() => Effect.succeed(resolved)))
6543
: undefined
6644
if (vcs && !config.includes(".git") && !config.includes(vcs) && (!resolved || !config.includes(resolved))) {
67-
const ignore = (yield* fs.readDirectoryEntries(vcs).pipe(Effect.catch(() => Effect.succeed([])))).flatMap(
68-
(entry) => (entry.name === "HEAD" ? [] : [entry.name]),
69-
)
70-
const updates = yield* watcher.subscribe({ path: vcs, type: "directory", ignore })
45+
const updates = yield* watcher.subscribe({ path: path.join(vcs, "HEAD"), type: "file" })
7146
yield* updates.pipe(Stream.runForEach(publish), Effect.forkScoped)
7247
}
7348
}

‎packages/core/src/skill.ts‎

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ export * as Skill from "./skill"
22

33
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
44
import path from "path"
5-
import { Context, Effect, Layer, Schema, Scope, Stream, Types } from "effect"
6-
import { FileSystem } from "@opencode-ai/schema/filesystem"
5+
import { Context, Effect, FiberMap, Layer, PubSub, Schema, Semaphore, Stream, Types } from "effect"
76
import { Skill } from "@opencode-ai/schema/skill"
87
import { Agent } from "./agent"
98
import { ConfigMarkdown } from "./config/markdown"
@@ -83,47 +82,78 @@ const layer = Layer.effect(
8382
const fs = yield* FSUtil.Service
8483
const bus = yield* Bus.Service
8584
const watcher = yield* Watcher.Service
86-
const scope = yield* Scope.Scope
8785
const cache = new Map<string, { skills: Info[]; paths: readonly string[] }>()
88-
const watched = new Set<string>()
86+
const watches = yield* FiberMap.make<string>()
87+
const lock = Semaphore.makeUnsafe(1)
88+
const changes = yield* PubSub.unbounded<string>()
8989

9090
const invalidate = Effect.fn("Skill.invalidateFromWatcher")(function* (file: string) {
91-
const invalidated = Array.from(cache.entries()).filter(([, loaded]) =>
92-
loaded.paths.some((item) => FSUtil.overlaps(item, file)),
91+
const changed = yield* lock.withPermit(
92+
Effect.gen(function* () {
93+
const invalidated = Array.from(cache.entries()).filter(([, loaded]) =>
94+
loaded.paths.some((item) => FSUtil.overlaps(item, file)),
95+
)
96+
if (invalidated.length === 0) return false
97+
cache.clear()
98+
yield* FiberMap.clear(watches)
99+
yield* Effect.logInfo("skill cache invalidated", {
100+
file,
101+
sources: invalidated.map(([key]) => key),
102+
skills: invalidated.flatMap(([, loaded]) => loaded.skills.map((skill) => skill.id)),
103+
})
104+
return true
105+
}),
93106
)
94-
if (invalidated.length === 0) return
95-
for (const [key] of invalidated) cache.delete(key)
96-
yield* Effect.logInfo("skill cache invalidated", {
97-
file,
98-
sources: invalidated.map(([key]) => key),
99-
skills: invalidated.flatMap(([, loaded]) => loaded.skills.map((skill) => skill.id)),
100-
})
107+
if (!changed) return
101108
yield* bus.publish(Skill.Event.Updated, {}).pipe(Effect.asVoid)
102109
})
103110

104-
const watch = Effect.fn("Skill.watch")(function* (directory: string) {
111+
yield* Stream.fromPubSub(changes).pipe(Stream.runForEach(invalidate), Effect.forkScoped({ startImmediately: true }))
112+
113+
const watch = Effect.fn("Skill.watch")(function* (directory: string, type: Watcher.WatchInput["type"]) {
105114
const target = path.resolve(directory)
106-
if (watched.has(target)) return
107-
watched.add(target)
108-
const updates = yield* watcher.subscribe({ path: target, type: "directory" })
109-
yield* updates.pipe(
110-
Stream.runForEach((update) => invalidate(update.path)),
111-
Effect.forkIn(scope, { startImmediately: true }),
115+
const updates = yield* watcher.subscribe(
116+
type === "file" ? { path: target, type: "file" } : { path: target, type: "directory" },
117+
)
118+
yield* FiberMap.run(
119+
watches,
120+
`${type}:${target}`,
121+
updates.pipe(Stream.runForEach((update) => PubSub.publish(changes, update.path).pipe(Effect.asVoid))),
122+
{
123+
onlyIfMissing: true,
124+
startImmediately: true,
125+
},
112126
)
113127
})
114128

115-
const watchDirectory = Effect.fn("Skill.watchDirectory")(function* (directory: string) {
129+
function firstMissing(target: string): Effect.Effect<string | undefined> {
130+
const parent = path.dirname(target)
131+
if (parent === target) return Effect.succeed(undefined)
132+
return fs.isDir(parent).pipe(Effect.flatMap((exists) => (exists ? Effect.succeed(target) : firstMissing(parent))))
133+
}
134+
135+
const watchDirectory: (directory: string) => Effect.Effect<string[]> = Effect.fn("Skill.watchDirectory")(function* (
136+
directory: string,
137+
) {
116138
const target = path.resolve(directory)
117139
const resolved = yield* fs.realPath(directory).pipe(Effect.catch(() => Effect.succeed(undefined)))
118140
if (resolved) {
119-
yield* watch(resolved)
141+
yield* watch(resolved, "directory")
120142
if (resolved !== target) {
121-
yield* watch(path.dirname(target))
143+
yield* watch(target, "file")
122144
}
123145
return resolved === target ? [target] : [target, resolved]
124146
}
125-
if (yield* fs.isDir(path.dirname(target))) {
126-
yield* watch(path.dirname(target))
147+
const missing = yield* firstMissing(target)
148+
if (missing) yield* watch(missing, "file")
149+
if (
150+
yield* fs.realPath(directory).pipe(
151+
Effect.as(true),
152+
Effect.catch(() => Effect.succeed(false)),
153+
)
154+
) {
155+
if (missing) yield* FiberMap.remove(watches, `file:${path.resolve(missing)}`)
156+
return yield* watchDirectory(directory)
127157
}
128158
return [target]
129159
})
@@ -139,7 +169,9 @@ const layer = Layer.effect(
139169
list: () => draft.sources as Source[],
140170
}),
141171
finalize: () =>
142-
Effect.sync(() => cache.clear()).pipe(Effect.andThen(bus.publish(Skill.Event.Updated, {})), Effect.asVoid),
172+
lock
173+
.withPermit(FiberMap.clear(watches).pipe(Effect.andThen(Effect.sync(() => cache.clear())), Effect.asVoid))
174+
.pipe(Effect.andThen(bus.publish(Skill.Event.Updated, {})), Effect.asVoid),
143175
})
144176

145177
const load = Effect.fn("Skill.load")(function* (source: Source) {
@@ -165,7 +197,7 @@ const layer = Layer.effect(
165197
if (!roots.some((root) => FSUtil.contains(root, resolved))) {
166198
const external = path.dirname(resolved)
167199
paths.push(external)
168-
yield* watch(external)
200+
yield* watch(external, "directory")
169201
}
170202
const content = yield* fs.readFileStringSafe(filepath).pipe(Effect.catch(() => Effect.succeed(undefined)))
171203
if (!content) continue
@@ -197,20 +229,19 @@ const layer = Layer.effect(
197229
return { skills, paths }
198230
})
199231

200-
yield* bus.subscribe(FileSystem.Event.Changed).pipe(
201-
Stream.runForEach((event) => invalidate(event.data.file)),
202-
Effect.forkScoped({ startImmediately: true }),
203-
)
204-
205232
const list = Effect.fn("Skill.list")(function* () {
206-
const skills = new Map<ID, Info>()
207-
for (const source of state.get().sources) {
208-
const key = Source.key(source)
209-
const loaded = cache.get(key) ?? (yield* load(source))
210-
cache.set(key, loaded)
211-
for (const skill of loaded.skills) skills.set(skill.id, skill)
212-
}
213-
return Array.from(skills.values())
233+
return yield* lock.withPermit(
234+
Effect.gen(function* () {
235+
const skills = new Map<ID, Info>()
236+
for (const source of state.get().sources) {
237+
const key = Source.key(source)
238+
const loaded = cache.get(key) ?? (yield* load(source))
239+
cache.set(key, loaded)
240+
for (const skill of loaded.skills) skills.set(skill.id, skill)
241+
}
242+
return Array.from(skills.values())
243+
}),
244+
)
214245
})
215246

216247
return Service.of({

0 commit comments

Comments
 (0)