Skip to content

The agent that grows with you

fix(terminal): background commands keep spawn PATH dirs a login profile resets

Teknium29fef14fbf35
diff --git a/tests/tools/test_login_shell_path_preservation.py b/tests/tools/test_login_shell_path_preservation.py
index be2bd1b6754..1d86cf11744 100644
--- a/tests/tools/test_login_shell_path_preservation.py
+++ b/tests/tools/test_login_shell_path_preservation.py
@@ -132,3 +132,36 @@ def test_dropped_spawn_path_dirs_return_after_the_login_path(tmp_path, monkeypat
assert out == ["extra-b", "user-bin"]
finally:
env.cleanup()
+
+
+def test_background_command_keeps_spawn_path_dirs_a_login_profile_resets(tmp_path, monkeypatch):
+ """Background commands run ``bash -lic`` with no snapshot, so a ``~/.profile`` that
+ resets PATH Debian-style dropped every parent-only dir: the terminal tool's background
+ spawn re-appends them through the same helper as the foreground, profile dirs first."""
+ import time
+ from types import SimpleNamespace
+
+ from tools.process_registry import ProcessRegistry
+ from tools.terminal_tool_background import _spawn
+
+ profile_bin, parent_bin = tmp_path / "profile-bin", tmp_path / "parent-bin"
+ for d in (profile_bin, parent_bin):
+ d.mkdir()
+ _exe(parent_bin, "only-in-parent")
+ (tmp_path / ".profile").write_text(f'PATH="{profile_bin}:/usr/local/bin:/usr/bin:/bin"\n')
+ monkeypatch.setenv("HOME", str(tmp_path))
+ registry = ProcessRegistry()
+ session = _spawn(registry, env=SimpleNamespace(env={"PATH": f"{parent_bin}:/usr/bin:/bin"}),
+ env_type="local", command='only-in-parent; printf "PATH=%s\\n" "$PATH"',
+ cwd=str(tmp_path), effective_task_id="t", task_id="t", session_key="",
+ effective_pty=False)
+ deadline = time.monotonic() + 30
+ while not session.exited and time.monotonic() < deadline:
+ time.sleep(0.05)
+ assert session.exited, session.output_buffer
+ assert "parent-bin" in session.output_buffer.split(), session.output_buffer
+ path_line = next(ln for ln in session.output_buffer.splitlines() if ln.startswith("PATH="))
+ entries = path_line.removeprefix("PATH=").split(":")
+ assert entries[0] == str(profile_bin)
+ assert entries.index("/bin") < entries.index(str(parent_bin))
diff --git a/tools/environments/local.py b/tools/environments/local.py
index 00eac61b500..3e0b263aa88 100644
--- a/tools/environments/local.py
+++ b/tools/environments/local.py
@@ -871,7 +871,9 @@ def _append_dropped_path_entries(cmd_string: str, parent_path: str) -> str:
``/opt/hermes/bin`` and bare ``hermes`` exits 127 (#56634). Dropped dirs go AFTER
the login PATH in parent order, so a command that resolved before still resolves
to the same binary (the user's nvm/pyenv keep precedence); only commands that had
- no match gain one. Present entries are never duplicated; missing dirs are skipped."""
+ no match gain one. Present entries are never duplicated; missing dirs are skipped.
+ Background commands (``ProcessRegistry.spawn_local``, ``bash -lic``, no snapshot) use
+ this same prelude so the two paths cannot diverge."""
if _IS_WINDOWS:
return cmd_string # native PATH stays ``;``-joined; bash sees a converted copy
entries = [d for d in dict.fromkeys(parent_path.split(":")) if d and os.path.isdir(d)]
diff --git a/tools/process_registry.py b/tools/process_registry.py
index 451dd566049..29618ffe6f8 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -25,7 +25,7 @@ _IS_WINDOWS = platform.system() == "Windows"
# (not merely "not Windows") so macOS and other POSIX platforms never touch systemd.
# See #70716.
_IS_LINUX = platform.system() == "Linux"
-from tools.environments.local import _find_shell, _resolve_safe_cwd, _sanitize_subprocess_env, _terminal_env
+from tools.environments.local import _append_dropped_path_entries, _find_shell, _resolve_safe_cwd, _sanitize_subprocess_env, _terminal_env
from hermes_cli._subprocess_compat import windows_hide_flags
from dataclasses import dataclass, field
from typing import Any, Dict, List, Literal, NamedTuple, Optional
@@ -1176,15 +1176,17 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
def spawn_local(
self, command: str, cwd: str | None = None, task_id: str = "", session_key: str = "",
env_vars: dict | None = None, use_pty: bool = False, owner_task_id: str = "",
- persist_on_release: bool = False) -> ProcessSession:
+ persist_on_release: bool = False, restore_login_path: bool = False) -> ProcessSession:
"""Spawn a background process locally (TERMINAL_ENV=local; other backends: spawn_via_env()).
``use_pty`` asks ptyprocess/pywinpty for a pseudo-terminal; on failure: pipe + ``pty_fallback``.
- ``persist_on_release`` keeps the process out of agent-lifecycle kill sweeps (#41225)."""
+ ``persist_on_release`` keeps the process out of agent-lifecycle kill sweeps (#41225).
+ ``restore_login_path`` re-appends spawn-env PATH dirs the login profile reset (#56634)."""
# Bash runs ``A && B &`` as a subshell holding our stdout pipe open while a server B lives;
# the rewriter makes it ``A && { B & }`` (#68915). Lazy: terminal_tool imports this module.
from tools.terminal_tool_sudo import _rewrite_compound_background as _rewrite_bg
- safe_command = _rewrite_bg(command)
+ login_path = self._spawn_env(env_vars).get("PATH", "") if restore_login_path else ""
+ safe_command = _append_dropped_path_entries(_rewrite_bg(command), login_path)
session = self._new_session(command, task_id, owner_task_id, session_key, _resolve_safe_cwd(cwd or os.getcwd()),
persist_on_release=persist_on_release)
pty_scope_attempted = False
diff --git a/tools/terminal_tool_background.py b/tools/terminal_tool_background.py
index dda1a5dd460..12dd49ff975 100644
--- a/tools/terminal_tool_background.py
+++ b/tools/terminal_tool_background.py
@@ -99,7 +99,8 @@ def _spawn(process_registry, *, env, env_type, command, cwd, effective_task_id,
persist_on_release=persist_on_release)
if env_type == "local":
return process_registry.spawn_local(
- env_vars=env.env if hasattr(env, 'env') else None, use_pty=effective_pty, **common)
+ env_vars=env.env if hasattr(env, 'env') else None, use_pty=effective_pty,
+ restore_login_path=True, **common)
return process_registry.spawn_via_env(env=env, **common)