Files
hermes-webui/tests/test_issue1426_openrouter_free_tier_live_fetch.py
T
nesquena-hermesandnesquena-hermes 29f67b4289 Release exp-v0.52.39: transparent-stream mode-precedence fix (#5942/#5943) + hermetic OpenRouter test (#1426) (#5948)
* fix(transparent-stream): stop compact worklog from hijacking transparent live render (#5942, #5943)

Transparent Stream turns with tool calls alternated between the grouped compact
worklog summary ("Processed Ns") and the individual per-event row list (#5942), and
rebuilt every live row element each tick causing flicker (#5943, regression of #5367).

Root cause (bisected to the #5739 merge as a MERGE INTERACTION; #5700/#5739 timestamp
code is NOT causal): #5746 (final-answer-only mode) reworked renderLiveAnchorActivityScene
so a caller-supplied `requestedMode` (opts.mode) OVERRODE the user's active display mode.
Several live callers (appendLiveToolCard, ensureLiveWorklogShell, appendLiveCompressionCard,
appendLiveThinking, session restore in sessions.js) hardcode {mode:'compact_worklog'}.
Before #5746 the dispatch checked isTransparentStream() FIRST and ignored that hint; after,
those callers forced the compact grouped render onto a transparent turn. The compact pass
deletes the keyed transparent rows, so the next transparent tick's preserveByKey reuse
(the #5367 element-reuse fix) misses -> it rebuilds a grouped frame then tears it down and
recreates every row = alternating + flicker.

Fix (two layers, defence in depth):
1. renderLiveAnchorActivityScene: the USER's active chatActivityMode() is authoritative;
   opts.mode is only a fallback when there is no usable active mode. A compact_worklog hint
   can no longer override an active transparent_stream (restores the good-build precedence).
2. Removed the now-dead hardcoded {mode:'compact_worklog'} argument from all 8 current-view
   live/restore call sites (ui.js x5, sessions.js x3) so intent is explicit and the helpers
   resolve the active mode via chatActivityMode().

Compact_worklog folding, hide_all_activity suppression, the #5700/#5739 timestamp feature,
and #5860 lazy settled-worklog materialization are all unaffected.

Verified with the crown-jewel stream regression gate (private workspace): transparent_stream
goes rowElementSwaps 1105->0 and groupedFramesInTransparent 42->0; hide_all_activity clean.
Independently root-caused + fix-shape-confirmed by Codex adversarial review.

* fix(transparent-stream): also make projection mode active-authoritative in _renderLiveAnchorActivitySceneForStream (Fable advisory — kill latent requestedMode-wins drift)

* test(#5942/#5943): update brittle source-string assertions for removed {mode:'compact_worklog'} arg

test_live_to_final_anchor_visible_order.py asserted the literal presence of the now-removed
hardcoded compact-mode arg (in ensureLiveWorklogShell + the sessions.js restore path). The fix
correctly drops that arg so the helpers resolve the active display mode. Updated both to assert
the mode-free call / active-mode behavior instead of the removed literal. 63/63 in-file pass.

* revert wrapper projection-mode change (preserve #3820 explicit-mode contract)

The follow-up 8f654f0c0 made _renderLiveAnchorActivitySceneForStream projection-mode
active-authoritative, but that broke the #3820 contract (test_issue3820: explicit hide_all
honored when active=compact; hide_all-active always wins). Fable flagged this only as an
advisory/latent cleanup, NOT required — the real fix is the central renderLiveAnchorActivityScene
dispatch + call-site arg removal, both proven by the gate. Reverting the wrapper restores #3820
(109/109 targeted tests pass) with the bug fix fully intact.

* test(#1426): make OpenRouter live-fetch test hermetic by mocking curated fetch

test_openrouter_group_uses_live_fetch_when_available failed deterministically
in any environment where the hermes_cli agent package is installed AND the
network is reachable, e.g.:

    pytest "tests/test_issue1426_openrouter_free_tier_live_fetch.py::\
test_openrouter_group_uses_live_fetch_when_available" -p no:xdist

    assert 'minimax/minimax-m2.5:free' in
        ['anthropic/claude-sonnet-4.6', 'anthropic/claude-fable-5', ...]

Root cause — a gap in the test's mock, not a production regression:

get_available_models() populates the OpenRouter group from TWO live fetches:

  (1) hermes_cli.models.fetch_openrouter_models() — the curated catalog.
      This routes through hermes_cli's open_credentialed_url(), which builds
      its own opener and calls .open(); it does NOT go through the
      module-level urllib.request.urlopen symbol the test monkeypatches.
  (2) a direct urllib.request.urlopen(".../v1/models") free-tier augment —
      this one IS intercepted by the monkeypatch.

The test only patched urllib.request.urlopen, so fetch (1) hit the real
network and returned ~36 live curated models. Those filled the visible picker
cap (15), pushing the mocked free-tier entries (minimax/minimax-m2.5:free,
openrouter/elephant-alpha) into the group's extra_models bucket. The
assertion only inspected the visible `models` bucket, so it failed. In CI
(hermes_cli absent) fetch (1) raised ImportError, the curated base was empty,
the 2-3 mocked entries fit the visible cap, and the test passed — hence the
environment-dependent failure.

Production is correct: free-tier models DO surface in the picker (verified
they land in extra_models). The fix keeps the coverage intent intact:

  - Mock fetch (1) too (as sibling tests already do) so the test is genuinely
    hermetic — matching this module's docstring promise of "without depending
    on real network access" — with a small curated base so live data, not the
    fallback list, is exercised.
  - Assert the free-tier IDs surface across BOTH picker surfaces (`models` +
    `extra_models`), matching the sibling fail-closed and cap tests, so a
    large curated base overflowing the visible cap no longer flips the result.

No production code changed.

* Release exp-v0.52.39: transparent-stream mode-precedence fix (#5946/#5942/#5943) + hermetic OpenRouter live-fetch test (#5947/#1426)

---------

Co-authored-by: nesquena-hermes <agent@nesquena-hermes>
2026-07-11 14:40:59 -07:00

352 lines
15 KiB
Python

"""Regression tests for #1426 — OpenRouter free-tier visibility (structural fix).
Original PR #1548 added 6 hardcoded `_FALLBACK_MODELS` entries. This is the
structural augmentation: WebUI now does TWO live fetches when populating the
OpenRouter group:
(1) `hermes_cli.models.fetch_openrouter_models()` — the curated tool-supporting
list, which goes through the tool-support filter (Kilo-Org/kilocode#9068).
(2) Direct `https://openrouter.ai/api/v1/models` — filtered to free-tier-only,
bypassing the tool-support filter so newly-added free variants appear.
Both fall back to `_FALLBACK_MODELS` (which retains @bergeouss's hardcoded list
as a defense-in-depth fallback) when the API is unreachable.
These tests verify the structural fix without depending on real network access:
the urllib.request layer is monkeypatched.
"""
from __future__ import annotations
import json
import urllib.request
import pytest
import api.config as config
class _FakeResponse:
def __init__(self, payload: dict):
self._buf = json.dumps(payload).encode()
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def read(self) -> bytes:
return self._buf
def _make_or_payload(*items: dict) -> dict:
return {"data": list(items)}
def _get_grouped_models() -> list[dict]:
"""Helper: return the `groups` field from get_available_models()."""
# Reset internal cache so each call re-runs the live-fetch path
try:
config.invalidate_models_cache()
except Exception:
pass
result = config.get_available_models()
return result.get("groups", [])
@pytest.fixture(autouse=True)
def _isolate_openrouter_cache(monkeypatch):
"""Reset the curated cache before each test so the live-fetch path runs.
Also force `openrouter` as the active provider so the openrouter branch
in get_available_models() actually runs."""
try:
from hermes_cli import models as _hm
monkeypatch.setattr(_hm, "_openrouter_catalog_cache", None, raising=False)
except Exception:
pass
# Force openrouter to be detected by injecting it into config
monkeypatch.setattr(
config,
"cfg",
{
"model": {"provider": "openrouter", "default": "anthropic/claude-sonnet-4.6"},
"providers": {"openrouter": {"api_key": "sk-or-test-key"}},
},
raising=False,
)
# Reset module-level cache
try:
config.invalidate_models_cache()
except Exception:
pass
def test_fallback_list_contains_free_tier_entries():
"""The hardcoded fallback list (defense-in-depth) still contains the
contributor's free-tier entries so offline / test envs see them."""
or_entries = [m for m in config._FALLBACK_MODELS if m.get("provider") == "OpenRouter"]
assert len(or_entries) >= 5, "fallback list should include at least 5 free-tier entries"
free_labels = [m["label"] for m in or_entries if "free" in m["label"].lower()]
assert len(free_labels) >= 5, f"expected ≥5 free-tier entries in fallback, got {len(free_labels)}"
def test_openrouter_group_uses_live_fetch_when_available(monkeypatch):
"""When OpenRouter /v1/models is reachable, the picker shows live data,
not just the fallback list. Free-tier entries get a (free) suffix."""
fake_payload = _make_or_payload(
# Tool-supporting paid model
{"id": "anthropic/claude-sonnet-4.6", "name": "Claude Sonnet 4.6",
"supported_parameters": ["tools"], "pricing": {"prompt": "0.000003", "completion": "0.000015"}},
# Free-tier model NOT advertising tools — the bug from #1426
{"id": "minimax/minimax-m2.5:free", "name": "MiniMax M2.5",
"supported_parameters": [], "pricing": {"prompt": "0", "completion": "0"}},
# Free model without :free suffix but pricing shows free
{"id": "openrouter/elephant-alpha", "name": "Elephant Alpha",
"supported_parameters": ["tools"], "pricing": {"prompt": "0", "completion": "0"}},
)
def _fake_urlopen(req, timeout=None):
return _FakeResponse(fake_payload)
monkeypatch.setattr(urllib.request, "urlopen", _fake_urlopen)
# The OpenRouter branch does TWO live fetches:
# (1) hermes_cli.models.fetch_openrouter_models() — the curated catalog.
# This routes through hermes_cli's `open_credentialed_url()`, which
# builds its own opener and calls `.open()`; it does NOT go through
# the module-level `urllib.request.urlopen` symbol patched above.
# So the monkeypatch alone leaves fetch (1) hitting the REAL network
# whenever hermes_cli is installed — returning the live curated list,
# which fills the visible picker cap and pushes the mocked free-tier
# entries into `extra_models`. Mock fetch (1) here too so the test is
# genuinely hermetic (as the module docstring promises) and the
# free-tier augment path (2) is what is actually under test.
# (2) the direct urllib.request.urlopen(".../v1/models") — the free-tier
# augment; this one IS intercepted by the monkeypatch above.
try:
from hermes_cli import models as _hm
monkeypatch.setattr(_hm, "_openrouter_catalog_cache", None, raising=False)
# A small curated base ("live data, not just the fallback list") so the
# tool-supporting paid model is present alongside the free-tier augment.
monkeypatch.setattr(
_hm, "fetch_openrouter_models",
lambda *a, **k: [("anthropic/claude-sonnet-4.6", "")],
)
except Exception:
pass
grouped = _get_grouped_models()
or_group = next((g for g in grouped if g.get("provider_id") == "openrouter"), None)
assert or_group is not None, "openrouter group must be present"
# Both `models` (visible dropdown) and `extra_models` (slash-command
# autocomplete / dynamic-label overflow) are picker surfaces — a free-tier
# entry that lands in either bucket has surfaced in the picker. Assert
# across both, matching the sibling fail-closed / cap tests, so a large
# curated base overflowing the visible cap does not flip this assertion.
model_ids = [
m["id"]
for bucket in ("models", "extra_models")
for m in or_group.get(bucket, [])
]
# Resilient to test-isolation pollution: when a sibling test mutates
# `cfg` and triggers the openrouter-not-active branch, _apply_provider_prefix
# adds an `@openrouter:` prefix to model IDs. Skip rather than fail — the
# API contract under test here is "the live-fetch branch surfaces these
# IDs", and either prefixed or unprefixed form satisfies that contract.
has_prefix = any(mid.startswith("@openrouter:") for mid in model_ids)
if has_prefix:
import pytest
pytest.skip("openrouter active provider not honored (likely test-isolation pollution from sibling test)")
# Free-tier variants must be visible despite not advertising tool support
assert "minimax/minimax-m2.5:free" in model_ids, \
"free-tier minimax/minimax-m2.5:free must surface in the picker even without tools support"
assert "openrouter/elephant-alpha" in model_ids, \
"free pricing model must surface even without :free suffix"
@pytest.mark.parametrize(
("pricing", "case_name"),
[
({}, "missing"),
({"prompt": "0"}, "completion missing"),
({"completion": "0"}, "prompt missing"),
({"prompt": None, "completion": None}, "null"),
({"prompt": "n/a", "completion": "0"}, "malformed"),
],
)
def test_openrouter_free_tier_pricing_fails_closed(
monkeypatch,
pricing,
case_name,
):
"""Unknown pricing must not be presented as free without a :free ID."""
candidate_id = f"vendor/{case_name.replace(' ', '-')}-pricing"
explicit_free_id = "vendor/explicit-free:free"
fake_payload = _make_or_payload(
{"id": candidate_id, "name": case_name, "pricing": pricing},
{"id": explicit_free_id, "name": "Explicit Free", "pricing": pricing},
)
monkeypatch.setattr(
urllib.request,
"urlopen",
lambda *_args, **_kwargs: _FakeResponse(fake_payload),
)
# hermes_cli (the agent package) is an optional dependency and is not
# installed in the WebUI CI test environment. Force its live-fetch to
# return empty when present so the fail-closed static path is exercised;
# when absent, that static path is already the only one — mirror the
# try/except guard the sibling tests in this file use.
try:
from hermes_cli import models as hermes_models
monkeypatch.setattr(hermes_models, "fetch_openrouter_models", lambda **_kwargs: [])
monkeypatch.setattr(hermes_models, "provider_model_ids", lambda *_args, **_kwargs: [])
except Exception:
pass
grouped = _get_grouped_models()
openrouter_group = next(
(group for group in grouped if group.get("provider_id") == "openrouter"),
None,
)
if openrouter_group is None:
# Test-isolation pollution: when a sibling test (in this file or another
# shard-adjacent module) mutates config.cfg away from the openrouter
# active provider, get_available_models() omits the openrouter group
# entirely. Skip rather than fail — same guard the sibling live-fetch
# tests use — since the fail-closed contract under test only applies
# when that branch actually runs.
pytest.skip("openrouter group absent (likely test-isolation pollution from a sibling test)")
model_ids = {
model["id"]
for bucket_name in ("models", "extra_models")
for model in openrouter_group.get(bucket_name, [])
}
assert candidate_id not in model_ids
assert explicit_free_id in model_ids
def test_openrouter_falls_back_to_static_when_live_fails(monkeypatch):
"""If both hermes_cli.fetch and the direct urlopen raise, the picker
must fall back to the hardcoded `_FALLBACK_MODELS` list — never empty."""
def _fake_urlopen(req, timeout=None):
raise OSError("simulated network outage")
monkeypatch.setattr(urllib.request, "urlopen", _fake_urlopen)
# Force hermes_cli to fail too
import sys
fake_module = type(sys)("hermes_cli.models")
def _raise(*args, **kwargs):
raise RuntimeError("simulated import failure")
fake_module.fetch_openrouter_models = _raise
fake_module.provider_model_ids = lambda *a, **k: []
monkeypatch.setitem(sys.modules, "hermes_cli.models", fake_module)
grouped = _get_grouped_models()
or_group = next((g for g in grouped if g.get("provider_id") == "openrouter"), None)
assert or_group is not None, "openrouter group must still be present in fallback path"
assert len(or_group["models"]) > 0, "fallback must produce a non-empty model list"
# The hardcoded free-tier entries MUST be in the fallback
fallback_ids = {m["id"] for m in or_group["models"]}
# At least one of the contributor's hardcoded free-tier entries must be present
expected_free_ids = {
"openrouter/elephant-alpha",
"openrouter/owl-alpha",
"tencent/hy3-preview:free",
"nvidia/nemotron-3-super-120b-a12b:free",
"arcee-ai/trinity-large-preview:free",
}
overlap = fallback_ids & expected_free_ids
assert len(overlap) >= 3, \
f"static fallback must include the contributor's hardcoded free-tier entries; got overlap={overlap}"
def test_free_tier_cap_prevents_picker_drowning(monkeypatch):
"""OpenRouter may return hundreds of free-tier variants — the implementation
must keep the picker usable without letting the experimental free-tier
augmentation balloon without bound."""
items = []
for i in range(50):
items.append({
"id": f"vendor{i}/model-{i}:free",
"name": f"Model {i}",
"supported_parameters": [],
"pricing": {"prompt": "0", "completion": "0"},
})
fake_payload = _make_or_payload(*items)
def _fake_urlopen(req, timeout=None):
return _FakeResponse(fake_payload)
monkeypatch.setattr(urllib.request, "urlopen", _fake_urlopen)
try:
from hermes_cli import models as _hm
monkeypatch.setattr(_hm, "_openrouter_catalog_cache", None, raising=False)
except Exception:
pass
grouped = _get_grouped_models()
or_group = next((g for g in grouped if g.get("provider_id") == "openrouter"), None)
assert or_group is not None
free_added_ids = {
m["id"]
for bucket_name in ("models", "extra_models")
for m in or_group.get(bucket_name, [])
if m["id"].startswith("vendor")
}
expected_ids = {
f"vendor{i}/model-{i}:free"
for i in range(config._OPENROUTER_FREE_TIER_AUGMENT_CAP)
}
assert expected_ids.issubset(free_added_ids), (
"The picker should retain the first capped tranche of free-tier models "
"instead of discarding the augmentation entirely."
)
assert len(free_added_ids) == config._OPENROUTER_FREE_TIER_AUGMENT_CAP, (
"The OpenRouter free-tier live fetch should stay capped so extra_models "
"does not balloon to hundreds of experimental variants."
)
assert len(or_group.get("extra_models", [])) > 0, (
"When the visible picker cap is exceeded, free-tier overflow models "
"must move into extra_models instead of being discarded."
)
def test_openrouter_dedupe_curated_and_free_tier(monkeypatch):
"""If a model appears in both the curated catalog AND the free-tier fetch,
it must appear exactly once in the picker (via `seen_ids` deduplication)."""
fake_payload = _make_or_payload(
{"id": "anthropic/claude-sonnet-4.6", "name": "Claude Sonnet 4.6",
"supported_parameters": ["tools"], "pricing": {"prompt": "0", "completion": "0"}},
)
def _fake_urlopen(req, timeout=None):
return _FakeResponse(fake_payload)
monkeypatch.setattr(urllib.request, "urlopen", _fake_urlopen)
import sys
fake_module = type(sys)("hermes_cli.models")
fake_module.fetch_openrouter_models = lambda **k: [("anthropic/claude-sonnet-4.6", "")]
fake_module.provider_model_ids = lambda *a, **k: ["anthropic/claude-sonnet-4.6"]
monkeypatch.setitem(sys.modules, "hermes_cli.models", fake_module)
grouped = _get_grouped_models()
or_group = next((g for g in grouped if g.get("provider_id") == "openrouter"), None)
assert or_group is not None
# Skip on prefix pollution — see test_openrouter_group_uses_live_fetch_when_available
if any(m["id"].startswith("@openrouter:") for m in or_group["models"]):
import pytest
pytest.skip("openrouter active provider not honored (likely test-isolation pollution from sibling test)")
matching = [m for m in or_group["models"] if m["id"] == "anthropic/claude-sonnet-4.6"]
assert len(matching) == 1, \
f"model present in both surfaces should appear once, got {len(matching)}"