File tree Expand file tree Collapse file tree
scorpio.ai/src/Memory/Service Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,7 +7,11 @@ export interface SettingsCrudOptions {
77 label ?: string ;
88 checkOnUpdate ?: boolean ;
99 checkOnDelete ?: boolean ;
10- beforeDelete ?: ( id : string ) => void ;
10+ /**
11+ * 删除流程在 config 落库 *之前* 调用——此时 profile 仍在 settings 里,可以走 resolver
12+ * 拉服务、做带 lifecycle 的清理(如 service.markForDeletion 触发 store.deleteAll)。
13+ */
14+ beforeDelete ?: ( id : string ) => Promise < void > | void ;
1115 afterDelete ?: ( id : string ) => Promise < void > | void ;
1216 afterSave ?: ( id : string ) => Promise < void > | void ;
1317 createReturn ?: ( id : string , body : any ) => any ;
@@ -49,9 +53,9 @@ export class SettingsCrudHelper {
4953
5054 app . delete ( `/api/settings/${ section } /:id` , api ( async req => {
5155 const id = req . params . id as string ;
52- opts ?. beforeDelete ?.( id ) ;
5356 const map = getSection ( ) ;
5457 if ( checkOnDelete && ! map [ id ] ) throwBad ( `${ label } "${ id } " not found` ) ;
58+ await opts ?. beforeDelete ?.( id ) ;
5559 delete map [ id ] ;
5660 config . saveSettings ( ) ;
5761 await opts ?. afterDelete ?.( id ) ;
Original file line number Diff line number Diff line change @@ -118,13 +118,13 @@ export class SettingsRoutes {
118118 // 配置变更(writerModel / prompt 等)不强制丢弃缓存实例 —— 那会让同 id 同时跑两个
119119 // live MemoryService,破坏 store 数据。已 acquire 的实例继续吃旧配置,等 refCount
120120 // 归零自然 evict,下次 acquire 才拿新配置。
121- afterDelete : async ( id ) => {
121+ //
122+ // 走 beforeDelete 而不是 afterDelete —— markForDeletion 内部 acquire 需要 resolver
123+ // 仍能解析 profile 配置;afterDelete 时 config 已被删,acquire 会 throw。
124+ // markForDeletion 已包含 cache miss / hit 两种情况下的 deleteAll 触发,无需 fallback。
125+ beforeDelete : async ( id ) => {
122126 const { memoryServicePool } = await import ( '../../Memory/MemoryServicePool' ) ;
123- // 有活实例 → 打标记,teardown 时由 store.deleteAll() 删
124- if ( memoryServicePool . markForDeletion ( id ) ) return ;
125- // 无活实例 → 直接 rm(profile 删了,无人会再 acquire)
126- const fs = await import ( 'fs' ) ;
127- try { fs . rmSync ( config . getMemoryPath ( id ) , { recursive : true , force : true } ) ; } catch { }
127+ await memoryServicePool . markForDeletion ( id ) ;
128128 } ,
129129 } ) ;
130130 settingsCrudHelper . register ( app , 'agendaProfiles' , {
Original file line number Diff line number Diff line change @@ -113,18 +113,19 @@ export class MemoryServicePool {
113113 }
114114
115115 /**
116- * 标记 service 在 refCount 归零 teardown 时执行 store.deleteAll()。
117- * 返回 true = 已标记;false = cache 没命中(caller 自处理 fs.rm)。
116+ * profile 删除路径专用:标记 service 在 refCount 归零 teardown 时执行 store.deleteAll()。
118117 *
119- * 不走 acquire/release —— markForDeletion 只是设置 deleteOnTeardown 这个 boolean,
120- * 不需要持有引用。cache 里活着的 service refCount 必然 > 0(归零会立即 evict),
121- * 标记后等所有 caller 自然 release 到归零,service.release() 内部统一处理 deleteAll。
118+ * 必须在 settings 还没把 profile 从 config 中移除之前调用(resolver 仍能解析),
119+ * 走 beforeDelete 而不是 afterDelete。流程:
120+ * - cache 命中:incRef → 设 flag → release,service 仍被其他 caller 持有,他们归零时触发 deleteAll
121+ * - cache miss:build 出新实例 → 设 flag → 立即 release 归零 → service.release() 内部触发 deleteAll
122+ *
123+ * 统一一条 lifecycle 路径,caller 不需要写 fs.rm fallback。
122124 */
123- markForDeletion ( memoryId : string ) : boolean {
124- const service = this . cache . get ( memoryId ) ;
125- if ( ! service ) return false ;
126- service . markForDeletion ( ) ;
127- return true ;
125+ async markForDeletion ( memoryId : string ) : Promise < void > {
126+ const service = ( await this . acquire ( memoryId ) ) as MemoryService ;
127+ try { service . markForDeletion ( ) ; }
128+ finally { service . release ( ) ; }
128129 }
129130
130131 /** Service 自驱逐:refCount 归零 teardown 完成后由 service.release() 调进来。 */
You can’t perform that action at this time.
0 commit comments