feat(ai-adapters,agentic): recover Write tool args truncated by max_t…#643
Merged
GCWing merged 1 commit intoGCWing:mainfrom May 11, 2026
Merged
feat(ai-adapters,agentic): recover Write tool args truncated by max_t…#643GCWing merged 1 commit intoGCWing:mainfrom
GCWing merged 1 commit intoGCWing:mainfrom
Conversation
…okens
When the model hits its max_tokens limit while streaming a Write tool call,
the accumulated arguments end inside an unterminated string and fail JSON
parsing. Previously this surfaced as "Arguments are invalid JSON" and the
deep-research agent would retry from scratch, hitting the same cap again.
- Add repair_truncated_json in the tool-call accumulator: closes the open
string and any unclosed {/[ brackets. Refuses to fabricate values when
truncated mid-pair (trailing ',' or ':').
- Gate recovery by tool name (Write/file_write/write_notebook). Bash, Edit,
Task etc. still fail loudly — executing a partial shell command or a
partial old_string is unsafe.
- Plumb a recovered_from_truncation flag through FinalizedToolCall ->
ToolCall (#[serde(default)] for back-compat) -> tool pipeline.
- When recovery succeeds, prepend a warning to result_for_assistant
instructing the agent to continue with a follow-up call rather than
rewriting the whole file.
- When recovery isn't possible for an unsafe tool, surface a clearer error
message naming max_tokens instead of generic "invalid JSON".
- 8 new accumulator tests cover the Write recovery path, the Bash refusal
path, multibyte content, nested-bracket repair, and the refuse-mid-pair
guards. All 23 existing tests still pass.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.
…okens
When the model hits its max_tokens limit while streaming a Write tool call, the accumulated arguments end inside an unterminated string and fail JSON parsing. Previously this surfaced as "Arguments are invalid JSON" and the deep-research agent would retry from scratch, hitting the same cap again.