|
| 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 { replaceSessionEntry } from "../../config/sessions/session-accessor.js"; |
| 6 | +import { clearSessionStoreCaches } from "../../config/sessions/store-cache.js"; |
| 7 | +import { withSessionStoreWriterForTest } from "../../config/sessions/store-writer.js"; |
| 8 | +import { loadSessionStore } from "../../config/sessions/store.js"; |
| 9 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 10 | +import { resolveSessionWithReservation } from "./session-resolution-reservation.js"; |
| 11 | +import { resolveSession } from "./session.js"; |
| 12 | + |
| 13 | +/** Flush queued micro/macro tasks so an in-flight reservation reaches its persist. */ |
| 14 | +async function flushPendingWork(): Promise<void> { |
| 15 | + for (let i = 0; i < 5; i += 1) { |
| 16 | + await new Promise((resolve) => { |
| 17 | + setTimeout(resolve, 0); |
| 18 | + }); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +async function withTempStore<T>( |
| 23 | + run: (params: { cfg: OpenClawConfig; storePath: string }) => Promise<T>, |
| 24 | +): Promise<T> { |
| 25 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-session-reservation-")); |
| 26 | + const storePath = path.join(dir, "sessions.json"); |
| 27 | + const cfg = { session: { store: storePath, mainKey: "main" } } as OpenClawConfig; |
| 28 | + try { |
| 29 | + return await run({ cfg, storePath }); |
| 30 | + } finally { |
| 31 | + clearSessionStoreCaches(); |
| 32 | + await fs.rm(dir, { recursive: true, force: true }); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + clearSessionStoreCaches(); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("resolveSessionWithReservation", () => { |
| 41 | + const sessionKey = "agent:main:finn:c1"; |
| 42 | + |
| 43 | + it("forks distinct sessionIds without the reservation lock (regression repro)", async () => { |
| 44 | + await withTempStore(async ({ cfg }) => { |
| 45 | + const first = resolveSession({ cfg, sessionKey }); |
| 46 | + const second = resolveSession({ cfg, sessionKey }); |
| 47 | + expect(first.isNewSession).toBe(true); |
| 48 | + expect(second.isNewSession).toBe(true); |
| 49 | + // Without serialization both requests mint their own id, so the second |
| 50 | + // request runs in an isolated, memory-less session. |
| 51 | + expect(first.sessionId).not.toBe(second.sessionId); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("gives concurrent same-key requests one shared sessionId", async () => { |
| 56 | + await withTempStore(async ({ cfg, storePath }) => { |
| 57 | + const [first, second] = await Promise.all([ |
| 58 | + resolveSessionWithReservation({ cfg, sessionKey }), |
| 59 | + resolveSessionWithReservation({ cfg, sessionKey }), |
| 60 | + ]); |
| 61 | + expect(first.sessionId).toBe(second.sessionId); |
| 62 | + // Exactly one resolution created the session; the other adopted it. |
| 63 | + expect([first.isNewSession, second.isNewSession].filter(Boolean)).toHaveLength(1); |
| 64 | + // The reserved mapping is persisted so later turns resume the same session. |
| 65 | + const persisted = loadSessionStore(storePath, { skipCache: true })[sessionKey]; |
| 66 | + expect(persisted?.sessionId).toBe(first.sessionId); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("reuses the reserved id for a follow-up after the first request", async () => { |
| 71 | + await withTempStore(async ({ cfg }) => { |
| 72 | + const first = await resolveSessionWithReservation({ cfg, sessionKey }); |
| 73 | + const second = await resolveSessionWithReservation({ cfg, sessionKey }); |
| 74 | + expect(second.sessionId).toBe(first.sessionId); |
| 75 | + expect(second.isNewSession).toBe(false); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("leaves the explicit sessionId path unchanged", async () => { |
| 80 | + await withTempStore(async ({ cfg }) => { |
| 81 | + const resolution = await resolveSessionWithReservation({ |
| 82 | + cfg, |
| 83 | + sessionId: "explicit-123", |
| 84 | + }); |
| 85 | + expect(resolution.sessionId).toBe("explicit-123"); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not write a visible store row for internal handoffs (suppressVisibleSessionEffects)", async () => { |
| 90 | + await withTempStore(async ({ cfg, storePath }) => { |
| 91 | + const resolution = await resolveSessionWithReservation({ |
| 92 | + cfg, |
| 93 | + sessionKey, |
| 94 | + suppressVisibleSessionEffects: true, |
| 95 | + }); |
| 96 | + expect(resolution.isNewSession).toBe(true); |
| 97 | + expect(resolution.sessionId).toBeTruthy(); |
| 98 | + // Internal handoffs must not leak a visible session-store row. |
| 99 | + const persisted = loadSessionStore(storePath, { skipCache: true })[sessionKey]; |
| 100 | + expect(persisted).toBeUndefined(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("adopts a concurrent rebind instead of reverting it to the reserved id", async () => { |
| 105 | + await withTempStore(async ({ cfg, storePath }) => { |
| 106 | + const now = Date.now(); |
| 107 | + let releaseHold: () => void = () => {}; |
| 108 | + const held = new Promise<void>((resolve) => { |
| 109 | + releaseHold = resolve; |
| 110 | + }); |
| 111 | + let signalHeld: () => void = () => {}; |
| 112 | + const queueHeld = new Promise<void>((resolve) => { |
| 113 | + signalHeld = resolve; |
| 114 | + }); |
| 115 | + |
| 116 | + // Hold the per-store writer queue so the competing rebind and the |
| 117 | + // reservation's persist run under the lock in a deterministic order. |
| 118 | + const holder = withSessionStoreWriterForTest(storePath, async () => { |
| 119 | + signalHeld(); |
| 120 | + await held; |
| 121 | + }); |
| 122 | + await queueHeld; |
| 123 | + |
| 124 | + // A non-reservation writer (models /reset or another endpoint) claims the |
| 125 | + // key with a different sessionId; it is queued ahead of the reservation |
| 126 | + // persist while the reservation still observes an empty key. |
| 127 | + const rebind = replaceSessionEntry( |
| 128 | + { sessionKey, storePath }, |
| 129 | + { sessionId: "rebind-id", updatedAt: now, sessionStartedAt: now }, |
| 130 | + ); |
| 131 | + |
| 132 | + // The reservation mints its own id and its persist queues behind the |
| 133 | + // rebind, so the rebind wins the store write lock first. |
| 134 | + const reservation = resolveSessionWithReservation({ cfg, sessionKey }); |
| 135 | + await flushPendingWork(); |
| 136 | + |
| 137 | + releaseHold(); |
| 138 | + const [, resolution] = await Promise.all([rebind, reservation, holder]); |
| 139 | + |
| 140 | + // The reservation must not stamp its stale minted id back over the newer |
| 141 | + // rebind; it adopts the winning identity instead. |
| 142 | + expect(resolution.sessionId).toBe("rebind-id"); |
| 143 | + const persisted = loadSessionStore(storePath, { skipCache: true })[sessionKey]; |
| 144 | + expect(persisted?.sessionId).toBe("rebind-id"); |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments