Skip to content

Commit 9f2526f

Browse files
fix(core): close skill-refresh review findings
Addresses code-review findings on the SkillV2 no-restart skill refresh (commit 6860c42, issue anomalyco#37426): - Add a 60-minute cache-clear timer for url-type sources only; there is no push notification for arbitrary HTTP endpoints, so this is the one automatic way to observe a changed remote catalog while running. Directory sources remain purely event-driven via the existing finalize-triggered watch sync. - Fix a scope/finalizer leak: each directory watch's Effect.acquireRelease finalizer was tied to the service's single long-lived scope, and manually running the stored unsubscribe on removal never deregistered it. Directory watches now use a per-watch child scope (Scope.fork/Scope.close), which Effect detaches from the parent on close instead of accumulating. - Fix symlinked directory sources never invalidating: invalidateDirectory now compares against the watched directory's resolved real path (native watcher backends report events using the resolved path), matching the existing realpath handling for git directories in this same file. - Fix a native watch-handle leak in watcher.ts's watch() when subscribe() resolves after the 10s timeout, mirroring the existing subscribe() cleanup path. - Fix a cache-resurrection race: an in-flight list() load could resurrect a cache entry a concurrent reconcile had just invalidated; list() now only caches its result if no reconcile ran while the load was in flight. - Note the intentional duplication between this file's OPENCODE_DISABLE_EXTERNAL_SKILLS/OPENCODE_DISABLE_CLAUDE_CODE_SKILLS and packages/opencode's RuntimeFlags equivalents (core cannot depend on opencode, so both must be kept in sync manually). - Add tests: url-source refresh via the periodic timer (using TestClock), and an end-to-end symlinked-directory invalidation test against the real filesystem watcher (previously only a stubbed watcher was exercised). bun typecheck and bun test (packages/core, 1080 pass) are green. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent 6860c42 commit 9f2526f

4 files changed

Lines changed: 169 additions & 23 deletions

File tree

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,22 @@ const layer = Layer.effect(
109109

110110
const watch = (directory: string, ignore: string[] = []) =>
111111
Effect.acquireRelease(
112-
Effect.promise(() => w.subscribe(directory, callback, { ignore, backend })).pipe(
113-
Effect.timeout(SUBSCRIBE_TIMEOUT_MS),
114-
Effect.map((subscription) => Effect.promise(() => subscription.unsubscribe()).pipe(Effect.ignore)),
115-
Effect.catchCause((cause) =>
116-
Effect.logError("failed to watch directory", { directory, cause: Cause.pretty(cause) }).pipe(
117-
Effect.as(Effect.void),
118-
),
119-
),
120-
),
112+
Effect.suspend(() => {
113+
const pending = w.subscribe(directory, callback, { ignore, backend })
114+
return Effect.promise(() => pending).pipe(
115+
Effect.timeout(SUBSCRIBE_TIMEOUT_MS),
116+
Effect.map((subscription) => Effect.promise(() => subscription.unsubscribe()).pipe(Effect.ignore)),
117+
Effect.catchCause((cause) => {
118+
// A late-resolving subscribe() after the timeout must still be
119+
// unsubscribed, or its native watch handle leaks for the
120+
// process lifetime (mirrors subscribe() above).
121+
pending.then((subscription) => subscription.unsubscribe()).catch(() => {})
122+
return Effect.logError("failed to watch directory", { directory, cause: Cause.pretty(cause) }).pipe(
123+
Effect.as(Effect.void),
124+
)
125+
}),
126+
)
127+
}),
121128
(unsubscribe) => unsubscribe,
122129
)
123130

‎packages/core/src/flag/flag.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export const Flag = {
5454
get OPENCODE_DISABLE_PROJECT_CONFIG() {
5555
return truthy("OPENCODE_DISABLE_PROJECT_CONFIG")
5656
},
57+
// Mirrors packages/opencode/src/effect/runtime-flags.ts's
58+
// disableExternalSkills/disableClaudeCodeSkills (same env vars, V1 skill
59+
// discovery). Core cannot depend on opencode's Effect Config-based
60+
// RuntimeFlags, so this reads process.env directly like the rest of this
61+
// file; keep both definitions' semantics in sync if either changes.
5762
get OPENCODE_DISABLE_EXTERNAL_SKILLS() {
5863
return truthy("OPENCODE_DISABLE_EXTERNAL_SKILLS")
5964
},

‎packages/core/src/skill.ts‎

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

33
import { makeLocationNode } from "./effect/app-node"
44
import path from "path"
5-
import { Context, Effect, Layer, Scope, Schema, Stream, Types } from "effect"
5+
import { Context, Effect, Exit, Layer, Schedule, Scope, Schema, Stream, Types } from "effect"
66
import { Skill } from "@opencode-ai/schema/skill"
77
import { AgentV2 } from "./agent"
88
import { ConfigMarkdown } from "./config/markdown"
@@ -64,7 +64,14 @@ const layer = Layer.effect(
6464

6565
const watcher = yield* Watcher.Service
6666
const cache = new Map<string, Info[]>()
67-
const watches = new Map<string, Watcher.Unsubscribe>
67+
const watches = new Map<string, Scope.Closeable>()
68+
// Directory sources are watched by their real path (parcel-watcher/native
69+
// backends report events using the resolved path), so a symlinked source
70+
// (e.g. a dotfiles-managed ~/.claude) still matches on invalidation.
71+
const realPaths = new Map<string, string>()
72+
// Bumped on every reconcile so an in-flight list() load started before a
73+
// reconcile doesn't resurrect a cache entry that reconcile just cleared.
74+
let generation = 0
6875

6976
const invalidate = (source: Source) => {
7077
cache.delete(Source.key(source))
@@ -73,34 +80,43 @@ const layer = Layer.effect(
7380
const invalidateDirectory = (file: string) => {
7481
for (const source of state.get().sources) {
7582
if (source.type !== "directory") continue
76-
if (!file.startsWith(source.path + path.sep)) continue
83+
const real = realPaths.get(source.path) ?? source.path
84+
if (!file.startsWith(real + path.sep)) continue
7785
invalidate(source)
7886
}
7987
}
8088

8189
const syncWatches = (sources: ReadonlyArray<Source>) =>
8290
Effect.gen(function* () {
8391
const directories = new Set(sources.filter((s) => s.type === "directory").map((s) => s.path as string))
84-
for (const [directory, unsubscribe] of watches) {
92+
for (const [directory, childScope] of watches) {
8593
if (directories.has(directory)) continue
8694
watches.delete(directory)
87-
yield* unsubscribe
95+
realPaths.delete(directory)
96+
// Closing the child scope detaches it from the parent `scope`, so
97+
// this doesn't leave a stale finalizer behind on repeated
98+
// add/remove cycles the way manually running `unsubscribe` did.
99+
yield* Scope.close(childScope, Exit.void)
88100
}
89101
for (const directory of directories) {
90102
if (watches.has(directory)) continue
91-
const unsubscribe = yield* watcher.watch(directory)
92-
watches.set(directory, unsubscribe)
103+
const real = yield* fs.realPath(directory).pipe(Effect.catch(() => Effect.succeed(directory)))
104+
realPaths.set(directory, real)
105+
const childScope = yield* Scope.fork(scope)
106+
yield* watcher.watch(directory).pipe(Scope.provide(childScope))
107+
watches.set(directory, childScope)
93108
}
94109
})
95110

96111
const reconcile = (sources: ReadonlyArray<Source>) =>
97112
Effect.gen(function* () {
113+
generation++
98114
const keys = new Set(sources.map(Source.key))
99115
for (const key of cache.keys()) {
100116
if (!keys.has(key)) cache.delete(key)
101117
}
102118
yield* syncWatches(sources)
103-
}).pipe(Effect.provideService(Scope.Scope, scope))
119+
})
104120

105121
const state = State.create<Data, Draft>({
106122
initial: () => ({ sources: [] }),
@@ -114,17 +130,24 @@ const layer = Layer.effect(
114130
finalize: (draft) => reconcile(draft.list()),
115131
})
116132

117-
yield* Effect.addFinalizer(() =>
118-
Effect.forEach(watches.values(), (unsubscribe) => unsubscribe, { discard: true }),
119-
)
120-
121133
const events = yield* EventV2.Service
122134

123135
yield* events.subscribe(Watcher.Event.Updated).pipe(
124136
Stream.runForEach((event) => Effect.sync(() => invalidateDirectory(event.data.file))),
125137
Effect.forkScoped({ startImmediately: true }),
126138
)
127139

140+
// HTTP/URL sources have no push-based change notification, so this is the
141+
// only automatic way to observe a changed remote catalog while running.
142+
yield* Effect.repeat(
143+
Effect.sync(() => {
144+
for (const source of state.get().sources) {
145+
if (source.type === "url") cache.delete(Source.key(source))
146+
}
147+
}),
148+
Schedule.spaced("60 minutes"),
149+
).pipe(Effect.forkScoped({ startImmediately: true }))
150+
128151
const load = Effect.fn("SkillV2.load")(function* (source: Source) {
129152
const skills: Info[] = []
130153
if (source.type === "embedded") return [source.skill]
@@ -161,10 +184,14 @@ const layer = Layer.effect(
161184

162185
const list = Effect.fn("SkillV2.list")(function* () {
163186
const skills = new Map<string, Info>()
187+
const startGeneration = generation
164188
for (const source of state.get().sources) {
165189
const key = Source.key(source)
166190
const loaded = cache.get(key) ?? (yield* load(source))
167-
cache.set(key, loaded)
191+
// Only cache the result if no reconcile ran while this load was in
192+
// flight; otherwise a stale load could resurrect a cache entry that
193+
// reconcile just invalidated for a removed/changed source.
194+
if (generation === startGeneration) cache.set(key, loaded)
168195
for (const skill of loaded) skills.set(skill.name, skill)
169196
}
170197
return Array.from(skills.values())

‎packages/core/test/skill.test.ts‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import fs from "fs/promises"
22
import path from "path"
33
import { describe, expect } from "bun:test"
4-
import { Effect, Layer } from "effect"
4+
import { Duration, Effect, Layer } from "effect"
5+
import * as TestClock from "effect/testing/TestClock"
56
import { AgentV2 } from "@opencode-ai/core/agent"
7+
import { Config } from "@opencode-ai/core/config"
68
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
79
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
810
import { Database } from "@opencode-ai/core/database/database"
911
import { EventV2 } from "@opencode-ai/core/event"
1012
import { FSUtil } from "@opencode-ai/core/fs-util"
1113
import { Watcher } from "@opencode-ai/core/filesystem/watcher"
14+
import { Location } from "@opencode-ai/core/location"
1215
import { AbsolutePath } from "@opencode-ai/core/schema"
1316
import { SkillV2 } from "@opencode-ai/core/skill"
1417
import { SkillDiscovery } from "@opencode-ai/core/skill/discovery"
18+
import { location } from "./fixture/location"
1519
import { tmpdir } from "./fixture/tmpdir"
1620
import { testEffect } from "./lib/effect"
1721

@@ -231,4 +235,107 @@ describe("SkillV2", () => {
231235
),
232236
),
233237
)
238+
239+
it.effect("refreshes URL source cache on the periodic timer", () =>
240+
Effect.gen(function* () {
241+
const url = "https://example.test/refresh/"
242+
pulls = 0
243+
urls.set(url, [])
244+
245+
const skill = yield* SkillV2.Service
246+
yield* skill.transform((editor) => editor.source({ type: "url", url }))
247+
248+
expect(yield* skill.list()).toEqual([])
249+
expect(pulls).toBe(1)
250+
251+
const dir = yield* Effect.promise(() => tmpdir())
252+
yield* Effect.promise(async () => {
253+
await fs.mkdir(path.join(dir.path, "deploy"), { recursive: true })
254+
await write(dir.path, "deploy", "Deploy production")
255+
})
256+
// Simulate the remote catalog changing with no local config/plugin reload.
257+
urls.set(url, [AbsolutePath.make(dir.path)])
258+
259+
// Still cached until the periodic refresh fires.
260+
expect(yield* skill.list()).toEqual([])
261+
expect(pulls).toBe(1)
262+
263+
yield* TestClock.adjust(Duration.minutes(60))
264+
yield* Effect.yieldNow
265+
266+
expect((yield* skill.list()).map((item) => item.name)).toEqual(["deploy"])
267+
expect(pulls).toBe(2)
268+
269+
yield* Effect.promise(() => dir[Symbol.asyncDispose]())
270+
}),
271+
)
272+
})
273+
274+
const describeRealWatcher = Watcher.hasNativeBinding() && !process.env.CI ? describe : describe.skip
275+
276+
describeRealWatcher("SkillV2 with a real filesystem watcher", () => {
277+
// A separate, minimal `it` is required here: the file-level `it` already
278+
// bakes a stubbed Watcher.node into its base layer (for the other tests
279+
// above), and providing a second, real Watcher.node per-test on top of that
280+
// base does not take effect. Mirrors the pattern in
281+
// test/filesystem/watcher.test.ts, whose shared `it` has no Watcher.node
282+
// (or SkillV2.node) in its base layer either.
283+
const itReal = testEffect(Layer.empty)
284+
285+
const configLayer = Layer.succeed(Config.Service, Config.Service.of({ entries: () => Effect.succeed([]) }))
286+
287+
function provideRealWatcher(directory: string) {
288+
return Effect.provide(
289+
AppNodeBuilder.build(LayerNode.group([SkillV2.node, AgentV2.node, Database.node, EventV2.node]), [
290+
[SkillDiscovery.node, discovery],
291+
[Config.node, configLayer],
292+
[
293+
Location.node,
294+
Layer.succeed(Location.Service, Location.Service.of(location({ directory: AbsolutePath.make(directory) }))),
295+
],
296+
]),
297+
)
298+
}
299+
300+
itReal.live(
301+
"invalidates a symlinked directory source through the real watcher",
302+
() =>
303+
Effect.acquireRelease(
304+
Effect.promise(() => tmpdir()),
305+
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
306+
).pipe(
307+
Effect.flatMap((tmp) =>
308+
Effect.gen(function* () {
309+
const real = path.join(tmp.path, "real-skills")
310+
const link = path.join(tmp.path, "linked-skills")
311+
yield* Effect.promise(async () => {
312+
await fs.mkdir(path.join(real, "review"), { recursive: true })
313+
await write(real, "review", "First")
314+
await fs.symlink(real, link)
315+
})
316+
317+
const skill = yield* SkillV2.Service
318+
yield* skill.transform((editor) => editor.source({ type: "directory", path: AbsolutePath.make(link) }))
319+
expect((yield* skill.list()).map((item) => item.description)).toEqual(["First"])
320+
321+
yield* Effect.promise(async () => {
322+
await fs.mkdir(path.join(real, "review"), { recursive: true })
323+
await write(real, "review", "Second")
324+
})
325+
326+
yield* Effect.gen(function* () {
327+
let attempt = 0
328+
while (attempt < 20) {
329+
const list = yield* skill.list()
330+
if (list.some((item) => item.description === "Second")) return
331+
yield* Effect.sleep("250 millis")
332+
attempt++
333+
}
334+
return yield* Effect.fail(new Error("timed out waiting for the real watcher to invalidate the cache"))
335+
})
336+
}).pipe(provideRealWatcher(tmp.path)),
337+
),
338+
),
339+
10000,
340+
)
234341
})

0 commit comments

Comments
 (0)