fix(compact): bound truncated tool result within byte budget#39
Merged
Conversation
truncateMiddle preserved maxBytes of original content and then added the truncation marker on top, so the returned string exceeded the documented compactToolResultMaxBytes cap. Reserve the marker's footprint inside the budget so head + marker + tail is always <= maxBytes; hard-cap when the budget is too small for the marker. Head/tail preservation is kept for normal budgets and history is still never mutated. Closes #31 Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Summary
truncateMiddlepreservedmaxBytesof original content and appended the truncation marker on top, so the result exceeded the documentedcompactToolResultMaxBytescap by the marker's length.head + marker + tailis always<= maxBytes. When the budget is too small to fit a marker, the output is hard-capped tomaxBytesbytes.Test plan
go test ./internal/agent— 98 passedgo vet ./internal/agentcleanTestTruncateMiddle_NeverExceedsBudgetconfirmed to fail without the fix (maxBytes=1produced 62 bytes) and pass with itTestTruncateMiddle_PreservesHeadTailWithinBudgetlocks marker presence + head/tail preservationTestShrinkHistoryForCompact_TruncatesOversizedToolResultsto require<= maxBytes(was<= 1300for a 1000 budget)Closes #31
🤖 Generated with Claude Code