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 }; }