@@ -277,34 +277,19 @@ class SessionObserverManager {
277277 } catch { }
278278 }
279279
280- private sessionLearnings : SessionLearning [ ] = [ ]
281-
282280 public async recordLearning (
283281 learning : Omit < SessionLearning , "id" | "timestamp" > ,
284282 workspaceDir ?: string ,
285283 ) : Promise < SessionLearning > {
286284 const normalizedText = learning . lesson . trim ( ) . toLowerCase ( )
287- const existingIndex = this . sessionLearnings . findIndex (
288- ( l ) => l . lesson . trim ( ) . toLowerCase ( ) === normalizedText ,
289- )
290- if ( existingIndex !== - 1 ) {
291- // Update existing entry timestamp and return without duplicating
292- const existing = this . sessionLearnings [ existingIndex ]
293- existing . timestamp = new Date ( ) . toISOString ( )
294- return existing
295- }
296-
285+ const paths = learningStorePaths ( workspaceDir )
286+ const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
297287 const entry : SessionLearning = {
298288 id : `learn_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 7 ) } ` ,
299289 timestamp : new Date ( ) . toISOString ( ) ,
300290 ...learning ,
301291 }
302- this . sessionLearnings . unshift ( entry )
303- if ( this . sessionLearnings . length > 100 ) this . sessionLearnings . pop ( )
304-
305- // Persist to the global and project stores, serialised against forgetLearning.
306- const paths = learningStorePaths ( workspaceDir )
307- const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
292+ let stored = entry
308293 await this . mutate ( async ( ) => {
309294 for ( const file of files ) {
310295 try {
@@ -314,26 +299,29 @@ class SessionObserverManager {
314299 const parsed = JSON . parse ( await fs . readFile ( file , "utf8" ) )
315300 if ( Array . isArray ( parsed ) ) existing = parsed
316301 } catch { }
317- if ( ! existing . some ( ( e ) => e . lesson . trim ( ) . toLowerCase ( ) === normalizedText ) ) {
318- await writeJsonAtomic ( file , [ entry , ...existing ] . slice ( 0 , 100 ) )
302+ const found = existing . find ( ( e ) => e . lesson . trim ( ) . toLowerCase ( ) === normalizedText )
303+ if ( found ) {
304+ // Re-recording refreshes recency but never duplicates.
305+ found . timestamp = new Date ( ) . toISOString ( )
306+ stored = found
307+ await writeJsonAtomic ( file , existing . slice ( 0 , 100 ) )
308+ continue
319309 }
310+ await writeJsonAtomic ( file , [ entry , ...existing ] . slice ( 0 , 100 ) )
320311 } catch { }
321312 }
322313 } )
323- return entry
314+ return stored
324315 }
325316
326317 /**
327318 * The rules actually selected for injection, in the same order the prompt
328319 * builder uses. Shared so the listed `injected` flag cannot drift from reality.
329320 */
330321 private async selectInjected ( limit : number , workspaceDir ?: string ) : Promise < SessionLearning [ ] > {
331- if ( this . sessionLearnings . length > 0 ) {
332- return this . sessionLearnings . slice ( 0 , limit )
333- }
334- // Read through the mutation queue: a read that starts while a forget is still
335- // writing must not reload the store it is about to forget from.
336- const loaded = await this . mutate ( async ( ) => {
322+ // Always read from disk, from the right stores for THIS workspace. One process
323+ // can serve several projects, so no cached list may be reused across them.
324+ return this . mutate ( async ( ) => {
337325 const read = async ( file ?: string ) : Promise < SessionLearning [ ] > => {
338326 if ( ! file ) return [ ]
339327 try {
@@ -354,18 +342,8 @@ class SessionObserverManager {
354342 seen . add ( key )
355343 merged . push ( rule )
356344 }
357- return merged
345+ return merged . slice ( 0 , limit )
358346 } )
359- if ( loaded . length > 0 ) {
360- // Merge, never clobber: a rule recorded while the read was pending must not
361- // be dropped from the cache by the older file contents.
362- const seen = new Set ( loaded . map ( learningKey ) )
363- this . sessionLearnings = [
364- ...this . sessionLearnings . filter ( ( l ) => ! seen . has ( learningKey ( l ) ) ) ,
365- ...loaded ,
366- ]
367- }
368- return this . sessionLearnings . slice ( 0 , limit )
369347 }
370348
371349 public async getRecentLearnings ( limit = 5 , workspaceDir ?: string ) : Promise < SessionLearning [ ] > {
@@ -422,7 +400,6 @@ class SessionObserverManager {
422400 const needle = lessonText . trim ( ) . toLowerCase ( )
423401 if ( ! needle ) return 0
424402 const matches = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . toLowerCase ( ) . includes ( needle )
425- this . sessionLearnings = this . sessionLearnings . filter ( ( l ) => ! matches ( l ) )
426403 const paths = learningStorePaths ( workspaceDir )
427404 const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
428405 const removed = new Set < string > ( )
0 commit comments