fix: context readout collapses to the KV-cache delta, then jumps ~4x when the task selector moves - #318
Merged
Conversation
…cluded llama-server's prompt_n / tokens_evaluated count only the tokens evaluated on this request; the prefix reused from the KV cache sits in tokens_cached and was dropped. Every consumer treats promptTokens as "how big was the prompt" (their fallback is prompt.tokens.total, and the TUI overwrites the context readout with it after each completion), so on a warm cache the occupied-context figure collapsed to the newly-evaluated slice — and then leapt ~4x back to the estimator's full figure the moment the context panel's task selector reprojected it. Report evaluated + cached from the llama client so the measured readout and the selector projection agree. Cloud adapters already include cached tokens in their prompt counts and are untouched.
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.
The bug
With a local llama-server model on a warm KV cache, the composer's context chip shows a fraction of the real occupied context. Open the context panel and move the task selector to any value other than the current cap (e.g. 22 while only 3–4 tasks exist) and the occupied figure suddenly leaps ~4x — even though the extra tasks don't exist and nothing about the prompt changed.
Root cause
After every completion the TUI overwrites
contextUsage.tokenswithtiming.promptTokens(llm_completedinagent-event-reducer.ts). For llama-server that value comes fromtimings.prompt_n/tokens_evaluated— which counts only the tokens evaluated on this request. The prefix reused from the KV cache is reported separately astokens_cachedand was dropped. So the "measured" readout was the cache delta, not the prompt.The task selector's projection (
usageAtPairs) recomputes the total from the full-prompt estimator sections. The moment the selector leaves the measured cap, the panel switches from the understated figure to the honest one — that's the "×4 jump". The projection itself is correct (it clamps to the tasks that actually exist); the measurement it was compared against was wrong.Fix
normaliseCompletionResponseinllama-server-client.tsnow reportsprompt_n + tokens_cachedastiming.promptTokens. That matches what every consumer already assumes — each one falls back toprompt.tokens.total(a full-prompt figure), and the TUI shows it as occupied context. Cloud adapters (OpenAI-compatible, claude/codex CLI) already include cached tokens in their prompt counts and are untouched;cacheHitTokensis unchanged.Tests
prompt_n: 40+tokens_cached: 30→promptTokens: 70tokens_cached→promptTokens: 40,cacheHitTokens: 0npx vitest run src/llm(56 files, 597 tests) andtsc --noEmitclean