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): serve Model Hub's config from a snapshot until the file changes (#2612)
Call Whited59fde83aa3f
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.pyindex f7f5951d6..b88d5094f 100644--- a/core/handlers/model_hub/service.py+++ b/core/handlers/model_hub/service.py@@ -3,9 +3,11 @@from __future__ import annotationsimport asyncio+import copyimport hashlibimport jsonimport logging+import threadingimport timeimport uuidfrom contextlib import asynccontextmanager@@ -497,12 +499,39 @@ class ModelHubConfigStore(Protocol):def mutate(self, mutator: Callable[[ModelHubConfig], bool]) -> None: ...+def _file_identity(path: Path) -> tuple | None:+ """What changes whenever the config file is replaced or rewritten: its path, inode and size, and its times."""++ try:+ stat = path.stat()+ except FileNotFoundError:+ return None+ return (str(path), stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns)++class V2ModelHubConfigStore:+ """Model Hub's config, read from the shared config file and served from a snapshot (#2568).++ Reading the file parses the whole config, which grows with Sources,+ inventories and routes, and Model Hub reads it on every request. So the+ store keeps the last parse and serves a copy of it while the file is+ unchanged: a read costs one ``stat`` and a copy, never a parse. Any change to+ the file (a write through this store, another writer in this process, the UI+ process or the CLI) replaces or rewrites it, which changes its identity, and+ the next read parses again. A write through this store keeps what it+ committed as the snapshot, inside its own lock, so the reads after it parse+ nothing.+ """+_NATIVE_AUTH_FIELDS = {"claude": ("auth_mode", "api_key", "base_url", "auth_mode_set"),"codex": ("auth_mode", "api_key", "base_url", "oauth_relay_marker"),}+ def __init__(self) -> None:+ self._snapshot_lock = threading.Lock()+ self._snapshot: tuple[tuple, ModelHubConfig] | None = None+def native_auth_snapshot(self, backends: tuple[str, ...]) -> dict[str, dict]:try:config = V2Config.load()@@ -524,26 +553,47 @@ class V2ModelHubConfigStore:*,expected_hub: tuple[dict, ...],) -> None:- from config.v2_config import config_write_transaction+ from config.v2_config import config_file_lock, config_write_transactionself.ensure_writable()- with config_write_transaction() as config:- if config.model_hub.to_payload() not in expected_hub:- raise MigrationConflictError- for backend, values in desired.items():- target = getattr(config.agents, backend)- actual = {name: getattr(target, name) for name in values}- if actual not in (expected.get(backend), values):+ path = paths.get_config_path()+ with config_file_lock(path):+ with config_write_transaction(path) as config:+ if config.model_hub.to_payload() not in expected_hub:raise MigrationConflictError- for name, value in values.items():- setattr(target, name, value)- config.model_hub = ModelHubConfig.from_payload(model_hub.to_payload())+ for backend, values in desired.items():+ target = getattr(config.agents, backend)+ actual = {name: getattr(target, name) for name in values}+ if actual not in (expected.get(backend), values):+ raise MigrationConflictError+ for name, value in values.items():+ setattr(target, name, value)+ config.model_hub = ModelHubConfig.from_payload(model_hub.to_payload())+ self._seed(path, config.model_hub)def load(self) -> ModelHubConfig:+ path = paths.get_config_path()+ identity = _file_identity(path)+ with self._snapshot_lock:+ snapshot = self._snapshot+ if snapshot is not None and identity is not None and snapshot[0] == identity:+ # Callers own what they load and may change it; the snapshot stays as the file holds it.+ return copy.deepcopy(snapshot[1])+ fresh = self._read(path)+ return default_config().model_hub if fresh is None else fresh[1]++ def _read(self, path: Path) -> tuple[tuple, ModelHubConfig] | None:+ """Parse the file and keep the parse, when the file did not change while it was read."""++ before = _file_identity(path)try:- return V2Config.load().model_hub+ model_hub = V2Config.load().model_hubexcept FileNotFoundError:- return default_config().model_hub+ return None+ if before is not None and _file_identity(path) == before:+ with self._snapshot_lock:+ self._snapshot = (before, copy.deepcopy(model_hub))+ return (before or (), model_hub)def ensure_writable(self) -> None:try:@@ -557,17 +607,36 @@ class V2ModelHubConfigStore:def save(self, model_hub: ModelHubConfig) -> None:model_hub = ModelHubConfig.from_payload(model_hub.to_payload())self.ensure_writable()- from config.v2_config import update_config_fields+ from config.v2_config import config_file_lock, update_config_fields- update_config_fields(lambda cfg: setattr(cfg, "model_hub", model_hub))+ path = paths.get_config_path()+ with config_file_lock(path):+ committed = update_config_fields(lambda cfg: setattr(cfg, "model_hub", model_hub), config_path=path)+ self._seed(path, committed.model_hub)def mutate(self, mutator: Callable[[ModelHubConfig], bool]) -> None:from config.v2_config import config_file_lock- with config_file_lock():+ path = paths.get_config_path()+ with config_file_lock(path):config = V2Config.load()if mutator(config.model_hub):config.save()+ self._seed(path, config.model_hub)++ def _seed(self, path: Path, model_hub: ModelHubConfig) -> None:+ """Keep what a write just committed as the snapshot, without reading the file back.++ Called inside the write's config lock, so no other writer can have+ replaced the file since: its identity now names exactly this content. The+ section is settled the way a load settles it (the one repairing door).+ """++ identity = _file_identity(path)+ if identity is not None:+ settled = ModelHubConfig.from_payload(model_hub.to_payload(), repairing=True)+ with self._snapshot_lock:+ self._snapshot = (identity, settled)class UnavailableEngineAdapter:"""Explicit fail-closed adapter for isolated callers and tests."""diff --git a/tests/test_model_hub_config_read_compatibility.py b/tests/test_model_hub_config_read_compatibility.pyindex 10151cc91..52c98d9b7 100644--- a/tests/test_model_hub_config_read_compatibility.py+++ b/tests/test_model_hub_config_read_compatibility.py@@ -370,3 +370,54 @@ def test_runtime_start_failure_retains_explicit_start_intent(tmp_path, marker):assert main.load_config().model_hub.enabled is Trueassert path.read_bytes() == afterassert not list(path.parent.glob("config.json.bak-*"))+++def test_the_store_parses_the_config_once_until_the_file_changes(monkeypatch):+ """#2568: Model Hub reads its config on every request, so the store serves its last parse while the file is+ unchanged. Any change to the file, by this store or another writer, is read on the next load, as before."""+ from core.handlers.model_hub.service import V2ModelHubConfigStore++ config = V2Config.default()+ model_id = config.model_hub.agents["codex"].models[0].id+ config.model_hub.sources = [_source("src_snapshot1", (model_id,), vendor="openai")]+ path = paths.get_config_path()+ config.save(path)+ parses = []+ load = V2Config.load++ def counting(*args, **kwargs):+ parses.append(1)+ return load(*args, **kwargs)++ monkeypatch.setattr(V2Config, "load", counting)+ store = V2ModelHubConfigStore()++ first = store.load()+ for _ in range(5):+ store.load()+ assert len(parses) == 1++ # What a caller loads is its own: changing it never reaches the snapshot.+ first.sources.clear()+ assert [source.id for source in store.load().sources] == ["src_snapshot1"]++ # Another writer replaces the file: the next load reads it.+ payload = json.loads(path.read_text(encoding="utf-8"))+ payload["model_hub"]["sources"][0]["display_name"] = "另一个进程改的名字"+ path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")+ assert store.load().sources[0].display_name == "另一个进程改的名字"+ assert len(parses) == 2++ # A write through the store keeps what it committed, without reading the file back, and the reads after it parse+ # nothing.+ edited = store.load()+ edited.sources[0].display_name = "保存后的名字"+ with monkeypatch.context() as patched:+ patched.setattr(store, "_read", lambda _path: pytest.fail("a write read the file back"))+ store.save(edited)+ store.mutate(lambda hub: setattr(hub.sources[0], "display_name", "改过两次的名字") or True)+ after_writes = len(parses)+ assert store.load().sources[0].display_name == "改过两次的名字"+ assert store.load().sources[0].display_name == "改过两次的名字"+ assert len(parses) == after_writes+ assert V2ModelHubConfigStore().load().sources[0].display_name == "改过两次的名字"