hermes-agent/tests/gateway/test_fresh_reset_skill_injection.py
Teknium 39975613b1
test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
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.
2026-07-29 13:39:40 -07:00

133 lines
5.1 KiB
Python

"""Regression tests for topic/channel skill auto-injection after /new or /reset.
Covers the fix for issue #6508.
Before the fix:
1. User sends ``/new`` — ``reset_session`` creates a fresh SessionEntry
with ``created_at == updated_at``.
2. User sends the next message.
3. ``get_or_create_session`` finds the entry and bumps
``entry.updated_at = now`` (microseconds after ``created_at``).
4. ``_handle_message_with_agent`` checks
``_is_new_session = (created_at == updated_at) or was_auto_reset``.
Both are False → ``_is_new_session = False`` → topic/channel skills
are silently skipped for the first message of a manually reset session.
After the fix:
``reset_session`` stamps the new entry with ``is_fresh_reset=True``.
``_handle_message_with_agent`` ORs this into ``_is_new_session`` and
consumes the flag immediately after the check, so subsequent messages
are treated as continuing the session and the flag does not leak.
We use ``was_auto_reset`` for surprise resets (idle/daily/suspended) and
``is_fresh_reset`` for user-initiated resets because the former also drives
a "Session automatically reset due to inactivity" user-facing notice and
a context-note prepend into the agent's prompt — both wrong for an explicit
/new or /reset.
"""
from gateway.config import GatewayConfig, Platform
from gateway.session import SessionEntry, SessionSource, SessionStore
def _make_store(tmp_path):
return SessionStore(sessions_dir=tmp_path, config=GatewayConfig())
def _make_source(chat_id="123", user_id="u1"):
return SessionSource(
platform=Platform.TELEGRAM,
chat_id=chat_id,
user_id=user_id,
)
def _is_new_session(entry) -> bool:
"""Mirror of the predicate in ``_handle_message_with_agent``.
Kept in-sync with the production check so this test fails loudly if the
upstream logic regresses.
"""
return (
entry.created_at == entry.updated_at
or getattr(entry, "was_auto_reset", False)
or getattr(entry, "is_fresh_reset", False)
)
# ---------------------------------------------------------------------------
# reset_session stamps is_fresh_reset=True
# ---------------------------------------------------------------------------
class TestResetSessionStampsFreshReset:
def test_reset_session_sets_is_fresh_reset_true(self, tmp_path):
store = _make_store(tmp_path)
source = _make_source()
store.get_or_create_session(source)
session_key = store._generate_session_key(source)
new_entry = store.reset_session(session_key)
assert new_entry is not None
assert new_entry.is_fresh_reset is True
# ---------------------------------------------------------------------------
# Core regression: _is_new_session stays True after updated_at bump
# ---------------------------------------------------------------------------
class TestIsNewSessionSurvivesUpdatedAtBump:
def test_is_new_session_true_after_reset_then_next_message(self, tmp_path):
"""The actual bug: _is_new_session was False on message after /reset."""
store = _make_store(tmp_path)
source = _make_source()
store.get_or_create_session(source)
session_key = store._generate_session_key(source)
# User sends /reset
store.reset_session(session_key)
# Next inbound message — get_or_create_session bumps updated_at
entry = store.get_or_create_session(source)
# Before the fix: created_at != updated_at, was_auto_reset=False → False
# After the fix: is_fresh_reset=True carries the signal through the bump
assert _is_new_session(entry) is True
# ---------------------------------------------------------------------------
# Vanilla-session behavior is unchanged
# ---------------------------------------------------------------------------
class TestVanillaBehaviorUnaffected:
def test_ongoing_session_not_flagged_as_new(self, tmp_path):
store = _make_store(tmp_path)
source = _make_source()
store.get_or_create_session(source)
# Second message on the same session — updated_at bumps,
# is_fresh_reset was never set
entry = store.get_or_create_session(source)
assert entry.is_fresh_reset is False
assert _is_new_session(entry) is False
# ---------------------------------------------------------------------------
# Persistence through sessions.json round-trip
# ---------------------------------------------------------------------------
class TestPersistence:
def test_is_fresh_reset_survives_to_dict_from_dict(self, tmp_path):
"""Protect against the gateway restarting between /reset and the
next message — the flag must be persisted in sessions.json.
"""
store = _make_store(tmp_path)
source = _make_source()
store.get_or_create_session(source)
session_key = store._generate_session_key(source)
new_entry = store.reset_session(session_key)
assert new_entry.is_fresh_reset is True
restored = SessionEntry.from_dict(new_entry.to_dict())
assert restored.is_fresh_reset is True