@@ -69,6 +69,7 @@ function resolveLegacyMainStoreSessionForDefaultAgent(opts: {
6969 sessionKey ?: string ;
7070 sessionStore : Record < string , SessionEntry > ;
7171 storePath : string ;
72+ cloneOnWrite ?: boolean ;
7273} ) : SessionKeyResolution | undefined {
7374 if ( opts . defaultAgentId === DEFAULT_AGENT_ID || ! opts . sessionKey ) {
7475 return undefined ;
@@ -92,24 +93,29 @@ function resolveLegacyMainStoreSessionForDefaultAgent(opts: {
9293 for ( const legacyKey of legacyKeys ) {
9394 const legacyEntry = opts . sessionStore [ legacyKey ] ;
9495 if ( legacyEntry ) {
95- opts . sessionStore [ opts . sessionKey ] = { ...legacyEntry } ;
96+ const sessionStore = opts . cloneOnWrite ? { ...opts . sessionStore } : opts . sessionStore ;
97+ sessionStore [ opts . sessionKey ] = { ...legacyEntry } ;
9698 return {
9799 sessionKey : opts . sessionKey ,
98- sessionStore : opts . sessionStore ,
100+ sessionStore,
99101 storePath : opts . storePath ,
100102 } ;
101103 }
102104 }
103105 return undefined ;
104106 }
105- const legacyStore = loadSessionStore ( legacyStorePath ) ;
107+ const legacyStore = loadSessionStore (
108+ legacyStorePath ,
109+ opts . cloneOnWrite ? { clone : false } : undefined ,
110+ ) ;
106111 for ( const legacyKey of legacyKeys ) {
107112 const legacyEntry = legacyStore [ legacyKey ] ;
108113 if ( legacyEntry ) {
109- opts . sessionStore [ opts . sessionKey ] = { ...legacyEntry } ;
114+ const sessionStore = opts . cloneOnWrite ? { ...opts . sessionStore } : opts . sessionStore ;
115+ sessionStore [ opts . sessionKey ] = { ...legacyEntry } ;
110116 return {
111117 sessionKey : opts . sessionKey ,
112- sessionStore : opts . sessionStore ,
118+ sessionStore,
113119 storePath : opts . storePath ,
114120 } ;
115121 }
@@ -124,6 +130,7 @@ function collectSessionIdMatchesForRequest(opts: {
124130 storeAgentId ?: string ;
125131 sessionId : string ;
126132 searchOtherAgentStores : boolean ;
133+ clone ?: boolean ;
127134} ) : SessionIdMatchSet {
128135 const matches : Array < [ string , SessionEntry ] > = [ ] ;
129136 const primaryStoreMatches : Array < [ string , SessionEntry ] > = [ ] ;
@@ -160,7 +167,10 @@ function collectSessionIdMatchesForRequest(opts: {
160167 continue ;
161168 }
162169 const candidateStorePath = resolveStorePath ( opts . cfg . session ?. store , { agentId } ) ;
163- addMatches ( loadSessionStore ( candidateStorePath ) , candidateStorePath ) ;
170+ addMatches (
171+ loadSessionStore ( candidateStorePath , opts . clone === false ? { clone : false } : undefined ) ,
172+ candidateStorePath ,
173+ ) ;
164174 }
165175
166176 return { matches, primaryStoreMatches, storeByKey } ;
@@ -203,6 +213,7 @@ export function resolveSessionKeyForRequest(opts: {
203213 sessionId ?: string ;
204214 sessionKey ?: string ;
205215 agentId ?: string ;
216+ clone ?: boolean ;
206217} ) : SessionKeyResolution {
207218 const sessionCfg = opts . cfg . session ;
208219 const scope = sessionCfg ?. scope ?? "per-sender" ;
@@ -226,7 +237,8 @@ export function resolveSessionKeyForRequest(opts: {
226237 const storePath = resolveStorePath ( sessionCfg ?. store , {
227238 agentId : storeAgentId ,
228239 } ) ;
229- const sessionStore = loadSessionStore ( storePath ) ;
240+ const loadOptions = opts . clone === false ? { clone : false as const } : undefined ;
241+ const sessionStore = loadSessionStore ( storePath , loadOptions ) ;
230242
231243 const ctx : MsgContext | undefined = opts . to ?. trim ( ) ? { From : opts . to } : undefined ;
232244 let sessionKey : string | undefined =
@@ -240,6 +252,7 @@ export function resolveSessionKeyForRequest(opts: {
240252 sessionKey,
241253 sessionStore,
242254 storePath,
255+ cloneOnWrite : opts . clone === false ,
243256 } ) ;
244257 if ( legacyMainSession ) {
245258 return legacyMainSession ;
@@ -262,6 +275,7 @@ export function resolveSessionKeyForRequest(opts: {
262275 storeAgentId,
263276 sessionId : requestedSessionId ,
264277 searchOtherAgentStores : requestedAgentId === undefined ,
278+ ...( opts . clone === false ? { clone : false } : { } ) ,
265279 } ) ;
266280 const preferredSelection = resolveSessionIdMatchSelection ( matches , requestedSessionId ) ;
267281 const currentStoreSelection =
@@ -293,6 +307,7 @@ export function resolveSession(opts: {
293307 sessionId ?: string ;
294308 sessionKey ?: string ;
295309 agentId ?: string ;
310+ clone ?: boolean ;
296311} ) : SessionResolution {
297312 const sessionCfg = opts . cfg . session ;
298313 const { sessionKey, sessionStore, storePath } = resolveSessionKeyForRequest ( {
@@ -301,6 +316,7 @@ export function resolveSession(opts: {
301316 sessionId : opts . sessionId ,
302317 sessionKey : opts . sessionKey ,
303318 agentId : opts . agentId ,
319+ ...( opts . clone === false ? { clone : false } : { } ) ,
304320 } ) ;
305321 const now = Date . now ( ) ;
306322
0 commit comments