Skip to content

The agent that grows with you

fix(auth): mirror plugin providers lock-free and never mid-discovery

Ben Barclay5aaf0fdb5d95
diff --git a/hermes_cli/auth_plugin_providers.py b/hermes_cli/auth_plugin_providers.py
index a5cec6edbf9..3658c209de8 100644
--- a/hermes_cli/auth_plugin_providers.py
+++ b/hermes_cli/auth_plugin_providers.py
@@ -37,7 +37,7 @@ PLUGIN_AUTH_ACTIONS = ("add", "status", "logout", "refresh")
PLUGIN_MIRRORED_PROVIDERS: set[str] = set()
-_SYNC_LOCK = threading.RLock()
+_SYNCING = threading.local() # .active: this thread runs the pending mirror
class HomeScopedRegistry(dict):
@@ -74,17 +74,21 @@ class HomeScopedRegistry(dict):
self._sync_pending = True
def _ready(self) -> None:
- if not self._sync_pending:
+ """Run the pending mirror on this read. No lock is held across it (it lists providers,
+ which reads config): concurrent first readers each run the idempotent sync. Never while
+ ``providers`` is mid-discovery: a partial profile list would mirror a provider that a
+ later, higher-priority registration replaces (discovery's completion sync runs then)."""
+ if not self._sync_pending or getattr(_SYNCING, "active", False):
return
- with _SYNC_LOCK: # re-entrant: the sync reads the registry
- if not self._sync_pending:
- return
- self._sync_pending = False
- try:
- sync_plugin_provider_registry()
- except BaseException:
- self._sync_pending = True
- raise
+ import providers
+ if getattr(providers, "_discovering", False):
+ return
+ _SYNCING.active = True # the sync reads the registry
+ try:
+ sync_plugin_provider_registry()
+ finally:
+ _SYNCING.active = False
+ self._sync_pending = False
def __getitem__(self, key: str) -> Any:
self._ready()
diff --git a/tests/providers/test_auth_registry_lazy_mirror.py b/tests/providers/test_auth_registry_lazy_mirror.py
new file mode 100644
index 00000000000..aac0ea89094
--- /dev/null
+++ b/tests/providers/test_auth_registry_lazy_mirror.py
@@ -0,0 +1,60 @@
+"""PROVIDER_REGISTRY mirrors plugin providers on first read, never from a partial discovery."""
+from __future__ import annotations
+
+import json
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+REPO = Path(__file__).resolve().parents[2]
+
+# An enabled entry-point plugin registers a bundled name (lower priority than the bundled profile)
+# and reads the auth registry while discovery is still running.
+SCRIPT = r"""
+import importlib.metadata as md, json, os, sys
+from pathlib import Path
+home = Path(os.environ["HERMES_HOME"])
+(home / "config.yaml").write_text("plugins:\n enabled: [probe-ep]\n isolation: in-process\n")
+
+class EP:
+ name, group = "probe-ep", "hermes_agent.plugins"
+ def load(self):
+ def register():
+ import providers
+ from providers.base import ProviderProfile
+ providers.register_provider(ProviderProfile(
+ name="fireworks", base_url="https://impostor.test/v1", env_vars=("IMPOSTOR_KEY",)))
+ from hermes_cli.auth import PROVIDER_REGISTRY
+ PROVIDER_REGISTRY.get("fireworks")
+ return register
+
+class EPs:
+ def select(self, **kw):
+ return [EP()]
+
+md.entry_points = lambda: EPs()
+import hermes_cli.auth as auth
+from hermes_cli.config import OPTIONAL_ENV_VARS
+len(OPTIONAL_ENV_VARS)
+import providers
+profile = providers.get_provider_profile("fireworks")
+row = auth.PROVIDER_REGISTRY["fireworks"]
+print("RESULT=" + json.dumps([profile.base_url, list(profile.env_vars), row.inference_base_url,
+ list(row.api_key_env_vars)]))
+"""
+
+
+def test_registry_read_during_entry_point_discovery_keeps_the_final_winner(tmp_path):
+ home = tmp_path / "home"
+ home.mkdir()
+ env = {k: v for k, v in os.environ.items() if k in {"PATH", "LANG", "TZ", "TMPDIR", "PYTHONHASHSEED"}}
+ env.update(HOME=str(tmp_path), HERMES_HOME=str(home), PYTHONPATH=str(REPO), PYTHONDONTWRITEBYTECODE="1",
+ HERMES_CONFIG_BACKEND="file")
+ proc = subprocess.run([sys.executable, "-c", SCRIPT], cwd=REPO, env=env, capture_output=True, text=True,
+ timeout=60, check=False)
+ assert proc.returncode == 0, proc.stderr[-3000:]
+ line = [ln for ln in proc.stdout.splitlines() if ln.startswith("RESULT=")][-1]
+ profile_url, profile_keys, auth_url, auth_keys = json.loads(line[len("RESULT="):])
+ assert "impostor" not in profile_url and "IMPOSTOR_KEY" not in profile_keys # the bundled profile wins
+ assert (auth_url, auth_keys) == (profile_url, profile_keys) # and the auth registry agrees