Skip to content

Prevent shared preview loads from evicting the active Studio model - #7104

Merged
Etherll merged 73 commits into
unslothai:mainfrom
NilayYadav:fix-preview-model-eviction
Aug 25, 2026
Merged

Etherll merged 73 commits into
unslothai:mainfrom
NilayYadav:fix-preview-model-eviction

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Shared preview links (/p/...) share the single model slot with Studio. Previously, opening a preview link would evict the currently loaded Studio model and try to restore it later, which could silently swap models mid-conversation in Studio chat.

Now:

  • A preview load only proceeds if the slot is empty or already has the preview's checkpoint loaded. If Studio has a different model loaded, the preview returns a 503 instead of evicting it.
  • Removed the displaced-model restore machinery since previews no longer displace anything.
  • The preview page shows a friendly message on 503 ("Studio is currently using another model...") instead of a raw error.
  • Preview responses disable thinking/reasoning output, since some small reasoning models emit their entire reply as hidden reasoning content, making the shared preview appear blank.

@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: ffe70a6e55

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/preview.py Resolved

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a model-slot guard mechanism to prevent preview models from overriding or interrupting active models in Studio. Specifically, it adds checks to refuse loading a preview model if a different model is already loaded or if the preview is busy, returning a 503 status code. The frontend preview page has been updated to handle these 503 errors gracefully with a user-friendly message. Additionally, new thinking-related fields are sanitized in the preview payload, and comprehensive unit tests have been added to verify the new guard behavior. I have no further feedback to provide as the implementation is clean and well-tested.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@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: 9d8fd8a99b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/preview.py Resolved

@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: 18f03796ab

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
@danielhanchen

Copy link
Copy Markdown
Member

@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: 110098c7f8

ℹ️ 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 thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/assets/preview_page.html Resolved
- Re-apply the sidecar-install guard on preview loads (409 while a transformers
  install holds the swap), which load_model_for_preview bypassed by calling
  _load_model_impl directly.
- Clear preview ownership when an already-serving auto-switch adopts the model,
  so a later preview cannot swap it out from under an active OpenAI caller.
- Untrack no-touch preview refusals so public /p traffic cannot keep an idle
  Studio model warm; make untrack_current_request balance the preview counter.
- Render structured FastAPI error bodies in the preview page instead of
  [object Object].
@danielhanchen

Copy link
Copy Markdown
Member

@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: c5186ad2f4

