hermes-agent/tests/agent/test_auxiliary_relay.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

262 lines
7.6 KiB
Python

from types import SimpleNamespace
import pytest
pytest.importorskip("nemo_relay")
from agent import auxiliary_client, relay_llm, relay_runtime
@pytest.fixture()
def relay_turn(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "profile"))
relay_runtime._reset_for_tests()
lease = relay_runtime.SESSION_COORDINATOR.acquire_conversation(
profile_key=relay_runtime.current_profile_key(),
session_id="session-1",
platform="cli",
)
turn = relay_runtime.SESSION_COORDINATOR.begin_turn(
lease,
turn_id="turn-1",
task_id="task-1",
)
try:
yield lease.host.relay, turn
finally:
relay_runtime.SESSION_COORDINATOR.end_turn(turn, outcome="success")
relay_runtime.SESSION_COORDINATOR.release_conversation(lease)
relay_runtime._reset_for_tests()
def test_auxiliary_retries_share_logical_relay_identity(monkeypatch):
attempts = []
logical_completions = []
responses = iter([
SimpleNamespace(choices=[]),
SimpleNamespace(
choices=[SimpleNamespace(message=SimpleNamespace(content="ok"))]
),
])
client = SimpleNamespace(
chat=SimpleNamespace(
completions=SimpleNamespace(
create=lambda **_kwargs: next(responses),
)
)
)
def execute_current(request, callback, **kwargs):
attempts.append(kwargs)
return callback(request)
monkeypatch.setattr(relay_llm, "execute_current", execute_current)
monkeypatch.setattr(
relay_llm,
"complete_logical_call",
lambda request_id, *, outcome: logical_completions.append(
(request_id, outcome)
),
)
@auxiliary_client._relay_auxiliary_call
def run(task):
auxiliary_client._set_relay_auxiliary_route(
"openrouter",
"test-model",
"chat_completions",
)
with pytest.raises(RuntimeError, match="invalid response"):
auxiliary_client._validate_llm_response(
auxiliary_client._relay_sync_completion(
client,
{"model": "test-model", "messages": []},
),
task,
)
return auxiliary_client._validate_llm_response(
auxiliary_client._relay_sync_completion(
client,
{"model": "test-model", "messages": []},
),
task,
)
result = run("compression")
assert result.choices[0].message.content == "ok"
assert attempts[0]["metadata"]["api_request_id"] == (
attempts[1]["metadata"]["api_request_id"]
)
assert [attempt["metadata"]["retry_count"] for attempt in attempts] == [0, 1]
assert attempts[0]["metadata"]["call_role"] == "auxiliary:compression"
assert all(attempt["defer_logical_completion"] is True for attempt in attempts)
assert logical_completions == [
(attempts[0]["metadata"]["api_request_id"], "success")
]
@pytest.mark.asyncio
async def test_async_auxiliary_attempt_uses_inherited_relay_adapter(monkeypatch):
captured = {}
logical_completions = []
async def create(**kwargs):
return SimpleNamespace(
request=kwargs,
choices=[SimpleNamespace(message=SimpleNamespace(content="ok"))],
)
client = SimpleNamespace(
chat=SimpleNamespace(completions=SimpleNamespace(create=create))
)
async def execute_current_async(request, callback, **kwargs):
captured.update(kwargs)
return await callback(request)
monkeypatch.setattr(
relay_llm,
"execute_current_async",
execute_current_async,
)
monkeypatch.setattr(
relay_llm,
"complete_logical_call",
lambda request_id, *, outcome: logical_completions.append(
(request_id, outcome)
),
)
@auxiliary_client._relay_auxiliary_call_async
async def run(task):
auxiliary_client._set_relay_auxiliary_route(
"anthropic",
"claude-test",
"chat_completions",
)
return auxiliary_client._validate_llm_response(
await auxiliary_client._relay_async_completion(
client,
{"model": "claude-test", "messages": []},
),
task,
)
result = await run("title_generation")
assert result.request["model"] == "claude-test"
assert captured["name"] == "anthropic"
assert captured["metadata"]["call_role"] == "auxiliary:title_generation"
assert captured["defer_logical_completion"] is True
assert logical_completions == [
(captured["metadata"]["api_request_id"], "success")
]
def test_partial_auxiliary_stream_failure_closes_before_recovery(
relay_turn, monkeypatch
):
_relay, turn = relay_turn
consumer = "test.partial-auxiliary-stream-failure"
turn.lease.host.retain_managed_execution(consumer)
outcomes = []
original_pop = turn.lease.host.relay.scope.pop
def record_pop(*args, **kwargs):
outcomes.append((kwargs.get("output") or {}).get("outcome"))
return original_pop(*args, **kwargs)
monkeypatch.setattr(turn.lease.host.relay.scope, "pop", record_pop)
class ProviderError(Exception):
pass
provider_error = ProviderError("stream failed")
partial_chunk = SimpleNamespace(
model="test-model",
choices=[
SimpleNamespace(
delta=SimpleNamespace(content="partial", tool_calls=None),
finish_reason=None,
)
],
usage=None,
)
def partial_stream():
yield partial_chunk
raise provider_error
stream_client = SimpleNamespace(
chat=SimpleNamespace(
completions=SimpleNamespace(
create=lambda **_kwargs: partial_stream(),
)
)
)
recovery_client = SimpleNamespace(
chat=SimpleNamespace(
completions=SimpleNamespace(
create=lambda **_kwargs: SimpleNamespace(
choices=[
SimpleNamespace(message=SimpleNamespace(content="recovered"))
]
),
)
)
)
@auxiliary_client._relay_auxiliary_call
def start_stream(task):
auxiliary_client._set_relay_auxiliary_route(
"openrouter",
"test-model",
"chat_completions",
)
return auxiliary_client._relay_sync_stream(
stream_client,
{"model": "test-model", "messages": [], "stream": True},
)
@auxiliary_client._relay_auxiliary_call
def recover(task):
auxiliary_client._set_relay_auxiliary_route(
"openrouter",
"test-model",
"chat_completions",
)
return auxiliary_client._validate_llm_response(
auxiliary_client._relay_sync_completion(
recovery_client,
{"model": "test-model", "messages": []},
),
task,
)
try:
stream = start_stream("moa")
assert next(stream) is partial_chunk
with pytest.raises(ProviderError) as caught:
next(stream)
assert caught.value is provider_error
assert outcomes == ["failed"]
assert turn.logical_llm_calls == {}
result = recover("moa")
assert result.choices[0].message.content == "recovered"
assert outcomes == ["failed", "success"]
assert turn.logical_llm_calls == {}
finally:
turn.lease.host.release_managed_execution(consumer)