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.
108 lines
4.1 KiB
Python
108 lines
4.1 KiB
Python
"""Accretion caps for _read_tracker (file_tools) and _completion_consumed
|
|
(process_registry).
|
|
|
|
Both structures are process-lifetime singletons that previously grew
|
|
unbounded in long-running CLI / gateway sessions:
|
|
|
|
file_tools._read_tracker[task_id]
|
|
├─ read_history (set) — one entry per unique (path, offset, limit)
|
|
├─ dedup (dict) — one entry per unique (path, offset, limit)
|
|
└─ read_timestamps (dict) — one entry per unique resolved path
|
|
process_registry._completion_consumed (set) — one entry per session_id
|
|
ever polled / waited / logged
|
|
|
|
None of these were ever trimmed. A 10k-read CLI session accumulated
|
|
roughly 1.5MB of tracker state; a gateway with high background-process
|
|
churn accumulated ~20B per session_id until the process exited.
|
|
|
|
These tests pin the new caps + prune hooks.
|
|
"""
|
|
|
|
|
|
class TestReadTrackerCaps:
|
|
def setup_method(self):
|
|
from tools import file_tools
|
|
|
|
# Clean slate per test.
|
|
with file_tools._read_tracker_lock:
|
|
file_tools._read_tracker.clear()
|
|
|
|
def test_read_history_capped(self, monkeypatch):
|
|
"""read_history set is bounded by _READ_HISTORY_CAP."""
|
|
from tools import file_tools as ft
|
|
|
|
monkeypatch.setattr(ft, "_READ_HISTORY_CAP", 10)
|
|
task_data = {
|
|
"last_key": None,
|
|
"consecutive": 0,
|
|
"read_history": set((f"/p{i}", 0, 500) for i in range(50)),
|
|
"dedup": {},
|
|
"read_timestamps": {},
|
|
}
|
|
ft._cap_read_tracker_data(task_data)
|
|
assert len(task_data["read_history"]) == 10
|
|
|
|
|
|
def test_live_cap_applied_after_read_add(self, tmp_path, monkeypatch):
|
|
"""Live read_file path enforces caps."""
|
|
from tools import file_tools as ft
|
|
|
|
monkeypatch.setattr(ft, "_READ_HISTORY_CAP", 3)
|
|
monkeypatch.setattr(ft, "_DEDUP_CAP", 3)
|
|
monkeypatch.setattr(ft, "_READ_TIMESTAMPS_CAP", 3)
|
|
|
|
# Create 10 distinct files and read each once.
|
|
for i in range(10):
|
|
p = tmp_path / f"file_{i}.txt"
|
|
p.write_text(f"content {i}\n" * 10)
|
|
ft.read_file_tool(path=str(p), task_id="long-session")
|
|
|
|
with ft._read_tracker_lock:
|
|
td = ft._read_tracker["long-session"]
|
|
assert len(td["read_history"]) <= 3
|
|
assert len(td["dedup"]) <= 3
|
|
# read_timestamps is populated lazily (via setdefault) only
|
|
# when os.path.getmtime() succeeds. On some CI filesystems
|
|
# that stat can race with file creation — skip rather than
|
|
# hard-error if the dict hasn't been created yet.
|
|
assert len(td.get("read_timestamps", {})) <= 3
|
|
|
|
|
|
class TestCompletionConsumedPrune:
|
|
def test_prune_drops_completion_entry_with_expired_session(self):
|
|
"""When a finished session is pruned, _completion_consumed is
|
|
cleared for the same session_id."""
|
|
from tools.process_registry import ProcessRegistry, FINISHED_TTL_SECONDS
|
|
import time
|
|
|
|
reg = ProcessRegistry()
|
|
# Fake a finished session whose started_at is older than the TTL.
|
|
class _FakeSess:
|
|
def __init__(self, sid):
|
|
self.id = sid
|
|
self.started_at = time.time() - (FINISHED_TTL_SECONDS + 100)
|
|
self.exited = True
|
|
|
|
reg._finished["stale-1"] = _FakeSess("stale-1")
|
|
reg._completion_consumed.add("stale-1")
|
|
|
|
with reg._lock:
|
|
reg._prune_if_needed()
|
|
|
|
assert "stale-1" not in reg._finished
|
|
assert "stale-1" not in reg._completion_consumed
|
|
|
|
|
|
def test_prune_clears_dangling_completion_entries(self):
|
|
"""Stale entries in _completion_consumed without a backing session
|
|
record are cleared out (belt-and-suspenders invariant)."""
|
|
from tools.process_registry import ProcessRegistry
|
|
|
|
reg = ProcessRegistry()
|
|
# Add a dangling entry that was never in _running or _finished.
|
|
reg._completion_consumed.add("dangling-never-tracked")
|
|
|
|
with reg._lock:
|
|
reg._prune_if_needed()
|
|
|
|
assert "dangling-never-tracked" not in reg._completion_consumed
|