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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
perf(model-hub): resume the engine after readiness, off the network path (#2277)
Call Whitede46f2033cfe
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.pyindex 8aeb55406..1de831ba2 100644--- a/core/handlers/model_hub/service.py+++ b/core/handlers/model_hub/service.py@@ -1012,6 +1012,7 @@ class ModelHubService:self._runtime_install_reconcile_lock = asyncio.Lock()self._runtime_install_reconciled = Falseself._runtime_lifecycle_lock = asyncio.Lock()+ self._runtime_resume_task: asyncio.Task[None] | None = Noneself._builtin_snapshot_generations: dict[BackendName, str] = {}self._builtin_snapshot_cache: dict[BackendName, list[dict[str, Any]]] = {}self._pending_builtin_catalog_refresh: set[BackendName] = set()@@ -1593,7 +1594,12 @@ class ModelHubService:return recovered if isinstance(recovered, EngineStatus) else Noneasync def recover_runtime_intent(self) -> None:- """Restore the runtime only when the user left it enabled."""+ """Recover credential custody, then resume a runtime the user left enabled.++ Only custody recovery gates service readiness. Resuming may first+ download the engine, so it runs in the background, retired by `stop()`;+ every consumer prepares the engine on demand in the meantime.+ """try:await recover_native_migration(self)@@ -1602,12 +1608,32 @@ class ModelHubService:# writer or native launch. Controller recovery preserves this gate.self.migration_blocked_backends.update(MODEL_HUB_BACKENDS)raise ModelHubError("migration_item_conflict", status=409) from None- async with self._runtime_lifecycle_lock:+ task = self._runtime_resume_task+ if task is None or task.done():+ self._runtime_resume_task = asyncio.create_task(+ self._resume_runtime_intent(),+ name="model-hub-runtime-resume",+ )++ async def _resume_runtime_intent(self) -> None:+ try:await self.reconcile_runtime_installation()if not self.store.load().enabled:return+ # Preparing may download the engine. Like every demand path, it+ # holds no lifecycle lock, so an explicit start or stop is served+ # meanwhile instead of after the download.await self._prepare_engine_for_demand()- await self._engine_call(self.adapter.start())+ async with self._runtime_lifecycle_lock:+ # An explicit stop clears the intent; `stop()` retires this+ # resume. Either way it must not start the engine.+ if self._runtime_resume_task is not asyncio.current_task():+ return+ if not self.store.load().enabled:+ return+ await self._engine_call(self.adapter.start())+ except Exception:+ logger.exception("Model Hub runtime resume failed; the engine is prepared on demand")async def _ensure_runtime_dependency(self,@@ -1631,6 +1657,12 @@ class ModelHubService:await await_owned_task(task)except Exception:logger.warning("Native takeover remains pending during shutdown")+ resume, self._runtime_resume_task = self._runtime_resume_task, None+ if resume is not None and not resume.done():+ # Drain rather than cancel: the installer runs in a worker thread+ # that cancellation cannot stop, and the adapter must not stop+ # beneath it. A retired resume never starts the engine.+ await await_owned_task(resume)async with self._runtime_lifecycle_lock:await self.adapter.stop()diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yamlindex 52aaaab57..43a723d6e 100644--- a/tests/scenarios/model_hub/catalog.yaml+++ b/tests/scenarios/model_hub/catalog.yaml@@ -546,6 +546,12 @@ scenarios:test: tests/test_model_hub_retry_lifecycle.py::test_slow_stream_resolution_keeps_claude_connectedrelated_tests:- tests/test_model_hub_injection.py::test_claude_hub_waits_out_resolution_as_one_streaming_request+ - id: MH-RUNTIME-011+ name: Avibe readiness never waits for the engine download; an explicit stop is served during it and shutdown drains it+ status: covered+ kind: lifecycle+ layer: unit+ test: tests/test_model_hub_api.py::test_runtime_recovery_does_not_wait_for_the_engine_download- id: MH-USAGE-001name: Tokens an upstream reported before the turn ended are still meteredstatus: covereddiff --git a/tests/test_model_hub_api.py b/tests/test_model_hub_api.pyindex 0c71f1196..9fa764339 100644--- a/tests/test_model_hub_api.py+++ b/tests/test_model_hub_api.py@@ -1056,10 +1056,15 @@ def test_runtime_stop_returns_explicit_not_started_state(tmp_path):_assert_valid("runtime-dependency.schema.json", runtime)+async def _recover_runtime(service):+ await service.recover_runtime_intent()+ await service._runtime_resume_task++def test_runtime_recovery_respects_the_persisted_default_on_intent(tmp_path):service, store, adapter = _service(tmp_path)- asyncio.run(service.recover_runtime_intent())+ asyncio.run(_recover_runtime(service))assert adapter.start_calls == 1assert adapter.ensure_calls == [False]@@ -1074,12 +1079,77 @@ def test_runtime_recovery_respects_the_persisted_default_on_intent(tmp_path):migration_home=tmp_path / "restarted-native-home",)- asyncio.run(restarted.recover_runtime_intent())+ asyncio.run(_recover_runtime(restarted))assert adapter.start_calls == 1assert adapter.ensure_calls == [False][email protected]("ending", ["download_completes", "runtime_stop", "shutdown"])+def test_runtime_recovery_does_not_wait_for_the_engine_download(tmp_path, ending):+ """MH-RUNTIME-011: readiness follows custody recovery; the engine download is background work."""++ class DownloadingAdapter(FakeAdapter):+ def __init__(self):+ super().__init__()+ self.downloading = asyncio.Event()+ self.release = threading.Event()+ self.calls: list[str] = []++ async def ensure_installed(self, *, force=False, offline=False):+ self.downloading.set()+ # Like the real installer, the download runs in a worker thread+ # that cancelling its awaiter cannot stop.+ await asyncio.to_thread(self.release.wait, 5)+ self.calls.append("download_done")+ return await super().ensure_installed(force=force, offline=offline)++ async def stop(self):+ self.calls.append("adapter_stop")++ async def scenario():+ adapter = DownloadingAdapter()+ service, store, _ = _service(tmp_path, adapter)++ await asyncio.wait_for(service.recover_runtime_intent(), 1)+ await asyncio.wait_for(adapter.downloading.wait(), 1)+ assert adapter.start_calls == 0+ resume = service._runtime_resume_task+ if ending == "download_completes":+ adapter.release.set()+ await asyncio.wait_for(resume, 1)+ await asyncio.wait_for(service.stop(), 1)+ return adapter, store+ if ending == "runtime_stop":+ # An explicit stop is served during the download, and the resume+ # then honors it instead of starting the engine.+ for agent in store.config.agents.values():+ agent.mode = "direct"+ payload = await asyncio.wait_for(service.runtime_stop(), 1)+ assert payload["enabled"] is False+ adapter.release.set()+ await asyncio.wait_for(resume, 1)+ return adapter, store+ # Shutdown drains the download before stopping the adapter beneath it.+ shutdown = asyncio.create_task(service.stop())+ await asyncio.sleep(0.2)+ assert not shutdown.done()+ adapter.release.set()+ await asyncio.wait_for(shutdown, 1)+ return adapter, store++ adapter, store = asyncio.run(scenario())+ if ending == "download_completes":+ assert adapter.start_calls == 1+ assert adapter.calls == ["download_done", "adapter_stop"]+ elif ending == "runtime_stop":+ assert adapter.start_calls == 0+ assert store.load().enabled is False+ else:+ assert adapter.start_calls == 0+ assert adapter.calls == ["download_done", "adapter_stop"]++def test_runtime_start_syncs_sources_before_starting_once(tmp_path):class OrderedAdapter(FakeAdapter):def __init__(self):diff --git a/tests/test_model_hub_provisional_ownership.py b/tests/test_model_hub_provisional_ownership.pyindex 1c0f0c57f..75b955984 100644--- a/tests/test_model_hub_provisional_ownership.py+++ b/tests/test_model_hub_provisional_ownership.py@@ -175,7 +175,13 @@ def test_prepared_save_uncertain_outcome_keeps_its_provisional_grant(assert refs <= {entry.credential_ref for entry in service.revocations.list()}recovered, _, _ = _service(tmp_path, migration_home=tmp_path / "native")recovered.store, recovered.adapter = store, adapter- asyncio.run(recovered.recover_runtime_intent())++ async def recover():+ await recovered.recover_runtime_intent()+ # Resuming the engine consumes the recovered revocation journal.+ await recovered._runtime_resume_task++ asyncio.run(recover())assert recovered.migration_journal.load() is Noneassert not recovered.revocations.list()assert {source.credential_ref for source in store.config.sources} == refs