Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
fix(studio): poll inference status while CLI model is loading (#5901)
James Dawdy56b9ca4ad81d
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 276c4f25315..880db4f9175 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -6898,6 +6898,23 @@ def _llama_public_model_id(llama_backend, fallback: Optional[str] = None) -> Opt)+def _loading_public_id(model_path: Optional[str]) -> Optional[str]:+ """The id ``/api/inference/status`` publishes for a load still in flight.++ The request carries whatever the client sent, which for an on-device model is an+ absolute path. What the same load reports once it lands is path-free (see+ ``_llama_status_model_ids`` and ``core.inference.model_ids.public_model_id``), so+ reporting it mid-load must be too: a registered native-lease label if the grant has+ been redeemed, else the clean public id.+ """+ if not model_path:+ return model_path+ label = display_label_for_native_path(model_path)+ if label != model_path:+ return label+ return public_model_id(model_path) or model_path++def _orchestrator_public_model_id(backend) -> Optional[str]:"""The id to report for the model loaded in the inference orchestrator(safetensors, MLX): the advertised repo id from an auto-switch load, else the@@ -12925,6 +12942,7 @@ _scoped_load_attempts_lock = threading.Lock()_scoped_load_attempts: dict[tuple[str, str], _ScopedLoadAttempt] = {}_scoped_load_cancel_tombstones: dict[tuple[str, str], tuple[str, float]] = {}_running_load_attempt: Optional[_ScopedLoadAttempt] = None+_pending_load_attempts: dict[str, _ScopedLoadAttempt] = {}_SCOPED_LOAD_CANCEL_TOMBSTONE_TTL_S = 60.0# Bound on waiting for a cancel's teardown to report back. Only the /unload# handler sets cancel_complete for a running attempt, so a disconnect or a@@ -13107,6 +13125,8 @@ async def load_model_gated(from core.inference.llama_keepwarm import inference_lifecycle_gateattempt = _begin_load_attempt(request, current_subject)+ with _scoped_load_attempts_lock:+ _pending_load_attempts[attempt.token] = attempttry:_raise_if_sidecar_swap_in_progress()# Hold the lifecycle gate across the load so idle auto-unload can't unload the@@ -13135,6 +13155,8 @@ async def load_model_gated(get_llama_cpp_backend()._loaded_by_user_action = user_initiatedreturn responsefinally:+ with _scoped_load_attempts_lock:+ _pending_load_attempts.pop(attempt.token, None)_finish_load_attempt(attempt)@@ -16136,6 +16158,18 @@ async def get_status(current_subject: str = Depends(get_current_subject)):_installed_tag = _freshness.get("installed_tag")_latest_tag = _freshness.get("latest_tag")+ with _scoped_load_attempts_lock:+ _tracked_loading_id = (+ _running_load_attempt.model_path if _running_load_attempt is not None else ""+ )+ if not _tracked_loading_id:+ _queued = next(iter(_pending_load_attempts.values()), None)+ _tracked_loading_id = _queued.model_path if _queued is not None else ""+ # The attempt holds what the client sent, which for an on-device model is a path.+ _tracked_loading_id = _loading_public_id(_tracked_loading_id) or ""+ _loading = [_tracked_loading_id] if _tracked_loading_id else []+ backend = _peek_inference_backend()+# If a GGUF model is loaded via llama-server, report thatif llama_backend.is_loaded:_model_id = llama_backend.model_identifier@@ -16173,7 +16207,7 @@ async def get_status(current_subject: str = Depends(get_current_subject)):llama_backend, _native_grant_backed, _model_id),gguf_variant = llama_backend.hf_variant,- loading = [],+ loading = _loading,# Plus anything the Unsloth registry still holds: the GGUF load# only unloaded the ACTIVE one, so a model cached behind it is# still in VRAM and was invisible to every client reading this.@@ -16202,9 +16236,9 @@ async def get_status(current_subject: str = Depends(get_current_subject)):# Otherwise report Unsloth backend status. Peek rather than build: no singleton means# nothing is loaded, and the chat UI polls this from first paint.- backend = _peek_inference_backend()if backend is None:return InferenceStatusResponse(+ loading = _loading,llama_cpp_supports_mtp = _supports_mtp,llama_cpp_prebuilt_stale = _stale,llama_cpp_installed_tag = _installed_tag,@@ -16233,6 +16267,14 @@ async def get_status(current_subject: str = Depends(get_current_subject)):load_inference_config(backend.active_model_name) if backend.active_model_name else None)+ # The backend and the attempt registry name the same load, so compare public ids+ # or a model loaded from a path is listed twice.+ _loading_models = list(getattr(backend, "loading_models", set()))+ if _tracked_loading_id and not any(+ _loading_public_id(_name) == _tracked_loading_id for _name in _loading_models+ ):+ _loading_models.append(_tracked_loading_id)+return InferenceStatusResponse(active_model = backend.active_model_name,model_identifier = backend.active_model_name,@@ -16252,7 +16294,7 @@ async def get_status(current_subject: str = Depends(get_current_subject)):mlx_kv_quant_note = model_info.get("mlx_kv_quant_note"),chat_template_override = model_info.get("chat_template_override_requested"),chat_template_override_reason = model_info.get("chat_template_override_reason"),- loading = list(getattr(backend, "loading_models", set())),+ loading = _loading_models,loaded = list(backend.models.keys()),inference = inference_config,requires_trust_remote_code = _resolve_loaded_trust_remote_code(diff --git a/studio/backend/tests/test_inference_status_route.py b/studio/backend/tests/test_inference_status_route.pyindex 90eb75d9b3b..126e6443e8d 100644--- a/studio/backend/tests/test_inference_status_route.py+++ b/studio/backend/tests/test_inference_status_route.py@@ -8,9 +8,12 @@ from __future__ import annotationsimport asyncioimport sysimport threading+from contextlib import asynccontextmanagerfrom concurrent.futures import ThreadPoolExecutorfrom pathlib import Path+import pytest+_BACKEND = Path(__file__).resolve().parents[1]if str(_BACKEND) not in sys.path:sys.path.insert(0, str(_BACKEND))@@ -156,3 +159,156 @@ def test_overlapping_status_probes_leave_default_executor_for_streaming(monkeypaassert started, "the status probe never ran"assert token == "token"assert len(responses) == 2+++def _attempt(model_path: str):+ return inference_route._ScopedLoadAttempt(+ token = "attempt",+ request_id = None,+ model_path = model_path,+ subject = "test",+ cancel_event = threading.Event(),+ cancel_complete = threading.Event(),+ )+++def _patch_fast_status(monkeypatch, backend = None):+ _patch_status_dependencies(monkeypatch)+ monkeypatch.setattr(+ inference_route,+ "_probe_llama_cpp_status",+ lambda _backend: (False, {}),+ )+ monkeypatch.setattr(inference_route, "_peek_inference_backend", lambda: backend)+ monkeypatch.setattr(inference_route, "_running_load_attempt", None)+ monkeypatch.setattr(inference_route, "_pending_load_attempts", {})+++def test_status_reports_a_load_queued_on_the_lifecycle_gate(monkeypatch):+ _patch_fast_status(monkeypatch)+ monkeypatch.setattr(+ inference_route,+ "_pending_load_attempts",+ {"attempt": _attempt("org/slow-model-GGUF")},+ )++ response = asyncio.run(inference_route.get_status(current_subject = "test"))++ assert response.active_model is None+ assert response.loading == ["org/slow-model-GGUF"]+++def test_status_keeps_the_resident_model_visible_during_a_load(monkeypatch):+ backend = _FakeInferenceBackend()+ backend.active_model_name = "org/resident-model"+ backend.models = {"org/resident-model": {}}+ backend.loading_models = set()+ _patch_fast_status(monkeypatch, backend)+ monkeypatch.setattr(inference_route, "load_inference_config", lambda _model: None)+ monkeypatch.setattr(+ inference_route,+ "_running_load_attempt",+ _attempt("org/incoming-model"),+ )++ response = asyncio.run(inference_route.get_status(current_subject = "test"))++ assert response.active_model == "org/resident-model"+ assert response.model_identifier == "org/resident-model"+ assert response.loaded == ["org/resident-model"]+ assert response.loading == ["org/incoming-model"]+++def test_load_is_registered_before_the_lifecycle_gate_and_always_cleared(monkeypatch):+ from core.inference import llama_keepwarm++ monkeypatch.setattr(inference_route, "_raise_if_sidecar_swap_in_progress", lambda: None)+ monkeypatch.setattr(inference_route, "_pending_load_attempts", {})+ seen = []++ @asynccontextmanager+ async def _gate():+ seen.append([a.model_path for a in inference_route._pending_load_attempts.values()])+ yield++ async def _fail(*_args, **_kwargs):+ raise RuntimeError("load failed")++ monkeypatch.setattr(llama_keepwarm, "inference_lifecycle_gate", _gate)+ monkeypatch.setattr(inference_route, "_run_tracked_load_model_impl", _fail)++ request = inference_route.LoadRequest(model_path = "org/queued-model-GGUF")+ with pytest.raises(RuntimeError):+ asyncio.run(inference_route.load_model_gated(request, object(), "test"))++ assert seen == [["org/queued-model-GGUF"]]+ assert inference_route._pending_load_attempts == {}+++def test_status_reports_an_on_device_load_by_its_public_id(monkeypatch):+ """A load still on its way to the backend must not publish the on-disk path the+ completed load is careful to keep out of the same response."""+ _patch_fast_status(monkeypatch)+ monkeypatch.setattr(+ inference_route,+ "_running_load_attempt",+ _attempt("/home/alice/models/Qwen3-30B-A3B-Q4_K_M.gguf"),+ )++ response = asyncio.run(inference_route.get_status(current_subject = "test"))++ assert response.loading == ["Qwen3-30B-A3B-Q4_K_M"]+++def test_status_reports_a_leased_native_load_by_its_registered_label(monkeypatch):+ """Once the grant is redeemed the label is what every other field reports."""+ from utils import native_path_leases++ path = "/home/alice/Downloads/private-model.gguf"+ native_path_leases._remember_native_path_for_redaction(path, "private-model")+ try:+ _patch_fast_status(monkeypatch)+ monkeypatch.setattr(inference_route, "_running_load_attempt", _attempt(path))++ response = asyncio.run(inference_route.get_status(current_subject = "test"))+ finally:+ with native_path_leases._REDACTION_LOCK:+ native_path_leases._NATIVE_PATH_LABELS.pop(path, None)+ if path in native_path_leases._NATIVE_PATH_REDACTIONS:+ native_path_leases._NATIVE_PATH_REDACTIONS.remove(path)++ assert response.loading == ["private-model"]+++def test_status_leaves_a_hub_repo_id_alone(monkeypatch):+ """The redaction only has to reach paths; a repo id is already public."""+ _patch_fast_status(monkeypatch)+ monkeypatch.setattr(+ inference_route,+ "_running_load_attempt",+ _attempt("unsloth/gemma-4-E2B-it-GGUF"),+ )++ response = asyncio.run(inference_route.get_status(current_subject = "test"))++ assert response.loading == ["unsloth/gemma-4-E2B-it-GGUF"]+++def test_status_does_not_list_one_transformers_load_twice(monkeypatch):+ """The backend names the load it is running and the registry names the one the+ route accepted. Reporting the attempt by its public id must not un-merge them."""+ backend = _FakeInferenceBackend()+ backend.active_model_name = None+ backend.models = {}+ backend.loading_models = {"/home/alice/models/local-llama"}+ _patch_fast_status(monkeypatch, backend)+ monkeypatch.setattr(inference_route, "load_inference_config", lambda _model: None)+ monkeypatch.setattr(+ inference_route,+ "_running_load_attempt",+ _attempt("/home/alice/models/local-llama"),+ )++ response = asyncio.run(inference_route.get_status(current_subject = "test"))++ assert response.loading == ["/home/alice/models/local-llama"]diff --git a/studio/frontend/src/features/chat/api/chat-adapter.ts b/studio/frontend/src/features/chat/api/chat-adapter.tsindex f7263ac6a23..753fffaaee3 100644--- a/studio/frontend/src/features/chat/api/chat-adapter.ts+++ b/studio/frontend/src/features/chat/api/chat-adapter.ts@@ -132,6 +132,10 @@ import {tryAdoptServerActiveModel,} from "../lib/apply-inference-status-to-store";import { isSpeechOnlyStatus } from "../lib/speech-only-status";+import {+ beginServerModelWait,+ statusPollSignal,+} from "../lib/server-model-wait";import { syncModelCapabilities } from "../hooks/use-chat-model-runtime";import {clampReasoningEffortToLevels,@@ -188,6 +192,7 @@ import type {CpuFallbackReason,MmprojFallbackReason,GgufVariantDetail,+ InferenceStatusResponse,OpenAIChatChunk,OpenAIChatCompletionsRequest,OpenAIChatMessage,@@ -2917,6 +2922,112 @@ async function ensureDefaultModelDownloaded(});}+// The test harness slices helpers from here; keep them below MAX_AUTO_LOAD_ATTEMPTS.+// Slow downloads and llama-server warm-up need a long cap.+const CLI_LOAD_ADOPT_MAX_MS = 600_000;++type ServerLoadBlocked = "still-loading" | "status-unavailable";++type SettledServerStatus =+ | { outcome: "settled"; status: InferenceStatusResponse }+ | { outcome: "stopped"; status?: undefined }+ | { outcome: ServerLoadBlocked; status?: undefined };++/**+ * Poll until nothing is loading. A status carrying `loading` still names the model being+ * replaced, so no caller may read a residency off it. `stopEarly` abandons the wait; a+ * caller that passes none never sees that outcome.+ */+async function waitForSettledServerStatus(options: {+ abortSignal?: AbortSignal;+ stopEarly: () => boolean;+}): Promise<SettledServerStatus>;+async function waitForSettledServerStatus(options?: {+ abortSignal?: AbortSignal;+}): Promise<Exclude<SettledServerStatus, { outcome: "stopped" }>>;+async function waitForSettledServerStatus(options?: {+ abortSignal?: AbortSignal;+ stopEarly?: () => boolean;+}): Promise<SettledServerStatus> {+ const deadline = Date.now() + CLI_LOAD_ADOPT_MAX_MS;+ let failures = 0;+ let announced = false;+ // This loop owns settlement while it runs, so an ordinary refresh must not publish a+ // status taken mid-replacement as the pick: stopEarly would read the outgoing model as+ // a user selection and hand it to the send.+ const release = beginServerModelWait(options?.abortSignal);++ try {+ for (;;) {+ options?.abortSignal?.throwIfAborted();+ if (options?.stopEarly?.()) return { outcome: "stopped" };++ let status: InferenceStatusResponse | null = null;+ // Capped, or a half-open read parks this poll past the deadline below, holding the+ // send's model-loading lease and the gate above with it.+ const poll = statusPollSignal(options?.abortSignal);+ try {+ status = await getInferenceStatus(poll.signal);+ failures = 0;+ } catch {+ // Cancellation, not a server that cannot answer: it must not become a toast.+ options?.abortSignal?.throwIfAborted();+ // A failed read is not evidence the server is idle. A timed-out one is a failure+ // like any other, so two in a row still end the wait rather than repeat it.+ if (++failures >= 2) return { outcome: "status-unavailable" };+ } finally {+ poll.dispose();+ }++ options?.abortSignal?.throwIfAborted();+ if (options?.stopEarly?.()) return { outcome: "stopped" };+ if (status && (status.loading?.length ?? 0) === 0) {+ return { outcome: "settled", status };+ }+ if (status && !announced) {+ toast.info("Waiting for model to finish loading…");+ announced = true;+ }+ if (Date.now() >= deadline) return { outcome: "still-loading" };+ await new Promise((resolve) => setTimeout(resolve, 500));+ }+ } finally {+ release();+ }+}++/** Name the reason nothing loaded, so the caller's generic advice stays suppressed. */+function reportBlockedServerLoad(outcome: ServerLoadBlocked): void {+ const stillLoading = outcome === "still-loading";+ toast.error(+ stillLoading ? "A model is still loading" : "Could not reach the model server",+ {+ description: stillLoading+ ? "Send again once it finishes, or pick a model in the top bar."+ : "Check that the server is running, then send again.",+ },+ );+}++type CliLoadAdoption = "adopted" | "server-idle" | ServerLoadBlocked;++async function adoptInFlightServerLoad(+ abortSignal?: AbortSignal,+): Promise<CliLoadAdoption> {+ const settled = await waitForSettledServerStatus({+ abortSignal,+ stopEarly: () => Boolean(useChatRuntimeStore.getState().params.checkpoint),+ });+ if (settled.outcome === "stopped") return "adopted";+ if (settled.outcome !== "settled") return settled.outcome;+ return (await tryAdoptServerActiveModel({+ allowWhileModelLoading: true,+ status: settled.status,+ }))+ ? "adopted"+ : "server-idle";+}+async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{loaded: boolean;blockedByTrustRemoteCode: boolean;@@ -2925,11 +3036,20 @@ async function autoLoadSmallestModel(options?: AutoLoadOptions): Promise<{}> {options?.abortSignal?.throwIfAborted();if (!options?.skipAdoptServerModel) {- const adoptedServerModel = await tryAdoptServerActiveModel();+ const adoption = await adoptInFlightServerLoad(options?.abortSignal);options?.abortSignal?.throwIfAborted();- if (adoptedServerModel) {+ if (adoption === "adopted") {return { loaded: true, blockedByTrustRemoteCode: false };}+ if (adoption !== "server-idle") {+ // Never auto-load unless status confirmed the server is idle.+ reportBlockedServerLoad(adoption);+ return {+ loaded: false,+ blockedByTrustRemoteCode: false,+ loadFailureReported: true,+ };+ }}const store = useChatRuntimeStore.getState();@@ -3943,11 +4063,20 @@ async function resolveQueuedEmptyLocalModel(abortSignal: AbortSignal): Promise<{// Hold the lifecycle lease across the probe. Its response cannot become// stale behind a foreground or sibling queued load, and only this owner// may clear modelLoading afterward.- // A failed probe is not evidence that the local server is empty. Fail- // closed so a transient status error cannot replace a valid resident- // model with the recorded/default auto-load candidate.- const status = await getInferenceStatus();+ // A failed probe is not evidence the local server is empty, and neither is one read+ // mid-replacement: fail closed, or the outgoing model stands in for the incoming one.+ const settled = await waitForSettledServerStatus({ abortSignal });abortSignal.throwIfAborted();+ if (settled.outcome !== "settled") {+ reportBlockedServerLoad(settled.outcome);+ return {+ loaded: false,+ blockedByTrustRemoteCode: false,+ loadFailureReported: true,+ modelRuntime: null,+ };+ }+ const status = settled.status;// The other door into adoption, bypassing tryAdoptServerActiveModel: a speech// model is not one chat can queue against, so read the slot as empty and let the// sweep below load a real chat model.diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex 1a8545c5e1a..ed1848764bc 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -3811,13 +3811,23 @@ export function ChatPage({useEffect(() => {if (getTrainingCompareHandoff()) return;- void refresh({ includeLoras: false });+ const controller = new AbortController();+ // Models and status only: a LoRA scan that hangs or 500s takes the whole Promise.all+ // with it and leaves the picker empty. The deferred refresh below owns that inventory.+ void refresh({+ includeLoras: false,+ signal: controller.signal,+ waitForServerModel: !useChatRuntimeStore.getState().params.checkpoint,+ });const timeoutId = window.setTimeout(() => {if (!inventoryRefreshStartedRef.current) {refreshDeferredModelInventories();}}, 1200);- return () => window.clearTimeout(timeoutId);+ return () => {+ controller.abort();+ window.clearTimeout(timeoutId);+ };}, [refresh, refreshDeferredModelInventories]);useEffect(() => {diff --git a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.tsindex 496d19fb216..4f0258394ef 100644--- a/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts+++ b/studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts@@ -38,6 +38,11 @@ import {} from "@/features/hub/lib/model-identity";// eslint-disable-next-line no-restricted-imports -- Avoid the hub barrel's React and download-manager exports.import { subscribeResidentStatusRefresh } from "@/features/hub/lib/resident-status-refresh";+import {+ beginServerModelWait,+ serverModelWaitOutstanding,+ statusPollSignal,+} from "../lib/server-model-wait";// eslint-disable-next-line no-restricted-imports -- The hub barrel imports chat; this lifecycle leaf does not.import { dismissStartToastsForModelSelection } from "@/features/hub/download-manager";import { prepareHfTokenForUse } from "@/features/hf-auth";@@ -81,6 +86,7 @@ import {clampLocalReasoningEffort,normalizeSpeculativeType,resolveInferenceCheckpointId,+ tryAdoptServerActiveModel,} from "../lib/apply-inference-status-to-store";import {isIdleUnloadedStatus,@@ -225,6 +231,53 @@ const MODEL_LOAD_TOAST_CLASSNAMES = {const LORA_SUFFIX_RE = /_(\d{9,})$/;+const CLI_LOAD_POLL_IDLE_MS = 60_000;+const CLI_LOAD_POLL_MAX_MS = 600_000;++async function waitForServerModel(signal?: AbortSignal): Promise<void> {+ const started = Date.now();+ let sawLoad = false;++ while (+ !signal?.aborted &&+ !useChatRuntimeStore.getState().params.checkpoint &&+ !useChatRuntimeStore.getState().modelLoading+ ) {+ let status: InferenceStatusResponse | null = null;+ // Capped, or a half-open read parks the loop past both caps below and past the+ // unmount that aborted it, with the settlement gate still up behind it.+ const poll = statusPollSignal(signal);+ try {+ status = await getInferenceStatus(poll.signal);+ } catch {+ // A later poll can recover from a transient status failure.+ } finally {+ poll.dispose();+ }+ if (+ signal?.aborted ||+ useChatRuntimeStore.getState().params.checkpoint ||+ useChatRuntimeStore.getState().modelLoading+ ) {+ return;+ }++ if (status) {+ const loading = (status.loading?.length ?? 0) > 0;+ sawLoad ||= loading;+ if (!loading && status.active_model) {+ await tryAdoptServerActiveModel({ status });+ return;+ }+ if (!loading && sawLoad) return;+ }+ const elapsed = Date.now() - started;+ if (!sawLoad && elapsed >= CLI_LOAD_POLL_IDLE_MS) return;+ if (elapsed >= CLI_LOAD_POLL_MAX_MS) return;+ await new Promise((resolve) => setTimeout(resolve, 500));+ }+}+function parseTrailingEpoch(input: string): number | undefined {const match = input.match(LORA_SUFFIX_RE);if (!match) {@@ -393,12 +446,7 @@ function getTransformersUpgradeRequiredMessage(modelName: string): string {* is a thin wrapper over it. External selections are left untouched since they* have no backend mirror.*/-// Bumped by every call. Two refreshes can be in flight at once -- a media load-// announces itself before its POST and again once the backend has committed the-// eviction -- and they read the status at different moments. Completion order is-// not the order they were issued in, so without this the older one could land-// last and re-pin the model the newer one had just seen released, leaving chat-// claiming a model that 400s on send until the load finally settled.+// Prevent older concurrent status reads from overwriting newer results.let syncGeneration = 0;let loraSyncGeneration = 0;let lastIdleUnloadArmed = false;@@ -417,10 +465,11 @@ async function syncInferenceStatusToStore(options?: {signal?: AbortSignal;includeLoras?: boolean;preserveIdleUnloaded?: boolean;+ /** A TTS load owns the chat slot even though Chat did not start the load. */+ externalChatSlotLoad?: boolean;}): Promise<void> {const signal = options?.signal;const includeLoras = options?.includeLoras ?? true;- // Last issued wins: it read the freshest status, whichever answers first.const generation = ++syncGeneration;const loraGeneration = includeLoras ? ++loraSyncGeneration : null;const superseded = () => generation !== syncGeneration;@@ -430,9 +479,10 @@ async function syncInferenceStatusToStore(options?: {useChatRuntimeStore.getState();setModelsError(null);try {+ const selectedAtStart = useChatRuntimeStore.getState().params.checkpoint;const [listRes, statusRes, , idleUnloadArmed] = await Promise.all([listModels(),- getInferenceStatus(),+ getInferenceStatus(signal),// Settled from this request alone. Read out of the aggregate below, a sibling// rejection discarded a good list yet still marked the inventory settled, so a// resident LoRA classified as a base model and pinned a new pair generalized.@@ -457,23 +507,31 @@ async function syncInferenceStatusToStore(options?: {: Promise.resolve(false),]);- // Cancellation can land while the requests above are in flight. Bail- // before writing backend state back -- cancelLoading already cleared it.- // Same for a refresh that a later one has already superseded: its answer- // describes a moment that has passed.if (signal?.aborted || superseded()) return;setModels(listRes.models.map(toChatModelRow));+ const statusLoading = (statusRes.loading?.length ?? 0) > 0;+ // A replacement names the outgoing model active and the incoming one loading. Adopting+ // the outgoing one sets the checkpoint the observer needs empty, so it writes this alone.+ if (statusLoading && serverModelWaitOutstanding()) return;+const selectedCheckpoint = useChatRuntimeStore.getState().params.checkpoint;const isExternalSelectionActive = isExternalModelId(selectedCheckpoint);+ const selectionChanged = selectedCheckpoint !== selectedAtStart;// The local selection is re-derived from this status on every mount, so adopting a// TTS model made it the chat model without the user picking it. Read the slot as// empty and the eviction branch below clears the stale pick, as it does for an// image or video load.const chatActiveModel =- statusRes.active_model && !isSpeechOnlyStatus(statusRes);- if (chatActiveModel && !isExternalSelectionActive) {+ statusRes.active_model &&+ !isSpeechOnlyStatus(statusRes) &&+ !(statusLoading && options?.externalChatSlotLoad);+ if (+ chatActiveModel &&+ !isExternalSelectionActive &&+ !selectionChanged+ ) {const checkpointId = resolveInferenceCheckpointId(statusRes);if (checkpointId) {const previousGgufVariant =@@ -513,7 +571,11 @@ async function syncInferenceStatusToStore(options?: {void refreshContextUsage({ threadId: hydrated.activeThreadId });}}- } else if (!chatActiveModel && !isExternalSelectionActive) {+ } else if (+ !chatActiveModel &&+ !isExternalSelectionActive &&+ !selectionChanged+ ) {if (isIdleUnloadedStatus(statusRes, idleUnloadArmed)) return;// Loading an image, video or audio model evicts the chat one (the GPU arbiter// allows a single owner), and nothing else here would say so: the picker@@ -532,12 +594,12 @@ async function syncInferenceStatusToStore(options?: {loadedIsDiffusion: false,});// A known prior resident or a definitive speech-only owner both prove the- // persisted Chat pick is stale. A load in flight has no active model yet- // either, so never clear while one is starting.+ // persisted Chat pick is stale. Do not clear for a Chat load that this tab+ // started. TTS is different because it owns this same slot.if ((wasResident || isSpeechOnlyStatus(statusRes)) &&selectedCheckpoint &&- !modelLoading+ (!modelLoading || options?.externalChatSlotLoad)) {// Already the clean id the header shows: resolveInferenceCheckpointId// put it there, not a load path.@@ -567,6 +629,28 @@ async function syncInferenceStatusToStore(options?: {}}+/**+ * The mount handoff: hydrate from status, then observe until the server settles.+ *+ * The gate is up before the sync is issued, since a refresh issued later can answer first+ * and adopt the outgoing model.+ */+async function refreshAndWaitForServerModel(options?: {+ signal?: AbortSignal;+ includeLoras?: boolean;+ preserveIdleUnloaded?: boolean;+ externalChatSlotLoad?: boolean;+}): Promise<void> {+ const signal = options?.signal;+ const release = beginServerModelWait(signal);+ try {+ await syncInferenceStatusToStore(options);+ await waitForServerModel(signal);+ } finally {+ release();+ }+}+/*** Reconcile the UI after the SERVER unloaded the active model out from under it* (e.g. a llama.cpp update unloads the running model to swap the binary): the@@ -675,12 +759,19 @@ export function useChatModelRuntime() {);const refresh = useCallback(- (options?: {+ async (options?: {+ waitForServerModel?: boolean;signal?: AbortSignal;includeLoras?: boolean;preserveIdleUnloaded?: boolean;- }) =>- syncInferenceStatusToStore(options),+ externalChatSlotLoad?: boolean;+ }) => {+ if (options?.waitForServerModel) {+ await refreshAndWaitForServerModel(options);+ return;+ }+ await syncInferenceStatusToStore(options);+ },[],);@@ -700,7 +791,10 @@ export function useChatModelRuntime() {// image or video load POST, before the download starts, so waiting for// the load to finish left the picker and the header naming a model that// had already gone and that 400s on send, for the whole download.- void refresh({ includeLoras: false });+ void refresh({+ includeLoras: false,+ externalChatSlotLoad: runtime === "tts",+ });}),[refresh],);@@ -875,6 +969,7 @@ export function useChatModelRuntime() {// another tab can swap the resident model, which this one is never told about// (the lifecycle events are dispatched on its own window).const adoptable = (status: InferenceStatusResponse) =>+ (status.loading?.length ?? 0) === 0 &&residentModelMatchesPick(status, {id: modelId,loadPath,diff --git a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.tsindex a167e7ad3eb..d2093d9aaa4 100644--- a/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts+++ b/studio/frontend/src/features/chat/lib/apply-inference-status-to-store.ts@@ -143,6 +143,8 @@ export type ApplyInferenceStatusOptions = {* status -- without it a variant-only switch underneath the tab reads as* steady state and the hydration reseed keeps the old quant's baselines. */previousGgufVariant?: string | null;+ /** Seed settings while the caller holds the model-loading lease. */+ seedLoadParams?: boolean;/** This status belongs to the model already resident when Studio started,* so the persisted global sampling snapshot belongs to this checkpoint. */adoptingExistingServerModel?: boolean;@@ -223,7 +225,7 @@ export function applyActiveModelStatusToStore(: status.chat_template;// While a load is in flight, performLoad owns the load params. Seeding them// from a stale poll here would clobber the values the load dialog just set.- const seedLoadParams = !prevState.modelLoading;+ const seedLoadParams = options.seedLoadParams ?? !prevState.modelLoading;// A model/variant change underneath this tab. The controls in the store belong// to the model that just left, so they are reseeded here the way every other// load param at this site already is: the echo cannot stand in, since a new@@ -720,27 +722,38 @@ export function applyActiveModelStatusToStore(}}-/**- * Adopt the model already loaded on the inference server (e.g. via- * ``unsloth studio run -m``) into the chat UI checkpoint without- * triggering a new /api/inference/load.- */-export async function tryAdoptServerActiveModel(): Promise<boolean> {+/** Adopt a server-loaded model without issuing another inference load. */+export async function tryAdoptServerActiveModel(options?: {+ /** Ignore modelLoading because the caller owns that lease. */+ allowWhileModelLoading?: boolean;+ /** A status the caller already read, so the send path does not fetch twice. */+ status?: InferenceStatusResponse;+}): Promise<boolean> {const store = useChatRuntimeStore.getState();if (store.params.checkpoint) {return true;}-- let status: InferenceStatusResponse;- try {- status = await getInferenceStatus();- } catch {- // Status endpoint unavailable: fall back to the normal auto-load path.+ if (store.modelLoading && !options?.allowWhileModelLoading) {return false;}+ let status: InferenceStatusResponse;+ if (options?.status) {+ status = options.status;+ } else {+ try {+ status = await getInferenceStatus();+ } catch {+ // Status endpoint unavailable: fall back to the normal auto-load path.+ return false;+ }+ }// Not something chat can adopt; the sweep below picks a real chat model, which evicts// it exactly as an image load would.- if (!status.active_model || isSpeechOnlyStatus(status)) {+ if (+ !status.active_model ||+ (status.loading?.length ?? 0) > 0 ||+ isSpeechOnlyStatus(status)+ ) {return false;}@@ -749,16 +762,21 @@ export async function tryAdoptServerActiveModel(): Promise<boolean> {return false;}- // Re-check after the await: keep a checkpoint the user picked meanwhile.- const previousCheckpoint = useChatRuntimeStore.getState().params.checkpoint;- if (previousCheckpoint) {- return true;+ // Preserve concurrent changes unless this caller owns the load lease.+ const latest = useChatRuntimeStore.getState();+ const previousCheckpoint = latest.params.checkpoint;+ if (+ previousCheckpoint ||+ (latest.modelLoading && !options?.allowWhileModelLoading)+ ) {+ return !!previousCheckpoint;}- const previousGgufVariant = useChatRuntimeStore.getState().activeGgufVariant;+ const previousGgufVariant = latest.activeGgufVariant;store.setCheckpoint(checkpointId, status.gguf_variant);applyActiveModelStatusToStore(status, {previousCheckpoint,previousGgufVariant,+ seedLoadParams: options?.allowWhileModelLoading,adoptingExistingServerModel: true,});return true;diff --git a/studio/frontend/src/features/chat/lib/server-model-wait.ts b/studio/frontend/src/features/chat/lib/server-model-wait.tsnew file mode 100644index 00000000000..ac27190853b--- /dev/null+++ b/studio/frontend/src/features/chat/lib/server-model-wait.ts@@ -0,0 +1,53 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// Shared by the two loops that wait for the server to settle: the mount observer and the+// send-path probe. Both poll on the same selection every refresh writes, so ownership of+// settlement cannot be a per-call argument -- a refresh landing mid-wait would publish the+// outgoing model as the pick and end the wait on it.++import {+ disposableTimeoutSignal,+ pollSignal,+ type PollSignal,+} from "@/features/hub/lib/abort-signals";++let outstanding = 0;++/** Whether some loop is waiting for the server to settle and will publish the answer. */+export function serverModelWaitOutstanding(): boolean {+ return outstanding > 0;+}++/**+ * Register a wait. Released by the returned call and by ``signal`` aborting, since the+ * requests these loops make are not all cancellable and one of them stalling would keep+ * the gate up past the page that raised it.+ */+export function beginServerModelWait(signal?: AbortSignal): () => void {+ outstanding += 1;+ let held = true;+ const release = () => {+ if (!held) return;+ held = false;+ outstanding -= 1;+ signal?.removeEventListener("abort", release);+ };+ signal?.addEventListener("abort", release, { once: true });+ return release;+}++/**+ * Per-request cap for a status poll, so the loop's own deadline is real: fetch has no+ * timeout of its own, and a half-open connection would otherwise park a poll forever and+ * hold both the gate above and the send's model-loading lease. Two orders of magnitude+ * over a healthy read (~140ms against an idle backend), so only a wedged one hits it.+ */+export const STATUS_POLL_TIMEOUT_MS = 30_000;++/** The capped signal for one status read. Callers MUST dispose it once the read settles. */+export function statusPollSignal(parent?: AbortSignal): PollSignal {+ return parent+ ? pollSignal(parent, STATUS_POLL_TIMEOUT_MS)+ : disposableTimeoutSignal(STATUS_POLL_TIMEOUT_MS);+}diff --git a/studio/frontend/tests/chat-model-residency.test.ts b/studio/frontend/tests/chat-model-residency.test.tsindex 5359cda6940..b1389103e54 100644--- a/studio/frontend/tests/chat-model-residency.test.ts+++ b/studio/frontend/tests/chat-model-residency.test.ts@@ -175,7 +175,10 @@ test("another runtime loading re-reads the chat status", () => {/if \(loading \|\| runtime === "chat"\) return;/,"the settle-only guard is what left the picker naming an evicted model",);- assert.match(hook, /void refresh\(\{ includeLoras: false \}\)/);+ assert.match(+ hook,+ /void refresh\(\{\s*includeLoras: false,\s*externalChatSlotLoad: runtime === "tts",\s*\}\)/,+ );// And the branch it feeds still clears residency.assert.match(hook, /residentCheckpoint: null,/);});@@ -193,7 +196,10 @@ test("an eviction drops the pick, not just the loaded marks", () => {);// Anchored on the branch, not on the file: other catches sit above it now.// chatActiveModel, not status.active_model: this branch owns the resident-TTS case too.- const branchStart = hook.indexOf("} else if (!chatActiveModel");+ // Matched loosely: the guard has been reflowed across lines, and a literal that+ // stopped matching would slice nothing and fail on an empty string instead.+ const branchStart = hook.search(/\} else if \(\s*!chatActiveModel/);+ assert.notEqual(branchStart, -1, "the eviction branch anchor no longer matches");const branch = hook.slice(branchStart,hook.indexOf("} catch (error) {", branchStart),diff --git a/studio/frontend/tests/chat-speech-model-not-adopted.test.ts b/studio/frontend/tests/chat-speech-model-not-adopted.test.tsindex b19fb586a86..6f55e563c3d 100644--- a/studio/frontend/tests/chat-speech-model-not-adopted.test.ts+++ b/studio/frontend/tests/chat-speech-model-not-adopted.test.ts@@ -92,13 +92,16 @@ test("the mount-time status sync treats a speech model as an empty slot", () =>);assert.match(hook,- /const chatActiveModel =\s*\n?\s*statusRes\.active_model && !isSpeechOnlyStatus\(statusRes\);/,+ /const chatActiveModel =\s*statusRes\.active_model &&\s*!isSpeechOnlyStatus\(statusRes\) &&\s*!\(statusLoading && options\?\.externalChatSlotLoad\);/,);// Both edges, or the eviction branch would stop clearing a stale pick.- assert.match(hook, /if \(chatActiveModel && !isExternalSelectionActive\)/);assert.match(hook,- /\} else if \(!chatActiveModel && !isExternalSelectionActive\)/,+ /if \(\s*chatActiveModel &&\s*!isExternalSelectionActive &&\s*!selectionChanged\s*\)/,+ );+ assert.match(+ hook,+ /\} else if \(\s*!chatActiveModel &&\s*!isExternalSelectionActive &&\s*!selectionChanged\s*\)/,);assert.match(hook,@@ -123,6 +126,16 @@ test("a TTS load announces its own runtime so chat re-reads the slot", () => {/if \(runtime === "chat" \|\| runtime === "stt"\) return;/,);assert.doesNotMatch(hook, /runtime === "tts"\) return;/);+ assert.match(+ hook,+ /externalChatSlotLoad: runtime === "tts"/,+ "the shared loading lease must not hide TTS eviction from Chat",+ );+ assert.match(+ hook,+ /\(!modelLoading \|\| options\?\.externalChatSlotLoad\)/,+ "the TTS settle event runs before Audio releases the shared loading lease",+ );});test("chat re-reads status when a different tab returns to the foreground", () => {diff --git a/studio/frontend/tests/chat-status-refresh-sequencing.test.ts b/studio/frontend/tests/chat-status-refresh-sequencing.test.tsindex 36678df6e49..3db14173369 100644--- a/studio/frontend/tests/chat-status-refresh-sequencing.test.ts+++ b/studio/frontend/tests/chat-status-refresh-sequencing.test.ts@@ -84,6 +84,67 @@ test("the eviction branch is behind the same guard", () => {assert.ok(guardAt !== -1 && guardAt < evictionAt);});+test("residency is deferred only while a settlement wait is outstanding", () => {+ assert.doesNotMatch(+ SYNC,+ /if \(statusLoading\) return;/,+ "the lifecycle bus owns settlement itself; generic hydration must still publish residency",+ );+ // Shared with the send-path poll, so both get it. Behaviour is pinned in+ // tests/studio/test_chat_mount_cli_load_adoption.py.+ assert.match(SYNC, /if \(statusLoading && serverModelWaitOutstanding\(\)\) return;/);+ // Up before the sync is issued: a refresh issued later can answer first.+ const handoff = SOURCE.slice(+ SOURCE.indexOf("async function refreshAndWaitForServerModel("),+ SOURCE.indexOf("* Reconcile the UI after the SERVER unloaded"),+ );+ assert.ok(+ handoff.indexOf("beginServerModelWait(signal)") <+ handoff.indexOf("await syncInferenceStatusToStore(options);"),+ );+ assert.match(handoff, /await waitForServerModel\(signal\);/);+ assert.match(handoff, /\} finally \{\s*release\(\);\s*\}/);+});++test("a completed UI load still publishes residency while holding its lease", () => {+ const selectionGuard = SYNC.slice(+ SYNC.indexOf("const selectionChanged"),+ SYNC.indexOf("const chatActiveModel"),+ );+ assert.match(selectionGuard, /selectedCheckpoint !== selectedAtStart/);+ assert.doesNotMatch(+ selectionGuard,+ /modelLoading/,+ "the load lease must not suppress the successful load's own settled refresh",+ );++ const activeBranch = SYNC.slice(+ SYNC.indexOf("if (\n chatActiveModel"),+ SYNC.indexOf("} else if (", SYNC.indexOf("if (\n chatActiveModel")),+ );+ assert.match(activeBranch, /applyActiveModelStatusToStore\(statusRes,/);+});++test("the mount observer adopts only a settled model", () => {+ const wait = SOURCE.slice(+ SOURCE.indexOf("async function waitForServerModel("),+ SOURCE.indexOf("function parseTrailingEpoch("),+ );+ assert.match(+ wait,+ /if \(!loading && status\.active_model\) \{\s*await tryAdoptServerActiveModel\(\{ status \}\);/,+ );+ assert.match(+ wait,+ /!useChatRuntimeStore\.getState\(\)\.params\.checkpoint &&\s*!useChatRuntimeStore\.getState\(\)\.modelLoading/,+ );+ // Into the request, and capped: a stalled read would otherwise outlive both the unmount+ // that aborted it and the two caps below.+ assert.match(wait, /const poll = statusPollSignal\(signal\);/);+ assert.match(wait, /await getInferenceStatus\(poll\.signal\)/);+ assert.match(wait, /\} finally \{\s*poll\.dispose\(\);\s*\}/);+});+test("normal startup status adoption owns the resident model's globals", () => {assert.match(SYNC,diff --git a/studio/frontend/tests/llama-extra-args-status-hydration.test.ts b/studio/frontend/tests/llama-extra-args-status-hydration.test.tsindex a3b0c13b8e6..8cd40dfb123 100644--- a/studio/frontend/tests/llama-extra-args-status-hydration.test.ts+++ b/studio/frontend/tests/llama-extra-args-status-hydration.test.ts@@ -38,6 +38,21 @@ test("the applier seeds the loaded baseline from the status echo", () => {);});+test("the CLI adoption path hydrates settings while it owns the load lease", () => {+ assert.match(+ APPLIER,+ /const seedLoadParams = options\.seedLoadParams \?\? !prevState\.modelLoading/,+ );+ assert.match(+ APPLIER,+ /seedLoadParams: options\?\.allowWhileModelLoading/,+ );+ assert.match(+ APPLIER,+ /!status\.active_model \|\|\s*\(status\.loading\?\.length \?\? 0\) > 0 \|\|\s*isSpeechOnlyStatus\(status\)/,+ );+});+test("an older backend that omits the field changes nothing", () => {// undefined is "this server does not publish it", which must leave a baseline this// tab recorded first-hand alone rather than clearing it to null.diff --git a/studio/frontend/tests/resident-config-match.test.ts b/studio/frontend/tests/resident-config-match.test.tsindex 7933afb79c4..5d3ebdf3fcb 100644--- a/studio/frontend/tests/resident-config-match.test.ts+++ b/studio/frontend/tests/resident-config-match.test.ts@@ -1640,7 +1640,7 @@ test("the shortcut re-reads and re-judges the status before adopting", () => {// The verdict is a named predicate, so it can be applied to more than one status.assert.match(source,- /const adoptable = \(status: InferenceStatusResponse\) =>/,+ /const adoptable = \(status: InferenceStatusResponse\) =>\s*\(status\.loading\?\.length \?\? 0\) === 0 &&/,"the residency verdict is no longer callable against a second status",);const decision = source.search(diff --git a/tests/studio/test_chat_autoload_failure_gate.py b/tests/studio/test_chat_autoload_failure_gate.pyindex 8cdde321677..2c056ceed92 100644--- a/tests/studio/test_chat_autoload_failure_gate.py+++ b/tests/studio/test_chat_autoload_failure_gate.py@@ -30,6 +30,62 @@ def _source_path(relative_path: str) -> Path:ADAPTER = _source_path("studio/frontend/src/features/chat/api/chat-adapter.ts")+# Inlined for real, not stubbed: waitForSettledServerStatus raises this gate so an ordinary+# refresh cannot publish a mid-replacement status as the pick underneath it.+WAIT_GATE = _source_path("studio/frontend/src/features/chat/lib/server-model-wait.ts")+++def _wait_gate_source() -> str:+ """The gate module with its two ponyfill imports dropped; PONYFILLS supplies those."""+ gate = "\n".join(+ line+ for line in WAIT_GATE.read_text(encoding = "utf-8").splitlines()+ if not line.startswith(+ (+ "import ",+ " disposableTimeoutSignal,",+ " pollSignal,",+ " type PollSignal,",+ '} from "@/features/hub',+ )+ )+ ).replace(": PollSignal", "")+ assert "export function beginServerModelWait(" in gate+ return gate+++# Short so no scenario waits out the real 30s cap; the shipped value is asserted in+# tests/studio/test_chat_mount_cli_load_adoption.py.+PONYFILLS = """+export function disposableTimeoutSignal(_ms: number) {+ const controller = new AbortController();+ const timer = setTimeout(+ () => controller.abort(new DOMException("The operation timed out.", "TimeoutError")),+ 1200,+ );+ return { signal: controller.signal, dispose: () => clearTimeout(timer) };+}+export function pollSignal(parent: AbortSignal, ms: number) {+ const timeout = disposableTimeoutSignal(ms);+ const controller = new AbortController();+ const abort = (reason: unknown) => {+ if (!controller.signal.aborted) controller.abort(reason);+ };+ const onParent = () => abort(parent.reason);+ const onTimeout = () => abort(timeout.signal.reason);+ parent.addEventListener("abort", onParent, { once: true });+ timeout.signal.addEventListener("abort", onTimeout, { once: true });+ if (parent.aborted) onParent();+ return {+ signal: controller.signal,+ dispose: () => {+ parent.removeEventListener("abort", onParent);+ timeout.signal.removeEventListener("abort", onTimeout);+ timeout.dispose();+ },+ };+}+"""TEMP = WORKDIR / "temp" / "chat_autoload_failure_gate"DEFAULT_MODEL = "unsloth/gemma-4-E2B-it-GGUF"GEMMA_REPO = "unsloth/gemma-4-26B-A4B-it-qat-GGUF"@@ -165,9 +221,30 @@ const GPU_LAYERS_AUTO = -1;// The sliced region now includes the queue-specific empty-model resolver and// visible-state snapshot helpers, so their imported contracts must exist even// though these scenarios call autoLoadSmallestModel directly.+let STATUS_CALLS = 0;async function getInferenceStatus() {- return { active_model: null };+ const call = STATUS_CALLS++;+ if (call < (SCENARIO.statusFailures ?? 0)) {+ throw new Error("status unavailable");+ }+ const loading = SCENARIO.serverLoading ?? [];+ const clearsAfter = SCENARIO.serverLoadingClearsAfter ?? null;+ const settled = clearsAfter !== null && call >= clearsAfter;+ // A replacement keeps the outgoing model resident and reported until the+ // incoming one lands, so status names both at once.+ const resident = SCENARIO.serverResident ?? null;+ const active = settled ? (loading[0] ?? resident) : resident;+ return {+ active_model: active,+ model_identifier: active,+ loading: settled ? [] : loading,+ };}+// Defined above the sliced region in chat-adapter.ts, so the slice would call a+// name the harness lacks. Reached only when the lease is already held, which no+// scenario here does.+async function waitForModelReady(_signal?: any) {}+const window: any = { location: { href: "http://localhost/chat" } };function isExternalModelId(value: unknown) {return typeof value === "string" && value.startsWith("external::");}@@ -236,16 +313,29 @@ function retainedContextPin(args: any) {function makeStore(): any {const state: any = {hfToken: null,- params: { maxSeqLength: 4096, checkpoint: "" },+ // The visible selection the queued resolver reads; external means the turn+ // has to resolve a local model of its own.+ params: { maxSeqLength: 4096, checkpoint: SCENARIO?.visibleCheckpoint ?? "" },activeGgufVariant: null,activePresetSource: null,gpuMemoryMode: "auto",selectedGpuIds: null,models: [],+ modelLoading: false,+ activeThreadEpoch: 0,+ queuedSettingsEpoch: 0,+ contextUsage: null,+ contextUsageByThreadId: {},setCheckpoint: () => {},setModelRequiresTrustRemoteCode: () => {},setParams: (p: any) => { state.params = p; },setModels: (m: any[]) => { state.models = m; },+ beginModelLoading: () => {+ if (state.modelLoading) return null;+ state.modelLoading = true;+ return { id: 1 };+ },+ endModelLoading: () => { state.modelLoading = false; },};return state;}@@ -381,7 +471,13 @@ function mmprojFallbackMessage(reason: string) {return `mmproj fallback: ${reason}`;}-async function tryAdoptServerActiveModel() { return false; }+async function tryAdoptServerActiveModel(options: any) {+ // Record adoption without reproducing store hydration.+ const id = options?.status?.active_model;+ if (!id) return false;+ EVENTS.push({ kind: "adoptServerModel", id });+ return true;+}function resolveSpeculativeSettingsForLoad() {return { speculativeType: null, specDraftNMax: 0 };}@@ -663,6 +759,7 @@ SCENARIO_HELPERS = """size_bytes: 1_100_000_000,capabilities: { can_chat: true, requires_variant: false },};+ const EXTERNAL = "external::openai/gpt-5";// A safetensors repo, which MLX serves on a Mac and transformers everywhere else.const QWEN = {repo_id: "unsloth/Qwen3.5-4B",@@ -691,6 +788,11 @@ SCENARIO_HELPERS = """platformFetched: true,variants: {},lastLoaded: null,+ serverLoading: [],+ serverResident: null,+ statusFailures: 0,+ serverLoadingClearsAfter: null,+ visibleCheckpoint: "",validate: VALIDATE_OK,load: LOADED,download: () => "complete",@@ -773,6 +875,7 @@ def _build_harness(run_dir: Path):# not count as a use.code = re.sub(r"/\*.*?\*/", "", body, flags = re.S)code = re.sub(r"//[^\n]*", "", code)+ preamble = PREAMBLE + PONYFILLS + _wait_gate_source()missing = sorted(namefor name in imported@@ -780,7 +883,7 @@ def _build_harness(run_dir: Path):and not re.search(rf"^(?:export\s+)?(?:async\s+)?"rf"(?:function\s+|const\s+|let\s+|var\s+|class\s+){re.escape(name)}\b",- PREAMBLE,+ preamble,re.M,))@@ -791,36 +894,53 @@ def _build_harness(run_dir: Path):)(run_dir / "harness.ts").write_text("// @ts-nocheck\n"- + PREAMBLE+ + preamble+ "\n"+ body- + "\nexport { autoLoadSmallestModel };\nexport function storeState() { return STORE; }\n",+ + "\nexport { autoLoadSmallestModel, resolveQueuedEmptyLocalModel };\n"+ + "export function storeState() { return STORE; }\n",encoding = "utf-8",)-def _run(scenario_expr: str) -> dict:+def _run(+ scenario_expr: str,+ prelude: str = "",+ *,+ queued: bool = False,+) -> dict:_require_node()# Its own directory per invocation: a shared file lets one runner read another's rewrite.TEMP.mkdir(parents = True, exist_ok = True)run_dir = Path(tempfile.mkdtemp(prefix = "run", dir = TEMP))_build_harness(run_dir)+ # The real send path always supplies an abort signal, so every scenario+ # exercises the signal plumbing whichever entry point it enters through.+ entry = (+ "resolveQueuedEmptyLocalModel(signal)"+ if queued+ else "autoLoadSmallestModel({ abortSignal: signal })"+ )script = (textwrap.dedent("""// @ts-nocheck- import { autoLoadSmallestModel, setScenario, EVENTS, storeState } from "./harness.ts";+ import {+ autoLoadSmallestModel,+ resolveQueuedEmptyLocalModel,+ setScenario,+ EVENTS,+ storeState,+ } from "./harness.ts";""")+ SCENARIO_HELPERS+ + textwrap.dedent(prelude)+ textwrap.dedent(f"""setScenario({scenario_expr});- // The real send path always supplies one, so the signal plumbing is- // exercised by every scenario.- const result = await autoLoadSmallestModel({{- abortSignal: new AbortController().signal,- }});+ const signal = new AbortController().signal;+ const result = await {entry};const s = storeState();console.log(JSON.stringify({{result,@@ -1683,6 +1803,131 @@ def test_an_unclassified_local_row_is_never_auto_loaded(fmt):assert _loaded_paths(out) == [DEFAULT_MODEL]+_EXPIRE_CLI_LOAD_WAIT = """+const realNow = Date.now.bind(Date);+let nowCalls = 0;+Date.now = () => realNow() + (nowCalls++ === 0 ? 0 : 600_001);+"""+++def test_send_retries_status_then_waits_for_and_adopts_the_cli_model():+ """Send retries status, waits for the CLI load, and adopts its model."""+ out = _run(+ "scenario({ statusFailures: 1, serverLoading: ['org/slow-model-GGUF'],"+ " serverLoadingClearsAfter: 2,"+ " ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })"+ )++ adopted = [e["id"] for e in out["events"] if e["kind"] == "adoptServerModel"]+ assert adopted == ["org/slow-model-GGUF"]+ assert _loaded_paths(out) == []+ assert _downloads_started(out) == []+ assert out["result"]["loaded"] is True+ # Announced once, not once per poll.+ assert [t["msg"] for t in _toasts(out, "toast.info")] == [+ "Waiting for model to finish loading…"+ ]+++def test_a_load_still_in_flight_at_the_cap_refuses_instead_of_auto_loading():+ """The wait cap must not release auto-load over a running CLI load."""+ out = _run(+ "scenario({ serverLoading: ['org/slow-model-GGUF'],"+ " ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })",+ prelude = _EXPIRE_CLI_LOAD_WAIT,+ )++ assert _loaded_paths(out) == []+ assert _downloads_started(out) == []+ assert out["result"]["loaded"] is False+ assert out["result"]["loadFailureReported"] is True+ assert [t["msg"] for t in _toasts(out, "toast.error")] == ["A model is still loading"]+++def test_an_idle_server_still_auto_loads():+ """An idle server still reaches automatic loading."""+ out = _run("scenario({ ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })")++ assert _loaded_paths(out) == [GEMMA_REPO]+ assert out["result"]["loaded"] is True+ assert _toasts(out, "toast.info") == []+++def test_a_status_endpoint_that_never_answers_refuses_rather_than_guessing():+ """An unavailable status endpoint must not be treated as idle."""+ out = _run(+ "scenario({ statusFailures: 99, ggufRepos: [GEMMA],"+ " variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })"+ )++ assert _loaded_paths(out) == []+ assert out["result"]["loaded"] is False+ assert out["result"]["loadFailureReported"] is True+ assert [t["msg"] for t in _toasts(out, "toast.error")] == ["Could not reach the model server"]+++# A queued turn whose thread carries no model resolves one of its own, and the visible+# tab may be on a provider model meanwhile. That resolver reads /status directly, so it+# needs the same in-flight-load gate the sweep above has.+++def test_a_queued_turn_binds_the_incoming_model_not_the_one_being_replaced():+ """During a replacement the status names the resident model and the incoming one at+ once, and only the incoming one is what this turn will actually run against."""+ out = _run(+ "scenario({ visibleCheckpoint: EXTERNAL, serverResident: 'org/outgoing-GGUF',"+ " serverLoading: ['org/slow-model-GGUF'], serverLoadingClearsAfter: 2,"+ " ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })",+ queued = True,+ )++ assert out["result"]["loaded"] is True+ assert out["result"]["modelRuntime"]["checkpoint"] == "org/slow-model-GGUF"+ assert _loaded_paths(out) == []+ assert _downloads_started(out) == []+++def test_a_queued_turn_refuses_rather_than_auto_loading_over_a_cli_load():+ """The wait cap must not release the queued sweep over a running CLI load either."""+ out = _run(+ "scenario({ visibleCheckpoint: EXTERNAL, serverLoading: ['org/slow-model-GGUF'],"+ " ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })",+ prelude = _EXPIRE_CLI_LOAD_WAIT,+ queued = True,+ )++ assert _loaded_paths(out) == []+ assert _downloads_started(out) == []+ assert out["result"]["loaded"] is False+ assert out["result"]["loadFailureReported"] is True+ assert out["result"]["modelRuntime"] is None+ assert [t["msg"] for t in _toasts(out, "toast.error")] == ["A model is still loading"]+++def test_a_queued_turn_still_binds_a_resident_model_on_an_idle_server():+ out = _run(+ "scenario({ visibleCheckpoint: EXTERNAL, serverResident: 'org/resident-GGUF',"+ " ggufRepos: [GEMMA], variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })",+ queued = True,+ )++ assert out["result"]["loaded"] is True+ assert out["result"]["modelRuntime"]["checkpoint"] == "org/resident-GGUF"+ assert _loaded_paths(out) == []+ assert _toasts(out, "toast.info") == []+++def test_a_queued_turn_on_an_empty_idle_server_still_auto_loads():+ out = _run(+ "scenario({ visibleCheckpoint: EXTERNAL, ggufRepos: [GEMMA],"+ " variants: { [GEMMA.repo_id]: GEMMA_VARIANTS } })",+ queued = True,+ )++ assert _loaded_paths(out) == [GEMMA_REPO]+ assert out["result"]["loaded"] is True++def test_an_unpinned_mlx_candidate_is_loaded_at_the_window_the_backend_resolved():"""The auto-load runs unwatched, so its request is what an MLX model is left serving;the app default there loaded every model at 4096."""diff --git a/tests/studio/test_chat_mount_cli_load_adoption.py b/tests/studio/test_chat_mount_cli_load_adoption.pynew file mode 100644index 00000000000..514adfd5799--- /dev/null+++ b/tests/studio/test_chat_mount_cli_load_adoption.py@@ -0,0 +1,675 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The chat mount must end up on the model the server is loading, not the one it is replacing.++Runs the real ``refresh`` -> ``syncInferenceStatusToStore`` -> ``waitForServerModel`` sequence from+use-chat-model-runtime.ts under node with the module boundary stubbed, so these assert behaviour+rather than source text. A replacement names the outgoing model ``active_model`` and the incoming+one ``loading``; adopting the outgoing one sets the checkpoint the observer needs empty.+"""++import json+import os+import re+import shutil+import subprocess+import tempfile+import textwrap+from pathlib import Path++import pytest++WORKDIR = Path(__file__).resolve().parents[2]+++def _source_path(relative_path: str) -> Path:+ direct = WORKDIR / relative_path+ if direct.exists():+ return direct+ return WORKDIR / "unsloth_repo" / relative_path+++HOOK = _source_path("studio/frontend/src/features/chat/hooks/use-chat-model-runtime.ts")+CHAT_PAGE = _source_path("studio/frontend/src/features/chat/chat-page.tsx")+# Inlined for real, not stubbed: the gate is shared with the send-path poll, so a stub would+# only prove this file agrees with itself.+WAIT_GATE = _source_path("studio/frontend/src/features/chat/lib/server-model-wait.ts")+TEMP = WORKDIR / "temp" / "chat_mount_cli_load_adoption"+OUTGOING = "org/outgoing-A-GGUF"+INCOMING = "org/incoming-B-GGUF"++# Every module-boundary name the sliced region uses. A missing one is a ReferenceError the+# sync's catch turns into a toast, so it reads as a wrong-checkpoint failure instead.+PREAMBLE = """+export const EVENTS: any[] = [];++type Scenario = {+ status: (elapsedMs: number) => any;+ models: any[];+ /** Per-call listModels() latency, last entry repeating: lets a later refresh answer first. */+ listModelsDelays?: number[];+ /** 1-based /status reads that only end on abort (or 5s). Indexed, so their neighbours answer. */+ hangStatusReads?: number[];+ listLorasFails?: boolean;+};+let SCENARIO: Scenario = { status: () => ({}), models: [] };+let scenarioStart = 0;+let listModelsCalls = 0;+export function setScenario(next: Scenario): void {+ SCENARIO = next;+ scenarioStart = Date.now();+ listModelsCalls = 0;+ statusReads = 0;+}++let STATE: Record<string, any> = {};+export function setStoreState(next: Record<string, any>): void {+ STATE = { ...next, ...ACTIONS };+}+export function storeState(): Record<string, any> {+ return STATE;+}++const ACTIONS = {+ setModels(models: any[]) {+ STATE.models = models;+ EVENTS.push({ kind: "setModels", ids: models.map((model: any) => model.id) });+ },+ setLoras(loras: any[]) {+ STATE.loras = loras;+ },+ setModelsError(message: string | null) {+ STATE.modelsError = message;+ if (message) EVENTS.push({ kind: "modelsError", message });+ },+ setCheckpoint(checkpoint: string, ggufVariant?: string | null) {+ STATE.params = { ...STATE.params, checkpoint };+ STATE.activeGgufVariant = ggufVariant ?? null;+ EVENTS.push({ kind: "setCheckpoint", checkpoint });+ },+ clearCheckpoint() {+ STATE.params = { ...STATE.params, checkpoint: "" };+ EVENTS.push({ kind: "clearCheckpoint" });+ },+};++export const useChatRuntimeStore = {+ getState: () => STATE,+ setState: (patch: Record<string, any>) => {+ STATE = { ...STATE, ...patch };+ EVENTS.push({ kind: "setState", patch });+ },+};++export async function listModels(): Promise<any> {+ const delays = SCENARIO.listModelsDelays ?? [];+ const delay = delays[Math.min(listModelsCalls, delays.length - 1)] ?? 0;+ listModelsCalls += 1;+ if (delay > 0) await new Promise((resolve) => setTimeout(resolve, delay));+ return { models: SCENARIO.models };+}+export async function listLoras(): Promise<any> {+ if (SCENARIO.listLorasFails) throw new Error("lora scan failed");+ return { loras: [] };+}+let statusReads = 0;+export async function getInferenceStatus(signal?: AbortSignal): Promise<any> {+ statusReads += 1;+ if ((SCENARIO.hangStatusReads ?? []).includes(statusReads)) {+ EVENTS.push({ kind: "status.hang", signalled: Boolean(signal) });+ // Bounded so an unfixed source fails on the assertion below rather than hanging.+ await new Promise((resolve, reject) => {+ const timer = setTimeout(resolve, 5000);+ signal?.addEventListener("abort", () => {+ clearTimeout(timer);+ reject(new Error("aborted"));+ });+ });+ }+ const status = SCENARIO.status(Date.now() - scenarioStart);+ EVENTS.push({ kind: "status", active_model: status.active_model, loading: status.loading });+ return status;+}+export async function loadOpenAIAutoSwitchSettings(): Promise<any> {+ return { idleUnloadActive: false };+}++export const toChatModelRow = (model: any) => model;+export const toLoraSummary = (lora: any) => lora;+export const isExternalModelId = (id: unknown) =>+ typeof id === "string" && id.startsWith("external:");+export const isSpeechOnlyStatus = (status: any) => Boolean(status?.speechOnly);+export const isIdleUnloadedStatus = (status: any, _armed: boolean) =>+ Boolean(status?.idleUnloaded);+export const resolveInferenceCheckpointId = (status: any) =>+ status?.model_identifier ?? status?.active_model ?? null;++export function applyActiveModelStatusToStore(status: any, _options: any): void {+ STATE.residentCheckpoint = resolveInferenceCheckpointId(status);+ EVENTS.push({ kind: "applyActiveModel", checkpoint: STATE.residentCheckpoint });+}+export function syncModelCapabilities(_checkpoint: string, _status: any): void {}+export function refreshContextUsage(_options: any): void {}++// Stubbed at the module boundary it really crosses, but keeping the three refusals: "a pick+// already landed" is what a concurrent refresh produces, and adopting anyway would hide the bug.+export async function tryAdoptServerActiveModel(options: any): Promise<boolean> {+ if (STATE.params.checkpoint) return true;+ const status = options?.status;+ if (!status?.active_model) return false;+ if ((status.loading?.length ?? 0) > 0) return false;+ if (isSpeechOnlyStatus(status)) return false;+ const checkpoint = resolveInferenceCheckpointId(status);+ if (!checkpoint) return false;+ ACTIONS.setCheckpoint(checkpoint, status.gguf_variant ?? null);+ STATE.residentCheckpoint = checkpoint;+ EVENTS.push({ kind: "adopt", checkpoint });+ return true;+}++export const toast = {+ info: (title: string, options?: any) => EVENTS.push({ kind: "toast.info", title, options }),+ error: (title: string, options?: any) => EVENTS.push({ kind: "toast.error", title, options }),+};++// The two ponyfills server-model-wait.ts imports. Its own per-read cap is 30s, which no test+// should wait out, so the timeout here is short and the real constant is asserted separately.+export const TEST_POLL_TIMEOUT_MS = 1200;+export function disposableTimeoutSignal(_ms: number) {+ const controller = new AbortController();+ const timer = setTimeout(+ () => controller.abort(new DOMException("The operation timed out.", "TimeoutError")),+ TEST_POLL_TIMEOUT_MS,+ );+ return { signal: controller.signal, dispose: () => clearTimeout(timer) };+}+export function pollSignal(parent: AbortSignal, _ms: number) {+ const timeout = disposableTimeoutSignal(_ms);+ const controller = new AbortController();+ const onAbort = (reason: unknown) => {+ if (!controller.signal.aborted) controller.abort(reason);+ };+ const a = () => onAbort(parent.reason);+ const b = () => onAbort(timeout.signal.reason);+ parent.addEventListener("abort", a, { once: true });+ timeout.signal.addEventListener("abort", b, { once: true });+ if (parent.aborted) a();+ return {+ signal: controller.signal,+ dispose: () => {+ parent.removeEventListener("abort", a);+ timeout.signal.removeEventListener("abort", b);+ timeout.dispose();+ },+ };+}+"""+++def _require_node() -> None:+ if shutil.which("node") is None:+ pytest.skip("node not available")+ if not HOOK.exists():+ pytest.skip("studio chat sources not present")+ try:+ result = subprocess.run(+ ["node", "--experimental-strip-types", "--version"],+ capture_output = True,+ text = True,+ # A cold Windows runner is slow to start node; an impatient probe would fail the gate.+ timeout = 60,+ )+ except (OSError, subprocess.SubprocessError):+ pytest.skip("node could not be started")+ if result.returncode != 0:+ pytest.skip("node --experimental-strip-types not available")+++def _between(source: str, start: str, end: str) -> str:+ first = source.find(start)+ assert first != -1, f"could not locate {start!r} in {HOOK.name}"+ last = source.find(end, first)+ assert last != -1, f"could not locate {end!r} after {start!r} in {HOOK.name}"+ return source[first:last]+++def _build_harness(run_dir: Path) -> None:+ """Slice the mount path verbatim out of the hook."""+ source = HOOK.read_text(encoding = "utf-8")+ # The real gate, with its imports dropped: the preamble supplies those two ponyfills.+ gate = "\n".join(+ line+ for line in WAIT_GATE.read_text(encoding = "utf-8").splitlines()+ if not line.startswith(+ (+ "import ",+ " disposableTimeoutSignal,",+ " pollSignal,",+ " type PollSignal,",+ '} from "@/features/hub',+ )+ )+ ).replace(": PollSignal", "")+ assert "export function beginServerModelWait(" in gate+ poll = _between(source, "const CLI_LOAD_POLL_IDLE_MS", "function parseTrailingEpoch(")+ assert "async function waitForServerModel(" in poll+ sync = _between(+ source,+ "// Prevent older concurrent status reads",+ "/**\n * Reconcile the UI after the SERVER unloaded",+ )+ assert "async function syncInferenceStatusToStore(" in sync+ # The sequencing IS the bug, so refresh runs for real rather than being re-typed here.+ match = re.search(+ r"const refresh = useCallback\(\n"+ r" async \(options\?: \{(?P<params>.*?)\}\) => \{(?P<body>.*?)\n"+ r" \},\n \[\],\n \);",+ source,+ re.S,+ )+ assert match, "could not locate the refresh callback in use-chat-model-runtime.ts"+ refresh = (+ f"export async function refresh(options?: {{{match.group('params')}}}) "+ f"{{{match.group('body')}\n}}\n"+ )+ assert "syncInferenceStatusToStore(options)" in refresh+ # The handoff owner has to come along, or the wait is registered nowhere.+ assert "async function refreshAndWaitForServerModel(" in sync+ (run_dir / "harness.ts").write_text(+ "// @ts-nocheck\n" + PREAMBLE + "\n" + gate + "\n" + poll + "\n" + sync + "\n" + refresh,+ encoding = "utf-8",+ )+++def _run(script_body: str) -> dict:+ _require_node()+ TEMP.mkdir(parents = True, exist_ok = True)+ # Its own directory per invocation: a shared file lets one runner read another's rewrite.+ run_dir = Path(tempfile.mkdtemp(prefix = "run", dir = TEMP))+ _build_harness(run_dir)+ script = (+ textwrap.dedent(+ """+ // @ts-nocheck+ import {+ EVENTS,+ beginServerModelWait,+ refresh,+ setScenario,+ setStoreState,+ storeState,+ } from "./harness.ts";++ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));++ function emptyStore(over = {}) {+ return {+ params: { checkpoint: "" },+ modelLoading: false,+ residentCheckpoint: undefined,+ activeGgufVariant: null,+ contextUsage: null,+ activeThreadId: null,+ ggufContextLength: null,+ models: [],+ loras: [],+ ...over,+ };+ }++ const REPLACING = {+ active_model: "%(outgoing)s",+ model_identifier: "%(outgoing)s",+ is_gguf: true,+ gguf_variant: "Q4_K_M",+ loading: ["%(incoming)s"],+ };+ const SETTLED_ON_INCOMING = {+ active_model: "%(incoming)s",+ model_identifier: "%(incoming)s",+ is_gguf: true,+ gguf_variant: "Q4_K_M",+ loading: [],+ };+ const IDLE = { active_model: null, model_identifier: null, loading: [] };+ const STARTING = {+ active_model: null,+ model_identifier: null,+ is_gguf: true,+ loading: ["%(incoming)s"],+ };+ const settlesAfter = (ms) => (elapsed) =>+ elapsed < ms ? REPLACING : SETTLED_ON_INCOMING;+ """+ % {"outgoing": OUTGOING, "incoming": INCOMING}+ )+ + textwrap.dedent(script_body)+ + textwrap.dedent(+ """+ console.log(+ JSON.stringify({+ checkpoint: storeState().params.checkpoint,+ residentCheckpoint: storeState().residentCheckpoint ?? null,+ models: (storeState().models ?? []).map((model) => model.id),+ events: EVENTS,+ }),+ );+ """+ )+ )+ (run_dir / "run.mts").write_text(script, encoding = "utf-8")+ completed = subprocess.run(+ ["node", "--experimental-strip-types", "--no-warnings", "run.mts"],+ cwd = str(run_dir),+ capture_output = True,+ text = True,+ # Explicit: text alone decodes with the Windows ANSI code page, which mangles the+ # non-ASCII toast copy node emits as UTF-8.+ encoding = "utf-8",+ timeout = 120,+ env = dict(os.environ, NODE_NO_WARNINGS = "1"),+ )+ assert completed.returncode == 0, f"stderr: {completed.stderr}\nstdout: {completed.stdout}"+ last = [line for line in completed.stdout.strip().splitlines() if line.strip()][-1]+ return json.loads(last)+++# The ChatPage mount effect, argument for argument (chat-page.tsx).+MOUNT = """+ setStoreState(emptyStore());+ const mount = refresh({+ includeLoras: false,+ signal: new AbortController().signal,+ waitForServerModel: true,+ });+"""+++def test_mount_adopts_the_incoming_cli_model_not_the_outgoing_one():+ """The reported case: A resident, a CLI-requested B in flight, and no local pick to protect."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }, { id: "%(incoming)s" }],+ status: settlesAfter(700),+ });+ """+ % {"outgoing": OUTGOING, "incoming": INCOMING}+ + MOUNT+ + " await mount;\n"+ )+ assert out["checkpoint"] == INCOMING+ assert out["residentCheckpoint"] == INCOMING+++def test_a_refresh_landing_mid_wait_does_not_end_it_on_the_outgoing_model():+ """ChatPage refreshes again 1.2s after mount, the lifecycle bus and a returning tab more.+ None asks to wait, so each would adopt the outgoing model and leave the observer nothing."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }, { id: "%(incoming)s" }],+ status: settlesAfter(1500),+ });+ """+ % {"outgoing": OUTGOING, "incoming": INCOMING}+ + MOUNT+ + """+ await sleep(600);+ await refresh({ includeLoras: false });+ await mount;+ """+ )+ assert out["checkpoint"] == INCOMING+ assert out["residentCheckpoint"] == INCOMING+++def test_mount_waits_for_a_cli_load_that_starts_after_it():+ """The case the PR was opened for: the UI opens before `studio run -m` reaches the loader,+ so the first read is a bare idle server and the load only shows up a poll or two later."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(incoming)s" }],+ status: (elapsed) =>+ elapsed < 600 ? IDLE : elapsed < 1600 ? STARTING : SETTLED_ON_INCOMING,+ });+ """+ % {"incoming": INCOMING}+ + MOUNT+ + " await mount;\n"+ )+ assert out["checkpoint"] == INCOMING+ assert out["residentCheckpoint"] == INCOMING+++def test_a_refresh_that_answers_before_the_mount_sync_does_not_end_the_wait():+ """Registered before the sync is issued, not after it returns: a refresh issued second can+ answer first, and inside that window it saw no wait outstanding and adopted the outgoing one."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }, { id: "%(incoming)s" }],+ status: settlesAfter(1500),+ listModelsDelays: [400, 0],+ });+ """+ % {"outgoing": OUTGOING, "incoming": INCOMING}+ + MOUNT+ + """+ await sleep(50);+ await refresh({ includeLoras: false });+ await mount;+ """+ )+ assert out["checkpoint"] == INCOMING+ assert out["residentCheckpoint"] == INCOMING+++def test_aborting_the_mount_releases_the_wait_even_on_a_stalled_status_read():+ """Deferring residency only holds if the wait can end: checked around the request rather+ than passed into it, an unmount leaves the poll parked and every later refresh behind it."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }],+ status: () => REPLACING,+ // The observer's first poll: the reads on either side of it still answer.+ hangStatusReads: [2],+ });+ setStoreState(emptyStore());+ const controller = new AbortController();+ const mount = refresh({+ includeLoras: false,+ signal: controller.signal,+ waitForServerModel: true,+ });+ await sleep(300);+ controller.abort();+ await sleep(50);+ await refresh({ includeLoras: false });+ await mount;+ """+ % {"outgoing": OUTGOING}+ )+ assert out["checkpoint"] == OUTGOING+ assert out["residentCheckpoint"] == OUTGOING+++def test_aborting_the_mount_releases_the_wait_even_on_an_unsignalled_read():+ """And down on the abort itself, not on the way out: listModels and listLoras take no+ AbortSignal, so waiting for the sync to return leaves the gate up past its own page."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }],+ status: () => REPLACING,+ listModelsDelays: [2500, 0],+ });+ setStoreState(emptyStore());+ const controller = new AbortController();+ const mount = refresh({+ includeLoras: false,+ signal: controller.signal,+ waitForServerModel: true,+ });+ await sleep(300);+ controller.abort();+ await sleep(50);+ await refresh({ includeLoras: false });+ await mount;+ """+ % {"outgoing": OUTGOING}+ )+ assert out["checkpoint"] == OUTGOING+ assert out["residentCheckpoint"] == OUTGOING+++def test_mount_publishes_the_model_list_while_it_waits():+ """Waiting must not cost the selector its inventory: the picker stays usable during the load."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }, { id: "%(incoming)s" }],+ status: settlesAfter(700),+ });+ """+ % {"outgoing": OUTGOING, "incoming": INCOMING}+ + MOUNT+ + " await mount;\n"+ )+ kinds = [event["kind"] for event in out["events"]]+ assert out["models"] == [OUTGOING, INCOMING]+ assert kinds.index("setModels") < kinds.index("setCheckpoint")+++def test_mount_still_adopts_a_settled_model_with_no_load_in_flight():+ """Nothing loading is the ordinary mount, and it must still hydrate from the first read."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(incoming)s" }],+ status: settlesAfter(0),+ });+ """+ % {"incoming": INCOMING}+ + MOUNT+ + " await mount;\n"+ )+ assert out["checkpoint"] == INCOMING+ # One read: the sync's own. A settled status must not cost a second round trip.+ assert [event["kind"] for event in out["events"]].count("status") == 1+++def test_a_failing_lora_scan_takes_the_model_list_with_it():+ """Why the mount must not ask for LoRAs: the three reads share one Promise.all and the LoRA+ handler rethrows, so a rejection skips setModels and the picker offers nothing at all."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }],+ status: () => SETTLED_ON_INCOMING,+ listLorasFails: true,+ });+ setStoreState(emptyStore());+ await refresh({ includeLoras: true });+ """+ % {"outgoing": OUTGOING}+ )+ assert out["models"] == []+ assert [event["kind"] for event in out["events"]].count("modelsError") == 1+++def test_the_mount_refresh_does_not_wait_on_the_lora_inventory():+ """So the mount reads models and status only. The deferred inventory refresh 1.2s later+ owns the LoRA list, and its failing is survivable: the picker already has its models."""+ source = CHAT_PAGE.read_text(encoding = "utf-8")+ effect = _between(source, "if (getTrainingCompareHandoff()) return;", "}, 1200);")+ assert re.search(r"void refresh\(\{\s*includeLoras: false,", effect), effect+ assert "waitForServerModel: !useChatRuntimeStore.getState().params.checkpoint" in effect+ assert "refreshDeferredModelInventories();" in effect+++def test_a_wait_from_the_send_path_also_stops_a_refresh_publishing_the_outgoing_model():+ """The gate is shared, so the send-path poll gets the same protection as the mount one.++ That poll's stopEarly reads params.checkpoint to decide the user picked something. A+ refresh hydrating the outgoing model mid-replacement would look exactly like a pick and+ hand it to the send, which then talks to the model the server is replacing.+ """+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }],+ status: () => REPLACING,+ });+ setStoreState(emptyStore());+ const release = beginServerModelWait();+ await refresh({ includeLoras: false });+ const during = storeState().params.checkpoint;+ release();+ await refresh({ includeLoras: false });+ EVENTS.push({ kind: "duringWait", checkpoint: during });+ """+ % {"outgoing": OUTGOING}+ )+ during = [e for e in out["events"] if e["kind"] == "duringWait"][0]["checkpoint"]+ assert during == "", "a refresh published the outgoing model while a wait was outstanding"+ # And the gate is a gate, not a mute: once the wait is over the same refresh publishes.+ assert out["checkpoint"] == OUTGOING+++def test_a_stalled_status_read_does_not_park_the_poll_on_one_request():+ """Each read is capped, so the loop's own deadline is real.++ fetch has no timeout, so before this the advertised cap bounded nothing: one half-open+ read parked the poll indefinitely, holding the shared gate and the send's lease with it.+ """+ out = _run(+ """+ setScenario({+ models: [{ id: "%(incoming)s" }],+ // Reads 2 and 3 are the observer's, and neither ever answers.+ hangStatusReads: [2, 3],+ status: (elapsed) => (elapsed < 4000 ? STARTING : SETTLED_ON_INCOMING),+ });+ """+ % {"incoming": INCOMING}+ + MOUNT+ + " await mount;\n"+ )+ assert out["checkpoint"] == INCOMING+ # Two capped reads plus the ones on either side: the loop kept going instead of parking.+ assert [event["kind"] for event in out["events"]].count("status.hang") == 2+ assert [event["kind"] for event in out["events"]].count("status") >= 3+++def test_the_shipped_per_read_cap_is_the_one_the_harness_stands_in_for():+ """The harness shortens the cap so no test waits it out; this pins what really ships."""+ gate = WAIT_GATE.read_text(encoding = "utf-8")+ assert "export const STATUS_POLL_TIMEOUT_MS = 30_000;" in gate+ assert re.search(r"return parent\s*\?\s*pollSignal\(parent, STATUS_POLL_TIMEOUT_MS\)", gate)+ adapter = _source_path("studio/frontend/src/features/chat/api/chat-adapter.ts").read_text(+ encoding = "utf-8"+ )+ assert "statusPollSignal(options?.abortSignal)" in adapter+ assert "beginServerModelWait(options?.abortSignal)" in adapter+++def test_a_refresh_that_is_not_the_mount_waiter_still_publishes_residency():+ """The guard for the commit that removed the blanket loading bail-out: a refresh with no+ waiter behind it owns residency itself, so a load in flight must not stop it publishing."""+ out = _run(+ """+ setScenario({+ models: [{ id: "%(outgoing)s" }],+ status: () => REPLACING,+ });+ setStoreState(emptyStore());+ await refresh({ includeLoras: false });+ """+ % {"outgoing": OUTGOING}+ )+ assert out["checkpoint"] == OUTGOING+ assert out["residentCheckpoint"] == OUTGOINGdiff --git a/tests/studio/test_multi_chat_prompt_queue_contract.py b/tests/studio/test_multi_chat_prompt_queue_contract.pyindex 929a56ab4ec..883e64093f4 100644--- a/tests/studio/test_multi_chat_prompt_queue_contract.py+++ b/tests/studio/test_multi_chat_prompt_queue_contract.py@@ -518,13 +518,30 @@ def test_queued_settings_are_thread_scoped_without_cross_chat_fallback():"async function resolveQueuedEmptyLocalModel(","export function createOpenAIStreamAdapter",)- assert lifecycle.index("beginModelLoading()") < lifecycle.index("await getInferenceStatus()")- assert lifecycle.index("await getInferenceStatus()") < lifecycle.index(- "await autoLoadSmallestModel("- )+ # The probe waits out an in-flight load rather than reading a status taken+ # mid-replacement, which names the outgoing model alongside the incoming one.+ probe = "await waitForSettledServerStatus({ abortSignal })"+ assert lifecycle.index("beginModelLoading()") < lifecycle.index(probe)+ assert lifecycle.index(probe) < lifecycle.index("await autoLoadSmallestModel(")assert "getInferenceStatus().catch(() => null)" not in lifecycle- assert "const status = await getInferenceStatus();" in lifecycle+ assert f"const settled = {probe};" in lifecycle+ assert "const status = settled.status;" in lifecycleassert "options?.abortSignal?.throwIfAborted()" in CHAT_ADAPTER+ # Into the request, not only around it, and capped by the loop's own deadline, or a+ # half-open read parks the send past its cancellation. The abort goes ahead of the failure+ # counter, or a cancelled read surfaces as "could not reach the model server".+ poll = _between(+ CHAT_ADAPTER,+ "const deadline = Date.now() + CLI_LOAD_ADOPT_MAX_MS;",+ "function reportBlockedServerLoad(",+ )+ assert "const poll = statusPollSignal(options?.abortSignal);" in poll+ assert "await getInferenceStatus(poll.signal)" in poll+ assert "poll.dispose();" in poll+ assert poll.index("options?.abortSignal?.throwIfAborted();") < poll.index("++failures")+ # And the loop registers as a settlement wait, so a refresh cannot publish a status taken+ # mid-replacement as the pick that stopEarly then reads as a user selection.+ assert "const release = beginServerModelWait(options?.abortSignal);" in pollassert (len(re.findall(