Skip to content

fix(compression): skip session rotation when compress returns message… - #50996

Closed
MorAlekss wants to merge 2 commits into
NousResearch:mainfrom
MorAlekss:fix/compression-skip-rotation-on-no-op
Closed

fix(compression): skip session rotation when compress returns message…#50996
MorAlekss wants to merge 2 commits into
NousResearch:mainfrom
MorAlekss:fix/compression-skip-rotation-on-no-op

Conversation

@MorAlekss

Copy link
Copy Markdown
Contributor

Summary

Prevents session rotation when compress() returns messages unchanged
due to a no-op early exit. Before this fix, a no-op compression could
trigger a full session rotation, archiving the conversation history
without any actual context reduction.


Root cause

compress_context() in agent/conversation_compression.py checked
_last_compress_aborted to detect failed compressions and skip
rotation. However, ContextCompressor.compress() has two additional
early-exit paths that return messages unchanged without setting
_last_compress_aborted:

  1. Line 2336: fewer messages than the minimum required for compression.
  2. Line 2361: compress_start >= compress_end — the entire transcript
    fits within the tail budget, leaving no compressible window. This
    path increments _ineffective_compression_count to eventually
    suppress future compression attempts via the anti-thrashing guard,
    but does not set _last_compress_aborted.

In both cases, compress_context() proceeded past the _last_compress_aborted
check with len(compressed) == len(messages) and entered the session
rotation path: end_session() was called, a new session_id was
assigned, and the conversation history was archived — all without any
actual compression having occurred.


Behavioral change

Before: a no-op compression caused a full session rotation, archiving
history and assigning a new session ID without reducing context size.

After: if len(compressed) >= len(messages) after compress() returns
and _last_compress_aborted is False, the rotation is skipped. The
lock is released and the original messages are returned unchanged, the
same way an explicit abort is handled.


What changed

agent/conversation_compression.py: added a guard after the
_last_compress_aborted check. If compress() returned as many or
more messages than the input, the function releases the lock and returns
without rotating the session.

tests/run_agent/test_infinite_compaction_loop.py: added
TestNoOpCompressionSkipsRotation with
test_no_op_compression_does_not_rotate_session. The test wires a real
session DB mock, configures compress() to return messages unchanged,
and asserts that end_session() is never called and the session ID
remains unchanged.


What is NOT changed

  • _last_compress_aborted logic unchanged
  • _ineffective_compression_count anti-thrashing guard unchanged
  • In-place compaction path unchanged
  • All existing compression tests pass

@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 labels Jun 22, 2026
@teknium1

Copy link
Copy Markdown
Contributor

Thanks @MorAlekss — correct fix for the no-op-compress-triggers-rotation path.

As of #52658 (#38763), compression.in_place now defaults to True: compaction keeps one durable session id and does not rotate at all, so the no-op→rotation path this guards no longer runs by default. Rotation remains only as the explicit legacy opt-out (compression.in_place: false), which we're not investing further in.

Closing as superseded by the in-place default — the code was a correct fix for the behavior at the time. Credit preserved.

@teknium1 teknium1 closed this Jun 25, 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 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.

3 participants