hermes-agent/tests/tui_gateway
bogerman1 c7e8854cb3 fix(tui): persist session messages on force-quit / signal shutdown
Mirror the CLI's exit-path behaviour in the TUI gateway so that
unpersisted conversation messages are flushed to state.db and the
on_session_end plugin hook fires before the session is closed.

Root cause: _finalize_session() only called db.end_session() to
mark the session row as ended, but did NOT flush in-memory messages
via _persist_session() or fire the on_session_end hook.  When the
user force-quit (double Ctrl-C, terminal-close, SIGHUP) while the
agent was mid-turn, messages accumulated since the last persist
point were silently lost.

Changes
-------
tui_gateway/server.py - _finalize_session():
  - Persist unflushed messages via agent._persist_session() before
    db.end_session(). Prefers agent._session_messages (set by the
    last _persist_session call inside run_conversation) over
    session['history'] (stale when agent is mid-turn).
  - Fire on_session_end(interrupted=True) plugin hook so crash-
    recovery plugins can flush buffers, matching cli.py behaviour.

tui_gateway/entry.py - _log_signal():
  - Explicitly call _shutdown_sessions() before sys.exit(0) in the
    SIGHUP/SIGTERM handler as belt-and-suspenders over atexit.

tests/tui_gateway/test_finalize_session_persist.py (new):
  - 11 tests covering: history persistence, _session_messages
    priority, empty-history skip, missing-agent, double-finalize,
    persist-exception resilience, hook firing, hook-exception
    resilience, and db.end_session preservation.

Related
-------
Closes the TUI half of #5021 (CLI already handles this via its
atexit handler).  Also addresses the session-persistence gap
discussed in #18465 and #18269.
2026-06-21 07:26:07 -07:00
..
__init__.py feat: add tests and update mds 2026-04-08 19:31:25 -05:00
test_billing_rpc.py feat(billing): /billing terminal billing — interactive TUI + CLI client (#45449) 2026-06-19 01:53:32 +05:30
test_compaction_status.py fix(desktop): show summarizing indicator during auto-compaction 2026-06-14 02:28:07 -05:00
test_custom_provider_session_persistence.py fix(desktop): never persist or restore a named custom provider as bare "custom" (#48547) 2026-06-18 11:11:51 -07:00
test_entry_sys_path.py chore: prune unused imports and duplicate import redefinitions 2026-05-28 22:26:25 -07:00
test_finalize_session_persist.py fix(tui): persist session messages on force-quit / signal shutdown 2026-06-21 07:26:07 -07:00
test_goal_command.py fix(tui): route pending-input commands via command.dispatch (#48848) 2026-06-19 14:53:33 +05:30
test_make_agent_provider.py fix(model): persist /model switch by default across sessions 2026-06-19 07:07:06 -07:00
test_protocol.py fix(tui): route pending-input commands via command.dispatch (#48848) 2026-06-19 14:53:33 +05:30
test_render.py feat: add tests and update mds 2026-04-08 19:31:25 -05:00
test_review_summary_callback.py fix(desktop,tui): surface self-improvement review summary + honor memory_notifications 2026-06-18 13:22:12 -05:00
test_subagent_child_mirror.py feat(desktop): stream subagent activity into watch windows (#47060) 2026-06-16 14:30:11 -04:00
test_undo_command.py feat(undo): /undo [N] backs up N user turns with prefill + soft-delete 2026-06-01 01:22:38 -07:00
test_wait_for_mcp_discovery.py perf(tui): stop slow/dead MCP servers from freezing TUI startup 2026-05-30 02:53:37 -07:00