fix(tui-gateway): secondary side workers and agent builds bind their own terminal scope

Under multiplexing tools/terminal_tool.py no longer bridges a profile's
terminal.* into os.environ while a home override is bound (#108440), so any
secondary-profile entrypoint that binds only the home (+ secrets) leaves
terminal_tool on the launch process's ambient TERMINAL_*: a secondary with
`terminal.backend: docker` ran its prompt.background / prompt.btw /
preview.restart side agents, its eager resume and its session.branch build
on the launch `local` backend.

_spawn_side_agent and _profile_build_scope now enter
_session_profile_runtime_scope, the same home -> secrets -> terminal
composition a prompt turn binds (and gateway/run.py::_profile_runtime_scope
mirrors). The terminal scope is installed for the whole worker/build
lifetime and reset on success and on exception; a malformed secondary
config still yields the fail-closed refusal scope. No ambient os.environ
write is restored.

Refs #108440 review (andrexibiza), #107442 (ehz0ah).
This commit is contained in:
teknium1
2026-09-13 12:47:54 -07:00
committed by Teknium
parent 3f86ed75da
commit 484b8e96e8
2 changed files with 14 additions and 28 deletions
+9 -16
View File
@@ -938,24 +938,17 @@ def _spawn_side_agent(
def run():
session_tokens = _set_session_context(task_id, cwd=(cwd or _session_cwd(session)))
# Bug #50233: ephemeral agent threads don't inherit the session's HERMES_HOME override (the
# ContextVar set on the session-create thread doesn't propagate here), so a background turn under a
# non-default profile would run against the wrong home. Re-bind the override for the duration of
# this turn, exactly as the normal prompt turn does, and restore it afterward.
# Bug #50233: ephemeral preview-restart agent threads don't inherit the session's HERMES_HOME
# override (the ContextVar set on the session-create thread doesn't propagate here). Re-bind it for
# the duration of the turn, mirroring the normal prompt turn, then restore it. NOTE: we deliberately
# do NOT close this agent through task-wide process cleanup — the whole point of preview.restart is
# to leave a background server running under this task_id, and AIAgent.close() would kill every
# process for the task_id and tear down the very server the restart just started.
profile_home = session.get("profile_home")
home_token = set_hermes_home_override(profile_home) if profile_home else None
# Bug #50233: ephemeral agent threads don't inherit the session's ContextVar scopes (set on the
# session-create thread), so a side turn under a non-default profile ran against the wrong home.
# Bind the profile's home + secrets + terminal policy for the whole body, exactly as a prompt turn
# does: home alone left terminal_tool on the launch process's ambient TERMINAL_* (a docker
# secondary's background/btw/preview agent ran local). NOTE: we deliberately do NOT close this
# agent through task-wide process cleanup — the whole point of preview.restart is to leave a
# background server running under this task_id, and AIAgent.close() would kill every process for
# the task_id and tear down the very server the restart just started.
try:
try:
with _session_profile_runtime_scope(session):
text = body()
finally:
if home_token is not None:
reset_hermes_home_override(home_token)
_emit(event, parent, {"task_id": task_id, **extra, "text": text})
except Exception as e:
_emit(event, parent, {"task_id": task_id, **extra, "text": f"error: {e}"})
+5 -12
View File
@@ -67,19 +67,12 @@ def _new_runtime_ids(params: dict) -> tuple[str, str]:
return uuid.uuid4().hex[:8], _resolve_session_source(_str_param(params, "source") or None)
@contextlib.contextmanager
def _profile_build_scope(profile_home):
"""Bind HERMES_HOME + secret scope for an agent build (home alone leaves get_secret() on the LAUNCH .env)."""
if not profile_home:
yield
return
home_token = set_hermes_home_override(str(profile_home))
secret_token = set_secret_scope(build_profile_secret_scope(Path(str(profile_home))))
try:
yield
finally:
reset_hermes_home_override(home_token)
reset_secret_scope(secret_token)
"""Bind HERMES_HOME + secret + terminal scope for an agent build: the same composition a turn
binds (``_session_profile_runtime_scope``). Home alone leaves ``get_secret()`` on the LAUNCH
``.env``; home + secrets alone leaves ``_make_agent``'s terminal probing on the launch process's
ambient ``TERMINAL_*`` (a ``terminal.backend: docker`` secondary built a ``local`` agent)."""
return _session_profile_runtime_scope({"profile_home": str(profile_home) if profile_home else None})
def _make_agent_in_context(sid: str, key: str, **kwargs):