You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(compaction): splitMessagesByTokenShare can orphan tool_use from toolResult #58836
splitMessagesByTokenShare() in src/agents/compaction.ts splits message arrays purely by token count. When a split point lands between an assistant message containing a tool_use block and its corresponding toolResult message, the resulting chunks contain orphaned messages that produce invalid API request sequences.
When splitMessagesByTokenShare(messages, 2) is called with a target of ~1600 tokens per chunk, the split can land between the assistant (index 1) and toolResult (index 2), producing:
Chunk 1: [user, assistant+tool_use:tc1] — orphaned tool_use with no result
Chunk 2: [toolResult:tc1, user] — orphaned toolResult with no matching tool_use
Anthropic API rejects orphaned tool_use_id references
repairToolUseResultPairing() in pruneHistoryForContextShare already handles this for pruning, but splitMessagesByTokenShare lacks equivalent protection
Expected Behavior
Split points should respect tool call boundaries — never splitting between a tool_use and its matching toolResult. The split should snap to the nearest safe boundary (after a complete tool call/result pair).
Activity