fix: fix CompactionHook mis-counting context size when measuring a conversation with no assistant messages - #12594
Open
sjrl wants to merge 3 commits into
Open
fix: fix CompactionHook mis-counting context size when measuring a conversation with no assistant messages#12594sjrl wants to merge 3 commits into
CompactionHook mis-counting context size when measuring a conversation with no assistant messages#12594sjrl wants to merge 3 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
sjrl
commented
Sep 4, 2026
Comment on lines
+44
to
+48
| # messages after it still need estimating. If there is no assistant message, `context_tokens` accounts for the | ||
| # whole conversation, so nothing more needs counting. | ||
| last_assistant_index = _last_assistant_index(messages=messages) | ||
| if last_assistant_index < 0: | ||
| return context_tokens |
Contributor
Author
There was a problem hiding this comment.
this is the part of the function that was updated
sjrl
commented
Sep 4, 2026
| @@ -106,6 +113,70 @@ def _assert_every_tool_result_is_answered(messages: list[ChatMessage]) -> None: | |||
| assert result.origin.id in offered_call_ids, f"orphaned tool result: {result.origin}" | |||
|
|
|||
|
|
|||
| class TestEstimatedContextTokens: | |||
Contributor
Author
There was a problem hiding this comment.
this test class was moved from test_utils.py
sjrl
commented
Sep 4, 2026
Comment on lines
+268
to
+272
| # added the trailing tool results, a second registered hook could double count them. If there is no | ||
| # assistant message, count everything. | ||
| last_assistant_index = _last_assistant_index(messages=compacted) | ||
| accounted = compacted if last_assistant_index < 0 else compacted[: last_assistant_index + 1] | ||
| state.set("context_tokens", self.token_counter.count(messages=accounted) + estimated_overhead) |
Contributor
Author
There was a problem hiding this comment.
This is the other change that is part of the fix where the whole convo is counted when there are no assistant messages present.
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||
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.
Related Issues
Proposed Changes:
Fixed
CompactionHookmis-estimating the context size when compaction leaves a conversation with no assistant message, for example when a compactor summarizes every Agent step away. The hook now records the whole compacted conversation as accounted for, and the estimate reads that count back unchanged, so a second compaction hook running right after sees the true size instead of counting the conversation twice.How did you test it?
New tests
Notes for the reviewer
I also noticed the util function
_estimated_context_tokensis only used byCompactionHookso I moved it to be in the same file so the implementation details are closer together.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.