hermes-agent/tests/tui_gateway
Yishova be14a4bee3 tui_gateway: close dedicated profile SessionDB handles at teardown too
Follow-up to the review on the session.resume ownership fix. Closing the
pre-transfer early returns left two gaps, both real.

1. The transfer had no owner on the other side. Once ownership moved to the
   agent, teardown ran AIAgent.close() (via _teardown_session on session.close
   and the orphaned-session reaper), which called session_db.end_session() —
   that finalizes the session ROW, not the connection. A successfully resumed
   profile session kept its dedicated handle, its db/-wal/-shm fds and its
   background token-writer thread for the life of the gateway.

   AIAgent now carries an explicit _owns_session_db, defaulting False so the
   SHARED launch handle — which outlives every agent and backs every other live
   session — is still never closed there. Only the dedicated-open sites set it,
   at the point ownership actually changes hands.

2. session.resume was not the only profile-scoped open with no close on its
   failure paths. Covered here with the same flag, via a _transfer_db_to_agent
   helper that refuses the transfer unless the agent really holds that handle:

   - the deferred builder (_start_agent_build), including the session-reaped-
     mid-build case, where the built agent is discarded and never torn down, so
     transferring to it would leak exactly as before;
   - session.branch's branch_db;
   - the compute host's per-profile open;
   - AIAgent's own lazy open in _get_session_db_for_recall, which no other
     object ever references and so was unconditionally abandoned.

Where a handle has already reached a registered session, the drop is
unconditional and the transfer is best-effort on top: a refused transfer leaves
the old leak, which is survivable, whereas closing under a live session is the
permanent "Cannot operate on a closed database" break the original patch exists
to avoid.

Tests: tests/tui_gateway/test_session_db_ownership_teardown.py (new, 14).
11 of the 14 fail without this change; the 3 that pass are the "must NOT close"
guards, which hold in both directions by design.
2026-08-07 19:44:41 +05:30
..
__init__.py
…
test_auto_continue.py
…
test_billing_rpc.py
…
test_change_watcher.py
…
test_codex_app_server_live_events.py
…
test_cold_start_gil_stall.py test: harden cold-start regression tests + debug-log the env-var skip 2026-08-03 21:16:09 +05:30
test_compaction_status.py
…
test_compress_lock_skip.py
…
test_compute_host.py fix(compression): let explicit interrupts cancel safely 2026-08-02 22:15:20 +05:30
test_compute_host_phase1.py fix(tui_gateway): retain live-turn sessions unfinalized when the drain deadline expires 2026-08-03 09:59:58 +05:30
test_custom_provider_session_persistence.py
…
test_delegation_session_lifecycle.py
…
test_entry_import_off_main_thread.py
…
test_entry_picker_prewarm.py test(tui): pin picker-cache prewarm wiring in entry.main() 2026-08-04 12:28:05 +05:30
test_entry_sys_path.py
…
test_failed_turn_retention.py
…
test_fast_session_scope.py
…
test_finalize_session_persist.py
…
test_gateway_owned_session_reap.py
…
test_goal_command.py
…
test_gui_surface_toolsets.py fix(agent): the desktop's tools reach it on remote and cloud backends too 2026-08-06 19:35:47 -06:00
test_image_routing_stale_model.py
…
test_inline_rpc_gil_starvation.py
…
test_interim_assistant_callback.py
…
test_iso_certify_seam.py
…
test_kanban_notify_poller.py fix(tui): avoid writable Kanban opens on empty polls 2026-08-03 20:40:28 +05:30
test_make_agent_provider.py
…
test_mcp_late_refresh_thread_owner.py
…
test_mcp_reload_rev.py
…
test_moa_reference_emit.py
…
test_model_switch_marker_role.py
…
test_pet_generate_rpc.py
…
test_project_tree.py
…
test_projects_rpc.py
…
test_protocol.py test(tui_gateway): accept predicate kwarg in _close_session_by_id stub 2026-08-02 14:02:08 -07:00
test_reasoning_config_per_model.py
…
test_reasoning_session_scope.py
…
test_render.py
…
test_review_summary_callback.py
…
test_session_cwd_follow.py
…
test_session_db_ownership_teardown.py tui_gateway: close dedicated profile SessionDB handles at teardown too 2026-08-07 19:44:41 +05:30
test_session_id_injection.py
…
test_session_images_dir.py
…
test_session_platform_resolution.py
…
test_session_reclaim_notify.py
…
test_session_resume_db_ownership.py tui_gateway: session.resume abandons the profile SessionDB it opens 2026-08-07 19:44:41 +05:30
test_slash_worker_ansi.py
…
test_slash_worker_mcp_discovery.py
…
test_slash_worker_profile_home.py
…
test_slash_worker_sys_path.py
…
test_subagent_child_mirror.py
…
test_subprocess_encoding.py
…
test_undo_command.py
…
test_wait_for_mcp_discovery.py
…