The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(providers): seed_pool_credentials takes plain values; hosted ambient tokens reach the host
Teknium99cf3a635855
diff --git a/agent/credential_pool.py b/agent/credential_pool.pyindex ffc727bd0e5..cebe55908e3 100644--- a/agent/credential_pool.py+++ b/agent/credential_pool.py@@ -2637,12 +2637,12 @@ def _seed_profile_singleton(seed: _Seeder) -> None:validates or exchanges. The profile gates its own work on suppression and explicit config."""from providers import get_provider_profileprofile = get_provider_profile(seed.provider)- if profile is None:+ if profile is None or not _profile_seeds_pool(seed.provider):return- from hermes_cli.auth import is_provider_explicitly_configured- rows = profile.seed_pool_credentials(- is_suppressed=lambda source: seed.is_suppressed(seed.provider, source),- explicitly_configured=lambda: is_provider_explicitly_configured(seed.provider))+ from hermes_cli.auth import is_provider_explicitly_configured, suppressed_credential_sources+ rows = profile.seed_pool_credentials( # values, not callbacks: a hosted profile gets plain JSON+ suppressed_sources=suppressed_credential_sources(seed.provider),+ explicitly_configured=is_provider_explicitly_configured(seed.provider))pconfig = PROVIDER_REGISTRY.get(seed.provider)for row in rows or ():seed.upsert(row["source"], {diff --git a/hermes_cli/auth.py b/hermes_cli/auth.pyindex b18ab1959f8..d11b068f766 100644--- a/hermes_cli/auth.py+++ b/hermes_cli/auth.py@@ -1155,6 +1155,16 @@ def is_source_suppressed(provider_id: str, source: str) -> bool:return False+def suppressed_credential_sources(provider_id: str) -> frozenset[str]:+ """Every credential source the user suppressed for *provider_id* (unreadable store = none)."""+ try:+ section = _load_auth_store().get("suppressed_sources")+ except OSError: # unreadable store (logged by the loader): nothing to honour, as before+ return frozenset()+ listed = section.get(provider_id) if isinstance(section, dict) else None+ return frozenset(map(str, listed)) if isinstance(listed, (list, dict)) else frozenset()++def unsuppress_credential_source(provider_id: str, source: str) -> bool:"""Clear a suppression marker so the source will be re-seeded on the next load."""with _auth_store_lock():diff --git a/hermes_cli/plugin_host.py b/hermes_cli/plugin_host.pyindex 5ba92a76541..b4687026a88 100644--- a/hermes_cli/plugin_host.py+++ b/hermes_cli/plugin_host.py@@ -52,6 +52,23 @@ _RESTART_BUDGET = 3_RESTART_WINDOW_SECS = 600.0+def _hosted_ambient_credentials(home: Path) -> dict[str, str]:+ """``env_vars`` of this home's hosted ``ambient_credentials`` providers (``GH_TOKEN``-style names the+ child-env scrub always strips), read the way that provider reads them in-process: ``get_secret``+ under the home's scope, so the launch home keeps its shell value and a routed home never gets it."""+ from agent.secret_scope import build_profile_secret_scope, get_secret, reset_secret_scope, set_secret_scope+ from hermes_cli.plugin_host_profiles import is_hosted_profile+ from providers import list_providers+ names = {name for p in list_providers() if p.ambient_credentials and is_hosted_profile(p) for name in p.env_vars}+ if not names:+ return {}+ token = set_secret_scope(build_profile_secret_scope(home), profile_home=str(home))+ try:+ return {name: value for name in sorted(names) if (value := get_secret(name))}+ finally:+ reset_secret_scope(token)++class PluginHost:"""One host process for one :class:`~hermes_cli.plugins.PluginManager` (one profile home)."""@@ -110,6 +127,7 @@ class PluginHost:def _env(self) -> dict[str, str]:from tools.environments.local import served_profile_child_envenv = self._base_context.run(served_profile_child_env, target_home=self._home, inherit_credentials=True)+ env.update(self._base_context.run(_hosted_ambient_credentials, self._home))repo_root = str(Path(__file__).resolve().parents[1])env["PYTHONPATH"] = os.pathsep.join(p for p in (repo_root, env.get("PYTHONPATH", "")) if p)env.setdefault("HERMES_PLUGIN_HOST_LOG_LEVEL", "WARNING")diff --git a/hermes_cli/plugin_host_profiles.py b/hermes_cli/plugin_host_profiles.pyindex 27791f3f51d..9118bc4da6d 100644--- a/hermes_cli/plugin_host_profiles.py+++ b/hermes_cli/plugin_host_profiles.py@@ -44,6 +44,11 @@ def load_hosted_profiles(plugin_dir: Path, module_name: str) -> tuple[list[Any],return hosted, in_process+def is_hosted_profile(profile: Any) -> bool:+ """True for a profile proxy whose code runs in the plugin host."""+ return type(profile).__module__ == __name__++def _fingerprint(plugin_dir: Path) -> str:digest = hashlib.sha256(f"{_CACHE_VERSION}|{sys.executable}".encode())try:diff --git a/providers/base.py b/providers/base.pyindex 254d3bd8419..48e0418db21 100644--- a/providers/base.py+++ b/providers/base.py@@ -631,11 +631,12 @@ class ProviderProfile:"""Turn the selected pool row's secret into the runtime key (may update ``entry``); default as-is."""return api_key- def seed_pool_credentials(self, *, is_suppressed: Callable[[str], bool],- explicitly_configured: Callable[[], bool]) -> list[dict] | None:- """Pool rows ``{source, access_token, base_url, label}`` this provider seeds itself. Overriding it- replaces generic env seeding (that loop would overwrite exchanged tokens with the raw env value),- so an override returns ``[]`` for none; only the base implementation keeps generic env seeding."""+ def seed_pool_credentials(self, *, suppressed_sources: frozenset[str],+ explicitly_configured: bool) -> list[dict] | None:+ """Pool rows ``{source, access_token, base_url, label}`` this provider seeds itself; skip any source in+ ``suppressed_sources`` (``hermes auth remove``). Plain data, so it crosses ``plugins.isolation: host``.+ Overriding it replaces generic env seeding (that loop would overwrite exchanged tokens with the raw env+ value), so an override returns ``[]`` for none; only the base implementation keeps generic env seeding."""return Nonedef remove_credential_source(self, source: str) -> tuple[tuple[str, ...], tuple[str, ...]] | None:diff --git a/tests/agent/test_provider_credential_seams.py b/tests/agent/test_provider_credential_seams.pyindex 4037b3e5c75..657b1efdea5 100644--- a/tests/agent/test_provider_credential_seams.py+++ b/tests/agent/test_provider_credential_seams.py@@ -11,8 +11,8 @@ from utils import base_url_host_matchesclass _OwnCreds(ProviderProfile):- def seed_pool_credentials(self, *, is_suppressed, explicitly_configured):- return [] if is_suppressed("env:ACME_TOKEN") else [+ def seed_pool_credentials(self, *, suppressed_sources, explicitly_configured):+ return [] if "env:ACME_TOKEN" in suppressed_sources else [{"source": "env:ACME_TOKEN", "access_token": "minted", "base_url": None, "label": "ACME_TOKEN"}]def remove_credential_source(self, source):diff --git a/tests/hermes_cli/test_plugin_host_seeded_credentials.py b/tests/hermes_cli/test_plugin_host_seeded_credentials.pynew file mode 100644index 00000000000..ee1ec294fbb--- /dev/null+++ b/tests/hermes_cli/test_plugin_host_seeded_credentials.py@@ -0,0 +1,107 @@+"""plugins.isolation: host — a hosted provider that seeds its own pool rows gets plain data and the+ambient token it declares, exactly as it would in-process."""++import os+import sys++import hermes_yaml as yaml+import pytest++from hermes_cli import plugins as plugins_mod++SEEDING_PLUGIN = '''+import os+from providers import register_provider+from providers.base import ProviderProfile++class Seeds(ProviderProfile):+ def seed_pool_credentials(self, *, suppressed_sources, explicitly_configured):+ token = os.environ.get("GH_TOKEN", "")+ if not token or "env:GH_TOKEN" in suppressed_sources:+ return []+ return [{"source": "env:GH_TOKEN", "access_token": f"minted-{token}-{explicitly_configured}",+ "base_url": None, "label": f"pid {os.getpid()}"}]++register_provider(Seeds(name="seedhost", base_url="https://seedhost.example/v1",+ env_vars=("GH_TOKEN",), ambient_credentials=True))+'''+++def _install(tmp_path, monkeypatch):+ home = tmp_path / ".hermes"+ plugin_dir = home / "plugins" / "model-providers" / "seedhost"+ plugin_dir.mkdir(parents=True)+ (plugin_dir / "plugin.yaml").write_text("name: seedhost\nkind: model-provider\n", encoding="utf-8")+ (plugin_dir / "__init__.py").write_text(SEEDING_PLUGIN, encoding="utf-8")+ (home / "config.yaml").write_text(yaml.safe_dump({"plugins": {"isolation": "host"}}), encoding="utf-8")+ monkeypatch.setenv("HOME", str(tmp_path / "os-home"))+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setenv("GH_TOKEN", "shell-only") # exported in the shell, absent from .env+++def _shutdown():+ host = getattr(plugins_mod.get_plugin_manager(), "_plugin_host_instance", None)+ if host is not None:+ host.shutdown()+ for name in [m for m in sys.modules if m.endswith("_seedhost")]:+ sys.modules.pop(name, None)++[email protected]("any") # the host is a child process: its env/home resolution is per-OS+def test_hosted_profile_seeds_the_pool_from_a_shell_exported_ambient_token(tmp_path, monkeypatch):+ import providers+ from agent.credential_pool import load_pool++ _install(tmp_path, monkeypatch)+ try:+ profile = providers.get_provider_profile("seedhost")+ assert type(profile).__module__ == "hermes_cli.plugin_host_profiles"+ rows = [(e.source, e.access_token, e.label) for e in load_pool("seedhost").entries()]+ assert rows == [("env:GH_TOKEN", "minted-shell-only-True", rows[0][2])]+ assert rows[0][2] != f"pid {os.getpid()}" # seeded by the plugin host, not in-process+ finally:+ _shutdown()++[email protected]("any")+def test_hosted_profile_skips_a_suppressed_source(tmp_path, monkeypatch):+ from agent.credential_pool import load_pool+ from hermes_cli.auth import suppress_credential_source++ _install(tmp_path, monkeypatch)+ suppress_credential_source("seedhost", "env:GH_TOKEN")+ try:+ assert load_pool("seedhost").entries() == []+ finally:+ _shutdown()+++def test_a_routed_home_host_never_inherits_the_launch_shell_token(tmp_path, monkeypatch):+ import providers+ from hermes_cli.plugin_host import _hosted_ambient_credentials+ from hermes_cli.plugin_host_profiles import _profile_proxy+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override+ from providers.base import ProviderProfile++ launch, coder, writer = tmp_path / "launch", tmp_path / "coder", tmp_path / "writer"+ for home in (launch, coder, writer):+ home.mkdir()+ (writer / ".env").write_text("GH_TOKEN=writer-own\n", encoding="utf-8")+ monkeypatch.setenv("HERMES_HOME", str(launch))+ monkeypatch.setenv("GH_TOKEN", "launch-shell")+ hosted = _profile_proxy(ProviderProfile, str(tmp_path), "m", {+ "name": "amb", "fields": {"env_vars": ["GH_TOKEN"], "ambient_credentials": True}})+ monkeypatch.setattr(providers, "list_providers", lambda: [hosted, ProviderProfile(+ name="inproc", env_vars=("GITHUB_TOKEN",), ambient_credentials=True)])+ monkeypatch.setenv("GITHUB_TOKEN", "never-for-an-in-process-profile")++ def env_for(home):+ token = set_hermes_home_override(home)+ try:+ return _hosted_ambient_credentials(home)+ finally:+ reset_hermes_home_override(token)++ assert env_for(launch) == {"GH_TOKEN": "launch-shell"}+ assert env_for(coder) == {}+ assert env_for(writer) == {"GH_TOKEN": "writer-own"}