Skip to content

Commit 17479ce

Browse files
authored
fix(auto-reply): suppress JSON-wrapped NO_REPLY payloads before channel delivery (#56612)
Add shared isSilentReplyPayloadText() detector that catches both bare NO_REPLY tokens and JSON {"action":"NO_REPLY"} envelopes. Apply at the reply directive parser, reply normalizer, and embedded agent payload builder so the control payload is stripped before any channel sees it. Preserves media when text is only a silent control envelope. Fixes #37727
1 parent ab2ef7b commit 17479ce

7 files changed

Lines changed: 104 additions & 5 deletions

File tree

‎src/agents/pi-embedded-runner/run/payloads.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,10 @@ describe("buildEmbeddedRunPayloads tool-error warnings", () => {
9090
didSendDeterministicApprovalPrompt: true,
9191
});
9292
});
93+
94+
it("suppresses JSON NO_REPLY assistant payloads", () => {
95+
expectNoPayloads({
96+
assistantTexts: ['{"action":"NO_REPLY"}'],
97+
});
98+
});
9399
});

‎src/agents/pi-embedded-runner/run/payloads.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AssistantMessage } from "@mariozechner/pi-ai";
22
import { hasOutboundReplyContent } from "openclaw/plugin-sdk/reply-payload";
33
import { parseReplyDirectives } from "../../../auto-reply/reply/reply-directives.js";
44
import type { ReasoningLevel, VerboseLevel } from "../../../auto-reply/thinking.js";
5-
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../../../auto-reply/tokens.js";
5+
import { isSilentReplyPayloadText, SILENT_REPLY_TOKEN } from "../../../auto-reply/tokens.js";
66
import { formatToolAggregate } from "../../../auto-reply/tool-meta.js";
77
import type { OpenClawConfig } from "../../../config/config.js";
88
import {
@@ -340,7 +340,7 @@ export function buildEmbeddedRunPayloads(params: {
340340
if (!hasOutboundReplyContent(p)) {
341341
return false;
342342
}
343-
if (p.text && isSilentReplyText(p.text, SILENT_REPLY_TOKEN)) {
343+
if (p.text && isSilentReplyPayloadText(p.text, SILENT_REPLY_TOKEN)) {
344344
return false;
345345
}
346346
return true;

‎src/auto-reply/reply/normalize-reply.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { hasReplyPayloadContent } from "../../interactive/payload.js";
33
import { stripHeartbeatToken } from "../heartbeat.js";
44
import {
55
HEARTBEAT_TOKEN,
6+
isSilentReplyPayloadText,
67
isSilentReplyText,
78
SILENT_REPLY_TOKEN,
89
stripSilentToken,
@@ -52,7 +53,7 @@ export function normalizeReplyPayload(
5253

5354
const silentToken = opts.silentToken ?? SILENT_REPLY_TOKEN;
5455
let text = payload.text ?? undefined;
55-
if (text && isSilentReplyText(text, silentToken)) {
56+
if (text && isSilentReplyPayloadText(text, silentToken)) {
5657
if (!hasContent("")) {
5758
opts.onSkip?.("silent");
5859
return null;

‎src/auto-reply/reply/reply-directives.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { splitMediaFromOutput } from "../../media/parse.js";
22
import { parseInlineDirectives } from "../../utils/directive-tags.js";
3-
import { isSilentReplyText, SILENT_REPLY_TOKEN } from "../tokens.js";
3+
import { isSilentReplyPayloadText, SILENT_REPLY_TOKEN } from "../tokens.js";
44

55
export type ReplyDirectiveParseResult = {
66
text: string;
@@ -31,7 +31,7 @@ export function parseReplyDirectives(
3131
}
3232

3333
const silentToken = options.silentToken ?? SILENT_REPLY_TOKEN;
34-
const isSilent = isSilentReplyText(text, silentToken);
34+
const isSilent = isSilentReplyPayloadText(text, silentToken);
3535
if (isSilent) {
3636
text = "";
3737
}

‎src/auto-reply/reply/reply-utils.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ describe("normalizeReplyPayload", () => {
141141
expect(reasons).toEqual(["silent"]);
142142
});
143143

144+
it("suppresses JSON NO_REPLY action payloads", () => {
145+
const reasons: string[] = [];
146+
const result = normalizeReplyPayload(
147+
{ text: '{"action":"NO_REPLY"}' },
148+
{ onSkip: (reason) => reasons.push(reason) },
149+
);
150+
expect(result).toBeNull();
151+
expect(reasons).toEqual(["silent"]);
152+
});
153+
154+
it("does not suppress JSON NO_REPLY objects with extra fields", () => {
155+
const result = normalizeReplyPayload({
156+
text: '{"action":"NO_REPLY","note":"example"}',
157+
});
158+
expect(result).not.toBeNull();
159+
expect(result!.text).toBe('{"action":"NO_REPLY","note":"example"}');
160+
});
161+
144162
it("strips NO_REPLY but keeps media payload", () => {
145163
const result = normalizeReplyPayload({
146164
text: "NO_REPLY",
@@ -151,6 +169,16 @@ describe("normalizeReplyPayload", () => {
151169
expect(result!.mediaUrl).toBe("https://example.com/img.png");
152170
});
153171

172+
it("strips JSON NO_REPLY action text but keeps media payload", () => {
173+
const result = normalizeReplyPayload({
174+
text: '{"action":"NO_REPLY"}',
175+
mediaUrl: "https://example.com/img.png",
176+
});
177+
expect(result).not.toBeNull();
178+
expect(result!.text).toBe("");
179+
expect(result!.mediaUrl).toBe("https://example.com/img.png");
180+
});
181+
154182
it("does not compile Slack directives unless interactive replies are enabled", () => {
155183
const result = normalizeReplyPayload({
156184
text: "hello [[slack_buttons: Retry:retry, Ignore:ignore]]",

‎src/auto-reply/tokens.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,43 @@ export function isSilentReplyText(
4040
return getSilentExactRegex(token).test(text);
4141
}
4242

43+
type SilentReplyActionEnvelope = { action?: unknown };
44+
45+
export function isSilentReplyEnvelopeText(
46+
text: string | undefined,
47+
token: string = SILENT_REPLY_TOKEN,
48+
): boolean {
49+
if (!text) {
50+
return false;
51+
}
52+
const trimmed = text.trim();
53+
if (!trimmed || !trimmed.startsWith("{") || !trimmed.endsWith("}") || !trimmed.includes(token)) {
54+
return false;
55+
}
56+
try {
57+
const parsed = JSON.parse(trimmed) as SilentReplyActionEnvelope;
58+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
59+
return false;
60+
}
61+
const keys = Object.keys(parsed);
62+
return (
63+
keys.length === 1 &&
64+
keys[0] === "action" &&
65+
typeof parsed.action === "string" &&
66+
parsed.action.trim() === token
67+
);
68+
} catch {
69+
return false;
70+
}
71+
}
72+
73+
export function isSilentReplyPayloadText(
74+
text: string | undefined,
75+
token: string = SILENT_REPLY_TOKEN,
76+
): boolean {
77+
return isSilentReplyText(text, token) || isSilentReplyEnvelopeText(text, token);
78+
}
79+
4380
/**
4481
* Strip a trailing silent reply token from mixed-content text.
4582
* Returns the remaining text with the token removed (trimmed).

‎src/infra/outbound/payloads.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,33 @@ describe("normalizeReplyPayloadsForDelivery", () => {
5252
]);
5353
});
5454

55+
it("drops JSON NO_REPLY action payloads without media", () => {
56+
expect(
57+
normalizeReplyPayloadsForDelivery([
58+
{ text: '{"action":"NO_REPLY"}' },
59+
{ text: "{\n \"action\": \"NO_REPLY\"\n}" },
60+
]),
61+
).toEqual([]);
62+
});
63+
64+
it("keeps JSON NO_REPLY objects that include extra fields", () => {
65+
expect(
66+
normalizeReplyPayloadsForDelivery([
67+
{ text: '{"action":"NO_REPLY","note":"example"}' },
68+
]),
69+
).toEqual([
70+
{
71+
text: '{"action":"NO_REPLY","note":"example"}',
72+
mediaUrls: undefined,
73+
mediaUrl: undefined,
74+
replyToId: undefined,
75+
replyToCurrent: false,
76+
replyToTag: false,
77+
audioAsVoice: false,
78+
},
79+
]);
80+
});
81+
5582
it("keeps renderable channel-data payloads and reply-to-current markers", () => {
5683
expect(
5784
normalizeReplyPayloadsForDelivery([

0 commit comments

Comments
 (0)