@@ -447,6 +447,9 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
447447 ) ;
448448 }
449449
450+ /** The key the operator's own pages are indexed under; an id no key can be built from is refused before any store is made. */
451+ const operatorKey = conversationScopeKey ( agentId , { kind : 'operator' } ) ;
452+
450453 // The operator's pages under memory/ are markdown files; what people said to the agent lives in the store.
451454 const storeDir = path . join ( loaded . memoryDir , '.store' ) ;
452455 await fs . mkdir ( storeDir , { recursive : true } ) ;
@@ -456,12 +459,21 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
456459 graph : true ,
457460 selfImprove : true ,
458461 } ) ;
462+ /** Runs a step of the open; a step that throws closes the store first, so a failed open leaves no handle open. */
463+ const closingOnThrow = async < T > ( step : ( ) => T | Promise < T > ) : Promise < T > => {
464+ try {
465+ return await step ( ) ;
466+ } catch ( err ) {
467+ await memory . close ( ) . catch ( ( ) => undefined ) ;
468+ throw err ;
469+ }
470+ } ;
459471
460472 /** The user every session that names none, and every one-shot call, runs as; undefined without `sharedMemory`. */
461473 const sharedUser = sharedMemory ?. as ;
462474 if ( sharedUser !== undefined ) {
463475 // One person's memory: a store that holds another user's, or a session's, is not opened as theirs.
464- const others = ( await memory . listScopes ( ) ) . filter ( ( row ) => {
476+ const others = ( await closingOnThrow ( ( ) => memory . listScopes ( ) ) ) . filter ( ( row ) => {
465477 if ( row . kind === 'operator' ) return false ;
466478 const subject = parseConversationScopeKey ( row . scopeId ) ?. subject ;
467479 return ! ( subject ?. kind === 'user' && subject . id === sharedUser ) ;
@@ -477,13 +489,13 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
477489 // Traces an earlier version filed under the agent's plain id, in either form it was written in: no turn reads
478490 // them, and nothing here deletes, moves or rewrites them.
479491 const encodedAgentId = encodePlainScopeId ( 'persona' , agentId ) ;
480- const earlierTraces =
481- ( await memory . countScope ( 'persona' , agentId ) ) +
482- ( encodedAgentId !== agentId ? await memory . countScope ( 'persona' , encodedAgentId ) : 0 ) ;
492+ const earlierTraces = await closingOnThrow (
493+ async ( ) =>
494+ ( await memory . countScope ( 'persona' , agentId ) ) +
495+ ( encodedAgentId !== agentId ? await memory . countScope ( 'persona' , encodedAgentId ) : 0 ) ,
496+ ) ;
483497 if ( earlierTraces > 0 ) memoryLog . warn ( 'memory.legacy_traces' , { count : earlierTraces } ) ;
484498
485- /** The key the operator's own pages are indexed under. */
486- const operatorKey = conversationScopeKey ( agentId , { kind : 'operator' } ) ;
487499 /** The subject a turn's memory is kept under: the one user of `sharedMemory` for a subject that names none. */
488500 const subjectOf = ( subject : MemorySubject ) : MemorySubject =>
489501 sharedUser !== undefined && subject . kind === 'session' ? { kind : 'user' , id : sharedUser } : subject ;
@@ -912,7 +924,7 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
912924
913925 const chunker = new SemanticChunker ( ) ;
914926 const priorTools = rest . tools ;
915- const { tools : augmentedTools } = await attachMemoryWiki ( {
927+ const { tools : augmentedTools } = await closingOnThrow ( ( ) => attachMemoryWiki ( {
916928 memory,
917929 memoryDir : loaded . memoryDir ,
918930 agentId,
@@ -928,7 +940,7 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
928940 ...( memoryPolicy || screen ? { beforeScopeCompile } : { } ) ,
929941 // No compile at all: raw traces only, and no page is ever written.
930942 ...( compile === false ? { compile : false as const } : { } ) ,
931- } ) ;
943+ } ) ) ;
932944 if ( compile === false ) memoryLog . warn ( 'memory.compile_skipped' , { reason : 'compile_off' } ) ;
933945
934946 // attachMemoryWiki appends read_memory_page to an array. Use the augmented
@@ -938,7 +950,9 @@ export async function souledAgent(opts: SouledAgentOptions): Promise<SouledAgent
938950 priorTools && ! Array . isArray ( priorTools ) ? priorTools : augmentedTools
939951 ) as AgentOptions [ 'tools' ] ;
940952
941- const built = agent ( { ...rest , ...( budget ? { budget } : { } ) , memoryProvider : provider , tools } ) as SouledAgent ;
953+ const built = ( await closingOnThrow ( ( ) =>
954+ agent ( { ...rest , ...( budget ? { budget } : { } ) , memoryProvider : provider , tools } ) ,
955+ ) ) as SouledAgent ;
942956
943957 // Every turn joins what close() waits for: its promise, or a stream's settled usage. The default runtime's own close
944958 // does not wait for a running turn, and the GMI runtime's does not wait for a one-shot call.
0 commit comments