Files
hermes-agent/tests/hermes_cli/test_handoff_cleanup_race.py
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

160 lines
5.2 KiB
Python

"""Regression tests for #88234 — CLI cleanup must NOT finalize a session that
was handed off to the gateway.
The handoff flow re-binds the CLI session_id to a gateway session_key via
``switch_session``, which reopens the session row. The CLI then exits and
``_run_cleanup`` fires ``_notify_session_finalize`` on that same session_id.
The resulting ``end_session`` call sets ``end_reason`` on a row the gateway
just reopened and is actively writing to — the handoff leg vanishes from
session history and ``session_search`` cannot find it.
The fix adds a module-level ``_handed_off_session_ids`` set (mirroring the
existing ``_single_query_finalize_attempted_session_ids`` pattern).
``_handle_handoff_command`` registers the session_id when the handoff
completes, and ``_should_emit_cleanup_session_finalize`` /
``_emit_interrupted_session_end`` check the set before firing.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
def _reset_cli_globals(cli_mod):
"""Reset the module-level globals the cleanup path checks."""
cli_mod._cleanup_done = False
cli_mod._cleanup_in_progress = False
cli_mod._single_query_finalize_attempted_session_ids.clear()
cli_mod._handed_off_session_ids.clear()
cli_mod._active_agent_ref = None
def test_handed_off_session_skips_cleanup_finalize():
"""_should_emit_cleanup_session_finalize returns False for a handed-off session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
cli_mod._handed_off_session_ids.add("handoff-session-123")
assert cli_mod._should_emit_cleanup_session_finalize("handoff-session-123") is False
def test_normal_session_still_finalizes():
"""_should_emit_cleanup_session_finalize returns True for a non-handed-off session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
cli_mod._single_query_finalize_attempted_session_ids.add("other-session")
assert cli_mod._should_emit_cleanup_session_finalize("normal-session") is True
assert cli_mod._should_emit_cleanup_session_finalize("other-session") is False
def test_interrupted_session_end_skipped_for_handed_off():
"""_emit_interrupted_session_end returns early for a handed-off session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
cli_mod._handed_off_session_ids.add("handoff-session-456")
agent = MagicMock()
agent.session_id = "handoff-session-456"
cli_mod._active_agent_ref = agent
cli_mock = MagicMock()
cli_mock.agent = agent
cli_mock.session_id = "handoff-session-456"
with patch("hermes_cli.lifecycle.invoke_hook") as mock_hook:
cli_mod._emit_interrupted_session_end(cli_mock, reason="keyboard_interrupt")
# on_session_end hook must NOT fire for a handed-off session
mock_hook.assert_not_called()
def test_interrupted_session_end_fires_for_normal():
"""_emit_interrupted_session_end fires for a normal (non-handed-off) session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
agent = MagicMock()
agent.session_id = "normal-session-789"
agent._current_task_id = ""
agent._current_turn_id = ""
agent._current_api_request_id = ""
agent.model = "test-model"
agent.platform = "cli"
cli_mod._active_agent_ref = agent
cli_mock = MagicMock()
cli_mock.agent = agent
cli_mock.session_id = "normal-session-789"
with patch("hermes_cli.lifecycle.invoke_hook") as mock_hook:
cli_mod._emit_interrupted_session_end(cli_mock, reason="keyboard_interrupt")
mock_hook.assert_called_once()
def test_cleanup_does_not_finalize_handed_off_session():
"""_run_cleanup must not call finalize_session for a handed-off session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
cli_mod._handed_off_session_ids.add("handoff-session-abc")
agent = MagicMock()
agent.session_id = "handoff-session-abc"
agent._session_messages = []
cli_mod._active_agent_ref = agent
with (
patch("hermes_cli.lifecycle.finalize_session") as mock_finalize,
patch("hermes_cli.plugins.invoke_hook"),
):
cli_mod._run_cleanup()
mock_finalize.assert_not_called()
def test_cleanup_finalizes_normal_session():
"""_run_cleanup DOES call finalize_session for a normal session."""
import cli as cli_mod
_reset_cli_globals(cli_mod)
agent = MagicMock()
agent.session_id = "normal-session-def"
agent._session_messages = []
cli_mod._active_agent_ref = agent
with (
patch("hermes_cli.lifecycle.finalize_session") as mock_finalize,
patch("hermes_cli.plugins.invoke_hook"),
):
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()