Skip to content

fix(agent): honor protect_last_n (capped) as the compaction tail floor (salvages #39170) - #45259

Merged
teknium1 merged 1 commit into
mainfrom
fix/compression-min-tail-floor
Jun 12, 2026
Merged

fix(agent): honor protect_last_n (capped) as the compaction tail floor (salvages #39170)#45259
teknium1 merged 1 commit into
mainfrom
fix/compression-min-tail-floor

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Summary

The tail-protection floor during compaction is now max(3, min(protect_last_n, 8)) messages instead of a hard 3 — so a compaction can no longer shave the conversation down to just 3 verbatim recent messages when the user's protect_last_n config promised more (#39170).

Changes

  • agent/context_compressor.py: _find_tail_cut_by_tokens min_tail floor honors protect_last_n up to a _MAX_TAIL_MESSAGE_FLOOR = 8 cap (full 20 would resurrect the oversized-tool-output "nothing compactable" case); short transcripts keep ≥2 compressible non-head messages so compression never runs as a no-op
  • tests/agent/test_context_compressor.py: +34 lines of floor/cap regression tests

Token budget remains primary: the 1.5x soft ceiling still wins, and the cut still falls back to right-after-head when even the floor exceeds it. Composes cleanly with the just-merged #45249 assistant-tail anchor (anchors run after this and only grow the tail).

Validation

Before After
protect_last_n: 20 (default), heavy compaction tail could shrink to 3 messages ≥8 recent messages kept verbatim
Oversized tool output in tail compactable (1.5x budget override) unchanged
Short transcript could summarize a tiny middle for zero savings ≥2 compressible turns guaranteed
compressor + anchor + boundary suites 146 passed*

*4 pre-existing SSL-socket env flakes in test_context_compressor.py reproduce identically on clean origin/main; not introduced here.

Attribution

Salvages #39170 by @konsisumer — cherry-picked with authorship preserved; rebase-merge to keep per-commit credit.

Infographic

min-tail-floor

@github-actions

Copy link
Copy Markdown
Contributor

🔎 Lint report: fix/compression-min-tail-floor vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 10881 on HEAD, 10881 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5705 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@teknium1
teknium1 merged commit aec3885 into main Jun 12, 2026
28 checks passed
@teknium1
teknium1 deleted the fix/compression-min-tail-floor branch June 12, 2026 23:27
yingliang-zhang added a commit to yingliang-zhang/hermes-agent that referenced this pull request Aug 20, 2026
PR NousResearch#45259 hardened the compaction tail floor from a fixed 3 to
min(protect_last_n, 8).  The cap of 8 was chosen to avoid preserving
a whole run of bulky tool outputs on every compaction, but it is not
configurable — users with large context windows and light tails
(e.g. 340K context, protect_last_n=20) cannot raise it beyond 8,
causing recent assistant replies to be summarized away even when
plenty of context budget remains.

This adds a compression.max_tail_message_floor config key (default 0
= use the module-level default of 8, preserving backward compatibility).
Set it higher (e.g. 20) to keep more recent messages verbatim during
compaction at the cost of a smaller summarization window when tool
outputs are bulky.

Changes:
- agent/context_compressor.py: rename _MAX_TAIL_MESSAGE_FLOOR to
  _DEFAULT_MAX_TAIL_MESSAGE_FLOOR, add max_tail_message_floor ctor
  param + _effective_max_tail_message_floor property, use it in
  _should_compact instead of the module-level constant.
- agent/agent_init.py: read compression.max_tail_message_floor from
  config and pass it to ContextCompressor.
- hermes_cli/config.py: add max_tail_message_floor to the
  compression config section.
- tests/agent/test_context_compressor.py: test the configurable floor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants