Files
hermes-agent/tests/agent/test_copilot_initiator.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

140 lines
5.1 KiB
Python

"""Tests for per-turn Copilot x-initiator header injection (issue #3040).
Copilot bills "premium requests" only when a request is marked as
user-initiated via the ``x-initiator: user`` header. Hermes previously sent
``x-initiator: agent`` on every request (client-level default headers), so
user prompts never consumed premium requests and were throttled as agent
traffic. The fix marks the FIRST API call of each user turn as "user" and
lets tool-loop follow-ups keep the "agent" default.
Salvaged from PR #4097 (@tjp2021); adapted to the post-refactor layout
(conversation_loop.py owns the injection site, the codex transport now
accepts extra_headers).
"""
import pytest
from run_agent import AIAgent
def _tool_defs(*names):
return [
{"type": "function", "function": {"name": n, "description": n, "parameters": {}}}
for n in names
]
class _FakeOpenAI:
def __init__(self, **kw):
self.api_key = kw.get("api_key", "test")
self.base_url = kw.get("base_url", "http://test")
def close(self):
pass
def _make_agent(monkeypatch, base_url, api_mode="chat_completions"):
"""Create an AIAgent pointing at the given base_url."""
monkeypatch.setattr("model_tools.get_tool_definitions", lambda **kw: _tool_defs("web_search"))
monkeypatch.setattr("model_tools.check_toolset_requirements", lambda: {})
monkeypatch.setattr("agent.process_bootstrap.OpenAI", _FakeOpenAI)
return AIAgent(
api_key="test-key",
base_url=base_url,
provider="copilot" if "githubcopilot" in base_url else "openrouter",
api_mode=api_mode,
max_iterations=4,
quiet_mode=True,
skip_context_files=True,
skip_memory=True,
)
def _inject(agent, api_kwargs):
"""Mirror the injection block in agent/conversation_loop.py."""
if getattr(agent, "_is_user_initiated_turn", False) and agent._is_copilot_url():
_xh = dict(api_kwargs.get("extra_headers") or {})
_xh["x-initiator"] = "user"
api_kwargs["extra_headers"] = _xh
agent._is_user_initiated_turn = False
return api_kwargs
class TestIsCopilotUrl:
"""_is_copilot_url() detects GitHub Copilot endpoints."""
def test_standard_copilot_url(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://api.githubcopilot.com")
assert agent._is_copilot_url() is True
def test_github_models_url(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://models.github.ai/inference")
assert agent._is_copilot_url() is True
def test_openrouter_url(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://openrouter.ai/api/v1")
assert agent._is_copilot_url() is False
def test_case_insensitive(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://API.GITHUBCOPILOT.COM")
assert agent._is_copilot_url() is True
class TestUserInitiatedTurnFlag:
"""_is_user_initiated_turn lifecycle."""
def test_default_is_false(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://api.githubcopilot.com")
assert agent._is_user_initiated_turn is False
def test_reset_session_clears_flag(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://api.githubcopilot.com")
agent._is_user_initiated_turn = True
agent.reset_session_state()
assert agent._is_user_initiated_turn is False
class TestFlagFlipOnInjection:
"""Flag flips immediately on injection so tool-loop calls use 'agent'."""
def test_first_call_injects_user_initiator(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://api.githubcopilot.com")
agent._is_user_initiated_turn = True
kwargs = _inject(agent, {})
assert kwargs["extra_headers"] == {"x-initiator": "user"}
assert agent._is_user_initiated_turn is False
def test_second_call_has_no_injection(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://api.githubcopilot.com")
agent._is_user_initiated_turn = True
kwargs1 = _inject(agent, {})
kwargs2 = _inject(agent, {})
assert "extra_headers" in kwargs1
assert "extra_headers" not in kwargs2
def test_non_copilot_flag_not_flipped(self, monkeypatch):
agent = _make_agent(monkeypatch, "https://openrouter.ai/api/v1")
agent._is_user_initiated_turn = True
kwargs = _inject(agent, {})
assert "extra_headers" not in kwargs
# Flag unchanged — non-Copilot path doesn't touch it
assert agent._is_user_initiated_turn is True
class TestHeaderValues:
"""copilot_default_headers(is_agent_turn=...) sets x-initiator correctly."""
def test_default_is_agent(self):
from hermes_cli.models import copilot_default_headers
assert copilot_default_headers()["x-initiator"] == "agent"
def test_user_turn(self):
from hermes_cli.models import copilot_default_headers
assert copilot_default_headers(is_agent_turn=False)["x-initiator"] == "user"
def test_agent_turn_explicit(self):
from hermes_cli.models import copilot_default_headers
assert copilot_default_headers(is_agent_turn=True)["x-initiator"] == "agent"