Skip to content

fix(agent+gateway): break infinite context compression loop by persis… - #29505

Closed
LifeJiggy wants to merge 1 commit into
NousResearch:mainfrom
LifeJiggy:fix/context-compression-loop
Closed

fix(agent+gateway): break infinite context compression loop by persis…#29505
LifeJiggy wants to merge 1 commit into
NousResearch:mainfrom
LifeJiggy:fix/context-compression-loop

Conversation

@LifeJiggy

Copy link
Copy Markdown
Contributor

Title: fix(agent+gateway): break infinite context compression loop by persisting rotated session ID

Summary

Three-part fix for the infinite context compression loop that occurs when
compression creates a new session but the gateway never learns about it:

  1. Preflight anti-thrashing (agent/conversation_loop.py): Use
    should_compress() in preflight check instead of raw >= threshold_tokens
    comparison, so the ineffective-compression guard prevents repeated
    compression passes when system prompt overhead dominates.

  2. Session ID return (agent/conversation_loop.py): Include
    session_id in the run_conversation() result dict so the gateway
    can detect when compression rotated the session.

  3. Persist rotation (gateway/run.py): Call session_store._save()
    after updating session_entry.session_id so the change survives
    gateway restarts and session reloads.

Changes

File Change
agent/conversation_loop.py:442 Added should_compress() guard to preflight
agent/conversation_loop.py:3999 Added session_id to result dict
gateway/run.py:8512-8513 Persist session ID change via _save()

Fixes #29335

@alt-glitch alt-glitch added P1 High — major feature broken, no workaround 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 labels May 20, 2026
@LifeJiggy
LifeJiggy force-pushed the fix/context-compression-loop branch from 1495303 to 096d0c7 Compare May 21, 2026 02:20
zombi3butt pushed a commit to zombi3butt/hermes-agent that referenced this pull request May 21, 2026
…onversation (issue NousResearch#29926)

When auto-compression rotates the session mid-run, result["messages"]
contains the inflated post-turn list (compressed baseline + this turn's
growth). The CLI was overwriting conversation_history with this inflated
list, causing the next turn to start from 130K+ tokens instead of the
compressed ~24K baseline — wasting VRAM and API time.

Fix: detect session rotation via _cli_last_run_old_session_id (captured
before run_conversation in the agent thread) and use agent._session_messages
instead of result["messages"] when rotation occurred. Falls back to
result["messages"] for normal (non-rotated) runs.

Mirrors the gateway path fix in PR NousResearch#29505.
zombi3butt pushed a commit to zombi3butt/hermes-agent that referenced this pull request May 21, 2026
…onversation (issue NousResearch#29926)

When auto-compression rotates the session mid-run, result["messages"]
contains the inflated post-turn list (compressed baseline + this turn's
growth). The CLI was overwriting conversation_history with this inflated
list, causing the next turn to start from 130K+ tokens instead of the
compressed ~24K baseline — wasting VRAM and API time.

Fix: detect session rotation via _cli_last_run_old_session_id (captured
before run_conversation in the agent thread) and use agent._session_messages
instead of result["messages"] when rotation occurred. Falls back to
result["messages"] for normal (non-rotated) runs.

Mirrors the gateway path fix in PR NousResearch#29505.
@LifeJiggy
LifeJiggy force-pushed the fix/context-compression-loop branch from 096d0c7 to 2660d3a Compare May 26, 2026 12:54
@LifeJiggy

Copy link
Copy Markdown
Contributor Author

@teknium1 PTAL

…urability

Tests (24 new):
- Anti-thrashing protection blocks after 2 ineffective compressions
- session_id and compression_loop_count in result dict
- Gateway session_store._save() and Telegram topic binding sync
- Preflight pass limit and savings tracking

Enhancements:
1. compression_loop_count in result dict — observability metric showing
   how many preflight compression passes ran this turn
2. Configurable compression.max_preflight_passes (default 3) — replaces
   hardcoded range(3) in preflight loop
3. Anti-thrashing warning dedup — _anti_thrash_warning_emitted flag
   prevents repeated warnings within a turn; resets on good compression
@LifeJiggy
LifeJiggy force-pushed the fix/context-compression-loop branch from 2660d3a to ce162c9 Compare June 9, 2026 05:19
@LifeJiggy

Copy link
Copy Markdown
Contributor Author

Tests added (tests/agent/test_compression_loop_fix.py — 24 tests):

  • Anti-thrashing: blocks after 2 ineffective compressions, resets on good compression
  • Result dict: session_id and compression_loop_count present
  • Gateway: _save() and _sync_telegram_topic_binding() called after rotation
  • Config: max_preflight_passes defaults to 3, configurable
  • Warning dedup: _anti_thrash_warning_emitted prevents log spam

3 Enhancements:

  1. compression_loop_count in result dict — observability for how many preflight passes ran
  2. compression.max_preflight_passes config option — replaces hardcoded range(3)
  3. Anti-thrashing warning dedup — _anti_thrash_warning_emitted flag, resets on good compression

@teknium1

Copy link
Copy Markdown
Contributor

Closing as superseded by the compression split fix that landed in PR #45529:
#45529

That merged change now syncs compression-created session IDs before failed/no-final-response paths can return, resets the history offset for split failures, and refreshes the Telegram topic binding on the rotated child session. Thanks for the fix/report — the payload here is now covered on current main.

@teknium1 teknium1 closed this Jun 13, 2026
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 comp/gateway Gateway runner, session dispatch, delivery P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Context compression creates new session but gateway never sees it — infinite compression loop

3 participants