mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
255 lines
9.2 KiB
Python
255 lines
9.2 KiB
Python
"""Tests for acp_adapter.events — callback factories for ACP notifications."""
|
|
|
|
import asyncio
|
|
import gc
|
|
import warnings
|
|
from concurrent.futures import Future
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
import acp
|
|
from acp.schema import AgentPlanUpdate
|
|
|
|
from acp_adapter.events import (
|
|
_build_plan_update_from_todo_result,
|
|
_send_update,
|
|
make_message_cb,
|
|
make_step_cb,
|
|
make_thinking_cb,
|
|
make_tool_progress_cb,
|
|
)
|
|
|
|
|
|
@pytest.fixture()
|
|
def mock_conn():
|
|
"""Mock ACP Client connection."""
|
|
conn = MagicMock(spec=acp.Client)
|
|
conn.session_update = AsyncMock()
|
|
return conn
|
|
|
|
|
|
@pytest.fixture()
|
|
def event_loop_fixture():
|
|
"""Create a real event loop for testing threadsafe coroutine submission."""
|
|
loop = asyncio.new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Tool progress callback
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestToolProgressCallback:
|
|
def test_emits_tool_call_start(self, mock_conn, event_loop_fixture):
|
|
"""Tool progress should emit a ToolCallStart update."""
|
|
tool_call_ids = {}
|
|
tool_call_meta = {}
|
|
loop = event_loop_fixture
|
|
|
|
cb = make_tool_progress_cb(mock_conn, "session-1", loop, tool_call_ids, tool_call_meta)
|
|
|
|
# Run callback in the event loop context
|
|
with patch("acp_adapter.events.asyncio.run_coroutine_threadsafe") as mock_rcts:
|
|
future = MagicMock(spec=Future)
|
|
future.result.return_value = None
|
|
mock_rcts.return_value = future
|
|
|
|
cb("tool.started", "terminal", "$ ls -la", {"command": "ls -la"})
|
|
|
|
# Should have tracked the tool call ID
|
|
assert "terminal" in tool_call_ids
|
|
|
|
# Should have called run_coroutine_threadsafe
|
|
mock_rcts.assert_called_once()
|
|
coro = mock_rcts.call_args[0][0]
|
|
# The coroutine should be conn.session_update
|
|
assert mock_conn.session_update.called or coro is not None
|
|
|
|
|
|
|
|
def test_duplicate_same_name_tool_calls_use_fifo_ids(self, mock_conn, event_loop_fixture):
|
|
"""Multiple same-name tool calls should be tracked independently in order."""
|
|
tool_call_ids = {}
|
|
tool_call_meta = {}
|
|
loop = event_loop_fixture
|
|
|
|
progress_cb = make_tool_progress_cb(mock_conn, "session-1", loop, tool_call_ids, tool_call_meta)
|
|
step_cb = make_step_cb(mock_conn, "session-1", loop, tool_call_ids, tool_call_meta)
|
|
|
|
with patch("acp_adapter.events.asyncio.run_coroutine_threadsafe") as mock_rcts:
|
|
future = MagicMock(spec=Future)
|
|
future.result.return_value = None
|
|
mock_rcts.return_value = future
|
|
|
|
progress_cb("tool.started", "terminal", "$ ls", {"command": "ls"})
|
|
progress_cb("tool.started", "terminal", "$ pwd", {"command": "pwd"})
|
|
assert len(tool_call_ids["terminal"]) == 2
|
|
|
|
step_cb(1, [{"name": "terminal", "result": "ok-1"}])
|
|
assert len(tool_call_ids["terminal"]) == 1
|
|
|
|
step_cb(2, [{"name": "terminal", "result": "ok-2"}])
|
|
assert "terminal" not in tool_call_ids
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Thinking callback
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Step callback
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestStepCallback:
|
|
def test_completes_tracked_tool_calls(self, mock_conn, event_loop_fixture):
|
|
"""Step callback should mark tracked tools as completed."""
|
|
tool_call_ids = {"terminal": "tc-abc123"}
|
|
loop = event_loop_fixture
|
|
|
|
cb = make_step_cb(mock_conn, "session-1", loop, tool_call_ids, {})
|
|
|
|
with patch("acp_adapter.events.asyncio.run_coroutine_threadsafe") as mock_rcts:
|
|
future = MagicMock(spec=Future)
|
|
future.result.return_value = None
|
|
mock_rcts.return_value = future
|
|
|
|
cb(1, [{"name": "terminal", "result": "success"}])
|
|
|
|
# Tool should have been removed from tracking
|
|
assert "terminal" not in tool_call_ids
|
|
mock_rcts.assert_called_once()
|
|
|
|
|
|
|
|
def test_result_passed_to_build_tool_complete(self, mock_conn, event_loop_fixture):
|
|
"""Tool result from prev_tools dict is forwarded to build_tool_complete."""
|
|
from collections import deque
|
|
|
|
tool_call_ids = {"terminal": deque(["tc-xyz789"])}
|
|
loop = event_loop_fixture
|
|
|
|
cb = make_step_cb(mock_conn, "session-1", loop, tool_call_ids, {})
|
|
|
|
with patch("acp_adapter.events.asyncio.run_coroutine_threadsafe") as mock_rcts, \
|
|
patch("acp_adapter.events.build_tool_complete") as mock_btc:
|
|
future = MagicMock(spec=Future)
|
|
future.result.return_value = None
|
|
mock_rcts.return_value = future
|
|
|
|
# Provide a result string in the tool info dict
|
|
cb(1, [{"name": "terminal", "result": '{"output": "hello"}'}])
|
|
|
|
mock_btc.assert_called_once_with(
|
|
"tc-xyz789", "terminal", result='{"output": "hello"}', function_args=None, snapshot=None
|
|
)
|
|
|
|
|
|
|
|
def test_tool_progress_captures_snapshot_metadata(self, mock_conn, event_loop_fixture):
|
|
tool_call_ids = {}
|
|
tool_call_meta = {}
|
|
loop = event_loop_fixture
|
|
|
|
with patch("acp_adapter.events.make_tool_call_id", return_value="tc-meta"), \
|
|
patch("acp_adapter.events._send_update") as mock_send, \
|
|
patch("agent.display.capture_local_edit_snapshot", return_value="snapshot"):
|
|
cb = make_tool_progress_cb(mock_conn, "session-1", loop, tool_call_ids, tool_call_meta)
|
|
cb("tool.started", "write_file", None, {"path": "diff-test.txt", "content": "hello"})
|
|
|
|
assert list(tool_call_ids["write_file"]) == ["tc-meta"]
|
|
assert tool_call_meta["tc-meta"] == {
|
|
"args": {"path": "diff-test.txt", "content": "hello"},
|
|
"snapshot": "snapshot",
|
|
}
|
|
mock_send.assert_called_once()
|
|
|
|
def test_todo_completion_emits_native_plan_update_after_tool_completion(self, mock_conn, event_loop_fixture):
|
|
from collections import deque
|
|
|
|
tool_call_ids = {"todo": deque(["tc-todo"])}
|
|
loop = event_loop_fixture
|
|
cb = make_step_cb(mock_conn, "session-1", loop, tool_call_ids, {})
|
|
todo_result = (
|
|
'{"todos":['
|
|
'{"id":"inspect","content":"Inspect ACP","status":"completed"},'
|
|
'{"id":"patch","content":"Patch renderer","status":"in_progress"},'
|
|
'{"id":"old","content":"Drop stale task","status":"cancelled"}'
|
|
'],"summary":{"total":3}}'
|
|
)
|
|
|
|
with patch("acp_adapter.events._send_update") as mock_send:
|
|
cb(1, [{"name": "todo", "result": todo_result}])
|
|
|
|
updates = [call.args[3] for call in mock_send.call_args_list]
|
|
assert [getattr(update, "session_update", None) for update in updates] == [
|
|
"tool_call_update",
|
|
"plan",
|
|
]
|
|
plan = updates[1]
|
|
assert isinstance(plan, AgentPlanUpdate)
|
|
assert [entry.content for entry in plan.entries] == [
|
|
"Inspect ACP",
|
|
"Patch renderer",
|
|
"[cancelled] Drop stale task",
|
|
]
|
|
assert [entry.status for entry in plan.entries] == ["completed", "in_progress", "completed"]
|
|
assert [entry.priority for entry in plan.entries] == ["medium", "medium", "medium"]
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Message callback
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Scheduler-failure regression
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestSendUpdate:
|
|
def test_scheduler_failure_closes_update_coroutine(self, event_loop_fixture):
|
|
"""If run_coroutine_threadsafe raises, _send_update must close the coro."""
|
|
created = {"coro": None}
|
|
|
|
async def _session_update(session_id, update):
|
|
return None
|
|
|
|
conn = MagicMock()
|
|
|
|
def _capture_update(session_id, update):
|
|
created["coro"] = _session_update(session_id, update)
|
|
return created["coro"]
|
|
|
|
conn.session_update = _capture_update
|
|
|
|
with warnings.catch_warnings(record=True) as caught:
|
|
warnings.simplefilter("always")
|
|
with patch(
|
|
"agent.async_utils.asyncio.run_coroutine_threadsafe",
|
|
side_effect=RuntimeError("scheduler down"),
|
|
):
|
|
_send_update(conn, "session-1", event_loop_fixture, {"type": "noop"})
|
|
gc.collect()
|
|
|
|
assert created["coro"] is not None
|
|
assert created["coro"].cr_frame is None
|
|
# Only count warnings about THIS test's coroutine; other tests
|
|
# may emit unrelated
|
|
# "coroutine was never awaited" warnings that bleed through.
|
|
runtime_warnings = [
|
|
w for w in caught
|
|
if issubclass(w.category, RuntimeWarning)
|
|
and "was never awaited" in str(w.message)
|
|
and "_session_update" in str(w.message)
|
|
]
|
|
assert runtime_warnings == []
|