Skip to content

Commit 4a006b1

Browse files
committed
refactor(core): derive config watches from entries
1 parent 2db7ccb commit 4a006b1

2 files changed

Lines changed: 69 additions & 19 deletions

File tree

‎packages/core/src/config.ts‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ export class Directory extends Schema.Class<Directory>("Config.Directory")({
119119
path: AbsolutePath,
120120
}) {}
121121

122+
export class File extends Schema.Class<File>("Config.File")({
123+
type: Schema.Literal("file"),
124+
path: AbsolutePath,
125+
}) {}
126+
122127
export class AgentsDirectory extends Schema.Class<AgentsDirectory>("Config.AgentsDirectory")({
123128
type: Schema.Literal("agents"),
124129
path: AbsolutePath,
@@ -129,7 +134,7 @@ export class ClaudeDirectory extends Schema.Class<ClaudeDirectory>("Config.Claud
129134
path: AbsolutePath,
130135
}) {}
131136

132-
export type Entry = Document | Directory | AgentsDirectory | ClaudeDirectory
137+
export type Entry = Document | Directory | File | AgentsDirectory | ClaudeDirectory
133138

134139
export function latest<K extends keyof Info>(entries: readonly Entry[], key: K): Info[K] | undefined {
135140
return entries
@@ -138,7 +143,7 @@ export function latest<K extends keyof Info>(entries: readonly Entry[], key: K):
138143
}
139144

140145
export interface Interface {
141-
/** Returns location config documents and supplemental directories from lowest to highest priority. */
146+
/** Returns location config documents and discovery sources from lowest to highest priority. */
142147
readonly entries: () => Effect.Effect<Entry[]>
143148
}
144149

@@ -227,31 +232,36 @@ const layer = Layer.effect(
227232
const directPaths = discovered
228233
.filter((item) => ![".agents", ".claude", ".opencode"].includes(path.basename(item)))
229234
.toReversed()
230-
const direct = yield* Effect.forEach(directPaths, loadFile).pipe(
235+
const direct = yield* Effect.forEach(directPaths, (filepath) =>
236+
loadFile(filepath).pipe(
237+
Effect.map((config) => [
238+
...(config ? [config] : []),
239+
new File({ type: "file", path: AbsolutePath.make(filepath) }),
240+
]),
241+
),
242+
).pipe(
231243
Effect.orDie,
232-
Effect.map((configs) => configs.filter((config): config is Document => config !== undefined)),
244+
Effect.map((entries) => entries.flat()),
233245
)
234246

235247
const supplementary = yield* Effect.forEach(directories, loadDirectory).pipe(Effect.orDie)
236-
return {
237-
entries: [...claude, ...agents, ...(supplementary[0] ?? []), ...direct, ...supplementary.slice(1).flat()],
238-
directories: [...directories, ...claude.map((entry) => entry.path), ...agents.map((entry) => entry.path)],
239-
files: directPaths,
240-
}
248+
return [...claude, ...agents, ...(supplementary[0] ?? []), ...direct, ...supplementary.slice(1).flat()]
241249
})
242250

243251
const initial = yield* discover()
244-
let configs = initial.entries
252+
let configs = initial
245253
const updates = yield* PubSub.unbounded<Watcher.Update>()
246254
const subscriptions = new Map<string, Effect.Effect<unknown>>()
247-
const targets = (snapshot: typeof initial) => [
248-
...snapshot.directories.map((path) => ({ path, type: "directory" as const })),
249-
...snapshot.files
250-
.filter((file) => !snapshot.directories.some((directory) => FSUtil.contains(directory, file)))
251-
.map((path) => ({ path, type: "file" as const })),
252-
]
253-
const reconcile = Effect.fn("Config.reconcileWatches")(function* (snapshot: typeof initial) {
254-
const next = new Map(targets(snapshot).map((target) => [JSON.stringify(target), target]))
255+
const reconcile = Effect.fn("Config.reconcileWatches")(function* (entries: readonly Entry[]) {
256+
const directories = entries.flatMap((entry) => (entry.type === "directory" ? [entry.path] : []))
257+
const files = entries.flatMap((entry) => (entry.type === "file" ? [entry.path] : []))
258+
const targets = [
259+
...directories.map((path) => ({ path, type: "directory" as const })),
260+
...files
261+
.filter((file) => !directories.some((directory) => FSUtil.contains(directory, file)))
262+
.map((path) => ({ path, type: "file" as const })),
263+
]
264+
const next = new Map(targets.map((target) => [JSON.stringify(target), target]))
255265
for (const [key, stop] of subscriptions) {
256266
if (next.has(key)) continue
257267
yield* stop
@@ -272,7 +282,7 @@ const layer = Layer.effect(
272282
Stream.runForEach((update) =>
273283
Effect.gen(function* () {
274284
const next = yield* discover()
275-
configs = next.entries
285+
configs = next
276286
yield* reconcile(next)
277287
yield* events.publish(ConfigSchema.Event.Updated, {})
278288
}).pipe(Effect.catchCause((cause) => Effect.logError("failed to reload config", { path: update.path, cause }))),

‎packages/core/test/config/config.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,43 @@ describe("Config", () => {
254254
),
255255
)
256256

257+
it.live("does not watch ecosystem config roots", () =>
258+
Effect.acquireRelease(
259+
Effect.promise(() => tmpdir()),
260+
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
261+
).pipe(
262+
Effect.flatMap((tmp) =>
263+
Effect.gen(function* () {
264+
yield* Effect.promise(() =>
265+
Promise.all([
266+
fs.mkdir(path.join(tmp.path, ".claude", "skills"), { recursive: true }),
267+
fs.mkdir(path.join(tmp.path, ".agents"), { recursive: true }),
268+
]),
269+
)
270+
const targets: Watcher.WatchInput[] = []
271+
const watcher = Layer.succeed(
272+
Watcher.Service,
273+
Watcher.Service.of({
274+
subscribe: (input) => {
275+
targets.push(input)
276+
return Stream.never
277+
},
278+
}),
279+
)
280+
281+
return yield* Effect.gen(function* () {
282+
const config = yield* Config.Service
283+
yield* config.entries()
284+
285+
expect(targets).toEqual([
286+
{ type: "directory", path: AbsolutePath.make(path.join(tmp.path, "global")) },
287+
])
288+
}).pipe(Effect.provide(testLayer(tmp.path, undefined, undefined, undefined, watcher)))
289+
}),
290+
),
291+
),
292+
)
293+
257294
it.live("loads opencode JSON and JSONC files from lowest to highest priority", () =>
258295
Effect.acquireRelease(
259296
Effect.promise(() => tmpdir()),
@@ -915,8 +952,11 @@ describe("Config", () => {
915952
"global",
916953
AbsolutePath.make(global),
917954
"root",
955+
AbsolutePath.make(path.join(root, "opencode.json")),
918956
"parent",
957+
AbsolutePath.make(path.join(parent, "opencode.jsonc")),
919958
"directory",
959+
AbsolutePath.make(path.join(directory, "opencode.json")),
920960
"root-dot",
921961
AbsolutePath.make(path.join(root, ".opencode")),
922962
"directory-dot",

0 commit comments

Comments
 (0)