fix(model): custom-to-custom switch drops the previous endpoint's inline api_key
model_selection_config_updates cleared model.api_key/api only when the target was not a custom provider, so custom:legacy-box -> custom:other-box (or the same 'custom' provider on a different base_url) carried endpoint A's inline secret to endpoint B. The old dashboard writer cleared on any provider change. The inline key now survives only a same-route re-pick (same provider AND same normalized base_url); an explicitly submitted key is re-added by the dashboard after this shape is applied, as before.
This commit is contained in:
@@ -1527,7 +1527,10 @@ def model_selection_config_updates(result: ModelSwitchResult, current_model_cfg:
|
||||
``None`` when the target has none — otherwise the OLD provider's endpoint/wire-protocol lingers
|
||||
(#25106). A context pin is dropped only when its route identity changed (fail-closed).
|
||||
Non-custom targets resolve credentials from env/auth.json/the pool, so an inline
|
||||
``model.api_key`` is a leftover that would contaminate later custom resolution."""
|
||||
``model.api_key`` is a leftover that would contaminate later custom resolution. For custom
|
||||
targets the inline key belongs to ONE endpoint: it survives only a same-route re-pick (same
|
||||
provider and base_url) — ``custom:a`` -> ``custom:b`` must not hand endpoint A's secret to B.
|
||||
The dashboard re-adds an explicitly submitted key after this (``_apply_main_model_assignment``)."""
|
||||
model_cfg = current_model_cfg if isinstance(current_model_cfg, dict) else {}
|
||||
updates: dict[str, Any] = {
|
||||
"default": result.new_model, "provider": result.target_provider,
|
||||
@@ -1539,13 +1542,22 @@ def model_selection_config_updates(result: ModelSwitchResult, current_model_cfg:
|
||||
model_cfg.get("default") or model_cfg.get("model"), result.new_model,
|
||||
model_cfg.get("base_url"), result.base_url, model_cfg.get("provider"), result.target_provider):
|
||||
updates["context_length"] = None
|
||||
if not str(result.target_provider or "").strip().lower().startswith("custom"):
|
||||
target = str(result.target_provider or "").strip().lower()
|
||||
if not target.startswith("custom") or _route_changed(model_cfg, result):
|
||||
for key in ("api_key", "api"):
|
||||
if key in model_cfg:
|
||||
updates[key] = None
|
||||
return updates
|
||||
|
||||
|
||||
def _route_changed(model_cfg: dict, result: ModelSwitchResult) -> bool:
|
||||
"""Provider or endpoint differs between the on-disk ``model:`` block and the switch target."""
|
||||
from hermes_cli.route_identity import normalize_route_base_url
|
||||
if str(model_cfg.get("provider") or "").strip().lower() != str(result.target_provider or "").strip().lower():
|
||||
return True
|
||||
return normalize_route_base_url(model_cfg.get("base_url")) != normalize_route_base_url(result.base_url)
|
||||
|
||||
|
||||
def apply_model_selection(model_cfg: Any, result: ModelSwitchResult) -> dict:
|
||||
"""Apply the canonical shape to an in-memory ``model:`` dict (``None`` = key removed) for
|
||||
callers that save a whole config document they are already mutating."""
|
||||
|
||||
@@ -113,6 +113,25 @@ def test_same_route_repick_keeps_the_context_pin(seeded_home):
|
||||
assert block["api_key"] == "sk-stale" # custom targets keep their inline key
|
||||
|
||||
|
||||
def test_custom_to_other_custom_endpoint_drops_the_inline_key(seeded_home):
|
||||
"""``custom`` -> ``custom:other-box``: endpoint A's inline ``api_key`` must not become endpoint B's
|
||||
credential (the pointer-not-secret rule, #88990). Same provider *string* but a different
|
||||
``base_url`` drops it too — the key belongs to one endpoint, not to the word "custom"."""
|
||||
from hermes_cli.model_switch import persist_model_selection
|
||||
other_provider = ModelSwitchResult(
|
||||
success=True, new_model="other-model", target_provider="custom:other-box",
|
||||
base_url="http://other-box:8000/v1", api_mode="chat_completions", is_global=True)
|
||||
persist_model_selection(other_provider)
|
||||
assert "api_key" not in _model_block(seeded_home)
|
||||
|
||||
(seeded_home / "config.yaml").write_text(_SEED, encoding="utf-8")
|
||||
same_provider_other_host = ModelSwitchResult(
|
||||
success=True, new_model="local-model", target_provider="custom",
|
||||
base_url="http://10.0.0.9:1234/v1", api_mode="anthropic_messages", is_global=True)
|
||||
persist_model_selection(same_provider_other_host)
|
||||
assert "api_key" not in _model_block(seeded_home)
|
||||
|
||||
|
||||
def test_gateway_persists_to_the_profile_config_it_was_given(tmp_path, monkeypatch):
|
||||
"""Multiplexed gateway: the write lands in the routed profile's config.yaml, never the
|
||||
process-level HERMES_HOME."""
|
||||
|
||||
Reference in New Issue
Block a user