Skip to content

Studio: keep chat context when switching models - #16493

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:chat-cache-lost-model-switches
Nov 4, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
NilayYadav:chat-cache-lost-model-switches

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Switching to another chat model and back threw away everything the first model had already read, so the next message in a long chat reprocessed the whole history. A 23,000 token chat took 20 seconds before the first word on a T4, and a 100,000 token chat on slower hardware takes around 11 minutes. The cache was only saved on idle unloads and when an image or video model took the GPU.

Unloading a GGUF model from the model picker now saves its chat cache first, under the existing "Keep chat context across unloads" setting and the same 10 GB limit. Loading that model again restores the cache, so the next message only reads what is new. The restore keeps the existing checks, so a changed file, context length or other load setting starts fresh. The save adds a few seconds to the switch, holds other model loads while it runs, and stops after 45 seconds. It is skipped while another request is running or when the chat belongs to another account.

Turning the setting off deletes these saves. Restarting Studio still starts with an empty cache. MLX models, the /v1 auto-switch, a forced /load without an unload and a switch while a reply is generating are not covered by this change, so those still reprocess the history.

Refs #9037

Before After
Setting description before after
Next message after A, unload, B, unload, A. The replies differed, so prompt totals differ before after

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-11-03T23:34:02.360794Z e093bbd 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.

@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: 1e7a79bf22

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

Comment on lines +612 to +613
while len(_unload_kv) > 1 and sum(map(_resume_bytes, _unload_kv)) > _SLOT_SAVE_MAX_BYTES:
stale.append(_unload_kv.pop(0))

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 Include idle and swap saves in the retained-cache disk budget

With keep-context and idle unloading enabled, manually unloading model A and then idle-unloading model B leaves saves in both _unload_kv and _kv_resume. This eviction loop only counts _unload_kv, while idle_unload_loop retains B's independently capped save through _set_kv_resume. Two 6 GiB saves therefore remain on disk despite the advertised 10 GB limit, and retained storage can approach twice the configured cap. Enforce a shared budget whenever either cache changes.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not changing this one. Each save already has its own 10 GB cap and a free space check before writing, and leftovers are swept on startup, so the worst case is extra disk use, not a failed or wrong restore.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changed my mind on this one, fixed in e093bbd. Idle and media swap saves now share the same 10 GB budget with the unload saves, oldest goes first.

Comment on lines +389 to +390
abort.set()
future.cancel()

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 Delete completed save results before cancelling the future

When a save completes at the timeout boundary, _deliver can set the shielded future's result before wait_for raises TimeoutError. Cancelling that already-completed future does not delete its manifest, and the worker no longer owns cleanup, leaving the cache files untracked on disk. These files evade both budget eviction and purge_kv_resume() when the setting is disabled. Check and delete a completed successful result before cancelling, as the existing save_chat_kv_before_swap handler does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Leaving this as is. It needs the save to land in the same loop iteration as the 45s timeout, and the only cost is one orphaned slot file that the startup sweep deletes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in e093bbd. A result that lands at the timeout or a cancel is deleted before cancelling, same as the swap save.

@NilayYadav NilayYadav self-assigned this Nov 3, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator 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: 1912d720f8

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

Comment on lines +369 to +370
if _chat_account() != _account_id() or _chat_busy():
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 Check resident ownership for CPU-only unload saves

For a primary GGUF loaded in Manual mode with gpu_layers=0 and no other kept chat model, _load_model_impl deliberately releases its CHAT claim through _release_chat_for_zero_vram_primary (routes/inference.py:19544-19546). _chat_account() then returns None, whereas _account_id() is still an account ID such as "owner", so this guard skips saving even for the caller's idle model with keep-context enabled. These CPU-only chats therefore still reprocess their whole history after every manual switch. Check chat-resident ownership, or provide a safe zero-VRAM ownership fallback, rather than requiring a GPU claim.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Keeping the ownership guard. A gpu_layers=0 primary just falls back to what main does today and reprocesses the history, nothing breaks, and the media swap save uses the same check. CPU-only loads can be a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed in e093bbd. When no CHAT claim is held, a zero VRAM primary is checked against the account that loaded it, so CPU-only chats get saved too.

@LeoBorcherding

Copy link
Copy Markdown
Collaborator

I cancelled the CI runs on this PR for now, since Codex hasn't converged at the current head yet: Codex still has open findings on the head commit. CI here runs on the team's shared runner pool, and it's backed up right now (380+ runs queued), so runs on code that's still changing push everyone else's back.

Once Codex says "Didn't find any major issues" on the head commit, one push or a re-run brings CI back. A small ask going forward: hold the push until Codex is clean, and batch follow-up fixes into a single push. Thanks!

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: e093bbdc0d

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants