fix(compress): reserve output tokens in the compaction threshold (#23767, #43547) - #50781
Merged
Merged
Conversation
Contributor
🔎 Lint report:
|
, #43547) The compaction trigger compared estimated input against context_length * threshold, but the provider reserves max_tokens of OUTPUT out of the same window. With a large max_tokens (e.g. 65536 on a custom provider) the usable input budget is materially smaller than the raw window, so sessions hit a provider 400 before compaction ever fired. _compute_threshold_tokens now subtracts the output reservation (context_length - max_tokens) before applying the percentage and the small-window 85% guard. max_tokens is stored on the compressor (threaded from agent.max_tokens at construction) and reused across update_model() switches; None = provider default = no reservation (full-window behavior, unchanged). Reimplemented on the current _compute_threshold_tokens surface (the inline threshold calc the original PR targeted was since refactored for the small-window #14690 fix); composes with that 85% guard on the effective budget. Credit: @kyssta-exe (#43651) — original design for the output-token reservation in the compaction threshold. Closes #43547.
kshitijk4poor
force-pushed
the
salvage/output-token-reservation-threshold
branch
from
June 22, 2026 11:56
584571a to
623b21b
Compare
kshitijk4poor
enabled auto-merge
June 22, 2026 11:59
waefrebeorn
pushed a commit
to waefrebeorn/slermes
that referenced
this pull request
Jul 2, 2026
…t-token-reservation-threshold fix(compress): reserve output tokens in the compaction threshold (NousResearch#23767, NousResearch#43547)
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…t-token-reservation-threshold fix(compress): reserve output tokens in the compaction threshold (NousResearch#23767, NousResearch#43547)
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…t-token-reservation-threshold fix(compress): reserve output tokens in the compaction threshold (NousResearch#23767, NousResearch#43547)
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…t-token-reservation-threshold fix(compress): reserve output tokens in the compaction threshold (NousResearch#23767, NousResearch#43547)
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…t-token-reservation-threshold fix(compress): reserve output tokens in the compaction threshold (NousResearch#23767, NousResearch#43547)
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
The compaction trigger compared estimated input against
context_length × threshold, but the provider reservesmax_tokensof output out of that same window. With a largemax_tokens(e.g. 65536 on a custom provider), the usable input budget is materially smaller than the raw window — so a session could hit a provider 400 ("context length exceeded") before compaction ever fired. The threshold is now based on the effective input budget(context_length − max_tokens). (Mode B of #23767; fixes #43547.)Salvage of #43651 by @kyssta-exe — reimplemented on the current threshold surface (see below).
Why a fresh commit (partly-superseded)
The original PR edited an inline
int(context_length * threshold_percent)inupdate_model/__init__. That code was since refactored into the static_compute_threshold_tokens()helper (for the small-window 85% guard, #14690), and the threshold reset now runs inupdate_modelalongside the #50137 calibration reset. A cherry-pick would conflict and reintroduce the old shape, so this re-implements the one surviving design point (output-token reservation) on the current surface.Changes
agent/context_compressor.py:_compute_threshold_tokens()gains an optionalmax_tokensparam and subtracts it from the effective window before both the percentage and the#14690small-window 85% guard;self.max_tokensstored in__init__and reused byupdate_model(optional explicit override).max_tokens=None(provider default) → no reservation → full-window behavior, byte-identical to before.agent/agent_init.py: passmax_tokens=agent.max_tokensat construction.tests/agent/test_context_compressor.py: 3 tests (reservation lowers threshold; small-window floor composition;max_tokens ≥ context_lengthfalls back to full window).Design note vs original PR
The original threaded
max_tokensas a requiredupdate_modelkwarg but updated only the one construction caller — the other 6update_modelcallers would have zeroed the reservation on every switch. This version storesmax_tokensand reuses it across switches (the output cap is a user setting, not model-specific), so the reservation survives/modelswitches; an explicit kwarg can still override it.Validation
Interactions verified orthogonal: #50137 (calibration reset in
update_model) and #50136 (tool-output persistence cap, different layer).Part of #23767 — this is the last of the 6 failure modes; #23767 can close once this lands.
Credit
_compute_threshold_tokenshelper.Closes #43547.
Infographic
Image generation is unavailable in this environment (FAL_KEY unset, no managed-provider credits); to be attached once available.