Skip to content

fix(compressor): prevent orphan user turn after compaction (turn-pair preservation) - #56121

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-4e547cdc
Jul 1, 2026
Merged

fix(compressor): prevent orphan user turn after compaction (turn-pair preservation)#56121
teknium1 merged 2 commits into
mainfrom
hermes/hermes-4e547cdc

Conversation

@teknium1

@teknium1 teknium1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Compaction no longer orphans the last user turn. When the last user message sits exactly at head_end (the first compressible index), the completed turn-pair is summarised as a unit instead of being split — so the summariser stops recording a finished ask as "pending" and the next session stops re-executing already-done work.

Root cause: _ensure_last_user_message_in_tail's final max(last_user_idx, head_end + 1) clamp returns head_end + 1 when last_user_idx == head_end, pushing the user past the cut into the compressed region without its assistant reply.

Salvages #22523 by @H2KFORGIVEN — reimplemented on current main (the original branch was ~6000 commits stale; the code moved from line ~1190 to ~2332).

Changes

  • agent/context_compressor.py: add _find_turn_pair_end() and a Causal Coupling guard in _ensure_last_user_message_in_tail — when the clamp would orphan the user, push the cut forward to pair_end so (user → assistant → tool results) lands together in the summary.
  • tests/agent/test_context_compressor.py: 8 new tests in TestTurnPairPreservation (4 for _find_turn_pair_end, 4 for the anchor incl. an end-to-end no-orphan invariant).
  • scripts/release.py: AUTHOR_MAP entry for @H2KFORGIVEN.

Validation

E2E-verified on current main: the orphan reproduces exactly as described (returns head_end+1, user pushed out of the tail); the fix keeps the completed pair together and the tail starts on an assistant reply. #10896's mid-conversation pullback is unchanged (guard only fires when last_user_idx == head_end).

Before After
last user at head_end, cut>head_end returns head_end+1 → user orphaned in summary returns pair_end → user+reply summarised together
tail start after compaction dangling user ask assistant reply
compressor tests 125 pass 133 pass (8 new)

Infographic

Turn-Pair Preservation

H2KFORGIVEN and others added 2 commits July 1, 2026 00:14
…air preservation

When the last user message sits exactly at head_end (the first compressible
index), _ensure_last_user_message_in_tail's final max(last_user_idx,
head_end + 1) clamp returns head_end + 1, pushing the user into the compressed
region without its assistant reply. The summariser then records it as a
pending ask, and the next session re-executes the already-completed task
(lights off twice, file deleted twice, message re-sent).

Fix: apply Causal Coupling — a compaction boundary must never split a
(user -> assistant [-> tool results]) turn-pair. Add _find_turn_pair_end and,
when the clamp would orphan the user, push the cut forward to pair_end so the
completed pair is summarised together and marked done.

8 new tests in TestTurnPairPreservation; 133 compressor tests pass.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P1 High — major feature broken, no workaround sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state labels Jul 1, 2026
@teknium1
teknium1 merged commit 3aebdb1 into main Jul 1, 2026
31 checks passed
@teknium1
teknium1 deleted the hermes/hermes-4e547cdc branch July 1, 2026 07:27
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 sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants