Skip to content

The agent that grows with you

fix(env-policy): a profile gate is a platform prefix plus a gate suffix, not a bare name shape

teknium1ebb9bc56999e
diff --git a/tests/tools/test_profile_gate_env_shape.py b/tests/tools/test_profile_gate_env_shape.py
new file mode 100644
index 00000000000..2010c1cdc4a
--- /dev/null
+++ b/tests/tools/test_profile_gate_env_shape.py
@@ -0,0 +1,43 @@
+"""A profile gate is a PLATFORM prefix plus a gate-shaped suffix; an operator's own variable that
+merely contains ``_ALLOWED_`` is script data and must survive the routed-child strip (#119539)."""
+
+import os
+
+from tools.environments.local import build_subprocess_env
+from tools.environments.local_env_policy import is_profile_gate_env
+
+
+def test_operator_allowlist_shaped_vars_are_not_gates_but_platform_gates_are():
+ # Same suffix shape on both sides — only the owner prefix decides.
+ assert not is_profile_gate_env("DEMO_ALLOWED_SENDER")
+ assert not is_profile_gate_env("ACCESS_ALLOWED_CALLBACK_ACE_TYPE")
+ assert not is_profile_gate_env("HERMES_MEDIA_ALLOW_DIRS")
+ for gate in ("DISCORD_ALLOWED_CHANNELS", "TELEGRAM_GROUP_ALLOWED_CHATS", "GATEWAY_ALLOW_ALL_USERS",
+ "WHATSAPP_GROUP_ALLOW_FROM", "QQ_GROUP_ALLOWED_USERS", "IRC_ALLOW_ALL_USERS"):
+ assert is_profile_gate_env(gate), gate
+
+
+def test_routed_no_agent_script_env_keeps_operator_allowlist_and_drops_platform_gate(tmp_path, monkeypatch):
+ """The cron ``no_agent`` spawn seam: a child built for ANOTHER profile drops Hermes gates but
+ keeps the operator's script data, whatever its name looks like."""
+ from agent.secret_scope import build_profile_secret_scope, reset_secret_scope, set_secret_scope
+ from hermes_constants import reset_hermes_home_override, set_hermes_home_override
+
+ launch = tmp_path / ".hermes"
+ other = launch / "profiles" / "other"
+ other.mkdir(parents=True)
+ (other / ".env").write_text("OTHER_MARKER=1\n", encoding="utf-8")
+ monkeypatch.setenv("HERMES_HOME", str(launch))
+ monkeypatch.setenv("DEMO_ALLOWED_SENDER", "[email protected]")
+ monkeypatch.setenv("DISCORD_ALLOWED_CHANNELS", "111")
+
+ ht = set_hermes_home_override(str(other))
+ st = set_secret_scope(build_profile_secret_scope(other), profile_home=str(other))
+ try:
+ env = build_subprocess_env(strip_launch_profile=True)
+ finally:
+ reset_secret_scope(st)
+ reset_hermes_home_override(ht)
+ assert env.get("DEMO_ALLOWED_SENDER") == "[email protected]"
+ assert "DISCORD_ALLOWED_CHANNELS" not in env
+ assert os.environ["DISCORD_ALLOWED_CHANNELS"] == "111" # the strip never mutates the process env
diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.py
index fdcad79f5b9..d4a7bbcd895 100644
--- a/tools/environments/local_env_policy.py
+++ b/tools/environments/local_env_policy.py
@@ -199,23 +199,53 @@ def _is_hermes_internal_secret(key: str) -> bool:
# no secret scrub touches them, and most profiles' ``.env`` files do not define them, so the
# child's own dotenv load never overwrites an inherited value: a child spawned FOR profile B
# from a process that loaded profile A's gates (or a unit-file ``Environment=``) would enforce
-# A's channel/user/role list as its own (#113270). Matched by shape so a gate added to any
-# adapter is covered without a second edit; ``HERMES_*`` never counts (``HERMES_MEDIA_ALLOW_DIRS``,
+# A's channel/user/role list as its own (#113270). The suffix is matched by shape so a gate
+# added to any adapter is covered without a second edit, but ONLY under a platform prefix
+# (``DISCORD_``, ``GATEWAY_``, a plugin adapter's name): an operator's own ``DEMO_ALLOWED_SENDER``
+# is script data, not a Hermes gate, and deleting it by name shape broke routed ``no_agent``
+# cron scripts (#119539). ``HERMES_*`` never counts (``HERMES_MEDIA_ALLOW_DIRS``,
# ``HERMES_ALLOW_PRIVATE_URLS`` are process settings, not adapter gates).
_PROFILE_GATE_ENV_MARKERS = (
"_ALLOWED_", "_ALLOW_ALL_", "_ALLOW_FROM", "_ALLOW_BOTS", "_ALLOW_PUBLIC_", "_IGNORED_CHANNELS",
"_NO_THREAD_CHANNELS", "_FREE_RESPONSE_CHANNELS", "_BACKFILL_CHANNELS", "_GROUP_ALLOWED",
)
+# Gate owners that are not a ``Platform`` value: the cross-platform pairing gate and adapters whose
+# env prefix differs from their platform name (``qqbot`` reads ``QQ_*``).
+_EXTRA_GATE_ENV_PREFIXES = frozenset({"GATEWAY", "QQ"})
+
+
+def _platform_gate_env_prefixes() -> frozenset:
+ """Upper-cased owners of authorization gates: built-in platforms, bundled platform plugins and
+ runtime-registered plugin adapters (``platform_registry``), plus :data:`_EXTRA_GATE_ENV_PREFIXES`.
+ Resolved per call — plugin registration is dynamic and profile-scoped."""
+ names = set(_EXTRA_GATE_ENV_PREFIXES)
+ try:
+ from gateway.config import Platform
+ names.update(m.value for m in Platform.__members__.values())
+ bundled, aliases = Platform._scan_bundled_plugin_platforms()
+ names.update(bundled)
+ names.update(aliases)
+ except Exception: # noqa: BLE001 — a broken gateway import must not disable the gate strip
+ pass
+ try:
+ from gateway.platform_registry import platform_registry
+ names.update(platform_registry.registered_names())
+ except Exception: # noqa: BLE001
+ pass
+ return frozenset(str(n).upper().replace("-", "_") for n in names if n)
def is_profile_gate_env(name: str) -> bool:
"""True for a platform authorization gate (``DISCORD_ALLOWED_CHANNELS``, ``TELEGRAM_ALLOW_ALL_USERS``,
``GATEWAY_ALLOWED_USERS``, ``WHATSAPP_GROUP_ALLOW_FROM`` ...) — profile-scoped policy a child acting
- for ANOTHER profile must never inherit."""
+ for ANOTHER profile must never inherit. A gate is a platform prefix AND a gate-shaped suffix;
+ an operator variable that merely contains ``_ALLOWED_`` is not one."""
upper = name.upper()
if upper.startswith("HERMES_") or upper.startswith("_"):
return False
- return any(marker in upper for marker in _PROFILE_GATE_ENV_MARKERS)
+ if not any(marker in upper for marker in _PROFILE_GATE_ENV_MARKERS):
+ return False
+ return any(upper.startswith(prefix + "_") for prefix in _platform_gate_env_prefixes())
def strip_profile_gate_env(env: dict) -> dict: