Skip to content

The agent that grows with you

fix(local_runtime): keep the idle clock across failed telemetry probes

Konstantin Khlopkovb83ee209d935
diff --git a/hermes_cli/local_runtime/supervisor.py b/hermes_cli/local_runtime/supervisor.py
index 0160ebef840..768bc8f058c 100644
--- a/hermes_cli/local_runtime/supervisor.py
+++ b/hermes_cli/local_runtime/supervisor.py
@@ -375,7 +375,9 @@ class LlamaServerSupervisor:
def sweep_idle(self, now: float | None = None) -> list[str]:
"""Unload models idle past IDLE_UNLOAD_S; returns their ids. Idle = no busy slots and no
- queued work, tracked per model across calls; a model seen busy resets its clock."""
+ queued work, tracked per model across calls; a model seen busy resets its clock. A
+ failed telemetry probe is neither idle nor busy: the clock is kept, so a flaky probe
+ cannot pin a resident model (and its VRAM) indefinitely."""
now = time.monotonic() if now is None else now
unloaded: list[str] = []
try:
@@ -383,7 +385,19 @@ class LlamaServerSupervisor:
except Exception: # noqa: BLE001
return unloaded
for model_id, status in statuses.items():
- if status not in _RESIDENT or not self.is_idle(model_id):
+ if status not in _RESIDENT:
+ self._idle_since.pop(model_id, None)
+ continue
+ probe = self._probe_idle(model_id)
+ if probe is None:
+ idle_for = int(now - self._idle_since.get(model_id, now))
+ logger.info(
+ "idle probe for %s failed; keeping idle clock (idle %ds)",
+ model_id,
+ idle_for,
+ )
+ continue
+ if probe is False:
self._idle_since.pop(model_id, None)
continue
first_idle = self._idle_since.setdefault(model_id, now)
@@ -427,6 +441,12 @@ class LlamaServerSupervisor:
"""No processing requests and no busy slots. Router quirk: /slots and /metrics are
per-child and require ?model= (bare calls 400). With ``model_id`` checks that one child;
without, every loaded child."""
+ return self._probe_idle(model_id) is True
+
+ def _probe_idle(self, model_id: str | None = None) -> bool | None:
+ """Tri-state idle probe for the sweeper: True = confirmed idle, False = confirmed
+ busy, None = the probe itself failed. The sweeper must never mistake a dead probe
+ for activity — that resets the idle clock and pins the model's VRAM."""
try:
loaded = ([model_id] if model_id is not None
else [m for m, status in self.models().items() if status in _RESIDENT])
@@ -442,4 +462,4 @@ class LlamaServerSupervisor:
return False
return True
except Exception: # noqa: BLE001
- return False
+ return None
diff --git a/tests/hermes_cli/test_local_runtime.py b/tests/hermes_cli/test_local_runtime.py
index 6053305ffd9..71065b8b98e 100644
--- a/tests/hermes_cli/test_local_runtime.py
+++ b/tests/hermes_cli/test_local_runtime.py
@@ -38,6 +38,8 @@ class _StubHandler(BaseHTTPRequestHandler):
chat_answer = "Paris"
requests_processing = 0
slots: list = []
+ slots_error = 0
+ metrics_error = 0
def _send(self, code: int, body: dict | str | None = None) -> None:
raw = (json.dumps(body) if isinstance(body, dict) else (body or "")).encode()
@@ -62,8 +64,16 @@ class _StubHandler(BaseHTTPRequestHandler):
else:
self._send(200, self.models)
elif path == "/metrics":
- self._send(200, f"llamacpp:requests_processing {self.requests_processing}\n")
+ if self.metrics_error:
+ self._send(self.metrics_error, {})
+ else:
+ self._send(
+ 200, f"llamacpp:requests_processing {self.requests_processing}\n"
+ )
elif path == "/slots":
+ if self.slots_error:
+ self._send(self.slots_error, {})
+ return
raw = json.dumps(self.slots).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
@@ -586,6 +596,68 @@ def test_idle_sweep_busy_model_resets_clock(tmp_path, monkeypatch, stub_server):
assert handler.unloaded == []
+def test_idle_sweep_probe_failure_keeps_clock(tmp_path, monkeypatch, stub_server):
+ """A failed telemetry probe is not activity: /slots or /metrics errors must keep the
+ idle clock instead of resetting it, so one flaky probe per sweep can't pin a resident
+ model (and its VRAM) for hours."""
+ port, handler = stub_server
+ handler.models = {"data": [{"id": "stuck-m", "status": {"value": "loaded"}}]}
+ handler.slots = []
+ handler.unloaded = []
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ from hermes_cli.local_runtime.supervisor import LlamaServerSupervisor
+
+ sup = LlamaServerSupervisor(tmp_path / "i", tmp_path / "m", port=port)
+
+ t0 = 1000.0
+ assert sup.sweep_idle(now=t0) == [] # clock starts
+ handler.metrics_error = 500 # probe fails mid-window
+ assert sup.sweep_idle(now=t0 + sup.IDLE_UNLOAD_S + 1) == [] # kept, not reset
+ assert handler.unloaded == []
+ handler.metrics_error = 0 # telemetry recovers
+ # The clock survived the failures: unload happens at the first healthy sweep.
+ assert sup.sweep_idle(now=t0 + sup.IDLE_UNLOAD_S + 2) == ["stuck-m"]
+ assert handler.unloaded == ["stuck-m"]
+
+
+def test_idle_sweep_busy_after_probe_failure_still_resets_clock(
+ tmp_path, monkeypatch, stub_server
+):
+ """A kept clock must not mask real activity: a confirmed busy slot still resets it."""
+ port, handler = stub_server
+ handler.models = {"data": [{"id": "m", "status": {"value": "loaded"}}]}
+ handler.unloaded = []
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
+ from hermes_cli.local_runtime.supervisor import LlamaServerSupervisor
+
+ sup = LlamaServerSupervisor(tmp_path / "i", tmp_path / "m", port=port)
+
+ t0 = 1000.0
+ assert sup.sweep_idle(now=t0) == [] # clock starts
+ handler.slots_error = 500 # probe fails: clock kept
+ assert sup.sweep_idle(now=t0 + 100) == []
+ handler.slots_error = 0
+ handler.slots = [{"is_processing": True}] # confirmed busy: clock resets
+ assert sup.sweep_idle(now=t0 + sup.IDLE_UNLOAD_S + 5) == []
+ handler.slots = []
+ # Fresh clock since the busy sighting — not the original t0 one.
+ assert sup.sweep_idle(now=t0 + sup.IDLE_UNLOAD_S + 6) == []
+ assert handler.unloaded == []
+
+
+def test_is_idle_probe_failure_reports_not_idle(stub_server, tmp_path):
+ """The public bool contract is unchanged: a failed probe reads as "not idle" —
+ callers outside the sweep never unload on a telemetry hiccup."""
+ port, handler = stub_server
+ sup = _make_supervisor(tmp_path, port)
+ handler.models = {"data": [{"id": "m", "status": {"value": "loaded"}}]}
+ handler.slots = []
+ handler.metrics_error = 503
+ assert sup.is_idle() is False
+ handler.metrics_error = 0
+ assert sup.is_idle() is True
+
+
def test_staged_models_requires_every_split_part(tmp_path, monkeypatch):
"""A split GGUF mid-download must NOT count as staged: the picker, the
catalog's 'downloaded' flag, and the router's model list all read