hermes-agent/tests/hermes_cli/test_run_with_idle_timeout.py
Teknium 432a691758
fix(update): stream + idle-kill npm run build so a stalled webui-build can't soft-brick the install (#33803)
`hermes update` ran the webui build with `capture_output=True` and no timeout. On low-memory hosts (WSL2's 4 GB default, small VPSes, antivirus stalls) Vite goes silent for minutes; users see a frozen terminal, decide the update is hung, and reboot. The reboot lands *after* `pip install -e .` has already touched the install but *before* the build completes, leaving the `hermes` launcher in place while `hermes_cli` is no longer importable — i.e. `ModuleNotFoundError: No module named 'hermes_cli'` (#33788, same class as #32384).

Changes:

- New `_run_with_idle_timeout()` helper: streams subprocess output line-by-line (so the user sees Vite progress in real time) and kills the process if no bytes appear on stdout/stderr for 180s. The existing stale-dist fallback (#23817) then serves the previous build instead of failing the update.
- `_build_web_ui()` uses the helper for `npm run build` (the actual stall site). `npm install` keeps `subprocess.run` + capture_output to preserve the existing EPERM-retry-on-Windows contract.
- Both `cmd_update` call sites print `→ Core update complete. Building dashboard (optional)...` before the webui build. The CLI is fully functional at this point; a webui-build failure only affects `hermes dashboard`. Telegraphing the boundary explicitly stops users from rebooting through the build step.

Tests:

- `tests/hermes_cli/test_run_with_idle_timeout.py` — 4 tests covering streaming success, nonzero exit, idle-kill, and missing-binary cases. Uses real `subprocess.Popen` on tiny Python scripts; isolated in its own file so per-file canonical-runner parallelism doesn't pair it with the mock-heavy tests.
- `tests/hermes_cli/test_web_ui_build.py` — updated existing tests to patch `_run_with_idle_timeout` for the build step in addition to `subprocess.run` for the install step.
- `tests/hermes_cli/test_cmd_update.py::test_update_refreshes_repo_and_tui_node_dependencies` — same update.

Full suite: `scripts/run_tests.sh tests/hermes_cli/` → 5646 passed, 0 failed.

Fixes #33788.
2026-05-28 03:34:47 -07:00

67 lines
2.3 KiB
Python

"""Coverage for _run_with_idle_timeout — the streaming subprocess helper.
Kept in a dedicated test file because the tests spawn real ``subprocess.Popen``
instances; pytest-isolate runs each test file in its own worker process, so
isolating these here prevents real-Popen state from racing with the
``subprocess.run`` / ``_run_with_idle_timeout`` patches used by
``test_web_ui_build.py``.
Added for issue #33788: ``hermes update`` got stuck at "webui-build" because
``npm run build`` ran with ``capture_output=True`` and no timeout. The helper
fixes both halves — streams output AND idle-kills the process.
"""
import sys as _sys
import time
from hermes_cli.main import _run_with_idle_timeout
def test_streams_output_and_returns_zero_on_success(tmp_path):
script = tmp_path / "ok.py"
script.write_text("print('line one'); print('line two')\n")
result = _run_with_idle_timeout(
[_sys.executable, str(script)], cwd=tmp_path, idle_timeout_seconds=10
)
assert result.returncode == 0
assert "line one" in result.stdout
assert "line two" in result.stdout
def test_propagates_nonzero_exit(tmp_path):
script = tmp_path / "fail.py"
script.write_text("import sys; print('boom', file=sys.stderr); sys.exit(7)\n")
result = _run_with_idle_timeout(
[_sys.executable, str(script)], cwd=tmp_path, idle_timeout_seconds=10
)
assert result.returncode == 7
# stderr is merged into stdout in the helper.
assert "boom" in result.stdout
def test_kills_process_on_idle_timeout(tmp_path):
# Sleeps without printing — exactly the failure mode users see when
# `npm run build` stalls. Idle timeout must terminate it.
script = tmp_path / "stall.py"
script.write_text("import time; time.sleep(30)\n")
start = time.monotonic()
result = _run_with_idle_timeout(
[_sys.executable, str(script)],
cwd=tmp_path,
idle_timeout_seconds=1,
)
elapsed = time.monotonic() - start
# Should have died well before the 30s sleep completes.
assert elapsed < 15
assert result.returncode != 0
assert "produced no output" in result.stdout
def test_returns_127_when_binary_missing(tmp_path):
result = _run_with_idle_timeout(
["/nonexistent/binary/does/not/exist"],
cwd=tmp_path,
idle_timeout_seconds=5,
)
assert result.returncode == 127