fix(context): prevent orphaned tool results in MemoryFirst drain#2380
Merged
fix(context): prevent orphaned tool results in MemoryFirst drain#2380
Conversation
The MemoryFirst strategy drained conversation history with a fixed keep_tail=2, which left a Role::User/ToolResult message without its preceding Role::Assistant/ToolUse when a tool-call turn landed at the boundary. OpenAI rejects such sequences with HTTP 400. Extract memory_first_keep_tail() to walk backward from the drain boundary, extending keep_tail past any ToolResult messages until the first retained message is the matching assistant tool-call or a plain user message. Add four unit tests covering normal, single-round, multi-round, and degenerate cases.
fd3d00d to
a6a0872
Compare
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
memory_first_keep_tail()helper inassembly.rsthat extends the keep-tail window past anyRole::User/ToolResultmessages at the drain boundary, ensuring the first retained message is always the matchingRole::Assistant/ToolUseor a plain user messagecrossover_turn_threshold=5)Closes #2366
Test plan
cargo +nightly fmt --check— cleancargo clippy --workspace --features full -- -D warnings— cleancargo nextest run --config-file .github/nextest.toml --workspace --features full --lib --bins— 7163 passed, 0 failed