hermes-agent/tests/acp_adapter/test_session_reasoning_config.py
Austin Pickett 81f481b2db fix(agent): carry a custom provider's extra_body into TUI/Desktop agents and aux calls
TUI/Desktop `_make_agent` (plus `hermes -z` and ACP) built AIAgent without the
resolver's `request_overrides`, so a custom entry's `extra_body` only reached
the agent through the init-time re-derivation, which matches `custom:*` alone.
Auxiliary requests never read the entry's `extra_body` at all. A proxy that
requires a body field (`user`) therefore 400'd in the app and on smart
approval while `hermes chat` worked.

- Pass `runtime["request_overrides"]` at every remaining AIAgent build site
  (ACP only when no explicit base_url points at another endpoint).
- `_build_call_kwargs` layers the destination entry's `extra_body` under the
  task/caller body, using the agent's own matcher, so primary, retry and
  fallback requests each get their own endpoint's body and nothing leaks.

Covers holes 1 and 3 of #103738; hole 2 (matching bare/unprefixed names) is
#124290 and is untouched here.
2026-09-28 19:49:00 -04:00

73 lines
3.6 KiB
Python

"""ACP sessions honor the configured reasoning setting (#85153).
``SessionManager._make_agent`` builds its ``AIAgent`` from config like every other surface but never
passed ``reasoning_config``, so ``agent.reasoning_effort: none`` was ignored and the transport applied its
default effort — a 400 on non-reasoning models such as ``gpt-4o-mini``. Real config-file → ``load_config``
→ ``resolve_reasoning_config`` chain on the per-test ``HERMES_HOME``; only the agent constructor and
provider resolution are stubbed.
"""
import os
from pathlib import Path
import pytest
import hermes_yaml as yaml
from acp_adapter.session import SessionManager
class _CapturingAgent:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.model = kwargs.get("model") or "stub-model"
@pytest.fixture
def acp_env(monkeypatch, tmp_path):
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
monkeypatch.setattr("run_agent.AIAgent", _CapturingAgent)
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda requested=None, **_kwargs: {"provider": requested or "openai-api", "api_mode": "codex_responses",
"base_url": "https://example.invalid/v1", "api_key": "test-key"},
)
monkeypatch.setattr("acp_adapter.session._register_task_cwd", lambda task_id, cwd: None)
monkeypatch.setattr("hermes_cli.mcp_startup.ensure_mcp_discovery_before_agent_build", lambda **_kwargs: None)
def _write_config(cfg: dict) -> None:
(Path(os.environ["HERMES_HOME"]) / "config.yaml").write_text(yaml.safe_dump(cfg), encoding="utf-8")
return _write_config
def test_acp_agent_receives_configured_reasoning(acp_env):
acp_env({"model": {"default": "gpt-4o-mini", "provider": "openai-api"}, "agent": {"reasoning_effort": "none"}})
sm = SessionManager(db=None)
sm._get_db = lambda: None
agent = sm._make_agent(session_id="s1", cwd=".")
assert agent.kwargs["reasoning_config"] == {"enabled": False}
# Per-model overrides key off the session's model, not ``model.default``.
acp_env({"model": {"default": "gpt-4o-mini", "provider": "openai-api"},
"agent": {"reasoning_effort": "none", "reasoning_overrides": {"gpt-5.6": "high"}}})
agent = sm._make_agent(session_id="s2", cwd=".", model="gpt-5.6")
assert agent.kwargs["reasoning_config"] == {"enabled": True, "effort": "high"}
def test_acp_agent_receives_custom_provider_request_body(acp_env, monkeypatch):
"""#103738: the resolver's ``request_overrides`` (a custom entry's ``extra_body``) reach the ACP agent, as
on the CLI; an explicit base_url naming a different endpoint does not inherit them."""
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda requested=None, **_kwargs: {"provider": "custom", "api_mode": "chat_completions",
"base_url": "https://proxy.example/v1", "api_key": "k",
"request_overrides": {"extra_body": {"user": "proxy-user"}}},
)
acp_env({"model": {"default": "some-model", "provider": "custom:my-proxy"}})
sm = SessionManager(db=None)
sm._get_db = lambda: None
assert sm._make_agent(session_id="s1", cwd=".").kwargs["request_overrides"] == {"extra_body": {"user": "proxy-user"}}
same = sm._make_agent(session_id="s2", cwd=".", base_url="https://proxy.example/v1")
assert same.kwargs["request_overrides"] == {"extra_body": {"user": "proxy-user"}}
other = sm._make_agent(session_id="s3", cwd=".", base_url="https://elsewhere.example/v1")
assert "request_overrides" not in other.kwargs