@@ -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,17 @@ 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
308313 await writeJsonAtomic ( file , [ found , ...existing . filter ( ( e ) => e !== found ) ] . slice ( 0 , 100 ) )
309314 continue
310315 }
311- await writeJsonAtomic ( file , [ entry , ...existing ] . slice ( 0 , 100 ) )
316+ await writeJsonAtomic ( file , [ { ... entry , scope } , ...existing ] . slice ( 0 , 100 ) )
312317 } catch { }
313318 }
314319 } )
@@ -337,7 +342,7 @@ class SessionObserverManager {
337342 // global note (AIMETH-008 - shared repo facts win). One entry per rule text.
338343 const seen = new Set < string > ( )
339344 const merged : SessionLearning [ ] = [ ]
340- for ( const rule of [ ...( await read ( paths . project ) ) , ...( await read ( paths . global ) ) ] ) {
345+ for ( const rule of [ ...( await read ( paths . project ) ) , ...( await read ( paths . global ) ) ] . filter ( isLive ) ) {
341346 const key = learningKey ( rule )
342347 if ( seen . has ( key ) ) continue
343348 seen . add ( key )
@@ -379,8 +384,8 @@ class SessionObserverManager {
379384 }
380385 }
381386 const paths = learningStorePaths ( workspaceDir )
382- const global = await read ( paths . global )
383- const project = await read ( paths . project )
387+ const global = ( await read ( paths . global ) ) . filter ( isLive )
388+ const project = ( await read ( paths . project ) ) . filter ( isLive )
384389 const globalKeys = new Set ( global . map ( learningKey ) )
385390 const merged = [
386391 ...global . map ( ( l ) => ( { ...l , origin : "global" as const } ) ) ,
@@ -393,9 +398,9 @@ class SessionObserverManager {
393398 }
394399
395400 /**
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.
401+ * Forget rules whose text matches (case-insensitive substring): mark them deleted
402+ * (tombstone) in both persisted stores. Returns the number of distinct rules newly
403+ * forgotten; a rule present in both stores is counted once.
399404 */
400405 public async forgetLearning ( lessonText : string , workspaceDir ?: string ) : Promise < number > {
401406 const needle = lessonText . trim ( ) . toLowerCase ( )
@@ -405,32 +410,33 @@ class SessionObserverManager {
405410 const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
406411 const removed = new Set < string > ( )
407412 for ( const file of files ) {
408- for ( const key of await this . dropRules ( file , matches ) ) removed . add ( key )
413+ for ( const key of await this . tombstoneRules ( file , matches ) ) removed . add ( key )
409414 }
410415 return removed . size
411416 }
412417
413418 /**
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 .
419+ * Mark matching live rules as deleted in one store, atomically. The rows are kept
420+ * as tombstones so a later merge or sync cannot resurrect them. A tombstone is only
421+ * reported when the write landed; a failed write returns nothing .
417422 */
418- private dropRules ( file : string , matches : ( l : SessionLearning ) => boolean ) : Promise < Set < string > > {
423+ private tombstoneRules ( file : string , matches : ( l : SessionLearning ) => boolean ) : Promise < Set < string > > {
419424 return this . mutate ( async ( ) => {
420425 const found = new Set < string > ( )
421426 try {
422427 const parsed = JSON . parse ( await fs . readFile ( file , "utf8" ) )
423428 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 )
429+ const rows = parsed as SessionLearning [ ]
430+ let changed = false
431+ for ( const row of rows ) {
432+ if ( ! isLive ( row ) || ! matches ( row ) ) continue
433+ row . deletedAt = new Date ( ) . toISOString ( )
434+ row . deletedBy = process . env . USERNAME || process . env . USER || "local"
435+ found . add ( learningKey ( row ) )
436+ changed = true
431437 }
432- if ( found . size === 0 ) return found
433- await writeJsonAtomic ( file , kept . slice ( 0 , 100 ) )
438+ if ( ! changed ) return found
439+ await writeJsonAtomic ( file , rows . slice ( 0 , 100 ) )
434440 } catch {
435441 return new Set < string > ( )
436442 }
@@ -461,6 +467,9 @@ export function learningStorePaths(workspaceDir?: string): { global?: string; pr
461467
462468const learningKey = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . trim ( ) . toLowerCase ( )
463469
470+ /** A rule is live unless it carries a tombstone. */
471+ const isLive = ( l : SessionLearning ) => ! l . deletedAt
472+
464473/** Write JSON via a temp file + rename so a reader never sees a half-written file. */
465474async function writeJsonAtomic ( file : string , rows : SessionLearning [ ] ) : Promise < void > {
466475 const tmp = `${ file } .${ process . pid } .${ Math . random ( ) . toString ( 36 ) . slice ( 2 , 7 ) } .tmp`
@@ -474,6 +483,11 @@ export interface SessionLearning {
474483 lesson : string
475484 context ?: string
476485 source : "user_feedback" | "buddy_review" | "auto_correction" | "session_retro"
486+ /** Which store this row belongs to. */
487+ scope ?: "global" | "project"
488+ /** Tombstone: set when the rule is forgotten, so a later merge cannot resurrect it. */
489+ deletedAt ?: string
490+ deletedBy ?: string
477491}
478492
479493export const sessionObserver = new SessionObserverManager ( )
0 commit comments