From 750dea9ff877470d7b52fe7d469ed2db62873580 Mon Sep 17 00:00:00 2001 From: moscowchill Date: Sat, 7 Feb 2026 10:04:22 +0100 Subject: [PATCH] Fix false positive billing error detection in agent messages detectApiError() was checking the full assistant message content including serialized tool_use JSON. When agents wrote security reports containing terms like "usage limit" or "rate limit", the billing error patterns matched on report content inside Write tool calls, killing the agent. Add extractTextOnlyContent() that filters out tool_use blocks so only the agent's actual text responses are checked for billing patterns. Co-Authored-By: Claude Opus 4.6 --- src/ai/message-handlers.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/ai/message-handlers.ts b/src/ai/message-handlers.ts index b57ffa3e..5f32612b 100644 --- a/src/ai/message-handlers.ts +++ b/src/ai/message-handlers.ts @@ -49,6 +49,20 @@ export function extractMessageContent(message: AssistantMessage): string { return String(messageContent.content); } +// Extracts only text content (no tool_use JSON) to avoid false positives in error detection +export function extractTextOnlyContent(message: AssistantMessage): string { + const messageContent = message.message; + + if (Array.isArray(messageContent.content)) { + return messageContent.content + .filter((c: ContentBlock) => c.type === 'text' || c.text) + .map((c: ContentBlock) => c.text || '') + .join('\n'); + } + + return String(messageContent.content); +} + export function detectApiError(content: string): ApiErrorDetection { if (!content || typeof content !== 'string') { return { detected: false }; @@ -106,7 +120,10 @@ export function handleAssistantMessage( ): AssistantResult { const content = extractMessageContent(message); const cleanedContent = filterJsonToolCalls(content); - const errorDetection = detectApiError(content); + // Use text-only content for error detection to avoid false positives + // from tool_use JSON (e.g. security reports containing "usage limit") + const textOnlyContent = extractTextOnlyContent(message); + const errorDetection = detectApiError(textOnlyContent); const result: AssistantResult = { content,