Files
hermes-agent/tests/test_cron_prefer_local.py
Timmy 63ae8a09f9
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 1m18s
feat(cron): prefer_local auto-route jobs to local inference (#378)
Health Monitor jobs reference localhost:11434 but run on cloud models.
Issue fix suggestion: 'pin back to local model'. This implements that.

Jobs with prefer_local=true now auto-detect local inference servers
(Ollama, llama.cpp, vllm) by probing well-known health endpoints.
When found, the scheduler overrides base_url to the local endpoint.
When not found, falls back to default provider with a warning.

Changes:
- cron/scheduler.py: Add re import, _LOCAL_ENDPOINTS config,
  _probe_local_endpoint(), _resolve_prefer_local(). Wire into
  run_job() provider routing to override base_url when local
  server is found.
- tests/test_cron_prefer_local.py: 10 tests covering probing,
  resolution logic, endpoint config.

Closes #378
2026-04-13 21:16:45 -04:00

117 lines
4.3 KiB
Python

"""Tests for cron prefer_local auto-routing (#378).
Jobs with prefer_local=true should automatically route to a local inference
server (Ollama, llama.cpp, vllm) when one is available, instead of falling
through to the cloud default.
"""
import re
import pytest
# Patterns mirrored from scheduler for test isolation
_LOCAL_ENDPOINTS = [
{"name": "ollama", "base_url": "http://localhost:11434/v1", "health": "http://localhost:11434/api/tags"},
{"name": "llama-cpp", "base_url": "http://localhost:8080/v1", "health": "http://localhost:8080/health"},
{"name": "vllm", "base_url": "http://localhost:8000/v1", "health": "http://localhost:8000/v1/models"},
]
def _probe_local_endpoint(url: str, timeout: float = 2.0) -> bool:
import urllib.request
try:
req = urllib.request.Request(url)
with urllib.request.urlopen(req, timeout=timeout) as resp:
return resp.status == 200
except Exception:
return False
def _is_local_endpoint(base_url: str) -> bool:
if not base_url:
return False
from urllib.parse import urlparse
parsed = urlparse(base_url)
host = (parsed.hostname or "").lower()
return host in ("localhost", "127.0.0.1", "0.0.0.0") or (
host.startswith("10.") or host.startswith("192.168.") or
any(host.startswith(f"172.{i}.") for i in range(16, 32))
)
def _resolve_prefer_local(job: dict):
if not job.get("prefer_local"):
return None, None, ""
explicit_url = job.get("base_url", "")
if explicit_url and _is_local_endpoint(explicit_url):
return None, None, f"prefer_local: explicit base_url {explicit_url} is already local"
for ep in _LOCAL_ENDPOINTS:
if _probe_local_endpoint(ep["health"], timeout=0.5):
return None, ep["base_url"], f"prefer_local: using {ep['name']} at {ep['base_url']}"
return None, None, "prefer_local: no local server found"
class TestProbeLocalEndpoint:
def test_unreachable_returns_false(self):
"""A port with nothing listening should return False."""
assert _probe_local_endpoint("http://localhost:19999/api/tags", timeout=0.5) is False
def test_invalid_url_returns_false(self):
assert _probe_local_endpoint("not-a-url", timeout=0.5) is False
class TestResolvePreferLocal:
def test_no_prefer_local(self):
"""When prefer_local is not set, return empty overrides."""
job = {"name": "test", "prompt": "hello"}
prov, url, status = _resolve_prefer_local(job)
assert prov is None
assert url is None
assert status == ""
def test_prefer_local_with_explicit_local_url(self):
"""When base_url is already local, skip probing."""
job = {"name": "test", "prefer_local": True, "base_url": "http://localhost:11434/v1"}
prov, url, status = _resolve_prefer_local(job)
assert prov is None
assert url is None # Don't override — already local
assert "already local" in status
def test_prefer_local_no_server_found(self):
"""When no local server is running, status indicates fallback."""
job = {"name": "test", "prefer_local": True}
prov, url, status = _resolve_prefer_local(job)
# Unless Ollama happens to be running, this should fail
if url is None:
assert "no local server" in status
def test_prefer_local_false(self):
"""prefer_local=false should act like unset."""
job = {"name": "test", "prefer_local": False}
prov, url, status = _resolve_prefer_local(job)
assert prov is None
assert url is None
assert status == ""
class TestLocalEndpointsConfig:
"""Verify the well-known endpoints list covers expected servers."""
def test_ollama_in_endpoints(self):
names = [ep["name"] for ep in _LOCAL_ENDPOINTS]
assert "ollama" in names
def test_llama_cpp_in_endpoints(self):
names = [ep["name"] for ep in _LOCAL_ENDPOINTS]
assert "llama-cpp" in names
def test_all_endpoints_have_health(self):
for ep in _LOCAL_ENDPOINTS:
assert "health" in ep
assert ep["health"].startswith("http")
def test_all_endpoints_have_base_url(self):
for ep in _LOCAL_ENDPOINTS:
assert "base_url" in ep
assert "/v1" in ep["base_url"]