Skip to content

Studio: show context usage for a revisited chat under a cloud model or none - #13225

Merged
danielhanchen merged 6 commits into
mainfrom
studio-cloud-chat-usage-estimate
Oct 10, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
studio-cloud-chat-usage-estimate

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

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 refreshContextUsage returns 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 tokens estimate from #9475.

ThreadContextUsageRecount now 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:

  • the chat's own saved usage if the selected model produced it (modelId matches);
  • otherwise the text estimate (~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 with runtime-provider.tsx reverted 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 typecheck clean; 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.
  • Live, three isolated Studios (v0.1.806-beta, main, this branch), unsloth/Qwen3-0.6B-GGUF Q4_K_M, a llama_cpp provider 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.
Step main this branch
open A / open B (local GGUF) 165/41.0k / 205/41.0k 254/41.0k / 255/41.0k
pick cloud model, on B no bar ~260 tokens
back to A no bar ~229 tokens
B again no bar ~260 tokens
chat answered by the cloud model 159 tokens 270 tokens (its own saved usage)
eject, back to A / B no bar ~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.

before after

@danielhanchen danielhanchen self-assigned this Oct 10, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T14:18:12.049449Z bccdaf9 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

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".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: bccdaf95df

ℹ️ 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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Tokens per chat are not being counted Again

1 participant