hermes-agent/tests/tui_gateway/test_finalize_session_persist.py
teknium1 1693fca97a test: purge low-value tests, lane py20 (206 removed)
Change-detectors, tautologies, source-reading tests, redundant duplicates,
mock-echo tests and dead/unrunnable tests. Per-test rationale in the lane
ledger (category + reason for every removal).
2026-09-23 03:15:26 -07:00

208 lines
7.7 KiB
Python

"""
Integration test: verify _finalize_session persists messages on force-quit.
Tests the fix for TUI sessions losing conversation history when the
user interrupts and exits before the agent thread finishes flushing.
Scenarios:
1. Normal interrupt (single Ctrl+C) — messages already in session["history"]
2. Force-quit mid-tool (double Ctrl+C) — session["history"] has previous turns
3. Empty session — no-op, no crash
4. Agent with _persist_session missing — graceful no-op
"""
import threading
from unittest.mock import MagicMock, patch
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_agent(history=None, session_id="test_session_001"):
"""Build a mock AIAgent with enough surface for _finalize_session."""
agent = MagicMock()
agent._persist_session = MagicMock()
agent.commit_memory_session = MagicMock()
agent.session_id = session_id
agent.model = "test-model"
agent.platform = "tui"
# _session_messages must be explicitly absent (None), otherwise
# MagicMock auto-creates it and getattr returns a truthy mock.
agent._session_messages = None
return agent
def _make_session(agent=None, history=None, session_key="test_key_001"):
return {
"agent": agent,
"history": history or [],
"history_lock": threading.Lock(),
"session_key": session_key,
"_finalized": False,
}
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
class TestFinalizeSessionPersist:
"""Verify _finalize_session flushes messages via _persist_session."""
def test_commit_memory_still_called(self):
"""Existing memory commit path is preserved."""
from tui_gateway.server import _finalize_session
history = [{"role": "user", "content": "x"}]
agent = _make_agent()
session = _make_session(agent=agent, history=history)
_finalize_session(session)
agent.commit_memory_session.assert_called_once()
def test_already_finalized_skips(self):
"""Double-finalize is a no-op."""
from tui_gateway.server import _finalize_session
agent = _make_agent()
session = _make_session(agent=agent, history=[{"role": "user", "content": "x"}])
session["_finalized"] = True
_finalize_session(session)
agent._persist_session.assert_not_called()
class TestFinalizeSessionPersistE2E:
"""End-to-end: _finalize_session must actually land unflushed turns in
state.db on disconnect/restart.
The mock-based tests above assert that _persist_session is *called*, but a
call whose arguments neuter the underlying flush persists nothing. These
tests drive the REAL AIAgent flush against a REAL SessionDB, reproducing
the "conversation contains many events yet is absent from state.db across
disconnect/restart" symptom.
"""
@staticmethod
def _real_agent(db, session_id, session_messages):
from run_agent import AIAgent
agent = object.__new__(AIAgent)
agent._session_db = db
agent._session_db_created = True
agent.session_id = session_id
agent.platform = "tui"
agent.model = "test-model"
agent._session_messages = session_messages
agent._last_flushed_db_idx = 0
agent._flushed_db_message_ids = set()
agent._flushed_db_message_session_id = None
agent._persist_disabled = False
agent._cached_system_prompt = None
agent._session_init_model_config = None
agent._parent_session_id = None
agent.quiet_mode = True
# commit_memory_session runs heavy machinery we don't exercise here.
agent.commit_memory_session = lambda *a, **k: None
return agent
def test_unflushed_turn_survives_disconnect(self, tmp_path, monkeypatch):
"""A completed turn whose transcript flush did NOT durably persist
(messages live only in agent._session_messages / session['history'],
never written to the DB) must be flushed to state.db when the WS
disconnect tears the session down."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
from hermes_state import SessionDB
import tui_gateway.server as srv
db = SessionDB(db_path=tmp_path / "state.db")
session_id = "sess-unflushed"
db.create_session(session_id=session_id, source="tui")
monkeypatch.setattr(srv, "_get_db", lambda: db)
# The live turn list that became session["history"] AND
# agent._session_messages (same object), but was never persisted.
turn = [
{"role": "user", "content": "scan the repo and summarise"},
{"role": "assistant", "content": "Here is the summary…"},
{"role": "user", "content": "now open a PR"},
{"role": "assistant", "content": "PR opened."},
]
agent = self._real_agent(db, session_id, turn)
session = _make_session(agent=agent, history=turn, session_key=session_id)
assert db.get_messages_as_conversation(session_id) == []
srv._finalize_session(session, end_reason="ws_disconnect")
after = db.get_messages_as_conversation(session_id)
contents = [m.get("content") for m in after]
assert len(after) == 4, after
assert any("scan the repo" in (c or "") for c in contents), contents
assert any("PR opened" in (c or "") for c in contents), contents
def test_resumed_session_not_reflushed_as_duplicates(self, tmp_path, monkeypatch):
"""A resumed session torn down before any new turn (its transcript is
already durable in the DB) must NOT re-append duplicate rows."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
from hermes_state import SessionDB
import tui_gateway.server as srv
db = SessionDB(db_path=tmp_path / "state.db")
session_id = "sess-resumed"
db.create_session(session_id=session_id, source="tui")
loaded = [
{"role": "user", "content": "hello"},
{"role": "assistant", "content": "hi there"},
]
for m in loaded:
db.append_message(session_id=session_id, role=m["role"], content=m["content"])
monkeypatch.setattr(srv, "_get_db", lambda: db)
# Resumed session: history hydrated from the DB, no turn ran, so the
# agent processed nothing this session.
agent = self._real_agent(db, session_id, [])
session = _make_session(agent=agent, history=loaded, session_key=session_id)
srv._finalize_session(session, end_reason="ws_disconnect")
after = db.get_messages_as_conversation(session_id)
assert len(after) == 2, after
class TestOnSessionEndHook:
"""Verify on_session_end plugin hook fires on finalize."""
@patch("hermes_cli.plugins.invoke_hook")
def test_hook_fired_with_interrupted_true(self, mock_invoke_hook):
"""on_session_end is called with interrupted=True when finalizing."""
from tui_gateway.server import _finalize_session
agent = _make_agent(session_id="hook_test_001")
agent.model = "claude-sonnet-4"
agent.platform = "tui"
session = _make_session(agent=agent, history=[{"role": "user", "content": "test"}])
_finalize_session(session, end_reason="tui_close")
mock_invoke_hook.assert_any_call(
"on_session_end",
session_id="hook_test_001",
completed=False,
interrupted=True,
model="claude-sonnet-4",
platform="tui",
)