@@ -21,6 +21,7 @@ import { SCHEMA_INSTRUCTION_LEAD } from '../runtime/structuredReply';
2121import { resetLoggerFactory , setLogger } from '../../core/logging/loggerFactory' ;
2222import { reply , script , type ProviderScript } from './helpers/stubProviders' ;
2323import { globalLLMProviderHealth } from '../../core/safety/LLMProviderHealthRegistry' ;
24+ import { Brain } from '../../cognition/memory/retrieval/store/Brain' ;
2425
2526let n = 0 ;
2627const key = ( ) => `k-soul-identity-${ ++ n } ` ;
@@ -286,3 +287,70 @@ describe("a soul's identity on the GMI path", () => {
286287 }
287288 } ) ;
288289} ) ;
290+
291+ describe ( "the soul's id as the agent's key" , ( ) => {
292+ it . each ( [ [ 'the default runtime' , { } ] , [ "runtime: 'gmi'" , { runtime : 'gmi' } ] ] as const ) (
293+ 'souledAgent() on %s files its turns under the soul agentId, whatever the agent is named' ,
294+ async ( _name , extra ) => {
295+ writeSoul ( FULL ) ;
296+ const k = key ( ) ;
297+ script ( 'openai' , k , { whole : true , replies : [ reply . text ( 'Lisbon it is.' ) , reply . text ( 'Notes.' ) , reply . text ( 'Notes.' ) , reply . text ( 'Notes.' ) ] } ) ;
298+ const a = await souledAgent ( { ...stubbed ( k , { ...extra , name : 'Support' } ) , soul : dir } as never ) ;
299+ try {
300+ await a . session ( 's' ) . send ( 'I moved to Lisbon.' ) ;
301+ await vi . waitFor ( async ( ) => expect ( ( await a . memory ! . recall ( 'Lisbon' , { scope : 'persona' , scopeId : 'aria-1' } ) ) . length ) . toBe ( 2 ) ) ;
302+ expect ( await a . memory ! . recall ( 'Lisbon' , { scope : 'persona' , scopeId : 'Support' } ) ) . toEqual ( [ ] ) ;
303+ } finally {
304+ await a . close ( ) ;
305+ }
306+ } ,
307+ ) ;
308+
309+ it ( 'files under the slug of the soul name when the soul has no agentId' , async ( ) => {
310+ writeSoul ( 'name: Aria Stone' ) ;
311+ const k = key ( ) ;
312+ script ( 'openai' , k , { whole : true , replies : [ reply . text ( 'Lisbon it is.' ) , reply . text ( 'Notes.' ) ] } ) ;
313+ const a = await souledAgent ( { ...stubbed ( k ) , soul : dir } as never ) ;
314+ try {
315+ await a . session ( 's' ) . send ( 'I moved to Lisbon.' ) ;
316+ await vi . waitFor ( async ( ) => expect ( ( await a . memory ! . recall ( 'Lisbon' , { scope : 'persona' , scopeId : 'aria-stone' } ) ) . length ) . toBe ( 2 ) ) ;
317+ } finally {
318+ await a . close ( ) ;
319+ }
320+ } ) ;
321+
322+ it ( 'souledAgent() rejects a soul that gives no id on an agent with no name, and makes no store' , async ( ) => {
323+ writeSoul ( 'hardLimits:\n - Never guess.' , 'You are terse.' ) ;
324+ await expect ( souledAgent ( { ...stubbed ( key ( ) ) , soul : dir } as never ) ) . rejects . toThrow (
325+ "souledAgent: the soul has no agentId and no name, and the agent has no name; set agentId in SOUL.md's front matter so the agent's memory has one stable id" ,
326+ ) ;
327+ expect ( fs . existsSync ( path . join ( dir , 'memory' , '.store' ) ) ) . toBe ( false ) ;
328+ } ) ;
329+
330+ it ( 'gmi() with memory.store opens its brain under the soul id with no learning.agentKey, and under learning.agentKey when one is set' , async ( ) => {
331+ writeSoul ( FULL ) ;
332+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => undefined ) ;
333+ const open = vi . spyOn ( Brain , 'openSqlite' ) ;
334+ const first = path . join ( dir , 'first.sqlite' ) ;
335+ const a = gmi ( stubbed ( key ( ) , { soul : dir , memory : { store : { path : first } } , learning : { mode : 'off' } } ) ) ;
336+ // Opening a session reads its generation from the store, which opens the brain.
337+ a . session ( 's' ) ;
338+ await vi . waitFor ( ( ) => expect ( open ) . toHaveBeenCalledWith ( first , { brainId : 'aria-1' } ) ) ;
339+ await a . close ( ) ;
340+
341+ const second = path . join ( dir , 'second.sqlite' ) ;
342+ const b = gmi ( stubbed ( key ( ) , { soul : dir , memory : { store : { path : second } } , learning : { mode : 'off' , agentKey : 'kept' } } ) ) ;
343+ b . session ( 's' ) ;
344+ await vi . waitFor ( ( ) => expect ( open ) . toHaveBeenCalledWith ( second , { brainId : 'kept' } ) ) ;
345+ await b . close ( ) ;
346+ } ) ;
347+
348+ it ( 'gmi() with memory.store refuses a soul that gives no id, telling the host to set agentId' , ( ) => {
349+ writeSoul ( 'hardLimits:\n - Never guess.' , 'You are terse.' ) ;
350+ const build = ( ) => gmi ( stubbed ( key ( ) , { soul : dir , memory : { store : { path : path . join ( dir , 'x.sqlite' ) } } , learning : { mode : 'off' } } ) ) ;
351+ expect ( build ) . toThrow ( 'gmi(): memory.store needs learning.agentKey' ) ;
352+ expect ( build ) . toThrow ( "set agentId in SOUL.md's front matter, and the soul's id names the brain." ) ;
353+ // Without a soul the error is the one it was: it ends where it ended.
354+ 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 \. $ / ) ;
355+ } ) ;
356+ } ) ;
0 commit comments