Skip to content

feat(ai-adapters,agentic): recover Write tool args truncated by max_t…#643

Merged
GCWing merged 1 commit intoGCWing:mainfrom
nonoqing:yuyiqing/dev
May 11, 2026
Merged

feat(ai-adapters,agentic): recover Write tool args truncated by max_t…#643
GCWing merged 1 commit intoGCWing:mainfrom
nonoqing:yuyiqing/dev

Conversation

@nonoqing
Copy link
Copy Markdown
Collaborator

…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.

…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>
@GCWing GCWing merged commit f0bb3bf into GCWing:main May 11, 2026
4 checks passed
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.

2 participants