From ff954a711b9d75cd165226d040340168ec049918 Mon Sep 17 00:00:00 2001 From: MoerAI Date: Thu, 19 Mar 2026 21:24:39 +0900 Subject: [PATCH] fix(agents): handle quoted JSON stop_reason payloads in sensitive detection Extend SENSITIVE_STOP_REASON_RE to match JSON-stringified variants like '"stop_reason":"sensitive"' since describeUnknownError uses JSON.stringify for non-Error objects. --- src/agents/pi-embedded-runner/run.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/agents/pi-embedded-runner/run.ts b/src/agents/pi-embedded-runner/run.ts index cf57890acd2..0c350d72b16 100644 --- a/src/agents/pi-embedded-runner/run.ts +++ b/src/agents/pi-embedded-runner/run.ts @@ -109,8 +109,10 @@ const ANTHROPIC_MAGIC_STRING_REPLACEMENT = "ANTHROPIC MAGIC STRING TRIGGER REFUS // Detect model safety-filter rejections (stop_reason: sensitive). // This is NOT a timeout — it is a content policy block that requires a user-facing message. +// Matches both bare text ("stop_reason: sensitive") and JSON-stringified variants +// ('"stop_reason":"sensitive"') since describeUnknownError uses JSON.stringify. const SENSITIVE_STOP_REASON_RE = - /\bunhandled\s+stop[_ ]reason:\s*sensitive\b|\bstop[_ ]reason:\s*sensitive\b|\breason:\s*sensitive\b/i; + /\bunhandled\s+stop[_ ]reason:\s*"?sensitive"?\b|\bstop[_ ]reason"?:\s*"?sensitive"?\b|\breason"?:\s*"?sensitive"?\b/i; function scrubAnthropicRefusalMagic(prompt: string): string { if (!prompt.includes(ANTHROPIC_MAGIC_STRING_TRIGGER_REFUSAL)) {