Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(model-hub): a key replacement reloads the engine without reading the old credential it recovers (#2672)

Call White37363a2488f6
diff --git a/tests/e2e/test_model_hub_migration_persisted.py b/tests/e2e/test_model_hub_migration_persisted.py
index 44d2ea126..2756a77d4 100644
--- a/tests/e2e/test_model_hub_migration_persisted.py
+++ b/tests/e2e/test_model_hub_migration_persisted.py
@@ -353,10 +353,7 @@ def test_f4_shell_data_roles_allow_http_migration_without_changing_data(
assert app.client.post("/api/models/migration/scan", {}).json()["scan"]["items"] == []
[email protected]("damage", ["missing", pytest.param("corrupt", marks=pytest.mark.xfail(strict=True, reason=(
- "on a running engine the reload reads the previous projection's credentials to find stale model names, "
- "and a content-corrupt old document raises there; fixed separately"
-)))])
[email protected]("damage", ["missing", "corrupt"])
def test_f4_replace_migrated_bearer_without_old_secret_retains_cpa_transport(
model_hub_app_factory, mock_llm_upstream, damage,
):
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index 43e4d7746..46e2aed24 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -3226,6 +3226,66 @@ def test_source_sync_waits_for_a_reload_that_removes_every_model(tmp_path: Path)
supervisor.stop()
+ ("damage", "error"),
+ [("missing", None), ("corrupt", "JSONDecodeError"), ("non_object", "NonObjectDocument")],
+)
+def test_a_key_replacement_reloads_without_reading_the_old_credential(
+ tmp_path: Path, caplog: pytest.LogCaptureFixture, damage: str, error: str | None,
+) -> None:
+ """Replacing the key is how a damaged credential is recovered, so the reload never depends on reading it."""
+
+ supervisor, store = _fixture_supervisor(tmp_path)
+ old_ref = store.store_api_key("old-secret", base_url="https://api.example.test/v1")
+ store.sync_sources([_binding(old_ref)])
+ connection = supervisor.ensure_running()
+ adapter = CLIProxyEngineAdapter(supervisor=supervisor, state_store=store)
+ document = store.root / "credentials" / f"{old_ref}.json"
+ try:
+ if damage == "missing":
+ document.unlink()
+ else:
+ document.write_bytes(b"{fixture-corrupt" if damage == "corrupt" else b"[]")
+ new_ref = store.store_api_key("new-secret", base_url="https://api.example.test/v1")
+ with caplog.at_level(logging.WARNING, logger="vibe.model_hub_runtime.supervisor"):
+ asyncio.run(adapter.sync_sources([_binding(new_ref)]))
+ prefix = store.get_source("src_fixture123").prefix
+ assert EngineClient(connection).list_model_ids() == {f"{prefix}/model-a"}
+ warnings = [record.getMessage() for record in caplog.records if "previous credential" in record.getMessage()]
+ assert warnings == ([] if error is None else [
+ f"Model Hub reload cannot read the previous credential of source=src_fixture123 error={error}; "
+ "its removal is awaited by the names its projection routes"
+ ])
+ # Every other reader still refuses the damaged document.
+ if damage != "missing":
+ with pytest.raises(EngineStateError):
+ store.credential_metadata(old_ref)
+ finally:
+ supervisor.stop()
+
+
+def test_removing_a_source_with_an_unreadable_credential_is_confirmed_only_once_its_names_leave(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A lost acknowledgment proves nothing: the removed Source's names, read from its projection, must leave."""
+
+ monkeypatch.setattr(supervisor_module, "MODEL_HUB_CONFIG_RELOAD_TIMEOUT_SECONDS", 0.5)
+ supervisor, store = _fixture_supervisor(tmp_path, drop_reload_ack=True, reload_delay=30)
+ credential_ref = store.store_api_key("upstream-secret", base_url="https://api.example.test/v1")
+ store.sync_sources([_binding(credential_ref)])
+ connection = supervisor.ensure_running()
+ prefix = store.get_source("src_fixture123").prefix
+ adapter = CLIProxyEngineAdapter(supervisor=supervisor, state_store=store)
+ try:
+ (store.root / "credentials" / f"{credential_ref}.json").write_bytes(b"{fixture-corrupt")
+ # The write is neither acknowledged nor applied: the engine still serves the removed Source.
+ with pytest.raises((EngineStateError, EngineUnavailableError)):
+ asyncio.run(adapter.sync_sources([]))
+ assert EngineClient(connection).list_model_ids() == {f"{prefix}/model-a"}
+ finally:
+ supervisor.stop()
+
+
def test_source_sync_never_restarts_a_live_engine_that_lost_the_reload_ack(tmp_path: Path) -> None:
supervisor, store = _fixture_supervisor(tmp_path, drop_reload_ack=True)
credential_ref = store.store_api_key("upstream-secret", base_url="https://api.example.test/v1")
diff --git a/vibe/model_hub_runtime/config.py b/vibe/model_hub_runtime/config.py
index ca6329fb1..468cf5b47 100644
--- a/vibe/model_hub_runtime/config.py
+++ b/vibe/model_hub_runtime/config.py
@@ -225,6 +225,11 @@ def _append_source(
raise EngineStateError("unsupported source protocol")
+def registered_model_ids(source: SourceRecord) -> tuple[str, ...]:
+ """The model ids an API-key Source registers in the rendered YAML, each routed as ``prefix/model``."""
+ return tuple(dict.fromkeys((*source.model_ids, *source.route_model_ids, *image_model_ids(source))))
+
+
def expected_model_names(
sources: Iterable[SourceRecord],
store: EngineStateStore,
@@ -236,7 +241,7 @@ def expected_model_names(
metadata = store.credential_metadata_if_present(source.credential_ref)
if metadata is None or metadata.get("kind") != "api_key":
continue
- for model in dict.fromkeys((*source.model_ids, *source.route_model_ids, *image_model_ids(source))):
+ for model in registered_model_ids(source):
expected[f"{source.prefix}/{model}"] = reload_display_name(model, generation)
return expected
diff --git a/vibe/model_hub_runtime/state.py b/vibe/model_hub_runtime/state.py
index b22908ac5..5c4fd4e48 100644
--- a/vibe/model_hub_runtime/state.py
+++ b/vibe/model_hub_runtime/state.py
@@ -42,6 +42,10 @@ class EngineStateError(RuntimeError):
pass
+class EngineStateContentError(EngineStateError):
+ """A state file that was read but does not hold a JSON object: its contents, not its access, are bad."""
+
+
@dataclass(frozen=True)
class RuntimeSecrets:
management_key: str
@@ -1260,10 +1264,12 @@ class EngineStateStore:
payload = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError:
return None
- except (OSError, ValueError) as exc:
+ except ValueError as exc:
+ raise EngineStateContentError(f"invalid engine state file: {path.name}") from exc
+ except OSError as exc:
raise EngineStateError(f"invalid engine state file: {path.name}") from exc
if not isinstance(payload, dict):
- raise EngineStateError(f"invalid engine state file: {path.name}")
+ raise EngineStateContentError(f"invalid engine state file: {path.name}")
return payload
@classmethod
diff --git a/vibe/model_hub_runtime/supervisor.py b/vibe/model_hub_runtime/supervisor.py
index bb1a3e599..c3aaa80f8 100644
--- a/vibe/model_hub_runtime/supervisor.py
+++ b/vibe/model_hub_runtime/supervisor.py
@@ -11,7 +11,7 @@ import time
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
-from typing import Any, Callable, TypeVar
+from typing import Any, Callable, Iterable, TypeVar
from config import paths
from config.atomic_io import write_atomic
@@ -32,10 +32,15 @@ from core.process_isolation import (
signal_process_tree,
)
from vibe.model_hub_runtime.client import EngineClient, EngineClientError, EngineConnection
-from vibe.model_hub_runtime.config import expected_model_names, render_engine_config, write_engine_config
+from vibe.model_hub_runtime.config import (
+ expected_model_names,
+ registered_model_ids,
+ render_engine_config,
+ write_engine_config,
+)
from vibe.model_hub_runtime.environment import engine_subprocess_environment
from vibe.model_hub_runtime.installer import EngineRuntimeManager
-from vibe.model_hub_runtime.state import EngineStateStore, SourceRecord
+from vibe.model_hub_runtime.state import EngineStateContentError, EngineStateStore, SourceRecord
logger = logging.getLogger(__name__)
@@ -196,10 +201,38 @@ class EngineSupervisor:
# management path does not prove live streams dead. The
# generation check decides; the process is never killed here.
expected = expected_model_names(sources, self.state_store, generation)
- stale = frozenset(expected_model_names(previous_sources or (), self.state_store, generation)) - set(expected)
+ stale = self._previous_model_names(previous_sources or (), generation) - set(expected)
self._await_models_locked(client, expected, stale)
self._last_check = _utc_now()
+ def _previous_model_names(self, previous_sources: Iterable[SourceRecord], generation: str) -> frozenset[str]:
+ """The routed names the previous projection registered, so the reload can await their removal.
+
+ Replacing a Source's key is how the user recovers a credential document
+ that no longer parses, so the reload must not depend on reading it.
+ Such a Source's names come from its projection record instead, the
+ names the rendered YAML registered for it, so a reload that removes it
+ still waits for them to leave, whether or not the engine acknowledged
+ the write. A replacement keeps the prefix and model ids, so those names
+ stay expected and are never awaited as stale; a Source still projected
+ with such a document fails at render, before this. Every other reader
+ still refuses the document.
+ """
+
+ names: set[str] = set()
+ for source in previous_sources:
+ try:
+ names.update(expected_model_names((source,), self.state_store, generation))
+ except EngineStateContentError as exc:
+ logger.warning(
+ "Model Hub reload cannot read the previous credential of source=%s error=%s; "
+ "its removal is awaited by the names its projection routes",
+ source.source_id,
+ type(exc.__cause__).__name__ if exc.__cause__ is not None else "NonObjectDocument",
+ )
+ names.update(f"{source.prefix}/{model}" for model in registered_model_ids(source))
+ return frozenset(names)
+
def _await_models_locked(
self,
client: EngineClient,