|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { clearSessionStoreCaches } from "../../config/sessions/store-cache.js"; |
| 6 | +import { loadSessionStore } from "../../config/sessions/store.js"; |
| 7 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 8 | +import { resolveSessionWithReservation } from "./session-resolution-reservation.js"; |
| 9 | +import { resolveSession } from "./session.js"; |
| 10 | + |
| 11 | +async function withTempStore<T>( |
| 12 | + run: (params: { cfg: OpenClawConfig; storePath: string }) => Promise<T>, |
| 13 | +): Promise<T> { |
| 14 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-reservation-")); |
| 15 | + const storePath = path.join(dir, "sessions.json"); |
| 16 | + const cfg = { session: { store: storePath, mainKey: "main" } } as OpenClawConfig; |
| 17 | + try { |
| 18 | + return await run({ cfg, storePath }); |
| 19 | + } finally { |
| 20 | + clearSessionStoreCaches(); |
| 21 | + await fs.rm(dir, { recursive: true, force: true }); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + clearSessionStoreCaches(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("resolveSessionWithReservation", () => { |
| 30 | + const sessionKey = "agent:main:finn:c1"; |
| 31 | + |
| 32 | + it("forks distinct sessionIds without the reservation lock (regression repro)", async () => { |
| 33 | + await withTempStore(async ({ cfg }) => { |
| 34 | + const first = resolveSession({ cfg, sessionKey }); |
| 35 | + const second = resolveSession({ cfg, sessionKey }); |
| 36 | + expect(first.isNewSession).toBe(true); |
| 37 | + expect(second.isNewSession).toBe(true); |
| 38 | + // Without serialization both requests mint their own id, so the second |
| 39 | + // request runs in an isolated, memory-less session. |
| 40 | + expect(first.sessionId).not.toBe(second.sessionId); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("gives concurrent same-key requests one shared sessionId", async () => { |
| 45 | + await withTempStore(async ({ cfg, storePath }) => { |
| 46 | + const [first, second] = await Promise.all([ |
| 47 | + resolveSessionWithReservation({ cfg, sessionKey }), |
| 48 | + resolveSessionWithReservation({ cfg, sessionKey }), |
| 49 | + ]); |
| 50 | + expect(first.sessionId).toBe(second.sessionId); |
| 51 | + // Exactly one resolution created the session; the other adopted it. |
| 52 | + expect([first.isNewSession, second.isNewSession].filter(Boolean)).toHaveLength(1); |
| 53 | + // The reserved mapping is persisted so later turns resume the same session. |
| 54 | + const persisted = loadSessionStore(storePath, { skipCache: true })[sessionKey]; |
| 55 | + expect(persisted?.sessionId).toBe(first.sessionId); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("reuses the reserved id for a follow-up after the first request", async () => { |
| 60 | + await withTempStore(async ({ cfg }) => { |
| 61 | + const first = await resolveSessionWithReservation({ cfg, sessionKey }); |
| 62 | + const second = await resolveSessionWithReservation({ cfg, sessionKey }); |
| 63 | + expect(second.sessionId).toBe(first.sessionId); |
| 64 | + expect(second.isNewSession).toBe(false); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("leaves the explicit sessionId path unchanged", async () => { |
| 69 | + await withTempStore(async ({ cfg }) => { |
| 70 | + const resolution = await resolveSessionWithReservation({ |
| 71 | + cfg, |
| 72 | + sessionId: "explicit-123", |
| 73 | + }); |
| 74 | + expect(resolution.sessionId).toBe("explicit-123"); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("does not write a visible store row for internal handoffs (suppressVisibleSessionEffects)", async () => { |
| 79 | + await withTempStore(async ({ cfg, storePath }) => { |
| 80 | + const resolution = await resolveSessionWithReservation({ |
| 81 | + cfg, |
| 82 | + sessionKey, |
| 83 | + suppressVisibleSessionEffects: true, |
| 84 | + }); |
| 85 | + expect(resolution.isNewSession).toBe(true); |
| 86 | + expect(resolution.sessionId).toBeTruthy(); |
| 87 | + // Internal handoffs must not leak a visible session-store row. |
| 88 | + const persisted = loadSessionStore(storePath, { skipCache: true })[sessionKey]; |
| 89 | + expect(persisted).toBeUndefined(); |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments