Skip to content

The agent that grows with you

fix(local-runtime): leave a live owner's server to its existing path; keep legacy owner check

emozilla82d22408411a
diff --git a/hermes_cli/local_runtime/bootstrap.py b/hermes_cli/local_runtime/bootstrap.py
index 30d1c5643e6..39390fe3018 100644
--- a/hermes_cli/local_runtime/bootstrap.py
+++ b/hermes_cli/local_runtime/bootstrap.py
@@ -154,11 +154,11 @@ def _presets_stale() -> bool:
return False
-def _stop_state_server() -> bool:
- """Stop the server the state file points at so this process can boot a replacement; False when
- it must be left alone. Only an orphan (recorded owner gone) is stopped: a live owner's watchdog
- would respawn its router beside ours. The process comes from the identity-guarded reader, never
- a bare PID — the endpoint dict callers hold carries no process identity."""
+def _stop_state_server() -> None:
+ """Stop the server the state file points at before this process boots a replacement, but only
+ an orphan (recorded owner gone): a live owner's watchdog would respawn its router, so that one
+ is left running and the replacement boots beside it, as it always has. The process comes from
+ the identity-guarded reader, never a bare PID — the endpoint dict callers hold has none."""
from hermes_cli.local_runtime.recovery import (
_owner_is_dead,
is_modern,
@@ -172,18 +172,17 @@ def _stop_state_server() -> bool:
if is_modern(state):
if not _owner_is_dead(state):
logger.info("llama-server pid=%s belongs to a live Hermes process; leaving it", state.get("pid"))
- return False
+ return
proc = recorded_process(state)
else:
proc = legacy_recorded_process(state)
if proc is None:
- return True
+ return
try:
# Waits for the tree to exit, so the port and the GPU are free for the replacement.
LlamaServerSupervisor._terminate_tree(proc, verified_root=True)
except Exception as exc: # noqa: BLE001 - a failed stop must not block the replacement boot
logger.warning("could not stop the incumbent llama-server (pid=%s): %s", proc.pid, exc)
- return True
def refresh_local_runtime() -> bool:
@@ -200,8 +199,7 @@ def refresh_local_runtime() -> bool:
state = _state_endpoint()
if state is None:
return False
- if not _stop_state_server():
- return False
+ _stop_state_server()
else:
shutdown_local_runtime()
return ensure_local_runtime(load_config(), force=True) is not None
@@ -441,8 +439,7 @@ def ensure_local_runtime(config: dict, force: bool = False) -> "object | None":
return None
logger.info("running server's presets predate the staged models; "
"replacing it so every model launches with a policy")
- if not _stop_state_server():
- return None
+ _stop_state_server()
try:
from hermes_cli.local_runtime.binaries import installed_engine
diff --git a/hermes_cli/local_runtime/recovery.py b/hermes_cli/local_runtime/recovery.py
index c4a08b94357..f7a481be173 100644
--- a/hermes_cli/local_runtime/recovery.py
+++ b/hermes_cli/local_runtime/recovery.py
@@ -86,8 +86,13 @@ def _owner_is_dead(state: dict) -> bool:
return False
try:
owner = psutil.Process(pid)
- # A newer incarnation proves the recorded owner has exited.
- return not owner.is_running() or not _same_birth(owner, state, "owner_")
+ if not owner.is_running():
+ return True
+ if _valid_birth(state.get("owner_start_time")):
+ return not _same_birth(owner, state, "owner_")
+ # Records that predate start_time: only a NEWER incarnation proves the owner exited. A
+ # stepped clock (WSL) moves create_time, so inequality alone would bury a live owner.
+ return owner.create_time() > created
except psutil.NoSuchProcess:
return True
except (KeyError, TypeError, ValueError, OverflowError, OSError, psutil.Error):
diff --git a/tests/hermes_cli/test_local_runtime_recovery.py b/tests/hermes_cli/test_local_runtime_recovery.py
index 0c10309764d..80836fa6283 100644
--- a/tests/hermes_cli/test_local_runtime_recovery.py
+++ b/tests/hermes_cli/test_local_runtime_recovery.py
@@ -421,8 +421,8 @@ def test_clock_step_keeps_the_recorded_router(tmp_path, monkeypatch):
@pytest.mark.parametrize("owner", ["exited", "alive"])
def test_replacement_stops_only_an_orphaned_router(tmp_path, monkeypatch, owner):
"""Stale-preset replacement and the rescan bounce stop the recorded router only when its owner
- is gone: a live owner's watchdog would respawn its router next to the replacement. (POSIX: a
- Windows router dies with its owner's job object, so no orphan exists to stop.)"""
+ is gone; a live owner's router keeps running (its watchdog would respawn it anyway). POSIX: a
+ Windows router dies with its owner's job object, so no orphan exists to stop."""
from gateway.status import get_process_start_time
from hermes_cli.local_runtime import bootstrap, endpoint, supervisor
@@ -441,10 +441,9 @@ def test_replacement_stops_only_an_orphaned_router(tmp_path, monkeypatch, owner)
holder.kill()
holder.wait(timeout=5)
- stopped = bootstrap._stop_state_server()
+ bootstrap._stop_state_server()
- assert stopped == (owner == "exited")
- if stopped:
+ if owner == "exited":
router.wait(timeout=10)
assert endpoint._state_endpoint() is None
else:
@@ -456,6 +455,23 @@ def test_replacement_stops_only_an_orphaned_router(tmp_path, monkeypatch, owner)
proc.wait(timeout=5)
+def test_a_stepped_clock_never_buries_a_live_owner_of_an_older_record(monkeypatch):
+ """Records written before start_time compare create_time, which moves with a stepped clock
+ (WSL). Only a newer incarnation proves the owner exited; a live owner must never read as
+ dead, or a newer process would stop the server an older one is still using."""
+ import psutil._pslinux
+ from hermes_cli.local_runtime import recovery
+
+ me = psutil.Process()
+ state = {"owner_pid": me.pid, "owner_create_time": me.create_time()}
+ stepped = psutil._pslinux.boot_time() - 22
+ monkeypatch.setattr(psutil._pslinux, "boot_time", lambda: stepped)
+ assert psutil.Process(me.pid).create_time() != state["owner_create_time"]
+
+ assert recovery._owner_is_dead(state) is False
+
+
def test_the_process_that_boots_the_server_stops_it_on_a_clean_exit(tmp_path):
"""Every surface that boots the managed server (CLI, gateway, desktop backend) frees it when it
exits; before, only the desktop backend's shutdown handler did and every other owner orphaned