d10bb2ab6f
`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.
192 lines
7.8 KiB
Python
192 lines
7.8 KiB
Python
"""Regression tests for #48820 (4th repro): job-object teardown killed the
|
|
post-update respawned gateway silently, and the updater printed
|
|
"✓ Restarting Windows gateway profile(s)" anyway.
|
|
|
|
Two fixes under test:
|
|
|
|
1. ``_spawn_gateway_restart_watcher``'s inlined watcher source must
|
|
(a) route the respawned gateway's stray stdout/stderr to
|
|
``logs/gateway-stdio.log`` (it was ``DEVNULL`` — a gateway killed by
|
|
parent Job Object teardown left ZERO trace anywhere), and
|
|
(b) stamp ``_HERMES_GATEWAY_BREAKAWAY`` =1/0 on the respawn env exactly
|
|
like the canonical ``gateway_windows._spawn_detached``, so the
|
|
lifecycle/exit-diag records show whether the gateway escaped the
|
|
parent's Job Object.
|
|
|
|
2. ``_resume_windows_gateways_after_update`` must verify a stable gateway
|
|
process actually exists (via ``gateway_windows._wait_for_gateway_ready``)
|
|
before printing the ✓ — a truthy launch return only proves the watcher
|
|
process was created, not that the respawned gateway survived the
|
|
updater's Job Object teardown.
|
|
"""
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
import hermes_cli.gateway as gateway
|
|
import hermes_cli.gateway_windows as gateway_windows
|
|
import hermes_cli.main as hm
|
|
import hermes_cli.main_install_repair as main_install_repair
|
|
from hermes_cli._subprocess_compat import _WINDOWS_GATEWAY_BREAKAWAY_ENV
|
|
from hermes_cli.update_cmd import _resume_windows_gateways_after_update
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 1. Watcher template contract
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _captured_watcher_source(monkeypatch) -> str:
|
|
"""Spawn the watcher with a mocked Popen and return the inlined -c source."""
|
|
captured = {}
|
|
|
|
def fake_popen(argv, **kwargs):
|
|
captured["argv"] = argv
|
|
captured["kwargs"] = kwargs
|
|
|
|
class _P:
|
|
pid = 12345
|
|
|
|
return _P()
|
|
|
|
monkeypatch.setattr(gateway.subprocess, "Popen", fake_popen)
|
|
assert gateway._spawn_gateway_restart_watcher(
|
|
999999, ["python", "-m", "hermes_cli.main", "gateway", "run"]
|
|
)
|
|
argv = captured["argv"]
|
|
assert argv[1] == "-c"
|
|
return argv[2]
|
|
|
|
|
|
class TestWatcherRespawnTemplate:
|
|
def test_respawn_stdio_routed_to_sidecar_log_not_devnull(self, monkeypatch):
|
|
"""DEVNULL swallowed the dying gateway's last words (#48820 4th
|
|
repro: 'Zero trace anywhere ... because the watcher respawns with
|
|
stdout=DEVNULL, stderr=DEVNULL')."""
|
|
src = _captured_watcher_source(monkeypatch)
|
|
assert "gateway-stdio.log" in src, (
|
|
"watcher respawn must route stray stdout/stderr to the same "
|
|
"sidecar log _spawn_detached uses, so a gateway killed moments "
|
|
"after respawn leaves a trace"
|
|
)
|
|
# DEVNULL remains only as the fallback when the log dir is
|
|
# unavailable — the popen kwargs must not be hardwired to it.
|
|
assert '"stdout": _stdio_target' in src
|
|
assert '"stderr": _stdio_target' in src
|
|
|
|
def test_respawn_stamps_breakaway_state_like_spawn_detached(
|
|
self, monkeypatch
|
|
):
|
|
"""The respawned gateway must carry _HERMES_GATEWAY_BREAKAWAY=1 on
|
|
the primary (breakaway) spawn and =0 on the no-breakaway fallback,
|
|
mirroring gateway_windows._spawn_detached — without the stamp, a
|
|
job-teardown kill is indistinguishable from any other silent death
|
|
in the exit diagnostics."""
|
|
src = _captured_watcher_source(monkeypatch)
|
|
assert "_WINDOWS_GATEWAY_BREAKAWAY_ENV" in src
|
|
assert _WINDOWS_GATEWAY_BREAKAWAY_ENV == "_HERMES_GATEWAY_BREAKAWAY"
|
|
# Primary stamps "1", the OSError fallback stamps "0".
|
|
assert '_WINDOWS_GATEWAY_BREAKAWAY_ENV: "1"' in src
|
|
assert '_WINDOWS_GATEWAY_BREAKAWAY_ENV: "0"' in src
|
|
|
|
def test_respawn_source_compiles(self, monkeypatch):
|
|
"""The inlined -c template is built via str.format over a
|
|
dedented literal — guard against brace/indentation regressions."""
|
|
src = _captured_watcher_source(monkeypatch)
|
|
compile(src, "<watcher>", "exec")
|
|
|
|
def test_watcher_fallback_retry_preserved(self, monkeypatch):
|
|
"""The ERROR_ACCESS_DENIED retry without breakaway must survive."""
|
|
src = _captured_watcher_source(monkeypatch)
|
|
assert "windows_detach_flags_without_breakaway" in src
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 2. Post-update resume liveness gate
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def _token(profiles: dict) -> dict:
|
|
return {
|
|
"resume_needed": True,
|
|
"profiles": profiles,
|
|
"unmapped_pids": [],
|
|
"unmapped": [],
|
|
}
|
|
|
|
|
|
class TestResumeLivenessGate:
|
|
@pytest.fixture(autouse=True)
|
|
def _windows(self, monkeypatch):
|
|
monkeypatch.setattr(hm, "_is_windows", lambda: True)
|
|
monkeypatch.setattr(main_install_repair, "_is_windows", lambda: True)
|
|
monkeypatch.setattr(hm, "_refresh_windows_gateway_launchers", lambda: None)
|
|
monkeypatch.setattr(
|
|
gateway, "launch_detached_profile_gateway_restart", lambda *_a: True
|
|
)
|
|
monkeypatch.setattr(
|
|
gateway, "launch_detached_gateway_restart_by_cmdline", lambda *_a: True
|
|
)
|
|
|
|
def test_dead_respawn_fails_the_resume_instead_of_printing_check(
|
|
self, monkeypatch
|
|
):
|
|
"""No stable gateway after the relaunch → the resume raises (update
|
|
marked incomplete) instead of printing '✓ Restarting'. This is the
|
|
exact #48820 3rd/4th-repro hole: spawn succeeded, gateway died
|
|
within seconds, success was reported, platforms were offline for
|
|
12.5 hours."""
|
|
monkeypatch.setattr(
|
|
gateway_windows, "_wait_for_gateway_ready", lambda **_kw: []
|
|
)
|
|
token = _token({"default": 1111})
|
|
printed = []
|
|
with patch("builtins.print", side_effect=lambda *a, **k: printed.append(a)):
|
|
with pytest.raises(RuntimeError, match="not verified alive"):
|
|
_resume_windows_gateways_after_update(token)
|
|
|
|
text = " ".join(str(a) for a in printed)
|
|
assert "✓ Restarting" not in text
|
|
assert "could not be verified" in text
|
|
# The profile stays on the token so retry/reporting still sees it.
|
|
assert token["profiles"] == {"default": 1111}
|
|
assert token["resume_needed"] is True
|
|
|
|
def test_live_respawn_prints_check_and_writes_attestation(self, monkeypatch):
|
|
monkeypatch.setattr(
|
|
gateway_windows, "_wait_for_gateway_ready", lambda **_kw: [777]
|
|
)
|
|
attested = {}
|
|
monkeypatch.setattr(
|
|
gateway_windows,
|
|
"_write_start_attestation",
|
|
lambda pids, via: attested.update(pids=pids, via=via),
|
|
)
|
|
token = _token({"default": 1111})
|
|
printed = []
|
|
with patch("builtins.print", side_effect=lambda *a, **k: printed.append(a)):
|
|
_resume_windows_gateways_after_update(token)
|
|
|
|
text = " ".join(str(a) for a in printed)
|
|
assert "✓ Restarting" in text
|
|
assert attested == {"pids": [777], "via": "post-update relaunch"}
|
|
assert token["resume_needed"] is False
|
|
|
|
def test_liveness_poll_scans_all_profiles(self, monkeypatch):
|
|
"""The resume relaunches the whole fleet; the verification must not
|
|
be scoped to the active profile."""
|
|
seen = {}
|
|
|
|
def fake_wait(**kwargs):
|
|
seen.update(kwargs)
|
|
return [777]
|
|
|
|
monkeypatch.setattr(gateway_windows, "_wait_for_gateway_ready", fake_wait)
|
|
monkeypatch.setattr(
|
|
gateway_windows, "_write_start_attestation", lambda *_a, **_kw: None
|
|
)
|
|
with patch("builtins.print"):
|
|
_resume_windows_gateways_after_update(_token({"work": 2222}))
|
|
assert seen.get("all_profiles") is True
|