Skip to content

fix(compact): bound truncated tool result within byte budget#39

Merged
khang859 merged 1 commit into
mainfrom
fix/compact-truncation-budget
Jun 12, 2026
Merged

fix(compact): bound truncated tool result within byte budget#39
khang859 merged 1 commit into
mainfrom
fix/compact-truncation-budget

Conversation

@khang859

Copy link
Copy Markdown
Owner

Summary

  • truncateMiddle preserved maxBytes of original content and appended the truncation marker on top, so the result exceeded the documented compactToolResultMaxBytes cap by the marker's length.
  • Now reserves the marker's footprint inside the budget — head + marker + tail is always <= maxBytes. When the budget is too small to fit a marker, the output is hard-capped to maxBytes bytes.
  • Head/tail preservation is kept for normal budgets; the original session/history is still never mutated.

Test plan

  • go test ./internal/agent — 98 passed
  • go vet ./internal/agent clean
  • New TestTruncateMiddle_NeverExceedsBudget confirmed to fail without the fix (maxBytes=1 produced 62 bytes) and pass with it
  • New TestTruncateMiddle_PreservesHeadTailWithinBudget locks marker presence + head/tail preservation
  • Tightened TestShrinkHistoryForCompact_TruncatesOversizedToolResults to require <= maxBytes (was <= 1300 for a 1000 budget)

Closes #31

🤖 Generated with Claude Code

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>
@khang859 khang859 merged commit 1f1f466 into main Jun 12, 2026
1 check passed
@khang859 khang859 deleted the fix/compact-truncation-budget branch June 12, 2026 22:17
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.

Enforce compact tool-result truncation byte budget

1 participant