Skip to content

fix: fix CompactionHook mis-counting context size when measuring a conversation with no assistant messages - #12594

Open
sjrl wants to merge 3 commits into
mainfrom
fix/compaction-context-tokens-no-assistant
Open

fix: fix CompactionHook mis-counting context size when measuring a conversation with no assistant messages#12594
sjrl wants to merge 3 commits into
mainfrom
fix/compaction-context-tokens-no-assistant

Conversation

@sjrl

@sjrl sjrl commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Fixed CompactionHook mis-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_tokens is only used by CompactionHook so I moved it to be in the same file so the implementation details are closer together.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
haystack-docs Ready Ready Preview Sep 4, 2026 7:34am UTC

Request Review

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels 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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is the part of the function that was updated

@@ -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:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this test class was moved from test_utils.py

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is the other change that is part of the fix where the whole convo is counted when there are no assistant messages present.

@sjrl
sjrl marked this pull request as ready for review September 4, 2026 06:41
@sjrl
sjrl requested a review from a team as a code owner September 4, 2026 06:41
@sjrl
sjrl requested review from davidsbatista and removed request for a team September 4, 2026 06:41
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core/pipeline
  pipeline.py
  haystack/hooks/compaction
  hooks.py
  utils.py
Project Total  

This report was generated by python-coverage-comment-action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CompactionHook over-counts context tokens when no assistant message is in the conversation

1 participant