iMessage Reflection Guard (monitor/reflection-guard.ts)

ScienceClaw · src/imessage/monitor/reflection-guard.ts · typescript · MIT
voice, chat, infra, tag:reflection

Detects reflected content in iMessages to prevent echo loops.

// Source: https://github.com/beita6969/ScienceClaw | License: MIT | Copyright 2025 Peter Steinberger
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Detects inbound messages that are reflections of assistant-originated content.
 * These patterns indicate internal metadata leaked into a channel and then
 * bounced back as a new inbound message — creating an echo loop.
 */

import { findCodeRegions, isInsideCode } from "../../shared/text/code-regions.js";

const INTERNAL_SEPARATOR_RE = /(?:#\+){2,}#?/;
const ASSISTANT_ROLE_MARKER_RE = /\bassistant\s+to\s*=\s*\w+/i;
// Require closing `>` to avoid false-positives on phrases like "<thought experiment>".
const THINKING_TAG_RE = /<\s*\/?\s*(?:think(?:ing)?|thought|antthinking)\b[^<>]*>/i;
const RELEVANT_MEMORIES_TAG_RE =

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key