From f544ffe0ea9a3352b545ca92ea8fa1ad3e0dc73e Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Thu, 12 Mar 2026 15:03:30 +0000 Subject: [PATCH] Sessions: discover retired ACP stores under configured roots --- src/config/sessions/targets.test.ts | 72 +++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/config/sessions/targets.test.ts b/src/config/sessions/targets.test.ts index aee55706572..687765082f9 100644 --- a/src/config/sessions/targets.test.ts +++ b/src/config/sessions/targets.test.ts @@ -382,4 +382,76 @@ describe("resolveAllAgentSessionStoreTargetsSync", () => { }); }); }); + + it("discovers retired agent stores under a configured custom session root", async () => { + await withTempHome(async (home) => { + const customRoot = path.join(home, "custom-state"); + const opsSessionsDir = path.join(customRoot, "agents", "ops", "sessions"); + const retiredSessionsDir = path.join(customRoot, "agents", "retired", "sessions"); + await fs.mkdir(opsSessionsDir, { recursive: true }); + await fs.mkdir(retiredSessionsDir, { recursive: true }); + await fs.writeFile(path.join(opsSessionsDir, "sessions.json"), "{}", "utf8"); + await fs.writeFile(path.join(retiredSessionsDir, "sessions.json"), "{}", "utf8"); + + const cfg: OpenClawConfig = { + session: { + store: path.join(customRoot, "agents", "{agentId}", "sessions", "sessions.json"), + }, + agents: { + list: [{ id: "ops", default: true }], + }, + }; + + const targets = await resolveAllAgentSessionStoreTargets(cfg, { env: process.env }); + + expect(targets).toEqual( + expect.arrayContaining([ + { + agentId: "ops", + storePath: path.join(opsSessionsDir, "sessions.json"), + }, + { + agentId: "retired", + storePath: path.join(retiredSessionsDir, "sessions.json"), + }, + ]), + ); + expect( + targets.filter((target) => target.storePath === path.join(opsSessionsDir, "sessions.json")), + ).toHaveLength(1); + }); + }); + + it("respects the caller env when resolving configured and discovered store roots", async () => { + await withTempHome(async (home) => { + const envStateDir = path.join(home, "env-state"); + const mainSessionsDir = path.join(envStateDir, "agents", "main", "sessions"); + const retiredSessionsDir = path.join(envStateDir, "agents", "retired", "sessions"); + await fs.mkdir(mainSessionsDir, { recursive: true }); + await fs.mkdir(retiredSessionsDir, { recursive: true }); + await fs.writeFile(path.join(mainSessionsDir, "sessions.json"), "{}", "utf8"); + await fs.writeFile(path.join(retiredSessionsDir, "sessions.json"), "{}", "utf8"); + + const env = { + ...process.env, + OPENCLAW_STATE_DIR: envStateDir, + }; + const cfg: OpenClawConfig = {}; + + const targets = await resolveAllAgentSessionStoreTargets(cfg, { env }); + + expect(targets).toEqual( + expect.arrayContaining([ + { + agentId: "main", + storePath: path.join(mainSessionsDir, "sessions.json"), + }, + { + agentId: "retired", + storePath: path.join(retiredSessionsDir, "sessions.json"), + }, + ]), + ); + }); + }); });