From 305ccc168aa72743aeca2757ecf6d061fcc8f9d2 Mon Sep 17 00:00:00 2001 From: Jerry-Xin Date: Mon, 16 Mar 2026 18:06:25 +0800 Subject: [PATCH] fix(gateway): ensure agent:main:main in combined store when sessions.json missing Add ensureMainSessionKey call to the combined-store path so agent:main:main is injected even when the store file does not exist yet. Add test covering the missing-store scenario reported in #45754. --- src/gateway/session-utils.test.ts | 24 ++++++++++++++++++++++++ src/gateway/session-utils.ts | 1 + 2 files changed, 25 insertions(+) diff --git a/src/gateway/session-utils.test.ts b/src/gateway/session-utils.test.ts index c55202f8f25..108a44e8295 100644 --- a/src/gateway/session-utils.test.ts +++ b/src/gateway/session-utils.test.ts @@ -1085,4 +1085,28 @@ describe("listSessionsFromStore returns agent:main:main (#45754)", () => { ); }); }); + + test("agent:main:main included when sessions.json does not exist yet", async () => { + await withStateDirEnv("openclaw-no-store-file-", async ({ stateDir }) => { + const agentsDir = path.join(stateDir, "agents"); + const mainDir = path.join(agentsDir, "main", "sessions"); + // Create directory structure but do NOT create sessions.json + fs.mkdirSync(mainDir, { recursive: true }); + + const cfg = { + session: { + mainKey: "main", + store: path.join(stateDir, "agents", "{agentId}", "sessions", "sessions.json"), + }, + agents: { + list: [{ id: "main", default: true }], + }, + } as OpenClawConfig; + + const { store } = loadCombinedSessionStoreForGateway(cfg); + + // agent:main:main must be present even when store file does not exist + expect(store["agent:main:main"]).toBeDefined(); + }); + }); }); diff --git a/src/gateway/session-utils.ts b/src/gateway/session-utils.ts index 00a2cb7747e..f974e8394b3 100644 --- a/src/gateway/session-utils.ts +++ b/src/gateway/session-utils.ts @@ -709,6 +709,7 @@ export function loadCombinedSessionStoreForGateway(cfg: OpenClawConfig): { canonicalKey, }); } + ensureMainSessionKey(cfg, combined); return { storePath, store: combined }; }