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(config): migrate released OpenCode Model Hub catalogs

CodeHolic7c028d74ebc4
diff --git a/config/v2_config.py b/config/v2_config.py
index 68671855d..2b15933df 100644
--- a/config/v2_config.py
+++ b/config/v2_config.py
@@ -686,6 +686,82 @@ def _migrate_legacy_model_hub_payload(payload: dict) -> tuple[dict, bool, tuple[
return migrated_payload, True, tuple(warnings)
+def _migrate_legacy_opencode_catalog(payload: dict) -> tuple[dict, bool, tuple[str, ...]]:
+ """Fill released catalog omissions using only persisted routing evidence.
+
+ This is a disk-load migration, not a relaxed write parser. A selected model
+ with no unambiguous, representable native protocol must keep the original
+ file and the ordinary recovery write guard instead of losing its selection.
+ """
+ hub = payload.get("model_hub")
+ if not isinstance(hub, dict) or not isinstance(hub.get("agents"), dict):
+ return payload, False, ()
+ agent = hub["agents"].get("opencode")
+ if not isinstance(agent, dict):
+ return payload, False, ()
+ rows = agent.get("models")
+ missing_catalog = "models" not in agent
+ if not missing_catalog and (
+ not isinstance(rows, list)
+ or not any(isinstance(row, dict) and "native_protocol" not in row for row in rows)
+ ):
+ return payload, False, ()
+
+ try:
+ menu = ModelHubMenuConfig.from_payload(agent.get("menu"))
+ if missing_catalog:
+ rows = [{"id": model_id} for model_id in menu.checked]
+ models = [ModelHubBackendModelConfig.from_payload(row) for row in rows]
+ source_order = ModelHubAgentSourcesConfig.from_payload(agent.get("sources", {"order": []})).order
+ raw_sources = hub.get("sources", [])
+ raw_routes = agent.get("routes", {})
+ if not isinstance(raw_sources, list) or not isinstance(raw_routes, dict):
+ return payload, False, ()
+ sources = {
+ source.id: source
+ for source in (
+ ModelHubSourceConfig.from_payload(raw, repairing=True)
+ for raw in raw_sources
+ )
+ }
+ routes = {
+ model_id: ModelHubRouteConfig.from_payload(route, repairing=True)
+ for model_id, route in raw_routes.items()
+ }
+ except (TypeError, ValueError):
+ # Let the strict parser report malformed/retired fields as before.
+ return payload, False, ()
+
+ migrated_rows = copy.deepcopy(rows)
+ for row, model in zip(migrated_rows, models):
+ if "native_protocol" in row:
+ continue
+ route = routes.get(model.id)
+ if route and route.hops:
+ supplier_ids = [hop.source_id for hop in route.hops]
+ else:
+ supplier_ids = [
+ source_id for source_id in source_order
+ if source_id in sources
+ and any(item.id == model.id for item in sources[source_id].models)
+ ]
+ protocols = {
+ sources[source_id].protocol if source_id in sources else None
+ for source_id in supplier_ids
+ }
+ if len(protocols) != 1 or not protocols <= {"anthropic", "openai_responses"}:
+ return payload, False, (
+ "Model Hub legacy OpenCode selection has no unambiguous supported native_protocol; "
+ "repair model_hub.agents.opencode.models with an explicit native_protocol "
+ "for each selected model. The original config is preserved.",
+ )
+ row["native_protocol"] = next(iter(protocols))
+
+ migrated = copy.deepcopy(payload)
+ migrated["model_hub"]["agents"]["opencode"]["models"] = migrated_rows
+ return migrated, True, ()
+
+
def _migrate_opencode_active_turn_timeout_on_load(payload: dict) -> dict:
"""Neutralize the legacy wall-clock default echo on reload, once.
@@ -726,9 +802,10 @@ def _migrate_opencode_active_turn_timeout_on_load(payload: dict) -> dict:
def _migrate_config_payload_on_load(payload: dict) -> tuple[dict, bool, tuple[str, ...]]:
migrated, changed, warnings = _migrate_legacy_model_hub_payload(payload)
- model_hub_changed = changed or migrated.get("model_hub") != payload.get("model_hub")
+ migrated, catalog_changed, catalog_warnings = _migrate_legacy_opencode_catalog(migrated)
+ model_hub_changed = changed or catalog_changed or migrated.get("model_hub") != payload.get("model_hub")
migrated = _migrate_opencode_active_turn_timeout_on_load(migrated)
- return migrated, model_hub_changed, warnings
+ return migrated, model_hub_changed, (*warnings, *catalog_warnings)
# The spellings a hand-edited config may use for a boolean, both sides in one
diff --git a/docs/plans/model-hub-legacy-opencode-migration.md b/docs/plans/model-hub-legacy-opencode-migration.md
new file mode 100644
index 000000000..09729720a
--- /dev/null
+++ b/docs/plans/model-hub-legacy-opencode-migration.md
@@ -0,0 +1,33 @@
+# Released OpenCode catalog migration (MODEL-HUB-MIGRATION-001)
+
+The released Model Hub serializer could omit OpenCode `models`, including on
+unused, empty catalogs. The current strict parser requires that catalog and a
+`native_protocol` on each row. Disk loading must bridge those released shapes
+before validation; API writes keep the current schema.
+
+This supersedes the no-upgrade ruling in backend-model-catalogs.md C12 only at
+the disk-load boundary. Retired fields, including OpenCode `mappings`, remain
+invalid. No UI, runtime routing, schema, or credential behavior changes.
+
+Invariants:
+
+- Missing catalogs become rows in the existing `menu.checked` order; an empty
+ selection becomes `models: []`. Existing rows retain their IDs and metadata.
+- A missing native protocol is supplied only by unambiguous persisted route
+ Sources, or matching model inventory in the configured Source order when no
+ route override exists. Only current native protocols are representable;
+ Chat Completions, conflicting protocols, and missing evidence require repair.
+- Keep selected IDs, exact routes, Source order, opaque credential references,
+ and unrelated config. Do not guess protocols from model names or providers.
+- Modern configs are unchanged. Ambiguous or malformed input retains the
+ original file and uses the existing recovery warning/write guard. Successful
+ migration uses the existing private backup and concurrent-write protections.
+- Migration is forward-only. The backup preserves the original bytes for manual
+ recovery; no automated downgrade/rollback support is introduced. The current
+ pre-fix reader accepts the migrated current-schema payload.
+
+Validation uses real disk loads, persistence/reload, an unchanged modern control,
+failure preservation, settings API recovery/save, existing Model Hub/config and
+Member permission suites, and changed-file lint. Tests isolate all home/backend
+state and never invoke real providers. PM owns independent browser acceptance,
+PR, review/CI, and any later deployment decision.
diff --git a/tests/scenarios/INDEX.yaml b/tests/scenarios/INDEX.yaml
index a75e6f213..9c00cae59 100644
--- a/tests/scenarios/INDEX.yaml
+++ b/tests/scenarios/INDEX.yaml
@@ -143,6 +143,8 @@ capabilities:
- tests/test_model_hub_api.py
- tests/test_model_hub_admission.py
- tests/test_model_hub_config.py
+ - tests/test_model_hub_legacy_opencode_migration.py
+ - tests/test_member_management_parity.py
- tests/test_model_hub_provenance.py
- tests/test_model_hub_retention.py
- tests/test_model_hub_resolution.py
diff --git a/tests/scenarios/model_hub/catalog.yaml b/tests/scenarios/model_hub/catalog.yaml
index e1effcb03..092f37192 100644
--- a/tests/scenarios/model_hub/catalog.yaml
+++ b/tests/scenarios/model_hub/catalog.yaml
@@ -5,6 +5,8 @@ capability:
canonical_tests:
- tests/test_model_hub_api.py
- tests/test_model_hub_config.py
+ - tests/test_model_hub_legacy_opencode_migration.py
+ - tests/test_member_management_parity.py
- tests/test_model_hub_resolution.py
- tests/test_model_hub_injection.py
- tests/test_model_hub_runtime.py
@@ -1090,6 +1092,13 @@ scenarios:
reason: image-block terminal framing requires the private credential issued to a live backend turn
owning_layer: future live-turn suite (turn gateway)
evidence: docs/plans/model-hub-e2e-test-plan.md#h-cross-protocol-functional-floor-owner-ruling-baseline
+ - id: MODEL-HUB-MIGRATION-001
+ name: Released OpenCode catalogs migrate before strict validation and restore settings saves
+ status: covered
+ kind: migration
+ layer: config-api
+ test: tests/test_model_hub_legacy_opencode_migration.py::test_legacy_selected_models_preserve_sources_routes_and_order
+ evidence: tests/test_member_management_parity.py::test_legacy_opencode_catalog_clears_recovery_and_allows_config_save
next_priority:
- MH-PROTOCOL-004
- MH-RUNTIME-001
diff --git a/tests/test_member_management_parity.py b/tests/test_member_management_parity.py
index 8a24b68da..185f70b81 100644
--- a/tests/test_member_management_parity.py
+++ b/tests/test_member_management_parity.py
@@ -761,6 +761,27 @@ def test_member_settings_save_uses_config_recovery_semantics(management_http, ra
assert config_path.read_bytes() == raw # The policy read never persists recovery.
[email protected]("role", ["member", "owner"])
+def test_legacy_opencode_catalog_clears_recovery_and_allows_config_save(management_http, role):
+ """MODEL-HUB-MIGRATION-001: disk migration restores settings writes for both roles."""
+ request = management_http(role)
+ path = paths.get_config_path()
+ payload = json.loads(path.read_text())
+ payload["model_hub"]["agents"]["opencode"].pop("models")
+ path.write_text(json.dumps(payload))
+
+ response = request("GET", "/api/config")
+ assert response.status_code == 200, response.get_json()
+ assert response.get_json()["config_recovery"]["required"] is False
+ response = request("POST", "/api/config", payload={"show_duration": True})
+ assert response.status_code == 200, response.get_json()
+ saved = V2Config.load()
+ assert saved.load_warnings == ()
+ assert saved.show_duration is True
+ assert saved.model_hub.agents["opencode"].models == []
+ assert json.loads(path.read_text())["remote_access"] == payload["remote_access"]
+
+
@pytest.mark.parametrize("discord", [[], "invalid", {"bot_token": 12}])
@pytest.mark.parametrize("surface", ["channels", "thread", "users"])
def test_remote_member_can_save_with_recovered_discord_section(management_http, discord, surface):
diff --git a/tests/test_model_hub_legacy_opencode_migration.py b/tests/test_model_hub_legacy_opencode_migration.py
new file mode 100644
index 000000000..458f77686
--- /dev/null
+++ b/tests/test_model_hub_legacy_opencode_migration.py
@@ -0,0 +1,294 @@
+"""MODEL-HUB-MIGRATION-001: released catalogs load before strict validation."""
+
+from __future__ import annotations
+
+import copy
+import json
+import stat
+from pathlib import Path
+
+import pytest
+
+from config import v2_config
+from config.v2_config import ModelHubBackendModelConfig, ModelHubConfig, V2Config
+from core.services.settings import default_config
+from vibe import api
+
+
+def _source(source_id, protocol, model_ids):
+ example = json.loads(Path("docs/plans/model-hub-contracts/source.schema.json").read_text())["examples"][1]
+ source = copy.deepcopy(example)
+ source.update(id=source_id, protocol=protocol, credential_ref=f"opaque:{source_id}")
+ source["models"] = [
+ {"id": model_id, "origin": "manual", "reasoning_efforts": []}
+ for model_id in model_ids
+ ]
+ return source
+
+
+def _payload():
+ payload = api.config_to_payload(default_config(), include_secrets=True, include_internal=True)
+ payload["show_duration"] = True
+ payload["migration_sentinel"] = {"keep": "配置"}
+ payload["model_hub"]["enabled"] = True
+ payload["model_hub"]["agents"]["opencode"].pop("models")
+ return payload
+
+
+def _write(tmp_path, payload):
+ path = tmp_path / "config.json"
+ path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
+ return path, path.read_bytes()
+
+
+def _assert_migrated(path, original):
+ loaded = V2Config.load(config_path=path)
+ assert loaded.load_warnings == ()
+ assert loaded.recovered_sections == ()
+ assert loaded.model_hub.enabled is True
+ persisted = json.loads(path.read_text())
+ # Compare disk JSON to disk JSON: API metadata also contains Python tuples.
+ assert {k: v for k, v in persisted.items() if k != "model_hub"} == {
+ k: v for k, v in json.loads(original).items() if k != "model_hub"
+ }
+ [backup] = path.parent.glob("config.json.bak-model-hub-migration-*")
+ assert backup.read_bytes() == original
+ assert stat.S_IMODE(backup.stat().st_mode) == 0o600
+ after = path.read_bytes()
+ reloaded = V2Config.load(config_path=path)
+ assert reloaded.load_warnings == ()
+ assert reloaded.model_hub.to_payload() == loaded.model_hub.to_payload()
+ assert path.read_bytes() == after
+ assert len(list(path.parent.glob("config.json.bak-*"))) == 1
+ # The new artifact remains ordinary current-schema input, without a marker
+ # that requires the migration helper to read it (also the pre-fix parser).
+ assert ModelHubConfig.from_payload(persisted["model_hub"]).to_payload() == loaded.model_hub.to_payload()
+ return loaded
+
+
[email protected]("mode", ["direct", "hub"])
+def test_legacy_empty_opencode_catalog_migrates_and_reloads(tmp_path, mode):
+ payload = _payload()
+ payload["model_hub"]["agents"]["opencode"]["mode"] = mode
+ path, original = _write(tmp_path, payload)
+
+ loaded = _assert_migrated(path, original)
+
+ assert loaded.model_hub.agents["opencode"].models == []
+ assert loaded.model_hub.agents["opencode"].mode == mode
+ assert json.loads(path.read_text())["model_hub"]["agents"]["opencode"]["models"] == []
+
+
[email protected]("protocol", ["anthropic", "openai_responses"])
[email protected]("authority", ["route", "inventory"])
+def test_legacy_selected_models_preserve_sources_routes_and_order(tmp_path, protocol, authority):
+ """MODEL-HUB-MIGRATION-001: selected models retain their persisted routing evidence."""
+ payload = _payload()
+ hub = payload["model_hub"]
+ agent = hub["agents"]["opencode"]
+ model_ids = ["vendor/自定义模型", "model-second"]
+ source_ids = ["src_migration01", "src_migration02"]
+ source_model_ids = ["upstream-first", "upstream-second"] if authority == "route" else model_ids
+ hub["sources"] = [_source(source_id, protocol, source_model_ids) for source_id in source_ids]
+ agent["menu"] = {"view": "full", "checked": model_ids}
+ agent["sources"] = {"order": list(reversed(source_ids))}
+ agent["mode"] = "hub"
+ if authority == "route":
+ agent["routes"] = {
+ model_id: {"hops": [{"source_id": source_id, "model_id": upstream} for source_id in source_ids]}
+ for model_id, upstream in zip(model_ids, source_model_ids)
+ }
+ # A conflicting inventory is irrelevant to an explicit override.
+ hub["sources"].append(_source("src_unrelated01", "openai_chat", model_ids))
+ agent["sources"]["order"].insert(0, "src_unrelated01")
+ expected_sources = [v2_config.ModelHubSourceConfig.from_payload(s).to_payload() for s in hub["sources"]]
+ path, original = _write(tmp_path, payload)
+
+ loaded = _assert_migrated(path, original)
+
+ migrated = loaded.model_hub.agents["opencode"]
+ assert [(m.id, m.native_protocol) for m in migrated.models] == [(model_id, protocol) for model_id in model_ids]
+ assert migrated.menu.to_payload() == agent["menu"]
+ assert migrated.sources.to_payload() == agent["sources"]
+ assert {key: route.to_payload() for key, route in migrated.routes.items()} == agent["routes"]
+ assert [source.to_payload() for source in loaded.model_hub.sources] == expected_sources
+
+
+def test_legacy_explicit_catalog_keeps_row_metadata_and_modern_protocol(tmp_path):
+ payload = _payload()
+ hub = payload["model_hub"]
+ agent = hub["agents"]["opencode"]
+ legacy = ModelHubBackendModelConfig(
+ id="legacy-row", display_name="保留模型", origin="provider", context_window=64000,
+ reasoning_efforts=["low", "high"], native_protocol="anthropic",
+ ).to_payload()
+ legacy.pop("native_protocol")
+ modern = ModelHubBackendModelConfig(id="modern-row", native_protocol="openai_responses").to_payload()
+ agent["models"] = [legacy, modern]
+ agent["menu"]["checked"] = ["legacy-row", "modern-row"]
+ hub["sources"] = [_source("src_migration01", "anthropic", ["legacy-row"])]
+ agent["sources"]["order"] = ["src_migration01"]
+ path, original = _write(tmp_path, payload)
+
+ loaded = _assert_migrated(path, original)
+
+ assert [row.to_payload() for row in loaded.model_hub.agents["opencode"].models] == [
+ {**legacy, "native_protocol": "anthropic"}, modern,
+ ]
+
+
[email protected]("problem", ["missing", "conflicting", "chat", "missing-route-source"])
+def test_unrepresentable_legacy_selection_preserves_file_and_blocks_save(tmp_path, problem):
+ payload = _payload()
+ hub = payload["model_hub"]
+ agent = hub["agents"]["opencode"]
+ agent["menu"]["checked"] = ["model"]
+ if problem != "missing":
+ protocols = ["anthropic", "openai_responses"] if problem == "conflicting" else ["openai_chat"]
+ hub["sources"] = [_source(f"src_migration0{i}", protocol, ["model"]) for i, protocol in enumerate(protocols)]
+ agent["sources"]["order"] = [s["id"] for s in hub["sources"]]
+ if problem == "missing-route-source":
+ agent["routes"] = {"model": {"hops": [{"source_id": "src_missing001", "model_id": "model"}]}}
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert path.read_bytes() == original
+ assert loaded.load_warnings
+ assert "native_protocol" in " ".join(loaded.load_warnings)
+ assert "repair" in " ".join(loaded.load_warnings).lower()
+ assert loaded.show_duration is True
+ assert api.client_config_payload(loaded)["config_recovery"]["required"] is True
+ with pytest.raises(ValueError, match="recovery warnings"):
+ loaded.save(config_path=path)
+ assert path.read_bytes() == original
+ [backup] = path.parent.glob("config.json.bak-*")
+ assert backup.read_bytes() == original
+
+
[email protected]("nonempty", [False, True])
+def test_modern_opencode_catalog_is_unchanged_without_backup(tmp_path, nonempty):
+ payload = _payload()
+ agent = payload["model_hub"]["agents"]["opencode"]
+ agent["models"] = [ModelHubBackendModelConfig(id="model", native_protocol="anthropic").to_payload()] if nonempty else []
+ agent["menu"]["checked"] = ["model"] if nonempty else []
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.load_warnings == ()
+ assert path.read_bytes() == original
+ assert list(path.parent.glob("config.json.bak-*")) == []
+ assert loaded.model_hub.to_payload() == payload["model_hub"]
+
+
[email protected]("retired", ["mappings", "source-policy"])
+def test_legacy_opencode_migration_keeps_retired_field_validation(tmp_path, retired):
+ payload = _payload()
+ agent = payload["model_hub"]["agents"]["opencode"]
+ if retired == "mappings":
+ agent["mappings"] = []
+ else:
+ agent["sources"]["policy"] = "follow"
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.recovered_sections == ("model_hub",)
+ assert loaded.load_warnings
+ assert path.read_bytes() == original
+
+
+def test_legacy_opencode_migration_respects_read_only_load(tmp_path):
+ payload = _payload()
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path, persist_migrations=False)
+
+ assert loaded.load_warnings == ()
+ assert loaded.model_hub.enabled is True
+ assert loaded.model_hub.agents["opencode"].models == []
+ assert path.read_bytes() == original
+ assert list(path.parent.glob("config.json.bak-*")) == []
+
+
+def test_legacy_opencode_migration_requires_backup_before_persist(monkeypatch, tmp_path):
+ payload = _payload()
+ path, original = _write(tmp_path, payload)
+ monkeypatch.setattr(v2_config, "_backup_config_file", lambda *args, **kwargs: None)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.model_hub.enabled is True
+ assert "could not be backed up" in " ".join(loaded.load_warnings)
+ assert path.read_bytes() == original
+ with pytest.raises(ValueError, match="recovery warnings"):
+ loaded.save(config_path=path)
+
+
+def test_legacy_opencode_migration_does_not_replace_concurrent_save(monkeypatch, tmp_path):
+ payload = _payload()
+ path, original = _write(tmp_path, payload)
+ winner = copy.deepcopy(payload)
+ winner["model_hub"]["agents"]["opencode"]["models"] = []
+ winner["show_duration"] = False
+ winning_bytes = json.dumps(winner).encode()
+ write = v2_config._write_config_payload_if_unchanged
+
+ def concurrent_write(config_path, migrated_payload, expected_raw):
+ config_path.write_bytes(winning_bytes)
+ return write(config_path, migrated_payload, expected_raw)
+
+ monkeypatch.setattr(v2_config, "_write_config_payload_if_unchanged", concurrent_write)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.show_duration is False
+ assert "before replacement" in " ".join(loaded.load_warnings)
+ assert path.read_bytes() == winning_bytes
+ [backup] = path.parent.glob("config.json.bak-*")
+ assert backup.read_bytes() == original
+
+
[email protected]("models", [None, {}, [None], [{"id": "model", "native_protocol": None}]])
+def test_explicit_malformed_catalog_is_not_reinterpreted(tmp_path, models):
+ payload = _payload()
+ hub = payload["model_hub"]
+ agent = hub["agents"]["opencode"]
+ agent["models"] = models
+ agent["menu"]["checked"] = ["model"]
+ hub["sources"] = [_source("src_migration01", "anthropic", ["model"])]
+ agent["sources"]["order"] = ["src_migration01"]
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.recovered_sections == ("model_hub",)
+ assert loaded.load_warnings
+ assert path.read_bytes() == original
+
+
+def test_one_ambiguous_row_prevents_partial_catalog_migration(tmp_path):
+ payload = _payload()
+ hub = payload["model_hub"]
+ agent = hub["agents"]["opencode"]
+ agent["menu"]["checked"] = ["known", "unknown"]
+ hub["sources"] = [_source("src_migration01", "anthropic", ["known"])]
+ agent["sources"]["order"] = ["src_migration01"]
+ path, original = _write(tmp_path, payload)
+
+ loaded = V2Config.load(config_path=path)
+
+ assert loaded.load_warnings
+ assert path.read_bytes() == original
+ with pytest.raises(ValueError, match="recovery warnings"):
+ loaded.save(config_path=path)
+
+
[email protected]("catalog_present", [False, True])
+def test_legacy_catalog_remains_invalid_at_the_strict_write_boundary(catalog_present):
+ payload = _payload()["model_hub"]
+ if catalog_present:
+ payload["agents"]["opencode"]["models"] = [{"id": "model"}]
+ with pytest.raises(ValueError, match="models.*required"):
+ ModelHubConfig.from_payload(payload)