fix: close sibling finalize path for handed-off sessions (#88234)

/simplify-code review found _notify_single_query_session_finalize was
missing the _handed_off_session_ids guard that _should_emit_cleanup_session_finalize
and _emit_interrupted_session_end already had. One-shot CLI queries that
somehow handed off would still finalize the session via this path.

Added guard + test.
This commit is contained in:
kshitij
2026-08-17 13:26:29 +05:30
parent 59f302fef9
commit 3d6cfdd83b
2 changed files with 25 additions and 0 deletions
+4
View File
@@ -1363,6 +1363,10 @@ def _notify_single_query_session_finalize(cli, *, reason: str = "shutdown") -> N
session_id = getattr(agent, "session_id", None) or getattr(cli, "session_id", None)
if session_id in _single_query_finalize_attempted_session_ids:
return
# Don't finalize a session that was handed off to the gateway —
# the gateway owns the lifecycle now (#88234).
if session_id in _handed_off_session_ids:
return
try:
_notify_session_finalize(
+21
View File
@@ -136,3 +136,24 @@ def test_cleanup_finalizes_normal_session():
cli_mod._run_cleanup()
mock_finalize.assert_called_once()
def test_single_query_finalize_skipped_for_handed_off():
"""_notify_single_query_session_finalize must not fire for a handed-off session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
cli_mod._handed_off_session_ids.add("handoff-session-single")
agent = MagicMock()
agent.session_id = "handoff-session-single"
agent.platform = "cli"
cli_mock = MagicMock()
cli_mock.agent = agent
cli_mock.session_id = "handoff-session-single"
with patch("hermes_cli.lifecycle.finalize_session") as mock_finalize:
cli_mod._notify_single_query_session_finalize(cli_mock)
mock_finalize.assert_not_called()