Skip to content

Commit 41051fe

Browse files
harjothkharaclaudevincentkoc
authored
fix(imessage): keep fenced role-marker mapping keys out of the outbound stripper (#117159)
* fix(imessage): stop the outbound sanitizer deleting fenced role keys sanitizeOutboundText strips standalone `user:`/`system:`/`assistant:` lines (leaked turn boundaries), `#+#` separators and `assistant to=` markers, but the three regexes had no code-fence awareness. A bare YAML mapping key like `user:` on its own line inside a ```yaml block matched ROLE_TURN_MARKER_RE and was silently deleted before iMessage delivery, reparenting its children under the wrong node — still-valid YAML, wrong meaning, and nothing signalling a line was removed. Skip matches whose offset falls inside a markdown code region for all three patterns, reusing findCodeRegions/isInsideCode already applied by the sibling reflection-guard on this channel. Regions are recomputed per pass because each strip shifts later offsets. Leaked markers in prose are still stripped. Refs: #116942 Co-Authored-By: Claude Opus 4.8 <[email protected]> * test(imessage): cover code-aware marker stripping --------- Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Vincent Koc <[email protected]>
1 parent 75830f0 commit 41051fe

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎extensions/imessage/src/monitor/sanitize-outbound.test-support.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,39 @@ describe("sanitizeOutboundText", () => {
6363
expect(sanitizeOutboundText(text)).toBe("Hello\n\nWorld");
6464
});
6565

66+
it.each([
67+
{
68+
name: "backtick fence preserves user while stripping prose user",
69+
input: "user:\n```yaml\nuser:\n name: alice\n```",
70+
expected: "```yaml\nuser:\n name: alice\n```",
71+
},
72+
{
73+
name: "tilde fence preserves system while stripping prose system",
74+
input: "system:\n~~~yaml\nsystem:\n enabled: true\n~~~",
75+
expected: "~~~yaml\nsystem:\n enabled: true\n~~~",
76+
},
77+
{
78+
name: "indented code preserves assistant while stripping prose assistant",
79+
input: "keep\n\nassistant:\n\n assistant:\n enabled: true",
80+
expected: "keep\n\n assistant:\n enabled: true",
81+
},
82+
{
83+
name: "unterminated fence preserves every marker family after offset shifts",
84+
input: [
85+
"#+#+#",
86+
"assistant to=final",
87+
"user:",
88+
"```text",
89+
"#+#+#",
90+
"assistant to=tool",
91+
"user:",
92+
].join("\n"),
93+
expected: ["```text", "#+#+#", "assistant to=tool", "user:"].join("\n"),
94+
},
95+
] as const)("$name", ({ input, expected }) => {
96+
expect(sanitizeOutboundText(input)).toBe(expected);
97+
});
98+
6699
it("handles combined internal markers in one message", () => {
67100
const text = "<thinking>step 1</thinking>NO_REPLY +#+#+#+# assistant to=final\n\nActual reply";
68101
const result = sanitizeOutboundText(text);

‎extensions/imessage/src/monitor/sanitize-outbound.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Imessage plugin module implements sanitize outbound behavior.
2-
import { sanitizeAssistantVisibleText } from "openclaw/plugin-sdk/text-chunking";
2+
import {
3+
findCodeRegions,
4+
isInsideCode,
5+
sanitizeAssistantVisibleText,
6+
} from "openclaw/plugin-sdk/text-chunking";
37

48
/**
59
* Patterns that indicate assistant-internal metadata leaked into text.
@@ -11,6 +15,19 @@ const ASSISTANT_ROLE_MARKER_RE = /\bassistant\s+to\s*=\s*\w+/gi;
1115
// any line that merely ends with the word "user/system/assistant:" in prose.
1216
const ROLE_TURN_MARKER_RE = /^[ \t]*(?:user|system|assistant)\s*:\s*$/gm;
1317

18+
/**
19+
* Strip an internal-marker pattern from prose while leaving matches that fall
20+
* inside a fenced/indented/inline code region untouched: there a bare `user:`
21+
* mapping key or `#+#` line is the user's own content, not leaked scaffolding.
22+
* Regions are recomputed per call because the prior strip shifted later offsets.
23+
*/
24+
function stripMarkerOutsideCode(text: string, marker: RegExp): string {
25+
const codeRegions = findCodeRegions(text);
26+
return text.replace(marker, (match, offset: number) =>
27+
isInsideCode(offset, codeRegions) ? match : "",
28+
);
29+
}
30+
1431
/**
1532
* Strip all assistant-internal scaffolding from outbound text before delivery.
1633
* Applies reasoning/thinking tag removal, memory tag removal, and
@@ -23,9 +40,9 @@ export function sanitizeOutboundText(text: string): string {
2340

2441
let cleaned = sanitizeAssistantVisibleText(text);
2542

26-
cleaned = cleaned.replace(INTERNAL_SEPARATOR_RE, "");
27-
cleaned = cleaned.replace(ASSISTANT_ROLE_MARKER_RE, "");
28-
cleaned = cleaned.replace(ROLE_TURN_MARKER_RE, "");
43+
cleaned = stripMarkerOutsideCode(cleaned, INTERNAL_SEPARATOR_RE);
44+
cleaned = stripMarkerOutsideCode(cleaned, ASSISTANT_ROLE_MARKER_RE);
45+
cleaned = stripMarkerOutsideCode(cleaned, ROLE_TURN_MARKER_RE);
2946

3047
// Collapse excessive blank lines left after stripping.
3148
cleaned = cleaned.replace(/\n{3,}/g, "\n\n").trim();

0 commit comments

Comments
 (0)