fix(chat): stop the context-usage percentage flickering during streaming - #122
Merged
Merged
Conversation
The indicator picked its context snapshot from the last assistant message carrying a `usage` object. A half-written streaming entry lands with an empty usage object, so the percentage dropped to 0% (or the capsule hid) until the next reload replaced it. - collectContextUsage now skips assistant messages whose usage totals zero, staying on the last message that actually measured the context. - updateContextUsage takes the snapshot it last rendered and returns the new one: a transient zero reuses the previous value, so once a value is on screen the capsule never flickers to 0% or hides. An empty model label likewise reuses the previous limit instead of the 128k default. - The controller holds the first paint until /api/models settles (and paints anyway if that fetch fails) so the denominator does not jump from the heuristic fallback to the registry value. Known limitation: the model label is not pinned the same way, so the first paint can still adjust once when worker status reports the model. Non-zero regressions are left alone on purpose — compaction legitimately drops the context. Fixes #110
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.
Fixes #110.
Problem
The context-usage capsule derives its snapshot from the last assistant message carrying a
usageobject. During a streamed turn a half-written assistant entry lands with an empty usage object, socontextTokensfell to 0 — the percentage jumped to 0% (or the capsule hid entirely) until the next reload put a real value back.Changes
collectContextUsageskips assistant messages whose usage totals zero when scanning backwards, staying on the last message that actually measured the context.updateContextUsagetakes the snapshot it last rendered (previous) and returns the new one. A transient zero reuses the previous value, so once a value is on screen the capsule never flickers to 0% or hides. An empty model label likewise reuses the previous limit instead of falling back to the 128k default.createContextUsageControllerholds the first paint until/api/modelssettles — and paints anyway if that fetch fails — so the denominator does not visibly jump from the heuristic fallback to the registry value.Tests
context-usage.test.jsgains regression coverage: incremental streaming entries without usage don't move or hide the indicator; the limit survives a momentarily unknown model label; the first paint waits for the registry; and it still paints when the registry fetch rejects.make checkpasses.Known limitations
/api/modelssettles before worker status reports the model, the first paint uses the 128k default and adjusts once when the label arrives.