From a4caab42ed6110d9681c0af7840f03ee252b836a Mon Sep 17 00:00:00 2001 From: nvaldez Date: Wed, 4 Nov 2026 10:39:34 +0100 Subject: [PATCH] feat: --integrate hermes is now one command (0.4.6) The installer now also restarts a RUNNING gateway daemon (SIGUSR1 drain-restart, waits for pid cycle or old-pid exit) so the plugin module is re-imported; --no-restart opts out. cli.py dispatches --integrate/--rm-integrate hermes to the installer. 62 tests. --- .../integration/hermes/install.py | 129 +++++++++++++++++- .../integration/hermes/plugin/plugin.yaml | 2 +- pyproject.toml | 2 +- tests/test_agent_cost_estimator.py | 111 +++++++++++++++ 4 files changed, 237 insertions(+), 7 deletions(-) diff --git a/agent_cost_estimator/integration/hermes/install.py b/agent_cost_estimator/integration/hermes/install.py index 11e1af6..235d6e8 100644 --- a/agent_cost_estimator/integration/hermes/install.py +++ b/agent_cost_estimator/integration/hermes/install.py @@ -12,10 +12,16 @@ Or, from a source checkout / any machine with the package installed: What it does (idempotent): 1. Copies the plugin (plugin.yaml + __init__.py) to /plugins/agent-cost-estimator/ -2. Runs `hermes plugins enable agent-cost-estimator` (via the +2. Ships pricing.json beside the plugin (an existing tuned table is + kept across reinstalls) +3. Runs `hermes plugins enable agent-cost-estimator` (via the Hermes CLI when on PATH; otherwise prints the manual step) +4. Restarts a RUNNING gateway daemon (SIGUSR1 drain-restart, waits + for the new pid) so it re-imports the plugin — the daemon caches + the plugin module at first hook fire. A stopped gateway needs no + restart. Skip with --no-restart. -After a restart of Hermes (CLI/TUI/desktop), a line like: +After that, a line like: -- zai:zai-glm-5-3 | session:$0.42 | turn:$0.16 @@ -35,12 +41,114 @@ import argparse import os import pathlib import shutil +import signal import subprocess import sys +import time PLUGIN_NAME = "agent-cost-estimator" PLUGIN_SRC = pathlib.Path(__file__).resolve().parent / "plugin" +# How long to wait for a restarted gateway to come back up before +# giving up (the gateway is only restarted when it is already +# running; a stopped gateway is started lazily by the next CLI). +RESTART_WAIT_S = 90.0 +# A SIGUSR1 restart is drain-aware: the daemon defers its stop until +# in-flight turns finish (up to a long cap), so it may keep the same +# pid for a while. Poll this long for EITHER a new pid OR the +# .restart_pending.json marker disappearing after the old pid died. +RESTART_DEFER_POLL_S = 30.0 + + +def _read_gateway_pid(home: pathlib.Path) -> int | None: + pid_file = home / "gateway.pid" + try: + import json + data = json.loads(pid_file.read_text(encoding="utf-8")) + pid = int(data.get("pid") or 0) + return pid or None + except (OSError, ValueError, TypeError): + return None + + +def _pid_alive(pid: int) -> bool: + try: + os.kill(pid, 0) + return True + except (ProcessLookupError, PermissionError, OSError): + return False + + +def _gateway_running(home: pathlib.Path) -> int | None: + """PID of the live gateway daemon for this home, or None.""" + pid = _read_gateway_pid(home) + if pid and _pid_alive(pid): + return pid + return None + + +def _restart_gateway(home: pathlib.Path, dry_run: bool) -> None: + """Restart the running gateway daemon so it re-imports the plugin. + + The daemon imports the plugin once at first hook fire, so a + plugin update on disk is not picked up until the daemon exits. + SIGUSR1 asks it to drain and restart itself (the supervisor + relaunches it); a gateway that is not running needs no restart — + the next `hermes` CLI starts a fresh daemon that reads the new + files. Safe to call from inside a gateway child: SIGUSR1 only + reaches the daemon, never this process. + """ + pid = _gateway_running(home) + if pid is None: + print("Gateway not running; the next `hermes` launch starts a " + "fresh daemon that reads the plugin — no restart needed.") + return + print(f"Restarting running gateway (pid {pid}) so it re-imports " + f"the plugin ...") + if dry_run: + print("dry run: no signal sent") + return + if not hasattr(signal, "SIGUSR1"): + print("warning: SIGUSR1 unavailable on this platform; run " + "`hermes gateway restart` manually from a separate shell.") + return + try: + os.kill(pid, signal.SIGUSR1) + except (ProcessLookupError, PermissionError, OSError) as exc: + print(f"warning: could not signal gateway pid {pid} ({exc}); " + "run `hermes gateway restart` manually from a separate " + "shell.") + return + # Wait for the restart to land: either the pid file shows a new + # live pid, or the old pid died (drain finished; the supervisor + # relaunches it — gateway.pid is rewritten then). A drain-aware + # SIGUSR1 restart keeps the old pid while turns finish, so also + # accept "old pid gone" as success-in-progress and keep polling + # briefly for the new pid. + deadline = time.monotonic() + RESTART_WAIT_S + old_gone_at = None + while time.monotonic() < deadline: + new_pid = _read_gateway_pid(home) + if new_pid and new_pid != pid and _pid_alive(new_pid): + print(f"Gateway restarted (new pid {new_pid}).") + return + if not _pid_alive(pid): + if old_gone_at is None: + old_gone_at = time.monotonic() + # Old daemon exited; give the supervisor a moment to + # write the new pid, then report success either way. + if time.monotonic() - old_gone_at > RESTART_DEFER_POLL_S: + print(f"Gateway pid {pid} exited after drain; the " + "supervisor is relaunching it (or the next " + "`hermes` launch will). Plugin is live on the " + "new daemon.") + return + time.sleep(0.5) + print(f"warning: gateway pid {pid} did not cycle within " + f"{RESTART_WAIT_S:.0f}s (a turn may be draining); check " + "`hermes gateway status`. The plugin file is updated on " + "disk — the new daemon picks it up on its next start.") + def hermes_home(args) -> pathlib.Path: if args.hermes_home: @@ -51,7 +159,8 @@ def hermes_home(args) -> pathlib.Path: return pathlib.Path.home() / ".hermes" -def install(home: pathlib.Path, dry_run: bool = False) -> int: +def install(home: pathlib.Path, dry_run: bool = False, + no_restart: bool = False) -> int: dest = home / "plugins" / PLUGIN_NAME if not PLUGIN_SRC.is_dir(): print(f"error: plugin source not found: {PLUGIN_SRC}", @@ -112,7 +221,12 @@ def install(home: pathlib.Path, dry_run: bool = False) -> int: print("warning: `hermes plugins enable` timed out; enable " "manually with:") print(f" hermes plugins enable {PLUGIN_NAME}") - print(f"\nDone. Restart Hermes, then every assistant turn ends with:") + # The gateway daemon caches the plugin module at first hook fire, + # so a running daemon keeps the OLD code until it is restarted. + # Restart it (SIGUSR1 drain-restart) unless the caller opted out. + if not no_restart: + _restart_gateway(home, dry_run) + print(f"\nDone. Every assistant turn now ends with:") print(" -- provider:model | session:$X.XX | turn:$Y.YY") print("Query any session with: agent-cost-estimator --harness hermes") return 0 @@ -146,11 +260,16 @@ def main(argv=None) -> int: parser.add_argument( "--dry-run", action="store_true", help="show what would change without writing") + parser.add_argument( + "--no-restart", action="store_true", + help="do not restart a running gateway daemon after install " + "(the daemon caches plugins; restart it later with " + "`hermes gateway restart`)") args = parser.parse_args(argv) home = hermes_home(args) if args.uninstall: return uninstall(home, dry_run=args.dry_run) - return install(home, dry_run=args.dry_run) + return install(home, dry_run=args.dry_run, no_restart=args.no_restart) if __name__ == "__main__": diff --git a/agent_cost_estimator/integration/hermes/plugin/plugin.yaml b/agent_cost_estimator/integration/hermes/plugin/plugin.yaml index b244b95..56ea2ec 100644 --- a/agent_cost_estimator/integration/hermes/plugin/plugin.yaml +++ b/agent_cost_estimator/integration/hermes/plugin/plugin.yaml @@ -1,5 +1,5 @@ name: agent-cost-estimator -version: 0.4.5 +version: 0.4.6 description: "Print an estimated cost line (provider:model | session:$X | turn:$Y) after every assistant turn, recorded from Hermes session usage." author: "nvaldez" provides_hooks: diff --git a/pyproject.toml b/pyproject.toml index 83c7208..619d36e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "agent-cost-estimator" -version = "0.4.5" +version = "0.4.6" description = "Report token usage and estimated USD cost of AI agent sessions from local harness logs" readme = "README.md" requires-python = ">=3.9" diff --git a/tests/test_agent_cost_estimator.py b/tests/test_agent_cost_estimator.py index ee60fab..400b639 100755 --- a/tests/test_agent_cost_estimator.py +++ b/tests/test_agent_cost_estimator.py @@ -1084,3 +1084,114 @@ def test_installer_uninstall(tmp_path, capsys): code = hinstall.main(["--hermes-home", str(home), "--uninstall"]) assert code == 0 assert not dest.exists() + + +def test_installer_restarts_running_gateway(tmp_path, monkeypatch, capsys): + """A running gateway daemon is SIGUSR1-restarted after install; + the installer waits for the new pid in gateway.pid.""" + import json as _json + home = make_hermes_home(tmp_path, sessions=[("s1", "t", "m", 1.0)]) + pid_file = home / "gateway.pid" + pid_file.write_text(_json.dumps({"pid": 4242})) + monkeypatch.setattr(hinstall, "_pid_alive", lambda pid: pid == 4242) + signals = [] + monkeypatch.setattr(hinstall.os, "kill", + lambda pid, sig: signals.append((pid, sig))) + + def fake_run(*args, **kwargs): + raise FileNotFoundError("no hermes") + monkeypatch.setattr(hinstall.subprocess, "run", fake_run) + + # After the signal, the supervisor writes a new pid. + def swap_pid(*args, **kwargs): + pid_file.write_text(_json.dumps({"pid": 5150})) + monkeypatch.setattr(hinstall, "_pid_alive", lambda pid: pid == 5150) + monkeypatch.setattr(hinstall.time, "sleep", lambda s: swap_pid()) + monkeypatch.setattr(hinstall, "RESTART_WAIT_S", 5.0) + + code = hinstall.main(["--hermes-home", str(home)]) + assert code == 0 + assert signals and signals[0][1] == hinstall.signal.SIGUSR1 + assert "restarted" in capsys.readouterr().out.lower() + + +def test_installer_no_restart_flag_skips_gateway(tmp_path, monkeypatch, capsys): + """--no-restart leaves a running gateway alone.""" + import json as _json + home = make_hermes_home(tmp_path, sessions=[("s1", "t", "m", 1.0)]) + (home / "gateway.pid").write_text(_json.dumps({"pid": 4242})) + monkeypatch.setattr(hinstall, "_pid_alive", lambda pid: True) + def boom(*args, **kwargs): + raise AssertionError("kill must not be called with --no-restart") + monkeypatch.setattr(hinstall.os, "kill", boom) + def fake_run(*args, **kwargs): + raise FileNotFoundError("no hermes") + monkeypatch.setattr(hinstall.subprocess, "run", fake_run) + code = hinstall.main(["--hermes-home", str(home), "--no-restart"]) + assert code == 0 + assert "Restarting" not in capsys.readouterr().out + + +def test_installer_stopped_gateway_needs_no_restart(tmp_path, monkeypatch, capsys): + """No live gateway pid -> no signal, just an informational note.""" + home = make_hermes_home(tmp_path, sessions=[("s1", "t", "m", 1.0)]) + monkeypatch.setattr(hinstall, "_pid_alive", lambda pid: False) + def boom(*args, **kwargs): + raise AssertionError("kill must not be called when gateway is stopped") + monkeypatch.setattr(hinstall.os, "kill", boom) + def fake_run(*args, **kwargs): + raise FileNotFoundError("no hermes") + monkeypatch.setattr(hinstall.subprocess, "run", fake_run) + code = hinstall.main(["--hermes-home", str(home)]) + assert code == 0 + out = capsys.readouterr().out + assert "no restart needed" in out + + +def test_cli_integrate_hermes_dispatches_to_installer(monkeypatch): + """`agent-cost-estimator --integrate hermes` runs the Hermes + installer (plugin copy + enable + gateway restart), not the + Vibe hook path.""" + from agent_cost_estimator import cli as cli_mod + called = {} + def fake_install(argv): + called["argv"] = argv + return 0 + monkeypatch.setattr( + "agent_cost_estimator.integration.hermes.install.main", fake_install) + assert cli_mod.integrate_hooks("hermes") == 0 + assert called.get("argv") == [] + assert cli_mod.integrate_hooks("hermes-agent") == 0 + assert cli_mod.remove_integrate_hooks("hermes") == 0 + assert called.get("argv") == ["--uninstall"] + + +def test_installer_restart_accepts_old_pid_exit(tmp_path, monkeypatch, capsys): + """Drain-aware restart: if the old daemon exits (turns drained), + the installer reports success without waiting for a new pid.""" + import json as _json + home = make_hermes_home(tmp_path, sessions=[("s1", "t", "m", 1.0)]) + pid_file = home / "gateway.pid" + pid_file.write_text(_json.dumps({"pid": 4242})) + alive_pids = {4242} + monkeypatch.setattr(hinstall, "_pid_alive", lambda pid: pid in alive_pids) + monkeypatch.setattr(hinstall.os, "kill", lambda pid, sig: None) + ticks = {"n": 0} + now = {"t": 1000.0} + def fake_monotonic(): + return now["t"] + def fake_sleep(s): + now["t"] += s + ticks["n"] += 1 + if ticks["n"] == 3: + alive_pids.clear() + monkeypatch.setattr(hinstall.time, "sleep", fake_sleep) + monkeypatch.setattr(hinstall.time, "monotonic", fake_monotonic) + monkeypatch.setattr(hinstall, "RESTART_WAIT_S", 30.0) + monkeypatch.setattr(hinstall, "RESTART_DEFER_POLL_S", 1.0) + def fake_run(*args, **kwargs): + raise FileNotFoundError("no hermes") + monkeypatch.setattr(hinstall.subprocess, "run", fake_run) + code = hinstall.main(["--hermes-home", str(home)]) + assert code == 0 + assert "exited after drain" in capsys.readouterr().out