Skip to content

The agent that grows with you

fix(local-runtime): first catalog refresh is due right after boot

Maciej Lisiewski58b420464b22
diff --git a/hermes_cli/local_runtime/catalog.py b/hermes_cli/local_runtime/catalog.py
index 7d9884237bc..9ec277542c1 100644
--- a/hermes_cli/local_runtime/catalog.py
+++ b/hermes_cli/local_runtime/catalog.py
@@ -293,7 +293,9 @@ _CATALOG_URL = ("https://raw.githubusercontent.com/NousResearch/hermes-agent"
_SCHEMA_VERSION = 2 # bump when an older app would read a newer catalog but launch its entries wrongly
_REFRESH_TTL_S = 6 * 3600
_refresh_lock = threading.Lock()
-_last_refresh_attempt = 0.0
+# None = never attempted: monotonic() counts from boot, so a 0.0 sentinel read as "refreshed at
+# boot" and suppressed the first fetch for the first six awake hours after every reboot.
+_last_refresh_attempt: float | None = None
def _asset_from(d: dict | None) -> AssetFile | None:
@@ -355,7 +357,7 @@ def refresh_catalog(force: bool = False) -> bool:
now = time.monotonic()
with _refresh_lock:
- if not force and now - _last_refresh_attempt < _REFRESH_TTL_S:
+ if not force and _last_refresh_attempt is not None and now - _last_refresh_attempt < _REFRESH_TTL_S:
return False
_last_refresh_attempt = now
try:
@@ -374,7 +376,8 @@ def refresh_catalog(force: bool = False) -> bool:
def refresh_catalog_soon() -> None:
"""TTL-gated background refresh; returns immediately. The current request serves the catalog
it already has — the refresh lands for the next one."""
- if time.monotonic() - _last_refresh_attempt < _REFRESH_TTL_S:
+ last = _last_refresh_attempt
+ if last is not None and time.monotonic() - last < _REFRESH_TTL_S:
return
threading.Thread(target=refresh_catalog, daemon=True, name="catalog-refresh").start()
diff --git a/tests/hermes_cli/test_catalog_json.py b/tests/hermes_cli/test_catalog_json.py
index 44da34cdba6..84911b93763 100644
--- a/tests/hermes_cli/test_catalog_json.py
+++ b/tests/hermes_cli/test_catalog_json.py
@@ -11,15 +11,20 @@ import io
import json
import urllib.request
+from types import SimpleNamespace
+
import pytest
import hermes_cli.local_runtime.catalog as cat
+# The never-refreshed state a freshly started backend has (nothing calls refresh at import).
+_NEVER_REFRESHED = cat._last_refresh_attempt
+
@pytest.fixture(autouse=True)
def _reset_refresh_state(monkeypatch):
- """Each test starts outside the TTL window with the packaged catalog."""
- monkeypatch.setattr(cat, "_last_refresh_attempt", 0.0)
+ """Each test starts never-refreshed with the packaged catalog."""
+ monkeypatch.setattr(cat, "_last_refresh_attempt", _NEVER_REFRESHED)
packaged = cat._packaged_catalog()
monkeypatch.setattr(cat, "CATALOG", packaged)
yield
@@ -119,3 +124,26 @@ def test_min_engine_gate(monkeypatch):
assert _engine_too_old("") is False, "no requirement, no gate"
assert _engine_too_old("b10000") is False, "installed engine suffices"
assert _engine_too_old("b10363") is True, "newer requirement gates"
+
+
+def test_first_refresh_is_due_shortly_after_boot(monkeypatch):
+ """time.monotonic() counts from boot: a backend started a minute after boot must still fetch
+ on first use instead of treating "never refreshed" as refreshed at boot."""
+ monkeypatch.setattr(cat, "time", SimpleNamespace(monotonic=lambda: 60.0))
+ started = []
+
+ class _Thread:
+ def __init__(self, target, **_kw):
+ self._target = target
+
+ def start(self):
+ started.append(self._target)
+
+ monkeypatch.setattr(cat, "threading", SimpleNamespace(Thread=_Thread))
+ cat.refresh_catalog_soon()
+ assert started == [cat.refresh_catalog]
+
+ _fetch_returns(monkeypatch, _doc_from(lambda m: m))
+ assert cat.refresh_catalog() is True
+ # The attempt is now stamped, so the TTL gate holds for the next six hours.
+ assert cat.refresh_catalog() is False
diff --git a/tests/hermes_cli/test_local_models_routes.py b/tests/hermes_cli/test_local_models_routes.py
index 0f22dfb4042..1ea400e57f1 100644
--- a/tests/hermes_cli/test_local_models_routes.py
+++ b/tests/hermes_cli/test_local_models_routes.py
@@ -25,7 +25,10 @@ from tests.pm._range_server import dl_server, url as _srv_url
def client(tmp_path, monkeypatch):
monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))
from hermes_cli import web_server
+ from hermes_cli.local_runtime import catalog
+ # The first catalog request after start is due a GitHub fetch; tests serve the packaged copy.
+ monkeypatch.setattr(catalog, "refresh_catalog_soon", lambda: None)
test_client = TestClient(web_server.app)
# Same auth pattern as the git-route tests: present the session token.
test_client.headers[web_server._SESSION_HEADER_NAME] = web_server._SESSION_TOKEN