You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(runtime): persist contextRemaining on the stored TokenUsageMessage so TUI ctx survives transcript rebuilds #4019
The TUI status line permanently degrades to ctx ?/<window> (#3371 fallback) even for providers that report per-step usage, because the storedTokenUsageMessage never carries contextRemaining.
In packages/runtime/src/ai-sdk-backend.ts the turn-end usage write has two asymmetric paths:
The durable TokenUsageMessage written via appendMessage(tu) does not — the field is absent from the object literal.
The TUI rebuilds usage from stored messages in replaceTranscriptWithStoredMessages (packages/cli/src/pi-transcript.ts), so after any transcript reload (abort refresh, session resume, reconnect) contextRemaining is washed back to undefined and the status line falls back to ctx ?/<window> — even though the runtime event ledger (runtime_events.actions.tokenUsage) proves the value was computed (e.g. contextRemaining: 230068 for a 262144-window model).
cacheHit/cacheMiss survive the same rebuild (they are persisted), which is why cache NN% stays correct while ctx reverts to ?.
Expected
The persisted TokenUsageMessage carries contextRemaining (same conditional-spread value as the live event), so the ctx segment survives transcript rebuilds.
Scope
Move contextRemainingForUsage computation above the stored tu literal and add the field to it (schema already allows it: packages/core/src/usage-record-schema.ts, packages/core/src/session.ts; replay/read-model paths already preserve it: runtime-event-backfill.ts, runtime-event-read-model.ts).
Extend the ai-sdk-backend usage tests to assert the stored message includes contextRemaining.
Field report consistent with this issue (posted via AI agent on behalf of the reporter):
Surface: TUI, model k3-256k (context window 262,144), thinking:high
Session shape: single turn ran >1h10m with many Bash tool calls (local CI run + polling); user steering messages and interrupts occurred during the turn, so a transcript rebuild (abort refresh) likely happened mid-session
If this provider does report per-step usage, this session is a real-world repro of the wash-out described here: after a transcript rebuild the durable TokenUsageMessage lacks contextRemaining, the status line falls back to ?/<window>, and the user loses exactly the visibility that would have warned them before the budget verdict. (If the provider does not report per-step usage, the ? is the expected #3371 degrade and orthogonal to this fix — the reporter believes usage is reported.)
Activity
me2seeks commentedon Aug 27, 2026
I'm working on this.
me2seeks commentedon Aug 27, 2026
Field report consistent with this issue (posted via AI agent on behalf of the reporter):
k3-256k(context window 262,144),thinking:highctx ?/262k(feat(cli): status line should degrade gracefully when ctx/token usage is unavailable #3371 degrade) for the whole observed session — no usage percentage was ever visibleError: Turn failed: context_budget_exhaustedwith no prior visible warning (the >80% yellow / >95% red cues from fix(cli): statusline density, color fallback, and activity-strip spacing #1064 never had a chance to show)If this provider does report per-step usage, this session is a real-world repro of the wash-out described here: after a transcript rebuild the durable
TokenUsageMessagelackscontextRemaining, the status line falls back to?/<window>, and the user loses exactly the visibility that would have warned them before the budget verdict. (If the provider does not report per-step usage, the?is the expected #3371 degrade and orthogonal to this fix — the reporter believes usage is reported.)fix(runtime): persist contextRemaining on the stored TokenUsageMessage (