Skip to content

The agent that grows with you

fix(config-remote): review round 9d: custom-endpoint make-default refuses when its provider entry is locked

Ben Barclayd70ad6d86d5d
diff --git a/hermes_cli/web_routers/config_env.py b/hermes_cli/web_routers/config_env.py
index b31b862a21e..676272cb93c 100644
--- a/hermes_cli/web_routers/config_env.py
+++ b/hermes_cli/web_routers/config_env.py
@@ -726,6 +726,10 @@ def _write_custom_endpoint(cfg: dict[str, Any], body: CustomEndpointUpdate) -> t
return endpoint_id, entry
+# Sections a custom-endpoint default assignment writes together: the route and its provider entry.
+_ROUTE_SECTIONS = ("model", "providers", "custom_providers")
+
+
@router.get("/api/providers/custom-endpoints")
def list_custom_endpoints(profile: Optional[str] = None):
"""Return configured OpenAI-compatible custom endpoints for Desktop.
@@ -752,7 +756,9 @@ def upsert_custom_endpoint(body: CustomEndpointUpdate, profile: Optional[str] =
created = _resolve_custom_endpoint_entry(
providers if isinstance(providers, dict) else {}, body.id or body.name)[1] is None
endpoint_id, _entry = _write_custom_endpoint(cfg, body)
- with refusing_locked_omissions("model") if body.make_default else contextlib.nullcontext():
+ # make_default: the model route and the provider definition it names are one change,
+ # so a lock on either refuses both (never a model.provider naming an omitted entry).
+ with refusing_locked_omissions(*_ROUTE_SECTIONS) if body.make_default else contextlib.nullcontext():
save_config(cfg)
response = _custom_endpoint_response(cfg)
from hermes_constants import get_hermes_home
@@ -819,7 +825,7 @@ def activate_custom_endpoint(endpoint_id: str, profile: Optional[str] = None):
else:
model_cfg["api_key"] = entry["api_key"]
cfg["model"] = model_cfg
- with refusing_locked_omissions("model"):
+ with refusing_locked_omissions(*_ROUTE_SECTIONS):
save_config(cfg)
return {"ok": True, "provider": provider_key, "model": model}
diff --git a/tests/plugins/config_backends/test_remote_backend.py b/tests/plugins/config_backends/test_remote_backend.py
index 829c8298e68..9c49015ddf4 100644
--- a/tests/plugins/config_backends/test_remote_backend.py
+++ b/tests/plugins/config_backends/test_remote_backend.py
@@ -1813,3 +1813,41 @@ def test_dashboard_model_switch_refuses_when_a_lock_lands_after_the_guard(plane,
if not conflict_first:
assert plane.patches() == []
assert plane.profile("default")["values"].get("model") in (None, {}), plane.profile("default")
+
+
[email protected]("schedule", ["locked", "late-poll", "late-retry"])
+def test_custom_endpoint_make_default_refuses_when_its_provider_entry_is_locked(plane, monkeypatch, schedule):
+ """Creating a named custom endpoint with make_default writes providers.<id> AND the model route
+ naming it. A lock on providers (present up front, seen by a poll before the save, or by the
+ re-read after a 409) refuses both: never model.provider naming an entry that was not saved."""
+ from fastapi import HTTPException
+ from hermes_cli.model_switch import ModelSwitchResult
+ from hermes_cli.web_models import CustomEndpointUpdate
+ from hermes_cli.web_routers import config_env
+ plane.upper = {"model": {"default": "old-model", "provider": "custom:old", "base_url": "https://old.example/v1"},
+ "providers": {"custom:old": {"name": "old", "base_url": "https://old.example/v1", "model": "old-model"}}}
+ lock = [{"path": "providers", "level": "tenant"}]
+ if schedule == "locked":
+ plane.upper_locks = lock
+ monkeypatch.setattr(config_env, "_record_custom_endpoint_setup", lambda home: None)
+ monkeypatch.setattr(config_env, "_validated_main_model_selection", lambda cfg, provider, model, base_url: ModelSwitchResult(
+ success=True, new_model=model, target_provider=provider, provider_changed=True, base_url=base_url,
+ api_mode="chat_completions", is_global=True))
+ if schedule != "locked":
+ backend = get_config_backend()
+ orig = config_env.save_config
+
+ def lock_then_save(cfg, **kw):
+ plane.upper_locks = lock
+ if schedule == "late-poll":
+ assert backend.poll_one(backend._state(plane.home))
+ else:
+ plane.profile("default")["version"] += 1
+ return orig(cfg, **kw)
+
+ monkeypatch.setattr(config_env, "save_config", lock_then_save)
+ with pytest.raises(HTTPException) as exc:
+ config_env.upsert_custom_endpoint(CustomEndpointUpdate(
+ id="new-probe", name="new-probe", base_url="https://new.example/v1", model="new-model", make_default=True))
+ assert exc.value.status_code == 403, exc.value
+ assert plane.profile("default")["values"].get("model") in (None, {}), plane.profile("default")