Files
hermes-agent/tests/gateway/test_model_alias_credential.py
Teknium 1a74bb74cb fix(gateway): /model <alias> commits a reference to the alias's own literal key
Mechanism: a literal model_aliases.<n>.api_key (and `${VAR}`, which
load_config expands first) was stored as a private config secret at
('model', 'api_key'). After any owner restart recover_config_secrets re-read
config.yaml at model.api_key, where the key is not, so the fingerprint check
failed and every turn and every later /model on that session answered
launch_credentials_unavailable until /new. Separately, _alias_credential read
the process-global DIRECT_ALIASES after switch_model's network validation; in a
multiplexed owner another served profile could refill it in that window and
this session committed THAT profile's key for this profile's endpoint.

Fix: _alias_credential reads the alias entry from this owner's own
config.yaml (raw for the `${VAR}` pointer, effective for the literal), never
the shared cache. A literal key is a private config secret at the alias
entry's own path (model_aliases.<as written>.api_key or
model.aliases.<name>.api_key), which recover_config_secrets re-derives and
fingerprints after a restart; the receipt carries only the non-secret pointer
model.api_key_from, which LocalSessionPolicy.config() projects into
model.api_key. `${VAR}` / `${env:VAR}` commit as key_env. No result.api_key
bind; endpoint A's launch key still drops on the route change.

Test: tests/gateway/test_model_alias_credential_restart.py (real prepare_model,
build_policy/bind_launch_key, _resolve_policy_agent_runtime, SessionDB, a real
config.yaml): red on r12-base with 'sk-other-profile' committed (cross-profile)
and, with the cache seeded correctly, launch_credentials_unavailable after a
fresh authority (restart). The existing alias test now writes its aliases to
the owner's config.yaml.

Review: dokterdok N48
Review: dokterdok N30
Review: dokterdok E02
Review: JoaoMarcos44 B34
(cherry picked from commit 95101bc37f41861e8dde14a65569fd7e7a38671e)
2026-10-11 07:34:30 -07:00

55 lines
4.0 KiB
Python

"""``/model <alias>`` keeps the alias endpoint's own credential on the next turn."""
import pytest
@pytest.mark.asyncio
async def test_model_switch_to_a_keyed_direct_alias_runs_the_next_turn_with_its_own_key(tmp_path, monkeypatch):
"""A direct alias declares its endpoint's credential (``key_env`` or a literal ``api_key``). The
route change drops the old endpoint's credentials; the alias's own must take their place in the
committed selection, or the next turn sends ``no-key-required`` to the alias host."""
from dataclasses import asdict
from types import SimpleNamespace
from gateway.run_turn_prepare import _resolve_policy_agent_runtime
from gateway.session_mutation_model import prepare_model
from gateway.session_policy import bind_launch_key, build_policy, restore_policy
from hermes_state import SessionDB
from hermes_cli import model_switch
old_url, far_url = 'http://127.0.0.1:9/v1', 'http://127.0.0.3:9/v1'
monkeypatch.setenv('FAR_ALIAS_KEY', 'sk-far-env')
monkeypatch.setitem(model_switch.DIRECT_ALIASES, 'far-env', model_switch.DirectAlias(
'far-model', 'custom', far_url, key_env='FAR_ALIAS_KEY'))
monkeypatch.setitem(model_switch.DIRECT_ALIASES, 'far-literal', model_switch.DirectAlias(
'far-model', 'custom', far_url, api_key='sk-far-literal'))
monkeypatch.setitem(model_switch.DIRECT_ALIASES, 'far-ref', model_switch.DirectAlias(
'far-model', 'custom', far_url, api_key='${FAR_ALIAS_KEY}'))
# The owner reads the alias credential from its own profile's config.yaml (N48).
(tmp_path / 'config.yaml').write_text(
'model_aliases:\n'
f' far-env: {{model: far-model, provider: custom, base_url: "{far_url}", key_env: FAR_ALIAS_KEY}}\n'
f' far-literal: {{model: far-model, provider: custom, base_url: "{far_url}", api_key: sk-far-literal}}\n'
f' far-ref: {{model: far-model, provider: custom, base_url: "{far_url}", api_key: "${{FAR_ALIAS_KEY}}"}}\n')
with SessionDB(tmp_path / 'state.db') as db:
authority = SimpleNamespace(instance_id='i', epoch=1, profile_id='p', db=db)
runner = SimpleNamespace(session_authority=authority,
_resolve_session_agent_runtime=lambda **k: (None, {'api_key': 'sk-endpoint-a'}))
authority.runner = runner
config = {'model': {'provider': 'custom', 'default': 'a', 'base_url': old_url, 'key_env': 'ENDPOINT_A_KEY'}}
private = {}
policy = build_policy({'cwd': str(tmp_path), 'model': 'a', 'provider': 'custom', 'base_url': old_url,
'api_key': 'sk-endpoint-a'}, config, private_secrets=private)
policy = bind_launch_key(authority, 'sid', policy, 'sk-endpoint-a', config_secrets=private)
prepared = {'snapshot': {'receipt': {'session_id': 'sid', 'policy': asdict(policy)}}}
# Every credential shape a direct alias can declare (DirectAlias / direct_alias_api_key).
for alias, expected in (('far-env', 'sk-far-env'), ('far-literal', 'sk-far-literal'),
('far-ref', 'sk-far-env')):
monkeypatch.setattr(model_switch, 'switch_model', lambda alias=alias, **k: model_switch.ModelSwitchResult(
success=True, new_model='far-model', target_provider='custom', base_url=far_url,
api_key='resolved-at-switch', resolved_via_alias=alias))
switched = restore_policy((await prepare_model(authority, SimpleNamespace(source=None, route='r'),
{'model': alias}, prepared))['policy'])
assert switched.credential_ref is None, "endpoint A's launch key crossed to the alias host"
assert 'sk-far-literal' not in switched.config_json, 'a literal alias key must stay private'
runtime = _resolve_policy_agent_runtime(runner, switched)[1]
assert runtime['base_url'].rstrip('/') == far_url
assert runtime['api_key'] == expected, f'{alias}: the next turn ran without the alias key'