Skip to content

fix(agent): preserve recent turns during compression - #39170

Closed
konsisumer wants to merge 1 commit into
NousResearch:mainfrom
konsisumer:fix/context-compression-tail-preservation
Closed

fix(agent): preserve recent turns during compression#39170
konsisumer wants to merge 1 commit into
NousResearch:mainfrom
konsisumer:fix/context-compression-tail-preservation

Conversation

@konsisumer

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes context compression tail selection so token-exhausted compactions keep a bounded run of recent messages verbatim instead of preserving only the latest user anchor plus a tiny tail. This prevents recent assistant/tool context immediately before the latest ask from being folded into the summary, while capping the message floor so large tool-output tails can still be compacted.

Related Issue

Fixes #9413

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • ✨ New feature (non-breaking change that adds functionality)
  • 🔒 Security fix
  • 📝 Documentation update
  • ✅ Tests (adding or improving test coverage)
  • ♻️ Refactor (no behavior change)
  • 🎯 New skill (bundled or hub)

Changes Made

  • agent/context_compressor.py: replace the hard-coded 3-message protected tail floor with a bounded protect_last_n-aware floor, capped at 8 messages and constrained to leave a real middle window to summarize.
  • tests/agent/test_context_compressor.py: add a regression test for tiny-budget compression preserving recent turns before the latest user ask.

How to Test

  1. HERMES_HOME=/private/tmp/hermes-agent-test-home-issue-new-5b851828 PATH="$VIRTUAL_ENV/bin:$PATH" /opt/homebrew/bin/timeout -k 30 480 sh -c 'pytest tests/ -q -x --timeout=60 "$@"' sh
  2. /opt/homebrew/bin/timeout -k 30 480 pytest tests/agent/test_context_compressor.py -q
  3. /opt/homebrew/bin/timeout -k 30 480 pytest -q -x --timeout=60 tests/agent/test_context_compressor.py tests/run_agent/test_compression_boundary.py tests/agent/test_compressor_image_tokens.py tests/agent/test_compressor_historical_media.py tests/agent/test_context_compressor_summary_continuity.py tests/run_agent/test_compressor_fallback_update.py
  4. ruff check agent/context_compressor.py tests/agent/test_context_compressor.py
  5. python scripts/check-windows-footguns.py agent/context_compressor.py tests/agent/test_context_compressor.py
  6. git diff --check

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS local worker (darwin-arm64)

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

For New Skills

  • This skill is broadly useful to most users (if bundled) — see Contributing Guide
  • SKILL.md follows the standard format (frontmatter, trigger conditions, steps, pitfalls)
  • No external dependencies that aren't already available (prefer stdlib, curl, existing Hermes tools)
  • I've tested the skill end-to-end: hermes --toolsets skills -q "Use the X skill to do Y"

Screenshots / Logs

  • tests/agent/test_context_compressor.py -q: 92 passed in 40.64s.
  • Compressor/boundary covering subset: 142 passed in 46.36s.
  • Scoped lint: ruff check agent/context_compressor.py tests/agent/test_context_compressor.py passed.
  • Windows footgun scan: python scripts/check-windows-footguns.py agent/context_compressor.py tests/agent/test_context_compressor.py passed.
  • git diff --check passed.
  • Full suite was attempted with the shared venv and writable HERMES_HOME; it stopped early in unrelated tests/acp/test_edit_approval.py::test_write_file_approval_mutates_and_request_includes_diff because this sandbox treats pytest's /private/var/.../T temp path as a sensitive system path, so write_file refused the test write before the suite reached compressor coverage.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jun 4, 2026
@konsisumer

Copy link
Copy Markdown
Contributor Author

Rebased onto origin/main and resolved the context-compressor merge conflict. Scoped verification passed (ruff check, tests/agent/test_context_compressor.py, python scripts/check-windows-footguns.py agent/context_compressor.py, git diff --check). The bounded full-suite run still hits an unrelated failure outside this PR's scope in tests/acp/test_approval_isolation.py.

@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #45259 — your commit was cherry-picked onto current main with your authorship preserved in git log (aec3885). The capped protect_last_n floor + short-transcript guard landed as-is. Thanks!

#45259

@teknium1 teknium1 closed this Jun 12, 2026
teknium1 pushed a commit that referenced this pull request Jul 1, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(#10896/#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from #36626 by Frank Song (issue #36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (#39170), the no-op compression counting (#40803), and the existing
413/disabled terminal-error paths.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
Jasper6439 pushed a commit to Jasper6439/hermes-agent that referenced this pull request Jul 5, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
waym0reom3ga pushed a commit to waym0reom3ga/autolycus-agent that referenced this pull request Jul 23, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
A context-compaction handoff banner is inserted with role="user" when the
protected head ends in an assistant/tool message. On a resumed or
multi-compaction session, _find_last_user_message_idx would return that
banner as the latest user turn, so _ensure_last_user_message_in_tail anchored
the tail to the summary and rolled the genuine last user message into the
next compaction — the exact active-task loss the anchor exists to prevent
(NousResearch#10896/NousResearch#22523).

Reuse the existing _is_context_summary_content helper to skip summary banners
when locating the last real user message.

Salvaged from NousResearch#36626 by Frank Song (issue NousResearch#36624). The PR's other two changes
(demoting completed tool results inside the protected tail; a preflight
compression_exhausted result) are superseded on current main by the min_tail
floor (NousResearch#39170), the no-op compression counting (NousResearch#40803), and the existing
413/disabled terminal-error paths.
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 type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Context compression drops recent conversation记忆 and injects cross-session fragments

3 participants