fix(compression): count Codex replay fields in tail-protection budget (#55572) - #59804
Merged
Conversation
This was referenced Jul 6, 2026
This was referenced Jul 28, 2026
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
Codex/Responses sessions no longer compact continuously near the 272K cap: the tail-protection token estimator now counts provider replay fields (
codex_reasoning_items,codex_message_items,reasoning,reasoning_content,reasoning_details), so compaction actually sheds enough tokens to get back under threshold.Root cause: the preflight "should I compress?" estimator stringifies the full message dict and sees the encrypted reasoning blobs, but
_estimate_msg_budget_tokens(the tail-protection walk) only countedcontent+tool_calls. On a real 214-turn Codex session those blobs were ~115K tokens (27% of the payload) — the "20K" protected tail silently held 100K+ real wire tokens, so each compaction left the session above threshold and re-fired. Fixes #55572; resolves the community report of gpt-5.5 sessions becoming unusable at ~260K.Salvaged from PR #45485 by @bigstar0920 (cherry-picked, authorship preserved), rebased onto current main: the helper is folded into the existing
_estimate_msg_budget_tokens(which #28053's fix had already centralized since the PR branched) so all four call sites — both_find_tail_cut_by_tokenswalks and the tool-result prune boundary walk — get the replay accounting.Changes
agent/context_compressor.py:_serialized_length_for_budget()helper +_REPLAY_BUDGET_KEYS;_estimate_msg_budget_tokensnow adds replay-field weight (accounting only, never mutates/prunes the fields)tests/agent/test_context_compressor.py: contributor'sTestTailBudgetCodexReplayFieldssuite (per-field parametrized + combined case)Validation
E2E run via real
ContextCompressor.compress()with mocked LLM against temp HERMES_HOME.Infographic