ℹ️ 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 thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
- Borrow a resident same-checkpoint model in load_model_for_preview instead of
  reloading it, so opening a preview of an idle Studio-owned GGUF can no longer
  reconfigure or restart it when its live settings differ from the bare request
  (unslothai#5401). Busy Studio traffic still blocks first.
- Clear preview ownership in the native generate_stream and generate_audio paths,
  so a model first loaded by a shared preview is claimed for Studio the moment
  Studio generates against it and a later preview cannot swap it out between turns.
@danielhanchen

Copy link
Copy Markdown
Member

@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: 7d77b8cd22

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
- Add _claim_slot_for_non_preview: clear the preview marker only for non-preview
  local generation, decided by the request path. An audio preview reaches
  generate_audio via openai_chat_completions, so the earlier unconditional claim
  there wrongly un-marked a preview-owned model.
- openai_chat_completions now claims the resident model on the local path, so a
  non-preview turn with auto-switch off or the model omitted (which
  _maybe_auto_switch_model returns early on) no longer leaves it preview-owned.
- generate_stream and generate_audio route their claim through the gated helper.
@danielhanchen

Copy link
Copy Markdown
Member

@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: 21673d0875

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
…d Studio traffic

Move the non-preview slot claim out of openai_chat_completions and into
_maybe_auto_switch_model, at every exit that adopts the resident model
(omitted/default model, auto-switch off, unknown-name-with-resident,
already-serving). This keeps the preview marker intact when the pre-load
vision guard rejects an image/audio request naming a text-only GGUF, and
covers all local inference routes (/v1/completions, /v1/embeddings,
/v1/responses, /v1/messages) that funnel through auto-switch, not just chat.

Add a _preview_pending subset counter to llama_keepwarm and
other_non_preview_pending_count(); the preview busy guard now also refuses
when a non-preview Studio request is queued on the lifecycle gate, so a
second preview cannot swap the model out before that queued request starts.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@mahiatlinux

Copy link
Copy Markdown
Contributor

Before

At merge base f11dc5ac, a public preview could return 200 and call the target load while Studio already owned a different model slot. The UI rendered the 503 response as raw HTTP and JSON text.

After

At repaired head c5563b4a3, the same reproducer returns 503 with zero model loads. The ownership-claim race and GPU-owner race also return 503 without replacing the Studio model or evicting diffusion/video work.

Verdict

The reported defect and both late review races were confirmed. The repaired branch is merged with current main, locally verified, and ready for CI review.

Regression risk

Low. The arbiter keeps its existing evicting default for all current callers. Preview loads alone request non-evicting acquisition. Slot ownership changes only after a clean non-preview completion or after all load refusal and drain points have passed.

Compatibility

No public request or response schema changed. Existing acquire_for() callers retain allow_evict=True. Focused checks passed on macOS arm64 with Python 3.12: canonical formatter clean, ruff clean, and 819 tests passed.

UI evidence

Preview busy error before and after

The capture used f11dc5ac before and 9f0c08d00 after. The relevant preview_page.html blobs are byte-identical to current main 5205a38f7 and final head c5563b4a3, respectively. Before shows the raw 503 JSON. After shows the same server refusal in a clear error card. Chromium and WebKit verified the behavior.

Fixes

  • Refuses preview replacement of a Studio-owned slot.
  • Makes the GPU ownership check atomic and non-evicting.
  • Closes the admitted-request and late Studio-claim races.
  • Preserves preview ownership across sidecar, cancellation, drain, teardown, and load failures.
  • Prevents failed or cancelled streams from claiming preview ownership.
  • Preserves current main load cancellation, modality, timing, media keep-warm, and MLX behavior.
  • Resolves all 83 Codex review threads after accepted fixes.

Gaps

No usable CUDA GPU was present, so GPU training was not run. Firefox Playwright launch hung on this macOS host. At posting time, the new CI run had 40 queued checks, 4 skipped checks, and 1 successful check.

@mahiatlinux

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c5563b4a31

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

@Etherll Etherll self-assigned this Aug 25, 2026
@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@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: 7538c98323

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

and not path.endswith("count_tokens")
# Image/video generation runs on the diffusion/video engine, not the llama slot.
and not path.endswith(_NON_LLM_SLOT_SUFFIXES)
and not scope.get(_RESPONSE_FAILED_SCOPE_KEY)

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 Exclude cancelled non-tool JSON completions from slot claims

Fresh evidence beyond the previously fixed tool drains: the ordinary GGUF JSON drain (routes/inference.py:17821-17941), standard safetensors JSON drain (19013-19218), and audio-input JSON drain (16068-16135) can all observe an explicit /inference/cancel, return a partial HTTP 200 response, and never set _RESPONSE_FAILED_SCOPE_KEY. When a preview-owned model is resident, this branch therefore treats those cancelled requests as clean Studio completions and clears preview ownership, causing a later preview for another checkpoint to be refused; mark these remaining cancellation returns failed before emitting their JSON responses.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fixed in 22270b1. Cancelled audio-input, GGUF, and safetensors JSON drains now mark the response failed before returning a partial 200 body; the regression test verifies all three call sites preserve preview ownership through the middleware.

@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: 224de11a72

ℹ️ 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 thread studio/backend/routes/inference.py Outdated
# was blocked on the gate through this load then sees the counter
# advance and is rejected by the middleware rather than running
# against the swapped-in preview checkpoint.
note_preview_swap()

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 Skip the swap bump when no model state changed

When the atomic GPU acquisition loses a race to an image/video load, acquire_for(..., allow_evict=False) raises GpuOwnerBusyError before evicting, registering, or changing ownership, but this finally still advances the preview-swap generation. A Studio request that entered before this failed preview and reaches local admission afterward is therefore rejected with a retryable 503 even though the chat slot was never touched; repeated requests to such a preview can unnecessarily disrupt Studio traffic. Only record a swap when the resident slot actually changed, or explicitly skip the bump for pre-transfer failures such as GpuOwnerBusyError.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fixed in 07a8547. The preview swap generation now advances only after a successful load or an observed resident-identifier change; atomic GPU-owner refusal leaves it unchanged.

# (which would reconfigure/restart an idle Studio-owned GGUF,
# #5401). Ownership is unchanged: Studio's model stays Studio's, a
# preview-owned one stays preview-owned.
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.

P1 Badge Restore Studio's adapter state after a same-target preview

When Studio owns this same LoRA checkpoint with adapters disabled for base-model comparison, this borrow path skips reloading but the preview sanitizer still forces use_adapter=True. The safetensors generation path calls _apply_adapter_state, which enables the shared PEFT adapter and does not restore it, so after the public preview finishes, subsequent Studio turns that omit use_adapter silently run the adapter instead of the base model. Preserve and restore the resident adapter state around a borrowed Studio model, or avoid borrowing mutable LoRA state.

Useful? React with 👍 / 👎.

@Etherll Etherll Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Fixed in 07a8547. Studio-owned LoRA checkpoints are no longer borrowed by public previews, so the forced adapter-on preview cannot mutate Studio's adapter state.

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 224de11a72

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

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 07a8547077

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

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

I finished the end-to-end pass on current head 07a85470777e6cc252f78c873d93a7d682471599.

I fixed the preview/Studio ownership races found during review, including the queued preview/Studio switch deadlock, incorrect count_tokens ownership claims, cancelled JSON responses being treated as successful, same-target Studio-owned LoRA adapter mutation, and the unnecessary swap-generation bump when an atomic acquisition refused to change the resident model. I also added focused regression coverage for each path.

Validation on the final head:

  • Focused ownership/race tests: 6 passed.
  • Broad changed-backend suite on Windows: 859 passed, 2 deselected. These are existing fixture assumptions incompatible with NTFS/case-insensitive filesystems, not product failures.
  • Both required security scans: 0 findings.
  • Final-head CI: 45/46 checks pass, including all Core pinned/default/latest Transformers+TRL lanes, Windows, Linux, macOS Studio, UI, API, GGUF, lint, wheel, startup, and studiobench workflows.
  • Codex reviewed exact head 07a8547077, found no major issues, and the PR now has the codex-converged# label.

The one failing check is the broader Repo tests (CPU) job. It completed 14,059 passing tests, then failed the source-encoding policy test because unchanged file tests/studio/playwright_declared_polls.py:210 calls write_text() without encoding="utf-8". That file is outside this PR's diff, so I did not patch unrelated code.

Chrome before/after evidence on 224de11 passed. The fixed preview shows the readable 503 detail, restores the prompt, and leaves Send enabled. The remaining 224de11..07a8547 delta touches only backend ownership logic and tests, not UI code. Two attempts to rebuild an exact-07a8547 Studio for a fresh screenshot were blocked by host OpenBLAS/Node memory allocation failures, so I am not presenting the earlier screenshot as exact-final-SHA browser evidence.

There are no unresolved in-scope code findings. The PR remains open, MERGEABLE, and GitHub reports BLOCKED / REVIEW_REQUIRED because the repository-level gate is still failing. No merge was attempted.

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator
pr7104_before_after

@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@Etherll

Etherll commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a70db7c081

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

@Etherll Etherll left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

@Etherll
Etherll merged commit 58c721d into unslothai:main Aug 25, 2026
45 of 46 checks passed
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.

4 participants