@@ -2,8 +2,7 @@ export * as Skill from "./skill"
22
33import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
44import 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"
76import { Skill } from "@opencode-ai/schema/skill"
87import { Agent } from "./agent"
98import { 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