Skip to content

The agent that grows with you

fix(logging): a routed profile's own log files follow its own redaction policy

John Paul Solivad9199d8e8d1a
diff --git a/agent/redact.py b/agent/redact.py
index 6e955012589..76f8da9ff5d 100644
--- a/agent/redact.py
+++ b/agent/redact.py
@@ -117,6 +117,11 @@ def _redact_enabled() -> bool:
from agent.secret_scope import current_secret_scope
scope = current_secret_scope()
raw = scope.get("HERMES_REDACT_SECRETS") if scope else None
+ if raw is None and scope is None:
+ # No live scope (the log listener thread formats routed records): read the profile's own .env, as
+ # its scope would, or a first call there would cache a config-only answer for the whole process.
+ from hermes_cli.config import load_env
+ raw = load_env().get("HERMES_REDACT_SECRETS")
if raw is None:
from hermes_cli.config import load_config_readonly
cfg_val = (load_config_readonly().get("security") or {}).get("redact_secrets")
diff --git a/hermes_logging.py b/hermes_logging.py
index 415e0c870b8..a5e8bf8a874 100644
--- a/hermes_logging.py
+++ b/hermes_logging.py
@@ -463,7 +463,19 @@ class _ProfileRoutingFileHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
try:
- self._handler_for_home(self._home_for_record(record)).handle(record)
+ home = self._home_for_record(record)
+ handler = self._handler_for_home(home)
+ if home == self._default_home:
+ handler.handle(record)
+ return
+ # Formatted here, on the listener thread, where the record's profile scope is gone: bind its home so
+ # RedactingFormatter applies THAT profile's redact_secrets policy and vault values, not the launch's.
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+ token = set_hermes_home_override(str(home))
+ try:
+ handler.handle(record)
+ finally:
+ reset_hermes_home_override(token)
except Exception:
self.handleError(record)
diff --git a/tests/test_hermes_logging.py b/tests/test_hermes_logging.py
index 7bb6b4aef1a..407515eb6a3 100644
--- a/tests/test_hermes_logging.py
+++ b/tests/test_hermes_logging.py
@@ -129,6 +129,43 @@ class TestSetupLogging:
hermes_home / "logs" / "agent.log"
).read_text()
+ @pytest.mark.parametrize("launch_redacts, routed_opt_out, routed_redacted", [
+ (False, None, True), # the launch profile opted out, the routed one did not
+ (True, "config", False), # the routed profile opted out in its config.yaml
+ (True, "env", False), # ... or in its own .env
+ ], ids=["launch-opt-out", "routed-config-opt-out", "routed-env-opt-out"])
+ def test_routed_records_follow_their_own_profiles_redaction_policy(
+ self, hermes_home, tmp_path, monkeypatch, launch_redacts, routed_opt_out, routed_redacted):
+ """The listener thread formats every record after its profile scope is gone, so a routed profile's own
+ agent.log was redacted by the LAUNCH profile's policy: raw credentials if only the launch opted out."""
+ from agent import redact
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+
+ monkeypatch.setattr(redact, "_REDACT_ENABLED", launch_redacts)
+ monkeypatch.setattr(redact, "_REDACT_ENABLED_BY_HOME", {})
+ routed = tmp_path / "profile-b"
+ routed.mkdir()
+ if routed_opt_out == "config":
+ (routed / "config.yaml").write_text("security:\n redact_secrets: false\n", encoding="utf-8")
+ elif routed_opt_out == "env":
+ (routed / ".env").write_text("HERMES_REDACT_SECRETS=false\n", encoding="utf-8")
+ hermes_logging.setup_logging(hermes_home=hermes_home)
+ assert hermes_logging.enable_profile_log_routing([hermes_home, routed]) is True
+ routed_secret = "sk-proj-ROUTEDPROFILE" + "b" * 24
+ launch_secret = "sk-proj-LAUNCHPROFILE" + "a" * 24
+
+ logger = logging.getLogger("gateway.redaction-routing-test")
+ token = set_hermes_home_override(routed)
+ try:
+ logger.warning("provider rejected key %s", routed_secret)
+ finally:
+ reset_hermes_home_override(token)
+ logger.warning("launch key %s", launch_secret)
+ hermes_logging.flush_log_queue()
+
+ assert (routed_secret not in (routed / "logs" / "agent.log").read_text()) is routed_redacted
+ assert (launch_secret not in (hermes_home / "logs" / "agent.log").read_text()) is launch_redacts
+
def test_release_profile_log_handlers_closes_only_deleted_profile(self, hermes_home, tmp_path):
"""Profile deletion releases its routed log files without disturbing another profile."""
from hermes_constants import reset_hermes_home_override, set_hermes_home_override