Skip to content

Commit 51bf8f8

Browse files
omarshahineclaude
andauthored
fix(messages): reply actions leak citation markers and reply/poll answers draw the no-reply fallback (#116909)
Sanitize citation control markers on the plugin-action reply path and mark authorized reply/poll deliveries to the current conversation as current-source, so message-tool answers no longer arrive with raw cite glyphs or a false "No reply was generated" fallback. Live iMessage proof (patched vs unpatched same-device control) in the PR body. Co-authored-by: Claude Fable 5 <[email protected]>
1 parent a0c98de commit 51bf8f8

6 files changed

Lines changed: 438 additions & 40 deletions

‎src/agents/embedded-agent-message-tool-source-reply.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,34 @@ describe("isDeliveredMessageToolOnlySourceReplyResult", () => {
367367
).toBe(false);
368368
});
369369

370+
it("accepts a confirmed current-source poll delivery", () => {
371+
expect(
372+
isDeliveredMessageToolOnlySourceReplyResult({
373+
sourceReplyDeliveryMode: "message_tool_only",
374+
toolName: "message",
375+
args: { action: "poll", pollQuestion: "Preferred default?", pollOption: ["a", "b"] },
376+
result: {
377+
details: {
378+
ok: true,
379+
pollId: "poll-1",
380+
sourceReplyRoute: "current-source",
381+
},
382+
},
383+
}),
384+
).toBe(true);
385+
});
386+
387+
it("rejects an unconfirmed poll delivery", () => {
388+
expect(
389+
isDeliveredMessageToolOnlySourceReplyResult({
390+
sourceReplyDeliveryMode: "message_tool_only",
391+
toolName: "message",
392+
args: { action: "poll", pollQuestion: "Preferred default?", pollOption: ["a", "b"] },
393+
result: { details: { ok: true, pollId: "poll-1" } },
394+
}),
395+
).toBe(false);
396+
});
397+
370398
it("accepts only confirmed implicit message sends", () => {
371399
expect(
372400
isDeliveredMessageToolOnlySourceReplyResult({

‎src/agents/embedded-agent-message-tool-source-reply.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ function isMessageToolSourceReplyActionName(action: unknown): boolean {
6363
if (typeof action !== "string") {
6464
return false;
6565
}
66+
// Polls and reply-type actions deliver the visible source answer too; they
67+
// qualify only when the runner confirmed the current-source route (or the
68+
// caller allows explicit routes), enforced by the caller below.
6669
const normalized = action.trim().toLowerCase();
67-
return normalized === "reply" || normalized === "thread-reply";
70+
return normalized === "reply" || normalized === "thread-reply" || normalized === "poll";
6871
}
6972

7073
function normalizeStatus(value: unknown): string | undefined {

‎src/infra/outbound/message-action-runner.plugin-dispatch.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ const mocks = vi.hoisted(() => ({
105105
prepareOutboundMirrorRoute: vi.fn(),
106106
beginTerminalSourceReplyDelivery: vi.fn(),
107107
cancelTerminalSourceReplyDelivery: vi.fn(),
108+
isCurrentSourceReplyActionName: vi.fn(() => false),
108109
isDeliveredCurrentSourceReply: vi.fn(() => false),
110+
isDeliveredCurrentSourceReplyAction: vi.fn(() => false),
109111
reconcileTerminalSourceReplyDelivery: vi.fn(),
110112
}));
111113

@@ -131,7 +133,9 @@ vi.mock("./message.gateway.runtime.js", () => ({
131133
vi.mock("./source-reply-mirror.js", () => ({
132134
beginTerminalSourceReplyDelivery: mocks.beginTerminalSourceReplyDelivery,
133135
cancelTerminalSourceReplyDelivery: mocks.cancelTerminalSourceReplyDelivery,
136+
isCurrentSourceReplyActionName: mocks.isCurrentSourceReplyActionName,
134137
isDeliveredCurrentSourceReply: mocks.isDeliveredCurrentSourceReply,
138+
isDeliveredCurrentSourceReplyAction: mocks.isDeliveredCurrentSourceReplyAction,
135139
reconcileTerminalSourceReplyDelivery: mocks.reconcileTerminalSourceReplyDelivery,
136140
}));
137141

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
// Covers reply-type plugin actions: outbound text hygiene (citation control
2+
// markers) and current-source delivery marking for implicit reply routes.
3+
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import type { ChannelPlugin } from "../../channels/plugins/types.public.js";
5+
import type { OpenClawConfig } from "../../config/config.js";
6+
import { setActivePluginRegistry } from "../../plugins/runtime.js";
7+
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
8+
import { runMessageAction } from "./message-action-runner.js";
9+
10+
type ChannelActionHandler = NonNullable<NonNullable<ChannelPlugin["actions"]>["handleAction"]>;
11+
12+
const testchatConfig = {
13+
channels: {
14+
testchat: {
15+
enabled: true,
16+
},
17+
},
18+
} as OpenClawConfig;
19+
20+
const CITATION_MARKED_MESSAGE = "Ayutthaya Thai is my pick. citeturn2search9turn2search6";
21+
22+
function createReplyActionPlugin(handleAction: ChannelActionHandler): ChannelPlugin {
23+
return {
24+
id: "testchat",
25+
meta: {
26+
id: "testchat",
27+
label: "Test Chat",
28+
selectionLabel: "Test Chat",
29+
docsPath: "/channels/testchat",
30+
blurb: "Reply action test plugin.",
31+
},
32+
capabilities: { chatTypes: ["direct"] },
33+
config: {
34+
listAccountIds: () => ["default"],
35+
resolveAccount: () => ({ enabled: true }),
36+
isConfigured: () => true,
37+
},
38+
outbound: {
39+
deliveryMode: "direct",
40+
sendText: async () => ({ channel: "testchat", messageId: "m-send-1" }),
41+
},
42+
messaging: {
43+
targetResolver: {
44+
looksLikeId: () => true,
45+
},
46+
},
47+
actions: {
48+
describeMessageTool: () => ({ actions: ["reply", "poll"] }),
49+
supportsAction: ({ action }) => action === "reply" || action === "poll",
50+
handleAction,
51+
},
52+
};
53+
}
54+
55+
function registerReplyPlugin() {
56+
const payload = { ok: true, messageId: "m-reply-1", repliedTo: "platform-guid-1" };
57+
const handleAction = vi.fn(async () => ({
58+
content: [{ type: "text" as const, text: JSON.stringify(payload) }],
59+
details: payload,
60+
}));
61+
setActivePluginRegistry(
62+
createTestRegistry([
63+
{ pluginId: "testchat", source: "test", plugin: createReplyActionPlugin(handleAction) },
64+
]),
65+
);
66+
return handleAction;
67+
}
68+
69+
function readHandledParams(handleAction: {
70+
mock: { calls: readonly unknown[][] };
71+
}): Record<string, unknown> {
72+
const [call] = handleAction.mock.calls;
73+
const arg = call?.[0];
74+
if (typeof arg !== "object" || arg === null || Array.isArray(arg)) {
75+
throw new Error("expected plugin handleAction call");
76+
}
77+
const params = (arg as Record<string, unknown>).params;
78+
if (typeof params !== "object" || params === null || Array.isArray(params)) {
79+
throw new Error("expected plugin handleAction params");
80+
}
81+
return params as Record<string, unknown>;
82+
}
83+
84+
async function runReplyAction(params: {
85+
actionParams: Record<string, unknown>;
86+
currentMessageId?: string | number;
87+
}) {
88+
const toolContext = {
89+
currentChannelProvider: "testchat" as const,
90+
currentChannelId: "direct:user-1",
91+
...(params.currentMessageId !== undefined ? { currentMessageId: params.currentMessageId } : {}),
92+
};
93+
return await runMessageAction({
94+
cfg: testchatConfig,
95+
action: "reply",
96+
params: { channel: "testchat", ...params.actionParams },
97+
toolContext,
98+
messageActionAuthorization: {
99+
requesterAccountId: "default",
100+
toolContext,
101+
},
102+
sessionKey: "agent:main:testchat:direct:user-1",
103+
defaultAccountId: "default",
104+
sourceReplyDeliveryMode: "message_tool_only",
105+
dryRun: false,
106+
});
107+
}
108+
109+
async function runPollAction(params: { to: string }) {
110+
const toolContext = {
111+
currentChannelProvider: "testchat" as const,
112+
currentChannelId: "direct:user-1",
113+
currentMessageId: "1783",
114+
};
115+
return await runMessageAction({
116+
cfg: testchatConfig,
117+
action: "poll",
118+
params: {
119+
channel: "testchat",
120+
to: params.to,
121+
pollQuestion: "Preferred default?",
122+
pollOption: ["Tell me right away", "Only important"],
123+
},
124+
toolContext,
125+
messageActionAuthorization: {
126+
requesterAccountId: "default",
127+
toolContext,
128+
},
129+
sessionKey: "agent:main:testchat:direct:user-1",
130+
defaultAccountId: "default",
131+
sourceReplyDeliveryMode: "message_tool_only",
132+
dryRun: false,
133+
});
134+
}
135+
136+
describe("runMessageAction reply-type plugin actions", () => {
137+
afterEach(() => {
138+
setActivePluginRegistry(createTestRegistry([]));
139+
});
140+
141+
it("strips citation control markers from reply text before plugin dispatch", async () => {
142+
const handleAction = registerReplyPlugin();
143+
144+
await runReplyAction({
145+
actionParams: { message: CITATION_MARKED_MESSAGE, messageId: "1783" },
146+
currentMessageId: "1783",
147+
});
148+
149+
const handledParams = readHandledParams(handleAction);
150+
expect(handledParams.message).toBe("Ayutthaya Thai is my pick.");
151+
});
152+
153+
it("marks replies to the run's inbound message as current-source deliveries", async () => {
154+
registerReplyPlugin();
155+
156+
const result = await runReplyAction({
157+
actionParams: { message: "visible reply", messageId: "1783" },
158+
currentMessageId: "1783",
159+
});
160+
161+
expect(result.kind).toBe("action");
162+
expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" });
163+
const details = "toolResult" in result ? result.toolResult?.details : undefined;
164+
expect(details).toMatchObject({ sourceReplyRoute: "current-source" });
165+
});
166+
167+
it("matches numeric replied-to message ids against string tool-context ids", async () => {
168+
registerReplyPlugin();
169+
170+
const result = await runReplyAction({
171+
actionParams: { message: "visible reply", messageId: 1783 },
172+
currentMessageId: "1783",
173+
});
174+
175+
expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" });
176+
});
177+
178+
it("leaves replies to other messages unmarked", async () => {
179+
registerReplyPlugin();
180+
181+
const result = await runReplyAction({
182+
actionParams: { message: "visible reply", messageId: "999" },
183+
currentMessageId: "1783",
184+
});
185+
186+
expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined();
187+
});
188+
189+
it("leaves explicitly targeted replies unmarked", async () => {
190+
registerReplyPlugin();
191+
192+
const result = await runReplyAction({
193+
actionParams: {
194+
message: "visible reply",
195+
messageId: "1783",
196+
to: "direct:someone-else",
197+
},
198+
currentMessageId: "1783",
199+
});
200+
201+
expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined();
202+
});
203+
204+
it("marks polls sent to the current conversation as current-source deliveries", async () => {
205+
registerReplyPlugin();
206+
207+
const result = await runPollAction({ to: "direct:user-1" });
208+
209+
expect(result.kind).toBe("poll");
210+
expect(result.payload).toMatchObject({ sourceReplyRoute: "current-source" });
211+
});
212+
213+
it("leaves polls sent to other conversations unmarked", async () => {
214+
registerReplyPlugin();
215+
216+
const result = await runPollAction({ to: "direct:someone-else" });
217+
218+
expect((result.payload as { sourceReplyRoute?: unknown }).sourceReplyRoute).toBeUndefined();
219+
});
220+
});

0 commit comments

Comments
 (0)