Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(write-gate): detector recall for passive routing rules, long agent-addressed rules, concealment and approval-bypass clauses (#5575 Cat 37 37-3, 37-4)
garrytan0cd343d00471
diff --git a/src/core/write-gate-patterns.ts b/src/core/write-gate-patterns.tsindex 0411081a7d..7dee9e1a67 100644--- a/src/core/write-gate-patterns.ts+++ b/src/core/write-gate-patterns.ts@@ -50,7 +50,7 @@ export interface WriteGatePattern {}/** Longest text a single pattern can match; windows overlap by this much (plus the preceding context). */-export const MAX_MATCH_CHARS = 400;+export const MAX_MATCH_CHARS = 480;/** Longest context a `preceded` regex may need before a match. */export const MAX_PRECEDING_CHARS = 260;/** Bound substituted for `+` and `*` in the cloned rewrite patterns. */@@ -93,15 +93,32 @@ const IMPERATIVE_META: ReadonlyArray<{ name: string; anchors: readonly string[];{ name: 'imperative-new-system-prompt', anchors: ['system'] },];+/** One character inside a sentence (a fast class for the scanning regexes). */const SENT = '[^.!?]';+/** One character inside a clause: also stops at a table cell boundary. */+const CLAUSE = '[^.!?|]';+/**+ * One character inside a sentence for `preceded` contexts, which run only on the text before a rare-token+ * match: a `.`, `!` or `?` ends the sentence only when followed by whitespace, so an address, a URL or "3.5"+ * earlier in the sentence does not cut the context short.+ */+const CTX = '(?:[^.!?]|[.!?](?=\\S))';+/** Past participles of the send-it-there verbs, for passive rules ("must be emailed to ..."). */+const PASSIVE_EXFIL = "(?:forwarded|sent|emailed|e-mailed|mailed|cc[’']?d|cced|bcc[’']?d|copied|posted|uploaded|transferred|wired|shared|leaked|synced|mirrored|reported|submitted|routed|rerouted|redirected|exported|remitted|delivered)";+/** "to <address>", allowing a few words before it ("to the portal at https://..."), or "shared with <address>". */+const TO_ADDRESS_HEAD = `\\b(?:to|with) ${'(?:[a-z-]{1,20} )?'.repeat(4)}`;const FUTURE = '(?:from now on|going forward|henceforth|from this point (?:on|forward)|in (?:all )?future (?:answers|responses|replies|conversations|sessions|chats))';const AGENT = '(?:ai assistant|assistant|chatbot|ai agent|agent|llm|claude|chatgpt|gpt|gemini|copilot|grok|bot|ai)';+/** In a vocative ("instructions for the model:") "model" addresses the agent too. */+const AGENT_V = '(?:ai assistant|assistant|chatbot|ai agent|agent|llm|claude|chatgpt|gpt|gemini|copilot|grok|bot|ai|(?:language )?model)';const SPEECH = '(?:say|tell|respond|reply|answer|recommend|mention|claim|state|describe|refer|suggest|output|report|deny|confirm|praise|promote|call|treat|use|ignore|include)';const ACTION = '(?:forward|send|email|e-mail|cc|bcc|share|reveal|upload|post|transfer|wire|pay|delete|remove|run|execute|invoke|book|buy|approve|grant|disclose)';const STRICT_SPEECH = '(?:say|tell|respond|reply|answer|recommend|mention|claim|state|deny|reveal|disclose|describe|suggest|output)';-const EXFIL_VERB = '(?:forward|send|email|e-mail|cc|bcc|copy|post|upload|transfer|wire|share|leak|exfiltrate|sync|mirror|report|submit)';-const STANDING = '(?:always|from now on|going forward|henceforth|whenever|every time|automatically|silently|secretly|quietly|without (?:telling|notifying|asking)|all future|any future|every future|in (?:the )?future)';-const ADDRESS = '(?:[a-z0-9._%+-]{1,64}@[a-z0-9-]{1,63}\\.[a-z]{2,24}|https?:\\/\\/|www\\.)';+const EXFIL_VERB = '(?:forward|send|email|e-mail|cc|bcc|copy|post|upload|transfer|wire|share|leak|exfiltrate|sync|mirror|report|submit|route|reroute|redirect|export|remit|deliver|'+ + PASSIVE_EXFIL + ')';+const STANDING = '(?:always|from now on|going forward|moving forward|henceforth|whenever|every time|automatically|silently|secretly|quietly|without (?:telling|notifying|asking)|all future|any future|every future|in (?:the )?future|effective immediately|until further notice|from this point(?: on| forward)?|new (?:policy|rule|procedure|process))';+/** An email address (whole host), or a URL / www host, so a rule after it ("... to [email protected] from now on") still reads on. */+const ADDRESS = `(?:[a-z0-9._%+-]{1,64}@[a-z0-9-]{1,30}${'(?:\\.[a-z0-9-]{1,30})?'.repeat(3)}\\.[a-z]{2,24}|(?:https?:\\/\\/|www\\.)[^\\s<>"']{1,120})`;const CREDENTIAL = '(?:api[ _-]?keys?|passwords?|passphrases?|credentials?|secret keys?|access tokens?|auth(?:entication)? tokens?|bearer tokens?|refresh tokens?|private keys?|ssh keys?|seed phrases?|recovery (?:codes?|phrases?)|2fa codes?|one-time (?:codes?|passwords?)|session (?:cookies?|tokens?)|\\.env files?)';/**@@ -134,7 +151,8 @@ function own(name: string, family: WriteGateReasonFamily, source: string, anchor}const AGENT_ANCHORS = ['assistant', 'chatbot', 'agent', 'llm', 'claude', 'chatgpt', 'gpt', 'gemini', 'copilot', 'grok', 'bot', 'ai'];-const STANDING_ANCHORS = ['always', 'from now on', 'going forward', 'henceforth', 'whenever', 'every time', 'automatically', 'silently', 'secretly', 'quietly', 'without', 'future'];+const STANDING_ANCHORS = ['always', 'from now on', 'going forward', 'moving forward', 'henceforth', 'whenever', 'every time', 'automatically', 'silently', 'secretly', 'quietly', 'without', 'future',+ 'effective immediately', 'until further notice', 'from this point', 'new policy', 'new rule', 'new procedure', 'new process'];const ADDRESS_ANCHORS = ['@', 'http', 'https', 'www'];const FUTURE_ANCHORS = ['from now on', 'going forward', 'henceforth', 'from this point', 'future answers', 'future responses', 'future replies', 'future conversations', 'future sessions', 'future chats'];const CREDENTIAL_ANCHORS = ['key', 'keys', 'api', 'apikey', 'password', 'passwords', 'passphrase', 'passphrases', 'credential', 'credentials', 'token', 'tokens', 'seed', 'recovery', '2fa', 'one-time', 'cookie', 'cookies', '.env'];@@ -151,19 +169,28 @@ const OWN_PATTERNS: readonly WriteGatePattern[] = [own('conceal-from-user', 'override',"\\b(?:do not|don't|never)\\b (?:tell|inform|notify|alert|warn|mention (?:this |it )?to|reveal (?:this |it )?to) (?:the )?(?:user|owner|human|operator)\\b",['user', 'owner', 'human', 'operator']),+ own('conceal-standing-action', 'override',+ "\\b(?:never|do not|don't) (?:mention|tell|reveal|disclose|inform|notify|alert|flag) (?:(?:it|this|that|them) (?:to )?)?(?:the user|the owner|the human|me|it|this|that|them)\\b",+ ['never', "don't", 'not'], { requires: STANDING_ANCHORS, preceded: `\\b${STANDING}\\b${CTX}{0,160}` }),+ own('act-without-asking', 'standing_instruction',+ '\\bwithout (?:asking|checking(?: with)?|confirming(?: with)?|consulting|telling|notifying|alerting|informing) (?:the user|the owner|the human|me|anyone|anybody|first)\\b',+ ['without'], { requires: STANDING_ANCHORS, preceded: `\\b${STANDING}\\b${CTX}{0,160}` }),own('redacted-marker', 'override', '\\[redacted(?:-attr)?\\]', ['redacted']),own('from-now-on-lead', 'standing_instruction',- `\\b${FUTURE}\\b${SENT}{0,100}\\b(?:${SPEECH}|${ACTION})\\b`,+ `\\b${FUTURE}\\b${SENT}{0,100}\\b(?:${SPEECH}|${ACTION}|(?:is|are|must|should|shall|will) (?:to )?be (?:called|referred to|described|treated|considered|known|listed|presented|named|introduced))\\b`,FUTURE_ANCHORS),own('from-now-on-trail', 'standing_instruction',`\\b${FUTURE}\\b`,- FUTURE_ANCHORS, { preceded: `\\b(?:${SPEECH}|${ACTION})\\b${SENT}{0,100}` }),+ FUTURE_ANCHORS, { preceded: `\\b(?:${SPEECH}|${ACTION})\\b${CTX}{0,100}` }),own('agent-vocative-note', 'standing_instruction',- `\\b(?:(?:note|message|reminder|instructions?|attention|attn|memo) (?:to|for) (?:the |any |all |my |our |your )?(?:ai |llm )?|(?:dear|hey|hello|attention|attn) (?:the )?)${AGENT}\\b${SENT}{0,80}\\b(?:always|never|must|should|do not|don't|from now on|going forward)\\b${SENT}{0,40}\\b(?:${SPEECH}|${ACTION})\\b`,- ['note', 'message', 'reminder', 'instruction', 'instructions', 'attention', 'attn', 'memo', 'dear', 'hey', 'hello'], { requires: AGENT_ANCHORS }),+ `\\b(?:(?:note|message|reminder|instructions?|attention|attn|memo) (?:to|for) (?:the |any |all |my |our |your )?(?:ai |llm )?|(?:dear|hey|hello|attention|attn) (?:the )?)${AGENT_V}\\b(?:${CLAUSE}{0,200}\\b(?:always|never|must|should|do not|don't|from now on|going forward)\\b${SENT}{0,40}\\b(?:${SPEECH}|${ACTION})| ?[:,] ?(?:please )?(?:always |never |only )?(?:${SPEECH}|${ACTION}))\\b`,+ ['note', 'message', 'reminder', 'instruction', 'instructions', 'attention', 'attn', 'memo', 'dear', 'hey', 'hello'], { requires: [...AGENT_ANCHORS, 'model'] }),+ own('agent-vocative-any', 'standing_instruction',+ `\\b(?:to|for) (?:any|all|every) (?:ai |llm )?${AGENT_V}s? (?:reading|seeing|processing|handling|that reads|who reads)\\b${CLAUSE}{0,200}\\b(?:always|never|must|should|do not|don't|from now on|going forward)\\b${SENT}{0,40}\\b(?:${SPEECH}|${ACTION})\\b`,+ ['reading', 'seeing', 'processing', 'handling', 'reads'], { requires: ['any', 'all', 'every'] }),own('agent-standing-rule', 'standing_instruction',- `\\b${AGENT}\\b(?: ?[,:]${SENT}{0,80}\\b(?:always|never|must|should|do not|don't|from now on|going forward)\\b${SENT}{0,40}\\b(?:${SPEECH}|${ACTION})|${SENT}{0,40}\\b(?:always|never|from now on|going forward|henceforth)\\b${SENT}{0,40}\\b(?:${STRICT_SPEECH}|${ACTION}))\\b`,- AGENT_ANCHORS, { requires: MODAL_ANCHORS }),+ `\\b${AGENT}\\b(?: ?[,:]${CLAUSE}{0,200}\\b(?:always|never|must|should|do not|don't|from now on|going forward)\\b${SENT}{0,40}\\b(?:${SPEECH}|${ACTION})|${SENT}{0,40}\\b(?:always|never|from now on|going forward|henceforth)\\b${SENT}{0,40}\\b(?:${STRICT_SPEECH}|${ACTION})| (?:is|are|will be|shall be) (?:instructed |required |expected |supposed )?to (?:always |never |only )?(?:${SPEECH}|${ACTION}))\\b`,+ AGENT_ANCHORS, { requires: [...MODAL_ANCHORS, 'is', 'are', 'will', 'shall'] }),own('you-always-say', 'standing_instruction',`\\byou\\b${SENT}{0,30}\\b(?:always|never)\\b${SENT}{0,40}\\b${STRICT_SPEECH}\\b`,['you'], { requires: ['always', 'never'] }),@@ -171,17 +198,25 @@ const OWN_PATTERNS: readonly WriteGatePattern[] = [`\\b(?:always|never) ${STRICT_SPEECH}\\b`,['always', 'never'], { preceded: '(?:[.!?:;\\n] ?|- )(?:please )?' }),own('when-asked-say', 'standing_instruction',- `\\b(?:asks?|asked|asking|inquires?|wants to know)\\b${SENT}{0,80}\\b(?:say|tell|respond|reply|answer|claim|state|insist|deny|recommend)\\b`,- ['ask', 'asks', 'asked', 'asking', 'inquire', 'inquires', 'wants to know'], { requires: ['say', 'tell', 'respond', 'reply', 'answer', 'claim', 'state', 'insist', 'deny', 'recommend'], preceded: `\\b(?:when|whenever|if)\\b${SENT}{0,60}` }),- own('exfil-standing-lead', 'exfiltration', `\\bto ?${ADDRESS}`, STANDING_ANCHORS,- { requires: ADDRESS_ANCHORS, preceded: `\\b${STANDING}\\b${SENT}{0,80}\\b${EXFIL_VERB}\\b${SENT}{0,80}` }),- own('exfil-standing-trail', 'exfiltration', `\\bto ?${ADDRESS}${SENT}{0,80}\\b${STANDING}\\b`, STANDING_ANCHORS,- { requires: ADDRESS_ANCHORS, preceded: `\\b${EXFIL_VERB}\\b${SENT}{0,80}` }),- own('exfil-agent-addressed', 'exfiltration', `\\bto ?${ADDRESS}`, AGENT_ANCHORS,- { requires: ADDRESS_ANCHORS, preceded: `\\b${AGENT}\\b${SENT}{0,60}\\b${EXFIL_VERB}\\b${SENT}{0,80}` }),- own('exfil-private-data', 'exfiltration', `\\bto ?${ADDRESS}`,+ `\\b(?:asks?|asked|asking|inquires?|wants to know|comes? up|is brought up)\\b${SENT}{0,80}\\b(?:say|tell|respond|reply|answer|response|claim|state|insist|deny|recommend)\\b`,+ ['ask', 'asks', 'asked', 'asking', 'inquire', 'inquires', 'wants to know', 'come up', 'comes up', 'brought up'],+ { requires: ['say', 'tell', 'respond', 'reply', 'answer', 'response', 'claim', 'state', 'insist', 'deny', 'recommend'], preceded: `\\b(?:when|whenever|if)\\b${CTX}{0,60}` }),+ own('exfil-standing-lead', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}`, STANDING_ANCHORS,+ { requires: ADDRESS_ANCHORS, preceded: `\\b${STANDING}\\b${CTX}{0,80}\\b${EXFIL_VERB}\\b${CTX}{0,80}` }),+ own('exfil-standing-trail', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}${SENT}{0,80}\\b${STANDING}\\b`, STANDING_ANCHORS,+ { requires: ADDRESS_ANCHORS, preceded: `\\b${EXFIL_VERB}\\b${CTX}{0,80}` }),+ own('exfil-agent-addressed', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}`, AGENT_ANCHORS,+ { requires: ADDRESS_ANCHORS, preceded: `\\b${AGENT}\\b${CTX}{0,60}\\b${EXFIL_VERB}\\b${CTX}{0,80}` }),+ own('exfil-private-data', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}`,+ ['memory', 'memories', 'history', 'system prompt', 'private notes', 'user data', ...CREDENTIAL_ANCHORS],+ { requires: ADDRESS_ANCHORS, preceded: `\\b${EXFIL_VERB}\\b${CTX}{0,40}\\b(?:memor(?:y|ies)|conversation history|chat history|system prompt|private notes|user data|${CREDENTIAL})\\b${CTX}{0,60}` }),+ own('exfil-standing-direct', 'exfiltration', `\\b(?:cc|bcc|copy|loop in|e-?mail) (?:in )?${ADDRESS}`, ['cc', 'bcc', 'copy', 'loop', 'email', 'e-mail'],+ { requires: ADDRESS_ANCHORS, preceded: `\\b${STANDING}\\b${CTX}{0,80}` }),+ own('exfil-passive-now', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}`, ['now'],+ { requires: ADDRESS_ANCHORS, preceded: `\\bnow (?:be|get|gets) (?:[a-z]{1,15} )?${PASSIVE_EXFIL}\\b${'(?: [a-z-]{1,15})?'.repeat(2)} ?` }),+ own('exfil-private-data-passive', 'exfiltration', `${TO_ADDRESS_HEAD}(?:at |on |via )?${ADDRESS}`,['memory', 'memories', 'history', 'system prompt', 'private notes', 'user data', ...CREDENTIAL_ANCHORS],- { requires: ADDRESS_ANCHORS, preceded: `\\b${EXFIL_VERB}\\b${SENT}{0,40}\\b(?:memor(?:y|ies)|conversation history|chat history|system prompt|private notes|user data|${CREDENTIAL})\\b${SENT}{0,60}` }),+ { requires: ADDRESS_ANCHORS, preceded: `\\b(?:memor(?:y|ies)|conversation history|chat history|system prompt|private notes|user data|${CREDENTIAL})\\b${CTX}{0,60}\\b(?:be|get|gets|got) (?:[a-z]{1,15} )?${PASSIVE_EXFIL}\\b${CTX}{0,20}` }),own('exfil-templated-url', 'exfiltration','https?:\\/\\/[^\\s)"\'<>]{1,200}[?&][a-z0-9_]{1,30}= ?(?:\\{|<|\\[|%7b|\\$\\{)',['http', 'https']),diff --git a/src/core/write-gate.ts b/src/core/write-gate.tsindex 0b5478ea38..7adf4bf0e1 100644--- a/src/core/write-gate.ts+++ b/src/core/write-gate.ts@@ -204,7 +204,8 @@ function matchesInContext(p: WriteGatePattern, window: string, atTextStart: boolrx.lastIndex = 0;for (let m = rx.exec(window); m; m = rx.exec(window)) {const at = m.index;- if (m[0].length === 0) rx.lastIndex++;+ // Resume one character later, not at the match end: a rejected match can overlap the one that holds.+ rx.lastIndex = at + 1;if (p.negatable && NEGATED_BEFORE_RE.test(window.slice(Math.max(0, at - 10), at))) continue;if (!p.preceded) return true;// The text start counts as a line start for `preceded` (a pattern may require one).@@ -276,8 +277,14 @@ function anchorOccurrences(text: string): { at: number[]; key: number[]; needsNoreturn { at, key, needsNormalizing: normalize };}-function hasAnchor(anchors: readonly string[], present: ReadonlySet<number>): boolean {- return anchors.some(a => { const k = ANCHOR_KEYS.get(a)!; return present.has(k === '@' ? AT_HASH : k); });+const keysOf = (anchors: readonly string[]) => [...new Set(anchors.map(a => { const k = ANCHOR_KEYS.get(a)!; return k === '@' ? AT_HASH : k; }))];+/** Per pattern (same order as WRITE_GATE_PATTERNS): the anchor keys, and the `requires` keys or null. */+const PATTERN_KEYS: ReadonlyArray<{ anchors: number[]; requires: number[] | null }> =+ WRITE_GATE_PATTERNS.map(p => ({ anchors: keysOf(p.anchors), requires: p.requires ? keysOf(p.requires) : null }));++function hasAny(keys: readonly number[], present: ReadonlySet<number>): boolean {+ for (const k of keys) if (present.has(k)) return true;+ return false;}/** Pure detector: every (family, pattern, field) the pattern table finds in the fields. */@@ -297,8 +304,10 @@ export function detectInstructionLike(fields: ReadonlyArray<readonly [WriteGateFwhile (first < occ.at.length && occ.at[first]! <= start) first++;const present = new Set<number>();for (let k = first; k < occ.at.length && occ.at[k]! <= end; k++) present.add(occ.key[k]!);- for (const p of WRITE_GATE_PATTERNS) {- if (found.has(p.name) || !hasAnchor(p.anchors, present) || (p.requires && !hasAnchor(p.requires, present))) continue;+ for (let i = 0; i < WRITE_GATE_PATTERNS.length; i++) {+ const p = WRITE_GATE_PATTERNS[i]!;+ const keys = PATTERN_KEYS[i]!;+ if (found.has(p.name) || !hasAny(keys.anchors, present) || (keys.requires && !hasAny(keys.requires, present))) continue;if (!(p.negatable || p.preceded ? matchesInContext(p, window, start === 0) : p.rx.test(window))) continue;found.add(p.name);hits.push({ family: p.family, pattern: p.name, field });