mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-27 17:58:07 +00:00
Completes #51690 on top of the salvaged #60378 timeout metadata: - async_delegation: terminal 'stalled' events now carry structured stall context (stalled_after_quiet_seconds, stall_threshold_seconds, stall_phase idle|in_tool, stall_grace_seconds) on both single and batch paths, persisted in the durable row so restart-restored events keep it. Mirrors the sync path's timeout_seconds/timed_out_after_ seconds/timeout_phase from #60378. - list_async_delegations(): exposes seconds_since_progress and live children_activity (per-child api_calls, current_tool, seconds_since_activity) sampled from the dispatch's progress_fn outside the records lock; private monitor bookkeeping and callables never leak. - /agents (CLI + gateway): background delegations render per-child activity rows, quiet-time hints, and the stalling state; gateway section is new (previously async delegations were invisible there). New locale key gateway.agents.background_delegations in all 17 catalogs. Tests: stall-metadata event shape, live-listing projection, gateway /agents rendering (real registry dispatch, sabotage-verified), sync timeout metadata fields, non-timeout None contract.
119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
"""Gateway /agents surfaces background delegations with live activity (#51690).
|
|
|
|
Drives the REAL GatewayRunner._handle_agents_command against a REAL
|
|
async-delegation registry dispatch (no mocked list function), so the test
|
|
covers the whole projection: registry record → list_async_delegations()
|
|
live sampling → /agents rendering.
|
|
"""
|
|
|
|
import threading
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from tools import async_delegation as ad
|
|
from tools.process_registry import process_registry
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_state():
|
|
ad._reset_for_tests()
|
|
while not process_registry.completion_queue.empty():
|
|
process_registry.completion_queue.get_nowait()
|
|
yield
|
|
deadline = time.monotonic() + 2.0
|
|
while ad.active_count() and time.monotonic() < deadline:
|
|
time.sleep(0.02)
|
|
ad._reset_for_tests()
|
|
while not process_registry.completion_queue.empty():
|
|
process_registry.completion_queue.get_nowait()
|
|
|
|
|
|
def _make_runner():
|
|
from gateway.run import GatewayRunner
|
|
|
|
runner = object.__new__(GatewayRunner)
|
|
runner._running_agents = {}
|
|
runner._running_agents_ts = {}
|
|
runner._background_tasks = set()
|
|
runner._session_key_for_source = lambda source: "agent:main:test:dm:1"
|
|
return runner
|
|
|
|
|
|
class _Event:
|
|
source = None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agents_command_lists_background_delegation_with_activity():
|
|
gate = threading.Event()
|
|
base_ts = time.time() - 8.0
|
|
|
|
res = ad.dispatch_async_delegation(
|
|
goal="research the delegation stall monitor",
|
|
context=None, toolsets=None, role="leaf", model="m",
|
|
session_key="agent:main:test:dm:1", max_async_children=1,
|
|
runner=lambda: {} if gate.wait(timeout=10) else {},
|
|
progress_fn=lambda: (((2, "web_search", base_ts),), True),
|
|
)
|
|
assert res["status"] == "dispatched"
|
|
|
|
try:
|
|
runner = _make_runner()
|
|
out = await runner._handle_agents_command(_Event())
|
|
finally:
|
|
gate.set()
|
|
|
|
assert res["delegation_id"] in out
|
|
assert "running" in out
|
|
assert "research the delegation stall monitor" in out
|
|
# Live per-child activity sampled from progress_fn.
|
|
assert "2 api calls" in out
|
|
assert "web_search" in out
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agents_command_marks_stalling_delegation(monkeypatch):
|
|
monkeypatch.setattr(ad, "_STALE_CHECK_INTERVAL", 0.03)
|
|
monkeypatch.setattr(ad, "_STALE_IDLE_SECONDS", 0.1)
|
|
# Long grace so the record stays in 'stalling' while we render.
|
|
monkeypatch.setattr(ad, "_STALL_GRACE_SECONDS", 30.0)
|
|
gate = threading.Event()
|
|
|
|
res = ad.dispatch_async_delegation(
|
|
goal="wedged child", context=None, toolsets=None, role="leaf",
|
|
model="m", session_key="agent:main:test:dm:1", max_async_children=1,
|
|
runner=lambda: {} if gate.wait(timeout=10) else {},
|
|
progress_fn=lambda: ((0, None), False),
|
|
)
|
|
assert res["status"] == "dispatched"
|
|
|
|
try:
|
|
deadline = time.monotonic() + 5.0
|
|
while time.monotonic() < deadline:
|
|
items = ad.list_async_delegations()
|
|
if any(
|
|
d["delegation_id"] == res["delegation_id"]
|
|
and d.get("status") == "stalling"
|
|
for d in items
|
|
):
|
|
break
|
|
time.sleep(0.02)
|
|
else:
|
|
pytest.fail("delegation never reached stalling state")
|
|
|
|
runner = _make_runner()
|
|
out = await runner._handle_agents_command(_Event())
|
|
finally:
|
|
gate.set()
|
|
|
|
assert res["delegation_id"] in out
|
|
assert "stalling" in out
|
|
assert "no progress" in out
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_agents_command_no_delegations_keeps_none_message():
|
|
runner = _make_runner()
|
|
out = await runner._handle_agents_command(_Event())
|
|
assert "No active agents" in out
|