Skip to content

Commit 00a823a

Browse files
committed
fix(imessage): text echo matching honors message-id conflicts; own sends stop feeding the loop limiter
Two silent-drop paths in the iMessage monitor: 1. The persisted 12h echo cache matched inbound user text against old outbound text even when the GUIDs conflicted — a user sending 'ok' within 12h of the agent sending 'ok' was dropped as an echo. The text branch now applies the same hasConflictingMessageIds guard the media branch has; genuine reconnect echoes still match by id or id-less entries. 2. Every from-me row fed the echo loop rate limiter, so a normal outbound burst (agent replies, multi-chunk sends, operator phone traffic) could trip the limiter and silently suppress the next legitimate inbound message. 'from me' is no longer counted as a loop signal, and a tripped limiter now logs a default-level warning once per conversation instead of verbose-only.
1 parent a23444f commit 00a823a

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎extensions/imessage/src/monitor/monitor-provider.echo-cache.test-support.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ describe("iMessage sent-message echo cache", () => {
167167
).toBe(true);
168168
});
169169

170+
it("does not match persisted text when both message ids differ", () => {
171+
// Outbound "ok" recorded with its GUID; a user later sends "ok" with a new
172+
// GUID. Same text, conflicting ids — a NEW message, not a reconnect echo.
173+
const scope = "acct:imessage:+1555";
174+
rememberPersistedIMessageEcho({ scope, text: "ok", messageId: "guid-agent-text" });
175+
176+
expect(hasPersistedIMessageEcho({ scope, text: "ok", messageId: "guid-user-text" })).toBe(
177+
false,
178+
);
179+
// Genuine echo still matches by id, and id-less probes still match by text.
180+
expect(hasPersistedIMessageEcho({ scope, text: "ok", messageId: "guid-agent-text" })).toBe(
181+
true,
182+
);
183+
expect(hasPersistedIMessageEcho({ scope, text: "ok" })).toBe(true);
184+
});
185+
170186
it("matches persisted media through the primary sent-message cache", () => {
171187
const scope = "acct:imessage:+1555";
172188
const media = { contentType: "image/png", kind: "image" as const };

‎extensions/imessage/src/monitor/monitor-provider.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,14 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
827827
if (decision.kind === "drop") {
828828
// Record echo/reflection drops so the rate limiter can detect sustained loops.
829829
// Only loop-related drop reasons feed the counter; policy/mention/empty drops
830-
// are normal and should not escalate.
830+
// are normal and should not escalate. "from me" is excluded: every own-send
831+
// (agent replies, multi-chunk sends, operator phone traffic) produces a
832+
// from-me row, so counting it lets a normal outbound burst trip the limiter
833+
// and silently suppress the next legitimate inbound message.
831834
const isLoopDrop =
832835
decision.reason === "echo" ||
833836
decision.reason === "self-chat echo" ||
834-
decision.reason === "reflected assistant content" ||
835-
decision.reason === "from me";
837+
decision.reason === "reflected assistant content";
836838
if (isLoopDrop) {
837839
loopRateLimiter.record(rateLimitKey);
838840
}
@@ -868,7 +870,15 @@ export async function monitorIMessageProvider(opts: MonitorIMessageOpts = {}): P
868870
// remaining messages as a safety net against amplification that slips
869871
// through the primary guards.
870872
if (decision.kind === "dispatch" && loopRateLimiter.isRateLimited(rateLimitKey)) {
871-
logVerbose(`imessage: rate-limited conversation ${conversationKey} (echo loop detected)`);
873+
// A tripped limiter silently eats real user messages — surface it at
874+
// default log level (once per conversation) instead of verbose-only.
875+
if (!loggedThrottledDropDiagnostics.check(`${rateLimitKey}:rate-limited`)) {
876+
runtime.log?.(
877+
warn(
878+
`[imessage:${accountInfo.accountId}] Suppressing inbound from ${conversationKey}: echo loop detected (rate limiter tripped)`,
879+
),
880+
);
881+
}
872882
return;
873883
}
874884

‎extensions/imessage/src/monitor/persisted-echo-cache.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,15 @@ export function hasPersistedIMessageEcho(params: {
225225
const hasConflictingMessageIds = Boolean(
226226
messageId && entry.messageId && messageId !== entry.messageId,
227227
);
228-
if (text && entry.text === text && (!entry.pending || params.includePendingText)) {
228+
// Same-id echoes match on the messageId branch above; an inbound message
229+
// whose GUID differs from the recorded outbound GUID is a NEW message even
230+
// when the text collides ("ok", "done"), never a reconnect echo.
231+
if (
232+
text &&
233+
!hasConflictingMessageIds &&
234+
entry.text === text &&
235+
(!entry.pending || params.includePendingText)
236+
) {
229237
return true;
230238
}
231239
if (

0 commit comments

Comments
 (0)