The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(config): the provider env fill runs holding no lock
Ben Barclay4905b9f9ffd8
diff --git a/hermes_cli/config_defaults_env.py b/hermes_cli/config_defaults_env.pyindex 128345e056a..d6de57643d5 100644--- a/hermes_cli/config_defaults_env.py+++ b/hermes_cli/config_defaults_env.py@@ -2,33 +2,43 @@import threading-_FILL_LOCK = threading.RLock() # re-entrant: the fill itself reads the table+_FILL_LOCK = threading.Lock() # held only to publish: the fill itself runs holding no lock+_FILLING = threading.local() # .active: this thread is running the fill (it reads the table)class OptionalEnvVars(dict):- """``OPTIONAL_ENV_VARS``: a dict that runs one armed ``fill(table)`` before it is first read.+ """``OPTIONAL_ENV_VARS``: a dict that runs one armed fill before it is first read.Some entries (provider profiles') are only known after work that must not run at import- (``hermes_cli.config_provider_env``); writes never trigger the fill. A fill that raises is- re-armed, so the next read tries again."""+ (``hermes_cli.config_provider_env``). ``fill()`` returns the entries to add and may read+ config, so it runs holding no lock (concurrent first readers may each run it; the first to+ finish publishes, under a lock held only for that). Reads made by the fill itself see the+ table without them. A fill that raises publishes nothing and stays armed. Writes never fill."""_fill = None+ _published = None- def arm(self, fill) -> None:- self._fill = fill+ def arm(self, fill, published=None) -> None:+ """``fill() -> dict`` of entries; ``published(entries)`` runs once they are in the table."""+ self._fill, self._published = fill, publisheddef _ready(self) -> None:- if self._fill is None:+ fill = self._fill+ if fill is None or getattr(_FILLING, "active", False):return+ _FILLING.active = True+ try:+ entries = fill()+ finally:+ _FILLING.active = Falsewith _FILL_LOCK:- fill, self._fill = self._fill, None- if fill is None:- return- try:- fill(self)- except BaseException:- self._fill = fill- raise+ if self._fill is not fill:+ return # another reader published first+ for key, value in entries.items():+ dict.__setitem__(self, key, value)+ self._fill = None+ if self._published is not None:+ self._published(entries)def __getitem__(self, key):self._ready()diff --git a/hermes_cli/config_provider_env.py b/hermes_cli/config_provider_env.pyindex 3a6dd932ecd..bb7d1f1cc13 100644--- a/hermes_cli/config_provider_env.py+++ b/hermes_cli/config_provider_env.py@@ -18,35 +18,36 @@ def arm(table, static_names) -> None:"""Called once by ``hermes_cli.config`` at the end of its import."""global _STATIC_NAMES_STATIC_NAMES = frozenset(static_names)- table.arm(_add_provider_entries)+ table.arm(_provider_entries, _PROVIDER_NAMES.update)-def _add_provider_entries(table) -> None:- """Expose the env vars of every ``auth_type="api_key"`` provider profile. A core entry wins;- a provider entry replaces one a platform plugin manifest added (it was added first only- because this runs late)."""+def _provider_entries() -> dict:+ """The env vars of every ``auth_type="api_key"`` provider profile. A core entry wins; a+ provider entry replaces one a platform plugin manifest added (it was added first only because+ this runs late); the first profile to declare a name wins."""try:from providers import list_providersprofiles = list_providers()except Exception: # health: allow BLE001 -- as before: a broken provider layer must not break config readers- # (ConfigBackendUnavailable is a SystemExit: it propagates, and the table re-arms)- return+ # (ConfigBackendUnavailable is a SystemExit: it propagates, and the table stays armed)+ return {}+ entries: dict = {}for pp in profiles:if pp.auth_type != "api_key":continuefor var in pp.env_vars:- if var in _STATIC_NAMES or var in _PROVIDER_NAMES:+ if var in _STATIC_NAMES or var in entries:continueis_key = not var.endswith(("_BASE_URL", "_URL"))label = pp.display_name or pp.name- dict.__setitem__(table, var, {+ entries[var] = {"description": f"{label} {'API key' if is_key else 'base URL override'}","prompt": f"{label} {'API key' if is_key else 'base URL (leave empty for default)'}","url": pp.signup_url or None,"password": is_key,"category": "provider",- "advanced": True})- _PROVIDER_NAMES.add(var)+ "advanced": True}+ return entriesdef core_declared_env_names() -> frozenset[str]:diff --git a/tests/hermes_cli/test_config_provider_env.py b/tests/hermes_cli/test_config_provider_env.pyindex e18d61821f6..5a1bf792f20 100644--- a/tests/hermes_cli/test_config_provider_env.py+++ b/tests/hermes_cli/test_config_provider_env.py@@ -30,7 +30,7 @@ def table(monkeypatch):monkeypatch.setattr(cpe, "_STATIC_NAMES", frozenset({"CORE_KEY"}))monkeypatch.setattr(cpe, "_PROVIDER_NAMES", set())t = OptionalEnvVars({"CORE_KEY": {"description": "core"}, "PLUG_KEY": {"description": "platform"}})- t.arm(cpe._add_provider_entries)+ t.arm(cpe._provider_entries, cpe._PROVIDER_NAMES.update)return t