Give HTTP chat context management: windowing, compaction, and a compaction event#561
Merged
Conversation
tobocop2
added a commit
that referenced
this pull request
Jul 20, 2026
…at-window clamp with #561 Main's #561 centralized the generation reserve and window margin into providers/base.py as prompt_token_budget(); this branch had rewritten the same _fit_chat_context lines to add the over-reservation clamp. Resolved by re-expressing the clamp via prompt_token_budget: a num_predict past the default shrinks the budget, and when the windowed prompt then does not fit, it falls back to the default budget (wider) instead of being rejected. Removed the branch's now-deleted _DEFAULT_GENERATION_RESERVE/_CONTEXT_WINDOW_MARGIN constants, corrected base.py's 'enforces and rejects' comment to note the clamp, and dropped a merge-duplicated probe-failure test in favor of main's canonical #540 pair.
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.
Problem
The TUI manages conversation context before every turn: it windows history to a token budget and, with chat_compaction on, folds the overflow into a rolling summary. The HTTP surface had none of this. History went to the model verbatim, so long conversations eventually overflowed the context window, and any HTTP client wanting compaction would have had to rebuild it from scratch.
What changed
/api/chat and /api/chat/stream now window incoming history to the same budget the TUI uses, and run the same compaction when the toggle is on and the conversation is over the trigger.
ChatRequest gains two optional fields: summary, the carry-forward notes folded into the prompt, and session_id, the session that receives a fresh summary when a turn compacts. A deleted session is tolerated, matching the TUI.
How clients see it
Streaming turns announce the work: a compacting event fires before the condensing model calls start (same idea as warming/warm), then a compaction event carries the new summary plus how many turns were condensed and how many were dropped without notes. The non-stream route reports the same result on a new optional compaction field of the response.
Notes
The history budget fraction moved out of the TUI screen into the shared compaction module, so both surfaces derive it from one place. With compaction off nothing new is emitted; the only behaviour change is that history is now windowed instead of overflowing.