@@ -22,6 +22,7 @@ import { resetLoggerFactory, setLogger } from '../../core/logging/loggerFactory'
2222import { reply , script , type ProviderScript } from './helpers/stubProviders' ;
2323import { globalLLMProviderHealth } from '../../core/safety/LLMProviderHealthRegistry' ;
2424import { Brain } from '../../cognition/memory/retrieval/store/Brain' ;
25+ import { InMemoryLearningStore } from '../../cognition/learning/store/InMemoryLearningStore' ;
2526
2627let n = 0 ;
2728const key = ( ) => `k-soul-identity-${ ++ n } ` ;
@@ -354,3 +355,61 @@ describe("the soul's id as the agent's key", () => {
354355 expect ( ( ) => gmi ( stubbed ( key ( ) , { memory : { store : { path : path . join ( dir , 'y.sqlite' ) } } , learning : { mode : 'off' } } ) ) ) . toThrow ( / o n e a g e n t t o t h e l e a r n i n g l a y e r \. $ / ) ;
355356 } ) ;
356357} ) ;
358+
359+ describe ( 'learners filed under the slug of the agent name before the soul had an id' , ( ) => {
360+ const learning = ( store : InMemoryLearningStore , extra : Record < string , unknown > = { } ) =>
361+ ( { runtime : 'gmi' , name : 'Aria' , personality : { openness : 0.9 } , learning : { store, traits : true , ...extra } } ) ;
362+ /** The agent keys of one user's session rows. */
363+ const keysOf = async ( store : InMemoryLearningStore , userId : string ) => new Set ( ( await store . sessionRowsOf ( '' , userId ) ) . map ( ( row ) => row . agentKey ) ) ;
364+
365+ it ( 'stay under it by one logged fallback, and forgetUser reaches them' , async ( ) => {
366+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => undefined ) ;
367+ const store = new InMemoryLearningStore ( ) ;
368+ const k = key ( ) ;
369+ script ( 'openai' , k , { replies : [ reply . text ( 'One.' ) , reply . text ( 'Two.' ) , reply . text ( 'Three.' ) ] } ) ;
370+ const before = agent ( stubbed ( k , learning ( store ) ) ) ;
371+ await before . session ( 's1' , { userId : 'ana' } ) . send ( 'Hello.' ) ;
372+ await before . close ( ) ;
373+ expect ( await keysOf ( store , 'ana' ) ) . toEqual ( new Set ( [ 'aria' ] ) ) ;
374+
375+ writeSoul ( FULL ) ;
376+ const after = agent ( stubbed ( k , { ...learning ( store ) , soul : dir } ) ) ;
377+ await after . session ( 's2' , { userId : 'ana' } ) . send ( 'Hello again.' ) ;
378+ await after . session ( 's3' , { userId : 'ana' } ) . send ( 'And again.' ) ;
379+ expect ( events ( 'soul.id_fallback' ) ) . toEqual ( [ expect . objectContaining ( { reason : 'old_slug_learner' } ) ] ) ;
380+ expect ( await keysOf ( store , 'ana' ) ) . toEqual ( new Set ( [ 'aria' ] ) ) ;
381+ await after . learning ! . forgetUser ( 'ana' ) ;
382+ expect ( await store . sessionRowsOf ( '' , 'ana' ) ) . toEqual ( [ ] ) ;
383+ await after . close ( ) ;
384+ } ) ;
385+
386+ it ( 'are left where they are, with no line, when learning.learnerKey names the key' , async ( ) => {
387+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => undefined ) ;
388+ const store = new InMemoryLearningStore ( ) ;
389+ const k = key ( ) ;
390+ script ( 'openai' , k , { replies : [ reply . text ( 'One.' ) , reply . text ( 'Two.' ) ] } ) ;
391+ const before = agent ( stubbed ( k , learning ( store ) ) ) ;
392+ await before . session ( 's1' , { userId : 'ben' } ) . send ( 'Hello.' ) ;
393+ await before . close ( ) ;
394+
395+ writeSoul ( FULL ) ;
396+ const chosen = agent ( stubbed ( k , { ...learning ( store , { learnerKey : 'aria-1' } ) , soul : dir } ) ) ;
397+ await chosen . session ( 's2' , { userId : 'ben' } ) . send ( 'Hello again.' ) ;
398+ await chosen . close ( ) ;
399+ expect ( events ( 'soul.id_fallback' ) ) . toEqual ( [ ] ) ;
400+ expect ( await keysOf ( store , 'ben' ) ) . toEqual ( new Set ( [ 'aria' , 'aria-1' ] ) ) ;
401+ } ) ;
402+
403+ it ( 'files a new agent under the soul id with no line' , async ( ) => {
404+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => undefined ) ;
405+ writeSoul ( FULL ) ;
406+ const store = new InMemoryLearningStore ( ) ;
407+ const k = key ( ) ;
408+ script ( 'openai' , k , { replies : [ reply . text ( 'One.' ) ] } ) ;
409+ const fresh = agent ( stubbed ( k , { ...learning ( store ) , soul : dir } ) ) ;
410+ await fresh . session ( 's1' , { userId : 'cy' } ) . send ( 'Hello.' ) ;
411+ await fresh . close ( ) ;
412+ expect ( events ( 'soul.id_fallback' ) ) . toEqual ( [ ] ) ;
413+ expect ( await keysOf ( store , 'cy' ) ) . toEqual ( new Set ( [ 'aria-1' ] ) ) ;
414+ } ) ;
415+ } ) ;
0 commit comments