Skip to content

[codex] Fix CJK context compression budgeting - #52517

Closed
plainOldCode wants to merge 1 commit into
NousResearch:mainfrom
plainOldCode:codex/compression-cjk-token-budgeting
Closed

[codex] Fix CJK context compression budgeting#52517
plainOldCode wants to merge 1 commit into
NousResearch:mainfrom
plainOldCode:codex/compression-cjk-token-budgeting

Conversation

@plainOldCode

Copy link
Copy Markdown

Summary

Fix context compression for CJK-heavy conversations and make gateway hygiene compression configurable.

This PR addresses two compression issues observed in long Korean Telegram gateway sessions:

  • Re-compaction could keep an existing protected-head compaction handoff while also inserting a new one, causing stale summaries to accumulate in the live transcript.
  • Rough token estimation used an English-centric chars / 4 heuristic, which substantially undercounted Hangul/CJK text. That made the compressor preserve too much recent tail context and report much lower request sizes than providers reported.

Changes

  • Replace protected-head context handoff summaries with the newly generated updated summary during re-compaction.
  • Add CJK/Hangul/Kana-aware rough token estimation.
  • Use the same CJK-aware estimator for compressor tail-budget decisions.
  • Add compression.hygiene_threshold support for gateway pre-agent hygiene compression while preserving the existing 85% default when unset.
  • Add regression coverage for protected handoff replacement and CJK token budgeting.

Root Cause

The compressor's tail selection depended on rough token estimates. For Korean/CJK text, the old len(text) / 4 estimate was too low, so the protected tail was much larger than intended. As a result, manual or automatic compression could report a small rough request size while the next provider API call still had a large prompt.

Additionally, resumed in-place compaction could rehydrate a persisted handoff into _previous_summary but still copy the old handoff into the new compressed transcript. Repeated compactions then retained old summaries in live context.

Validation

uv run --extra dev pytest tests/agent/test_context_compressor.py tests/agent/test_context_compressor_summary_continuity.py tests/agent/test_cjk_token_estimation.py -q
128 passed in 1.55s

Also tested against a live Korean gateway session:

  • Before estimator fix, rough request estimate was around 38k while provider usage stayed around 56k.
  • After estimator fix, rough request estimate rose to the same scale as provider usage, and compressor tail selection became materially more aggressive.

@plainOldCode
plainOldCode marked this pull request as ready for review June 25, 2026 14:23
@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 comp/gateway Gateway runner, session dispatch, delivery sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state P2 Medium — degraded but workaround exists labels Jun 25, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for tracing both compression paths. The two reported defects are present on current main: agent/model_metadata.py:2458-2467 still applies chars / 4, and agent/context_compressor.py:3061-3073 copies a protected handoff before adding the replacement summary.

Problems

  • The proposed dense-character predicate omits ordinary Hiragana (U+3040–U+309F) and Katakana (U+30A0–U+30FF), although the PR describes Kana-aware budgeting. Kana-only Japanese text would still use the English chars / 4 path. Please cover these ranges with regression tests.
  • compression.hygiene_threshold is only read in gateway/run.py; it is not declared with the other compression settings in hermes_cli/config.py:1412-1422 or documented alongside the existing gateway hygiene setting in website/docs/user-guide/configuration.md:738-758.

Suggested changes

  • Add the missing Kana ranges and tests for estimator and tail-budget behavior.
  • Add the default/config documentation and a gateway configuration-path test for valid override and invalid fallback.

Automated hermes-sweeper review.

Comment thread agent/model_metadata.py
or 0x2E80 <= code <= 0x9FFF # CJK radicals/ideographs
or 0xA960 <= code <= 0xA97F # Hangul Jamo Extended-A
or 0xAC00 <= code <= 0xD7AF # Hangul Syllables
or 0xF900 <= code <= 0xFAFF # CJK compatibility ideographs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This does not include standard Hiragana (U+3040–U+309F) or Katakana (U+30A0–U+30FF); FF00–FFEF only covers halfwidth kana/fullwidth forms. Please add those ranges and Japanese-script regression coverage before calling this Kana-aware.

@teknium1 teknium1 added sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit area/compression Context compression and continuation sessions labels Jul 15, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Merged via #69302 (commit ea0fd39). Your commit was cherry-picked with authorship preserved. The estimator gained an ASCII fast-path perf gate during salvage; the gateway hygiene knob was split out as scope. Thanks!

@teknium1 teknium1 closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/compression Context compression and continuation sessions comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists sweeper:blast-broad Sweeper blast radius: broad — a core path most sessions hit sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades 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.

3 participants