fix(compressor): skip context-summary markers as last-user tail anchor - #56165
Merged
Conversation
A context-compaction handoff banner is inserted with role="user" when the protected head ends in an assistant/tool message. On a resumed or multi-compaction session, _find_last_user_message_idx would return that banner as the latest user turn, so _ensure_last_user_message_in_tail anchored the tail to the summary and rolled the genuine last user message into the next compaction — the exact active-task loss the anchor exists to prevent (#10896/#22523). Reuse the existing _is_context_summary_content helper to skip summary banners when locating the last real user message. Salvaged from #36626 by Frank Song (issue #36624). The PR's other two changes (demoting completed tool results inside the protected tail; a preflight compression_exhausted result) are superseded on current main by the min_tail floor (#39170), the no-op compression counting (#40803), and the existing 413/disabled terminal-error paths.
Collaborator
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
Context compaction no longer mistakes its own handoff banner for the latest user turn. A
[CONTEXT COMPACTION — REFERENCE ONLY]summary is inserted withrole="user"when the protected head ends in an assistant/tool message; on a resumed or multi-compaction session,_find_last_user_message_idxreturned that banner as the "latest user message", so_ensure_last_user_message_in_tailanchored the tail to the summary and rolled the genuine last user turn into the next compaction — the exact active-task loss the anchor exists to prevent (#10896 / #22523).Root cause:
_find_last_user_message_idxreturned anyrole="user"message. The fix reuses the existing_is_context_summary_contenthelper (already used by_find_latest_context_summaryand the summarizer serialization) to skip summary banners.Changes
agent/context_compressor.py:_find_last_user_message_idxskips context-summary markers when locating the last real user turn.tests/agent/test_compressor_assistant_tail_anchor.py: addsTestFindLastUserMessageIdxSkipsSummaryMarker(skips user-role summary; returns real user when no summary; -1 when all user messages are summaries).Validation
test_compressor_assistant_tail_anchor.pygreentest_context_compressor.py+test_compression_boundary.py) greenSalvage note
Salvaged from #36626 by Frank Song (issue #36624), authorship preserved. The PR's other two proposed changes are superseded on current
mainand were intentionally dropped:_MAX_TAIL_MESSAGE_FLOOR=8(fix(agent): preserve recent turns during compression #39170) and no-op compression counting atcompress_start >= compress_end([Bug]: Infinite Context Compaction Loop (messages=N->N) on low context_length / limit configurations #40803); demoting the tail the model is about to read is unnecessary and risky.compression_exhaustedterminal result → main already returnscompression_exhaustedon the 413 path and an explicit "auto-compaction disabled" terminal error.Infographic
— Nous Research