openclaw/extensions/imessage/src/monitor/loop-rate-limiter.test.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

fix(imessage): prevent echo loop from leaking internal metadata and amplifying NO_REPLY into queue overflow (#33295) * fix(imessage): prevent echo loop from leaking internal metadata and amplifying NO_REPLY into queue overflow - Add outbound sanitization at channel boundary (sanitize-outbound.ts): strips thinking/reasoning tags, relevant-memories tags, model-specific separators (+#+#), and assistant role markers before iMessage delivery - Add inbound reflection guard (reflection-guard.ts): detects and drops messages containing assistant-internal markers that indicate a reflected outbound message, preventing recursive echo amplification - Harden echo cache: increase text TTL from 5s to 30s to catch delayed reflections that previously expired before the echo could be detected - Add loop rate limiter (loop-rate-limiter.ts): per-conversation rapid-fire detection that suppresses conversations exceeding threshold within a time window, acting as a safety net against amplification Closes #33281 * fix(imessage): address review — stricter reflection regex, loop-aware rate limiter - Reflection guard: require closing > bracket on thinking/final/memory tag patterns to prevent false-positives on user phrases like '<final answer>' or '<thought experiment>' (#33295 review) - Rate limiter: only record echo/reflection/from-me drops instead of all dispatches, so the limiter acts as a loop-specific escalation mechanism rather than a general throttle on normal conversation velocity (#33295 review) * Changelog: add iMessage echo-loop hardening entry * iMessage: restore short echo-text TTL * iMessage: ignore reflection markers in code --------- Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-03-06 16:19:57 -08:00
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createLoopRateLimiter } from "./loop-rate-limiter.js";
describe("createLoopRateLimiter", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it("allows messages below the threshold", () => {
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 3 });
limiter.record("conv:1");
limiter.record("conv:1");
expect(limiter.isRateLimited("conv:1")).toBe(false);
});
it("rate limits at the threshold", () => {
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 3 });
limiter.record("conv:1");
limiter.record("conv:1");
limiter.record("conv:1");
expect(limiter.isRateLimited("conv:1")).toBe(true);
});
it("does not cross-contaminate conversations", () => {
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 2 });
limiter.record("conv:1");
limiter.record("conv:1");
expect(limiter.isRateLimited("conv:1")).toBe(true);
expect(limiter.isRateLimited("conv:2")).toBe(false);
});
it("resets after the time window expires", () => {
const limiter = createLoopRateLimiter({ windowMs: 5_000, maxHits: 2 });
limiter.record("conv:1");
limiter.record("conv:1");
expect(limiter.isRateLimited("conv:1")).toBe(true);
vi.advanceTimersByTime(6_000);
expect(limiter.isRateLimited("conv:1")).toBe(false);
});
it("returns false for unknown conversations", () => {
const limiter = createLoopRateLimiter();
expect(limiter.isRateLimited("unknown")).toBe(false);
});
});