Files
hermes-agent/tests/hermes_state/test_resolve_resume_session_id.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

105 lines
3.9 KiB
Python

"""Regression guard for #15000: --resume <id> after compression loses messages.
Context compression ends the current session and forks a new child session
(linked by ``parent_session_id``). The SQLite flush cursor is reset, so
only the latest descendant ends up with rows in the ``messages`` table —
the parent row has ``message_count = 0``. ``hermes --resume <parent_id>``
used to load zero rows and show a blank chat.
``SessionDB.resolve_resume_session_id()`` walks the parent → child chain
and redirects to the first descendant that actually has messages. These
tests pin that behaviour.
"""
import time
import pytest
from hermes_state import SessionDB
@pytest.fixture
def db(tmp_path):
return SessionDB(tmp_path / "state.db")
def _make_chain(db: SessionDB, ids_with_parent):
"""Create sessions in order, forcing started_at so ordering is deterministic."""
base = int(time.time()) - 10_000
for i, (sid, parent) in enumerate(ids_with_parent):
db.create_session(sid, source="cli", parent_session_id=parent)
db._conn.execute(
"UPDATE sessions SET started_at = ? WHERE id = ?",
(base + i * 100, sid),
)
db._conn.commit()
def test_returns_self_when_only_parent_has_messages(db):
# When a session already has messages AND no descendant has messages,
# it should still be returned. The chain walk finds no better candidate.
_make_chain(db, [("root", None), ("child", "root")])
db.append_message("root", role="user", content="hi")
assert db.resolve_resume_session_id("root") == "root"
def test_walks_from_middle_of_chain(db):
# If the user happens to know an intermediate ID, we still find the msg-bearing descendant.
_make_chain(db, [("a", None), ("b", "a"), ("c", "b"), ("d", "c")])
db.append_message("d", role="user", content="x")
assert db.resolve_resume_session_id("b") == "d"
assert db.resolve_resume_session_id("c") == "d"
def test_follows_compression_tip_when_parent_retains_messages(db):
# The bug behind the desktop "I came back and the reply isn't there" report
# on large sessions: auto-compression ends the live session and forks a
# continuation child, but a long parent keeps its own flushed message rows.
# The empty-head walk below never redirects a non-empty head, so resuming
# the parent id reloaded the pre-compression transcript and the response
# generated *after* compression (which lives in the continuation) was
# missing. resolve_resume_session_id must follow the compression-tip chain
# forward even when the parent still has messages.
base = int(time.time()) - 10_000
db.create_session("root", source="cli")
db.append_message("root", role="user", content="pre-compression turn")
db.end_session("root", "compression")
db.create_session("cont", source="cli", parent_session_id="root")
db.append_message("cont", role="assistant", content="post-compression reply")
# Force deterministic ordering so the continuation's started_at is clearly
# at/after the parent's ended_at (the get_compression_tip discriminator).
conn = db._conn
assert conn is not None
conn.execute("UPDATE sessions SET started_at = ?, ended_at = ? WHERE id = 'root'", (base, base + 50))
conn.execute("UPDATE sessions SET started_at = ? WHERE id = 'cont'", (base + 100,))
conn.commit()
assert db.resolve_resume_session_id("root") == "cont"
def test_prefers_most_recent_child_when_fork_exists(db):
# If a session was somehow forked (two children), pick the latest one.
# In practice, compression only produces single-chain shape, but the helper
# should degrade gracefully.
_make_chain(db, [
("parent", None),
("older_fork", "parent"),
("newer_fork", "parent"),
])
db.append_message("newer_fork", role="user", content="x")
assert db.resolve_resume_session_id("parent") == "newer_fork"