Skip to content

fix(compression): 3-line fix for infinite compression loop (#29335) - #31730

Closed
someaka wants to merge 1 commit into
NousResearch:mainfrom
someaka:fix/29335-compression-session-id-propagation-clean
Closed

fix(compression): 3-line fix for infinite compression loop (#29335)#31730
someaka wants to merge 1 commit into
NousResearch:mainfrom
someaka:fix/29335-compression-session-id-propagation-clean

Conversation

@someaka

@someaka someaka commented May 24, 2026

Copy link
Copy Markdown

Problem

When context compression fires, three compounding issues cause an infinite compression loop:

Issue A — session_id not propagated

_compress_context() updates self.session_id to the new session, but run_conversation() never includes session_id in its return dict. The gateway at gateway/run.py:8700 has a guard but it's dead code — the agent never sends session_id.

Issue B — preflight bypasses anti-thrashing

Preflight compression uses a raw threshold comparison, bypassing should_compress() which has anti-thrashing logic. When system prompt + tool schemas dominate token count, compressing messages doesn't bring total below threshold — preflight re-triggers every turn despite saving <10%.

Issue C — gateway doesn't persist session_id update

Gateway updates session_entry.session_id in memory but doesn't call session_store._save(). Two other compression paths in the same file DO persist — this one was missed.

Fix — 4 lines, 2 files

 agent/conversation_loop.py | 3 +++
 gateway/run.py              | 1 +
 2 files changed, 4 insertions(+), 1 deletion(-)

Fixes: #29335

…rch#29335)

Three compounding root causes:

A) run_conversation() result dict missing session_id — gateway's
   dead-code guard at gateway/run.py:8700 never triggers
B) preflight compression bypasses should_compress() anti-thrashing —
   re-triggers every turn when tool schemas dominate token budget
C) gateway updates session_entry.session_id in memory but doesn't
   persist via session_store._save()

Fixes: NousResearch#29335
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint comp/gateway Gateway runner, session dispatch, delivery labels May 24, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Competing with open PRs #29505 (3-part fix: should_compress() + session_id + _save()), #26204, and #26088 — all fix #29335 (infinite compression loop due to session_id not propagated after compression). Clean replacement of stacked #31725. This PR addresses issues A (session_id not in result dict), B (preflight bypasses anti-thrashing), and C (gateway doesn't persist session_id update).

@teknium1

Copy link
Copy Markdown
Contributor

Merged via #31930 — your commit was cherry-picked onto current main with authorship preserved (3914089d5). Thanks for the fix and the clean root-cause writeup!

Follow-up commit (11c40d6a4) on top:

  • Polish: dropped the redundant _preflight_tokens >= threshold_tokens clause next to your new should_compress(_preflight_tokens) call (should_compress already short-circuits below threshold, so the explicit comparison was dead code on the True branch).
  • Tests: pinned the anti-thrash gating (mocks should_compress to return False even with tokens past threshold and asserts no compression runs — exact bug shape from Bug: Context compression creates new session but gateway never sees it — infinite compression loop #29335) and added an AST-scan test that asserts every session_entry.session_id = ... assignment in gateway/run.py is followed by a session_store._save() call within the same block. Empirically verified the second test catches the bug by reverting the new _save() line → red, restoring → green.
  • AUTHOR_MAP entry mapping ed@bebop.crew to @someaka for the release notes.

Closes #29335.

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