Repository navigation
fix(compressor): prevent orphan user turn after compaction (turn-pair preservation) #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hashbender
wants to merge
1
commit into
main
Choose a base branch
from
mirror/pr-56121
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 _find_turn_pair_end truncates multi-step tool-call turns, leaking mid-turn messages into the tail without user context (bug)
The new
_find_turn_pair_endhelper (agent/context_compressor.py:2406-2431) is called by the Causal Coupling guard in_ensure_last_user_message_in_tail(line 2394) to push the compaction boundary forward past the complete user→assistant→tool turn-pair when the user sits at the head boundary and cannot be pulled into the tail. However, the function only walks forward past the FIRST assistant message and its immediate consecutive tool results, then stops at the next non-tool role. This misses subsequent assistant(tool_calls)→tool(result) cycles and the assistant's final text reply, all of which are part of the same user turn. In real Hermes agent loops, a single user message commonly triggers multiple rounds of tool calls (the agent loops: call model → get tool_calls → execute → add tool results → call model again → … → get text reply). When the Causal Coupling guard fires with such a multi-step turn, the truncated pair_end causes: (1) only the first segment enters the compression summary (so the summarizer sees user asked + assistant started work + first tool result, but no completion); (2) the remaining mid-turn messages (subsequent assistant tool_calls + results + final text reply) land in the tail without their originating user context. The model on the next session may see mid-turn messages with no user instruction, causing confusion or re-execution of already-completed work.💡 Suggestion: Rewrite
_find_turn_pair_endto walk forward until the next user-role message (or end of list), which is the natural boundary between turns. This captures the complete multi-step turn: all assistant and tool messages belonging to the same user message stay together on one side of the compaction cut.📋 Prompt for AI Agents
In agent/context_compressor.py, replace the body of
_find_turn_pair_end(lines 2421-2431) with a loop that walks forward past ALL non-user messages:idx = user_idx + 1; while idx < n and messages[idx].get('role') != 'user': idx += 1; return idx. Update the docstring to document multi-step tool-call handling: change 'A turn-pair is: user -> assistant [-> zero-or-more tool results]' to describe the full agent-loop pattern including multiple assistant→tool cycles. Add a test case in tests/agent/test_context_compressor.py (TestTurnPairPreservation class) for a multi-step turn: user → assistant(tool_calls) → tool → assistant(tool_calls) → tool → assistant(text) → next_user, verifying pair_end returns the index of next_user.