Skip to content

fix(agent-loop): recover malformed tool arguments - #746

Open
imMamdouhaboammar wants to merge 1 commit into
lsdefine:mainfrom
imMamdouhaboammar:fix/agent-loop-invalid-tool-json-upstream
Open

fix(agent-loop): recover malformed tool arguments#746
imMamdouhaboammar wants to merge 1 commit into
lsdefine:mainfrom
imMamdouhaboammar:fix/agent-loop-invalid-tool-json-upstream

Conversation

@imMamdouhaboammar

Copy link
Copy Markdown

Problem

agent_runner_loop() parses model tool-call arguments before dispatch. Malformed JSON raised JSONDecodeError and terminated the task before GenericAgent could ask the model to correct the call.

A second edge case exists even when the JSON syntax is valid: null, arrays, strings, numbers, and booleans decode successfully but violate GenericAgent's tool contract, which expects an argument object. Those values later crash _compact_tool_args() or BaseHandler.dispatch() instead of entering the retry flow.

Review also identified a reliability edge: echoing the entire malformed argument payload into the retry prompt lets a very large invalid tool call inflate the next model request.

Fix

  • parse each tool call individually
  • require the decoded value to be a JSON object (dict)
  • route malformed JSON and valid non-object JSON through the existing bad_json recovery path
  • include the tool name plus a bounded 800-character preview of the raw arguments
  • when truncation occurs, include the original total character count
  • keep valid object tool calls unchanged

Verification

TDD and review-driven regression work was performed before rebuilding this clean branch:

  1. Regression-only run 31165097818 failed with the exact JSONDecodeError at agent_loop.py:70 before dispatch
  2. The initial malformed-JSON recovery fix passed run 31165170657
  3. CodeRabbit identified the valid-but-non-object JSON case. Regression-only run 31165565545 failed for all five targeted values: null, [], string, number, and boolean, while malformed-JSON recovery still passed
  4. The object-only validation passed run 31165676777: compile, malformed JSON coverage, all five non-object subcases, and git diff --check
  5. Qodo identified the unbounded raw-arguments echo. RED run 31167984365 kept the existing recovery tests green and failed only because a ~20k malformed payload produced a 20,112-character retry prompt
  6. GREEN run 31168074147 passed compile, all three regression methods, and git diff --check with a bounded preview that retains total length metadata
  7. This contribution branch was rebuilt directly from current main as one clean commit using the exact production/test blobs from the last GREEN validation

Scope

  • 1 core production file modified
  • 1 focused regression-test file added
  • 1 clean commit on top of current main
  • no dependency or configuration changes

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