fix(imessage): strip leading echo corruption markers in the persisted… · openclaw/openclaw@81e1ec4
ly-wang19
·
2026-06-23
·
via Recent Commits to openclaw:main
File tree
extensions/imessage/src/monitor
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | // Imessage plugin module implements echo cache behavior. |
| 2 | +import { stripLeadingEchoTextCorruptionMarkers } from "./echo-text-corruption.js"; |
2 | 3 | import { hasPersistedIMessageEcho } from "./persisted-echo-cache.js"; |
3 | 4 | |
4 | 5 | type SentMessageLookup = { |
@@ -35,20 +36,6 @@ export type SentMessageCache = {
|
35 | 36 | const SENT_MESSAGE_TEXT_TTL_MS = 4_000; |
36 | 37 | const SENT_MESSAGE_ID_TTL_MS = 60_000; |
37 | 38 | |
38 | | -function isLeadingEchoTextCorruptionMarker(code: number): boolean { |
39 | | -return ( |
40 | | -code === 0x0000 || code === 0xfeff || code === 0xfffd || code === 0xfffe || code === 0xffff |
41 | | -); |
42 | | -} |
43 | | - |
44 | | -function stripLeadingEchoTextCorruptionMarkers(text: string): string { |
45 | | -let offset = 0; |
46 | | -while (offset < text.length && isLeadingEchoTextCorruptionMarker(text.charCodeAt(offset))) { |
47 | | -offset += 1; |
48 | | -} |
49 | | -return offset === 0 ? text : text.slice(offset); |
50 | | -} |
51 | | - |
52 | 39 | function normalizeEchoTextKey(text: string | undefined): string | null { |
53 | 40 | if (!text) { |
54 | 41 | return null; |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// Imessage plugin shared helper: strip leading attributedBody corruption markers from echo text. |
| 2 | +// The in-memory (echo-cache) and persisted (persisted-echo-cache) echo-dedupe paths must normalize |
| 3 | +// identically, so a reflected own-message echo whose attributedBody decoded with a leading |
| 4 | +// NUL/replacement marker still matches the clean stored send. Kept here (a leaf module with no |
| 5 | +// imports) so the persisted path can reuse it without an echo-cache <-> persisted-echo-cache cycle. |
| 6 | + |
| 7 | +function isLeadingEchoTextCorruptionMarker(code: number): boolean { |
| 8 | +return ( |
| 9 | +code === 0x0000 || code === 0xfeff || code === 0xfffd || code === 0xfffe || code === 0xffff |
| 10 | +); |
| 11 | +} |
| 12 | + |
| 13 | +export function stripLeadingEchoTextCorruptionMarkers(text: string): string { |
| 14 | +let offset = 0; |
| 15 | +while (offset < text.length && isLeadingEchoTextCorruptionMarker(text.charCodeAt(offset))) { |
| 16 | +offset += 1; |
| 17 | +} |
| 18 | +return offset === 0 ? text : text.slice(offset); |
| 19 | +} |
| Original file line number | Diff line number | Diff line change |
|---|
@@ -91,6 +91,21 @@ describe("iMessage sent-message echo cache", () => {
|
91 | 91 | expect(cache.has("acct:imessage:+1555", { text: "Delayed\u0000echo reply" })).toBe(false); |
92 | 92 | }); |
93 | 93 | |
| 94 | +it("matches a delayed reflected echo with leading corruption markers via the persisted cache", () => { |
| 95 | +// The persisted 12h cache is the only matcher once the 4s in-memory text TTL expires, so it |
| 96 | +// must strip leading attributedBody corruption markers exactly like the in-memory key (#93511). |
| 97 | +const scope = "acct:imessage:+1555"; |
| 98 | +const markers = String.fromCharCode(0x0000, 0xfffd, 0xfffe, 0xffff, 0xfeff); |
| 99 | +rememberPersistedIMessageEcho({ scope, text: "Delayed echo reply" }); |
| 100 | + |
| 101 | +expect(hasPersistedIMessageEcho({ scope, text: "Delayed echo reply" })).toBe(true); |
| 102 | +expect(hasPersistedIMessageEcho({ scope, text: `${markers}Delayed echo reply` })).toBe(true); |
| 103 | +// Leading-only: a mid-string marker stays distinct. |
| 104 | +expect( |
| 105 | +hasPersistedIMessageEcho({ scope, text: `Delayed${String.fromCharCode(0x0000)}echo reply` }), |
| 106 | +).toBe(false); |
| 107 | +}); |
| 108 | + |
94 | 109 | it("matches by outbound message id and ignores placeholder ids", () => { |
95 | 110 | vi.useFakeTimers(); |
96 | 111 | vi.setSystemTime(new Date("2026-02-25T00:00:00Z")); |
|
| Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ import { createHash } from "node:crypto";
|
3 | 3 | import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime"; |
4 | 4 | import { logVerbose } from "openclaw/plugin-sdk/runtime-env"; |
5 | 5 | import { getIMessageRuntime } from "../runtime.js"; |
| 6 | +import { stripLeadingEchoTextCorruptionMarkers } from "./echo-text-corruption.js"; |
6 | 7 | |
7 | 8 | type PersistedEchoEntry = { |
8 | 9 | scope: string; |
@@ -26,7 +27,14 @@ export const IMESSAGE_SENT_ECHOES_MAX_ENTRIES = 256;
|
26 | 27 | type PersistedEchoStore = PluginStateSyncKeyedStore<PersistedEchoEntry>; |
27 | 28 | |
28 | 29 | function normalizeText(text: string | undefined): string | undefined { |
29 | | -const normalized = text?.replace(/\r\n?/g, "\n").trim(); |
| 30 | +if (!text) { |
| 31 | +return undefined; |
| 32 | +} |
| 33 | +// Match the in-memory echo-cache key so a reflected echo with a leading attributedBody |
| 34 | +// corruption marker still matches the clean stored send (the persisted sibling of #93511). |
| 35 | +const normalized = stripLeadingEchoTextCorruptionMarkers( |
| 36 | +text.replace(/\r\n?/g, "\n").trim(), |
| 37 | +).trim(); |
30 | 38 | return normalized || undefined; |
31 | 39 | } |
32 | 40 | |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。