Skip to content

Commit b35b599

Browse files
Ambient Code Botclaude
andcommitted
fix(skill): invalidate skill cache when SKILL.md files change on disk
Skills were loaded once at startup and cached permanently — editing a SKILL.md file while opencode was running had no effect until restart. Add cache invalidation triggered by the existing FileWatcher infrastructure: - Skill cache clears when a SKILL.md file changes (via Bus subscription) - Command cache rebuilds when skills reload (via Skill.Event.Invalidated) - Expose Skill.invalidate() for programmatic use Fixes anomalyco#19050 ## Summary - Fix skill cache not invalidating when SKILL.md files are edited on disk - Add `Skill.Event.Invalidated` bus event for cross-service coordination - Invalidate Command cache when skills reload so slash commands reflect changes ## Test plan - [x] 4 new regression tests (edit, get, add, delete) - [x] All 11 existing skill tests pass - [x] Full test suite: 1468 pass, 0 fail - [x] Type checking passes Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 4167e25 commit b35b599

3 files changed

Lines changed: 249 additions & 2 deletions

File tree

‎packages/opencode/src/command/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Bus } from "@/bus"
12
import { BusEvent } from "@/bus/bus-event"
23
import { InstanceState } from "@/effect/instance-state"
34
import { makeRunPromise } from "@/effect/run-service"
@@ -159,6 +160,12 @@ export namespace Command {
159160

160161
const cache = yield* InstanceState.make<State>((ctx) => init(ctx))
161162

163+
// Fix for #19050: rebuild commands when skills are reloaded from disk
164+
Bus.subscribe(Skill.Event.Invalidated, async () => {
165+
await Effect.runPromise(InstanceState.invalidate(cache)).catch(() => {})
166+
log.info("invalidated command cache after skill reload")
167+
})
168+
162169
const get = Effect.fn("Command.get")(function* (name: string) {
163170
const state = yield* InstanceState.get(cache)
164171
return state.commands[name]

‎packages/opencode/src/skill/index.ts‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { Effect, Layer, ServiceMap } from "effect"
66
import { NamedError } from "@opencode-ai/util/error"
77
import type { Agent } from "@/agent/agent"
88
import { Bus } from "@/bus"
9+
import { BusEvent } from "@/bus/bus-event"
910
import { InstanceState } from "@/effect/instance-state"
1011
import { makeRunPromise } from "@/effect/run-service"
12+
import { FileWatcher } from "@/file/watcher"
1113
import { Flag } from "@/flag/flag"
1214
import { Global } from "@/global"
1315
import { Permission } from "@/permission"
@@ -20,6 +22,10 @@ import { Discovery } from "./discovery"
2022

2123
export namespace Skill {
2224
const log = Log.create({ service: "skill" })
25+
export const Event = {
26+
Invalidated: BusEvent.define("skill.invalidated", z.object({})),
27+
}
28+
2329
const EXTERNAL_DIRS = [".claude", ".agents"]
2430
const EXTERNAL_SKILL_PATTERN = "skills/**/SKILL.md"
2531
const OPENCODE_SKILL_PATTERN = "{skill,skills}/**/SKILL.md"
@@ -59,13 +65,15 @@ export namespace Skill {
5965

6066
type Cache = State & {
6167
ensure: () => Promise<void>
68+
invalidate: () => void
6269
}
6370

6471
export interface Interface {
6572
readonly get: (name: string) => Effect.Effect<Info | undefined>
6673
readonly all: () => Effect.Effect<Info[]>
6774
readonly dirs: () => Effect.Effect<string[]>
6875
readonly available: (agent?: Agent.Info) => Effect.Effect<Info[]>
76+
readonly invalidate: () => Effect.Effect<void>
6977
}
7078

7179
const add = async (state: State, match: string) => {
@@ -175,7 +183,15 @@ export namespace Skill {
175183
return state.task
176184
}
177185

178-
return { ...state, ensure }
186+
// Fix for #19050: allow cache invalidation so skills are re-read from disk
187+
const invalidate = () => {
188+
state.task = undefined
189+
for (const key of Object.keys(state.skills)) delete state.skills[key]
190+
state.dirs.clear()
191+
log.info("invalidated skill cache")
192+
}
193+
194+
return { ...state, ensure, invalidate }
179195
}
180196

181197
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Skill") {}
@@ -188,6 +204,15 @@ export namespace Skill {
188204
Effect.fn("Skill.state")((ctx) => Effect.sync(() => create(discovery, ctx.directory, ctx.worktree))),
189205
)
190206

207+
// Fix for #19050: invalidate skill cache when SKILL.md files change on disk
208+
Bus.subscribe(FileWatcher.Event.Updated, async (evt) => {
209+
if (!evt.properties.file.endsWith("SKILL.md")) return
210+
const cache = await Effect.runPromise(InstanceState.get(state)).catch(() => undefined)
211+
if (!cache) return
212+
cache.invalidate()
213+
Bus.publish(Event.Invalidated, {})
214+
})
215+
191216
const ensure = Effect.fn("Skill.ensure")(function* () {
192217
const cache = yield* InstanceState.get(state)
193218
yield* Effect.promise(() => cache.ensure())
@@ -216,7 +241,12 @@ export namespace Skill {
216241
return list.filter((skill) => Permission.evaluate("skill", skill.name, agent.permission).action !== "deny")
217242
})
218243

219-
return Service.of({ get, all, dirs, available })
244+
const invalidate = Effect.fn("Skill.invalidate")(function* () {
245+
const cache = yield* InstanceState.get(state)
246+
cache.invalidate()
247+
})
248+
249+
return Service.of({ get, all, dirs, available, invalidate })
220250
}),
221251
)
222252

@@ -259,4 +289,8 @@ export namespace Skill {
259289
export async function available(agent?: Agent.Info) {
260290
return runPromise((skill) => skill.available(agent))
261291
}
292+
293+
export async function invalidate() {
294+
return runPromise((skill) => skill.invalidate())
295+
}
262296
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import { afterEach, test, expect } from "bun:test"
2+
import { Skill } from "../../src/skill"
3+
import { Instance } from "../../src/project/instance"
4+
import { tmpdir } from "../fixture/fixture"
5+
import path from "path"
6+
7+
afterEach(async () => {
8+
await Instance.disposeAll()
9+
})
10+
11+
test("skill content updates after invalidate + re-read", async () => {
12+
await using tmp = await tmpdir({
13+
git: true,
14+
init: async (dir) => {
15+
const skillDir = path.join(dir, ".opencode", "skill", "my-skill")
16+
await Bun.write(
17+
path.join(skillDir, "SKILL.md"),
18+
`---
19+
name: my-skill
20+
description: Original description.
21+
---
22+
23+
# My Skill - Version 1
24+
25+
Original content here.
26+
`,
27+
)
28+
},
29+
})
30+
31+
await Instance.provide({
32+
directory: tmp.path,
33+
fn: async () => {
34+
// First load — should see "Version 1"
35+
const skillsBefore = await Skill.all()
36+
expect(skillsBefore.length).toBe(1)
37+
expect(skillsBefore[0].description).toBe("Original description.")
38+
expect(skillsBefore[0].content).toContain("Version 1")
39+
40+
// Edit the skill file on disk
41+
const skillFile = path.join(tmp.path, ".opencode", "skill", "my-skill", "SKILL.md")
42+
await Bun.write(
43+
skillFile,
44+
`---
45+
name: my-skill
46+
description: Updated description.
47+
---
48+
49+
# My Skill - Version 2
50+
51+
Updated content with test1 label.
52+
`,
53+
)
54+
55+
// Invalidate the cache (simulates what FileWatcher triggers)
56+
await Skill.invalidate()
57+
58+
// Now skills should reflect the updated file
59+
const skillsAfter = await Skill.all()
60+
expect(skillsAfter.length).toBe(1)
61+
expect(skillsAfter[0].description).toBe("Updated description.")
62+
expect(skillsAfter[0].content).toContain("Version 2")
63+
expect(skillsAfter[0].content).toContain("test1 label")
64+
},
65+
})
66+
})
67+
68+
test("Skill.get() returns fresh content after invalidate", async () => {
69+
await using tmp = await tmpdir({
70+
git: true,
71+
init: async (dir) => {
72+
const skillDir = path.join(dir, ".opencode", "skill", "cached-skill")
73+
await Bun.write(
74+
path.join(skillDir, "SKILL.md"),
75+
`---
76+
name: cached-skill
77+
description: A cached skill.
78+
---
79+
80+
# Cached Skill
81+
82+
Initial instructions.
83+
`,
84+
)
85+
},
86+
})
87+
88+
await Instance.provide({
89+
directory: tmp.path,
90+
fn: async () => {
91+
const before = await Skill.get("cached-skill")
92+
expect(before).toBeDefined()
93+
expect(before!.content).toContain("Initial instructions")
94+
95+
// Edit the file
96+
const skillFile = path.join(tmp.path, ".opencode", "skill", "cached-skill", "SKILL.md")
97+
await Bun.write(
98+
skillFile,
99+
`---
100+
name: cached-skill
101+
description: An updated cached skill.
102+
---
103+
104+
# Cached Skill
105+
106+
Updated instructions with new workflow.
107+
`,
108+
)
109+
110+
// Invalidate and re-fetch
111+
await Skill.invalidate()
112+
113+
const after = await Skill.get("cached-skill")
114+
expect(after).toBeDefined()
115+
expect(after!.content).toContain("Updated instructions")
116+
expect(after!.description).toBe("An updated cached skill.")
117+
},
118+
})
119+
})
120+
121+
test("newly added skill is discovered after invalidate", async () => {
122+
await using tmp = await tmpdir({
123+
git: true,
124+
init: async (dir) => {
125+
const skillDir = path.join(dir, ".opencode", "skill", "existing-skill")
126+
await Bun.write(
127+
path.join(skillDir, "SKILL.md"),
128+
`---
129+
name: existing-skill
130+
description: Already exists.
131+
---
132+
133+
# Existing Skill
134+
`,
135+
)
136+
},
137+
})
138+
139+
await Instance.provide({
140+
directory: tmp.path,
141+
fn: async () => {
142+
const before = await Skill.all()
143+
expect(before.length).toBe(1)
144+
145+
// Add a new skill on disk
146+
const newSkillDir = path.join(tmp.path, ".opencode", "skill", "new-skill")
147+
await Bun.write(
148+
path.join(newSkillDir, "SKILL.md"),
149+
`---
150+
name: new-skill
151+
description: Brand new skill.
152+
---
153+
154+
# New Skill
155+
`,
156+
)
157+
158+
// Invalidate and re-fetch
159+
await Skill.invalidate()
160+
161+
const after = await Skill.all()
162+
expect(after.length).toBe(2)
163+
expect(after.find((s) => s.name === "existing-skill")).toBeDefined()
164+
expect(after.find((s) => s.name === "new-skill")).toBeDefined()
165+
expect(after.find((s) => s.name === "new-skill")!.description).toBe("Brand new skill.")
166+
},
167+
})
168+
})
169+
170+
test("deleted skill is removed after invalidate", async () => {
171+
await using tmp = await tmpdir({
172+
git: true,
173+
init: async (dir) => {
174+
const skillDir = path.join(dir, ".opencode", "skill", "doomed-skill")
175+
await Bun.write(
176+
path.join(skillDir, "SKILL.md"),
177+
`---
178+
name: doomed-skill
179+
description: Will be deleted.
180+
---
181+
182+
# Doomed Skill
183+
`,
184+
)
185+
},
186+
})
187+
188+
await Instance.provide({
189+
directory: tmp.path,
190+
fn: async () => {
191+
const before = await Skill.all()
192+
expect(before.length).toBe(1)
193+
expect(before[0].name).toBe("doomed-skill")
194+
195+
// Delete the skill file
196+
const fs = await import("fs/promises")
197+
await fs.rm(path.join(tmp.path, ".opencode", "skill", "doomed-skill"), { recursive: true })
198+
199+
// Invalidate and re-fetch
200+
await Skill.invalidate()
201+
202+
const after = await Skill.all()
203+
expect(after.length).toBe(0)
204+
},
205+
})
206+
})

0 commit comments

Comments
 (0)