Skip to content

fix(tools): use filtered messages list in async compaction#1124

Open
fede-kamel wants to merge 2 commits intoanthropics:mainfrom
fede-kamel:fix/async-compaction-bug-v2
Open

fix(tools): use filtered messages list in async compaction#1124
fede-kamel wants to merge 2 commits intoanthropics:mainfrom
fede-kamel:fix/async-compaction-bug-v2

Conversation

@fede-kamel
Copy link

Summary

Fixes a bug where async _check_and_compact() ignores the tool_use filtering logic.

The Bug

When compaction runs and the last message is an assistant with only tool_use blocks, those blocks should be filtered out before sending the summarization request. The sync version does this correctly, but the async version was using self._params["messages"] instead of the filtered messages variable.

Without fix:

BadRequestError: messages.2: `tool_use` ids were found without 
`tool_result` blocks immediately after: toolu_test123

With fix: Compaction succeeds - tool_use is filtered out.

Code Change

# Sync (line 202) - correct
messages = [*messages, ...]

# Async (line 453) - was buggy  
messages = [*self._params["messages"], ...]  # ignored filtering!

# Async (line 453) - fixed
messages = [*messages, ...]

Test Plan

  • Added regression test test_async_compaction_filters_tool_use
  • Test fails without fix, passes with fix
  • All existing tests pass
  • Verified with real API call

The async _check_and_compact() method was using self._params["messages"]
instead of the local `messages` variable when building the compaction
request. This caused the filtering logic (which removes tool_use blocks
from the last assistant message) to be ignored.

When compaction runs and the last message is an assistant with only
tool_use blocks, those blocks should be filtered out before sending
the summarization request. Without this fix, the API rejects with:

  "tool_use ids were found without tool_result blocks"

The sync version correctly uses `*messages`, the async version was
incorrectly using `*self._params["messages"]`.

Added regression test that verifies tool_use filtering works correctly.
@fede-kamel fede-kamel requested a review from a team as a code owner January 20, 2026 17:56
- Add cast() to fix pyright type error where lambda returns dict instead of ParseMessageCreateParamsBase
- Fix bug: use params["messages"] (existing conversation) instead of messages (input variable)
@fede-kamel fede-kamel force-pushed the fix/async-compaction-bug-v2 branch from 412ceba to 8684857 Compare January 21, 2026 20:32
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.

1 participant