Skip to content

The agent that grows with you

feat(update): hermes update --list-venv-holders prints the venv guard's holders as JSON, exit 3

teknium1eaef5ec7178d
diff --git a/hermes_cli/main.py b/hermes_cli/main.py
index 0de61ea4a80..5d8ffdca864 100644
--- a/hermes_cli/main.py
+++ b/hermes_cli/main.py
@@ -2350,6 +2350,19 @@ def _update_preflight_handled(args) -> bool:
print_update_plan(collect_runtime_inventory())
return True
+ if getattr(args, "list_venv_holders", False):
+ # Read-only twin of the Windows venv-holder refusal (#117246): same scan and classifiers,
+ # machine-readable, exit 3 when holders remain so automation can stop those PIDs and retry.
+ import json
+
+ from hermes_cli.update_cmd_windows import VENV_HOLDERS_EXIT, list_venv_holders
+
+ holders = list_venv_holders()
+ print(json.dumps(holders, indent=2))
+ if holders:
+ sys.exit(VENV_HOLDERS_EXIT)
+ return True
+
# Image/package-managed admission gate: baked provenance marker first
# (fail-closed on malformed), then docker/nix/apt heuristics. Records a
# `refused` receipt and exits 2 (refused-by-contract, distinct from errors).
diff --git a/hermes_cli/subcommands/update.py b/hermes_cli/subcommands/update.py
index d088af43451..de5c19ecad7 100644
--- a/hermes_cli/subcommands/update.py
+++ b/hermes_cli/subcommands/update.py
@@ -24,6 +24,12 @@ def build_update_parser(subparsers, *, cmd_update: Callable) -> None:
"kind (git/docker/nix), every running Hermes service across all "
"profiles with its supervisor and running code version, and how "
"each will be restarted. Read-only; safe on a live fleet.")
+ update_parser.add_argument(
+ "--list-venv-holders", action="store_true", default=False,
+ help="Print the processes the Windows venv-holder guard would refuse on as a JSON list "
+ "[{pid, exe, argv, kind}] and exit: 0 when the venv is free, 3 when holders are present. "
+ "Read-only; kind is gateway / backend (Desktop serve) / hermes:<subcommand> / python, so a "
+ "scheduled update can stop exactly those PIDs instead of looping. Always [] off Windows.")
update_parser.add_argument(
"--no-backup", action="store_true", default=False,
help="Skip ALL pre-update backups for this run (both the quick state snapshot and the full zip; overrides updates.pre_update_backup)",
diff --git a/hermes_cli/update_cmd_windows.py b/hermes_cli/update_cmd_windows.py
index f7937b7704c..51e2dc1c177 100644
--- a/hermes_cli/update_cmd_windows.py
+++ b/hermes_cli/update_cmd_windows.py
@@ -316,6 +316,46 @@ def _venv_launcher_ancestors(pids: list[int]) -> list[int]:
return found
+def _venv_holder_kind(cmdline: str) -> str:
+ """Machine-readable class of one venv holder for ``--list-venv-holders``.
+
+ ``gateway`` (the pausable gateway matcher), ``backend`` (``serve``/``dashboard`` -- the Desktop
+ app's backend shape), ``hermes:<subcommand>`` for any other Hermes entry, else ``python``.
+ Derived from the same classifiers the refusal path uses so automation stops exactly what the
+ guard would refuse on."""
+ from hermes_cli._scan_venv_blockers import _is_pausable_gateway
+ if _is_pausable_gateway(cmdline):
+ return "gateway"
+ subcommand = _hermes_holder_subcommand(cmdline)
+ if subcommand in _BACKEND_PURPOSES:
+ return "backend"
+ if subcommand:
+ return f"hermes:{subcommand}"
+ return "python"
+
+
+VENV_HOLDERS_EXIT = 3 # ``hermes update --list-venv-holders``: holders present (distinct from refusal 2)
+
+
+def list_venv_holders() -> list[dict]:
+ """``[{pid, exe, argv, kind}]`` for every process the venv-holder guard would refuse on, read-only.
+
+ Off Windows (or without psutil) the guard never fires, so the list is empty. ``exe``/``argv`` are the
+ live psutil values when readable (the scan may carry only a cmdline prefix)."""
+ from hermes_cli.update_cmd import _m
+ psutil = _psutil()
+ holders: list[dict] = []
+ for pid, name, cmdline in _m()._detect_venv_python_processes():
+ exe, argv = name, cmdline
+ if psutil is not None:
+ with suppress(Exception):
+ proc = psutil.Process(int(pid))
+ exe = proc.exe() or name
+ argv = " ".join(proc.cmdline()) or cmdline
+ holders.append({"pid": int(pid), "exe": exe, "argv": argv, "kind": _venv_holder_kind(argv)})
+ return holders
+
+
def _leftover_pausable_gateway_pids(matches: list[tuple[int, str, str]]) -> list[int] | None:
"""PIDs from *matches* when EVERY remaining venv holder is a pausable gateway, else ``None`` (keep refusing).
diff --git a/tests/hermes_cli/test_update_list_venv_holders.py b/tests/hermes_cli/test_update_list_venv_holders.py
new file mode 100644
index 00000000000..658f2f5775a
--- /dev/null
+++ b/tests/hermes_cli/test_update_list_venv_holders.py
@@ -0,0 +1,60 @@
+"""``hermes update --list-venv-holders``: read-only JSON twin of the Windows venv-holder refusal.
+
+Drives ``cmd_update`` (the production entry point) so the wiring in ``_update_preflight_handled``
+is what is proven, not the helper alone.
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+
+import pytest
+
+from hermes_cli import main as cli_main
+from hermes_cli import update_cmd_windows
+
+
+def _args(**overrides):
+ base = dict(list_venv_holders=True, plan=False, check=False, gateway=False, branch=None)
+ base.update(overrides)
+ return argparse.Namespace(**base)
+
+
+def _quiet_preflight(monkeypatch):
+ monkeypatch.setattr("hermes_cli.config.is_managed", lambda: False)
+ # No live psutil lookups: the fixture tuple IS the process table.
+ monkeypatch.setattr(update_cmd_windows, "_psutil", lambda: None)
+
+
+def test_list_venv_holders_json_and_exit_3_when_holders_present(monkeypatch, capsys, _quiet_preflight):
+ holders = [
+ (4242, "python.exe", r"C:\hermes\venv\Scripts\python.exe -m hermes_cli.main serve --port 8642"),
+ (4343, "python.exe", r"C:\hermes\venv\Scripts\python.exe -m hermes_cli.main gateway run"),
+ (4444, "python.exe", r"C:\hermes\venv\Scripts\python.exe -m hermes_cli.main -p work kanban list"),
+ (4545, "python.exe", r"C:\hermes\venv\Scripts\python.exe some_script.py"),
+ ]
+ monkeypatch.setattr(cli_main, "_detect_venv_python_processes", lambda: holders)
+
+ with pytest.raises(SystemExit) as exc:
+ cli_main.cmd_update(_args())
+
+ assert exc.value.code == update_cmd_windows.VENV_HOLDERS_EXIT == 3
+ payload = json.loads(capsys.readouterr().out)
+ assert [(h["pid"], h["kind"]) for h in payload] == [
+ (4242, "backend"), (4343, "gateway"), (4444, "hermes:kanban"), (4545, "python")]
+ assert set(payload[0]) == {"pid", "exe", "argv", "kind"}
+ assert payload[0]["argv"].endswith("serve --port 8642")
+
+
+def test_list_venv_holders_empty_list_exits_zero_without_updating(monkeypatch, capsys, _quiet_preflight):
+ monkeypatch.setattr(cli_main, "_detect_venv_python_processes", lambda: [])
+
+ def _boom(*_a, **_k): # the mutating update body must never run behind the read-only flag
+ raise AssertionError("update body ran")
+
+ monkeypatch.setattr("hermes_cli.update_cmd._cmd_update_impl", _boom)
+
+ assert cli_main.cmd_update(_args()) is None
+ assert json.loads(capsys.readouterr().out) == []
diff --git a/website/docs/getting-started/updating.md b/website/docs/getting-started/updating.md
index e139618b18b..715dd6ee306 100644
--- a/website/docs/getting-started/updating.md
+++ b/website/docs/getting-started/updating.md
@@ -210,6 +210,28 @@ Close the listed processes and re-run. If you're sure the concurrent process won
A second, separate guard refuses to touch the venv while any process is running from its Python interpreter (the Desktop app's backend, a gateway, a Python REPL). Those processes keep native extension files (`.pyd`) locked, and a dependency sync that dies partway on an access-denied error strands the install between versions. This guard is **not** bypassed by `--force`; if you're certain the detected holders are false positives, use the explicit `hermes update --force-venv`.
+#### Scripted updates: `hermes update --list-venv-holders`
+
+A scheduled `hermes update --yes` that keeps hitting the venv guard (typically because the
+Desktop app relaunches its backend) can ask first instead of looping. `hermes update
+--list-venv-holders` is read-only: it prints the processes the guard would refuse on as a
+JSON list of `{pid, exe, argv, kind}` and exits `0` when the venv is free or `3` when holders
+are present. `kind` is `gateway` (a pausable gateway the updater handles itself), `backend`
+(a `hermes serve` / dashboard backend — the Desktop app's shape), `hermes:<subcommand>` for any
+other Hermes process, or `python` for an unrelated interpreter. Automation can stop exactly
+those PIDs (or quit the Desktop app) and retry; nothing is terminated by the flag itself. The
+guard only exists on Windows, so the list is always `[]` elsewhere.
+
+```
+$ hermes update --list-venv-holders
+[
+ {"pid": 4242, "exe": "C:\\hermes\\venv\\Scripts\\python.exe",
+ "argv": "...python.exe -m hermes_cli.main serve --port 8642", "kind": "backend"}
+]
+$ echo $LASTEXITCODE
+3
+```
+
Both guards, the Desktop update preflight, and the dependency repair steps look for the environment at `venv` first and then at the uv-default `.venv`, so a source checkout set up with `uv venv` / `uv sync` updates the same way an installer-created `venv` does. When both directories exist, `venv` is the one that gets updated.
#### Windows: the update finishes under the venv Python