Skip to content

The agent that grows with you

test(models): trim probe negative-cache tests to two invariants

kshitijk4poor20762c67eb33
diff --git a/tests/hermes_cli/test_cached_fetch_api_models.py b/tests/hermes_cli/test_cached_fetch_api_models.py
index e2c6f1864d3..da03301207c 100644
--- a/tests/hermes_cli/test_cached_fetch_api_models.py
+++ b/tests/hermes_cli/test_cached_fetch_api_models.py
@@ -384,13 +384,20 @@ class TestProbeApiModelsNegativeCache:
"""#81123: a fully-failed /models probe must degrade fast, not re-burn
connect timeouts per URL candidate on every picker open."""
+ @pytest.fixture(autouse=True)
+ def _clear_probe_neg_cache(self):
+ import hermes_cli.models as mod
+
+ mod._probe_neg_cache.clear()
+ yield
+ mod._probe_neg_cache.clear()
+
def _fail(self, req, **kw):
raise TimeoutError("connect timed out")
- def test_repeat_failure_skips_network(self, monkeypatch):
+ def test_repeat_failure_within_ttl_skips_network(self, monkeypatch):
import hermes_cli.models as mod
- monkeypatch.setattr(mod, "_probe_neg_cache", {})
monkeypatch.setattr(mod, "_urlopen_model_catalog_request", self._fail)
r1 = mod.probe_api_models("", "https://blackhole.invalid/v1", timeout=1.0)
calls = []
@@ -403,12 +410,30 @@ class TestProbeApiModelsNegativeCache:
assert calls == [] # /v1 + root share one host:port entry
assert r2["probed_url"] == "https://blackhole.invalid/v1/models"
- def test_expired_entry_reprobes(self, monkeypatch):
+ def test_expired_entry_reprobes_and_success_clears_it(self, monkeypatch):
import hermes_cli.models as mod
+ key = "blackhole.invalid:443"
old = time.monotonic() - mod._PROBE_NEG_TTL - 1
- monkeypatch.setattr(mod, "_probe_neg_cache", {"blackhole.invalid:443": old})
+ mod._probe_neg_cache[key] = old
monkeypatch.setattr(mod, "_urlopen_model_catalog_request", self._fail)
out = mod.probe_api_models("", "https://blackhole.invalid/v1", timeout=1.0)
assert out["models"] is None
- assert mod._probe_neg_cache["blackhole.invalid:443"] > old
+ assert mod._probe_neg_cache[key] > old # expiry re-probed and re-recorded
+
+ mod._probe_neg_cache[key] = old # expired again: the real probe runs
+
+ class _Resp:
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *a):
+ return False
+
+ def read(self):
+ return b'{"data": [{"id": "m1"}]}'
+
+ monkeypatch.setattr(mod, "_urlopen_model_catalog_request", lambda req, **kw: _Resp())
+ ok = mod.probe_api_models("", "https://blackhole.invalid/v1", timeout=1.0)
+ assert ok["models"] == ["m1"]
+ assert key not in mod._probe_neg_cache # recovery is not masked by the stale entry