Skip to content

feat: make context compaction preserve recent turns - #7682

Closed
BestLemoon wants to merge 1 commit into
NousResearch:mainfrom
BestLemoon:feat/context-compression-heavy-handoff-clean
Closed

feat: make context compaction preserve recent turns#7682
BestLemoon wants to merge 1 commit into
NousResearch:mainfrom
BestLemoon:feat/context-compression-heavy-handoff-clean

Conversation

@BestLemoon

Copy link
Copy Markdown

Summary

  • add a separate protect_recent_n floor so the newest turns stay verbatim during compaction
  • add request-time micro-compaction to prune old tool outputs before triggering full compression
  • add a heavier repeated-compaction handoff mode so later summaries compress more aggressively under sustained pressure

Testing

  • source venv/bin/activate && python -m pytest tests/agent/test_context_compressor.py -q

@alt-glitch alt-glitch added type/feature New feature or request P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Apr 29, 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 the detailed compaction work. The title-level recent-turn problem has since been addressed on current main by aec38855b5792e4a912d527b64df1a43cbf09c90 (fix(agent): preserve recent turns during compression), which added a bounded preserved tail and regression coverage in tests/agent/test_context_compressor.py:2517.

Problems

  • The proposed per-request _prepare_messages_for_request() call (run_agent.py, diff RIGHT line 7793) prunes retained history before every API request. That mutates the cached prefix outside the current full-compaction path (agent/conversation_loop.py:1007-1058), conflicting with the prompt-cache invariant in AGENTS.md:19-23.
  • compression.protect_recent_n is added (hermes_cli/config.py, diff RIGHT line 357) without corresponding tests or documentation, despite compression settings being documented in website/docs/user-guide/configuration.md:737-745.

Suggested changes

  • Use the merged bounded-tail behavior as the baseline and scope any remaining micro/heavy-compaction idea as a cache-safe compression design on current main.
  • Add end-to-end coverage for config propagation and user/assistant/tool-group preservation before introducing a separate floor.

Automated hermes-sweeper review.

Comment thread run_agent.py
@@ -7741,6 +7793,11 @@ def run_conversation(
# Note: Reasoning is embedded in content via <think> tags for trajectory storage.

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 runs a history-mutating tool-result prune before every model request, not only at an explicit compression boundary. That invalidates the cached conversation prefix during the tool loop; please keep retained-message mutation within the full compaction path, with its session/cache bookkeeping.

@teknium1 teknium1 added sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) 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 labels Jul 12, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks @BestLemoon — this PR correctly identified the preserve-recent-turns gap before it was fixed on main. Where each piece stands now:

Closing as superseded — credit for early identification of the recent-turn loss problem. Thanks for the contribution!

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:blast-broad Sweeper blast radius: broad — a core path most sessions hit sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) 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/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants