hermes-agent/tests/test_copilot_initiator.py
Teknium 6b81590c55
test: prune low-value tests suite-wide (wave 1) — 46,820 → 28,106 test functions
Systematic prune per AGENTS.md test policy, one pass over every major
test tree (gateway, hermes_cli, tools, agent, run_agent, plugins, cli,
cron, tui_gateway, honcho/openviking, root-level):

- DELETE: source-reading tests (read_text/getsource on prod files),
  change-detector tests (exact catalog counts, model-name snapshots,
  config version literals), mock-echo tests (assert a mock returns what
  it was told), assertion-free/trivial tests, near-duplicate
  parametrizations (boundaries + one representative kept), async/sync
  twin duplicates, cosmetic within-file variations.
- KEEP (mandatory): security/redaction/approval guards, message-role
  alternation invariants, prompt-caching/deterministic-call-id
  invariants, issue-number regression tests (deduped), E2E tests.
- 6 test files deleted outright (script-style/no-assert or fully
  redundant); conftest.py, fakes/, fixtures/ untouched.
- tests/acp/conftest.py added: autouse fixture stubs the live
  models.dev/GitHub/Copilot/Anthropic inventory fetches that ACP server
  tests performed on every session create — test_server.py 147s → 3.4s,
  and the tests are now genuinely hermetic.
- Sleep-based slowness shrunk where safe (codex_ttfb_watchdog,
  compression_concurrent_fork, etc.); no wall-clock assertion tightened.

Verification: full hermetic suite via scripts/run_tests.sh —
2439 files, 31,130 tests passed, 0 failed, 0 flaky retries, 315s wall
(baseline: 583s wall, 13,564s subprocess CPU).
2026-07-29 13:10:23 -07:00

139 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("run_agent.get_tool_definitions", lambda **kw: _tool_defs("web_search"))
monkeypatch.setattr("run_agent.check_toolset_requirements", lambda: {})
monkeypatch.setattr("run_agent.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"