Skip to content

The agent that grows with you

fix(terminal-env): adapter secrets are Tier 1, owners are read per call

John Paul Solivadca8684c5905
diff --git a/providers/__init__.py b/providers/__init__.py
index 9787b61a55b..80fb63e22b0 100644
--- a/providers/__init__.py
+++ b/providers/__init__.py
@@ -233,6 +233,14 @@ def list_providers() -> list[ProviderProfile]:
return result
+def bundled_provider_profiles() -> list[ProviderProfile]:
+ """The profiles shipped with hermes-agent, from the process-wide layer only: unlike
+ :func:`list_providers` the answer never depends on which profile home is bound."""
+ if not _discovered:
+ _discover_providers()
+ return [p for name, p in _REGISTRY.items() if _SOURCES.get(name) == "bundled"]
+
+
def _home_layer(*, force_stamp_check: bool = False) -> _HomeLayer:
"""The layer for the home bound right now, importing plugin dirs it has not seen yet."""
layer, home, key = _bound_home_layer()
diff --git a/tests/tools/test_local_env_blocklist.py b/tests/tools/test_local_env_blocklist.py
index 44a9f588946..6b4229f6ae1 100644
--- a/tests/tools/test_local_env_blocklist.py
+++ b/tests/tools/test_local_env_blocklist.py
@@ -102,10 +102,10 @@ PHOTON_SIDECAR_TOKEN RAFT_CHANNEL_TOKEN MSGRAPH_WEBHOOK_CLIENT_STATE MSGRAPH_CLI
@pytest.mark.parametrize("builder", ["foreground", "background", "nonterminal"])
def test_adapter_and_provider_profile_secrets_never_reach_children(child_env, monkeypatch, builder):
- from providers import list_providers
+ from providers import bundled_provider_profiles
from tools.env_passthrough import is_env_passthrough, register_env_passthrough
secrets = set(ADAPTER_SECRETS)
- secrets.update(name for profile in list_providers() for name in (profile.env_vars or ()))
+ secrets.update(name for profile in bundled_provider_profiles() for name in (profile.env_vars or ()))
secrets.discard("CLAUDE_CODE_OAUTH_TOKEN") # operator's subscription, not Hermes inference
operator = ["MY_APP_KEY", "DEPLOY_WEBHOOK_SECRET"] # secret-shaped, but no adapter owns them
for name in [*secrets, *operator]:
@@ -122,6 +122,46 @@ def test_adapter_and_provider_profile_secrets_never_reach_children(child_env, mo
assert not any(is_env_passthrough(name) for name in secrets)
+def test_inheriting_child_gets_provider_keys_but_never_adapter_secrets(child_env, monkeypatch):
+ # inherit_credentials is the narrow grant for model-driving CLIs: provider keys only.
+ granted = ["OPENAI_API_KEY", "NOUS_API_KEY", "MY_APP_KEY", "DEPLOY_WEBHOOK_SECRET"]
+ for name in [*ADAPTER_SECRETS, *granted]:
+ monkeypatch.setenv(name, "fake-" + name)
+ observed = observe_child(local.hermes_subprocess_env(inherit_credentials=True),
+ sorted([*ADAPTER_SECRETS, *granted]))
+ assert observed == {**dict.fromkeys(ADAPTER_SECRETS), **{k: "fake-" + k for k in granted}}
+
+
+def test_runtime_adapter_secret_follows_the_bound_profiles_registry(child_env, monkeypatch):
+ from gateway.platform_registry import PlatformEntry, platform_registry
+ from tools.env_passthrough import is_env_passthrough, register_env_passthrough
+ home_a, home_b = child_env / "hermes", child_env / "profile-b"
+ home_a.mkdir(exist_ok=True)
+ home_b.mkdir()
+ name = "ACME_CHAT_SIGNING_SECRET" # read by a user adapter; no OPTIONAL_ENV_VARS entry
+ monkeypatch.setenv(name, "fake-secret")
+ monkeypatch.setenv("MY_APP_KEY", "operator")
+ scope_a = platform_registry.current_scope_key()
+ # Registered after the policy module was imported, in profile A only.
+ platform_registry.register(PlatformEntry(name="acme-chat", label="Acme", adapter_factory=lambda c: None,
+ check_fn=lambda: True), scope=scope_a)
+
+ def child_sees():
+ return observe_child(local.hermes_subprocess_env(), [name, "MY_APP_KEY"])
+
+ try:
+ assert child_sees() == {name: None, "MY_APP_KEY": "operator"}
+ register_env_passthrough([name])
+ assert not is_env_passthrough(name)
+ monkeypatch.setenv("HERMES_HOME", str(home_b)) # B has no such adapter: operator-owned
+ assert child_sees() == {name: "fake-secret", "MY_APP_KEY": "operator"}
+ monkeypatch.setenv("HERMES_HOME", str(home_a))
+ assert child_sees() == {name: None, "MY_APP_KEY": "operator"}
+ finally:
+ platform_registry.unregister("acme-chat", scope=scope_a)
+ assert child_sees() == {name: "fake-secret", "MY_APP_KEY": "operator"}
+
+
@pytest.mark.parametrize("builder", ["foreground", "background", "factory", "nonterminal"])
def test_builders_strip_runtime_markers_and_owned_paths(child_env, monkeypatch, builder):
repo, site = Path(__file__).resolve().parents[2], _running_site()
diff --git a/tools/environments/local.py b/tools/environments/local.py
index 42791325ff9..e2edb04324e 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -22,8 +22,8 @@ from tools.environments.base_output import _pipe_stdin
from hermes_cli._subprocess_compat import windows_hide_flags
from tools.environments.local_env_policy import ( # noqa: F401 — _HERMES_PROVIDER_ENV_BLOCKLIST stays importable from here
_ALWAYS_STRIP_KEYS, _HERMES_PROVIDER_ENV_BLOCKLIST, _HERMES_PROVIDER_ENV_FORCE_PREFIX,
- _is_hermes_internal_secret, _is_provider_env_blocklisted, _is_terminal_first_party_env,
- _matches_terminal_first_party_prefix, _plugin_terminal_env_strip_keys, strip_profile_gate_env)
+ _is_hermes_internal_secret, _is_platform_secret_env, _is_provider_env_blocklisted,
+ _is_terminal_first_party_env, _matches_terminal_first_party_prefix, _plugin_terminal_env_strip_keys, strip_profile_gate_env)
from tools.environments.local_pythonpath import (
_build_hermes_repo_root_aliases, _strip_hermes_owned_pythonpath_and_runtime_markers)
@@ -334,7 +334,8 @@ def _scrub_credentials(env: dict, *, inherit_credentials: bool) -> dict:
# itself is case-insensitive, so a lowercase-stored ``gh_token`` IS GH_TOKEN.
strip_folded = frozenset(k.upper() for k in (_ALWAYS_STRIP_KEYS | _plugin_terminal_env_strip_keys()))
for key in list(env):
- if (key.upper() in strip_folded
+ # Adapter secrets are Tier 1 like the bot tokens above: no provider-key child needs them.
+ if (key.upper() in strip_folded or _is_platform_secret_env(key.upper())
or (not inherit_credentials and _is_provider_env_blocklisted(key))
or key.startswith(_HERMES_PROVIDER_ENV_FORCE_PREFIX)
or _is_hermes_internal_secret(key)):
diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.py
index 940f29739f9..5fd4e41131b 100644
--- a/tools/environments/local_env_policy.py
+++ b/tools/environments/local_env_policy.py
@@ -61,9 +61,11 @@ def _build_provider_env_blocklist() -> frozenset:
pass
try:
# The registry mirror copies env_vars only for api_key profiles, but OAuth
- # profiles (nous, qwen-oauth) also accept a pasted key under theirs.
- from providers import list_providers
- for profile in list_providers():
+ # profiles (nous, qwen-oauth) also accept a pasted key under theirs. Bundled
+ # profiles only: this frozenset is process-wide, and a home's own provider
+ # plugins would freeze the home bound at import into every profile's policy.
+ from providers import bundled_provider_profiles
+ for profile in bundled_provider_profiles():
blocked.update(profile.env_vars or ())
except ImportError:
pass
@@ -109,13 +111,14 @@ def _is_provider_env_blocklisted(name: str) -> bool:
# Adapters read many secrets straight from the environment without listing them in
# OPTIONAL_ENV_VARS (TELEGRAM_WEBHOOK_SECRET, WHATSAPP_CLOUD_APP_SECRET, WEIXIN_TOKEN, ...), so
# the list above never sees them. Match them by shape, like the authorization gates below: an
-# adapter prefix plus a secret suffix, so a new adapter secret needs no second edit.
+# adapter prefix plus a secret suffix, so a new adapter secret needs no second edit. The owners
+# are read per call because plugin adapters register late and per profile.
_PLATFORM_SECRET_ENV_SUFFIXES = ("_TOKEN", "_SECRET", "_PASSWORD", "_KEY")
def _is_platform_secret_env(upper: str) -> bool:
return upper.endswith(_PLATFORM_SECRET_ENV_SUFFIXES) and any(
- upper.startswith(prefix + "_") for prefix in _static_gate_env_prefixes())
+ upper.startswith(prefix + "_") for prefix in _platform_gate_env_prefixes())
# First-party platform credentials (``BUZZ_*``, driving the platform-mandated ``buzz``
# CLI) carved out of the TERMINAL scrub only (``_make_run_env``,
@@ -318,6 +321,7 @@ _ALWAYS_STRIP_KEYS: frozenset[str] = frozenset({
# enumerated here to stay stripped on the inherit_credentials=True path.
"GATEWAY_RELAY_ID", "GATEWAY_RELAY_SECRET", "GATEWAY_RELAY_DELIVERY_KEY",
"HASS_TOKEN", "EMAIL_PASSWORD", "HERMES_DASHBOARD_SESSION_TOKEN",
+ "MSGRAPH_CLIENT_SECRET", "MSGRAPH_WEBHOOK_CLIENT_STATE",
# Remote-compute / infrastructure secrets
"MODAL_TOKEN_ID", "MODAL_TOKEN_SECRET", "DAYTONA_API_KEY",
})