|
| 1 | +// Boundary proof: the outbound delivery mirror fences its transcript write |
| 2 | +// against the DESTINATION session, never against the ambient admitted run. |
| 3 | +import { expectDefined } from "@openclaw/normalization-core"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { withOwnedSessionTranscriptWrites } from "../../config/sessions/transcript-write-context.js"; |
| 6 | +import type { OwnedSessionTranscriptWriteContext } from "../../config/sessions/transcript-write-context.js"; |
| 7 | + |
| 8 | +const appendAssistantMessageToSessionTranscriptMock = vi.hoisted(() => |
| 9 | + vi.fn( |
| 10 | + async (_params: { |
| 11 | + sessionKey?: string; |
| 12 | + expectedWriterRunId?: string; |
| 13 | + expectedLifecycleRevision?: string; |
| 14 | + }) => ({ |
| 15 | + ok: true, |
| 16 | + target: { sessionId: "session-b", sessionKey: "session-b", storePath: "/tmp/store.sqlite" }, |
| 17 | + }), |
| 18 | + ), |
| 19 | +); |
| 20 | + |
| 21 | +vi.mock("../../config/sessions/transcript.runtime.js", () => ({ |
| 22 | + appendAssistantMessageToSessionTranscript: appendAssistantMessageToSessionTranscriptMock, |
| 23 | +})); |
| 24 | + |
| 25 | +const deliverTranscript = async () => |
| 26 | + await import("./deliver-transcript.js").then((module) => module.mirrorDeliveredPayloads); |
| 27 | + |
| 28 | +const passthroughWrite = async <T>(run: () => Promise<T> | T) => await run(); |
| 29 | + |
| 30 | +const ownedRunContext = (sessionKey: string): OwnedSessionTranscriptWriteContext => ({ |
| 31 | + sessionFile: `/tmp/sessions/${sessionKey}.jsonl`, |
| 32 | + sessionKey, |
| 33 | + sessionTarget: { |
| 34 | + agentId: "main", |
| 35 | + sessionId: `session-${sessionKey}`, |
| 36 | + sessionKey, |
| 37 | + storePath: "/tmp/agents/main/agent/openclaw-agent.sqlite", |
| 38 | + expectedLifecycleRevision: "rev-1", |
| 39 | + expectedWriterRunId: `run-${sessionKey}`, |
| 40 | + }, |
| 41 | + withTranscriptWrite: passthroughWrite, |
| 42 | +}); |
| 43 | + |
| 44 | +const mirrorParams = (sessionKey: string) => ({ |
| 45 | + delivery: { |
| 46 | + cfg: undefined, |
| 47 | + mirror: { |
| 48 | + agentId: "main", |
| 49 | + sessionKey, |
| 50 | + expectedSessionId: `session-${sessionKey}`, |
| 51 | + idempotencyKey: `mirror-${sessionKey}`, |
| 52 | + }, |
| 53 | + }, |
| 54 | + payloads: [{ text: "delivered", mediaUrls: [] }], |
| 55 | + channel: "discord", |
| 56 | + to: "channel:1", |
| 57 | +}); |
| 58 | + |
| 59 | +describe("outbound delivery transcript mirror", () => { |
| 60 | + beforeEach(() => { |
| 61 | + appendAssistantMessageToSessionTranscriptMock.mockClear(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not inherit the sending run's writer claim for another session", async () => { |
| 65 | + const mirrorDeliveredPayloads = await deliverTranscript(); |
| 66 | + await withOwnedSessionTranscriptWrites( |
| 67 | + ownedRunContext("agent:main:discord:c:1"), |
| 68 | + async () => await mirrorDeliveredPayloads(mirrorParams("agent:main:discord:c:2") as never), |
| 69 | + ); |
| 70 | + expect(appendAssistantMessageToSessionTranscriptMock).toHaveBeenCalledTimes(1); |
| 71 | + const appendParams = expectDefined( |
| 72 | + appendAssistantMessageToSessionTranscriptMock.mock.calls[0]?.[0], |
| 73 | + "transcript append call", |
| 74 | + ); |
| 75 | + expect(appendParams.sessionKey).toBe("agent:main:discord:c:2"); |
| 76 | + expect(appendParams.expectedWriterRunId).toBeUndefined(); |
| 77 | + expect(appendParams.expectedLifecycleRevision).toBeUndefined(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("keeps the sending run's writer claim when mirroring into its own session", async () => { |
| 81 | + const mirrorDeliveredPayloads = await deliverTranscript(); |
| 82 | + await withOwnedSessionTranscriptWrites( |
| 83 | + ownedRunContext("agent:main:discord:c:1"), |
| 84 | + async () => await mirrorDeliveredPayloads(mirrorParams("agent:main:discord:c:1") as never), |
| 85 | + ); |
| 86 | + const appendParams = expectDefined( |
| 87 | + appendAssistantMessageToSessionTranscriptMock.mock.calls[0]?.[0], |
| 88 | + "transcript append call", |
| 89 | + ); |
| 90 | + expect(appendParams.expectedWriterRunId).toBe("run-agent:main:discord:c:1"); |
| 91 | + expect(appendParams.expectedLifecycleRevision).toBe("rev-1"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments