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): check the engine's health once per 30 s, not twice per request (#2513)

Call White47e4c06a47f6
diff --git a/tests/test_model_hub_runtime.py b/tests/test_model_hub_runtime.py
index 1f97863fd..4ef650eb7 100644
--- a/tests/test_model_hub_runtime.py
+++ b/tests/test_model_hub_runtime.py
@@ -529,6 +529,9 @@ def test_supervisor_records_health_transitions_without_repeating_or_leaking_erro
for _ in range(2):
assert supervisor.status()["status"]["health"] == "ok"
failure[0] = EngineClientError("private-body", error_type="TimeoutError")
+ # A passed check vouches for the engine until it expires.
+ assert supervisor.status()["status"]["health"] == "ok"
+ supervisor._verified_at -= supervisor_module.MODEL_HUB_HEALTH_TTL_SECONDS
assert supervisor.status()["status"]["health"] == "degraded"
messages = [record.getMessage() for record in caplog.records]
@@ -4027,6 +4030,63 @@ def test_supervisor_starts_checks_health_and_stops_mock_engine(
supervisor.stop()
+def test_a_verified_engine_serves_calls_with_a_liveness_check_until_its_health_check_expires(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ supervisor, _store = _fixture_supervisor(tmp_path)
+ checks: list[str] = []
+ real_health = EngineClient.health
+
+ def counted_health(client: EngineClient) -> bool:
+ checks.append(client.connection.base_url)
+ return real_health(client)
+
+ monkeypatch.setattr(EngineClient, "health", counted_health)
+ try:
+ first = supervisor.ensure_running()
+ at_ready = len(checks)
+ for _ in range(20):
+ assert supervisor.ensure_running() is first
+ assert supervisor.status()["status"]["health"] == "ok"
+ assert len(checks) == at_ready
+
+ supervisor._verified_at -= supervisor_module.MODEL_HUB_HEALTH_TTL_SECONDS
+ assert supervisor.ensure_running() is first
+ assert supervisor.ensure_running() is first
+ assert len(checks) == at_ready + 1
+
+ # An engine that exits is found by the next call's liveness check, which starts another.
+ supervisor._process.kill()
+ supervisor._process.wait(timeout=5)
+ assert supervisor.ensure_running() is not first
+ finally:
+ supervisor.stop()
+
+
+def test_a_failed_health_check_is_not_remembered(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ installer = SimpleNamespace(
+ status=lambda: {"installed": True, "version": "fixture"},
+ contract_manifest=lambda: {"name": "cliproxyapi", "version": "fixture", "assets": []},
+ )
+ supervisor = EngineSupervisor(installer=installer, state_store=EngineStateStore(tmp_path / "state"))
+ supervisor._process = SimpleNamespace(poll=lambda: None)
+ supervisor._connection = EngineConnection("http://127.0.0.1:15220", "management", "gateway")
+ answers = [False, False, True]
+ checks: list[bool] = []
+
+ def health(_client: EngineClient) -> bool:
+ checks.append(answers[min(len(checks), len(answers) - 1)])
+ return checks[-1]
+
+ monkeypatch.setattr(EngineClient, "health", health)
+
+ healths = [supervisor.status()["status"]["health"] for _ in range(5)]
+
+ assert healths == ["degraded", "degraded", "ok", "ok", "ok"]
+ assert checks == [False, False, True]
+ supervisor._process = None
+
+
def test_supervisor_disable_restores_explicit_off_state(tmp_path: Path) -> None:
supervisor, _store = _fixture_supervisor(tmp_path)
supervisor.ensure_running()
diff --git a/vibe/model_hub_runtime/supervisor.py b/vibe/model_hub_runtime/supervisor.py
index a27e9e55f..bb1a3e599 100644
--- a/vibe/model_hub_runtime/supervisor.py
+++ b/vibe/model_hub_runtime/supervisor.py
@@ -44,6 +44,12 @@ _T = TypeVar("_T")
MODEL_HUB_STARTUP_TIMEOUT_SECONDS = 30.0
MODEL_HUB_CONFIG_RELOAD_TIMEOUT_SECONDS = 5.0
+# How long a passed full health check vouches for a running engine. Within it a
+# call checks only that the process is alive. The full check (two management
+# round trips, tens of milliseconds, which a request holds the adapter's routing
+# lock across) runs at start and once this expires. A failed one is not
+# remembered: every call checks again, and a request restarts the engine.
+MODEL_HUB_HEALTH_TTL_SECONDS = 30.0
_STARTUP_POLL_INTERVAL_SECONDS = 0.05
# Durable identity of the engine this state root last spawned. A service that
# dies without running ``atexit`` (SIGKILL, crash, forced restart) leaves its
@@ -95,6 +101,8 @@ class EngineSupervisor:
self._start_attempted = False
self._health_failure_signature: tuple[str, str, int | None] | None = None
self._health_os_errno: int | None = None
+ # Monotonic time of the last full health check the running engine passed.
+ self._verified_at: float | None = None
self._record_os_errno: int | None = None
self._config_generation = 0
@@ -431,6 +439,7 @@ class EngineSupervisor:
self._stop_locked()
raise EngineUnavailableError("models.engine.unsafe_permissions") from exc
self._last_check = _utc_now()
+ self._verified_at = time.monotonic()
logger.info(
"Model Hub engine startup outcome=ready managed_version=%s "
"elapsed_seconds=%.3f child_output_retained=false",
@@ -456,7 +465,12 @@ class EngineSupervisor:
def _healthy_locked(self) -> bool:
if not self._is_running_locked() or self._connection is None:
return False
- return self._check_health_locked(EngineClient(self._connection, timeout=1.0))
+ verified_at = self._verified_at
+ if verified_at is not None and time.monotonic() - verified_at < MODEL_HUB_HEALTH_TTL_SECONDS:
+ return True
+ healthy = self._check_health_locked(EngineClient(self._connection, timeout=1.0))
+ self._verified_at = time.monotonic() if healthy else None
+ return healthy
def _check_health_locked(self, client: EngineClient) -> bool:
healthy = client.health()
@@ -494,6 +508,7 @@ class EngineSupervisor:
process = self._process
self._process = None
self._connection = None
+ self._verified_at = None
self._health_failure_signature = None
self._health_os_errno = None
if process is not None and process.poll() is None: