Files
hermes-agent/tests/agent/test_worker_rpc_loop_diagnostic.py
unsupportedpastels f15d719ffd fix(agent): name the caller when the worker RPC guard fires on a loop
A managed worker that reaches the synchronous persistence RPC on a thread
with a running event loop gets WorkerPersistenceError
('synchronous_rpc_on_event_loop'), which the store keeps as a sticky
failure. When a best-effort writer (aux usage, the tools pin) swallows
that first raise at debug, the logs only show the later sticky failures
(append_message, flush_token_counts, turn.cleanup) and never the code
that tripped the guard.

The guard stays as it is. Before it raises, it now logs a WARNING with
the RPC method, the thread name and a module:function:line summary of the
frames above the store. The store's own frames are left out, and no
locals or argument values are logged.

A static pass over the worker's loop owners did not confirm a trigger,
so this commit only adds the diagnostic. Checked: async tool handlers via
_run_async, async aux calls (accounting is already off-loop), MCP
sampling and list_changed, the periodic lease/liveness scheduler, the
process registry, deadline helpers and smart approvals.

Refs #139029
Refs #134889 (KB-2)
Refs #139162
2026-10-15 23:59:19 -04:00

67 lines
2.6 KiB
Python

"""The on-loop guard names its caller at WARNING (#139029): a best-effort write swallows the raise
at debug, and only the sticky follow-up failures reached the logs."""
import asyncio
import logging
import pytest
SECRET = 'arg-value-never-logged'
def _no_owner(*args, **kwargs):
raise AssertionError('the guard must fire before owner discovery')
def _guard_warnings(caplog):
return [r.getMessage() for r in caplog.records
if r.name == 'agent.runtime_session_store' and r.levelno == logging.WARNING]
def test_on_loop_rpc_logs_caller_frames_without_argument_values(tmp_path, monkeypatch, caplog):
from agent.runtime_session_store import WorkerPersistenceError, WorkerRPC
monkeypatch.setattr('hermes_cli.gateway_runtime.discover_gateway_endpoint', _no_owner)
rpc = WorkerRPC(tmp_path)
def persist_from_callback():
return rpc('worker.persist', payload=SECRET)
async def on_loop():
persist_from_callback()
caplog.set_level(logging.WARNING, logger='agent.runtime_session_store')
with pytest.raises(WorkerPersistenceError, match='synchronous_rpc_on_event_loop'):
asyncio.run(on_loop())
[message] = _guard_warnings(caplog)
assert 'synchronous_rpc_on_event_loop' in message and 'worker.persist' in message
assert f'{__name__}:persist_from_callback:' in message
assert f'{__name__}:on_loop:' in message
assert SECRET not in message
def test_on_loop_store_write_names_the_caller_not_the_store(tmp_path, monkeypatch, caplog):
"""Through the store, the summary skips its own frames (_apply, retry_pending) so the
swallowing best-effort caller is what the log names."""
from agent.runtime_session_store import RuntimeSessionStore, WorkerPersistenceError, WorkerRPC
monkeypatch.setattr('hermes_cli.gateway_runtime.discover_gateway_endpoint', _no_owner)
store = RuntimeSessionStore(WorkerRPC(tmp_path), {'session_id': 's'}, tmp_path / 'outbox')
def best_effort_usage():
try:
store.record_auxiliary_usage('s', 'approval', input_tokens=7, model=SECRET)
except WorkerPersistenceError:
pass
async def on_loop():
best_effort_usage()
caplog.set_level(logging.WARNING, logger='agent.runtime_session_store')
try:
asyncio.run(on_loop())
[message] = _guard_warnings(caplog)
assert message.split('caller: ', 1)[1].startswith(f'{__name__}:best_effort_usage:')
assert 'agent.runtime_session_store' not in message.split('caller: ', 1)[1]
assert SECRET not in message
assert store.failure == 'synchronous_rpc_on_event_loop'
finally:
store._outbox_owner.close()