`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.
103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
"""Regression guard for #62151 — gateway cron must not wedge on the 2nd+ call.
|
|
|
|
Gateway-fired cron jobs hung forever on the 2nd+ API call because both the
|
|
non-streaming (``interruptible_api_call``) and the default streaming
|
|
(``interruptible_streaming_api_call``) paths run the request on a spawned
|
|
daemon worker thread. Inside the gateway's nested cron thread pools that extra
|
|
worker wedged before the socket opened; the same job succeeded via ``hermes
|
|
cron tick`` (foreground, no nested pools). Cron has no interactive interrupt
|
|
surface, so both paths now run inline on the conversation thread for the
|
|
``cron`` platform.
|
|
|
|
These tests pin: (1) the inline gate is cron-only, (2) the inline call runs on
|
|
the *calling* thread — no worker is spawned — for both entry points, and (3)
|
|
the shared dispatch closes the per-request client.
|
|
"""
|
|
|
|
import threading
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock
|
|
|
|
from run_agent import AIAgent
|
|
|
|
from agent.chat_completion_helpers import (
|
|
direct_api_call,
|
|
interruptible_api_call,
|
|
interruptible_streaming_api_call,
|
|
should_use_direct_api_call,
|
|
)
|
|
|
|
|
|
def _make_agent(*, platform="cron"):
|
|
agent = MagicMock()
|
|
agent.platform = platform
|
|
agent.api_mode = "chat_completions"
|
|
agent.provider = "openrouter"
|
|
agent._interrupt_requested = False
|
|
agent._consecutive_stale_streams = 0
|
|
agent._touch_activity = MagicMock()
|
|
agent._close_request_openai_client = MagicMock()
|
|
return agent
|
|
|
|
|
|
def test_should_use_direct_api_call_only_for_cron_openai_wire():
|
|
assert should_use_direct_api_call(_make_agent(platform="cron")) is True
|
|
assert should_use_direct_api_call(_make_agent(platform="cli")) is False
|
|
assert should_use_direct_api_call(_make_agent(platform="telegram")) is False
|
|
assert should_use_direct_api_call(_make_agent(platform=None)) is False
|
|
|
|
for api_mode in ("codex_responses", "anthropic_messages", "bedrock_converse"):
|
|
agent = _make_agent(platform="cron")
|
|
agent.api_mode = api_mode
|
|
assert should_use_direct_api_call(agent) is False
|
|
|
|
moa = _make_agent(platform="cron")
|
|
moa.provider = "moa"
|
|
assert should_use_direct_api_call(moa) is False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_direct_api_call_interrupt_aborts_active_client_and_raises():
|
|
"""Cron's outer watchdog interrupts from another thread while inline."""
|
|
agent = _make_agent()
|
|
client_ready = threading.Event()
|
|
release_request = threading.Event()
|
|
fake_client = MagicMock()
|
|
|
|
def _create(**_kwargs):
|
|
client_ready.set()
|
|
return fake_client
|
|
|
|
def _request(**_kwargs):
|
|
assert release_request.wait(timeout=2)
|
|
raise RuntimeError("socket closed")
|
|
|
|
fake_client.chat.completions.create.side_effect = _request
|
|
agent._create_request_openai_client.side_effect = _create
|
|
result = {}
|
|
|
|
def _run():
|
|
try:
|
|
direct_api_call(agent, {"model": "m", "messages": []})
|
|
except Exception as exc:
|
|
result["exception"] = exc
|
|
|
|
worker = threading.Thread(target=_run)
|
|
worker.start()
|
|
assert client_ready.wait(timeout=1)
|
|
assert callable(agent._active_request_abort)
|
|
AIAgent.interrupt(agent, "cron timeout")
|
|
agent._abort_request_openai_client.assert_called_once_with(
|
|
fake_client, reason="interrupt_abort"
|
|
)
|
|
release_request.set()
|
|
worker.join(timeout=2)
|
|
assert not worker.is_alive()
|
|
assert isinstance(result.get("exception"), InterruptedError)
|
|
assert agent._active_request_abort is None
|
|
|
|
|