mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +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.
285 lines
11 KiB
Python
285 lines
11 KiB
Python
"""Tests for session auto-reset notifications.
|
|
|
|
Verifies that:
|
|
- _should_reset() returns a reason string ("idle" or "daily") instead of bool
|
|
- SessionEntry captures auto_reset_reason
|
|
- SessionResetPolicy.notify controls whether notifications are sent
|
|
- notify_exclude_platforms skips notifications for excluded platforms
|
|
- resume_pending_expired auto-reset sets the correct reason and DB end_reason
|
|
"""
|
|
|
|
from datetime import datetime, timedelta
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
from gateway.config import (
|
|
GatewayConfig,
|
|
Platform,
|
|
SessionResetPolicy,
|
|
)
|
|
from gateway.session import SessionEntry, SessionSource, SessionStore
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _make_source(platform=Platform.TELEGRAM, chat_id="123", user_id="u1"):
|
|
return SessionSource(
|
|
platform=platform,
|
|
chat_id=chat_id,
|
|
user_id=user_id,
|
|
)
|
|
|
|
|
|
def _make_store(policy=None, tmp_path=None, has_active_processes_fn=None):
|
|
config = GatewayConfig()
|
|
if policy:
|
|
config.default_reset_policy = policy
|
|
store = SessionStore(
|
|
sessions_dir=tmp_path or "/tmp/test-sessions",
|
|
config=config,
|
|
has_active_processes_fn=has_active_processes_fn,
|
|
)
|
|
return store
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# _should_reset returns reason string
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestShouldResetReason:
|
|
|
|
def test_returns_idle_when_idle_expired(self, tmp_path):
|
|
store = _make_store(
|
|
SessionResetPolicy(mode="idle", idle_minutes=30),
|
|
tmp_path,
|
|
)
|
|
entry = SessionEntry(
|
|
session_key="test",
|
|
session_id="s1",
|
|
created_at=datetime.now() - timedelta(hours=2),
|
|
updated_at=datetime.now() - timedelta(hours=1), # 60min ago > 30min threshold
|
|
)
|
|
source = _make_source()
|
|
assert store._should_reset(entry, source) == "idle"
|
|
|
|
|
|
def test_returns_none_when_active_process_check_raises(self, tmp_path):
|
|
def _raise(_session_key):
|
|
raise RuntimeError("process registry unavailable")
|
|
|
|
store = _make_store(
|
|
SessionResetPolicy(mode="idle", idle_minutes=30),
|
|
tmp_path,
|
|
has_active_processes_fn=_raise,
|
|
)
|
|
entry = SessionEntry(
|
|
session_key="test",
|
|
session_id="s1",
|
|
created_at=datetime.now() - timedelta(hours=2),
|
|
updated_at=datetime.now() - timedelta(hours=1),
|
|
)
|
|
source = _make_source()
|
|
|
|
assert store._should_reset(entry, source) is None
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# SessionEntry captures reason
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestSessionEntryReason:
|
|
|
|
|
|
def test_reset_had_activity_true_when_tokens_used(self, tmp_path):
|
|
"""Expired session with tokens → reset_had_activity=True."""
|
|
store = _make_store(
|
|
SessionResetPolicy(mode="idle", idle_minutes=1),
|
|
tmp_path,
|
|
)
|
|
source = _make_source()
|
|
|
|
entry1 = store.get_or_create_session(source)
|
|
# Simulate some conversation happened (last_prompt_tokens is the field
|
|
# written on every turn; total_tokens is never persisted).
|
|
entry1.last_prompt_tokens = 5000
|
|
entry1.updated_at = datetime.now() - timedelta(minutes=5)
|
|
store._save()
|
|
|
|
entry2 = store.get_or_create_session(source)
|
|
assert entry2.was_auto_reset is True
|
|
assert entry2.reset_had_activity is True
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# SessionResetPolicy notify config
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestResetPolicyNotify:
|
|
|
|
def test_notify_exclude_defaults(self):
|
|
policy = SessionResetPolicy()
|
|
assert "api_server" in policy.notify_exclude_platforms
|
|
assert "webhook" in policy.notify_exclude_platforms
|
|
|
|
|
|
def test_from_dict_with_custom_excludes(self):
|
|
policy = SessionResetPolicy.from_dict({
|
|
"notify_exclude_platforms": ["api_server", "webhook", "homeassistant"],
|
|
})
|
|
assert "homeassistant" in policy.notify_exclude_platforms
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# SessionEntry to_dict / from_dict roundtrip for auto-reset fields
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class TestSessionEntryAutoResetRoundtrip:
|
|
def test_was_auto_reset_persists_across_roundtrip(self, tmp_path):
|
|
"""was_auto_reset=True survives to_dict() → from_dict() (gateway restart)."""
|
|
store = _make_store(
|
|
SessionResetPolicy(mode="idle", idle_minutes=1),
|
|
tmp_path,
|
|
)
|
|
source = _make_source()
|
|
|
|
entry = store.get_or_create_session(source)
|
|
entry.updated_at = datetime.now() - timedelta(minutes=5)
|
|
store._save()
|
|
|
|
entry2 = store.get_or_create_session(source)
|
|
assert entry2.was_auto_reset is True
|
|
assert entry2.auto_reset_reason == "idle"
|
|
assert entry2.session_id != entry.session_id
|
|
|
|
# Simulate gateway restart: reload from disk
|
|
store._loaded = False
|
|
store._entries.clear()
|
|
store._ensure_loaded()
|
|
|
|
reloaded = store._entries.get(entry2.session_key)
|
|
assert reloaded is not None
|
|
assert reloaded.was_auto_reset is True
|
|
assert reloaded.auto_reset_reason == "idle"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# resume_pending_expired: auto_reset_reason and DB end_reason (#58933)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _make_db_mock() -> MagicMock:
|
|
"""Return a SessionDB mock with safe defaults for all lookup methods."""
|
|
db = MagicMock()
|
|
db.get_session.return_value = None
|
|
db.get_compression_tip.return_value = None # avoids MagicMock leaking into session_id
|
|
db.find_latest_gateway_session_for_peer.return_value = None
|
|
db.reopen_session.return_value = None
|
|
db.create_session.return_value = None
|
|
return db
|
|
|
|
|
|
def _make_store_with_db(tmp_path, db_mock=None, policy=None) -> SessionStore:
|
|
"""Build a SessionStore with a mock SessionDB, bypassing disk load."""
|
|
cfg_policy = policy or SessionResetPolicy(mode="none")
|
|
config = GatewayConfig(default_reset_policy=cfg_policy)
|
|
with patch("gateway.session.SessionStore._ensure_loaded"):
|
|
store = SessionStore(sessions_dir=tmp_path, config=config)
|
|
store._db = db_mock if db_mock is not None else _make_db_mock()
|
|
store._loaded = True
|
|
return store
|
|
|
|
|
|
class TestResumePendingExpiredAutoReset:
|
|
"""resume_pending sessions past the freshness window should fire
|
|
was_auto_reset=True with auto_reset_reason='resume_pending_expired' and
|
|
persist that reason to state.db (#58933)."""
|
|
|
|
def _seed_stale_resume_pending(self, store, source, freshness_seconds=3600):
|
|
"""Create a session, mark it resume_pending, then backdate the mark
|
|
past the freshness window so get_or_create_session treats it as a
|
|
zombie."""
|
|
entry = store.get_or_create_session(source)
|
|
store.mark_resume_pending(entry.session_key)
|
|
with store._lock:
|
|
entry = store._entries[entry.session_key]
|
|
entry.last_resume_marked_at = (
|
|
datetime.now() - timedelta(seconds=freshness_seconds + 60)
|
|
)
|
|
entry.updated_at = datetime.now() # keep updated_at fresh
|
|
store._save()
|
|
return entry
|
|
|
|
def test_stale_resume_pending_sets_auto_reset_reason(
|
|
self, tmp_path, monkeypatch
|
|
):
|
|
"""Stale resume_pending triggers was_auto_reset=True with reason
|
|
'resume_pending_expired', NOT 'idle'."""
|
|
monkeypatch.setenv("HERMES_AUTO_CONTINUE_FRESHNESS", "3600")
|
|
# The freshness gate requires an opted-in reset policy — mode "none"
|
|
# disables it entirely (#61052). Use a huge idle window so only the
|
|
# freshness gate (not the idle policy) can fire.
|
|
store = _make_store_with_db(
|
|
tmp_path,
|
|
policy=SessionResetPolicy(mode="idle", idle_minutes=999999),
|
|
)
|
|
source = _make_source()
|
|
|
|
old = self._seed_stale_resume_pending(store, source)
|
|
|
|
new = store.get_or_create_session(source)
|
|
|
|
assert new.session_id != old.session_id, "should have created a new session"
|
|
assert new.was_auto_reset is True
|
|
assert new.auto_reset_reason == "resume_pending_expired"
|
|
|
|
|
|
def test_stale_resume_pending_db_end_reason_is_specific(
|
|
self, tmp_path, monkeypatch
|
|
):
|
|
"""state.db must record end_reason='resume_pending_expired', NOT the
|
|
generic 'session_reset', so the event is auditable (#58933 fix)."""
|
|
monkeypatch.setenv("HERMES_AUTO_CONTINUE_FRESHNESS", "3600")
|
|
db = _make_db_mock()
|
|
store = _make_store_with_db(
|
|
tmp_path, db,
|
|
policy=SessionResetPolicy(mode="idle", idle_minutes=999999),
|
|
)
|
|
source = _make_source()
|
|
|
|
old = self._seed_stale_resume_pending(store, source)
|
|
store.get_or_create_session(source)
|
|
|
|
# Auto-reset now writes through promote_to_session_reset so an
|
|
# accidental agent_close end can't shadow the reset boundary.
|
|
db.promote_to_session_reset.assert_called_once()
|
|
ended_id, ended_reason = db.promote_to_session_reset.call_args.args
|
|
assert ended_id == old.session_id
|
|
assert ended_reason == "resume_pending_expired", (
|
|
f"expected 'resume_pending_expired', got {ended_reason!r} — "
|
|
"the DB end_reason must not be the generic 'session_reset'"
|
|
)
|
|
|
|
def test_idle_reset_db_end_reason_reflects_idle(
|
|
self, tmp_path
|
|
):
|
|
"""Regular idle auto-reset persists 'idle' as end_reason so that all
|
|
auto-reset paths are auditable (#58933 should not regress the common
|
|
idle/daily path)."""
|
|
db = _make_db_mock()
|
|
store = _make_store_with_db(
|
|
tmp_path, db, policy=SessionResetPolicy(mode="idle", idle_minutes=1)
|
|
)
|
|
source = _make_source()
|
|
|
|
entry = store.get_or_create_session(source)
|
|
# Age past idle threshold.
|
|
with store._lock:
|
|
entry.updated_at = datetime.now() - timedelta(minutes=5)
|
|
store._save()
|
|
|
|
store.get_or_create_session(source)
|
|
|
|
db.promote_to_session_reset.assert_called_once()
|
|
_, ended_reason = db.promote_to_session_reset.call_args.args
|
|
assert ended_reason == "idle"
|
|
|