fix(context): trim tool exchanges atomically - #292
Open
louiseboo wants to merge 2 commits into
Open
Conversation
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
Prevent context budget trimming from separating an assistant tool call from its matching tool result messages.
The existing canonicalization step closes selected history over tool exchanges, but the later budget loop removes one message at a time. That can leave an orphaned tool result in the provider request and cause OpenAI to reject a long-running session with HTTP 400.
This change treats an assistant tool-call message and all of its matching result messages as one structural unit during budget trimming. The regression test reproduces the previous orphaned-result case and verifies that the final prompt remains structurally valid.
Type
Verification
uv run pytest tests/test_history_trimmer.py -q- 3 passed.uv run ruff check raven/context_engine/history_trimmer.py tests/test_history_trimmer.py- passed.uv run ruff format --check raven/context_engine/history_trimmer.py tests/test_history_trimmer.py- passed.Relevant tests pass locally
Relevant lint / type checks pass locally
User-facing docs or screenshots are updated when needed
No user-facing documentation changes are required for this internal context-trimming fix.
Risk
Budget trimming may remove more than one history message when the selected message belongs to a tool exchange. This is intentional and preserves the provider message contract. Rollback is a revert of this change.
Related Issues
Fixes #291