Skip to content

The agent that grows with you

fix(providers): a provider registered after hermes_cli.models imports still reaches the picker catalog; unbind the terminal scope a test leaked

teknium1a25cf4d77d46
diff --git a/hermes_cli/models_catalog_static.py b/hermes_cli/models_catalog_static.py
index 3cfcda14ebd..8a9b1728770 100644
--- a/hermes_cli/models_catalog_static.py
+++ b/hermes_cli/models_catalog_static.py
@@ -369,20 +369,36 @@ def _plugin_provider_enters_picker(pp) -> bool:
return pp.name not in _canonical_slugs
-try:
- from providers import list_providers as _list_providers_for_canonical
- for _pp in _list_providers_for_canonical():
- if not _plugin_provider_enters_picker(_pp):
+def sync_plugin_provider_catalog() -> int:
+ """Admit every registered plugin provider without a built-in row; return how many were added.
+
+ Runs at import and again from ``providers._sync_auth_registry`` whenever a profile is registered
+ after this module was imported. The import-time pass alone observes a *partial* registry: a
+ plugin whose own imports pull ``hermes_cli.models`` in mid-``_discover_providers()``, or a
+ profile registered later at runtime, would otherwise never reach the picker, ``hermes model``,
+ ``/model`` or the Desktop ``model.options`` list until restart — the catalog twin of the auth
+ registry window (#102123). Idempotent by slug; built-in rows are never rewritten.
+ """
+ try:
+ from providers import list_providers
+ profiles = list_providers()
+ except Exception:
+ return 0
+ added = 0
+ for pp in profiles:
+ if not _plugin_provider_enters_picker(pp):
continue
- _label = _pp.display_name or _pp.name
- CANONICAL_PROVIDERS.append(ProviderEntry(_pp.name, _label, _pp.description or f"{_label} (direct API)"))
- _canonical_slugs.add(_pp.name)
-except Exception:
- pass
+ label = pp.display_name or pp.name
+ CANONICAL_PROVIDERS.append(ProviderEntry(pp.name, label, pp.description or f"{label} (direct API)"))
+ _canonical_slugs.add(pp.name)
+ _PROVIDER_LABELS[pp.name] = label
+ added += 1
+ return added
-_PROVIDER_LABELS = {p.slug: p.label for p in CANONICAL_PROVIDERS}
+_PROVIDER_LABELS: dict[str, str] = {p.slug: p.label for p in CANONICAL_PROVIDERS}
_PROVIDER_LABELS["custom"] = "Custom endpoint" # special case: not a named provider
+sync_plugin_provider_catalog()
# ---------------------------------------------------------------------------
diff --git a/providers/__init__.py b/providers/__init__.py
index 9f09982b514..71716649516 100644
--- a/providers/__init__.py
+++ b/providers/__init__.py
@@ -95,7 +95,7 @@ _BUNDLED_PLUGINS_DIR = (
def _sync_auth_registry() -> None:
- """Mirror profiles into ``hermes_cli.auth.PROVIDER_REGISTRY`` if that module is loaded.
+ """Mirror profiles into the ``hermes_cli`` snapshots (auth registry, picker catalog) that are loaded.
``hermes_cli.auth`` takes its own snapshot of ``list_providers()`` when it is imported. If a
plugin's imports pull that module in while :func:`_discover_providers` is still running, the
@@ -105,13 +105,17 @@ def _sync_auth_registry() -> None:
top-level code mid-scan and risk a circular import). Never raises: registration must not fail
because of the auth mirror.
"""
- sync = getattr(sys.modules.get("hermes_cli.auth"), "sync_plugin_provider_registry", None)
- if sync is None:
- return
- try:
- sync()
- except Exception as exc: # pragma: no cover — never break discovery
- logger.debug("auth registry sync skipped: %s", exc)
+ for module, attr in (
+ ("hermes_cli.auth", "sync_plugin_provider_registry"),
+ ("hermes_cli.models_catalog_static", "sync_plugin_provider_catalog"),
+ ):
+ sync = getattr(sys.modules.get(module), attr, None)
+ if sync is None:
+ continue
+ try:
+ sync()
+ except Exception as exc: # pragma: no cover — never break discovery
+ logger.debug("%s sync skipped: %s", module, exc)
def register_provider(profile: ProviderProfile) -> None:
diff --git a/tests/hermes_cli/test_models_catalog_late_plugin_provider.py b/tests/hermes_cli/test_models_catalog_late_plugin_provider.py
new file mode 100644
index 00000000000..4492a77083a
--- /dev/null
+++ b/tests/hermes_cli/test_models_catalog_late_plugin_provider.py
@@ -0,0 +1,39 @@
+"""A provider registered after ``hermes_cli.models`` was imported still reaches the picker catalog.
+
+``CANONICAL_PROVIDERS`` admitted plugin providers once, at import. A plugin whose imports pull
+``hermes_cli.models`` in mid-discovery, or a profile registered at runtime, never reached
+``list_available_providers`` / ``_PROVIDER_LABELS`` until restart: the picker twin of the auth
+registry window (#102123). ``providers._sync_auth_registry`` now re-admits into both snapshots.
+"""
+
+from providers import register_provider
+from providers.base import ProviderProfile
+
+
+def _profile(name: str) -> ProviderProfile:
+ return ProviderProfile(name=name, display_name=name, description="late plugin (direct API)")
+
+
+def test_late_registered_provider_reaches_picker_catalog(monkeypatch):
+ import hermes_cli.models_catalog_static as catalog
+ from hermes_cli.models import list_available_providers
+
+ # this module is imported (the snapshot exists) before the registration below
+ monkeypatch.setattr(catalog, "CANONICAL_PROVIDERS", list(catalog.CANONICAL_PROVIDERS))
+ monkeypatch.setattr(catalog, "_canonical_slugs", set(catalog._canonical_slugs))
+ monkeypatch.setattr(catalog, "_PROVIDER_LABELS", dict(catalog._PROVIDER_LABELS))
+ monkeypatch.setattr("hermes_cli.models.CANONICAL_PROVIDERS", catalog.CANONICAL_PROVIDERS)
+ monkeypatch.setattr("hermes_cli.models._PROVIDER_LABELS", catalog._PROVIDER_LABELS)
+ slug = "zz-late-plugin-provider"
+ assert slug not in {r["id"] for r in list_available_providers()}
+
+ import providers as registry
+ registry.list_providers() # discovery done: a registration now is a post-discovery one
+ monkeypatch.setitem(registry._REGISTRY, slug, _profile(slug)) # keeps the registry scoped
+ register_provider(_profile(slug))
+
+ assert slug in {r["id"] for r in list_available_providers()}
+ assert catalog._PROVIDER_LABELS[slug] == slug
+ # idempotent: a second sync adds nothing
+ assert catalog.sync_plugin_provider_catalog() == 0
+ assert sum(1 for p in catalog.CANONICAL_PROVIDERS if p.slug == slug) == 1
diff --git a/tests/tui_gateway/test_multi_profile_hosting_transitions.py b/tests/tui_gateway/test_multi_profile_hosting_transitions.py
index 7e706447bb9..16bf0749798 100644
--- a/tests/tui_gateway/test_multi_profile_hosting_transitions.py
+++ b/tests/tui_gateway/test_multi_profile_hosting_transitions.py
@@ -125,11 +125,17 @@ def test_release_resets_every_scope_when_one_reset_fails(two_homes, monkeypatch)
scopes = server._profile_runtime_scope_tokens(str(b))
assert current_secret_scope() is not None and get_hermes_home_override() == str(b)
+ real_reset = terminal_scope.reset_terminal_scope
+
def exploding(_token):
raise RuntimeError("terminal reset blew up")
monkeypatch.setattr(terminal_scope, "reset_terminal_scope", exploding)
server._release_build_profile_scopes(scopes) # suppresses the re-raised failure
+ # The exploding reset left the terminal ContextVar bound to B's scope on this thread. The
+ # scenario is proven here; unbind it, or every later test in the process inherits B's scope.
+ real_reset(scopes.terminal)
+ assert terminal_scope.get_terminal_scope() is None
assert current_secret_scope() is None
assert get_hermes_home_override() is None
assert Path(server._hermes_home) == root