Fix: Resolve JSON malformation causing infinite loops and TypeError#1037
Fix: Resolve JSON malformation causing infinite loops and TypeError#1037gdeyoung wants to merge 0 commit intoagent0ai:mainfrom
Conversation
Additional Fix v1.2 - HandledException ShadowingIssue FoundA duplicate local class definition of
Root Cause
Fix AppliedRemoved the duplicate class definition (lines 349-355) in Files Changed
Verification
Added: 2026-02-14 |
|
I can confirm this is a critical bug. I traced tool call failures across multiple chat sessions on my instance (v0.9.8.1, Claude Opus 4.6 via GitHub Copilot, 128k context) and found 105 empty tool_name failures in a single log file — all caused by the rfind bug in extract_json_object_string(). Evidence from my logs:
Additional issue not covered by this PR: The LLM frequently hallucinates tool names from training data instead of using the actual tool names in the system prompt:
A simple alias mapping in get_tool() would catch these. Happy to submit a PR for that. The consecutive misformat counter in this PR would have prevented the 38-message infinite loop. Please consider merging this — it's a significant stability improvement. |
Fix agent0ai#3 - Empty tool_name validation: - When DirtyJson parses valid JSON but the object has no tool_name field, the agent previously dispatched with an empty string, triggering 'Tool not found' errors. Now treats this as a misformat and increments the consecutive_misformat counter (integrates with PR agent0ai#1037's circuit breaker). - Evidence: 105 empty tool_name failures found in a single log session. Fix agent0ai#4 - Tool name alias mapping: - LLMs frequently hallucinate tool names from training data instead of using the actual names in the system prompt. Added TOOL_ALIASES dict that maps common hallucinated names to actual Agent Zero tool names: - code_execution/terminal/shell -> code_execution_tool - web_search/search -> search_engine - browser_tool/browser -> browser_agent - response_tool/message_tool/message/reply -> response - knowledge_tool/memory_tool -> memory - task_manager -> scheduler - Evidence: 20+ hallucinated tool name failures across multiple chat logs. Related: agent0ai#1031, agent0ai#805
…ng resistance Claude subordinates interpret Agent Zero system prompt as "prompt injection" and refuse to output JSON, causing infinite misformat loops (even with circuit breaker from PR agent0ai#1037). GLM-5 reliably follows JSON formatting instructions and is capable enough for agentic subordinate work. Uses existing initialize_agent(override_settings=) mechanism.
|
Hi @gdeyoung @frdel — I can independently confirm all three bugs here. I've been hitting the misformat infinite loop repeatedly with MiniMax M2.5 and GLM-5 on OpenRouter, and traced the root cause through the same path:
I've deployed all three fixes (rfind, circuit breaker, RepairableException) on my production instance and they work together correctly:
Note: @Krashnicov's review about the escape handling bug in this PR is correct — escaped quotes should NOT toggle @frdel this is probably the highest-impact stability fix pending for the project — multiple users are hitting this (#624, #841, #1031, #1234, #1241). Would love to see this reviewed and merged. |
Summary
Fixed multiple critical bugs causing JSON malformation, infinite loops, and TypeError crashes in Agent Zero.
Issues Fixed
1. JSON Object Extraction Bug (rfind)
File: python/helpers/extract_tools.py - Used rfind to find LAST closing brace instead of matching one
Fix: Proper nested brace tracking using depth counter
2. Escape Handling Logic Error
File: python/helpers/extract_tools.py - Escaped quotes not toggling in_string flag
Fix: Proper check for escaped quotes
3. No Loop Protection
File: agent.py - No protection against consecutive misformat errors
Fix: Added consecutive_misformat counter with 5-attempt limit + HandledException
4. TypeError: tool_args must be a mapping
File: agent.py - .get() returns string if key exists with string value
Fix: isinstance(tool_args, dict) validation