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(portal): say the model is unloaded instead of switching (#8182)

Mike Bradley7eb790b1439c
diff --git a/ods/extensions/services/dashboard-api/routers/pixel.py b/ods/extensions/services/dashboard-api/routers/pixel.py
index 41e6d923b..5fa14735b 100644
--- a/ods/extensions/services/dashboard-api/routers/pixel.py
+++ b/ods/extensions/services/dashboard-api/routers/pixel.py
@@ -72,6 +72,12 @@ _OPS_STATUSES = frozenset(
)
_CONTROL = re.compile(r"[\x00-\x1f\x7f-\x9f]")
_MODEL_SWITCH_DETAIL = "Model switch in progress; Portal will be ready when activation completes"
+_MODEL_UNLOADED_DETAIL = "The model is unloaded. Open Models and choose Resume model to use Portal again."
+# The Windows runtime ownership proof starts host processes and takes seconds.
+# Bound it inside the Portal page's 15 s status deadline, and let one answer
+# serve its 3 s polls while an unattended hold lasts.
+_RUNTIME_PROOF_SECONDS = 10.0
+_RUNTIME_PROOF_TTL_SECONDS = 30.0
_MODEL_HOLD_DETAIL = "A model transition is holding new messages. Check model update progress. If it has stopped, restore Sandbox in Portal permissions."
_CHAT_RECOVERY_DETAIL = "Portal stopped during an earlier turn and new messages are held. Open Portal permissions and use Verify Sandbox or Restore Sandbox before continuing."
_MODEL_CAPABILITY_DETAIL = (
@@ -443,7 +449,21 @@ async def _local_inference_issue(host_status: object) -> str | None:
return "The local model runtime is unavailable. Restore it in Models before sending another task."
-def _model_readiness_issue_from_status(status: object) -> tuple[str, str] | None:
+def _unattended_model_hold(status: object) -> bool:
+ """A held Portal model transaction that no host lifecycle operation owns.
+
+ A model switch owns the host's model_activation lifecycle until it ends,
+ so a hold without any operation will not finish by itself.
+ """
+ return (isinstance(status, dict) and status.get("modelTransactionPending") is True
+ and not status.get("lifecycleActive") and not status.get("activeOperation"))
+
+
+def _model_readiness_issue_from_status(status: object, *, runtime_stopped: bool = False) -> tuple[str, str] | None:
+ # Unload model keeps the Portal hold until Resume model. An unattended
+ # hold on a runtime the host proves stopped is that, not a switch.
+ if runtime_stopped and _unattended_model_hold(status):
+ return "model_unloaded", _MODEL_UNLOADED_DETAIL
if isinstance(status, dict) and status.get("modelTransactionPending") is True:
return "model_switching", _MODEL_SWITCH_DETAIL
switching = (
@@ -500,7 +520,7 @@ def _model_support_from_status(status: object) -> dict[str, str] | None:
async def _model_readiness_issue() -> tuple[str, str] | None:
- """Return a host-proven model transition, if one is under way.
+ """Return a host-proven model transition or unload hold, if one holds Portal.
A failed host lifecycle probe alone does not take down the Pixel edge.
llama-server serves the one model it was started with, and the router
@@ -547,8 +567,35 @@ async def _verified_external_host_runtime(host_status: object) -> dict[str, obje
return _active_runtime_projection({"activeRuntime": runtime})
+_runtime_proof: tuple[float, bool] | None = None
+
+
+async def _owned_runtime_stopped() -> bool:
+ """Whether the host proves the Windows model runtime it owns is stopped.
+
+ A failed or invalid proof is unknown, never "stopped". It is remembered
+ too, so a slow host is not asked again on every poll.
+ """
+ global _runtime_proof
+ if _runtime_proof is not None and time.monotonic() - _runtime_proof[0] < _RUNTIME_PROOF_TTL_SECONDS:
+ return _runtime_proof[1]
+ try:
+ async with async_timeout(_RUNTIME_PROOF_SECONDS):
+ value = await request_agent_json("GET", "/v1/model/management", timeout=_RUNTIME_PROOF_SECONDS)
+ except (asyncio.TimeoutError, AgentClientError):
+ value = None
+ # Exactly the proof for which the Models page offers Resume model.
+ stopped = (isinstance(value, dict) and value.get("managed") is True and value.get("canUnload") is True
+ and value.get("running") is False and value.get("canActivate") is False)
+ _runtime_proof = (time.monotonic(), stopped)
+ return stopped
+
+
async def _model_readiness_issue_for_status(status: object) -> tuple[str, str] | None:
- return _model_readiness_issue_from_status(status)
+ # Only an unattended hold needs the runtime proof; a switch in progress
+ # owns a host lifecycle operation.
+ stopped = _unattended_model_hold(status) and await _owned_runtime_stopped()
+ return _model_readiness_issue_from_status(status, runtime_stopped=stopped)
async def _model_activation_in_progress() -> bool:
diff --git a/ods/extensions/services/dashboard-api/tests/test_pixel.py b/ods/extensions/services/dashboard-api/tests/test_pixel.py
index 51313a62e..59d2140d1 100644
--- a/ods/extensions/services/dashboard-api/tests/test_pixel.py
+++ b/ods/extensions/services/dashboard-api/tests/test_pixel.py
@@ -135,6 +135,7 @@ def pixel_env(monkeypatch):
monkeypatch.setattr(pixel_chat_identity, "async_request_json", saved_identity)
monkeypatch.setenv("PIXEL_OPENWEBUI_KEY", EDGE_KEY)
monkeypatch.setenv("PIXEL_EDGE_URL", "http://pixel-edge:9595")
+ monkeypatch.setattr(pixel, "_runtime_proof", None)
async def idle_model_lifecycle(*_args, **_kwargs):
return {"status": "idle", "lifecycleActive": False, "activeOperation": None}
@@ -702,6 +703,129 @@ async def test_pending_native_model_transaction_blocks_pixel_after_host_restart(
assert exc.value.status_code == 409
+# The host's ownership proof after Unload model; the Models page offers Resume model for it.
+_STOPPED_RUNTIME = {"managed": True, "canActivate": False, "canUnload": True, "running": False, "vision": False}
+_UNATTENDED_HOLD = {"status": "idle", "modelTransactionPending": True}
+
+
+def _host_agent(monkeypatch, model_status, management):
+ """Answer the host reads Portal makes for a held model; record each path."""
+ paths = []
+
+ async def agent(_method, path, **_kwargs):
+ paths.append(path)
+ if path == "/v1/model/status":
+ return model_status
+ if path == "/v1/model/management":
+ if isinstance(management, BaseException):
+ raise management
+ return management
+ raise pixel.AgentClientError("not used by this test")
+
+ monkeypatch.setattr(pixel, "request_agent_json", agent)
+ return paths
+
+
+def test_an_unattended_hold_on_a_stopped_runtime_is_an_unload_not_a_switch():
+ assert pixel._model_readiness_issue_from_status(_UNATTENDED_HOLD, runtime_stopped=True) == (
+ "model_unloaded", pixel._MODEL_UNLOADED_DETAIL)
+ assert pixel._model_readiness_issue_from_status(_UNATTENDED_HOLD) == ("model_switching", pixel._MODEL_SWITCH_DETAIL)
+ assert pixel._model_readiness_issue_from_status({"status": "idle"}, runtime_stopped=True) is None
+
+
[email protected]("status", [
+ {**_UNATTENDED_HOLD, "lifecycleActive": True, "activeOperation": "model_activation", "activeTarget": "next-model"},
+ {"status": "idle", "lifecycleActive": True, "activeOperation": "model_activation"},
+ # Resume model runs as the host's model_runtime operation.
+ {**_UNATTENDED_HOLD, "lifecycleActive": True, "activeOperation": "model_runtime"},
+])
+def test_a_host_operation_in_progress_stays_a_switch_on_a_stopped_runtime(status):
+ assert pixel._model_readiness_issue_from_status(status, runtime_stopped=True) == (
+ "model_switching", pixel._MODEL_SWITCH_DETAIL)
+
+
+async def test_status_says_the_model_is_unloaded_and_a_send_names_resume(monkeypatch):
+ paths = _host_agent(monkeypatch, _UNATTENDED_HOLD, _STOPPED_RUNTIME)
+ with patch.object(
+ pixel.httpx,
+ "AsyncClient",
+ side_effect=AssertionError("an unloaded model reached Pixel edge"),
+ ):
+ status = await pixel.pixel_status()
+ assert status == {"available": False, "model": None, "state": "model_unloaded",
+ "detail": pixel._MODEL_UNLOADED_DETAIL}
+ body = pixel.ChatStreamRequest.model_validate({
+ "chat_id": "unloaded_model", "messages": [{"role": "user", "content": "hello"}],
+ })
+ with pytest.raises(HTTPException) as exc:
+ await pixel.pixel_chat_stream(ConnectedRequest(), body)
+ assert (exc.value.status_code, exc.value.detail) == (409, pixel._MODEL_UNLOADED_DETAIL)
+ assert (await pixel.pixel_status())["state"] == "model_unloaded"
+ # The proof starts Windows processes; one answer serves the following reads.
+ assert paths.count("/v1/model/management") == 1
+
+
+async def test_status_keeps_a_real_switch_without_asking_for_the_runtime_proof(monkeypatch):
+ switching = {**_UNATTENDED_HOLD, "lifecycleActive": True, "activeOperation": "model_activation"}
+ paths = _host_agent(monkeypatch, switching, _STOPPED_RUNTIME)
+ with patch.object(
+ pixel.httpx,
+ "AsyncClient",
+ side_effect=AssertionError("a model switch reached Pixel edge"),
+ ):
+ status = await pixel.pixel_status()
+ assert (status["state"], status["detail"]) == ("model_switching", pixel._MODEL_SWITCH_DETAIL)
+ assert "/v1/model/management" not in paths
+
+
[email protected]("management", [
+ {**_STOPPED_RUNTIME, "running": True, "canActivate": True},
+ {"managed": False, "canActivate": False, "canUnload": False, "running": False, "vision": False},
+ {**_STOPPED_RUNTIME, "canActivate": True},
+ {"status": "idle"},
+ pixel.AgentHTTPError(503, "Windows runtime management could not be verified"),
+ pixel.AgentClientError("unreachable"),
+], ids=["running", "unmanaged", "inconsistent", "malformed", "unverified", "unreachable"])
+async def test_an_unproven_stop_keeps_the_switching_hold(monkeypatch, management):
+ _host_agent(monkeypatch, _UNATTENDED_HOLD, management)
+ with patch.object(
+ pixel.httpx,
+ "AsyncClient",
+ side_effect=AssertionError("a held model reached Pixel edge"),
+ ):
+ status = await pixel.pixel_status()
+ assert (status["state"], status["detail"]) == ("model_switching", pixel._MODEL_SWITCH_DETAIL)
+
+
+async def test_the_runtime_proof_is_bounded_and_reused_until_it_expires(monkeypatch):
+ answers: dict[str, object] = {"management": None}
+ paths = []
+
+ async def host(_method, path, **_kwargs):
+ paths.append(path)
+ if path == "/v1/model/status":
+ return _UNATTENDED_HOLD
+ if answers["management"] is None:
+ await asyncio.sleep(5) # A proof slower than its bound.
+ return answers["management"]
+
+ monkeypatch.setattr(pixel, "request_agent_json", host)
+ monkeypatch.setattr(pixel, "_RUNTIME_PROOF_SECONDS", 0.05)
+ switching = ("model_switching", pixel._MODEL_SWITCH_DETAIL)
+ assert await pixel._model_readiness_issue() == switching
+ answers["management"] = _STOPPED_RUNTIME
+ assert await pixel._model_readiness_issue() == switching
+ assert paths.count("/v1/model/management") == 1
+ checked_at, stopped = pixel._runtime_proof
+ monkeypatch.setattr(pixel, "_runtime_proof", (checked_at - pixel._RUNTIME_PROOF_TTL_SECONDS, stopped))
+ assert await pixel._model_readiness_issue() == ("model_unloaded", pixel._MODEL_UNLOADED_DETAIL)
+ assert paths.count("/v1/model/management") == 2
+
+
@pytest.mark.asyncio
async def test_status_keeps_adaptive_model_available_with_fixed_advisory(monkeypatch):
async def adaptive_model(*_args, **_kwargs):
diff --git a/ods/extensions/services/dashboard/src/pages/Pixel.jsx b/ods/extensions/services/dashboard/src/pages/Pixel.jsx
index 23a7dcc0e..a72569aea 100644
--- a/ods/extensions/services/dashboard/src/pages/Pixel.jsx
+++ b/ods/extensions/services/dashboard/src/pages/Pixel.jsx
@@ -978,6 +978,8 @@ export default function Pixel({ systemStatus = null }) {
? 'available'
: data.state === 'model_switching'
? 'switching'
+ : data.state === 'model_unloaded'
+ ? 'unloaded'
: data.state === 'model_transition_pending'
? 'held'
: data.state === 'chat_recovery_required'
@@ -1769,6 +1771,8 @@ export default function Pixel({ systemStatus = null }) {
? (readinessView.attention ? 'Needs attention' : 'Available')
: status === 'switching'
? 'Switching model...'
+ : status === 'unloaded'
+ ? 'Model unloaded'
: status === 'held'
? 'Model transition pending'
: status === 'recovery'
@@ -1878,6 +1882,7 @@ export default function Pixel({ systemStatus = null }) {
<ModelActivationNotice value={modelActivation} pending={status === 'switching'} available={status === 'available'} portal onRefresh={()=>setModelStatusRefresh(value=>value+1)}/>
{status === 'held' && <p role="status" className="shrink-0 border-b border-theme-border px-4 py-2 text-xs text-theme-text-secondary sm:px-6">{statusDetail}</p>}
{status === 'recovery' && <p role="alert" className="shrink-0 border-b border-theme-border px-4 py-2 text-xs text-theme-text-secondary sm:px-6">{statusDetail} <Link to="/settings?section=access" className="underline">Portal permissions</Link></p>}
+ {status === 'unloaded' && <p role="status" className="shrink-0 border-b border-theme-border px-4 py-2 text-xs text-theme-text-secondary sm:px-6">{statusDetail} <Link to="/models" className="underline">Open Models</Link></p>}
{status === 'available' && modelSupport && (
<p role="status" aria-label="Model capability" className="shrink-0 border-b border-theme-border px-4 py-2 text-xs text-theme-text-secondary sm:px-6">
{modelSupport.detail}
@@ -2049,6 +2054,8 @@ export default function Pixel({ systemStatus = null }) {
? `Message ${displayName}...`
: status === 'switching'
? 'Waiting for model switch...'
+ : status === 'unloaded'
+ ? 'Messages are held until the model is resumed'
: status === 'held'
? 'Messages are held during a model transition'
: status === 'recovery'
diff --git a/ods/extensions/services/dashboard/src/pages/Pixel.model-switch.test.jsx b/ods/extensions/services/dashboard/src/pages/Pixel.model-switch.test.jsx
index 589ff7868..0b8b52820 100644
--- a/ods/extensions/services/dashboard/src/pages/Pixel.model-switch.test.jsx
+++ b/ods/extensions/services/dashboard/src/pages/Pixel.model-switch.test.jsx
@@ -71,6 +71,27 @@ it('requires an explicit unconfirmed outcome and never enables chat from diagnos
expect(screen.getByLabelText('Model activation')).toHaveTextContent('has not finished')
})
+it('says an unloaded model needs Resume model in Models instead of waiting for a switch',async()=>{
+ const detail='The model is unloaded. Open Models and choose Resume model to use Portal again.'
+ render(<Pixel/>); await act(async()=>{})
+ fireEvent.change(screen.getByRole('textbox'),{target:{value:'Keep this draft intact'}})
+ await poll({available:false,model:null,state:'model_unloaded',detail})
+ expect(screen.getByText('Model unloaded').querySelector('.animate-spin')).toBeNull()
+ expect(screen.queryByText('Switching model...')).toBeNull()
+ expect(screen.queryByLabelText('Model activation')).toBeNull()
+ expect(screen.getByText(detail)).toBeVisible()
+ expect(screen.getByRole('link',{name:'Open Models'})).toHaveAttribute('href','/models')
+ const composer=screen.getByPlaceholderText('Messages are held until the model is resumed')
+ expect(composer).toBeDisabled()
+ expect(composer).toHaveValue('Keep this draft intact')
+ expect(screen.getByTitle('Send')).toBeDisabled()
+ await poll({available:true})
+ expect(screen.queryByRole('link',{name:'Open Models'})).toBeNull()
+ expect(screen.getByRole('textbox')).toHaveValue('Keep this draft intact')
+ expect(screen.getByTitle('Send')).toBeEnabled()
+ expect(fetch.mock.calls.every(([url,options])=>options?.method!=='POST' || url==='/api/pixel/chat/context')).toBe(true)
+})
+
it('ignores malformed diagnostic metadata and removes a stale diagnosis on the next status read',async()=>{
status=switching('rolling_back')
render(<Pixel/>);await act(async()=>{})