Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/test_glm_5_3_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ def test_glm_5_3_in_models_payload_for_zai_provider(tmp_path, monkeypatch):
except Exception:
pass

# Pin the agent-core catalog: get_available_models() sources the zai
# model list from the INSTALLED hermes-cli core (via
# _read_live_provider_model_ids -> provider_model_ids), not from the
# repo's static _PROVIDER_MODELS. On a box whose installed core predates
# glm-5.3 this test previously failed spuriously ("glm-5.3 missing from
# zai group models"). Stub the core catalog to a deterministic empty
# list so the repo's own _PROVIDER_MODELS fallback is exercised — this
# tests WebUI catalog propagation, not the installed core version.
try:
import hermes_cli.models as hm
monkeypatch.setattr(hm, "provider_model_ids", lambda _pid: [])
except Exception:
pass

result = c.get_available_models()
c.reload_config()

Expand Down
76 changes: 49 additions & 27 deletions tests/test_ttl_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,39 +111,61 @@ def test_ttl_expiry():
def test_mtime_invalidation():
"""Populate the cache, then change _cfg_mtime to simulate a config file
change on disk. The next call should invalidate the cache and re-scan.
"""
_reset_cache()

# Ensure _cfg_mtime matches file so first call doesn't re-scan due to mtime
try:
real_mtime = config.Path(config._get_config_path()).stat().st_mtime
except OSError:
real_mtime = 0.0
config._cfg_mtime = real_mtime

# First call populates cache
result1 = config.get_available_models()
assert config._available_models_cache is not None
This test is fully self-contained: it invalidates both memory and disk
caches first, then populates the cache via a priming call, then verifies
that an mtime mismatch triggers a cache rebuild. It does not depend on
any sibling test's execution order.
"""
# Fully invalidate (memory + disk) so we start from a known clean state.
config.invalidate_models_cache()

# Simulate config.yaml changed on disk by setting _cfg_mtime to 0
# (which won't match the actual file mtime)
config._cfg_mtime = 0.0
# Force the synchronous (unbounded) rebuild path so the priming call is
# guaranteed to have published the cache when it returns. With the
# default bounded budget (_LIVE_REBUILD_BUDGET_SECONDS=4.0), a cold
# rebuild on a slow/loaded box can exceed the budget and return a
# static fallback WITHOUT populating _available_models_cache — which
# made this test fail when run in isolation (it previously only passed
# because a sibling test had already warmed the cache in-process).
original_budget = config._LIVE_REBUILD_BUDGET_SECONDS
config._LIVE_REBUILD_BUDGET_SECONDS = 0.0
real_mtime = 0.0

# The next call should detect mtime mismatch, reload, and invalidate cache
old_cache = config._available_models_cache
old_ts = config._available_models_cache_ts
try:
# Ensure _cfg_mtime matches file so first call doesn't re-scan due to mtime
try:
real_mtime = config.Path(config._get_config_path()).stat().st_mtime
except OSError:
real_mtime = 0.0
config._cfg_mtime = real_mtime

# Priming call: populate the cache (both memory and disk)
result1 = config.get_available_models()
assert config._available_models_cache is not None, (
"Priming call should populate the in-memory cache"
)
assert config._available_models_cache_ts > 0.0, (
"Priming call should set a valid cache timestamp"
)

result2 = config.get_available_models()
# Simulate config.yaml changed on disk by setting _cfg_mtime to 0
# (which won't match the actual file mtime)
config._cfg_mtime = 0.0

# Cache must have been refreshed — timestamp advanced since we reset it
# to 0.0 on invalidation.
assert config._available_models_cache_ts > 0.0, (
"Cache timestamp should be updated after invalidation + rebuild"
)
# The next call should detect mtime mismatch, reload, and invalidate cache
result2 = config.get_available_models()

# Restore
config._cfg_mtime = real_mtime
_reset_cache()
# Cache must have been refreshed — timestamp advanced since we reset it
# to 0.0 on invalidation.
assert config._available_models_cache_ts > 0.0, (
"Cache timestamp should be updated after invalidation + rebuild"
)
Comment on lines +159 to +162

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Assert the cache was rebuilt

The priming call already establishes that _available_models_cache_ts is positive, so repeating that assertion after the mtime mismatch does not distinguish a rebuild from reuse of the primed cache. Compare the cache identity or timestamp across the second call so an mtime-invalidation regression cannot pass undetected.

Context Used: AGENTS.md (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

# Both calls must return a valid /api/models payload.
assert "groups" in result1 and "groups" in result2
finally:
config._LIVE_REBUILD_BUDGET_SECONDS = original_budget
config._cfg_mtime = real_mtime
config.invalidate_models_cache()


# ── 4. test_deepcopy_isolation ────────────────────────────────────────────
Expand Down
Loading