The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(picker): native Ollama cache rows use the 300s TTL and never stale-serve empty catalogs
teknium1d6d6565d90bd
diff --git a/hermes_cli/model_switch_providers.py b/hermes_cli/model_switch_providers.pyindex 3af99e153e7..d5915738dde 100644--- a/hermes_cli/model_switch_providers.py+++ b/hermes_cli/model_switch_providers.py@@ -94,6 +94,7 @@ def _fetch_picker_live_models("""Fetch picker models with native Ollama and cached generic discovery."""from hermes_cli.models import _get_ollama_native_headers, cached_fetch_api_models, fetch_api_modelsfrom hermes_cli.models_local import (+ _OLLAMA_LOCAL_MODELS_CACHE_TTL,_normalize_openai_base_url,fetch_ollama_local_models,should_use_ollama_native_catalog,@@ -140,11 +141,13 @@ def _fetch_picker_live_models(# whole group vanished from the picker until the user hit Refresh Models. Key the entry on# the caller's ``headers`` (what that cache_only read hashes), not ``resolved_headers``: the# native probe's synthesized Authorization would otherwise land under a fingerprint the- # read side never computes, and a keyed endpoint kept flickering.+ # read side never computes, and a keyed endpoint kept flickering. Clamp the fresh window+ # to the native TTL (300s, as cached_provider_model_ids does for the built-in slug): a+ # locally pulled model must not stay invisible for the generic 1h TTL.native_models = (cached_fetch_api_models(api_key, api_url, timeout=timeout, headers=headers, api_mode=api_mode,- fetch_models=_probe_native_catalog)+ fetch_models=_probe_native_catalog, ttl_seconds=_OLLAMA_LOCAL_MODELS_CACHE_TTL)if cache else _probe_native_catalog())if native_models is not None:return native_modelsdiff --git a/hermes_cli/models.py b/hermes_cli/models.pyindex e5789c6d488..9f8b7a635c5 100644--- a/hermes_cli/models.py+++ b/hermes_cli/models.py@@ -2651,13 +2651,17 @@ def cached_fetch_api_models(cache = _load_provider_models_cache()entry = cache.get(cache_key)now = time.time()- valid = not force_refresh and _cache_entry_valid(entry, fp, allow_empty=isinstance(entry, dict) and entry.get("native_catalog") is True)+ native_row = isinstance(entry, dict) and entry.get("native_catalog") is True+ valid = not force_refresh and _cache_entry_valid(entry, fp, allow_empty=native_row)if valid:age = now - entry["at"]if age < ttl_seconds:return _catalog(entry)- if age < _PROVIDER_MODELS_STALE_SERVE_MAX:+ # An empty native catalog is authoritative only inside the TTL (as in+ # cached_provider_model_ids): never stale-serve it, or an Ollama that was model-less at+ # first open keeps an empty row for the whole stale window after models are pulled.+ if entry["models"] and age < _PROVIDER_MODELS_STALE_SERVE_MAX:# Stale-while-revalidate: serve now, refresh off-thread for the next open. cache_only# opens (GUI pickers that must not block on a stopped local server) take the same# non-blocking refresh: without it a locally loaded model stayed invisible for the@@ -2677,8 +2681,9 @@ def cached_fetch_api_models(stored = _entry(live, now)_store_cache_entry(cache_key, stored, cache)return _catalog(stored)- # Live returned nothing (offline, timeout, auth hiccup): a stale same-fingerprint entry beats it.- if _cache_entry_valid(entry, fp, allow_empty=isinstance(entry, dict) and entry.get("native_catalog") is True):+ # Live returned nothing (offline, timeout, auth hiccup): a stale same-fingerprint entry beats it+ # (non-empty only: an empty native row is not worth resurrecting over the generic fallback).+ if _cache_entry_valid(entry, fp):return _catalog(entry)return livediff --git a/tests/hermes_cli/test_model_switch_custom_providers.py b/tests/hermes_cli/test_model_switch_custom_providers.pyindex c0f49ff3203..d9b04edd9f4 100644--- a/tests/hermes_cli/test_model_switch_custom_providers.py+++ b/tests/hermes_cli/test_model_switch_custom_providers.py@@ -118,6 +118,66 @@ def test_picker_native_catalog_skips_cache_admission_when_cache_is_off(monkeypatassert cached_fetch_api_models(None, url, cache_only=True, timeout=1.5) is None+def _native_picker_probe(monkeypatch, models_by_call):+ """Native Ollama detection on, ``/api/tags`` answering successive ``models_by_call``."""+ monkeypatch.setattr(+ "hermes_cli.models_local.should_use_ollama_native_catalog", lambda *a, **k: True+ )+ monkeypatch.setattr("hermes_cli.models._get_ollama_native_headers", lambda *a, **k: {})+ answers = iter(models_by_call)+ monkeypatch.setattr(+ "hermes_cli.models_local.fetch_ollama_local_models", lambda *a, **k: next(answers)+ )+++def _age_cached_rows(seconds):+ from hermes_cli import models as models_mod++ cache = models_mod._load_provider_models_cache()+ for row in cache.values():+ row["at"] -= seconds+ for key, row in cache.items():+ models_mod._store_cache_entry(key, row, cache)+++def test_picker_native_catalog_uses_the_short_native_ttl(monkeypatch):+ """The current endpoint's native row must expire on the 300s Ollama TTL, not the 1h generic one.++ A model pulled after the first picker open otherwise stays invisible for up to an hour;+ ``cached_provider_model_ids`` clamps the built-in ``ollama`` slug the same way. Past the+ native TTL the row is stale: served once, with a background refresh scheduled.+ """+ from hermes_cli.models_local import _OLLAMA_LOCAL_MODELS_CACHE_TTL++ _native_picker_probe(monkeypatch, [["qwen3:8b"]])+ url = "http://127.0.0.1:11434/v1"+ assert _fetch_picker_live_models("sk-ollama", url, "custom", False) == ["qwen3:8b"]++ refreshes = []+ monkeypatch.setattr(+ "hermes_cli.models._spawn_swr_refresh", lambda key, fn=None: refreshes.append(key)+ )+ _age_cached_rows(_OLLAMA_LOCAL_MODELS_CACHE_TTL + 1)+ assert _fetch_picker_live_models("sk-ollama", url, "custom", False) == ["qwen3:8b"]+ assert len(refreshes) == 1, "row older than the native TTL must be revalidated, not fresh"+++def test_picker_empty_native_catalog_is_not_stale_served(monkeypatch):+ """An authoritative empty native row is valid only inside the TTL.++ Beyond it the probe must run again, or an Ollama that was model-less at first open keeps+ an empty picker row for the whole 7-day stale window after models are pulled.+ """+ from hermes_cli.models import _PROVIDER_MODELS_CACHE_TTL++ _native_picker_probe(monkeypatch, [[], ["back:latest"]])+ url = "http://127.0.0.1:11434/v1"+ assert _fetch_picker_live_models("sk-ollama", url, "custom", False) == []++ _age_cached_rows(_PROVIDER_MODELS_CACHE_TTL + 100)+ assert _fetch_picker_live_models("sk-ollama", url, "custom", False) == ["back:latest"]++def test_picker_generic_discovery_preserves_api_mode(monkeypatch):calls = []