mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
* fix(update): stop reading gateway identity off the restart watcher's argv (#107002) The detached restart watcher is spawned as `python -c <watcher source> <old_pid> <python> -m hermes_cli.main gateway run`. Its trailing argv is the command it will spawn LATER, but the canonical matchers read identity straight off the joined command line, so the watcher itself was classified as a live `gateway run` process — the documented "never infer process identity from argv substrings" bug class, on the exact surface `hermes update` uses to verify a post-update relaunch. Also budget the post-relaunch liveness poll against the watcher's own deadline: the watcher respawns the gateway only after the PID it was handed exits, so a 30 s window can expire before the relaunch it verifies was scheduled to start. * test(windows-live): run the gateway-ancestor harness parent from a script file A `python -c <src>` parent is an interpreter running inline source and carries no readable Hermes identity, so it is no longer a gateway to any classifier — the harness's own comment already said a realistic gateway argv is not a -c blob. * test(windows-live): share one sleeper SCRIPT across the live process-topology fixtures Four live Windows E2E files stood processes up as `python -c "sleep" <hermes argv tail>`. That shape no longer carries a readable Hermes identity, so the fixtures stopped standing in for the gateways they simulate. One shared sleeper script replaces the -c spelling. * fix(gateway): drop the duplicated _INLINE_SOURCE_FLAG_RE definition The constant was emitted twice around command_line_runs_inline_source. Same pattern both times, so behaviour is unchanged — but one definition is enough. * test(stderr-timestamp): run the gateway-lookalike children from script files Both lookalikes stood a gateway child up as `python -c <src> <gateway tail>`. That shape no longer carries a readable Hermes identity (#107002), so the wrapper correctly stopped treating them as gateway spawns and the tests failed. A `-c` tail is data for a program the inline source may spawn LATER, never the child's own identity — the real wrapper child is `python -m hermes_cli.main gateway run`, which has no `-c`. Running the stand-ins from a real script file restores what the tests mean to assert without depending on the misread. * test(windows-live): restore the tempfile import dropped with the local sleeper helper * test(windows-live): wait on the sleeper SCRIPT name, not its source text The live fixtures proved argv visibility by waiting for `time.sleep(120)` in the spawned process's command line. That string only ever appeared there because the sleeper was spelled `python -c "import time; time.sleep(120)"`; now that it runs from a file the source is in the file, so the probe timed out ("sleeper argv never visible") even though the argv was perfectly visible. Wait on the script name instead, exported as SLEEPER_MARKER next to the script so the probe and the spelling cannot drift apart again. * fix(tests,gateway): keep the live-system guard blocking -c-wrapped gateway spawns The #107002 identity fix made _gateway_command_subcommand return None for 'python -c <src> … -m hermes_cli.main gateway run'. tests/_fixtures/live_system_guard.py shares that matcher, so the autouse guard stopped blocking the detached restart watcher: real gateways leaked out of the e2e run and squatted the webhook port. Add gateway.status.gateway_spawn_intent_subcommand — the spawn-intent mirror of the identity matcher, peeling the inline-source wrapper token-wise and re-running the same canonical matcher on each suffix (still no substring matching) — and point the guard at it. Read-only subcommands stay spawnable. * fix(gateway): make the inline-source option walk value-aware so -X utf8 -c is not read as a gateway The walk that decides whether a command line is an interpreter running inline source (`python -c <src> ...`) treated every token starting with `-` as a flag and the first non-flag token as the end of the option block. CPython options that take a SEPARATE operand (-X/-W/-Q, --check-hash-based-pycs, --jit) break that model: the operand was mistaken for the end of the block, so the walk never reached the -c behind it and the watcher was read as a live gateway again -- exactly the #107002 misclassification, one shape further out. - Reuse the canonical operand sets from hermes_state_holders rather than hand-rolling a second copy (AGENTS.md: parser-derived flag sets). - Walk case-preserving tokens: operand-taking -Q/-W/-X must not be conflated with operand-less -q/-b, so callers no longer lowercase before the walk. - Handle clustered short options precisely (-uc is inline source, -Xc is -X c). - Replace the ad-hoc _INLINE_SOURCE_FLAG_RE rescan in gateway_spawn_intent_subcommand with the index the same walk returns; the regex could not find spellings the walk accepts and would raise StopIteration. Reported by an automated review on PR #121635 and reproduced here. Refs #107002 --------- Co-authored-by: Austin Pickett <[email protected]>
163 lines
5.7 KiB
Python
163 lines
5.7 KiB
Python
"""LIVE Windows E2E for the Desktop-lifecycle cold-start skip (#76129/#76745).
|
|
|
|
Runs ONLY on a real Windows host (the on-demand ``windows-venv-e2e.yml``
|
|
lane). Exercises the REAL ownership predicate against REAL processes:
|
|
|
|
1. A real child process self-registers in the REAL spawn ledger as a
|
|
``serve`` purpose with THIS process as its live spawner — ownership
|
|
must hold, and ``_pause_windows_gateways_for_update`` must return None
|
|
(no cold-start plan) even with an autostart artifact present.
|
|
2. Kill the child (dead serve) — ownership drops, the pause plan carries
|
|
``cold_start_if_installed`` again.
|
|
3. Venv-holder fallback rung: a real venv-python process with true
|
|
``serve`` argv is detected by the scan and, having a live parent,
|
|
confers ownership; the token classifier rejects a ``kanban
|
|
--preserve-cache`` lookalike (the #90778 class the salvage fixed).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from tests.live_process_fixtures import SLEEPER_MARKER, sleeper_script_path
|
|
|
|
pytestmark = pytest.mark.platforms("windows")
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
def _wait_until(predicate, timeout: float = 15.0, interval: float = 0.05) -> bool:
|
|
deadline = time.monotonic() + timeout
|
|
while time.monotonic() < deadline:
|
|
if predicate():
|
|
return True
|
|
time.sleep(interval)
|
|
return bool(predicate())
|
|
|
|
|
|
def _argv_visible(pid: int, marker: str) -> bool:
|
|
"""True once the process table shows *pid* with *marker* in its argv."""
|
|
import psutil
|
|
|
|
try:
|
|
return marker in " ".join(psutil.Process(pid).cmdline())
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
return False
|
|
|
|
|
|
@pytest.fixture()
|
|
def sleeper():
|
|
procs: list[subprocess.Popen] = []
|
|
|
|
def _spawn(*tail: str) -> subprocess.Popen:
|
|
p = subprocess.Popen(
|
|
[sys.executable, sleeper_script_path(), *tail],
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
procs.append(p)
|
|
assert _wait_until(lambda: _argv_visible(p.pid, SLEEPER_MARKER)), "sleeper argv never visible"
|
|
return p
|
|
|
|
yield _spawn
|
|
for p in procs:
|
|
if p.poll() is None:
|
|
p.kill()
|
|
p.wait()
|
|
|
|
|
|
def _write_ledger(entries: list[dict]) -> None:
|
|
from hermes_cli import process_identity as pid_mod
|
|
|
|
path = pid_mod._ledger_path()
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(entries), encoding="utf-8")
|
|
|
|
|
|
def _entry(proc: subprocess.Popen, purpose: str = "serve") -> dict:
|
|
import psutil
|
|
|
|
from hermes_cli import process_identity as pid_mod
|
|
|
|
return {
|
|
"install": pid_mod.install_id(None),
|
|
"pid": proc.pid,
|
|
"create_time": psutil.Process(proc.pid).create_time(),
|
|
"purpose": purpose,
|
|
"spawner_pid": os.getpid(),
|
|
"spawner_create": psutil.Process(os.getpid()).create_time(),
|
|
}
|
|
|
|
|
|
def test_live_supervised_serve_suppresses_cold_start(sleeper, monkeypatch, tmp_path):
|
|
from hermes_cli import gateway as hermes_gateway
|
|
from hermes_cli import gateway_windows
|
|
from hermes_cli import update_cmd
|
|
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
|
|
(tmp_path / ".hermes").mkdir()
|
|
|
|
serve = sleeper()
|
|
_write_ledger([_entry(serve)])
|
|
|
|
assert update_cmd._desktop_owns_gateway_lifecycle() is True
|
|
|
|
# Autostart artifact present + no gateway running: WITHOUT ownership the
|
|
# pause phase would plan a cold start; WITH it, no plan.
|
|
monkeypatch.setattr(hermes_gateway, "find_gateway_pids", lambda **_k: [])
|
|
monkeypatch.setattr(gateway_windows, "is_installed", lambda: True)
|
|
assert update_cmd._pause_windows_gateways_for_update() is None
|
|
|
|
# Dead serve → ownership drops → plan returns.
|
|
serve.kill()
|
|
serve.wait()
|
|
assert _wait_until(lambda: update_cmd._desktop_owns_gateway_lifecycle() is False)
|
|
token = update_cmd._pause_windows_gateways_for_update()
|
|
assert token is not None and token.get("cold_start_if_installed") is True
|
|
|
|
|
|
def test_holder_scan_fallback_respects_token_classifier(sleeper, monkeypatch, tmp_path):
|
|
from hermes_cli import update_cmd
|
|
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
|
|
(tmp_path / ".hermes").mkdir()
|
|
_write_ledger([]) # force the fallback rung
|
|
|
|
# Real process whose argv carries genuine serve shape, visible to psutil.
|
|
serve_like = sleeper("-m", "hermes_cli.main", "serve")
|
|
# Lookalike from the #90778 class — must NOT confer ownership.
|
|
kanban_like = sleeper("-m", "hermes_cli.main", "kanban", "--preserve-cache")
|
|
|
|
import psutil
|
|
|
|
# Snapshot both holder rows while both processes are alive. Building the
|
|
# kanban row later (after the serve kill) would re-read the dead serve pid,
|
|
# raise NoSuchProcess and hand the fallback an empty scan, which returns
|
|
# False without ever reaching the token classifier.
|
|
serve_row, kanban_row = (
|
|
(p.pid, psutil.Process(p.pid).name(), " ".join(psutil.Process(p.pid).cmdline()))
|
|
for p in (serve_like, kanban_like)
|
|
)
|
|
assert "--preserve-cache" in kanban_row[2]
|
|
|
|
monkeypatch.setattr(
|
|
"hermes_cli.main._detect_venv_python_processes", lambda: [serve_row, kanban_row]
|
|
)
|
|
# serve-shaped holder with a live parent (us) → owns
|
|
assert update_cmd._desktop_owns_gateway_lifecycle() is True
|
|
|
|
# Only the (still live) kanban lookalike left → classifier rejects → does not own
|
|
serve_like.kill()
|
|
serve_like.wait()
|
|
assert kanban_like.poll() is None
|
|
monkeypatch.setattr(
|
|
"hermes_cli.main._detect_venv_python_processes", lambda: [kanban_row]
|
|
)
|
|
assert update_cmd._desktop_owns_gateway_lifecycle() is False
|