Skip to content

fix(compression): count Codex replay fields in tail-protection budget (#55572) - #59804

Merged
teknium1 merged 2 commits into
mainfrom
fix/codex-replay-tail-budget
Jul 6, 2026
Merged

fix(compression): count Codex replay fields in tail-protection budget (#55572)#59804
teknium1 merged 2 commits into
mainfrom
fix/codex-replay-tail-budget

Conversation

@teknium1

@teknium1 teknium1 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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 counted content + 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_tokens walks 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_tokens now adds replay-field weight (accounting only, never mutates/prunes the fields)
  • tests/agent/test_context_compressor.py: contributor's TestTailBudgetCodexReplayFields suite (per-field parametrized + combined case)

Validation

Before After
Tail estimate, 10-msg Codex tail w/ 8KB blobs 765 tok 10,825 tok (real weight)
E2E compress() shed on 60-turn Codex-like session near-zero (tail held blobs) 67.7% serialized chars
Blob-bearing messages kept all in tail 19/60 (tail only)
tests: test_context_compressor.py, compression_boundary, infinite_compaction_loop, image_tokens all pass

E2E run via real ContextCompressor.compress() with mocked LLM against temp HERMES_HOME.

Infographic

infographic

@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state P2 Medium — degraded but workaround exists labels Jul 6, 2026
@teknium1
teknium1 merged commit 8cc1ca4 into main Jul 6, 2026
30 of 31 checks passed
@teknium1
teknium1 deleted the fix/codex-replay-tail-budget branch July 6, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tail-protection token estimate ignores codex_reasoning_items, so Codex/Responses sessions compact too late

3 participants