Skip to content

The agent that grows with you

fix(local-runtime): resolution kicks the managed boot when the router is dead and nobody is booting it

emozillad4865c6dbeb2
diff --git a/hermes_cli/local_runtime/endpoint.py b/hermes_cli/local_runtime/endpoint.py
index dc266f52613..b80a663d031 100644
--- a/hermes_cli/local_runtime/endpoint.py
+++ b/hermes_cli/local_runtime/endpoint.py
@@ -14,12 +14,16 @@ custom-provider path and its own error reporting.
from __future__ import annotations
import json
+import logging
+import threading
import time
import urllib.error
import urllib.request
LLAMACPP_ALIASES = frozenset({"llamacpp", "llama.cpp", "llama-cpp"})
+logger = logging.getLogger(__name__)
+
def _pid_alive(pid: int) -> bool:
"""Liveness for the state file's supervisor-child pid.
@@ -109,6 +113,7 @@ def resolve_llamacpp_endpoint(config: dict | None = None,
return {"base_url": hit.base_url, "api_key": ""}
if wait_for_boot_s > 0 and _boot_in_flight(config):
+ _kick_managed_boot(config)
deadline = time.monotonic() + wait_for_boot_s
while time.monotonic() < deadline:
time.sleep(0.25)
@@ -118,6 +123,45 @@ def resolve_llamacpp_endpoint(config: dict | None = None,
return None
+_KICK_LOCK = threading.Lock()
+
+
+def _kick_managed_boot(config: dict | None) -> None:
+ """Actively start the managed server when resolution finds it missing.
+
+ The wait loop above assumes some OTHER thread is bringing the server
+ up — true only at backend start (the lifespan boot thread). A router
+ that dies LATER leaves no boot in flight: the backend process was
+ killed with the router as part of its tree, or another install took
+ the stable port and the ownership guard rightly refused it. In those
+ states the wait just expired and agent init failed with 'no provider
+ configured', even though the fix is the same idempotent ensure call
+ the lifespan makes. Kick it here, off-thread (the resolver's wait
+ stays bounded; ensure's own state checks make a concurrent lifespan
+ boot harmless) and non-reentrant (racing resolutions kick once).
+ """
+ if not _KICK_LOCK.acquire(blocking=False):
+ return # a kick is already in flight
+
+ def _boot() -> None:
+ try:
+ cfg = config
+ if cfg is None:
+ from hermes_cli.config import load_config
+
+ cfg = load_config()
+ from hermes_cli.local_runtime.bootstrap import ensure_local_runtime
+
+ ensure_local_runtime(cfg)
+ except Exception: # noqa: BLE001 — best-effort; resolution falls back
+ logger.warning("on-demand managed-server boot failed", exc_info=True)
+ finally:
+ _KICK_LOCK.release()
+
+ threading.Thread(target=_boot, daemon=True,
+ name="lr-on-demand-boot").start()
+
+
def _boot_in_flight(config: dict | None) -> bool:
"""True when the managed runtime is enabled and installed — the state
a lifespan boot thread is (or is about to be) bringing up.
diff --git a/tests/hermes_cli/test_local_runtime.py b/tests/hermes_cli/test_local_runtime.py
index 316ca9f4507..82a6a271974 100644
--- a/tests/hermes_cli/test_local_runtime.py
+++ b/tests/hermes_cli/test_local_runtime.py
@@ -456,11 +456,45 @@ def test_llamacpp_endpoint_waits_for_boot_in_flight(tmp_path, monkeypatch):
assert resolved["api_key"] == "sk-boot"
+def test_resolution_kicks_boot_when_no_thread_is_booting(tmp_path, monkeypatch):
+ """The dead-router-mid-flight case: runtime enabled+installed, but no
+ state file and NO lifespan boot thread running (the router died after
+ backend start — tree-killed with a stale backend, or the stable port
+ was owned by another install and the ownership guard refused it).
+ Resolution must not just wait for a boot that nobody is doing — it
+ kicks ensure_local_runtime itself and picks up the state file that
+ boot writes."""
+ import time as _time
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ from hermes_cli.local_runtime import bootstrap as bs
+ from hermes_cli.local_runtime import endpoint as ep
+ from hermes_cli.local_runtime.supervisor import state_path
+
+ monkeypatch.setattr(ep, "_boot_in_flight", lambda config: True)
+ monkeypatch.setattr(ep, "_pid_alive", lambda pid: True)
+ monkeypatch.setattr("hermes_cli.local_runtime.detect.DEFAULT_PROBE_PORTS", ())
+
+ def _fake_ensure(config, force=False):
+ _time.sleep(0.3) # a real spawn takes a moment
+ state_path().parent.mkdir(parents=True, exist_ok=True)
+ state_path().write_text(json.dumps({
+ "base_url": "http://127.0.0.1:59998/v1",
+ "api_key": "sk-kicked", "pid": 778,
+ }), encoding="utf-8")
+
+ monkeypatch.setattr(bs, "ensure_local_runtime", _fake_ensure)
+
+ resolved = ep.resolve_llamacpp_endpoint(config={}, wait_for_boot_s=5.0)
+ assert resolved is not None
+ assert resolved["api_key"] == "sk-kicked"
+
+
def test_boot_in_flight_real_gate(tmp_path, monkeypatch):
"""_boot_in_flight exercised FOR REAL (the previous regression test
monkeypatched it — and the real one threw TypeError on every call,
- silently disabling the boot wait; Jeff's third onboarding bounce).
- Enabled + verified manifest on disk -> True; either missing -> False."""
+ silently disabling the boot wait). Enabled + verified manifest on
+ disk -> True; either missing -> False."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
from hermes_cli.local_runtime import endpoint as ep
from hermes_cli.local_runtime.binaries import runtimes_root