Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

fix(models): the Models page no longer waits seconds for the Windows runtime proof (#8212)

Mike Bradleyd9f976d64879
diff --git a/ods/extensions/services/dashboard-api/routers/models.py b/ods/extensions/services/dashboard-api/routers/models.py
index b45409761..ff5e44912 100644
--- a/ods/extensions/services/dashboard-api/routers/models.py
+++ b/ods/extensions/services/dashboard-api/routers/models.py
@@ -1928,6 +1928,104 @@ def _model_management() -> dict:
return {key: value for key, value in proof.items() if key != "vision"}
+# The ownership proof runs a controller on the Windows host and takes seconds,
+# more under load: on an RTX Spark it took 3.6-5.4 s during a Portal build and
+# was the slowest part of every GET /api/models. The catalog reuses the last
+# conclusive proof while the live runtime agrees with it and refreshes it in
+# the background. A runtime change made here forgets it, an owned lifecycle
+# operation always gets a fresh one, and the host proves ownership again
+# before any control acts.
+_MODEL_MANAGEMENT_REFRESH_SECONDS = 15.0
+_MODEL_MANAGEMENT_MAX_AGE_SECONDS = 6 * 3600.0
+_model_management_lock = threading.Lock()
+_model_management_remembered: tuple[float, dict] | None = None
+_model_management_generation = 0
+_model_management_proof_future: asyncio.Future | None = None
+_model_management_missed = False
+
+
+def _forget_model_management() -> None:
+ """Drop the remembered proof, and any proof still running from before a runtime change."""
+ global _model_management_remembered, _model_management_generation, _model_management_proof_future
+ global _model_management_missed
+ with _model_management_lock:
+ _model_management_remembered = None
+ _model_management_generation += 1
+ _model_management_proof_future = None
+ _model_management_missed = False
+
+
+async def _prove_model_management(generation: int) -> tuple[int, dict]:
+ global _model_management_remembered, _model_management_missed
+ value = await asyncio.to_thread(_model_management)
+ with _model_management_lock:
+ if generation == _model_management_generation:
+ if value.get("managed") is not None:
+ _model_management_remembered = (time.monotonic(), dict(value))
+ _model_management_missed = False
+ elif _model_management_missed:
+ # Two unverified proofs in a row: the host keeps failing, so the
+ # catalog stops showing the last good one. A single interop
+ # timeout leaves it in place.
+ _model_management_remembered = None
+ else:
+ _model_management_missed = True
+ return generation, value
+
+
+def _log_model_management_crash(done: asyncio.Future) -> None:
+ # A background refresh has no reader, so its crash is logged here.
+ if not done.cancelled() and done.exception() is not None:
+ logger.error("Model management proof crashed", exc_info=done.exception())
+
+
+def _model_management_in_flight() -> asyncio.Future:
+ """The proof already running, or a new one; concurrent catalog reads share it."""
+ global _model_management_proof_future
+ with _model_management_lock:
+ future = _model_management_proof_future
+ if future is None or future.done():
+ future = asyncio.ensure_future(_prove_model_management(_model_management_generation))
+ future.add_done_callback(_log_model_management_crash)
+ _model_management_proof_future = future
+ return future
+
+
+def _remembered_model_management() -> tuple[float, dict] | None:
+ with _model_management_lock:
+ remembered = _model_management_remembered
+ if remembered is None or time.monotonic() - remembered[0] > _MODEL_MANAGEMENT_MAX_AGE_SECONDS:
+ return None
+ return remembered
+
+
+async def _fresh_model_management(future: asyncio.Future) -> dict:
+ """The proof ``future`` yields, taken again if a runtime change landed meanwhile."""
+ generation, value = await asyncio.shield(future)
+ if generation != _model_management_generation:
+ _generation, value = await asyncio.shield(_model_management_in_flight())
+ return dict(value)
+
+
+async def _catalog_model_management(prefetch: asyncio.Future | None, agent_status: object,
+ loaded_model: str | None) -> dict:
+ """A fresh proof, or the remembered one when the live runtime agrees with it."""
+ if prefetch is not None:
+ return await _fresh_model_management(prefetch)
+ remembered = _remembered_model_management()
+ lifecycle = isinstance(agent_status, dict) and (
+ agent_status.get("lifecycleActive") is True or agent_status.get("modelTransactionPending") is True)
+ if remembered is not None and not lifecycle:
+ taken_at, value = remembered
+ # An owned runtime runs exactly when its model answers, so a crash or
+ # a stop made elsewhere never keeps the earlier controls on screen.
+ if value.get("managed") is not True or value.get("running") is (loaded_model is not None):
+ if time.monotonic() - taken_at > _MODEL_MANAGEMENT_REFRESH_SECONDS:
+ _model_management_in_flight()
+ return dict(value)
+ return await _fresh_model_management(_model_management_in_flight())
+
+
# Docker Desktop installs whose host agent launches llama-server.exe on
# Windows itself (the host agent's _WINDOWS_NATIVE_RUNTIME_MODES); that
# launch takes no projector.
@@ -1965,6 +2063,10 @@ async def _discover_loaded_model() -> str | None:
async def list_models(api_key: str = Depends(verify_api_key)):
"""List model catalog entries with source-labelled performance metadata."""
global _last_recorded_throughput_sample
+ host_runtime = _windows_hosted_runtime()
+ # With no proof to reuse, the slow ownership proof runs alongside these reads.
+ management_prefetch = (_model_management_in_flight()
+ if host_runtime and _remembered_model_management() is None else None)
gpu_info, loaded_model, agent_status = await asyncio.gather(
asyncio.to_thread(get_gpu_info),
_await_or_default(
@@ -2043,9 +2145,10 @@ async def list_models(api_key: str = Depends(verify_api_key)):
# what serves chat. The installer refuses URLs with credentials.
payload["externalModel"] = read_env_value("EXTERNAL_LLM_MODEL", INSTALL_DIR).strip() or None
payload["externalHost"] = _external_api_host(read_env_value("EXTERNAL_LLM_URL", INSTALL_DIR))
- payload["hostRuntime"] = _windows_hosted_runtime()
- if payload["hostRuntime"]:
- payload["modelManagement"] = await asyncio.to_thread(_model_management)
+ payload["hostRuntime"] = host_runtime
+ if host_runtime:
+ payload["modelManagement"] = await _catalog_model_management(
+ management_prefetch, agent_status, loaded_model)
# A held transition does not establish which route currently serves chat.
payload["activeRuntime"] = (
None if payload.get("modelRecoveryPending") or (payload.get("modelLifecycle") or {}).get("active")
@@ -2905,7 +3008,11 @@ def recover_model_switch(body: dict | None = Body(default=None), api_key: str =
if body is None or not any(body == allowed and all(type(body[key]) is type(value) for key, value in allowed.items())
for allowed in _RECOVERY_REQUESTS):
raise HTTPException(status_code=400, detail='Recovery accepts {} or {"releaseUnverified": true} only.')
- value = _model_recovery_request('POST', dict(body))
+ try:
+ value = _model_recovery_request('POST', dict(body))
+ finally:
+ # Recovery can restore the earlier runtime.
+ _forget_model_management()
return value if isinstance(value, JSONResponse) else JSONResponse(value, headers={'Cache-Control': 'no-store'})
@@ -2940,6 +3047,7 @@ def manage_model_runtime(operation: str, body: dict | None = Body(default=None),
value = _call_agent_model(f"/v1/model/runtime/{operation}", {}, timeout=1200)
finally:
_invalidate_agent_model_status_cache()
+ _forget_model_management()
expected = "started" if operation == "start" else "stopped"
if not isinstance(value, dict) or value.get("status") != expected:
raise HTTPException(status_code=502, detail="The runtime operation was not confirmed; refresh its status")
@@ -3084,6 +3192,7 @@ def _activate_model(model_id: str, body: dict[str, Any] | None, *, chat_template
# lifecycle as active. Drop it so the dashboard's confirming poll,
# sent as soon as this response lands, sees the settled state.
_invalidate_agent_model_status_cache()
+ _forget_model_management()
return result
diff --git a/ods/extensions/services/dashboard-api/tests/conftest.py b/ods/extensions/services/dashboard-api/tests/conftest.py
index 1ab6628a0..c30f718ee 100644
--- a/ods/extensions/services/dashboard-api/tests/conftest.py
+++ b/ods/extensions/services/dashboard-api/tests/conftest.py
@@ -67,6 +67,16 @@ def _no_remembered_portal_status(monkeypatch):
monkeypatch.setattr(pixel, "_last_available_status", None)
[email protected](autouse=True)
+def _no_remembered_model_management(monkeypatch):
+ """Each test starts without a model management proof remembered by an earlier one."""
+ from routers import models
+
+ monkeypatch.setattr(models, "_model_management_remembered", None)
+ monkeypatch.setattr(models, "_model_management_proof_future", None)
+ monkeypatch.setattr(models, "_model_management_missed", False)
+
+
@pytest.fixture(autouse=True)
def _no_hugging_face_header_reads(monkeypatch):
"""Unit tests never reach Hugging Face; a test that needs a header stubs one."""
diff --git a/ods/extensions/services/dashboard-api/tests/test_model_management.py b/ods/extensions/services/dashboard-api/tests/test_model_management.py
index d6ef5053c..c01e58d8c 100644
--- a/ods/extensions/services/dashboard-api/tests/test_model_management.py
+++ b/ods/extensions/services/dashboard-api/tests/test_model_management.py
@@ -2,13 +2,17 @@
from __future__ import annotations
+import asyncio
import json
+import threading
+import time
from unittest.mock import AsyncMock
import pytest
MANAGED = {"managed": True, "canActivate": True, "canUnload": True, "running": True}
+STOPPED = {**MANAGED, "canActivate": False, "running": False}
UNMANAGED = {key: False for key in MANAGED}
UNVERIFIED = {**UNMANAGED, "managed": None, "reason": "Runtime management could not be verified"}
@@ -280,3 +284,232 @@ def test_runtime_control_preserves_failure_and_invalidates_status(
assert response.status_code == status
assert response.json()["detail"] == detail
assert invalidated == [True]
+
+
+def _recording(calls, answers):
+ """A host-agent stub that records each path and answers with the latest of ``answers``."""
+ def request(_method, path, **_kwargs):
+ calls.append(path)
+ return answers[-1]
+ return request
+
+
+def _catalog_management(test_client):
+ response = test_client.get("/api/models", headers=test_client.auth_headers)
+ assert response.status_code == 200
+ return response.json()["modelManagement"]
+
+
+def test_catalog_reuses_a_proof_the_live_runtime_agrees_with(test_client, model_runtime, monkeypatch):
+ calls: list[str] = []
+ monkeypatch.setattr(model_runtime, "request_agent_json", _recording(calls, [MANAGED]))
+ monkeypatch.setattr(model_runtime, "get_loaded_model", AsyncMock(return_value="fixture"))
+ assert [_catalog_management(test_client) for _ in range(3)] == [MANAGED] * 3
+ assert calls == ["/v1/model/management"]
+
+
+def test_catalog_proves_again_when_the_live_runtime_disagrees(test_client, model_runtime, monkeypatch):
+ calls: list[str] = []
+ answers = [MANAGED]
+ monkeypatch.setattr(model_runtime, "request_agent_json", _recording(calls, answers))
+ loaded = AsyncMock(return_value="fixture")
+ monkeypatch.setattr(model_runtime, "get_loaded_model", loaded)
+ assert _catalog_management(test_client) == MANAGED
+ # The model stopped answering: a crash, or a stop made outside this dashboard.
+ loaded.return_value = None
+ answers.append(STOPPED)
+ assert _catalog_management(test_client) == STOPPED
+ assert _catalog_management(test_client) == STOPPED
+ assert calls == ["/v1/model/management"] * 2
+
+
[email protected]("status", [
+ {"status": "idle", "lifecycleActive": True, "activeOperation": "model_runtime"},
+ {"status": "idle", "modelTransactionPending": True},
+])
+def test_owned_lifecycle_operations_always_get_a_fresh_proof(test_client, model_runtime, monkeypatch, status):
+ calls: list[str] = []
+ monkeypatch.setattr(model_runtime, "request_agent_json", _recording(calls, [MANAGED]))
+ monkeypatch.setattr(model_runtime, "get_loaded_model", AsyncMock(return_value="fixture"))
+ _catalog_management(test_client)
+ monkeypatch.setattr(model_runtime, "_get_agent_model_status", lambda: status)
+ _catalog_management(test_client)
+ _catalog_management(test_client)
+ assert calls == ["/v1/model/management"] * 3
+
+
+def test_runtime_and_recovery_requests_forget_the_proof(test_client, model_runtime, monkeypatch):
+ calls = []
+
+ def request(method, path, **_kwargs):
+ calls.append(path)
+ if path == "/v1/model/recover":
+ raise model_runtime.AgentUnavailable("fixture unavailable")
+ return {"status": "stopped"} if method == "POST" else MANAGED
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ monkeypatch.setattr(model_runtime, "get_loaded_model", AsyncMock(return_value="fixture"))
+ _catalog_management(test_client)
+ assert test_client.post("/api/models/runtime/stop", headers=test_client.auth_headers, json={}).status_code == 200
+ _catalog_management(test_client)
+ assert test_client.post("/api/models/recovery", headers=test_client.auth_headers, json={}).status_code == 503
+ _catalog_management(test_client)
+ assert calls == ["/v1/model/management", "/v1/model/runtime/stop", "/v1/model/management",
+ "/v1/model/recover", "/v1/model/management"]
+
+
+def test_catalog_proves_ownership_alongside_the_other_reads(test_client, model_runtime, monkeypatch):
+ proving = threading.Event()
+ seen = []
+
+ def request(_method, _path, **_kwargs):
+ proving.set()
+ return MANAGED
+
+ async def discover():
+ # Taken one after the other, the proof would only start after this returned.
+ for _ in range(500):
+ if proving.is_set():
+ break
+ await asyncio.sleep(0.01)
+ seen.append(proving.is_set())
+ return "fixture"
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ monkeypatch.setattr(model_runtime, "get_loaded_model", discover)
+ assert _catalog_management(test_client) == MANAGED
+ assert seen == [True]
+
+
+def test_older_proof_is_shown_while_a_fresh_one_is_taken_in_the_background(model_runtime, monkeypatch):
+ fresh = {**MANAGED, "reason": "fresh"}
+ calls: list[str] = []
+ monkeypatch.setattr(model_runtime, "request_agent_json", _recording(calls, [fresh]))
+ monkeypatch.setattr(model_runtime, "_model_management_remembered", (time.monotonic() - 20, MANAGED))
+
+ async def reads():
+ shown = await model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture")
+ await model_runtime._model_management_proof_future
+ return shown, await model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture")
+
+ assert asyncio.run(reads()) == (MANAGED, fresh)
+ assert calls == ["/v1/model/management"]
+
+
+def test_catalog_never_reuses_a_proof_past_its_age_limit(model_runtime, monkeypatch):
+ calls: list[str] = []
+ monkeypatch.setattr(model_runtime, "request_agent_json", _recording(calls, [STOPPED]))
+ expired = time.monotonic() - model_runtime._MODEL_MANAGEMENT_MAX_AGE_SECONDS - 1
+ monkeypatch.setattr(model_runtime, "_model_management_remembered", (expired, STOPPED))
+ assert asyncio.run(model_runtime._catalog_model_management(None, {"status": "idle"}, None)) == STOPPED
+ assert calls == ["/v1/model/management"]
+
+
+def test_concurrent_catalog_reads_share_one_proof(model_runtime, monkeypatch):
+ calls = []
+
+ def request(_method, path, **_kwargs):
+ calls.append(path)
+ time.sleep(0.05)
+ return MANAGED
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+
+ async def reads():
+ return await asyncio.gather(*(
+ model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture") for _ in range(3)))
+
+ assert asyncio.run(reads()) == [MANAGED] * 3
+ assert calls == ["/v1/model/management"]
+
+
+def test_a_proof_overtaken_by_a_runtime_change_is_taken_again(model_runtime, monkeypatch):
+ calls = []
+
+ def request(_method, path, **_kwargs):
+ calls.append(path)
+ if len(calls) == 1:
+ # The runtime changes while the first proof is still running.
+ model_runtime._forget_model_management()
+ return MANAGED
+ return STOPPED
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ assert asyncio.run(model_runtime._catalog_model_management(None, {"status": "idle"}, None)) == STOPPED
+ assert calls == ["/v1/model/management"] * 2
+ assert model_runtime._model_management_remembered[1] == STOPPED
+
+
+def test_a_host_that_keeps_failing_stops_hiding_behind_the_last_good_proof(model_runtime, monkeypatch):
+ calls = []
+
+ def request(_method, path, **_kwargs):
+ calls.append(path)
+ raise model_runtime.AgentHTTPError(503, "fixture ownership changed")
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ monkeypatch.setattr(model_runtime, "_model_management_remembered", (time.monotonic() - 20, MANAGED))
+
+ async def reads():
+ shown = []
+ for _ in range(2):
+ shown.append(await model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture"))
+ await model_runtime._model_management_proof_future
+ shown.append(await model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture"))
+ return shown
+
+ assert asyncio.run(reads()) == [MANAGED, MANAGED, UNVERIFIED]
+ assert model_runtime._model_management_remembered is None
+ assert calls == ["/v1/model/management"] * 3
+
+
+def test_one_unverified_proof_keeps_the_last_good_one(model_runtime, monkeypatch):
+ answers = [MANAGED, "fail", "fail"]
+
+ def request(_method, _path, **_kwargs):
+ answer = answers.pop()
+ if answer == "fail":
+ raise model_runtime.AgentHTTPError(503, "fixture interop timeout")
+ return answer
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ monkeypatch.setattr(model_runtime, "_model_management_remembered", (time.monotonic() - 20, MANAGED))
+
+ async def refresh():
+ await model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture")
+ await model_runtime._model_management_proof_future
+
+ asyncio.run(refresh()) # one miss: kept
+ assert model_runtime._model_management_remembered[1] == MANAGED
+ monkeypatch.setattr(model_runtime, "_model_management_remembered", (time.monotonic() - 20, MANAGED))
+ asyncio.run(refresh()) # a second miss in a row: dropped
+ # getattr: a narrowed attribute would stay None for mypy after the next read.
+ assert getattr(model_runtime, "_model_management_remembered") is None
+ asyncio.run(model_runtime._catalog_model_management(None, {"status": "idle"}, "fixture"))
+ # A good proof ends the run of misses.
+ assert model_runtime._model_management_remembered[1] == MANAGED
+ assert model_runtime._model_management_missed is False
+
+
+def test_activation_forgets_the_proof(test_client, model_runtime, monkeypatch):
+ calls = []
+
+ def request(method, path, **kwargs):
+ calls.append(path)
+ if method == "GET":
+ return MANAGED
+ return {"status": "activated", "context_length": kwargs["payload"]["context_length"]}
+
+ monkeypatch.setattr(model_runtime, "request_agent_json", request)
+ monkeypatch.setattr(model_runtime, "get_loaded_model", AsyncMock(return_value="fixture"))
+ monkeypatch.setattr(model_runtime, "_find_loadable_model", lambda _id: {"id": "fixture", "gguf_file": "fixture.gguf"})
+ monkeypatch.setattr(model_runtime, "_already_active_model", lambda *_args: (False, None))
+ monkeypatch.setattr(model_runtime, "_bootstrap_upgrade_download_conflict", lambda: None)
+ _catalog_management(test_client)
+ response = test_client.post(
+ "/api/models/fixture/load", headers=test_client.auth_headers, json={"context_length": 8192},
+ )
+ assert response.status_code == 200
+ _catalog_management(test_client)
+ # The load's own ownership proof is fresh, never the catalog's remembered one.
+ assert calls == ["/v1/model/management", "/v1/model/management", "/v1/model/activate", "/v1/model/management"]
diff --git a/ods/extensions/services/dashboard/src/hooks/useModels.js b/ods/extensions/services/dashboard/src/hooks/useModels.js
index 36c03764c..fb334b655 100644
--- a/ods/extensions/services/dashboard/src/hooks/useModels.js
+++ b/ods/extensions/services/dashboard/src/hooks/useModels.js
@@ -265,28 +265,72 @@ function waitForActivationPoll(delay, signal, wake = null) {
})
}
-export function useModels({observe=true} = {}) {
- const [models, setModels] = useState(USE_MOCK_DATA ? getMockModels() : [])
- const [gpu, setGpu] = useState(USE_MOCK_DATA ? MOCK_GPU : null)
- const [currentModel, setCurrentModel] = useState(USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
- const [loadedModel, setLoadedModel] = useState(USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
- const [activationReadyModel, setActivationReadyModel] = useState(USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
- const [configuredModel, setConfiguredModel] = useState(USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
- const [modelLifecycle, setModelLifecycle] = useState(null)
- const [modelActivation, setModelActivation] = useState(null)
- const [modelRecoveryPending, setModelRecoveryPending] = useState(false)
- const [odsMode, setOdsMode] = useState(USE_MOCK_DATA ? MOCK_MODES.odsMode : 'unknown')
- const [configuredMode, setConfiguredMode] = useState(USE_MOCK_DATA ? MOCK_MODES.configuredMode : 'unknown')
- const [llmBackend, setLlmBackend] = useState(USE_MOCK_DATA ? 'llama-server' : 'unknown')
- const [hostRuntime, setHostRuntime] = useState(false)
- const [externalApi, setExternalApi] = useState(null)
- const [activeRuntime, setActiveRuntime] = useState(null)
- const [modelManagement, setModelManagement] = useState(() => normalizeModelManagement(null))
+// One /api/models answer as the hook's state.
+function catalogState(data) {
+ return {
+ models: data.models,
+ gpu: data.gpu,
+ currentModel: data.currentModel,
+ loadedModel: data.loadedModel ?? null,
+ activationReadyModel: data.activationReadyModel ?? null,
+ configuredModel: data.configuredModel ?? null,
+ modelLifecycle: normalizeModelLifecycle(data.modelLifecycle),
+ modelActivation: modelActivationStatus(data.modelActivation),
+ modelRecoveryPending: data.modelRecoveryPending === true,
+ odsMode: normalizeOdsMode(data.odsMode),
+ configuredMode: normalizeOdsMode(data.configuredMode ?? data.odsMode),
+ llmBackend: typeof data.llmBackend === 'string' ? data.llmBackend.trim().toLowerCase() : 'unknown',
+ // API mode: the model and host serving chat (never the key).
+ externalApi: String(data.llmBackend || '').trim().toLowerCase() === 'external'
+ ? {
+ model: typeof data.externalModel === 'string' && data.externalModel ? data.externalModel : null,
+ host: typeof data.externalHost === 'string' && data.externalHost ? data.externalHost : null,
+ }
+ : null,
+ hostRuntime: data.hostRuntime === true,
+ activeRuntime: normalizeActiveRuntime(data.activeRuntime),
+ modelManagement: normalizeModelManagement(data.modelManagement),
+ recommendationAlternatives: data.recommendationAlternatives ?? [],
+ hermesMinimumContext: Number(data.hermesMinimumContext || DEFAULT_HERMES_MIN_CONTEXT),
+ pixelMinimumContext: Number(data.pixelMinimumContext || DEFAULT_PIXEL_MIN_CONTEXT),
+ }
+}
+
+// The last catalog this page load read. The catalog can take seconds to
+// answer, so a Models page opened again starts from it instead of an empty
+// skeleton, holding its actions until its own first read lands.
+let lastCatalog = null
+
+export function forgetLastCatalog() {
+ lastCatalog = null
+}
+
+export function useModels({observe=true, reuseLastCatalog=false} = {}) {
+ // Never activation readback: confirmation waits for this mount's own reads.
+ const [seed] = useState(() => (reuseLastCatalog && observe && !USE_MOCK_DATA && lastCatalog
+ ? catalogState(lastCatalog) : null))
+ const [models, setModels] = useState(seed ? seed.models : USE_MOCK_DATA ? getMockModels() : [])
+ const [gpu, setGpu] = useState(seed ? seed.gpu : USE_MOCK_DATA ? MOCK_GPU : null)
+ const [currentModel, setCurrentModel] = useState(seed ? seed.currentModel : USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
+ const [loadedModel, setLoadedModel] = useState(seed ? seed.loadedModel : USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
+ const [activationReadyModel, setActivationReadyModel] = useState(seed ? seed.activationReadyModel : USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
+ const [configuredModel, setConfiguredModel] = useState(seed ? seed.configuredModel : USE_MOCK_DATA ? MOCK_CURRENT_MODEL : null)
+ const [modelLifecycle, setModelLifecycle] = useState(seed ? seed.modelLifecycle : null)
+ const [modelActivation, setModelActivation] = useState(seed ? seed.modelActivation : null)
+ const [modelRecoveryPending, setModelRecoveryPending] = useState(seed ? seed.modelRecoveryPending : false)
+ const [odsMode, setOdsMode] = useState(seed ? seed.odsMode : USE_MOCK_DATA ? MOCK_MODES.odsMode : 'unknown')
+ const [configuredMode, setConfiguredMode] = useState(seed ? seed.configuredMode : USE_MOCK_DATA ? MOCK_MODES.configuredMode : 'unknown')
+ const [llmBackend, setLlmBackend] = useState(seed ? seed.llmBackend : USE_MOCK_DATA ? 'llama-server' : 'unknown')
+ const [hostRuntime, setHostRuntime] = useState(seed ? seed.hostRuntime : false)
+ const [externalApi, setExternalApi] = useState(seed ? seed.externalApi : null)
+ const [activeRuntime, setActiveRuntime] = useState(seed ? seed.activeRuntime : null)
+ const [modelManagement, setModelManagement] = useState(() => (seed ? seed.modelManagement : normalizeModelManagement(null)))
const [runtimeActionLoading, setRuntimeActionLoading] = useState(null)
- const [recommendationAlternatives, setRecommendationAlternatives] = useState([])
- const [hermesMinimumContext, setHermesMinimumContext] = useState(DEFAULT_HERMES_MIN_CONTEXT)
- const [pixelMinimumContext, setPixelMinimumContext] = useState(DEFAULT_PIXEL_MIN_CONTEXT)
- const [loading, setLoading] = useState(USE_MOCK_DATA ? false : true)
+ const [recommendationAlternatives, setRecommendationAlternatives] = useState(seed ? seed.recommendationAlternatives : [])
+ const [hermesMinimumContext, setHermesMinimumContext] = useState(seed ? seed.hermesMinimumContext : DEFAULT_HERMES_MIN_CONTEXT)
+ const [pixelMinimumContext, setPixelMinimumContext] = useState(seed ? seed.pixelMinimumContext : DEFAULT_PIXEL_MIN_CONTEXT)
+ const [loading, setLoading] = useState(USE_MOCK_DATA ? false : !seed)
+ const [catalogConfirmed, setCatalogConfirmed] = useState(!seed)
const [fetchError, setFetchError] = useState(null)
const [mutationError, setMutationError] = useState(null)
const clearMutationError = useCallback(() => setMutationError(null), [])
@@ -371,34 +415,30 @@ export function useModels({observe=true} = {}) {
if (requestId < latestSettledModelsRequestRef.current) return data
latestSettledModelsRequestRef.current = requestId
latestModelsSnapshotRef.current = data
-
- setModels(data.models)
- setGpu(data.gpu)
- setCurrentModel(data.currentModel)
- setLoadedModel(data.loadedModel ?? null)
- setActivationReadyModel(data.activationReadyModel ?? null)
- setConfiguredModel(data.configuredModel ?? null)
- setModelLifecycle(normalizeModelLifecycle(data.modelLifecycle))
- setModelActivation(modelActivationStatus(data.modelActivation))
- setModelRecoveryPending(data.modelRecoveryPending === true)
- const effectiveMode = normalizeOdsMode(data.odsMode)
- setOdsMode(effectiveMode)
- setConfiguredMode(normalizeOdsMode(data.configuredMode ?? data.odsMode))
- setLlmBackend(typeof data.llmBackend === 'string' ? data.llmBackend.trim().toLowerCase() : 'unknown')
- // API mode: the model and host serving chat (never the key).
- setExternalApi(String(data.llmBackend || '').trim().toLowerCase() === 'external'
- ? {
- model: typeof data.externalModel === 'string' && data.externalModel ? data.externalModel : null,
- host: typeof data.externalHost === 'string' && data.externalHost ? data.externalHost : null,
- }
- : null)
- setHostRuntime(data.hostRuntime === true)
- setActiveRuntime(normalizeActiveRuntime(data.activeRuntime))
- setModelManagement(normalizeModelManagement(data.modelManagement))
- setRecommendationAlternatives(data.recommendationAlternatives ?? [])
- setHermesMinimumContext(Number(data.hermesMinimumContext || DEFAULT_HERMES_MIN_CONTEXT))
- setPixelMinimumContext(Number(data.pixelMinimumContext || DEFAULT_PIXEL_MIN_CONTEXT))
+ lastCatalog = data
+
+ const next = catalogState(data)
+ setModels(next.models)
+ setGpu(next.gpu)
+ setCurrentModel(next.currentModel)
+ setLoadedModel(next.loadedModel)
+ setActivationReadyModel(next.activationReadyModel)
+ setConfiguredModel(next.configuredModel)
+ setModelLifecycle(next.modelLifecycle)
+ setModelActivation(next.modelActivation)
+ setModelRecoveryPending(next.modelRecoveryPending)
+ setOdsMode(next.odsMode)
+ setConfiguredMode(next.configuredMode)
+ setLlmBackend(next.llmBackend)
+ setExternalApi(next.externalApi)
+ setHostRuntime(next.hostRuntime)
+ setActiveRuntime(next.activeRuntime)
+ setModelManagement(next.modelManagement)
+ setRecommendationAlternatives(next.recommendationAlternatives)
+ setHermesMinimumContext(next.hermesMinimumContext)
+ setPixelMinimumContext(next.pixelMinimumContext)
setFetchError(null)
+ setCatalogConfirmed(true)
reconcilePendingActions(data.models)
return data
} catch (err) {
@@ -750,6 +790,7 @@ export function useModels({observe=true} = {}) {
hermesMinimumContext,
pixelMinimumContext,
loading,
+ catalogConfirmed,
error,
actionLoading,
actionLoadingModels,
diff --git a/ods/extensions/services/dashboard/src/hooks/useModels.test.jsx b/ods/extensions/services/dashboard/src/hooks/useModels.test.jsx
index 0b3dd66b8..d8962dabe 100644
--- a/ods/extensions/services/dashboard/src/hooks/useModels.test.jsx
+++ b/ods/extensions/services/dashboard/src/hooks/useModels.test.jsx
@@ -1,5 +1,5 @@
import { act, renderHook } from '@testing-library/react'
-import { useModels } from './useModels'
+import { forgetLastCatalog, useModels } from './useModels'
const reply = (status, body) => ({ ok: status >= 200 && status < 300, status, json: async () => body })
const activating = { active: true, operation: 'model_activation', target: 'model-b' }
@@ -81,3 +81,65 @@ test('a load request still in flight keeps waiting for its answer', async () =>
expect(view.result.current.activationLoading).toBe('model-b')
view.unmount()
})
+
+test('a caller that asks starts from the last catalog and holds actions until its own read lands', async () => {
+ forgetLastCatalog()
+ server = { current: 'model-a', lifecycle: null }
+ let pending = null
+ vi.stubGlobal('fetch', vi.fn(async url => {
+ if (url !== '/api/models') throw new Error(`unexpected request ${url}`)
+ return pending || reply(200, snapshot())
+ }))
+ const first = renderHook(() => useModels({ reuseLastCatalog: true }))
+ expect(first.result.current.loading).toBe(true)
+ await act(async () => { await vi.advanceTimersByTimeAsync(0) })
+ expect(first.result.current.currentModel).toBe('model-a')
+ expect(first.result.current.catalogConfirmed).toBe(true)
+ first.unmount()
+
+ let release
+ pending = new Promise(resolve => { release = resolve })
+ server.current = 'model-b'
+ const again = renderHook(() => useModels({ reuseLastCatalog: true }))
+ expect(again.result.current.loading).toBe(false)
+ expect(again.result.current.currentModel).toBe('model-a')
+ expect(again.result.current.catalogConfirmed).toBe(false)
+
+ await act(async () => {
+ release(reply(200, snapshot()))
+ await vi.advanceTimersByTimeAsync(0)
+ })
+ expect(again.result.current.currentModel).toBe('model-b')
+ expect(again.result.current.catalogConfirmed).toBe(true)
+})
+
+test('a failed fresh read keeps the shown catalog held', async () => {
+ forgetLastCatalog()
+ server = { current: 'model-a', lifecycle: null }
+ let fail = false
+ vi.stubGlobal('fetch', vi.fn(async () => (fail ? reply(503, {}) : reply(200, snapshot()))))
+ const first = renderHook(() => useModels({ reuseLastCatalog: true }))
+ await act(async () => { await vi.advanceTimersByTimeAsync(0) })
+ first.unmount()
+
+ fail = true
+ const again = renderHook(() => useModels({ reuseLastCatalog: true }))
+ await act(async () => { await vi.advanceTimersByTimeAsync(0) })
+ expect(again.result.current.error).toBe('Failed to fetch models')
+ expect(again.result.current.currentModel).toBe('model-a')
+ expect(again.result.current.catalogConfirmed).toBe(false)
+})
+
+test('a caller that does not ask still starts empty', async () => {
+ forgetLastCatalog()
+ server = { current: 'model-a', lifecycle: null }
+ vi.stubGlobal('fetch', vi.fn(async () => reply(200, snapshot())))
+ const first = renderHook(() => useModels())
+ await act(async () => { await vi.advanceTimersByTimeAsync(0) })
+ first.unmount()
+
+ const other = renderHook(() => useModels())
+ expect(other.result.current.loading).toBe(true)
+ expect(other.result.current.models).toEqual([])
+ expect(other.result.current.catalogConfirmed).toBe(true)
+})
diff --git a/ods/extensions/services/dashboard/src/pages/Models.jsx b/ods/extensions/services/dashboard/src/pages/Models.jsx
index d33257f3f..11deaef6b 100644
--- a/ods/extensions/services/dashboard/src/pages/Models.jsx
+++ b/ods/extensions/services/dashboard/src/pages/Models.jsx
@@ -89,6 +89,7 @@ export default function Models({ compact = false }) {
hermesMinimumContext,
pixelMinimumContext,
loading,
+ catalogConfirmed,
error,
actionLoading,
actionLoadingModels,
@@ -98,7 +99,9 @@ export default function Models({ compact = false }) {
benchmarkModel,
deleteModel,
refresh,
- } = useModels()
+ } = useModels({ reuseLastCatalog: true })
+ // Opened again, the page shows the last catalog at once; actions wait for its fresh read.
+ const catalogHeld = catalogConfirmed === false
const [downloadStarting, setDownloadStarting] = useState(null)
const [downloadAwaitingStatus, setDownloadAwaitingStatus] = useState(false)
@@ -236,7 +239,7 @@ export default function Models({ compact = false }) {
await deleteModel(modelId)
}
- const activationBusy = Boolean(activationLoading || runtimeActionLoading || modelLifecycle?.active || modelRecoveryPending)
+ const activationBusy = Boolean(catalogHeld || activationLoading || runtimeActionLoading || modelLifecycle?.active || modelRecoveryPending)
const providerBusy = Boolean(modelLifecycle?.active && ['remote_provider_apply', 'remote_provider_proof'].includes(modelLifecycle.operation))
const activeRemoteRuntime = !activationBusy && activeRuntime?.source === 'remote-provider' ? activeRuntime : null
const remoteRouteActive = Boolean(activeRemoteRuntime)
@@ -259,7 +262,7 @@ export default function Models({ compact = false }) {
}
const pendingModelActions = actionLoadingModels ?? (actionLoading ? [actionLoading] : [])
- const runtimeControlsBusy = Boolean(runtimeActionLoading || activationLoading || modelLifecycle?.active || pendingModelActions.length || downloadProgress.isDownloading)
+ const runtimeControlsBusy = Boolean(catalogHeld || runtimeActionLoading || activationLoading || modelLifecycle?.active || pendingModelActions.length || downloadProgress.isDownloading)
// Unloading stops Portal and Hermes until Resume, so it asks first. The
// question closes for good if the model stops or another operation starts.
const unloadAvailable = Boolean(modelManagement?.managed === true && modelManagement.canUnload === true
@@ -285,7 +288,7 @@ export default function Models({ compact = false }) {
hermesMinimumContext={hermesMinimumContext} pixelMinimumContext={pixelMinimumContext}
isCurrentModel={model.id === currentModel} isLoading={pendingModelActions.includes(model.id)}
loadBusy={pendingModelActions.length > 0} activationBusy={activationBusy}
- downloadBusy={downloadProgress.isDownloading || !!downloadStarting} downloadStarting={downloadStarting === model.id}
+ downloadBusy={catalogHeld || downloadProgress.isDownloading || !!downloadStarting} downloadStarting={downloadStarting === model.id}
onDownload={() => handleDownload(model.id)} onLoad={() => { if (canActivateModels && !activationBusy) setActivationConfigModel(model) }}
onBenchmark={() => benchmarkModel(model.id)} onDelete={() => setDeleteConfirmModel(model)}/>
@@ -342,7 +345,7 @@ export default function Models({ compact = false }) {
<ModelActivationNotice value={modelActivation} pending={modelLifecycle?.operation === 'model_activation'} onRefresh={refresh}/>
{providerBusy && <p role="status">ODS is checking or changing the model connection. Wait for it to finish before switching models or requesting recovery.</p>}
- <PortalModelRecovery active={Boolean(modelRecoveryPending || modelActivation?.outcome === 'rollback_unconfirmed') && !modelLifecycle?.active} operationActive={Boolean(modelLifecycle?.active)} refreshKey={modelActivation} onRecovered={refresh}/>
+ <PortalModelRecovery active={Boolean(modelRecoveryPending || modelActivation?.outcome === 'rollback_unconfirmed') && !modelLifecycle?.active && !catalogHeld} operationActive={Boolean(modelLifecycle?.active)} refreshKey={modelActivation} onRecovered={refresh}/>
{error && (
<div className="mb-5 rounded-xl border border-red-500/30 bg-red-500/10 p-4 text-sm text-red-300">
@@ -373,7 +376,7 @@ export default function Models({ compact = false }) {
<DownloadProgressBar
progress={visibleDownloadProgress}
helpers={downloadProgress}
- onRetry={retryModelId
+ onRetry={retryModelId && !catalogHeld
? () => handleDownload(retryModelId)
: null}
/>
@@ -402,7 +405,7 @@ export default function Models({ compact = false }) {
gpu={gpu}
externalApi={externalApi}
activeRuntime={activeRemoteRuntime}
- showProfile={canActivateModels}
+ showProfile={canActivateModels && !catalogHeld}
lifecycleActive={Boolean(modelLifecycle?.active || modelActivation?.active || activationLoading || runtimeActionLoading)}
/>
@@ -439,7 +442,7 @@ export default function Models({ compact = false }) {
>
<HuggingFaceModelBrowser
gpu={gpu}
- downloadBusy={downloadProgress.isDownloading || Boolean(downloadStarting || runtimeActionLoading)}
+ downloadBusy={catalogHeld || downloadProgress.isDownloading || Boolean(downloadStarting || runtimeActionLoading)}
onImportStarted={handleHuggingFaceImportStarted}
/>
</section>
diff --git a/ods/extensions/services/dashboard/src/pages/Models.navigation.test.jsx b/ods/extensions/services/dashboard/src/pages/Models.navigation.test.jsx
index 7ac56dde0..102ac8f16 100644
--- a/ods/extensions/services/dashboard/src/pages/Models.navigation.test.jsx
+++ b/ods/extensions/services/dashboard/src/pages/Models.navigation.test.jsx
@@ -2,6 +2,7 @@ import { StrictMode } from 'react'
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import Models from './Models'
+import { forgetLastCatalog } from '../hooks/useModels'
// Keep the production inventory/activation hook and page together. Downloads
// are a separate transport and are not involved in these navigation cases.
@@ -38,6 +39,7 @@ async function startActivation() {
}
beforeEach(() => {
+ forgetLastCatalog()
vi.useFakeTimers()
Object.defineProperty(document, 'hidden', { configurable: true, value: false })
vi.stubGlobal('fetch', vi.fn((url, options) => {
@@ -196,3 +198,30 @@ it('reads backend activation on return without replaying the POST', async () =>
await act(async () => { await vi.advanceTimersByTimeAsync(620000) })
expect(reads()).toHaveLength(atNavigation)
})
+
+it('offers recovery from a remembered catalog only after its own read confirms it', async () => {
+ const held = { ok: true, json: async () => ({ pending: true, phase: 'held', transactionId: 'a'.repeat(64) }) }
+ const pendingCatalog = async () => {
+ const payload = await response(false).json()
+ return { ok: true, json: async () => ({ ...payload, modelRecoveryPending: true,
+ modelActivation: { active: false, outcome: 'rollback_unconfirmed', failureCode: 'rollback_unconfirmed' } }) }
+ }
+ vi.stubGlobal('fetch', vi.fn(async url => (url === '/api/models/recovery' ? held : pendingCatalog())))
+ const first = await openModels()
+ expect(screen.getByRole('button', { name: 'Recover model switch' })).toBeEnabled()
+ first.unmount()
+
+ let release
+ fetch.mockImplementation(url => (url === '/api/models/recovery'
+ ? Promise.resolve(held)
+ : new Promise(resolve => { release = () => resolve(pendingCatalog()) })))
+ render(<StrictMode><MemoryRouter><Models compact /></MemoryRouter></StrictMode>)
+ await act(async () => {})
+ expect(screen.queryByRole('button', { name: 'Recover model switch' })).toBeNull()
+ expect(screen.getByRole('button', { name: 'Run' })).toBeDisabled()
+
+ await act(async () => { release() })
+ await act(async () => {})
+ expect(screen.getByRole('button', { name: 'Recover model switch' })).toBeEnabled()
+ expect(posts()).toHaveLength(0)
+})
diff --git a/ods/extensions/services/dashboard/src/pages/Models.unload-confirm.test.jsx b/ods/extensions/services/dashboard/src/pages/Models.unload-confirm.test.jsx
index 59852a3b1..4cf57755d 100644
--- a/ods/extensions/services/dashboard/src/pages/Models.unload-confirm.test.jsx
+++ b/ods/extensions/services/dashboard/src/pages/Models.unload-confirm.test.jsx
@@ -1,7 +1,8 @@
import { StrictMode, createElement } from 'react'
-import { fireEvent, render, screen, within } from '@testing-library/react'
+import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import Models from './Models'
+import { forgetLastCatalog } from '../hooks/useModels'
// The production model hook drives the page, so these tests see the requests
// the runtime controls actually send. Downloads are a separate transport.
@@ -29,6 +30,7 @@ const catalog = () => ({
})
beforeEach(() => {
+ forgetLastCatalog()
vi.stubGlobal('fetch', vi.fn(async (url, options) => {
if (url === '/api/models') return json(catalog())
if (options?.method === 'POST' && url === '/api/models/runtime/stop') {
@@ -83,3 +85,29 @@ test('Resume model starts a stopped runtime without asking', async () => {
expect(await controls.findByRole('button', { name: 'Unload model' })).toBeEnabled()
expect(posts().map(([url]) => url)).toEqual(['/api/models/runtime/start'])
})
+
+test('Models opened again shows the last catalog at once and holds its controls until the fresh read', async () => {
+ running = true
+ const first = await openRuntimeControls()
+ expect(first.getByRole('button', { name: 'Unload model' })).toBeEnabled()
+ cleanup()
+
+ let answer
+ fetch.mockImplementation(async url => {
+ if (url === '/api/models') return new Promise(resolve => { answer = () => resolve(json(catalog())) })
+ return json({}, 404)
+ })
+ const profileReads = () => fetch.mock.calls.filter(([url]) => String(url).endsWith('/profile')).length
+ const profilesBefore = profileReads()
+ render(createElement(MemoryRouter, null, createElement(Models)))
+ const controls = within(screen.getByRole('region', { name: 'Model runtime controls' }))
+ expect(controls.getByRole('button', { name: 'Unload model' })).toBeDisabled()
+ // The remembered model's profile, and its template button, wait for the fresh read too.
+ await act(async () => {})
+ expect(profileReads()).toBe(profilesBefore)
+
+ await act(async () => { answer() })
+ expect(await controls.findByRole('button', { name: 'Unload model' })).toBeEnabled()
+ expect(profileReads()).toBeGreaterThan(profilesBefore)
+ expect(posts()).toEqual([])
+})