From 8ce7cfdc1237c5de1eca1959a5869568c26e7e9f Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Sat, 14 Feb 2026 13:35:36 -0500 Subject: [PATCH] test(gateway): cover default-agent transcript fallbacks --- src/gateway/session-utils.fs.test.ts | 16 ++++++++++++++++ src/gateway/session-utils.test.ts | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/gateway/session-utils.fs.test.ts b/src/gateway/session-utils.fs.test.ts index 0e324f78d5b..42f6bf43366 100644 --- a/src/gateway/session-utils.fs.test.ts +++ b/src/gateway/session-utils.fs.test.ts @@ -594,6 +594,22 @@ describe("resolveSessionTranscriptCandidates", () => { }); describe("resolveSessionTranscriptCandidates safety", () => { + test("keeps cross-agent absolute sessionFile when storePath agent context differs", () => { + const storePath = "/tmp/openclaw/agents/main/sessions/sessions.json"; + const sessionFile = "/tmp/openclaw/agents/ops/sessions/sess-safe.jsonl"; + const candidates = resolveSessionTranscriptCandidates("sess-safe", storePath, sessionFile); + + expect(candidates.map((value) => path.resolve(value))).toContain(path.resolve(sessionFile)); + }); + + test("keeps cross-agent absolute sessionFile for custom per-agent store roots", () => { + const storePath = "/srv/custom/agents/main/sessions/sessions.json"; + const sessionFile = "/srv/custom/agents/ops/sessions/sess-safe.jsonl"; + const candidates = resolveSessionTranscriptCandidates("sess-safe", storePath, sessionFile); + + expect(candidates.map((value) => path.resolve(value))).toContain(path.resolve(sessionFile)); + }); + test("drops unsafe session IDs instead of producing traversal paths", () => { const candidates = resolveSessionTranscriptCandidates( "../etc/passwd", diff --git a/src/gateway/session-utils.test.ts b/src/gateway/session-utils.test.ts index aa0d518712b..e57ea027a31 100644 --- a/src/gateway/session-utils.test.ts +++ b/src/gateway/session-utils.test.ts @@ -70,6 +70,25 @@ describe("gateway session utils", () => { ); }); + test("resolveSessionStoreKey falls back to first list entry when no agent is marked default", () => { + const cfg = { + session: { mainKey: "main" }, + agents: { list: [{ id: "ops" }, { id: "review" }] }, + } as OpenClawConfig; + expect(resolveSessionStoreKey({ cfg, sessionKey: "main" })).toBe("agent:ops:main"); + expect(resolveSessionStoreKey({ cfg, sessionKey: "discord:group:123" })).toBe( + "agent:ops:discord:group:123", + ); + }); + + test("resolveSessionStoreKey falls back to main when agents.list is missing", () => { + const cfg = { + session: { mainKey: "work" }, + } as OpenClawConfig; + expect(resolveSessionStoreKey({ cfg, sessionKey: "main" })).toBe("agent:main:work"); + expect(resolveSessionStoreKey({ cfg, sessionKey: "thread-1" })).toBe("agent:main:thread-1"); + }); + test("resolveSessionStoreKey normalizes session key casing", () => { const cfg = { session: { mainKey: "main" },