Skip to content

The agent that grows with you

fix(tools): unset every configured toolchain name a local client omitted

unsupportedpastelsaecea13d248a
diff --git a/agent/session_client_env.py b/agent/session_client_env.py
index ef8b38b7cdd..20b13dfc39f 100644
--- a/agent/session_client_env.py
+++ b/agent/session_client_env.py
@@ -110,10 +110,11 @@ def retry_matches(committed: dict, request: dict) -> bool:
@contextmanager
-def client_env_scope(env, *, local=True):
+def client_env_scope(env, *, local=True, rules=None):
"""Bind one session's client environment (``None``: the gateway's own) for this context; ``local``
- is the gateway's own proof, frozen in the session's policy, that the client runs on this host."""
- token = _BOUND.set(None if env is None else (env, bool(local)))
+ is the gateway's own proof, frozen in the session's policy, that the client runs on this host;
+ ``rules`` the normalisation rules frozen with it (``client_env_rules``; ``None``: the built-ins)."""
+ token = _BOUND.set(None if env is None else (env, bool(local), rules))
try:
yield
finally:
@@ -146,12 +147,16 @@ def tool_env() -> dict[str, str]:
def toolchain_environ(base=None) -> dict[str, str]:
"""The environment this session's tool subprocesses start from (``base``: the gateway's). Unbound
(messaging, cron, Desktop) or a client not attached locally: unchanged. A local shell client's
- session: its toolchain replaces the gateway's, and a built-in toolchain name it did not export is
- unset, as in main's process: one client's venv never reaches another's session through the daemon
- it started."""
+ session: its toolchain replaces the gateway's, and every toolchain name it did not export (built-in,
+ or configured for its profile when the session was created) is unset, as in main's process: one
+ client's venv or GEM_HOME never reaches another's session through the daemon it started. PATH is
+ the exception: a client that sent none runs with the gateway's."""
env = dict(os.environ if base is None else base)
bound = _BOUND.get()
if bound is None or not bound[1]:
return env
- kept = {k: v for k, v in env.items() if k == 'PATH' or not (k in TOOL_ENV or k.startswith(TOOL_ENV_PREFIXES))}
+ rules = bound[2] or {'names': TOOL_ENV, 'prefixes': TOOL_ENV_PREFIXES, 'secret': None}
+ names = {name for name in rules['names'] if _travels(name, rules['secret'])}
+ prefixes = tuple(rules['prefixes'])
+ kept = {k: v for k, v in env.items() if k == 'PATH' or not (k in names or k.startswith(prefixes))}
return kept | tool_env()
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index 432f2ccf5f5..a37c4e65123 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -82,7 +82,8 @@ class LocalSessionPolicy:
# ticket): only host-free values were kept, and its tools run with the gateway's own toolchain.
client_env_remote: bool = False
# The rules that map was normalised by (``client_env_rules``: allowlist, secret filter, locality) as
- # plain data: an exact retry is judged by them, never by the profile's current ones.
+ # plain data: an exact retry is judged by them, never by the profile's current ones, and the session's
+ # tools run without every toolchain name they cover that the client did not send.
client_env_rules_json: str | None = None
def client_env(self):
@@ -90,7 +91,8 @@ class LocalSessionPolicy:
def client_env_scope(self):
from agent.session_client_env import client_env_scope
- return client_env_scope(self.client_env(), local=not self.client_env_remote)
+ rules = None if self.client_env_rules_json is None else json.loads(self.client_env_rules_json)
+ return client_env_scope(self.client_env(), local=not self.client_env_remote, rules=rules)
def config(self, authority=None, *, secrets=True):
return self.effective(authority, secrets=secrets)[0]
diff --git a/tests/gateway/test_session_client_env.py b/tests/gateway/test_session_client_env.py
index 983492d1a32..550394fc7c6 100644
--- a/tests/gateway/test_session_client_env.py
+++ b/tests/gateway/test_session_client_env.py
@@ -305,6 +305,40 @@ async def test_a_copied_staged_receipt_is_never_judged_by_its_own_filtered_map(t
owner.db.close()
+async def test_configured_toolchain_names_a_local_client_omitted_never_leak_from_the_daemon(tmp_path, monkeypatch):
+ """Client A exported a configured extra (``terminal.client_env_passthrough``) and auto-started the
+ gateway, seeding the daemon's environment; client B's session, which did not export it, runs its tools
+ without it. PATH is the documented exception (a client that sent none keeps the gateway's)."""
+ import sys
+ from gateway.session_policy import policy_scope
+ from tools.code_execution_env import _build_child_env
+ from tools.environments.local import _make_run_env
+ from tools.process_registry import ProcessRegistry
+ home = _home(tmp_path, _CONFIG + 'terminal: {client_env_passthrough: [GEM_HOME]}\n')
+ monkeypatch.setenv('GEM_HOME', '/alpha')
+ monkeypatch.setenv('PATH', '/daemon/bin:/usr/bin:/bin')
+ owner, connection = await _owner(home, monkeypatch)
+
+ def seen(policy):
+ with policy_scope(policy, authority=owner):
+ envs = (_make_run_env({}), ProcessRegistry._spawn_env({}), _build_child_env(
+ rpc_endpoint='s', rpc_token='t', tmpdir=str(tmp_path), child_python=sys.executable))
+ backend = _make_run_env({'GEM_HOME': '/configured'})['GEM_HOME'] # terminal.env still wins
+ return [env.get('GEM_HOME') for env in envs], envs[0]['PATH'].split(':')[0], backend
+ try:
+ _, mine = _create(owner, connection.actor, home, 'a', client_env={'GEM_HOME': '/a', 'PATH': '/client/bin'})
+ _, other = _create(owner, connection.actor, home, 'b', client_env={'PATH': '/client/bin'})
+ _, bare = _create(owner, connection.actor, home, 'c', client_env={})
+ assert seen(mine) == (['/a'] * 3, '/client/bin', '/configured')
+ assert seen(other) == ([None] * 3, '/client/bin', '/configured')
+ assert seen(bare) == ([None] * 3, '/daemon/bin', '/configured')
+ assert _make_run_env({})['GEM_HOME'] == '/alpha' # messaging/cron/Desktop: the gateway's own
+ finally:
+ await connection.close()
+ owner.db.close()
+
+
@pytest.mark.asyncio
async def test_host_values_apply_only_over_a_gateway_proven_local_attach(tmp_path, monkeypatch):
"""Toolchain and path-valued values are the client's only when the gateway itself proves the attach is