Files
hermes-agent/hermes_cli/local_runtime/endpoint.py
emozilla 4bc2efbeb3 fix(local): keep long local-model turns alive on laptops
A press reviewer ran Qwen 27B on a 64 GB RTX Spark and saw single queries take hours. The
causes, and what each part of this change does about them:

- llama-server reads a long prompt for minutes before the first token. Local requests now
  always stream and ask for prompt-reading progress, so the stall timers see the server
  working and the router notices a hang-up within seconds. They no longer fall back to a
  non-streamed request, which the router can't cancel. The status line shows how much of
  the prompt the server has read.
- A laptop that sleeps mid-request froze the request, and the stall timer cut it on wake.
  Stall timers for a server on the same machine now skip time the process was stopped
  (sleep or Modern Standby). Background-call watchdogs run on the awake clock.
- After repeated stalls the breaker failed every later message at once, even after the local
  server recovered. At turn start, a local server that answers its health check gets one more
  attempt; one more stall shuts the breaker again.
- On Windows unified memory, the GPU carve-out is invisible to Windows, and the model plus
  llama-server's default 8 GiB prompt cache overran commit. Free commit now narrows the launch
  window (never refusing a model the pool can hold), caps how many models the router holds at
  once, and sizes the prompt cache, split across those models.
- Hermes's status reads named the model, and the router loads any model a request names, so
  Eject and the idle unload reloaded the model at once. Status reads pass autoload=false; an
  unloaded model counts as idle, and the context probe reads the router's launch args.
- The smart-approval verdict has a 16-token budget, and a thinking model spent all of it
  thinking (45 of 45 verdicts came back empty). The verdict asks for no thinking unless the
  operator set auxiliary.approval.reasoning_effort.
- Compression's 600 s limit counted the time the server spent reading a 150K-token
  conversation, which alone can exceed it on a laptop GPU. Prompt reading now pushes the fence
  deadline back (CompressionCommitFence.extend_deadline), only while the server reports newly read
  tokens, and never past a fence the host closed or one a commit holds. The agent and gateway
  hygiene waits read the moved deadline after every slice.
- Desktop keep-awake defaults to "while working". When a timed sleep interrupts a turn with
  keep-awake off, the next wake offers it once per computer; the offer survives the next prompt.

(cherry picked from commit 518ce272b9)
2026-10-16 15:21:07 -04:00

162 lines
5.7 KiB
Python

"""Endpoint resolution for llamacpp-alias requests (provider integration).
``provider: llamacpp`` with no explicit base_url resolves, in order, to the managed server (state
file), a detected external llama-server, or — during a backend boot race — the managed server once
its state file appears.
"""
from __future__ import annotations
from contextlib import suppress
import json
import logging
import threading
import time
import urllib.request
LLAMACPP_ALIASES = frozenset({"llamacpp", "llama.cpp", "llama-cpp"})
def child_query(model_id: str) -> str:
"""Query string for a status read of one router child (``/props``, ``/slots``, ``/metrics``).
The router loads an unloaded model for any request that names it (``--models-autoload``), so a
status read right after an Eject or an idle unload would load the model straight back.
``autoload=false`` makes the router answer 400 "model is not loaded" instead.
"""
from urllib.parse import quote
return f"model={quote(model_id)}&autoload=false"
logger = logging.getLogger(__name__)
def _pid_alive(pid: int) -> bool:
"""Liveness for the state file's supervisor-child pid: psutil when available, else True
(optimistic). On Windows ``os.kill(pid, 0)`` TERMINATES the process — never use it as a probe."""
if not pid or pid < 0:
return False
with suppress(Exception):
import psutil # type: ignore
return psutil.pid_exists(pid)
return True
def _state_endpoint() -> dict | None:
from hermes_cli.local_runtime.recovery import (
is_modern,
legacy_recorded_process,
read_state,
recorded_process,
)
state = read_state()
base_url = state.get("base_url", "")
if not isinstance(base_url, str) or not base_url:
return None
if is_modern(state):
if recorded_process(state) is None:
return None
else:
if legacy_recorded_process(state) is None:
return None
return {"base_url": base_url, "api_key": state.get("api_key", "")}
def managed_root() -> tuple[str, str] | None:
"""(base_root, api_key) of the managed router, or None. Resolved through the
ownership-guarded reader, not a raw state-file read: on the shared stable port a foreign
install's server answers /health for anyone, and a raw read would attach callers to someone
else's server."""
with suppress(Exception):
state = _state_endpoint()
if state is None:
return None
base = str(state.get("base_url", "")).rsplit("/v1", 1)[0]
return (base, str(state.get("api_key", ""))) if base else None
return None
def managed_get_json(base: str, api_key: str, route: str, timeout_s: float) -> object:
"""Authenticated GET against the managed router; raises on any transport/decode failure."""
req = urllib.request.Request(f"{base}{route}",
headers={"Authorization": f"Bearer {api_key}"})
with urllib.request.urlopen(req, timeout=timeout_s) as r:
return json.loads(r.read())
def resolve_llamacpp_endpoint(config: dict | None = None,
wait_for_boot_s: float = 8.0) -> dict | None:
"""Managed-first, detection-second endpoint for llamacpp aliases.
Boot-race rung: on a fresh backend start there is NO state file yet — the lifespan boot thread
is still spawning the server (≈1-3 s) while the desktop's readiness probe fires the moment the
WebSocket connects.
"""
managed = _state_endpoint()
if managed:
return managed
from hermes_cli.local_runtime.detect import detect_server
ports = ((config or {}).get("local_runtime") or {}).get("detect_ports") or []
hit = detect_server(extra_ports=tuple(int(p) for p in ports))
if hit and not hit.auth_required:
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)
managed = _state_endpoint()
if managed:
return managed
return None
_KICK_LOCK = threading.Lock()
def _load_config_if_none(config: dict | None) -> dict | None:
if config is not None:
return config
from hermes_cli.config import load_config
return load_config()
def _kick_managed_boot(config: dict | None) -> None:
"""Actively start the managed server when resolution finds it missing — the wait loop assumes
some OTHER thread is bringing it up, which is true only at backend start."""
if not _KICK_LOCK.acquire(blocking=False):
return # a kick is already in flight
def _boot() -> None:
try:
from hermes_cli.local_runtime.bootstrap import ensure_local_runtime
ensure_local_runtime(_load_config_if_none(config))
except Exception:
logger.warning("on-demand managed-server boot failed", exc_info=True)
finally:
_KICK_LOCK.release()
from agent.memory_provider import spawn_context_thread
# The caller's profile scope: ``config=None`` resolves (and the boot prices) under it.
spawn_context_thread(_boot, name="lr-on-demand-boot").start()
def _boot_in_flight(config: dict | None) -> bool:
"""True when the managed runtime is enabled and PM holds an installed engine."""
with suppress(Exception):
section = (_load_config_if_none(config) or {}).get("local_runtime") or {}
if not section.get("enabled"):
return False
from hermes_cli.local_runtime.binaries import installed_engine
return installed_engine(section.get("backend") or "auto") is not None
return False