Skip to content

The agent that grows with you

fix(profile-scope): stamp profile_home only for FOREIGN homes; cache the gate-prefix scan

teknium1b88b3de6e3ee
diff --git a/hermes_cli/kanban_db_dispatch.py b/hermes_cli/kanban_db_dispatch.py
index 8d6d74fb670..7b71be5569e 100644
--- a/hermes_cli/kanban_db_dispatch.py
+++ b/hermes_cli/kanban_db_dispatch.py
@@ -2604,7 +2604,7 @@ def _worker_profile_scope(hermes_home: str, *, bind_home: bool = True):
home_token = set_hermes_home_override(str(home)) if bind_home else None
secret_token = set_secret_scope(
launch_secret_scope(home) if is_launch_home else build_profile_secret_scope(home),
- profile_home=str(home))
+ profile_home=None if is_launch_home else str(home))
terminal_token = install_profile_terminal_scope(
home, env_overlay=launch_terminal_env() if is_launch_home else None) if bind_home else None
yield
diff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.py
index d4a7bbcd895..c85aa79b735 100644
--- a/tools/environments/local_env_policy.py
+++ b/tools/environments/local_env_policy.py
@@ -2,7 +2,9 @@
names are Hermes-managed credentials. The env *builders* applying it (``_make_run_env``,
``_sanitize_subprocess_env``, ``hermes_subprocess_env``) live in ``tools.environments.local``."""
+import functools
import os
+from typing import Optional
# Prefix a caller uses in ``extra_env`` to force a blocklisted var through.
_HERMES_PROVIDER_ENV_FORCE_PREFIX = "_HERMES_FORCE_"
@@ -214,10 +216,10 @@ _PROFILE_GATE_ENV_MARKERS = (
_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."""
[email protected]_cache(maxsize=1)
+def _static_gate_env_prefixes() -> frozenset:
+ """Built-in ``Platform`` values plus bundled platform plugins (directory names and manifest
+ aliases) — fixed for the life of the process, so scanned once."""
names = set(_EXTRA_GATE_ENV_PREFIXES)
try:
from gateway.config import Platform
@@ -227,15 +229,23 @@ def _platform_gate_env_prefixes() -> frozenset:
names.update(aliases)
except Exception: # noqa: BLE001 — a broken gateway import must not disable the gate strip
pass
+ return frozenset(str(n).upper().replace("-", "_") for n in names if n)
+
+
+def _platform_gate_env_prefixes() -> frozenset:
+ """Upper-cased owners of authorization gates: the static set plus runtime-registered plugin
+ adapters (``platform_registry``, dynamic and profile-scoped, so read per call — a cheap set
+ union under the registry lock)."""
+ names = set(_static_gate_env_prefixes())
try:
from gateway.platform_registry import platform_registry
- names.update(platform_registry.registered_names())
+ names.update(str(n).upper().replace("-", "_") for n in platform_registry.registered_names() if n)
except Exception: # noqa: BLE001
pass
- return frozenset(str(n).upper().replace("-", "_") for n in names if n)
+ return frozenset(names)
-def is_profile_gate_env(name: str) -> bool:
+def is_profile_gate_env(name: str, _prefixes: Optional[frozenset] = None) -> 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. A gate is a platform prefix AND a gate-shaped suffix;
@@ -245,12 +255,14 @@ def is_profile_gate_env(name: str) -> bool:
return False
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())
+ prefixes = _prefixes if _prefixes is not None else _platform_gate_env_prefixes()
+ return any(upper.startswith(prefix + "_") for prefix in prefixes)
def strip_profile_gate_env(env: dict) -> dict:
"""Drop every authorization gate from *env* in place (see :func:`is_profile_gate_env`)."""
- for key in [k for k in env if is_profile_gate_env(k)]:
+ prefixes = _platform_gate_env_prefixes()
+ for key in [k for k in env if is_profile_gate_env(k, prefixes)]:
del env[key]
return env
diff --git a/tui_gateway/launch_profile_policy.py b/tui_gateway/launch_profile_policy.py
index e984d4486fc..5b2213e27e2 100644
--- a/tui_gateway/launch_profile_policy.py
+++ b/tui_gateway/launch_profile_policy.py
@@ -169,7 +169,7 @@ def launch_profile_runtime_scope(launch_home: "str | Path") -> Iterator[None]:
home_token = secret_token = terminal_token = None
try:
home_token = set_hermes_home_override(str(home))
- secret_token = set_secret_scope(launch_secret_scope(home), profile_home=str(home))
+ secret_token = set_secret_scope(launch_secret_scope(home)) # own home: no foreign stamp
terminal_token = install_profile_terminal_scope(home, env_overlay=launch_terminal_env())
yield
finally:
diff --git a/tui_gateway/model_switch.py b/tui_gateway/model_switch.py
index 01c6f766945..9a9668a8b3a 100644
--- a/tui_gateway/model_switch.py
+++ b/tui_gateway/model_switch.py
@@ -86,13 +86,15 @@ def _profile_runtime_scope_tokens(profile_home, *, hydrate_secrets: bool = True)
from tui_gateway.launch_profile_policy import launch_secret_scope, launch_terminal_env
home = Path(_hermes_home)
secrets = launch_secret_scope(home)
- scopes.secret = set_secret_scope(secrets, profile_home=str(home))
+ # No home stamp: this IS the process's own profile, and the stamp exists only to
+ # mark a FOREIGN home for serves_routed_profile().
+ scopes.secret = set_secret_scope(secrets)
if not is_multiplex_active():
return scopes
scopes.home = set_hermes_home_override(str(home))
overlay = launch_terminal_env()
if scopes.secret is None:
- scopes.secret = set_secret_scope(secrets, profile_home=str(home))
+ scopes.secret = set_secret_scope(secrets, profile_home=str(home) if profile_home else None)
# Same terminal policy the gateway binds per turn: a docker-configured profile
# must never resolve the launch process's pinned env. Failure → refusal scope.
from tools.terminal_scope import install_profile_terminal_scope