fix(context_compressor): keep tool-call arguments JSON valid when shrinking#11788
Closed
honghua wants to merge 1 commit into
Closed
fix(context_compressor): keep tool-call arguments JSON valid when shrinking#11788honghua wants to merge 1 commit into
honghua wants to merge 1 commit into
Conversation
…inking
Pass 3 of `_prune_old_tool_results` previously shrunk long `function.arguments`
blobs by slicing the raw JSON string at byte 200 and appending the literal
text `...[truncated]`. That routinely produced payloads like::
{"path": "/foo.md", "content": "# Long markdown
...[truncated]
— an unterminated string with no closing brace. Strict providers (observed
on MiniMax) reject this as `invalid function arguments json string` with a
non-retryable 400. Because the broken call survives in the session history,
every subsequent turn re-sends the same malformed payload and gets the same
400, locking the session into a re-send loop until the call falls out of
the window.
Fix: parse the arguments first, shrink long string leaves inside the parsed
structure, and re-serialise. Non-string values (paths, ints, booleans, lists)
pass through intact. Arguments that are not valid JSON to begin with (rare,
some backends use non-JSON tool args) are returned unchanged rather than
replaced with something neither we nor the provider can parse.
Observed in the wild: a `write_file` with ~800 chars of markdown `content`
triggered this on a real session against MiniMax-M2.7; every turn after
compression got rejected until the session was manually reset.
Tests:
- 7 direct tests of `_truncate_tool_call_args_json` covering valid-JSON
output, non-JSON pass-through, nested structures, non-string leaves,
scalar JSON, and Unicode preservation
- 1 end-to-end test through `_prune_old_tool_results` Pass 3 that
reproduces the exact failure payload shape from the incident
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6 tasks
Contributor
|
Merged via PR #12259 — your commit was cherry-picked onto current main with your authorship preserved in git log (3128d9f). Thanks for the diagnosis and fix! Chose this implementation over two competing PRs (#11617, #11821) because walking the parsed structure preserves more tool-call context after compression than a sentinel-object replacement. Both other contributors were credited in the merge PR body. |
This was referenced Apr 18, 2026
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.
Summary
_prune_old_tool_resultssliced the raw JSON offunction.argumentsmid-string and appended the literal text...[truncated], producing invalid JSONinvalid function arguments json stringBefore / after
Original (800 chars):
Old output (invalid JSON — caused the 400):
New output (valid JSON, same shrunk quality):
{"path": "~/.hermes/skills/shopping/browser-setup-notes.md", "content": "# Shopping Browser Setup Notes\n\n## Overview\n...[truncated]"}Reproducer
Exact scenario that hit this in the wild:
write_fileargumentsJSON past its opening quote and never closes it400 invalid function arguments json string, tool_call_id: call_function_l22bfzt6fe2y_1 (2013)Test plan
pytest tests/agent/test_context_compressor.py -v— 48 passed (40 existing + 8 newTestTruncateToolCallArgsJson)_prune_old_tool_resultsPass 3 and asserts the output parses as JSONensure_ascii=Falseso CJK content (which triggered the reproducer — markdown about非德满) stays intact rather than bloating with\uXXXXescapesNotes
...[truncated]markerfunction.argumentsas JSON — only strengthens the contract🤖 Generated with Claude Code