Files
hermes-agent/tests/hermes_cli/conftest.py
teknium1 94ced1a2b2 fix(update): finish hermes update in an interpreter born on the pulled code
`hermes update` started in an interpreter that had imported the PRE-pull tree, then
kept running every post-swap phase (dependency sync, Node/web/Desktop builds,
maintenance, config migration, fleet restart, verification, receipt) in that same
process, lazily importing NEW source into an OLD `sys.modules` graph. Any rename
between the two commits surfaced as an ImportError/AttributeError inside the updater
after the code swap had already succeeded (#87134, #111271, #112465, #112558, #112604).
Each incident added another purge, reload list or per-step isolation, and each moved
the crash to the next module nobody had listed.

The pre-pull process now stops at the swap: it writes the open receipt, the pre-update
fleet plan, the pre-update version/active features and the Windows pause token to a
hand-off file and re-executes `hermes update <same flags> --post-swap <file>` under the
venv interpreter. The child imports exclusively from the pulled tree, resumes the
receipt and owns the rest of the run; the parent relays its exit code. Git and ZIP paths
both hand off. On Windows, when the updater runs from `hermes.exe`, the child is spawned
detached exactly as the shim hand-off already did (the shim cannot be awaited while the
sync must replace it).

With no pulled code ever executing in a pre-pull interpreter, the stale-module layer is
dead and removed: `_purge_stale_hermes_modules`, `_stale_purge_prefixes`,
`_evict_module`, `_STALE_PURGE_*`, `_reload_updated_runtime_modules`,
`_reload_process_scan_modules`, `_reload_config_modules`, `_UPDATE_RUNTIME_RELOAD_MODULES`
and their tests. `_run_config_check_fresh` / `_run_migrate_config_fresh` keep their names
and simply call the config API.

Tests: the hand-off boundary (child argv/env, detached receipt + plan in the payload,
exit-code relay) and the child side (receipt resumed with its history, plan rebuilt,
pre-update snapshots taken from the payload). Mocked updater flows run the tail
in-process through the same payload round-trip (autouse fixture; opt out with
`@pytest.mark.real_post_swap_handoff`).
2026-09-17 00:02:09 -07:00

158 lines
7.2 KiB
Python

"""Fixtures shared across hermes_cli tests."""
from __future__ import annotations
import pytest
@pytest.fixture
def all_assignees_spawnable(monkeypatch):
"""Pretend every assignee maps to a real Hermes profile.
Most dispatcher tests use synthetic assignees ("alice", "bob") that
don't correspond to actual profile directories on disk. Without this
patch, the dispatcher's profile-exists guard (PR #20105) routes
those tasks into ``skipped_nonspawnable`` instead of spawning, which
would break tests that assert spawn behavior.
"""
from hermes_cli import profiles
monkeypatch.setattr(profiles, "profile_exists", lambda name: True)
@pytest.fixture(autouse=True)
def _suppress_concurrent_hermes_gate(request, monkeypatch):
"""Default ``_detect_concurrent_hermes_instances`` to ``[]`` for every test.
The Windows update path now refuses to proceed when another
``hermes.exe`` is detected (issue #26670). On a developer's Windows
machine running the test suite via ``hermes`` itself, this would
flag the running agent as a concurrent instance and abort every
``cmd_update`` test. Tests that want to exercise the gate explicitly
re-patch ``_detect_concurrent_hermes_instances`` with their own
return value — autouse here gives a clean default without touching
the rest of the suite.
Tests that need to call the REAL function (e.g. unit tests for the
helper itself) opt out with ``@pytest.mark.real_concurrent_gate``.
"""
if request.node.get_closest_marker("real_concurrent_gate"):
return
try:
from hermes_cli import main as _cli_main
except Exception:
return
# raising=False: under pytest's per-test spawn isolation, a concurrent
# xdist worker importing a module that transitively touches hermes_cli.main
# can briefly expose a partially-initialized module object here — one where
# _detect_concurrent_hermes_instances isn't defined yet. A bare setattr
# would raise AttributeError and error the (unrelated) test. The attribute
# always exists once main.py finishes importing, so a no-op when it's
# transiently absent is the correct, race-free default.
monkeypatch.setattr(
_cli_main,
"_detect_concurrent_hermes_instances",
lambda *_a, **_k: [],
raising=False,
)
@pytest.fixture(autouse=True)
def _inline_post_swap_handoff(request, monkeypatch):
"""Run the post-swap tail in-process instead of re-executing ``hermes update --post-swap``.
``_apply_pulled_update`` / ``_update_via_zip`` hand the rest of the run to a child
interpreter on the pulled tree. A mocked updater flow must not spawn that child (it would
run a real dependency sync against the worktree), so the tail runs here through the same
payload round-trip — every step stays patchable and the payload shape is still exercised.
Tests of the hand-off itself opt out with ``@pytest.mark.real_post_swap_handoff``.
"""
if request.node.get_closest_marker("real_post_swap_handoff"):
return
try:
from hermes_cli import update_cmd, update_receipt
except Exception:
return
def _inline(args, **payload_kwargs):
payload = update_cmd._post_swap_payload(**payload_kwargs)
if payload["receipt"]:
update_receipt.resume_update_receipt(payload["receipt"])
update_cmd._execute_post_swap(payload, args, payload_kwargs["gateway_mode"])
monkeypatch.setattr(update_cmd, "_hand_off_post_swap", _inline, raising=False)
@pytest.fixture
def isolated_update_runtime(monkeypatch, tmp_path, request):
"""Keep mocked updater flows off the host checkout and runtime fleet."""
from hermes_cli import gateway, main, update_cmd, update_cmd_fleet
from hermes_cli import update_inventory, update_receipt
checkout = tmp_path / "isolated-update-checkout"
(checkout / ".git").mkdir(parents=True)
(checkout / "apps" / "desktop").mkdir(parents=True)
monkeypatch.setattr(main, "PROJECT_ROOT", checkout)
if hasattr(request.module, "PROJECT_ROOT"):
monkeypatch.setattr(request.module, "PROJECT_ROOT", checkout)
monkeypatch.setattr(gateway, "find_gateway_pids", lambda *a, **k: [])
monkeypatch.setattr(gateway, "find_profile_gateway_processes", lambda *a, **k: [])
monkeypatch.setattr(gateway, "_get_service_pids", lambda *a, **k: set())
monkeypatch.setattr(gateway, "supports_systemd_services", lambda: False)
monkeypatch.setattr(main, "_pause_windows_gateways_for_update", lambda: None)
monkeypatch.setattr(main, "_resume_windows_gateways_after_update", lambda *a, **k: None)
monkeypatch.setattr(main, "_detect_venv_python_processes", lambda: [])
monkeypatch.setattr(main, "_restore_active_tool_dependencies", lambda *a, **k: None)
monkeypatch.setattr(update_cmd, "_clear_windows_venv_holders_or_exit", lambda *a, **k: None)
monkeypatch.setattr(update_cmd, "_finish_dashboard_update_cleanup", lambda *a, **k: None)
monkeypatch.setattr(update_cmd, "_apply_pending_fleet_restart_catchup", lambda *a, **k: None)
monkeypatch.setattr(update_cmd_fleet, "_restart_macos_launchd_gateways", lambda *a, **k: None)
monkeypatch.setattr(update_inventory, "collect_runtime_inventory", lambda: None)
monkeypatch.setattr(update_receipt, "collect_fleet_versions", lambda *a, **k: [])
# ---- prompt_toolkit / capsys isolation ----
# ``cli._cprint`` renders through ``prompt_toolkit.print_formatted_text``,
# which — when called with no explicit ``output=`` — lazily creates an
# ``Output`` from ``sys.stdout`` **and caches it on the process-global default
# ``AppSession``** (``prompt_toolkit.application.current._current_app_session``,
# a ``ContextVar`` with a module-level default). The cache is keyed to nothing
# and never re-reads ``sys.stdout``.
#
# Under pytest, ``capsys`` swaps ``sys.stdout`` for a fresh buffer per test.
# So the first CLI test that emits through ``_cprint`` (e.g. one exercising
# ``/queue``, which prints a "Queued: …" line) locks prompt_toolkit's cached
# output onto *its* captured stdout. Every later ``capsys`` test that asserts
# on ``_cprint`` output then reads an empty buffer, because the render went to
# the first test's now-dead capture target. That is the mechanism behind the
# order-dependent ``test_resume_quiet_stderr`` failure: it passes in isolation
# and in its own file, but fails in a full ``tests/cli`` run.
#
# Reset the cached output before every CLI test so each one re-creates a fresh
# prompt_toolkit ``Output`` bound to its own ``sys.stdout`` on first use. This
# is a no-op when prompt_toolkit isn't importable and cheap otherwise (the
# property re-creates lazily).
@pytest.fixture(autouse=True)
def _reset_prompt_toolkit_output_cache():
"""Clear prompt_toolkit's cached AppSession output around each CLI test.
See the module docstring for the capsys/prompt_toolkit interaction this
guards against.
"""
def _clear() -> None:
try:
from prompt_toolkit.application.current import get_app_session
get_app_session()._output = None
except Exception:
# prompt_toolkit not importable / internal shape changed — the
# tests that rely on this simply keep their prior behavior.
pass
_clear()
yield
_clear()