Fix: Summarization - handle oversized messages and track statetool tokens#87
Merged
Conversation
…kens Two summarization-related bugs: Issue #85 — oversized messages stalled summarization. When a single record at the head of the history was larger than the summarization threshold, the record-selection walk chose nothing, so summarization fired on every query but never made progress. Selection and context-fitting are now separate gates: get_records_up_to_threshold selects records (and falls back to the minimal oversized prefix when the head record alone exceeds the threshold), and fit_records_to_context middle-truncates only the copies sent to the summarizer when the selected records overflow the context window. A record above the threshold but below the window is summarized in full; originals are never mutated. Issue #86 — statetool injections skewed token accounting. Statetool input/output pairs are ephemeral tail injections that never land in the persisted records, so their size was being absorbed into base_tokens and destabilizing estimates across loop steps. ChatHistory now tracks the last-known size of each statetool's injection separately (statetool_token_estimates) and folds it into token_overhead. Entries are updated when a statetool fires and retained (not zeroed) when it doesn't. Also clarifies summarizers.py: a module docstring describing the select-then-fit pipeline, helper extraction for the tool-response boundary walks, and explanatory comments on the fit budget. Adds tests/fake_model.py plus coverage for both fixes.
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.
Resolves two summarization-related bugs, issues #85 and #86:
Issue #85 — oversized messages stalled summarization. When a single record at the head of the history was larger than the summarization threshold, the record-selection walk chose nothing, so summarization fired on every query but never made progress. Selection and context-fitting are now separate gates: get_records_up_to_threshold selects records (and falls back to the minimal oversized prefix when the head record alone exceeds the threshold), and fit_records_to_context middle-truncates only the copies sent to the summarizer when the selected records overflow the context window. A record above the threshold but below the window is summarized in full; originals are never mutated.
Issue #86 — statetool injections skewed token accounting. Statetool input/output pairs are ephemeral tail injections that never land in the persisted records, so their size was being absorbed into base_tokens and destabilizing estimates across loop steps. ChatHistory now tracks the last-known size of each statetool's injection separately (statetool_token_estimates) and folds it into token_overhead. Entries are updated when a statetool fires and retained (not zeroed) when it doesn't.
Also clarifies summarizers.py: a module docstring describing the select-then-fit pipeline, helper extraction for the tool-response boundary walks, and explanatory comments on the fit budget.
Adds tests/fake_model.py plus coverage for both fixes.