fix: add context window overflow protection#27
Open
efecnc wants to merge 2 commits intoaltaidevorg:mainfrom
Open
fix: add context window overflow protection#27efecnc wants to merge 2 commits intoaltaidevorg:mainfrom
efecnc wants to merge 2 commits intoaltaidevorg:mainfrom
Conversation
Long conversations could exceed the model's context window, causing an immediate 400 error with no recovery path (compaction only ran after successful responses, creating a deadlock). Adds trim_context_to_budget() which estimates token count and removes oldest messages from the front when over budget, preserving: - The system message (index 0) - Tool call/response pairs (removed atomically) A marker message is inserted so the model knows earlier context was trimmed. Default budget is 120k tokens (conservative for most models).
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a context trimming mechanism to ensure chat history stays within a defined token budget. It adds a trim_context_to_budget function that prunes older messages while preserving the system message and keeping tool call/response pairs intact. A review comment suggests optimizing this function to avoid O(N^2) complexity by updating the token estimate incrementally instead of recalculating it in every iteration of the trimming loop.
Comment on lines
+110
to
+119
| let estimate_tokens = |msgs: &[crate::utils::ChatMessage]| -> usize { | ||
| msgs.iter() | ||
| .map(|m| { | ||
| m.content.as_ref().map_or(0, |c| c.text_content().len()) / 4 | ||
| + m.tool_calls | ||
| .as_ref() | ||
| .map_or(0, |tcs| tcs.iter().map(|t| t.function.arguments.len() / 4).sum()) | ||
| }) | ||
| .sum() | ||
| }; |
Contributor
There was a problem hiding this comment.
Contributor
Author
|
✅ Review fixes applied:
Ready for re-review. |
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
Root Cause
The reasoning loop fetched full context from memory and sent it directly to the provider with no size check. The existing compaction logic only triggered after a successful response, creating a deadlock: context too large → 400 error → no response → no compaction → permanent failure.
Test plan
cargo checkpasses (no new warnings)