Skip to content

The agent that grows with you

fix(gateway): revoke stale profile secret snapshots

fangliquanflq4a927e9a6e37
diff --git a/hermes_cli/env_loader.py b/hermes_cli/env_loader.py
index 6cb02dfe994..2f2f895eb54 100644
--- a/hermes_cli/env_loader.py
+++ b/hermes_cli/env_loader.py
@@ -101,6 +101,10 @@ def _hydrate_profile_secret_sources(home: Path) -> dict[str, str]:
if home_key in _APPLIED_HOMES:
return get_secret_source_values(home)
+ # A retry must not keep serving a partial result after the source is removed, disabled, or can no
+ # longer be evaluated. Publish only the snapshot established by this attempt.
+ _SECRET_SOURCE_VALUES_BY_HOME.pop(home_key, None)
+
try:
cfg = _load_secrets_config(home)
except Exception: # noqa: BLE001 — external sources must not block routing
diff --git a/tests/agent/test_env_loader_secret_sources.py b/tests/agent/test_env_loader_secret_sources.py
index b21ed48e53c..550d2d7a256 100644
--- a/tests/agent/test_env_loader_secret_sources.py
+++ b/tests/agent/test_env_loader_secret_sources.py
@@ -435,6 +435,57 @@ def test_cold_profile_hydration_replaces_partial_snapshot_after_failed_retry(
assert attempts == 2
+def test_cold_profile_hydration_clears_partial_snapshot_when_sources_are_removed(
+ tmp_path, monkeypatch
+):
+ """Removing secret sources during a retry must revoke values from a partial snapshot."""
+ from agent.secret_scope import build_profile_secret_scope
+ 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
+
+ config_path = tmp_path / "config.yaml"
+ config_path.write_text(
+ "secrets:\n command:\n enabled: true\n", encoding="utf-8"
+ )
+ failed = FetchResult().fail("helper exited 1", ErrorKind.AUTH_FAILED)
+
+ def _apply_partial_result(_cfg, _home_path, environ=None):
+ environ["OPENAI_API_KEY"] = "partial-key"
+ return ApplyReport(
+ sources=[
+ SourceReport(
+ name="onepassword",
+ label="1Password",
+ result=FetchResult(secrets={"OPENAI_API_KEY": "partial-key"}),
+ applied=["OPENAI_API_KEY"],
+ ),
+ SourceReport(name="command", label="command", result=failed),
+ ],
+ provenance={
+ "OPENAI_API_KEY": AppliedVar(
+ name="OPENAI_API_KEY",
+ source="onepassword",
+ shape="bulk",
+ overrode_env=False,
+ )
+ },
+ )
+
+ monkeypatch.setattr(reg_module, "apply_all", _apply_partial_result)
+
+ assert env_loader.hydrate_profile_secret_sources(tmp_path) == {
+ "OPENAI_API_KEY": "partial-key"
+ }
+ assert build_profile_secret_scope(tmp_path)["OPENAI_API_KEY"] == "partial-key"
+
+ config_path.write_text("{}\n", encoding="utf-8")
+
+ assert env_loader.hydrate_profile_secret_sources(tmp_path) == {}
+ assert env_loader.get_secret_source_values(tmp_path) == {}
+ assert "OPENAI_API_KEY" not in build_profile_secret_scope(tmp_path)
+
+
def test_apply_external_secret_sources_noop_when_disabled(tmp_path, monkeypatch):
"""Disabled Bitwarden config must not touch the source map."""