@@ -292,6 +292,7 @@ class SessionObserverManager {
292292 let stored = entry
293293 await this . mutate ( async ( ) => {
294294 for ( const file of files ) {
295+ const scope : "global" | "project" = file === paths . project ? "project" : "global"
295296 try {
296297 await fs . mkdir ( path . dirname ( file ) , { recursive : true } )
297298 let existing : SessionLearning [ ] = [ ]
@@ -302,13 +303,18 @@ class SessionObserverManager {
302303 const found = existing . find ( ( e ) => e . lesson . trim ( ) . toLowerCase ( ) === normalizedText )
303304 if ( found ) {
304305 // Re-recording refreshes recency AND moves the rule to the front, so it
305- // re-enters the injection window; it never duplicates.
306+ // re-enters the injection window; it never duplicates. It also clears any
307+ // tombstone, which is how a forgotten rule is restored.
306308 found . timestamp = new Date ( ) . toISOString ( )
309+ found . scope = scope
310+ found . deletedAt = undefined
311+ found . deletedBy = undefined
307312 stored = found
308- await writeJsonAtomic ( file , [ found , ...existing . filter ( ( e ) => e !== found ) ] . slice ( 0 , 100 ) )
313+ await writeJsonAtomic ( file , trimRules ( [ found , ...existing . filter ( ( e ) => e !== found ) ] ) )
309314 continue
310315 }
311- await writeJsonAtomic ( file , [ entry , ...existing ] . slice ( 0 , 100 ) )
316+ await writeJsonAtomic ( file , trimRules ( [ { ...entry , scope } , ...existing ] ) )
317+ stored = { ...entry , scope }
312318 } catch { }
313319 }
314320 } )
@@ -337,7 +343,7 @@ class SessionObserverManager {
337343 // global note (AIMETH-008 - shared repo facts win). One entry per rule text.
338344 const seen = new Set < string > ( )
339345 const merged : SessionLearning [ ] = [ ]
340- for ( const rule of [ ...( await read ( paths . project ) ) , ...( await read ( paths . global ) ) ] ) {
346+ for ( const rule of [ ...( await read ( paths . project ) ) , ...( await read ( paths . global ) ) ] . filter ( isLive ) ) {
341347 const key = learningKey ( rule )
342348 if ( seen . has ( key ) ) continue
343349 seen . add ( key )
@@ -379,8 +385,8 @@ class SessionObserverManager {
379385 }
380386 }
381387 const paths = learningStorePaths ( workspaceDir )
382- const global = await read ( paths . global )
383- const project = await read ( paths . project )
388+ const global = ( await read ( paths . global ) ) . filter ( isLive )
389+ const project = ( await read ( paths . project ) ) . filter ( isLive )
384390 const globalKeys = new Set ( global . map ( learningKey ) )
385391 const merged = [
386392 ...global . map ( ( l ) => ( { ...l , origin : "global" as const } ) ) ,
@@ -393,49 +399,66 @@ class SessionObserverManager {
393399 }
394400
395401 /**
396- * Remove rules whose text matches (case-insensitive substring) from memory and
397- * from both persisted stores. Returns the number of distinct rules removed; a
398- * rule present in both stores is counted once.
402+ * Forget rules whose text matches (case-insensitive substring): mark them deleted
403+ * (tombstone) in both persisted stores. Returns the number of distinct rules newly
404+ * forgotten; a rule present in both stores is counted once.
399405 */
400406 public async forgetLearning ( lessonText : string , workspaceDir ?: string ) : Promise < number > {
401407 const needle = lessonText . trim ( ) . toLowerCase ( )
402408 if ( ! needle ) return 0
403409 const matches = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . toLowerCase ( ) . includes ( needle )
404410 const paths = learningStorePaths ( workspaceDir )
405411 const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
406- const removed = new Set < string > ( )
407- for ( const file of files ) {
408- for ( const key of await this . dropRules ( file , matches ) ) removed . add ( key )
409- }
410- return removed . size
412+ // One mutation for both stores, so a record cannot interleave between them.
413+ return this . mutate ( async ( ) => {
414+ const results : Array < { keys : Set < string > ; ok : boolean } > = [ ]
415+ for ( const file of files ) results . push ( await this . applyTombstones ( file , matches ) )
416+ // If a store could not be read or written, we cannot claim its copy is gone, so
417+ // report nothing rather than a count that may be wrong.
418+ if ( results . some ( ( r ) => ! r . ok ) ) return 0
419+ const forgotten = new Set < string > ( )
420+ for ( const result of results ) for ( const key of result . keys ) forgotten . add ( key )
421+ return forgotten . size
422+ } )
411423 }
412424
413425 /**
414- * Remove matching rules from one store atomically and count them. A removal is
415- * only reported when the write landed; a failed write returns nothing and the
416- * file is left untouched.
426+ * Mark matching live rules as deleted in one store. The rows are kept as
427+ * tombstones so a later merge or sync cannot resurrect them. Non-queuing: the
428+ * caller must already hold the mutation lock. Returns the keys acted on and
429+ * whether the read and write both succeeded; an absent store is not a failure.
417430 */
418- private dropRules ( file : string , matches : ( l : SessionLearning ) => boolean ) : Promise < Set < string > > {
419- return this . mutate ( async ( ) => {
420- const found = new Set < string > ( )
421- try {
422- const parsed = JSON . parse ( await fs . readFile ( file , "utf8" ) )
423- if ( ! Array . isArray ( parsed ) ) return found
424- const kept : SessionLearning [ ] = [ ]
425- for ( const row of parsed as SessionLearning [ ] ) {
426- if ( matches ( row ) ) {
427- found . add ( String ( row . lesson ?? "" ) . trim ( ) . toLowerCase ( ) )
428- continue
429- }
430- kept . push ( row )
431- }
432- if ( found . size === 0 ) return found
433- await writeJsonAtomic ( file , kept . slice ( 0 , 100 ) )
434- } catch {
435- return new Set < string > ( )
431+ private async applyTombstones (
432+ file : string ,
433+ matches : ( l : SessionLearning ) => boolean ,
434+ ) : Promise < { keys : Set < string > ; ok : boolean } > {
435+ const keys = new Set < string > ( )
436+ let raw : string
437+ try {
438+ raw = await fs . readFile ( file , "utf8" )
439+ } catch ( error ) {
440+ // Only a missing store is a successful absence; any other read error means we
441+ // cannot know what the store holds.
442+ const code = error && typeof error === "object" && "code" in error ? ( error as { code ?: unknown } ) . code : undefined
443+ return { keys, ok : code === "ENOENT" }
444+ }
445+ try {
446+ const parsed = JSON . parse ( raw )
447+ if ( ! Array . isArray ( parsed ) ) return { keys, ok : false }
448+ const rows = parsed as SessionLearning [ ]
449+ let changed = false
450+ for ( const row of rows ) {
451+ if ( ! isLive ( row ) || ! matches ( row ) ) continue
452+ row . deletedAt = new Date ( ) . toISOString ( )
453+ row . deletedBy = process . env . USERNAME || process . env . USER || "local"
454+ keys . add ( learningKey ( row ) )
455+ changed = true
436456 }
437- return found
438- } )
457+ if ( changed ) await writeJsonAtomic ( file , trimRules ( rows ) )
458+ return { keys, ok : true }
459+ } catch {
460+ return { keys, ok : false }
461+ }
439462 }
440463
441464 public getLatestRetrospectives ( ) : SessionRetrospective [ ] {
@@ -461,6 +484,23 @@ export function learningStorePaths(workspaceDir?: string): { global?: string; pr
461484
462485const learningKey = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . trim ( ) . toLowerCase ( )
463486
487+ /** A rule is live unless it carries a tombstone. */
488+ const isLive = ( l : SessionLearning ) => ! l . deletedAt
489+
490+ const MAX_LIVE_RULES = 100
491+ const MAX_TOMBSTONES = 100
492+
493+ /**
494+ * Bound file size without evicting tombstones: a dropped tombstone would let a
495+ * later merge resurrect the rule it records as deleted.
496+ */
497+ function trimRules ( rows : SessionLearning [ ] ) : SessionLearning [ ] {
498+ return [
499+ ...rows . filter ( isLive ) . slice ( 0 , MAX_LIVE_RULES ) ,
500+ ...rows . filter ( ( r ) => ! isLive ( r ) ) . slice ( 0 , MAX_TOMBSTONES ) ,
501+ ]
502+ }
503+
464504/** Write JSON via a temp file + rename so a reader never sees a half-written file. */
465505async function writeJsonAtomic ( file : string , rows : SessionLearning [ ] ) : Promise < void > {
466506 const tmp = `${ file } .${ process . pid } .${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 7 ) } .tmp`
@@ -474,6 +514,11 @@ export interface SessionLearning {
474514 lesson : string
475515 context ?: string
476516 source : "user_feedback" | "buddy_review" | "auto_correction" | "session_retro"
517+ /** Which store this row belongs to. */
518+ scope ?: "global" | "project"
519+ /** Tombstone: set when the rule is forgotten, so a later merge cannot resurrect it. */
520+ deletedAt ?: string
521+ deletedBy ?: string
477522}
478523
479524export const sessionObserver = new SessionObserverManager ( )
0 commit comments