Skip to content

Commit f612443

Browse files
ruel225claude
andcommitted
fix(outbound): scope the outbound transcript mirror fence to its destination
Outbound mirror writes fetched the ambient transcript writer fence with no destination scoping, and the ambient context carries the sending run's session target, so every session-key-only call failed the store-identity match and silently dropped the fence. Mirror appends then lost optimistic-concurrency protection against the owning writer. Match the fence to the mirror's destination session key instead, and let session-key-only callers resolve the ambient claim through the context's target session key. Co-Authored-By: Claude <[email protected]>
1 parent b4f8510 commit f612443

7 files changed

Lines changed: 178 additions & 6 deletions

‎src/config/sessions/transcript-write-context.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
type SessionTranscriptRuntimeTarget,
1313
} from "./session-accessor.js";
1414
import {
15+
getOwnedSessionTranscriptWriterFence,
1516
SessionTranscriptWriterClaimReboundError,
1617
withOwnedSessionTranscriptWrites,
18+
type OwnedSessionTranscriptWriteContext,
1719
} from "./transcript-write-context.js";
1820

1921
async function withWriteTarget(run: (target: SessionTranscriptRuntimeTarget) => Promise<void>) {
@@ -134,3 +136,68 @@ describe("owned transcript commit boundary", () => {
134136
},
135137
);
136138
});
139+
140+
const passthroughWrite = async <T>(run: () => Promise<T> | T) => await run();
141+
142+
const ownedRunContext = (sessionKey: string): OwnedSessionTranscriptWriteContext => ({
143+
sessionFile: `/tmp/sessions/${sessionKey}.jsonl`,
144+
sessionKey,
145+
sessionTarget: {
146+
agentId: "main",
147+
sessionId: `session-${sessionKey}`,
148+
sessionKey,
149+
storePath: "/tmp/agents/main/agent/openclaw-agent.sqlite",
150+
expectedLifecycleRevision: "rev-1",
151+
expectedWriterRunId: `run-${sessionKey}`,
152+
},
153+
withTranscriptWrite: passthroughWrite,
154+
});
155+
156+
describe("owned transcript writer fence destination matching", () => {
157+
it("returns the admitted run claim for the owning session key", async () => {
158+
await withOwnedSessionTranscriptWrites(ownedRunContext("agent:main:discord:c:1"), async () => {
159+
expect(
160+
getOwnedSessionTranscriptWriterFence({ sessionKey: "agent:main:discord:c:1" }),
161+
).toEqual({
162+
expectedLifecycleRevision: "rev-1",
163+
expectedWriterRunId: "run-agent:main:discord:c:1",
164+
});
165+
});
166+
});
167+
168+
it("returns no claim for a different destination session key", async () => {
169+
await withOwnedSessionTranscriptWrites(ownedRunContext("agent:main:discord:c:1"), async () => {
170+
expect(
171+
getOwnedSessionTranscriptWriterFence({ sessionKey: "agent:main:discord:c:2" }),
172+
).toBeUndefined();
173+
});
174+
});
175+
176+
it("keeps matching the full store identity when the caller supplies a target", async () => {
177+
await withOwnedSessionTranscriptWrites(ownedRunContext("agent:main:discord:c:1"), async () => {
178+
expect(
179+
getOwnedSessionTranscriptWriterFence({
180+
sessionKey: "agent:main:discord:c:1",
181+
sessionTarget: {
182+
agentId: "main",
183+
sessionId: "session-agent:main:discord:c:1",
184+
sessionKey: "agent:main:discord:c:1",
185+
storePath: "/tmp/agents/main/agent/openclaw-agent.sqlite",
186+
},
187+
}),
188+
).toEqual({
189+
expectedLifecycleRevision: "rev-1",
190+
expectedWriterRunId: "run-agent:main:discord:c:1",
191+
});
192+
expect(
193+
getOwnedSessionTranscriptWriterFence({
194+
sessionKey: "agent:main:discord:c:1",
195+
sessionTarget: {
196+
sessionKey: "agent:main:discord:c:1",
197+
storePath: "/tmp/agents/other/agent/openclaw-agent.sqlite",
198+
},
199+
}),
200+
).toBeUndefined();
201+
});
202+
});
203+
});

