fix: sanitize broken tool_call arguments before storing in message history - #14518
Closed
guigui0711 wants to merge 1 commit into
Closed
fix: sanitize broken tool_call arguments before storing in message history#14518guigui0711 wants to merge 1 commit into
guigui0711 wants to merge 1 commit into
Conversation
…story The invalid-JSON recovery path (after 3 retries) appends the assistant message with its broken tool_calls directly into the message history. Strict APIs like GitHub Copilot validate ALL historical tool_calls and return HTTP 400 "Invalid JSON format in tool call arguments" when they encounter these poisoned entries — especially after context compression brings them back into the API request. Reuse the existing _repair_tool_call_arguments() helper to fix malformed arguments at write time (when stored) rather than only at read time (when sent to the API), preventing the bad data from persisting in session state. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
Contributor
|
Thanks for the thorough write-up and the clean fix, @guigui0711! This exact gap was addressed by PR #15348 (merged as This is an automated hermes-sweeper review. Why this is covered:
One minor caveat (noted for future reference): the fix in main repairs broken entries on the next iteration rather than preventing them from entering |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a model generates invalid JSON in tool call arguments 3 times in a row, the recovery path (
run_agent.py:11186) appends the assistant message with its brokentool_callsdirectly into the message history:These poisoned entries then cause HTTP 400 "Invalid JSON format in tool call arguments" errors — especially after context compression brings them back into the API request. Strict APIs like GitHub Copilot (
api.githubcopilot.com) validate ALL historicaltool_callsand reject the entire request.Root Cause
The existing
_repair_tool_call_arguments()function (introduced recently) already sanitizes tool call arguments at read time (when buildingapi_messagesfor the API call). However, the recovery code path writes broken arguments into the message history before that sanitization runs.This means:
.dbfiles)_repair_tool_call_arguments()can't fix an edge case, the session is permanently poisonedFix
Reuse the existing
_repair_tool_call_arguments()helper to sanitize arguments at write time — when storing the recovery message — so broken JSON never enters the message history.Impact
run_agent.py🤖 Generated with Claude Code