@@ -1155,4 +1155,57 @@ describe("loadCombinedSessionStoreForGateway includes disk-only agents (#32804)"
11551155 expect ( store [ "agent:codex:acp-task" ] ) . toBeDefined ( ) ;
11561156 } ) ;
11571157 } ) ;
1158+
1159+ test ( "agent-scoped loads read only matching agent stores" , async ( ) => {
1160+ await withStateDirEnv ( "openclaw-acp-scoped-" , async ( { stateDir } ) => {
1161+ const customRoot = path . join ( stateDir , "custom-state" ) ;
1162+ const agentsDir = path . join ( customRoot , "agents" ) ;
1163+ const mainDir = path . join ( agentsDir , "main" , "sessions" ) ;
1164+ const codexDir = path . join ( agentsDir , "codex" , "sessions" ) ;
1165+ fs . mkdirSync ( mainDir , { recursive : true } ) ;
1166+ fs . mkdirSync ( codexDir , { recursive : true } ) ;
1167+
1168+ const mainStorePath = path . join ( mainDir , "sessions.json" ) ;
1169+ const codexStorePath = path . join ( codexDir , "sessions.json" ) ;
1170+ fs . writeFileSync (
1171+ mainStorePath ,
1172+ JSON . stringify ( {
1173+ "agent:main:main" : { sessionId : "s-main" , updatedAt : 100 } ,
1174+ } ) ,
1175+ "utf8" ,
1176+ ) ;
1177+ fs . writeFileSync (
1178+ codexStorePath ,
1179+ JSON . stringify ( {
1180+ "agent:codex:acp-task" : { sessionId : "s-codex" , updatedAt : 200 } ,
1181+ } ) ,
1182+ "utf8" ,
1183+ ) ;
1184+
1185+ const cfg = {
1186+ session : {
1187+ mainKey : "main" ,
1188+ store : path . join ( customRoot , "agents" , "{agentId}" , "sessions" , "sessions.json" ) ,
1189+ } ,
1190+ agents : {
1191+ list : [ { id : "main" , default : true } ] ,
1192+ } ,
1193+ } as OpenClawConfig ;
1194+
1195+ const readSpy = vi . spyOn ( fs , "readFileSync" ) ;
1196+
1197+ const { store, storePath } = loadCombinedSessionStoreForGateway ( cfg , { agentId : "codex" } ) ;
1198+
1199+ expect ( storePath ) . toBe ( fs . realpathSync . native ( codexStorePath ) ) ;
1200+ expect ( store [ "agent:codex:acp-task" ] ) . toBeDefined ( ) ;
1201+ expect ( store [ "agent:main:main" ] ) . toBeUndefined ( ) ;
1202+ const readPaths = readSpy . mock . calls
1203+ . map ( ( call ) => call [ 0 ] )
1204+ . filter ( ( arg ) : arg is string => typeof arg === "string" ) ;
1205+ expect ( readPaths ) . toContain ( fs . realpathSync . native ( codexStorePath ) ) ;
1206+ expect ( readPaths ) . not . toContain ( fs . realpathSync . native ( mainStorePath ) ) ;
1207+
1208+ readSpy . mockRestore ( ) ;
1209+ } ) ;
1210+ } ) ;
11581211} ) ;
0 commit comments