‎src/config/sessions/transcript-write-context.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ function contextMatches(params: {
6565
};
6666
const contextTarget = normalizeTarget(params.context.sessionTarget);
6767
const requestedTarget = normalizeTarget(params.sessionTarget);
68-
if (params.context.sessionTarget || params.sessionTarget) {
68+
// A caller that passes a session target owns the full store identity, so an
69+
// unresolvable target never matches. Callers that pass only a session key --
70+
// outbound delivery mirrors naming their destination session -- fall through
71+
// to session-identity matching instead of inheriting the ambient claim.
72+
if (params.sessionTarget !== undefined) {
6973
return Boolean(
7074
contextTarget &&
7175
requestedTarget &&
@@ -85,7 +89,9 @@ function contextMatches(params: {
8589
return contextSessionFile === sessionFile;
8690
}
8791

88-
const contextSessionKey = params.context.sessionKey?.trim();
92+
const contextSessionKey = (
93+
params.context.sessionTarget?.sessionKey ?? params.context.sessionKey
94+
)?.trim();
8995
const sessionKey = params.sessionKey?.trim();
9096
return Boolean(contextSessionKey && sessionKey && contextSessionKey === sessionKey);
9197
}

‎src/gateway/internal-source-reply-persistence.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ export async function persistInternalSourceReply(params: {
171171
...(params.payload.text ? [{ type: "text", text: params.payload.text }] : []),
172172
...mediaBlocks,
173173
];
174-
const writerFence = getOwnedSessionTranscriptWriterFence();
174+
// Fence against the mirror destination, not the sending run's own session.
175+
const writerFence = getOwnedSessionTranscriptWriterFence({ sessionKey: params.sessionKey });
175176
const appended = await appendAssistantMessageToSessionTranscript({
176177
agentId: params.agentId,
177178
sessionKey: params.sessionKey,
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
});

‎src/infra/outbound/deliver-transcript.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export async function mirrorDeliveredPayloads(params: {
4040
// Keep mirror failures non-fatal so callers do not retry an already-sent payload.
4141
try {
4242
const { appendAssistantMessageToSessionTranscript } = await loadTranscriptRuntime();
43-
const writerFence = getOwnedSessionTranscriptWriterFence();
43+
// Fence against the mirror destination, not the sending run's own session.
44+
const writerFence = getOwnedSessionTranscriptWriterFence({ sessionKey: mirror.sessionKey });
4445
const mirrorResult = await appendAssistantMessageToSessionTranscript({
4546
agentId: mirror.agentId,
4647
sessionKey: mirror.sessionKey,

‎src/infra/outbound/outbound-send-service.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,10 @@ export async function executeSendAction(params: {
446446
params.mediaUrls ??
447447
(params.mediaUrl ? [params.mediaUrl] : undefined);
448448
try {
449-
const writerFence = getOwnedSessionTranscriptWriterFence();
449+
// Fence against the mirror destination, not the sending run's own session.
450+
const writerFence = getOwnedSessionTranscriptWriterFence({
451+
sessionKey: params.ctx.mirror.sessionKey,
452+
});
450453
const mirrorResult = await appendAssistantMessageToSessionTranscript({
451454
agentId: params.ctx.mirror.agentId,
452455
sessionKey: params.ctx.mirror.sessionKey,

‎src/infra/outbound/source-reply-mirror.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ export async function mirrorDeliveredSourceReplyToTranscript(
594594
return false;
595595
}
596596
const sourceTurnId = resolveCurrentSourceTurnId(params.toolContext);
597-
const writerFence = getOwnedSessionTranscriptWriterFence();
597+
// Fence against the mirror destination, not the sending run's own session.
598+
const writerFence = getOwnedSessionTranscriptWriterFence({ sessionKey: params.sessionKey });
598599
const result = await appendAssistantMessageToSessionTranscript({
599600
agentId: params.agentId,
600601
sessionKey: params.sessionKey,

0 commit comments

Comments
 (0)