Skip to content

The agent that grows with you

fix(gateway): retry failed profile secret hydration

fangliquanflq917cfbd74054
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index f42501dec58..ce64064b560 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -130,7 +130,11 @@ def _hydrate_profile_secret_sources(home: Path) -> dict[str, str]:
if not report.sources:
return {}
- _APPLIED_HOMES.add(home_key)
+ # Routed profiles have no runtime reset path. Keep a failed source retryable so correcting its
+ # profile-local bootstrap credentials takes effect on the next turn; successful sources from a
+ # mixed report are still snapshotted below and can be used while the failed source recovers.
+ if all(src.result.ok for src in report.sources):
+ _APPLIED_HOMES.add(home_key)
values: dict[str, str] = {}
for name, applied in report.provenance.items():
value = local_env.get(name)
diff --git a/tests/agent/test_env_loader_secret_sources.py b/tests/agent/test_env_loader_secret_sources.py
index 3ae55b94c16..4fc47bc1153 100644
--- a/tests/agent/test_env_loader_secret_sources.py
+++ b/tests/agent/test_env_loader_secret_sources.py
@@ -325,6 +325,58 @@ def test_cold_profile_hydration_dotenv_wins_over_op_env(tmp_path, monkeypatch):
assert seen_env.get("OP_SERVICE_ACCOUNT_TOKEN") == "ops_from-dotenv"
+def test_cold_profile_hydration_retries_failed_source(tmp_path, monkeypatch):
+ """A failed routed-profile fetch must not make its empty snapshot process-lifetime state."""
+ from agent.secret_sources.base import ErrorKind, FetchResult
+ from agent.secret_sources.registry import AppliedVar, ApplyReport, SourceReport
+ from agent.secret_sources import registry as reg_module
+
+ (tmp_path / "config.yaml").write_text(
+ "secrets:\n command:\n enabled: true\n", encoding="utf-8"
+ )
+ attempts = 0
+
+ def _apply_after_credentials_are_fixed(_cfg, _home_path, environ=None):
+ nonlocal attempts
+ attempts += 1
+ if attempts == 1:
+ failed = FetchResult().fail("helper exited 1", ErrorKind.AUTH_FAILED)
+ return ApplyReport(
+ sources=[SourceReport(name="command", label="command", result=failed)]
+ )
+
+ environ["OPENAI_API_KEY"] = "recovered-key"
+ return ApplyReport(
+ sources=[
+ SourceReport(
+ name="command",
+ label="command",
+ result=FetchResult(secrets={"OPENAI_API_KEY": "recovered-key"}),
+ applied=["OPENAI_API_KEY"],
+ )
+ ],
+ provenance={
+ "OPENAI_API_KEY": AppliedVar(
+ name="OPENAI_API_KEY",
+ source="command",
+ shape="bulk",
+ overrode_env=False,
+ )
+ },
+ )
+
+ monkeypatch.setattr(reg_module, "apply_all", _apply_after_credentials_are_fixed)
+
+ assert env_loader.hydrate_profile_secret_sources(tmp_path) == {}
+ assert env_loader.hydrate_profile_secret_sources(tmp_path) == {
+ "OPENAI_API_KEY": "recovered-key"
+ }
+ assert env_loader.hydrate_profile_secret_sources(tmp_path) == {
+ "OPENAI_API_KEY": "recovered-key"
+ }
+ assert attempts == 2
+
+
def test_apply_external_secret_sources_noop_when_disabled(tmp_path, monkeypatch):
"""Disabled Bitwarden config must not touch the source map."""