Skip to content

v1.4.5

Choose a tag to compare

@dalehurley dalehurley released this 06 Feb 01:57
· 5 commits to master since this release

Bug Fix

fix: prevent context compaction from corrupting tool_use/tool_result pairing

Fixed a critical bug where long-running agent workflows (15+ iterations) would fail with tool_use ids were found without tool_result blocks immediately after API errors.

Root Cause

Three interacting issues in the context compaction system:

  1. Compaction fired between tool_use and tool_result additionAgentContext::addMessage() triggered auto-compaction after adding the assistant's tool_use response but before the tool_result was added, orphaning the tool_use block.

  2. Initial user message dropped during compaction — The compaction algorithm preserved system messages but not the initial user task message, causing compacted messages to start with an assistant role and violating API requirements.

  3. Compaction algorithm misordered preserved messages — The array_unshift approach inserted recent messages before preserved messages, breaking message ordering.

Changes

  • AgentContext::addMessage() now defers compaction when the last message is a dangling tool_use (no matching tool_result yet)
  • ContextManager::compactMessages() preserves both the system message and initial user task message
  • Rewrote compaction to correctly build preserved + recent message sets, keeping the maximum number of recent message units that fit
  • Added early return after clearToolResults if that alone brings context within limits
  • Added 7 regression tests covering multi-iteration compaction, tool_use pairing integrity, and message ordering

Impact

Fixes production failures for:

  • Document analysis (PDFs, large files)
  • Complex multi-step automation workflows
  • Data extraction requiring 15+ agent iterations
  • Any workflow using context management with auto_compact enabled