Skip to content

The agent that grows with you

fix(model): keep YAML-aliased model routes on the new selection

Teknium76c302955102
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index ccb756d8b65..5d45e3407aa 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -1849,7 +1849,15 @@ def persist_model_selection(result: ModelSwitchResult, config_path: Any = None)
model_cfg = config.get("model")
if model_cfg != planned_from:
updates = model_selection_config_updates(result, model_cfg)
- config["model"] = _merge_selection_updates(model_cfg, updates)
+ merged = _merge_selection_updates(model_cfg, updates)
+ if isinstance(model_cfg, dict):
+ # A YAML anchor on ``model:`` reused elsewhere (``vision: *route``) loads as ONE shared
+ # dict; edit it in place so every alias carries the new route, as the shared node on
+ # disk will. A fresh copy left the alias stale and the round-trip merge wrote it back.
+ model_cfg.clear()
+ model_cfg.update(merged)
+ else:
+ config["model"] = merged
atomic_config_replace(path, config)
try: # owner-only: config files contain API keys
os.chmod(path, 0o600)
diff --git a/tests/hermes_cli/test_model_selection_atomic_persistence.py b/tests/hermes_cli/test_model_selection_atomic_persistence.py
index 95784451e72..978d7626056 100644
--- a/tests/hermes_cli/test_model_selection_atomic_persistence.py
+++ b/tests/hermes_cli/test_model_selection_atomic_persistence.py
@@ -135,3 +135,25 @@ def test_setting_saved_while_route_is_decided_survives(profile_config, monkeypat
saved = yaml.safe_load(profile_config.read_text(encoding="utf-8-sig"))
assert wrote and saved["display"]["theme"] == "dark"
assert saved["model"] == _EXPECTED
+
+
+def test_anchored_model_route_reused_later_saves_the_new_route(tmp_path, monkeypatch):
+ """``model: &route`` aliased later (``vision: *route``) must not resurrect the old route."""
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ path = tmp_path / "config.yaml"
+ path.write_text(
+ "model: &route\n"
+ " default: old-model\n"
+ " provider: custom:old\n"
+ " base_url: https://old.example.test/v1\n"
+ " api_mode: chat_completions\n"
+ "auxiliary:\n"
+ " vision: *route\n",
+ encoding="utf-8",
+ )
+ persist_model_selection(_RESULT, path)
+
+ saved = yaml.safe_load(path.read_text(encoding="utf-8-sig"))
+ route = {k: v for k, v in _EXPECTED.items() if k != "model_slots"}
+ assert saved["model"] == route
+ assert saved["auxiliary"]["vision"] == route # the alias still shares the node, as on main