hermes-agent/tests/agent/test_local_probe_disk_cache.py
teknium1 d7a4065568 perf(local-endpoints): disk L2 for server-type + ollama ctx probes, faster timeouts
Local-model users paid a fresh probe waterfall on EVERY CLI cold start
inside AIAgent.__init__: detect_local_server_type (up to 4 HTTP GETs,
2s timeout each on a hung server) + /api/show (3s timeout). The
existing caches were in-process only, so back-to-back invocations
(chat -q, cron ticks, subagents) re-paid the network every time.

- New 300s-TTL disk L2 at HERMES_HOME/cache/local_endpoint_probes.json
  for detect_local_server_type verdicts and query_ollama_num_ctx
  results. Only SUCCESSFUL probes persist (a down server never pins a
  negative verdict); stale entries pruned on write; corrupted cache
  degrades to a miss; atomic writes. 300s is strictly fresher than the
  1h in-process TTL that already accepts server-swap staleness.
- models.dev fetch timeout 15 -> (5, 10) connect/read tuple: a
  blackholed connect stalled the first-turn critical path 15s; now
  fails in 5s (matches the OpenRouter fetch convention, #46620).
- _auto_detect_local_model timeout 5 -> (2, 3): runs inside
  _get_model_config() at startup against a LOCAL endpoint; a hung local
  server cost 5s before the banner.

E2E (real HTTP server, two fresh subprocesses, isolated HERMES_HOME):
proc1 = 2 HTTP hits, proc2 = 0 HTTP hits, identical results
(ollama/131072), probe wall 74.5 -> 35.5 ms. 222 targeted tests green
incl. 9 new disk-L2 contract tests.
2026-07-29 10:52:13 -07:00

132 lines
5.2 KiB
Python

"""Tests for the local-endpoint probe disk L2 cache in agent/model_metadata.
Contract:
- only SUCCESSFUL probes are persisted (failures never pin a verdict);
- fresh disk entries serve cross-process (in-proc caches cleared);
- entries expire after _LOCAL_PROBE_DISK_TTL_SECONDS;
- corrupted cache files degrade to a miss;
- detect_local_server_type and query_ollama_num_ctx both use it.
"""
import json
import time
from unittest.mock import MagicMock, patch
import agent.model_metadata as MM
def _clear_in_proc():
MM._endpoint_probe_path_cache.clear()
MM._LOCAL_CTX_PROBE_CACHE.clear()
def _cache_file():
return MM._local_probe_disk_cache_path()
class TestDiskHelpers:
def test_put_get_roundtrip(self):
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:11434") == "ollama"
def test_expired_entry_is_miss(self):
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
path = _cache_file()
data = json.loads(path.read_text(encoding="utf-8"))
for entry in data.values():
entry["ts"] = time.time() - MM._LOCAL_PROBE_DISK_TTL_SECONDS - 1
path.write_text(json.dumps(data), encoding="utf-8")
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:11434") is None
def test_corrupted_file_is_miss(self):
path = _cache_file()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("not json{{{", encoding="utf-8")
assert MM._local_probe_disk_get("server_type", "anything") is None
# And put() recovers by rewriting cleanly
MM._local_probe_disk_put("server_type", "k", "vllm")
assert MM._local_probe_disk_get("server_type", "k") == "vllm"
def test_stale_entries_pruned_on_put(self):
MM._local_probe_disk_put("server_type", "old", "ollama")
path = _cache_file()
data = json.loads(path.read_text(encoding="utf-8"))
for entry in data.values():
entry["ts"] = time.time() - MM._LOCAL_PROBE_DISK_TTL_SECONDS - 1
path.write_text(json.dumps(data), encoding="utf-8")
MM._local_probe_disk_put("server_type", "new", "vllm")
data = json.loads(path.read_text(encoding="utf-8"))
assert "server_type:old" not in data
assert "server_type:new" in data
class TestDetectLocalServerTypeDiskL2:
def test_disk_hit_skips_http_entirely(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:9999", "ollama")
with patch("httpx.Client") as mock_client:
result = MM.detect_local_server_type("http://127.0.0.1:9999/v1")
assert result == "ollama"
mock_client.assert_not_called()
def test_successful_probe_persists_to_disk(self):
_clear_in_proc()
ok = MagicMock(status_code=200)
ok.json.return_value = {"models": []}
notfound = MagicMock(status_code=404, text="")
def route(url, *a, **k):
return ok if url.endswith("/api/tags") else notfound
client = MagicMock()
client.get.side_effect = route
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
result = MM.detect_local_server_type("http://127.0.0.1:8888/v1")
assert result == "ollama"
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:8888") == "ollama"
def test_failed_probe_not_persisted(self):
_clear_in_proc()
client = MagicMock()
client.get.side_effect = Exception("connection refused")
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
result = MM.detect_local_server_type("http://127.0.0.1:7777/v1")
assert result is None
assert MM._local_probe_disk_get("server_type", "http://127.0.0.1:7777") is None
class TestOllamaNumCtxDiskL2:
def test_disk_hit_skips_api_show(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
MM._local_probe_disk_put(
"ollama_num_ctx", "http://127.0.0.1:11434|llama3", 131072
)
with patch("httpx.Client") as mock_client:
ctx = MM.query_ollama_num_ctx("llama3", "http://127.0.0.1:11434/v1")
assert ctx == 131072
mock_client.assert_not_called()
def test_successful_show_persists(self):
_clear_in_proc()
MM._local_probe_disk_put("server_type", "http://127.0.0.1:11434", "ollama")
resp = MagicMock(status_code=200)
resp.json.return_value = {
"parameters": "",
"model_info": {"llama.context_length": 8192},
}
client = MagicMock()
client.post.return_value = resp
client.__enter__ = lambda s: client
client.__exit__ = lambda s, *a: False
with patch("httpx.Client", return_value=client):
ctx = MM.query_ollama_num_ctx("llama3", "http://127.0.0.1:11434/v1")
assert ctx == 8192
assert (
MM._local_probe_disk_get("ollama_num_ctx", "http://127.0.0.1:11434|llama3")
== 8192
)