Skip to content

fix(BetaToolRunner): include current turn in compaction scope#942

Open
gn00295120 wants to merge 1 commit intoanthropics:mainfrom
gn00295120:fix/compaction-message-scope
Open

fix(BetaToolRunner): include current turn in compaction scope#942
gn00295120 wants to merge 1 commit intoanthropics:mainfrom
gn00295120:fix/compaction-message-scope

Conversation

@gn00295120
Copy link

Summary

Fixes #892BetaToolRunner compaction only summarizes older messages, missing the token-heavy current turn.

When auto-compaction fires, #checkAndCompact() was called BEFORE the current assistant message and tool results were pushed to this.#state.params.messages. This meant the compaction summary only covered older history, leaving the most token-heavy content (tool results from the current turn) out of the summary — defeating the purpose of compaction.

Also fixes #886 as a side effect — the push/break logic is no longer gated on the compaction result, preventing the infinite loop and message loss issues.

Changes

src/lib/tools/BetaToolRunner.ts:

  • Moved assistant message push and tool response generation BEFORE #checkAndCompact()
  • Compaction now only runs when tool results exist (no need to compact on the final text response)
  • The if (!isCompacted) guard is removed — push and tool response always execute

tests/lib/tools/ToolRunner.test.ts:

  • Added test verifying compaction receives tool_result messages in its scope

Before/After

Before: Compaction fires → summarizes [user_msg] only → tool_result (90K tokens) stays in context
After: Push assistant + tool_result → compaction fires → summarizes [user_msg, assistant, tool_result] → full context reduction

Test plan

  • All 23 existing tests pass unchanged
  • New compaction scope test verifies tool_result is present in compaction messages
  • max_iterations test still works correctly (5 messages as expected)
  • generateToolResponse() caching still works
  • Streaming and non-streaming modes both work

When compaction fires, it should summarize ALL messages including the
current assistant response and tool results. Previously, compaction
ran before these were pushed to the messages array, so it only saw
older conversation history — leaving the token-heavy tool results
out of the summary.

Move the message push and tool response generation before the
compaction check so the summarization API call sees the full context.

This also fixes the infinite loop issue (anthropics#886) as a side effect since
the push/break logic is no longer gated on the compaction result.

Fixes anthropics#892
@gn00295120 gn00295120 requested a review from a team as a code owner March 12, 2026 05:20
Copilot AI review requested due to automatic review settings March 12, 2026 05:20
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes BetaToolRunner auto-compaction so that summarization includes the current turn’s assistant tool_use and subsequent tool_result messages, preventing token-heavy tool output from being left un-compacted. It also removes the prior compaction-gated push/break behavior that could cause looping and message loss.

Changes:

  • Reordered the tool loop to push the current assistant message and tool results before invoking #checkAndCompact().
  • Runs compaction only after tool results are generated (i.e., when a tool_result message is appended).
  • Added a unit test asserting the compaction request includes tool_result messages from the current turn.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/lib/tools/BetaToolRunner.ts Ensures compaction scope includes the current assistant + tool result messages by moving history updates ahead of compaction.
tests/lib/tools/ToolRunner.test.ts Adds coverage to verify compaction receives current-turn tool_result messages (regression test for #892).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

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.

BetaToolRunner: Incorrect compaction target scope involves tool execution context

2 participants