Repository navigation
fix(llm): make ChatContext.truncate() honor a non-positive max_items - #7367
Merged
Merged
Conversation
sxh313
force-pushed
the
fix/chat-context-truncate-zero
branch
from
September 20, 2026 16:56
642ffb8 to
b5acab8
Compare
longcw
reviewed
Sep 21, 2026
sxh313
force-pushed
the
fix/chat-context-truncate-zero
branch
2 times, most recently
from
September 25, 2026 12:53
dfa5c16 to
70710f2
Compare
`items[-max_items:]` is the whole list when max_items is 0, so truncating to zero silently kept the entire conversation; a negative value trimmed the front.
Review feedback: a negative budget has no sensible meaning, so rejecting it is better than guessing an empty one. Zero still keeps only the preserved instruction; the guard sits ahead of the under-limit early return so the contract holds for every context size.
sxh313
force-pushed
the
fix/chat-context-truncate-zero
branch
from
September 25, 2026 15:30
70710f2 to
e7b8d68
Compare
longcw
approved these changes
Sep 30, 2026
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.
What
ChatContext.truncate(max_items=...)usedself._items[-max_items:]. In Python-0is0, anditems[0:]is the whole list -- so truncating to a budget of zero items returned every item, and a negative budget silently trimmed from the front of the conversation instead of keeping nothing.The guard now treats any non-positive budget as "no conversational items", which still leaves the preserved instruction message, exactly as the docstring describes.
Why it matters
truncateis the documented way to bound a context (llm/chat_context.py:571). A budget normally comes from a token/message calculation, and the interesting case is the one where it reaches 0: the caller asks for no history and gets all of it, so the request goes out at full size instead of failing or shrinking -- the opposite of what the bound was for.#4843/#4845earlier fixed which roles count as an instruction and the duplicate insert, but the slice itself was untouched, so this path is still live.Valid (positive) budgets are unaffected:
len(self._items) <= max_itemsstill short-circuits, anditems[-max_items:]is unchanged formax_items > 0.Tests
Three regression cases in
tests/test_chat_ctx.py, next to the existing truncate tests. Differential, with onlychat_context.pyreverted:ruff checkandruff format --checkare clean on both files.AI disclosure
This fix was produced with an AI coding assistant (Claude), reviewed and verified by me locally as described above.