|
| 1 | +// Mirror fence tests cover which session's writer claim a delivery mirror carries. |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { withOwnedSessionTranscriptWrites } from "../../config/sessions/transcript-write-context.js"; |
| 4 | +import type { DeliverOutboundPayloadsCoreParams } from "./deliver-contracts.js"; |
| 5 | +import { mirrorDeliveredPayloads } from "./deliver-transcript.js"; |
| 6 | +import type { NormalizedOutboundPayload } from "./payloads.js"; |
| 7 | + |
| 8 | +const mocks = vi.hoisted(() => ({ |
| 9 | + // Typed with the append params so the recorded call is inspectable without a cast. |
| 10 | + appendAssistantMessageToSessionTranscript: vi.fn( |
| 11 | + async (_params: Record<string, unknown>) => ({ ok: true }) as const, |
| 12 | + ), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("../../config/sessions/transcript.runtime.js", async () => { |
| 16 | + const actual = await vi.importActual< |
| 17 | + typeof import("../../config/sessions/transcript.runtime.js") |
| 18 | + >("../../config/sessions/transcript.runtime.js"); |
| 19 | + return { |
| 20 | + ...actual, |
| 21 | + appendAssistantMessageToSessionTranscript: mocks.appendAssistantMessageToSessionTranscript, |
| 22 | + }; |
| 23 | +}); |
| 24 | + |
| 25 | +const RUNNING_SESSION_KEY = "agent:wolf:discord:channel:1497965766035640391"; |
| 26 | +const OTHER_SESSION_KEY = "agent:arthur:discord:channel:1538510183024689305"; |
| 27 | + |
| 28 | +function payload(text: string): NormalizedOutboundPayload { |
| 29 | + return { text, mediaUrls: [] }; |
| 30 | +} |
| 31 | + |
| 32 | +async function mirrorInto(sessionKey: string): Promise<void> { |
| 33 | + await mirrorDeliveredPayloads({ |
| 34 | + delivery: { |
| 35 | + cfg: {}, |
| 36 | + mirror: { agentId: "wolf", sessionKey }, |
| 37 | + } as unknown as DeliverOutboundPayloadsCoreParams, |
| 38 | + payloads: [payload("delivered to the user")], |
| 39 | + channel: "discord", |
| 40 | + to: "1497965766035640391", |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/** Runs a mirror inside a run that owns `RUNNING_SESSION_KEY`, the way a live delivery does. */ |
| 45 | +async function withRunningSession(run: () => Promise<void>): Promise<void> { |
| 46 | + await withOwnedSessionTranscriptWrites( |
| 47 | + { |
| 48 | + sessionKey: RUNNING_SESSION_KEY, |
| 49 | + sessionTarget: { |
| 50 | + agentId: "wolf", |
| 51 | + sessionKey: RUNNING_SESSION_KEY, |
| 52 | + storePath: "/state/agents/wolf/openclaw-agent.sqlite", |
| 53 | + expectedLifecycleRevision: "rev-7", |
| 54 | + expectedWriterRunId: "run-running", |
| 55 | + }, |
| 56 | + withTranscriptWrite: async (operation) => await operation(), |
| 57 | + }, |
| 58 | + run, |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +function appendedArgs() { |
| 63 | + return mocks.appendAssistantMessageToSessionTranscript.mock.calls[0]?.[0]; |
| 64 | +} |
| 65 | + |
| 66 | +describe("outbound delivery mirror writer fence", () => { |
| 67 | + beforeEach(() => { |
| 68 | + mocks.appendAssistantMessageToSessionTranscript.mockClear(); |
| 69 | + mocks.appendAssistantMessageToSessionTranscript.mockResolvedValue({ ok: true } as const); |
| 70 | + }); |
| 71 | + |
| 72 | + it("carries the running run's fence when it mirrors into that run's own session", async () => { |
| 73 | + await withRunningSession(async () => { |
| 74 | + await mirrorInto(RUNNING_SESSION_KEY); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(appendedArgs()).toMatchObject({ |
| 78 | + sessionKey: RUNNING_SESSION_KEY, |
| 79 | + expectedWriterRunId: "run-running", |
| 80 | + expectedLifecycleRevision: "rev-7", |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("withholds that fence when it mirrors into a different session", async () => { |
| 85 | + await withRunningSession(async () => { |
| 86 | + await mirrorInto(OTHER_SESSION_KEY); |
| 87 | + }); |
| 88 | + |
| 89 | + const args = appendedArgs(); |
| 90 | + expect(args).toMatchObject({ sessionKey: OTHER_SESSION_KEY }); |
| 91 | + // A claim about the delivering session is not a claim about this one. Passing it made |
| 92 | + // the transcript guards refuse the append as "session rebound", and the refusal is |
| 93 | + // warn-only after a successful channel send, so the mirror was lost for good. |
| 94 | + expect(args).not.toHaveProperty("expectedWriterRunId"); |
| 95 | + expect(args).not.toHaveProperty("expectedLifecycleRevision"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("mirrors unfenced when no run owns a transcript write", async () => { |
| 99 | + await mirrorInto(RUNNING_SESSION_KEY); |
| 100 | + |
| 101 | + const args = appendedArgs(); |
| 102 | + expect(args).toMatchObject({ sessionKey: RUNNING_SESSION_KEY }); |
| 103 | + expect(args).not.toHaveProperty("expectedWriterRunId"); |
| 104 | + }); |
| 105 | +}); |
0 commit comments