fix(compressor): pin summary role to user when only system prompt is protected (#52160) - #52167
Closed
liuhao1024 wants to merge 1 commit into
Closed
Conversation
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.
What does this PR do?
Fixes HTTP 400 errors from Anthropic (and Bedrock) after the second context compression. The compressor's summary message was assigned
role=assistantwhen only the system prompt was protected, making it the first visible message in the API request — which Anthropic unconditionally rejects.Related Issue
Fixes #52160
Type of Change
Changes Made
agent/context_compressor.py: Whenlast_head_role == "system"(only the system prompt is protected — typical on second+ compression), forcesummary_role = "user"and guard the tail-collision flip logic so it cannot revert to"assistant". If the first tail message is also"user", the summary merges into the tail instead.tests/agent/test_context_compressor.py: Two regression tests:test_double_compaction_summary_must_be_user_when_only_system_protected: Verifies the summary isrole=userafter the second compression.test_double_compaction_user_tail_merges_into_tail: Verifies that when the tail also starts withuser, the summary merges into the tail rather than flipping back toassistant.How to Test
python -m pytest tests/agent/test_context_compressor.py::TestCompressWithClient::test_double_compaction_summary_must_be_user_when_only_system_protected tests/agent/test_context_compressor.py::TestCompressWithClient::test_double_compaction_user_tail_merges_into_tail -vpython -m pytest tests/agent/test_context_compressor.py -q(122 tests, all should pass)Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/agent/test_context_compressor.py -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
agent/context_compressor.pyContextCompressor.compress()(lines 2596-2616, role-assignment block)last_head_role == "system", which is a previously-unhandled edge case_effective_protect_first_n()decay after first compression, Anthropic system-message extraction