Files
hermes-agent/tests/gateway/test_model_command_request_overrides.py
teknium1 1a59519244 fix(gateway): a --global /model pick leaves config.yaml as the only durable model authority
A `/model <m> --global` switch (typed or picker) used to persist twice: the profile
config.yaml AND a per-session `model_override` in the session store. The session copy
has higher precedence on rehydration, so after a later global change (CLI `hermes model`,
another chat's `--global`) and a gateway restart the stale override silently won —
#100314 saw an explicit `gpt-5.6-sol-900k` resume as the base 272K `gpt-5.6-sol`.

Now `_record_model_switch` writes config.yaml FIRST and, on success, drops the redundant
session override from memory and the store. If the config write fails the switch stays a
truthful session override and the confirmation says "config.yaml not updated (...)" plus
the session-only hint instead of claiming "Saved to config.yaml"; a riding `--reasoning`
pin follows the same effective scope. `--session` and `--once` semantics are unchanged.

Tests: two invariants in tests/gateway/test_model_picker_persist.py (typed+picker clear
the durable override and a fresh SessionStore rehydrates nothing; failed config write keeps
the override and an honest reply), red on base. test_model_command_request_overrides now
points get_hermes_home at its own config so the --provider switch resolves session-scoped
as intended instead of the sandbox's fresh-install first-pick rule.

Fixes #100314
Supersedes #99825 (slim redo; the original wrapped the commit boundary through a
sys.modules-swapped mixin).

Co-authored-by: Andrex Ibiza, MBA <andrexibiza@gmail.com>
2026-09-19 09:59:32 -07:00

98 lines
3.0 KiB
Python

"""Regression tests for gateway /model preserving named-custom request_overrides."""
import pytest
from gateway.config import Platform
from gateway.platforms.event import MessageEvent, MessageType
from gateway.run import GatewayRunner
from gateway.session import SessionSource
def _make_runner():
runner = object.__new__(GatewayRunner)
runner.adapters = {}
runner._voice_mode = {}
runner._session_model_overrides = {}
runner._pending_model_notes = {}
runner._agent_cache = {}
runner._agent_cache_lock = None
runner._session_db = None
runner._evict_cached_agent = lambda _session_key: None
runner.session_store = None
return runner
def _make_event(text="/model"):
return MessageEvent(
text=text,
message_type=MessageType.TEXT,
source=SessionSource(
platform=Platform.FEISHU,
chat_id="ou_test",
chat_type="dm",
user_id="user-1",
),
)
@pytest.mark.asyncio
async def test_handle_model_command_stores_request_overrides_for_named_custom_provider(
tmp_path,
monkeypatch,
):
import gateway.run as gateway_run
from hermes_cli.model_switch import ModelSwitchResult
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
(hermes_home / "config.yaml").write_text(
"""
model:
default: gpt-5.4
provider: openai-codex
providers: {}
custom_providers:
- name: Local (127.0.0.1:4141)
base_url: http://127.0.0.1:4141/v1
model: rotator-openrouter-coding
extra_body:
text:
verbosity: low
""".lstrip(),
encoding="utf-8",
)
monkeypatch.setattr(gateway_run, "_hermes_home", hermes_home)
# resolve_persist_behavior() reads the profile config through get_hermes_home(); without this
# the sandbox home looks like a fresh install and the --provider switch persists globally.
monkeypatch.setattr("hermes_cli.config.get_hermes_home", lambda: hermes_home)
monkeypatch.setattr("agent.models_dev.fetch_models_dev", lambda: {})
monkeypatch.setattr(
"hermes_cli.model_switch.switch_model",
lambda **kw: ModelSwitchResult(
success=True,
new_model="rotator-openrouter-coding",
target_provider="custom:local-(127.0.0.1:4141)",
provider_changed=True,
api_key="no-key-required",
base_url="http://127.0.0.1:4141/v1",
api_mode="codex_responses",
request_overrides={
"extra_body": {"text": {"verbosity": "low"}},
},
provider_label="Local (127.0.0.1:4141)",
is_global=False,
),
)
runner = _make_runner()
event = _make_event("/model rotator-openrouter-coding --provider custom:local-(127.0.0.1:4141)")
result = await runner._handle_model_command(event)
assert result is not None
session_key = runner._session_key_for_source(event.source)
assert runner._session_model_overrides[session_key]["request_overrides"] == {
"extra_body": {"text": {"verbosity": "low"}},
}