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): fill provider rows' models.dev limits once a copy arrives (#2390)

Call White7ab3106e9ef4
diff --git a/core/controller.py b/core/controller.py
index 2fd3b604d..9db75f2b1 100644
--- a/core/controller.py
+++ b/core/controller.py
@@ -2610,12 +2610,20 @@ class Controller:
async def reconcile() -> None:
try:
- await service.reconcile_builtin_models()
- except Exception:
- logger.warning(
- "Model Hub built-in reconciliation failed after snapshot refresh",
- exc_info=True,
- )
+ try:
+ await service.reconcile_builtin_models()
+ except Exception:
+ logger.warning(
+ "Model Hub built-in reconciliation failed after snapshot refresh",
+ exc_info=True,
+ )
+ try:
+ await service.backfill_models_dev_limits()
+ except Exception:
+ logger.warning(
+ "Model Hub models.dev limit backfill failed after snapshot refresh",
+ exc_info=True,
+ )
finally:
self._model_hub_snapshot_reconcile_task = None
if (
@@ -2744,6 +2752,7 @@ class Controller:
from modules.agents.model_hub import ModelHubRuntimeRouter
from vibe.api import resolve_cli_paths
from vibe.backend_model_catalog import set_remote_catalog_refresh_completed
+ from vibe.models_dev_catalog import set_models_dev_copy_arrived
def default_vibe_agent_model(backend: str) -> Optional[str]:
agent = self.vibe_agent_store.get_default_agent()
@@ -2852,6 +2861,9 @@ class Controller:
set_remote_catalog_refresh_completed(
self._model_hub_snapshot_refresh_completed
)
+ # A models.dev copy is another snapshot input: rows written before one
+ # was cached take their limits from it (backfill_models_dev_limits).
+ set_models_dev_copy_arrived(self._model_hub_snapshot_refresh_completed)
try:
asyncio.run(
self.model_hub_service.reconcile_builtin_models(notify=False)
diff --git a/core/handlers/model_hub/service.py b/core/handlers/model_hub/service.py
index 1e017c6a8..ff1f86a04 100644
--- a/core/handlers/model_hub/service.py
+++ b/core/handlers/model_hub/service.py
@@ -5256,6 +5256,67 @@ class ModelHubService:
except Exception as exc: # noqa: BLE001 - optional metadata never fails a seed
logger.info("Vibey seed has no models.dev metadata: %s", type(exc).__name__)
+ @staticmethod
+ def _rows_lacking_models_dev_limits(
+ config: ModelHubConfig,
+ ) -> list[tuple[BackendName, ModelHubBackendModelConfig]]:
+ return [
+ (backend, model)
+ for backend in MODEL_HUB_BACKENDS
+ for model in config.agents[backend].models
+ if model.origin == "provider"
+ and model.models_dev_id is None
+ and (model.context_window is None or model.max_output_tokens is None)
+ ]
+
+ async def backfill_models_dev_limits(self) -> list[BackendName]:
+ """Give provider rows written with no models.dev copy cached the limits one states.
+
+ A provider row takes models.dev's description as it is written (see
+ ``_provider_candidate``). Written before any copy was cached, by the
+ picker or a seed, it has none, and an Agent budgets it with default
+ limits. Once a copy names its id exactly, the row gets each limit it
+ lacks and the ``models_dev_id`` they came from. A limit already set
+ stands, and a row models.dev has described is not described again, so
+ a limit its user cleared stays cleared. Built-in rows are their CLI's
+ description and manual rows their user's; neither is filled. Returns
+ the backends whose catalog changed.
+ """
+
+ wanted = {model.id for _backend, model in self._rows_lacking_models_dev_limits(self.store.load())}
+ if not wanted:
+ return []
+ # The copy is parsed off the loop and outside the lock; the rows are
+ # read again under it.
+ described = await asyncio.to_thread(self._models_dev_descriptions, sorted(wanted))
+ if not described:
+ return []
+ async with self._mutation_lock:
+ if not self._reconcile_store_writable():
+ return []
+ previous = self.store.load()
+ config = self._clone_config(previous)
+ changed: list[BackendName] = []
+ for backend, model in self._rows_lacking_models_dev_limits(config):
+ match = described.get(model.id)
+ if match is None:
+ continue
+ filled = False
+ for field in ("context_window", "max_output_tokens"):
+ if getattr(model, field) is None and match[field] is not None:
+ setattr(model, field, match[field])
+ filled = True
+ if filled:
+ model.models_dev_id = match["models_dev_id"]
+ if backend not in changed:
+ changed.append(backend)
+ if not changed:
+ return []
+ await self._commit_synced(previous, config)
+ for backend in changed:
+ await self._refresh_backend_catalog(backend)
+ return changed
+
async def set_agent_mode(self, backend: str, mode: object) -> dict:
if mode not in {"hub", "direct"} or (backend == "vibey" and mode != "hub"):
raise ModelHubError("mode_switch_blocked")
diff --git a/docs/plans/backend-model-catalogs.md b/docs/plans/backend-model-catalogs.md
index d2a1b13cc..653c8d89e 100644
--- a/docs/plans/backend-model-catalogs.md
+++ b/docs/plans/backend-model-catalogs.md
@@ -274,6 +274,13 @@ the contract guard as the test. Nothing here introduces a second vocabulary for
models.dev suggestion. A proposal contains only values the catalog can store — an effort
value that violates `backend-model.schema.json` is left out of the proposal, never copied. `PUT` stores the request literally — an empty field in the request
is an empty field — so a value the user cleared before the first Save stays cleared.
+ One later fill exists, for C-9 budgets: a `provider` row with no `models_dev_id` was
+ written while no models.dev copy was cached (the picker on a cold cache, or Vibey's seed),
+ so it never had a proposal to keep or clear. When a copy arrives, or on the controller's
+ snapshot cadence, such a row whose id names an entry exactly (the C2 match) takes each of
+ `context_window` and `max_output_tokens` it lacks and that entry's `models_dev_id`, through
+ the ordinary committed catalog change (MH-LIMITS-001). A limit already set stands, a row
+ with a `models_dev_id` is never filled, and built-in and manual rows are not filled.
Server-side seeding at write time exists only for built-ins the reconcile (C6) adds without
a user draft, from the same snapshot values. `origin` records the creation path only —
`builtin` (built-in group or reconcile), `provider` (provider group), `models_dev` (custom
diff --git a/docs/plans/model-hub-contracts/vibey-consumer.md b/docs/plans/model-hub-contracts/vibey-consumer.md
index f77215ea2..f9feb0601 100644
--- a/docs/plans/model-hub-contracts/vibey-consumer.md
+++ b/docs/plans/model-hub-contracts/vibey-consumer.md
@@ -32,7 +32,8 @@ rule, decided under the Agent store's write lock so a chosen model stands, run
on every Vibey catalog change (`_refresh_backend_catalog`, which each seed also
announces) and at every start, so a lost hand-off heals on the next start. A
seed with no models.dev copy cached first fetches one in the foreground, bounded,
-so its rows usually carry the limits the Agent budgets with; a starting model is
+so its rows usually carry the limits the Agent budgets with, and rows written
+without one take them once a copy arrives (MH-LIMITS-001); a starting model is
one a placed Source's own non-retired inventory lists, never a passthrough route. A native Source that later moves
to the Hub joins Vibey like a new Source.
Where a user Agent already holds the name and no built-in exists, only the
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index 06a77d840..2cff3ec41 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -147,6 +147,15 @@ scenarios:
- tests/test_vibey_backend_registration.py::test_vibey_agent_runs_its_catalogs_first_model_until_it_has_one
- tests/test_vibey_backend_registration.py::test_a_lost_vibey_model_hand_off_heals_on_the_next_start
- tests/test_controller_im_ready.py::test_runtime_services_start_when_post_update_notification_fails
+ - id: MH-LIMITS-001
+ name: Provider rows written before any models.dev copy was cached take its limits once one arrives, keeping every limit already set
+ status: covered
+ kind: recovery
+ layer: contract
+ test: tests/test_model_hub_vibey_consumer.py::test_mh_limits_001_rows_written_before_a_models_dev_copy_take_its_limits_once_it_arrives
+ related_tests:
+ - tests/test_models_dev_catalog.py::test_mh_limits_001_every_saved_copy_signals_its_arrival
+ - tests/test_controller_model_hub_gate.py::test_controller_builds_one_model_hub_aggregate_by_default_or_explicit_enable
- id: MH-VIBEY-004
name: Vibey refuses native CLI sources and Direct mode
status: covered
diff --git a/tests/test_controller_model_hub_gate.py b/tests/test_controller_model_hub_gate.py
index b6bb6f25b..0e4a119e1 100644
--- a/tests/test_controller_model_hub_gate.py
+++ b/tests/test_controller_model_hub_gate.py
@@ -50,10 +50,11 @@ def test_controller_builds_one_model_hub_aggregate_by_default_or_explicit_enable
import core.handlers.model_hub.turn_gateway as turn_gateway
import modules.agents.model_hub as agent_model_hub
import vibe.model_hub_runtime as model_hub_runtime
- from vibe import api, backend_model_catalog
+ from vibe import api, backend_model_catalog, models_dev_catalog
service = SimpleNamespace(
reconcile_builtin_models=AsyncMock(return_value=[]),
+ backfill_models_dev_limits=AsyncMock(return_value=[]),
)
calls = []
@@ -97,6 +98,12 @@ def test_controller_builds_one_model_hub_aggregate_by_default_or_explicit_enable
"set_remote_catalog_refresh_completed",
refresh_callbacks.append,
)
+ arrival_callbacks = []
+ monkeypatch.setattr(
+ models_dev_catalog,
+ "set_models_dev_copy_arrived",
+ arrival_callbacks.append,
+ )
presence_probes = []
probe_failure = [False]
block_full_probe = [False]
@@ -144,6 +151,8 @@ def test_controller_builds_one_model_hub_aggregate_by_default_or_explicit_enable
assert captured["cli_present_override"]("claude") is False
service.reconcile_builtin_models.assert_awaited_once_with(notify=False)
assert refresh_callbacks == [controller._model_hub_snapshot_refresh_completed]
+ # MH-LIMITS-001: a models.dev copy landing takes the same refresh path.
+ assert arrival_callbacks == [controller._model_hub_snapshot_refresh_completed]
assert presence_probes == [(["claude", "codex", "opencode"], False)]
probe_failure[0] = True
captured["cli_presence_refresh"](True, ("opencode",))
@@ -219,6 +228,7 @@ def test_controller_builds_one_model_hub_aggregate_by_default_or_explicit_enable
asyncio.run(drain_refresh())
assert service.reconcile_builtin_models.await_count == 2
+ service.backfill_models_dev_limits.assert_awaited_once_with()
# A catalog change moves each runtime at its next turn: the agent adopts
# the committed catalog, and nothing restarts.
@@ -247,6 +257,7 @@ async def test_controller_periodic_tick_reconciles_cross_process_snapshot_change
controller = Controller.__new__(Controller)
controller.model_hub_service = SimpleNamespace(
reconcile_builtin_models=reconcile_builtin_models,
+ backfill_models_dev_limits=AsyncMock(return_value=[]),
)
controller._loop = asyncio.get_running_loop()
controller._shutdown_requested = False
@@ -285,6 +296,7 @@ async def test_controller_shutdown_joins_reconcile_and_rejects_late_completion()
controller = Controller.__new__(Controller)
controller.model_hub_service = SimpleNamespace(
reconcile_builtin_models=reconcile_builtin_models,
+ backfill_models_dev_limits=AsyncMock(return_value=[]),
stop=stop,
)
controller.runtime_work_supervisor = None
diff --git a/tests/test_model_hub_vibey_consumer.py b/tests/test_model_hub_vibey_consumer.py
index 52c4c25dd..59c67e2a7 100644
--- a/tests/test_model_hub_vibey_consumer.py
+++ b/tests/test_model_hub_vibey_consumer.py
@@ -759,6 +759,77 @@ async def test_the_first_source_seed_fetches_a_models_dev_copy_when_none_is_cach
assert (row["id"], row["context_window"], row["max_output_tokens"]) == ("claude-opus-5-5", 1_000_000, 128_000)
+async def test_mh_limits_001_rows_written_before_a_models_dev_copy_take_its_limits_once_it_arrives(
+ tmp_path, monkeypatch,
+):
+ """MH-LIMITS-001: a row written with no models.dev copy cached gets its limits when one arrives.
+
+ The seed's foreground fetch can fail while a background refresh lands later,
+ and a picker add on a cold cache has no metadata either; the Agent then
+ budgets the row with default limits. Only the limits a provider row lacks
+ are filled: what a user, a seed or models.dev already stated stands, and
+ built-in and manual rows are described by their CLI and their user.
+ """
+ catalog = {"anthropic": {"name": "Anthropic", "models": {
+ "claude-opus-5-5": {"name": "Claude Opus 5.5", "limit": {"context": 1_000_000, "output": 128_000}},
+ "claude-sonnet-5-5": {"name": "Claude Sonnet 5.5", "limit": {"context": 400_000, "output": 64_000}},
+ }}}
+ payload = ModelHubConfig().to_payload()
+ payload["agents"]["claude"]["models"] = [
+ {"id": "claude-opus-5-5", "origin": "builtin"},
+ {"id": "claude-sonnet-5-5", "origin": "manual"},
+ ]
+ payload["agents"]["opencode"]["models"] = [
+ # The user set the context window when adding it.
+ {"id": "claude-sonnet-5-5", "origin": "provider", "native_protocol": "anthropic", "context_window": 200_000},
+ # models.dev described it, and the user cleared both limits since.
+ {
+ "id": "claude-opus-5-5", "origin": "provider", "native_protocol": "anthropic",
+ "models_dev_id": "anthropic/claude-opus-5-5",
+ },
+ ]
+ payload["agents"]["codex"]["models"] = [{"id": "gpt-local-only", "origin": "provider"}]
+ service, store, announced = _seed_service(tmp_path, payload, [("claude", "claude-opus-5-5")])
+ cold = {}
+ service.models_dev_catalog = lambda: cold
+
+ def foreground_fetch_fails():
+ raise RuntimeError("models.dev catalog is unavailable")
+
+ monkeypatch.setattr("vibe.models_dev_catalog.load_models_dev_catalog", foreground_fetch_fails)
+ await service._commit_new_source_locked(copy.deepcopy(SEED_ANTHROPIC))
+ [seeded] = store.payload["agents"]["vibey"]["models"]
+ assert (seeded["id"], seeded["context_window"], seeded["max_output_tokens"]) == ("claude-opus-5-5", None, None)
+ assert announced == ["vibey"]
+ before = copy.deepcopy(store.payload)
+
+ assert await service.backfill_models_dev_limits() == []
+ assert store.payload == before
+
+ cold.update(catalog)
+ assert await service.backfill_models_dev_limits() == ["vibey", "opencode"]
+
+ def limits(backend):
+ return [
+ (row["id"], row["models_dev_id"], row["context_window"], row["max_output_tokens"])
+ for row in store.payload["agents"][backend]["models"]
+ ]
+
+ assert limits("vibey") == [("claude-opus-5-5", "anthropic/claude-opus-5-5", 1_000_000, 128_000)]
+ assert limits("opencode") == [
+ ("claude-sonnet-5-5", "anthropic/claude-sonnet-5-5", 200_000, 64_000),
+ ("claude-opus-5-5", "anthropic/claude-opus-5-5", None, None),
+ ]
+ for backend in ("claude", "codex"):
+ assert store.payload["agents"][backend] == before["agents"][backend]
+ # Committed as a catalog change, so each runtime takes it at its next turn.
+ assert announced == ["vibey", "vibey", "opencode"]
+ filled = copy.deepcopy(store.payload)
+ assert await service.backfill_models_dev_limits() == []
+ assert store.payload == filled
+
+
def test_native_cli_and_direct_are_not_vibey_channels(tmp_path):
"""MH-VIBEY-004: neither invalid persisted state nor routing can select a CLI."""
native = _source("src_native001", "Native", channel="native_cli", vendor="anthropic", protocol="anthropic")
diff --git a/tests/test_models_dev_catalog.py b/tests/test_models_dev_catalog.py
index 7ce7f4cec..e2dda008d 100644
--- a/tests/test_models_dev_catalog.py
+++ b/tests/test_models_dev_catalog.py
@@ -505,3 +505,55 @@ def test_first_catalog_read_reports_one_fetch_in_flight_until_it_fails(monkeypat
# That retry runs on its own thread: it must finish while this test's fetch
# and cache path are still patched, not after teardown restores the real ones.
join_refreshes()
+
+
[email protected]_catalog_refresh
+def test_mh_limits_001_every_saved_copy_signals_its_arrival(monkeypatch, tmp_path):
+ """MH-LIMITS-001: a copy saved by a foreground or a background fetch signals; no other fetch does.
+
+ Rows written while no copy was cached are filled when one arrives, and the
+ copy that most often arrives is the background refresh a picker read starts.
+ """
+
+ import threading
+
+ monkeypatch.setattr(models_dev_catalog, "_cache_path", lambda: tmp_path / "models_dev_catalog.json")
+ monkeypatch.setattr(models_dev_catalog, "_refresh_in_flight", False)
+ monkeypatch.setattr(models_dev_catalog, "_last_refresh_failed", False)
+ monkeypatch.setattr(models_dev_catalog, "_COPY_ARRIVED", None)
+ arrivals: list[str] = []
+ models_dev_catalog.set_models_dev_copy_arrived(lambda: arrivals.append(threading.current_thread().name))
+ outcomes: list[object] = []
+
+ def urlopen(_request, timeout):
+ outcome = outcomes.pop(0)
+ if isinstance(outcome, Exception):
+ raise outcome
+ return outcome
+
+ monkeypatch.setattr(models_dev_catalog.urllib.request, "urlopen", urlopen)
+
+ def refresh_in_background(outcome):
+ models_dev_catalog._write_cache({**models_dev_catalog._read_cache(), "fetched_at": 0})
+ outcomes.append(outcome)
+ assert models_dev_catalog.load_models_dev_catalog_with_date()[0] == _catalog()
+ for thread in threading.enumerate():
+ if thread.name == "models-dev-refresh":
+ thread.join(timeout=5)
+ assert not outcomes
+
+ outcomes.append(urllib.error.URLError("offline"))
+ with pytest.raises(RuntimeError, match="unavailable"):
+ models_dev_catalog.load_models_dev_catalog()
+ assert arrivals == []
+
+ outcomes.append(_Response(_catalog(), headers={"ETag": "v1"}))
+ assert models_dev_catalog.load_models_dev_catalog() == _catalog()
+ assert arrivals == [threading.current_thread().name]
+
+ # Unchanged upstream: the copy stays as it was, and nothing arrived.
+ refresh_in_background(urllib.error.HTTPError(models_dev_catalog._models_dev_url(), 304, "Not Modified", {}, None))
+ assert arrivals == [threading.current_thread().name]
+
+ refresh_in_background(_Response(_catalog(), headers={"ETag": "v2"}))
+ assert arrivals == [threading.current_thread().name, "models-dev-refresh"]
diff --git a/vibe/models_dev_catalog.py b/vibe/models_dev_catalog.py
index 3e3b0930a..336180522 100644
--- a/vibe/models_dev_catalog.py
+++ b/vibe/models_dev_catalog.py
@@ -11,7 +11,7 @@ import time
import urllib.error
import urllib.request
from pathlib import Path
-from typing import Any, Iterable
+from typing import Any, Callable, Iterable
from config import paths
from config.atomic_io import write_atomic
@@ -31,6 +31,7 @@ MODELS_DEV_CACHE_TTL_SECONDS = 24 * 60 * 60
MODELS_DEV_MAX_BYTES = 16 * 1024 * 1024
MODELS_DEV_MAX_MATCHES = 8
_CACHE_LOCK = threading.Lock()
+_COPY_ARRIVED: Callable[[], None] | None = None
def _vendor_map_path() -> Path:
@@ -166,6 +167,12 @@ def _fetch_catalog(previous: dict[str, Any]) -> dict[str, Any]:
if isinstance(value, str) and value:
payload[key] = value
_write_cache(payload)
+ arrived = _COPY_ARRIVED
+ if arrived is not None:
+ try:
+ arrived()
+ except Exception: # noqa: BLE001 - the copy is saved and every reader sees it
+ logger.warning("models.dev copy arrival signal failed", exc_info=True)
return catalog
except urllib.error.HTTPError as exc:
if exc.code == 304:
@@ -177,6 +184,19 @@ def _fetch_catalog(previous: dict[str, Any]) -> dict[str, Any]:
raise
+def set_models_dev_copy_arrived(callback: Callable[[], None] | None) -> None:
+ """Set the controller-owned signal that a new copy landed in this process.
+
+ A row written while no copy was cached lacks the metadata one gives it, and
+ its owner fills it once a copy arrives. Every fetch that saves a copy, in
+ the foreground or the background, calls the signal on its own thread, the
+ foreground one still holding ``_CACHE_LOCK``, so the signal only hands off.
+ """
+
+ global _COPY_ARRIVED
+ _COPY_ARRIVED = callback
+
+
def load_models_dev_catalog() -> dict[str, Any]:
with _CACHE_LOCK:
cached = _read_cache()