Skip to content

The agent that grows with you

fix(tools): keep the daemon's behaviour values out of a client session's tools

unsupportedpastels39f22caafe4b
diff --git a/agent/session_client_env.py b/agent/session_client_env.py
index 20b13dfc39f..d4273a4261d 100644
--- a/agent/session_client_env.py
+++ b/agent/session_client_env.py
@@ -150,10 +150,17 @@ def toolchain_environ(base=None) -> dict[str, str]:
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."""
+ the exception: a client that sent none runs with the gateway's. A shell client's session also drops
+ the gateway's own behaviour values it reads from its client (:func:`session_env`): tools never see
+ the daemon's ephemeral prompt or provider request in place of the session's."""
env = dict(os.environ if base is None else base)
bound = _BOUND.get()
- if bound is None or not bound[1]:
+ if bound is None:
+ return env
+ for name in BEHAVIOUR_ENV:
+ if bound[1] or name not in HOST_BEHAVIOUR:
+ env.pop(name, None)
+ if not bound[1]:
return env
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'])}
diff --git a/tests/tools/test_session_tool_env.py b/tests/tools/test_session_tool_env.py
index 0ad7c469616..fb42a85df28 100644
--- a/tests/tools/test_session_tool_env.py
+++ b/tests/tools/test_session_tool_env.py
@@ -39,6 +39,32 @@ def test_terminal_run_env_starts_from_the_sessions_toolchain(daemon_env):
assert '/daemon/bin' in _parts(env) and 'NVM_DIR' not in env
+def test_tool_envs_carry_the_behaviour_values_the_session_uses(daemon_env, monkeypatch, tmp_path):
+ """A shell client's session reads its client's behaviour values, so the daemon's own (an operator's
+ ephemeral prompt) never reach its tool subprocesses; a remote client's session still uses the
+ daemon's host values (prefill file), and an unbound session (messaging, cron) all of them."""
+ import sys
+ from agent.session_client_env import BEHAVIOUR_ENV, client_env_scope
+ from tools.code_execution_env import _build_child_env
+ from tools.environments.local import _make_run_env
+ from tools.process_registry import ProcessRegistry
+ daemon = {name: f'daemon-{name}' for name in BEHAVIOUR_ENV}
+ for name, value in daemon.items():
+ monkeypatch.setenv(name, value)
+
+ def envs():
+ return (_make_run_env({}), ProcessRegistry._spawn_env({}),
+ _build_child_env(rpc_endpoint='sock', rpc_token='t', tmpdir=str(tmp_path), child_python=sys.executable))
+ with client_env_scope({**CLIENT, 'HERMES_EPHEMERAL_SYSTEM_PROMPT': 'client prompt'}):
+ for env in envs():
+ assert not set(BEHAVIOUR_ENV) & set(env), env
+ with client_env_scope(CLIENT, local=False):
+ terminal = envs()[0]
+ assert 'HERMES_EPHEMERAL_SYSTEM_PROMPT' not in terminal and 'HERMES_INFERENCE_PROVIDER' not in terminal
+ assert terminal['HERMES_PREFILL_MESSAGES_FILE'] == daemon['HERMES_PREFILL_MESSAGES_FILE']
+ assert envs()[0]['HERMES_EPHEMERAL_SYSTEM_PROMPT'] == daemon['HERMES_EPHEMERAL_SYSTEM_PROMPT']
+
+
def test_background_process_env_starts_from_the_sessions_toolchain(daemon_env):
from agent.session_client_env import client_env_scope
from tools.process_registry import ProcessRegistry