Skip to content

The agent that grows with you

fix(skills): make env_passthrough registrations visible across tool context snapshots

liuhao10243c4a46def3cc
diff --git a/tests/tools/test_env_passthrough.py b/tests/tools/test_env_passthrough.py
index b676324edf7..e440ec13406 100644
--- a/tests/tools/test_env_passthrough.py
+++ b/tests/tools/test_env_passthrough.py
@@ -36,6 +36,56 @@ class TestSkillScopedPassthrough:
assert not is_env_passthrough("")
+class TestRegistrationVisibleAcrossToolContextSnapshots:
+ """Skill registration must survive the tool-dispatch context snapshot.
+
+ Tool dispatch fans each tool call onto a worker whose context is a
+ copy_context() snapshot taken at submit time
+ (tools.thread_context.propagate_context_to_thread). A registration made
+ inside one tool's worker (skill_view) used to be stored in a ContextVar
+ and therefore never reached the submitting thread — the next tool's
+ snapshot always saw an empty allowlist, so a skill's declared env vars
+ never passed through to execute_code/terminal (#90004).
+ """
+
+ def test_registration_in_copied_context_is_visible_to_sibling_snapshot(self):
+ import contextvars
+
+ def _scenario():
+ # The submitting thread has NEVER touched the allowlist var
+ # (mirrors a fresh gateway turn where only tools register). Each
+ # worker snapshot therefore starts without a set of its own.
+ tool_a_ctx = contextvars.copy_context()
+ tool_a_ctx.run(
+ lambda: register_env_passthrough(["NOTION_API_KEY"])
+ )
+ # The NEXT tool's submit-time snapshot must still see it.
+ tool_b_ctx = contextvars.copy_context()
+ return tool_b_ctx.run(
+ lambda: is_env_passthrough("NOTION_API_KEY")
+ )
+
+ # Run the whole scenario inside a pristine context so the module's
+ # (test-fixture) current-context initialization cannot leak in.
+ pristine = contextvars.Context()
+ visible = pristine.run(_scenario)
+ assert visible is True
+
+ def test_registration_in_worker_thread_is_visible_to_main_thread(self):
+ import concurrent.futures
+ import contextvars
+
+ with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
+ # Mirror tool_executor's propagate_context_to_thread snapshot:
+ # the worker runs inside a copy of the submitting context.
+ parent_ctx = contextvars.copy_context()
+ future = pool.submit(
+ parent_ctx.run, register_env_passthrough, ["AIRTABLE_API_KEY"]
+ )
+ future.result()
+ assert is_env_passthrough("AIRTABLE_API_KEY") is True
+
+
class TestConfigPassthrough:
def test_reads_from_config(self, tmp_path, monkeypatch):
config = {"terminal": {"env_passthrough": ["MY_CUSTOM_KEY", "ANOTHER_TOKEN"]}}
diff --git a/tools/env_passthrough.py b/tools/env_passthrough.py
index 48a1f11ad94..59212a655c7 100644
--- a/tools/env_passthrough.py
+++ b/tools/env_passthrough.py
@@ -7,25 +7,34 @@ forwarded values resolve through the profile's secret scope, not the process env
from __future__ import annotations
import logging
-from contextvars import ContextVar
from typing import Iterable
from hermes_cli.config import cfg_get, read_raw_config
logger = logging.getLogger(__name__)
-# Session-scoped allowlist; ContextVar-backed to prevent cross-session bleed
-# in the gateway pipeline.
-_allowed_env_vars_var: ContextVar[set[str]] = ContextVar("_allowed_env_vars")
+# Process-wide set of env var names registered by skills for sandbox
+# passthrough. Deliberately NOT a ContextVar: tool dispatch fans each tool
+# call onto a worker whose context is a copy_context() snapshot taken at
+# submit time (tools.thread_context.propagate_context_to_thread), so a
+# registration made inside one tool's worker (skill_view calling
+# register_env_passthrough) never reaches the submitting thread's context —
+# every subsequent tool (execute_code, terminal) re-snapshots the original
+# context and sees an empty allowlist, and the skill's declared env vars
+# never pass through (#90004). The config-based allowlist below is already
+# a module-level global with exactly the process-wide visibility the skill
+# path needs to match.
+#
+# Cross-session exposure is limited to the NAMES: the values still resolve
+# per profile through resolve_passthrough_value's secret_scope, so a name
+# registered by one session cannot read another profile's secret. The
+# previous ContextVar also never actually isolated anything within one
+# process running a single profile (the common deployment).
+_allowed_env_vars: set[str] = set()
def _get_allowed() -> set[str]:
- """Get or create the allowed env vars set for the current context/session."""
- try:
- return _allowed_env_vars_var.get()
- except LookupError:
- val: set[str] = set()
- _allowed_env_vars_var.set(val)
- return val
+ """Get the process-wide skill passthrough allowlist."""
+ return _allowed_env_vars
# Config-based allowlist, keyed by Hermes home: under gateway.multiplex_profiles one process serves
@@ -174,5 +183,8 @@ def scoped_passthrough_additions(present: Iterable[str]) -> dict[str, str]:
def clear_env_passthrough() -> None:
- """Reset the skill-scoped allowlist (e.g. on session reset)."""
+ """Reset the skill-registered allowlist (e.g. on session reset).
+
+ Clears the process-wide set; a later ``skill_view`` re-registers its
+ vars on demand, so recovery is a single skill load."""
_get_allowed().clear()