fix(terminal): background commands keep spawn PATH dirs a login profile resets
Background terminal commands run `bash -lic` with no env snapshot, so a ~/.profile or distro /etc/profile that resets PATH dropped every parent-only dir (bare `hermes` / the venv's tools: command not found), while the same command in the foreground now resolved. spawn_local gains an opt-in restore_login_path that prepends the SAME _append_dropped_path_entries prelude the foreground snapshot uses, fed from the spawn env's PATH; the terminal tool's local background spawn turns it on. The registry's shell argv shape is unchanged.
This commit is contained in:
@@ -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)]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user