@@ -302,12 +302,9 @@ class SessionObserverManager {
302302 this . sessionLearnings . unshift ( entry )
303303 if ( this . sessionLearnings . length > 100 ) this . sessionLearnings . pop ( )
304304
305- // Persist to the central and workspace stores, serialised against forgetLearning.
306- const homeDir = process . env . USERPROFILE || process . env . HOME || ""
307- const files = [
308- homeDir ? path . join ( homeDir , ".local" , "share" , "opencode" , "learnings.json" ) : undefined ,
309- workspaceDir ? path . join ( workspaceDir , ".system_generated" , "logs" , "learnings.json" ) : undefined ,
310- ] . filter ( ( file ) : file is string => Boolean ( file ) )
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 ) )
311308 await this . mutate ( async ( ) => {
312309 for ( const file of files ) {
313310 try {
@@ -330,22 +327,34 @@ class SessionObserverManager {
330327 * The rules actually selected for injection, in the same order the prompt
331328 * builder uses. Shared so the listed `injected` flag cannot drift from reality.
332329 */
333- private async selectInjected ( limit : number ) : Promise < SessionLearning [ ] > {
330+ private async selectInjected ( limit : number , workspaceDir ?: string ) : Promise < SessionLearning [ ] > {
334331 if ( this . sessionLearnings . length > 0 ) {
335332 return this . sessionLearnings . slice ( 0 , limit )
336333 }
337334 // Read through the mutation queue: a read that starts while a forget is still
338335 // writing must not reload the store it is about to forget from.
339336 const loaded = await this . mutate ( async ( ) => {
340- try {
341- const homeDir = process . env . USERPROFILE || process . env . HOME || ""
342- if ( ! homeDir ) return [ ] as SessionLearning [ ]
343- const centralFile = path . join ( homeDir , ".local" , "share" , "opencode" , "learnings.json" )
344- const content = JSON . parse ( await fs . readFile ( centralFile , "utf8" ) )
345- return Array . isArray ( content ) ? ( content as SessionLearning [ ] ) : [ ]
346- } catch {
347- return [ ] as SessionLearning [ ]
337+ const read = async ( file ?: string ) : Promise < SessionLearning [ ] > => {
338+ if ( ! file ) return [ ]
339+ try {
340+ const parsed = JSON . parse ( await fs . readFile ( file , "utf8" ) )
341+ return Array . isArray ( parsed ) ? ( parsed as SessionLearning [ ] ) : [ ]
342+ } catch {
343+ return [ ]
344+ }
348345 }
346+ const paths = learningStorePaths ( workspaceDir )
347+ // Project rules first: a committed, reviewed repo rule outranks an ad-hoc
348+ // global note (AIMETH-008 - shared repo facts win). One entry per rule text.
349+ const seen = new Set < string > ( )
350+ const merged : SessionLearning [ ] = [ ]
351+ for ( const rule of [ ...( await read ( paths . project ) ) , ...( await read ( paths . global ) ) ] ) {
352+ const key = learningKey ( rule )
353+ if ( seen . has ( key ) ) continue
354+ seen . add ( key )
355+ merged . push ( rule )
356+ }
357+ return merged
349358 } )
350359 if ( loaded . length > 0 ) {
351360 // Merge, never clobber: a rule recorded while the read was pending must not
@@ -359,8 +368,8 @@ class SessionObserverManager {
359368 return this . sessionLearnings . slice ( 0 , limit )
360369 }
361370
362- public async getRecentLearnings ( limit = 5 ) : Promise < SessionLearning [ ] > {
363- return this . selectInjected ( limit )
371+ public async getRecentLearnings ( limit = 5 , workspaceDir ?: string ) : Promise < SessionLearning [ ] > {
372+ return this . selectInjected ( limit , workspaceDir )
364373 }
365374
366375 private mutation : Promise < unknown > = Promise . resolve ( )
@@ -373,37 +382,34 @@ class SessionObserverManager {
373382 }
374383
375384 /**
376- * Every persisted rule, with where it was found and whether it is one of the
377- * most-recent rules actually injected into a session's system prompt. This is
378- * the honest answer to "what rules is the agent using?" - recorded rules that
379- * fall outside the injection window are shown as ` stored`, not `injected` .
385+ * Every persisted rule, tagged with which store it lives in (`global` = whole
386+ * machine, `project` = this repo) and whether it is actually injected into a
387+ * session's system prompt. This is the honest answer to "what rules is the
388+ * agent using?" - a rule outside the injection window is shown as stored.
380389 */
381390 public async listLearnings (
382391 workspaceDir ?: string ,
383- ) : Promise < Array < SessionLearning & { origin : "central" | "workspace" ; injected : boolean } > > {
384- const read = async ( file : string ) : Promise < SessionLearning [ ] > => {
392+ ) : Promise < Array < SessionLearning & { origin : "global" | "project" ; injected : boolean } > > {
393+ const read = async ( file ?: string ) : Promise < SessionLearning [ ] > => {
394+ if ( ! file ) return [ ]
385395 try {
386396 const parsed = JSON . parse ( await fs . readFile ( file , "utf8" ) )
387397 return Array . isArray ( parsed ) ? ( parsed as SessionLearning [ ] ) : [ ]
388398 } catch {
389399 return [ ]
390400 }
391401 }
392- const homeDir = process . env . USERPROFILE || process . env . HOME || ""
393- const central = homeDir
394- ? await read ( path . join ( homeDir , ".local" , "share" , "opencode" , "learnings.json" ) )
395- : [ ]
396- const workspace = workspaceDir
397- ? await read ( path . join ( workspaceDir , ".system_generated" , "logs" , "learnings.json" ) )
398- : [ ]
399- const centralKeys = new Set ( central . map ( ( l ) => l . lesson . trim ( ) . toLowerCase ( ) ) )
402+ const paths = learningStorePaths ( workspaceDir )
403+ const global = await read ( paths . global )
404+ const project = await read ( paths . project )
405+ const globalKeys = new Set ( global . map ( learningKey ) )
400406 const merged = [
401- ...central . map ( ( l ) => ( { ...l , origin : "central " as const } ) ) ,
402- ...workspace
403- . filter ( ( l ) => ! centralKeys . has ( l . lesson . trim ( ) . toLowerCase ( ) ) )
404- . map ( ( l ) => ( { ...l , origin : "workspace " as const } ) ) ,
407+ ...global . map ( ( l ) => ( { ...l , origin : "global " as const } ) ) ,
408+ ...project
409+ . filter ( ( l ) => ! globalKeys . has ( learningKey ( l ) ) )
410+ . map ( ( l ) => ( { ...l , origin : "project " as const } ) ) ,
405411 ] . sort ( ( a , b ) => ( a . timestamp < b . timestamp ? 1 : - 1 ) )
406- const injectedKeys = new Set ( ( await this . selectInjected ( INJECTED_LEARNINGS_LIMIT ) ) . map ( learningKey ) )
412+ const injectedKeys = new Set ( ( await this . selectInjected ( INJECTED_LEARNINGS_LIMIT , workspaceDir ) ) . map ( learningKey ) )
407413 return merged . map ( ( l ) => ( { ...l , injected : injectedKeys . has ( learningKey ( l ) ) } ) )
408414 }
409415
@@ -417,11 +423,8 @@ class SessionObserverManager {
417423 if ( ! needle ) return 0
418424 const matches = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . toLowerCase ( ) . includes ( needle )
419425 this . sessionLearnings = this . sessionLearnings . filter ( ( l ) => ! matches ( l ) )
420- const homeDir = process . env . USERPROFILE || process . env . HOME || ""
421- const files = [
422- homeDir ? path . join ( homeDir , ".local" , "share" , "opencode" , "learnings.json" ) : undefined ,
423- workspaceDir ? path . join ( workspaceDir , ".system_generated" , "logs" , "learnings.json" ) : undefined ,
424- ] . filter ( ( file ) : file is string => Boolean ( file ) )
426+ const paths = learningStorePaths ( workspaceDir )
427+ const files = [ paths . global , paths . project ] . filter ( ( file ) : file is string => Boolean ( file ) )
425428 const removed = new Set < string > ( )
426429 for ( const file of files ) {
427430 for ( const key of await this . dropRules ( file , matches ) ) removed . add ( key )
@@ -469,6 +472,15 @@ class SessionObserverManager {
469472/** How many of the most-recent rules are injected into a session system prompt. */
470473export const INJECTED_LEARNINGS_LIMIT = 5
471474
475+ /** The two on-disk rule stores: global (whole machine) and this project. */
476+ export function learningStorePaths ( workspaceDir ?: string ) : { global ?: string ; project ?: string } {
477+ const homeDir = process . env . USERPROFILE || process . env . HOME || ""
478+ return {
479+ global : homeDir ? path . join ( homeDir , ".local" , "share" , "opencode" , "learnings.json" ) : undefined ,
480+ project : workspaceDir ? path . join ( workspaceDir , ".system_generated" , "logs" , "learnings.json" ) : undefined ,
481+ }
482+ }
483+
472484const learningKey = ( l : SessionLearning ) => String ( l . lesson ?? "" ) . trim ( ) . toLowerCase ( )
473485
474486/** Write JSON via a temp file + rename so a reader never sees a half-written file. */
0 commit comments