fix(compressor): prevent orphan user turn after compaction (turn-pair preservation) - #56121
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 finalmax(last_user_idx, head_end + 1)clamp returnshead_end + 1whenlast_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 topair_endso(user → assistant → tool results)lands together in the summary.tests/agent/test_context_compressor.py: 8 new tests inTestTurnPairPreservation(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 (returnshead_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 whenlast_user_idx == head_end).head_end, cut>head_endhead_end+1→ user orphaned in summarypair_end→ user+reply summarised togetheruseraskassistantreplyInfographic