fix(BetaToolRunner): include current turn in compaction scope#942
Open
gn00295120 wants to merge 1 commit intoanthropics:mainfrom
Open
fix(BetaToolRunner): include current turn in compaction scope#942gn00295120 wants to merge 1 commit intoanthropics:mainfrom
gn00295120 wants to merge 1 commit intoanthropics:mainfrom
Conversation
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
There was a problem hiding this comment.
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_resultmessage is appended). - Added a unit test asserting the compaction request includes
tool_resultmessages 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #892 —
BetaToolRunnercompaction 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 tothis.#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:#checkAndCompact()if (!isCompacted)guard is removed — push and tool response always executetests/lib/tools/ToolRunner.test.ts:Before/After
Before: Compaction fires → summarizes
[user_msg]only → tool_result (90K tokens) stays in contextAfter: Push assistant + tool_result → compaction fires → summarizes
[user_msg, assistant, tool_result]→ full context reductionTest plan
max_iterationstest still works correctly (5 messages as expected)generateToolResponse()caching still works