Skip to content

Fix: Resolve JSON malformation causing infinite loops and TypeError#1037

Open
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung:main
Open

Fix: Resolve JSON malformation causing infinite loops and TypeError#1037
gdeyoung wants to merge 1 commit intoagent0ai:mainfrom
gdeyoung:main

Conversation

@gdeyoung
Copy link

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

- Fixed rfind bug in extract_tools.py - proper nested brace tracking
- Fixed escape handling for escaped quotes in JSON strings
- Added HandledException class for graceful loop termination
- Added consecutive_misformat counter (5 attempt limit)
- Fixed tool_args TypeError - ensure dict before unpacking
@gdeyoung
Copy link
Author

Additional Fix v1.2 - HandledException Shadowing

Issue Found

A duplicate local class definition of HandledException in agent.py was shadowing the imported class from python.helpers.errors. This caused:

  • isinstance(exception, HandledException) check to fail in handle_critical_exception
  • Unhandled exceptions causing crashes instead of graceful loop termination

Root Cause

  • Line 36: from python.helpers.errors import RepairableException, HandledException ✅ (import)
  • Line 353: class HandledException(Exception): pass ❌ (duplicate local definition)

Fix Applied

Removed the duplicate class definition (lines 349-355) in agent.py. Now uses only the imported HandledException from errors.py.

Files Changed

  • agent.py: Removed duplicate HandledException class definition

Verification

  • Python syntax validated
  • Import now works correctly (same class object used throughout)
  • Exception handling now works as intended

Added: 2026-02-14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments