Repository navigation
Studio: show context usage for a revisited chat under a cloud model or none - #13225
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b1bf7bae15
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| }; | ||
| if (exactCountFollows() || useChatRuntimeStore.getState().contextUsage != null) return; | ||
| const threadId = activeThreadId; | ||
| void listStoredChatMessages(threadId) |
There was a problem hiding this comment.
Use the live branch when refilling temporary chats
When a user switches to a cloud model or ejects the model while an active temporary/incognito chat already contains messages, this read always returns an empty array because readStoredChatMessages deliberately short-circuits incognito thread IDs. The subsequent estimate is therefore null, leaving the context bar blank—the exact regression this effect is intended to fix. Use the mounted branch exposed by ActiveBranchRegistrar (or another live-thread fallback) before relying on persisted records.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not introduced here: temporary chats are never read from storage by any path, and on main the history loader gives them no estimate either, so their bar is blank under a cloud model before and after this PR. Out of scope for this fix.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 82408a34c1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| }; | ||
| if (exactCountFollows() || useChatRuntimeStore.getState().contextUsage != null) return; | ||
| const threadId = activeThreadId; | ||
| void listStoredChatMessages(threadId) |
There was a problem hiding this comment.
Validate the live branch before publishing the refill
When a persisted chat changes while this storage read is in flight—for example, the user deletes an exchange immediately after switching to a cloud model, or the final assistant append is still pending—records can describe the old branch. The continuation only rechecks the thread, run, and loading state before installing that stale count; once contextUsage becomes non-null, neither the completed deletion nor append retriggers this effect, so the bar remains wrong. Fresh evidence beyond the rejected incognito case is that ordinary persistent message mutations are asynchronous and do not clear or rerun this effect; compare the captured records with the mounted branch, as refreshContextUsage does, or retry after pending storage writes settle.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not fixing: it needs a message deleted within the few milliseconds of one storage read right after a model change, and the worst case is a slightly off ~estimate, not a wrong request or lost data.
| store.setThreadContextUsage(threadId, usage); | ||
| store.setContextUsage(usage); | ||
| }) | ||
| .catch(() => undefined); |
There was a problem hiding this comment.
Retry the refill after a transient storage failure
When listStoredChatMessages rejects—for example, because the backend is temporarily unavailable and this modern chat has no legacy browser copy—the catch silently consumes the refill's only attempt. Storage recovery does not change any dependency of this effect, so contextUsage remains null and the already-mounted chat keeps a blank bar until the user switches threads or models again. Schedule a bounded retry or retrigger the refill when chat storage reports a successful update.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not fixing: with the chat store unreachable the bar stays blank exactly as on main, and the chat itself cannot load or save in that state; a retry loop here would be hardening for an outage, not a fix.
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a93d4ffacb
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const shown = useChatRuntimeStore.getState().contextUsage; | ||
| return shown != null && !shown.estimated; | ||
| }; | ||
| if (exactCountFollows() || shownIsExact()) return; |
There was a problem hiding this comment.
Reprice restored usage after an usage-less follow-up
When this effect restores a matching saved cloud count, a subsequent cancelled or failed run that adds a new turn but reports no usage leaves that restored count in contextUsage; when runActive becomes false, this guard treats the old count as exact and never reads the updated branch, so the bar and per-thread cache remain permanently stale. Fresh evidence beyond the previously covered estimated-value case is that the new restore path itself installs such an exact count before the follow-up run; track whether the exact value was produced by the just-finished run, or invalidate/re-estimate older exact values when an usage-less run ends.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not changing: a restored or completion-written count is treated as exact on every path (the local recount skips it too, 'only into a blank or estimated bar'), so after a failed run the bar keeps the last real count as it does on main; this refill follows that rule rather than adding a cloud-only exception.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Fixes #10337
Picking a cloud model, or ejecting the local one, clears every chat's saved context usage. A chat that is already open in the page never reruns the history loader that fills the bar, and with no local model
refreshContextUsagereturns early. So switching back to a chat you already visited showed no token count at all until you sent a new message. Only chats opened for the first time after the switch got the~N tokensestimate from #9475.ThreadContextUsageRecountnow has one more effect. When the bar is blank (or only an estimate) and no exact count can follow (cloud model, or no model selected / no window), it fills the bar from the branch the mounted runtime is showing, the same way the history loader does from storage:modelIdmatches);~N tokens).It reads no storage: the chat is mounted, so its branch is already in memory (a storage read 404s for a just-sent chat and showed up as a console error in the IME smoke). It waits while a run or a model load is in flight and re-fires when either settles, and it only prices the runtime's main thread when that is the thread the bar points at. An exact count is never replaced; an estimate is, so a run that ends without usage re-prices the new turn. Local GGUF behaviour is unchanged: when a local model with a known window is selected, this effect does nothing and the existing exact recount runs.
Tests
python -m pytest tests/studio/test_new_chat_context_recount.py -q: 75 passed. 10 of the 11 new cases fail withruntime-provider.tsxreverted to main (the 11th is the negative control that another mounted thread is never priced).tests/studio/playwright_chat_ime_i18n.py(the Studio UI CI IME smoke) against a build of this branch: 0 console errors, twice.npm run typecheckclean;node --experimental-strip-types --test tests/estimate-chat-tokens.test.ts tests/context-window-visible.test.ts tests/context-usage-compact.test.ts tests/current-date-context-usage.test.ts: 27 passed.unsloth/Qwen3-0.6B-GGUFQ4_K_M, allama_cppprovider connection as the cloud model. One page session: open chat A, open chat B, pick the cloud model (or eject), go back to A, then B.165/41.0k/205/41.0k254/41.0k/255/41.0k~260 tokens~229 tokens~260 tokens159 tokens270 tokens(its own saved usage)~144 tokens/~267 tokens(rerun on the final commit)The absolute counts differ between installs because each has its own seeded chats; compare blank against filled.
Before / after
Chat opened earlier in the same page session, after picking a cloud model (top pair) and after ejecting the model (bottom pair). main shows no token count; this branch shows the estimate.