The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tools): collapse suppress/try-pass layers, unify probe subprocess helper (_py_out), compact pythonpath/gitbash/policy docs
Teknium9289406e13e4
diff --git a/tools/env_passthrough.py b/tools/env_passthrough.pyindex 9b4dc8a8833..1ee587968ef 100644--- a/tools/env_passthrough.py+++ b/tools/env_passthrough.py@@ -1,12 +1,8 @@-"""Environment variable passthrough registry.--Skills that declare ``required_environment_variables`` need those vars in sandboxed-execution environments (execute_code, terminal), which strip secrets from the child-env by default. This module is the session-scoped allowlist, fed by skill-declarations (registered by ``skill_view``) and ``terminal.env_passthrough`` in-config.yaml. When profile multiplexing is active, forwarded values are resolved-through the current profile's secret scope rather than the process environment.-"""+"""Environment variable passthrough registry: the session-scoped allowlist of vars a+skill's ``required_environment_variables`` (registered by ``skill_view``) or+``terminal.env_passthrough`` in config.yaml may forward into sandboxed children+(execute_code, terminal), which strip secrets by default. Under profile multiplexing,+forwarded values resolve through the profile's secret scope, not the process env."""from __future__ import annotations@@ -46,29 +42,20 @@ def _is_hermes_provider_credential(name: str) -> bool:registerable. Fails closed when the blocklist cannot be imported."""try:from tools.environments.local import (- _HERMES_PROVIDER_ENV_BLOCKLIST,- _is_hermes_internal_secret,- )+ _HERMES_PROVIDER_ENV_BLOCKLIST, _is_hermes_internal_secret)except Exception as e:logger.warning("env passthrough: provider credential blocklist import failed; "- "failing closed and refusing passthrough registration for %r: %s",- name,- e,- )+ "failing closed and refusing passthrough registration for %r: %s", name, e)return Truereturn _is_hermes_internal_secret(name) or name in _HERMES_PROVIDER_ENV_BLOCKLISTdef register_env_passthrough(var_names: Iterable[str]) -> None:- """Register env var names as allowed in sandboxed environments (typically- from a skill's ``required_environment_variables``).-- Hermes-managed provider credentials are rejected to preserve the- ``execute_code`` sandbox's credential-scrubbing guarantee (GHSA-rhgp-j443-p4rf);- a skill needing a Hermes-managed provider should use the main-process tools- (web_search, web_extract, …). Third-party keys pass normally.- """+ """Register env var names as allowed in sandboxed environments (typically a+ skill's ``required_environment_variables``). Hermes-managed provider credentials+ are rejected (GHSA-rhgp-j443-p4rf) — such skills should use the main-process tools+ (web_search, web_extract, …); third-party keys pass normally."""for name in _accepted((n.strip() for n in var_names), ("env passthrough: refusing to register Hermes provider ""credential %r (blocked by _HERMES_PROVIDER_ENV_BLOCKLIST). "@@ -98,7 +85,6 @@ def _load_config_passthrough() -> frozenset[str]:global _config_passthroughif _config_passthrough is not None:return _config_passthrough-result: set[str] = set()try:passthrough = cfg_get(read_raw_config(), "terminal", "env_passthrough")@@ -113,7 +99,6 @@ def _load_config_passthrough() -> frozenset[str]:)))except Exception as e:logger.debug("Could not read tools.env_passthrough from config: %s", e)-_config_passthrough = frozenset(result)return _config_passthrough@@ -129,28 +114,18 @@ def get_all_passthrough() -> frozenset[str]:def resolve_passthrough_value(name: str, fallback: str | None = None) -> str | None:- """Resolve an allowlisted variable without crossing profile boundaries.-- ``fallback`` is the value the caller would have forwarded before profile secret- scopes existed (a snapshot of ``os.environ`` or the profile's ``.env``). An- active multiplex scope is authoritative: a missing key returns ``None`` and- never falls back to the process-global environment; an unscoped read while- multiplexing is active raises the fail-closed ``UnscopedSecretError`` from- :mod:`agent.secret_scope`. Outside multiplexing, an installed scope keeps the- overlay semantics and an unscoped caller keeps its already-resolved fallback.- """+ """Resolve an allowlisted variable without crossing profile boundaries. ``fallback``+ is what the caller would have forwarded before secret scopes existed (a snapshot of+ ``os.environ`` / the profile ``.env``). An active multiplex scope is authoritative:+ a missing key returns ``None``, never the process-global env, and an unscoped read+ raises the fail-closed ``UnscopedSecretError``. Outside multiplexing an installed+ scope keeps overlay semantics and an unscoped caller keeps its fallback."""from agent.secret_scope import (- _is_global_env,- current_secret_scope,- get_secret,- is_multiplex_active,- )-+ _is_global_env, current_secret_scope, get_secret, is_multiplex_active)# Global terminal/runtime settings are not profile secrets; ``fallback`` is# already the caller's effective value (incl. an explicit per-call override).if _is_global_env(name) and fallback is not None:return fallback-multiplex_active = is_multiplex_active()if current_secret_scope() is None:return get_secret(name) if multiplex_active else fallbackdiff --git a/tools/env_probe.py b/tools/env_probe.pyindex c0d83315df0..49405141a64 100644--- a/tools/env_probe.py+++ b/tools/env_probe.py@@ -1,12 +1,8 @@-"""Local-environment toolchain probe for the system prompt.--When the terminal backend is local, surface one deterministic line about Python-tooling state (python3/python versions, missing pip module, pip bound to a-different Python, PEP 668) so models don't discover it by hitting walls. Cheap-(~50ms), cached for the process lifetime, "" when the environment is clean.-Remote backends are skipped (the sandbox has its own probe in agent/prompt_builder).-Toggle via ``agent.environment_probe`` in config.yaml (default True).-"""+"""Local-environment toolchain probe for the system prompt: when the terminal backend+is local, one deterministic line about Python tooling (python3/python versions, missing+pip, pip bound to another Python, PEP 668) so models don't discover it by hitting+walls. Cached per process; "" when clean. Remote backends are skipped (the sandbox has+its own probe in agent/prompt_builder). Toggle: ``agent.environment_probe`` in config.yaml."""from __future__ import annotations@@ -67,59 +63,51 @@ def _run(cmd: list[str], timeout: float = 3.0) -> tuple[int, str, str]:result = subprocess.run(cmd, stdout=out_f, stderr=err_f, timeout=timeout, check=False,# CREATE_NO_WINDOW (0 on POSIX): pythonw hosts would flash a console- stdin=subprocess.DEVNULL, creationflags=windows_hide_flags(),- )+ stdin=subprocess.DEVNULL, creationflags=windows_hide_flags())except subprocess.TimeoutExpired:return -1, "", "timeout"out_f.seek(0)err_f.seek(0)- out = out_f.read().decode("utf-8", "replace").strip()- err = err_f.read().decode("utf-8", "replace").strip()- return result.returncode, out, err+ return (result.returncode, out_f.read().decode("utf-8", "replace").strip(),+ err_f.read().decode("utf-8", "replace").strip())except FileNotFoundError:return -1, "", "not found"except OSError as exc:return -1, "", f"oserror: {exc}"-def _python_version_of(binary: str) -> Optional[str]:- """Return a short version string like ``3.12.4`` for ``binary``, or None."""+def _py_out(binary: str, *args: str) -> Optional[str]:+ """stdout of ``<binary> *args`` when the binary is on PATH and exits 0, else None."""if not shutil.which(binary):return None+ rc, out, _err = _run([binary, *args])+ return out if rc == 0 else None+++def _python_version_of(binary: str) -> Optional[str]:+ """Return a short version string like ``3.12.4`` for ``binary``, or None."""code = "import sys; print('.'.join(map(str, sys.version_info[:3])))"- rc, out, _err = _run([binary, "-c", code])- return out if rc == 0 and out else None+ return _py_out(binary, "-c", code) or Nonedef _has_pip_module(binary: str) -> bool:"""True if ``<binary> -m pip --version`` succeeds."""- if not shutil.which(binary):- return False- return _run([binary, "-m", "pip", "--version"])[0] == 0+ return _py_out(binary, "-m", "pip", "--version") is not Nonedef _detect_pep668(binary: str) -> bool:"""True when ``<binary>`` is PEP-668 externally-managed (``EXTERNALLY-MANAGED``marker next to the stdlib, as Debian/Ubuntu ship)."""- if not shutil.which(binary):- return False- code = (- "import sys, os;"- "stdlib = os.path.dirname(os.__file__);"- "marker = os.path.join(stdlib, 'EXTERNALLY-MANAGED');"- "print('yes' if os.path.exists(marker) else 'no')"- )- rc, out, _err = _run([binary, "-c", code])- return rc == 0 and out.strip() == "yes"+ code = ("import os; print('yes' if os.path.exists(os.path.join("+ "os.path.dirname(os.__file__), 'EXTERNALLY-MANAGED')) else 'no')")+ return (_py_out(binary, "-c", code) or "").strip() == "yes"def _pip_python_version() -> Optional[str]:"""If ``pip`` is on PATH, the Python version it's bound to — the trailing``(python X.Y)`` of ``pip --version`` (e.g. ``"3.12"``), else None."""- if not shutil.which("pip"):- return None- rc, out, _err = _run(["pip", "--version"])- if rc == 0 and out and "(python " in out and out.endswith(")"):+ out = _py_out("pip", "--version") or ""+ if "(python " in out and out.endswith(")"):return out.rsplit("(python ", 1)[1][:-1].strip()return None@@ -151,20 +139,17 @@ def _build_probe_line() -> str:mismatch = bool(pip_bound_to and py3_ver and not py3_ver.startswith(pip_bound_to))if py3_ver is not None and py3_has_pip and not mismatch and (not py3_pep668 or has_uv):return ""-# Compact factual summary; ONE line so it doesn't dominate the prompt.bits: list[str] = []if py3_ver:bits.append(f"python3={py3_ver}" + ("" if py3_has_pip else " (no pip module)"))else:bits.append("python3=missing")-if py_ver and py_ver != py3_ver:bits.append(f"python={py_ver}")elif not py_ver and py3_ver:# Common on Debian/Ubuntu — stop the model typing `python`.bits.append("python=missing (use python3)")-if pip_bound_to:if mismatch:bits.append(f"pip→python{pip_bound_to} (mismatch)")@@ -173,7 +158,6 @@ def _build_probe_line() -> str:elif not py3_has_pip:# (when `pip` is off PATH but `python3 -m pip` works, say nothing)bits.append("pip=missing")-if py3_pep668:bits.append("PEP 668=yes (use venv or uv)")if has_uv:@@ -189,7 +173,6 @@ def get_environment_probe_line(*, force_refresh: bool = False) -> str:global _WAIT_ALREADY_TIMED_OUTif force_refresh:_reset_cache_for_tests()-# Resolve the backend HERE, in the caller's context: under gateway multiplexing the# routed profile's backend lives in the per-turn terminal scope, which the worker# thread does not inherit. Remote backends answer "" without consulting the cache@@ -197,10 +180,8 @@ def get_environment_probe_line(*, force_refresh: bool = False) -> str:backend = _resolve_terminal_backend()if backend in _REMOTE_BACKENDS or _plugin_backend_is_remote(backend):return ""-if _PROBE_DONE.is_set():return _CACHED_LINE or ""-_ensure_probe_started()wait_timeout = 0.05 if _WAIT_ALREADY_TIMED_OUT else _PROBE_WAIT_TIMEOUTif not _PROBE_DONE.wait(timeout=wait_timeout):@@ -210,9 +191,7 @@ def get_environment_probe_line(*, force_refresh: bool = False) -> str:_WAIT_ALREADY_TIMED_OUT = Truelogger.warning("env_probe did not finish within %.0fs; building the system "- "prompt without the Python toolchain line",- _PROBE_WAIT_TIMEOUT,- )+ "prompt without the Python toolchain line", _PROBE_WAIT_TIMEOUT)return ""return _CACHED_LINE or ""@@ -239,8 +218,7 @@ def _ensure_probe_started() -> None:if _PROBE_DONE.is_set() or (_PROBE_THREAD is not None and _PROBE_THREAD.is_alive()):return_PROBE_THREAD = threading.Thread(- target=_probe_worker, args=(_PROBE_GEN,), name="env-probe", daemon=True,- )+ target=_probe_worker, args=(_PROBE_GEN,), name="env-probe", daemon=True)_PROBE_THREAD.start()diff --git a/tools/environments/local.py b/tools/environments/local.pyindex 2b9b480bcd1..4b0ef14a2b4 100644--- a/tools/environments/local.py+++ b/tools/environments/local.py@@ -1,5 +1,6 @@"""Local execution environment — spawn-per-call with session snapshot."""+import contextlibimport loggingimport ntpathimport os@@ -115,9 +116,7 @@ def _msys_to_windows_path(cwd: str) -> str:"""``/c/Users/x`` / ``/cygdrive/c/..`` / ``/mnt/c/..`` -> native ``C:\\Users\\x`` so``isdir``/``Popen(cwd=)`` find it. No-op off Windows, for empty input and formulti-segment POSIX paths like ``/home/x``; idempotent on native paths."""- if not _IS_WINDOWS or not cwd:- return cwd- m = re.match(r'^/(?:(?:cygdrive|mnt)/)?([a-zA-Z])(/.*)?$', cwd)+ m = _IS_WINDOWS and cwd and re.match(r'^/(?:(?:cygdrive|mnt)/)?([a-zA-Z])(/.*)?$', cwd)if not m:return cwdtail = (m.group(2) or "").replace('/', '\\')@@ -137,7 +136,6 @@ def _resolve_local_initial_cwd(cwd: str) -> str:return expandedif os.path.isabs(expanded):return expanded-candidate = os.path.abspath(expanded)current = os.getcwd()# Relative name matching the tail of the current dir: use the current dir.@@ -151,9 +149,7 @@ def _resolve_local_initial_cwd(cwd: str) -> str:def _windows_to_msys_path(cwd: str) -> str:"""Native ``C:\\Users\\x`` -> Git Bash ``/c/Users/x`` so ``builtin cd`` resolvesit. No-op off Windows / for non-drive paths."""- if not _IS_WINDOWS or not cwd:- return cwd- m = re.match(r'^([a-zA-Z]):[\\/]*(.*)$', cwd)+ m = _IS_WINDOWS and cwd and re.match(r'^([a-zA-Z]):[\\/]*(.*)$', cwd)if not m:return cwdtail = (m.group(2) or "").replace('\\', '/').lstrip('/')@@ -164,9 +160,7 @@ def _bash_safe_path(path: str) -> str:"""*path* safe to embed in a Git Bash script: ``C:\\Users\\x`` / ``C:/Users/x``become ``/c/Users/x`` (MSYS argument conversion mangles ``C:/`` forms) andleftover backslashes are normalized so bash does not eat ``\\U``. No-op off Windows."""- if not _IS_WINDOWS or not path:- return path- return _windows_to_msys_path(path).replace("\\", "/")+ return _windows_to_msys_path(path).replace("\\", "/") if _IS_WINDOWS and path else pathdef _quote_bash_path(path: str) -> str:@@ -186,7 +180,7 @@ def _resolve_safe_cwd(cwd: str) -> str:``tempfile.gettempdir()``. MSYS paths are normalized first on Windows so a valid``pwd -P`` result is not rejected. Lets ``_run_bash`` recover from a deleted orinaccessible cwd instead of ``Popen`` raising and wedging every later call."""- cwd = _msys_to_windows_path(cwd) if _IS_WINDOWS else cwd+ cwd = _msys_to_windows_path(cwd)if cwd and _cwd_usable(cwd):return cwdif cwd and os.path.isdir(cwd):@@ -209,14 +203,12 @@ def _resolve_safe_cwd(cwd: str) -> str:# --- Child-process environment construction ---def _apply_profile_home(env: dict) -> None:"""Bridge the context-local HERMES_HOME override, then the subprocess HOME contract."""+ from hermes_constants import apply_subprocess_home_env, get_hermes_home_overridetry:- from hermes_constants import get_hermes_home_override- value = get_hermes_home_override()- if value:+ if value := get_hermes_home_override():env["HERMES_HOME"] = valueexcept Exception:pass- from hermes_constants import apply_subprocess_home_envapply_subprocess_home_env(env)@@ -230,7 +222,6 @@ def _inject_session_context_env(env: dict) -> None:from gateway.session_context import _UNSET, _VAR_MAP, session_context_engagedexcept Exception:return-_engaged = session_context_engaged()for var_name, var in _VAR_MAP.items():value = var.get()@@ -420,9 +411,9 @@ def _compute_git_bash_bin_dirs() -> list[str]:return []parent = os.path.dirname(os.path.dirname(bash)) # bash in <root>\bin or <root>\usr\bin (MinGit)root = os.path.dirname(parent) if os.path.basename(parent).lower() == "usr" else parent- subs = (("mingw64", "bin"), ("mingw32", "bin"), ("usr", "local", "bin"), ("usr", "bin"),- ("bin",))- return list(dict.fromkeys(c for sub in subs if os.path.isdir(c := os.path.join(root, *sub))))+ subs = ("mingw64/bin", "mingw32/bin", "usr/local/bin", "usr/bin", "bin")+ dirs = (os.path.join(root, *sub.split("/")) for sub in subs)+ return list(dict.fromkeys(d for d in dirs if os.path.isdir(d)))def _prepend_missing_path_entries(existing_path: str, dirs: list[str]) -> str:@@ -477,8 +468,6 @@ def _resolve_hermes_bin_dir() -> str | None:global _HERMES_BIN_DIRif _HERMES_BIN_DIR is not _SENTINEL:return _HERMES_BIN_DIR # type: ignore[return-value]-- candidate: str | None = Nonewhich = shutil.which("hermes")argv0 = sys.argv[0] if sys.argv else ""base = os.path.basename(argv0).lower()@@ -489,12 +478,10 @@ def _resolve_hermes_bin_dir() -> str | None:elif (os.path.isabs(argv0) and (base == "hermes" or base.startswith("hermes."))and os.path.isfile(argv0)):candidate = os.path.dirname(argv0)- elif exe_dir and os.path.isfile(os.path.join(exe_dir, shim)):- candidate = exe_dir- if candidate and not os.path.isdir(candidate):- candidate = None- _HERMES_BIN_DIR = candidate- return candidate+ else:+ candidate = exe_dir if exe_dir and os.path.isfile(os.path.join(exe_dir, shim)) else None+ _HERMES_BIN_DIR = candidate if candidate and os.path.isdir(candidate) else None+ return _HERMES_BIN_DIRdef _prepend_hermes_bin_dir(existing_path: str) -> str:@@ -509,8 +496,7 @@ def _managed_runtime_path_entries() -> list[str]:profile-scoped and a managed tree can appear mid-process."""try:from hermes_constants import get_hermes_home, iter_hermes_node_dirs- candidates = [*iter_hermes_node_dirs(), get_hermes_home() / "bin"]- return [str(d) for d in candidates if d.is_dir()]+ return [str(d) for d in (*iter_hermes_node_dirs(), get_hermes_home() / "bin") if d.is_dir()]except Exception:return []@@ -532,18 +518,15 @@ def _apply_windows_msys_bash_env_defaults(env: dict) -> None:"""Disable MSYS argument path conversion (``/FO`` -> ``C:/.../git/FO`` breakstasklist/schtasks/wmic/``cmd /c``). Git for Windows honors ``MSYS_NO_PATHCONV``;MSYS2/Cygwin bash honor ``MSYS2_ARG_CONV_EXCL`` — set both; users can override."""- if not _IS_WINDOWS:- return- env.setdefault("MSYS_NO_PATHCONV", "1")- env.setdefault("MSYS2_ARG_CONV_EXCL", "*")+ if _IS_WINDOWS:+ env.setdefault("MSYS_NO_PATHCONV", "1")+ env.setdefault("MSYS2_ARG_CONV_EXCL", "*")def _path_env_key(run_env: dict) -> str | None:"""PATH env key to update without altering Windows casing (``Path`` vs ``PATH``);None when a Windows env has no PATH key at all."""- if not _IS_WINDOWS:- return "PATH"- return next((key for key in run_env if key.upper() == "PATH"), None)+ return next((k for k in run_env if k.upper() == "PATH"), None) if _IS_WINDOWS else "PATH"def _make_run_env(env: dict) -> dict:@@ -554,19 +537,16 @@ def _make_run_env(env: dict) -> dict:# --- Hermes venv / repo-root detection (module-level, computed once) ---# Owned here; read lazily by tools.environments.local_pythonpath (tests patch here).-#: Repo root (three levels up). The Electron app prepends it to PYTHONPATH so the-#: backend can ``import tools``; other subprocesses must not inherit it.+# The Electron app prepends the repo root to PYTHONPATH so the backend can ``import+# tools``; other subprocesses must not inherit it. Aliases: launchers may emit other+# spellings — the Windows gateway launcher renders Hermes-owned paths under the+# configured HERMES_HOME spelling (possibly a junction to another drive)._hermes_repo_root: Path = Path(__file__).resolve().parents[2]-#: Alternate repo-root spellings launchers may emit: ``resolve()`` canonicalizes-#: junctions, but the Windows gateway launcher renders Hermes-owned paths under the-#: configured HERMES_HOME spelling (possibly a junction to another drive)._hermes_repo_root_aliases: tuple[Path, ...] = _build_hermes_repo_root_aliases(_hermes_repo_root, Path(__file__).absolute().parents[2], get_process_hermes_home())-#: Whether the interpreter runs inside a venv (``sys.real_prefix``: virtualenv<20)._in_venv: bool = (getattr(sys, "base_prefix", sys.prefix) != sys.prefix- or hasattr(sys, "real_prefix"))-#: Lazily-cached site-packages dirs of the running interpreter's own venv.-_hermes_site_packages: list[Path] | None = None+ or hasattr(sys, "real_prefix")) # real_prefix: virtualenv<20+_hermes_site_packages: list[Path] | None = None # lazily cached by local_pythonpath# --- Login-shell init files ---@@ -597,10 +577,10 @@ def _resolve_shell_init_files() -> list[str]:for raw in candidates:try:path = os.path.expandvars(os.path.expanduser(raw))+ if path and os.path.isfile(path):+ resolved.append(path)except Exception:continue- if path and os.path.isfile(path):- resolved.append(path)return resolved@@ -609,10 +589,8 @@ def _prepend_shell_init(cmd_string: str, files: list[str]) -> str:errors, ``2>/dev/null`` hides noisy prompts, ``|| true`` neutralises the status."""if not files:return cmd_string- prelude = ["set +e"]- for path in files:- safe = path.replace("'", "'\\''")- prelude.append(f"[ -r '{safe}' ] && . '{safe}' 2>/dev/null || true")+ safe = [p.replace("'", "'\\''") for p in files]+ prelude = ["set +e", *(f"[ -r '{p}' ] && . '{p}' 2>/dev/null || true" for p in safe)]return "\n".join(prelude) + "\n" + cmd_string@@ -649,7 +627,7 @@ def _sweep_escaped_descendants(descendants: list, pgid: int) -> None:try:if os.getpgid(child.pid) == pgid:continue # group-kill already covers it- except (ProcessLookupError, PermissionError, OSError):+ except OSError: # ProcessLookupError / PermissionError includedpasschild.kill()except Exception:@@ -664,8 +642,7 @@ def _kill_process_group_posix(proc) -> None:try:pgid = os.getpgid(proc.pid)except ProcessLookupError:- pgid = getattr(proc, "_hermes_pgid", None)- if pgid is None:+ if (pgid := getattr(proc, "_hermes_pgid", None)) is None:raisetry: # psutil children snapshot; empty on any failure (must never break the kill)import psutil@@ -678,10 +655,8 @@ def _kill_process_group_posix(proc) -> None:if not _wait_for_group_exit(proc, pgid, 1.0):os.killpg(pgid, signal.SIGKILL)_wait_for_group_exit(proc, pgid, 2.0)- try:+ with contextlib.suppress(subprocess.TimeoutExpired, OSError):proc.wait(timeout=0.2)- except (subprocess.TimeoutExpired, OSError):- passexcept ProcessLookupError:pass_sweep_escaped_descendants(descendants, pgid)@@ -694,10 +669,8 @@ def _kill_process_windows(proc) -> None:terminate_pid(proc.pid, force=True, expected_start_time=get_process_start_time(proc.pid))except Exception:proc.kill()- try:+ with contextlib.suppress(subprocess.TimeoutExpired, OSError):proc.wait(timeout=2.0)- except (subprocess.TimeoutExpired, OSError):- passclass LocalEnvironment(BaseEnvironment):@@ -776,8 +749,7 @@ class LocalEnvironment(BaseEnvironment):safe_cwd = _resolve_safe_cwd(self.cwd)if safe_cwd == self.cwd:return- normalized = _msys_to_windows_path(self.cwd) if _IS_WINDOWS else self.cwd- if safe_cwd != normalized:+ if safe_cwd != _msys_to_windows_path(self.cwd):logger.warning("LocalEnvironment cwd %r is missing on disk; ""falling back to %r so terminal commands keep working.",@@ -792,19 +764,16 @@ class LocalEnvironment(BaseEnvironment):if login:cmd_string = _prepend_shell_init(cmd_string, _resolve_shell_init_files())args = [bash, *(["-l"] if login else []), "-c", cmd_string]- run_env = _make_run_env(self.env)self._recover_cwd()proc = subprocess.Popen(- args, text=True, env=run_env, encoding="utf-8", errors="replace",+ args, text=True, env=_make_run_env(self.env), encoding="utf-8", errors="replace",stdout=subprocess.PIPE, stderr=subprocess.STDOUT,stdin=subprocess.PIPE if stdin_data is not None else subprocess.DEVNULL,start_new_session=True, cwd=self.cwd,**({"creationflags": windows_hide_flags()} if _IS_WINDOWS else {}))if not _IS_WINDOWS:- try:+ with contextlib.suppress(ProcessLookupError):proc._hermes_pgid = os.getpgid(proc.pid)- except ProcessLookupError:- passif stdin_data is not None:_pipe_stdin(proc, stdin_data)return proc@@ -812,15 +781,10 @@ class LocalEnvironment(BaseEnvironment):def _kill_process(self, proc):"""Kill the entire process group (all children)."""try:- if _IS_WINDOWS:- _kill_process_windows(proc)- else:- _kill_process_group_posix(proc)- except (ProcessLookupError, PermissionError, OSError):- try:+ (_kill_process_windows if _IS_WINDOWS else _kill_process_group_posix)(proc)+ except OSError: # ProcessLookupError / PermissionError included+ with contextlib.suppress(Exception):proc.kill()- except Exception:- passdef _extract_cwd_from_output(self, result: dict):"""Base semantics plus: Git Bash ``pwd -P`` emits MSYS form on Windows —@@ -830,7 +794,7 @@ class LocalEnvironment(BaseEnvironment):prev_cwd = self.cwdsuper()._extract_cwd_from_output(result)if self.cwd != prev_cwd:- normalized = _msys_to_windows_path(self.cwd) if _IS_WINDOWS else self.cwd+ normalized = _msys_to_windows_path(self.cwd)if normalized and os.path.isdir(normalized):self.cwd = normalizedresult["cwd"] = normalized@@ -848,7 +812,5 @@ class LocalEnvironment(BaseEnvironment):except Exception:stale = []for f in (self._snapshot_path, self._cwd_file, *stale):- try:+ with contextlib.suppress(OSError):os.unlink(f)- except OSError:- passdiff --git a/tools/environments/local_env_policy.py b/tools/environments/local_env_policy.pyindex 7d4120d2276..d6ead6fbdaf 100644--- a/tools/environments/local_env_policy.py+++ b/tools/environments/local_env_policy.py@@ -1,7 +1,6 @@-"""Secret-scrub policy for Hermes child processes: pure data + predicates for-which env names are Hermes-managed credentials. The env *builders* applying it-(``_sanitize_subprocess_env``, ``_make_run_env``, ``hermes_subprocess_env``,-``build_subprocess_env``) live in ``tools.environments.local``."""+"""Secret-scrub policy for Hermes child processes: pure data + predicates for which env+names are Hermes-managed credentials. The env *builders* applying it (``_make_run_env``,+``_sanitize_subprocess_env``, ``hermes_subprocess_env``) live in ``tools.environments.local``."""import os@@ -9,14 +8,11 @@ import os_HERMES_PROVIDER_ENV_FORCE_PREFIX = "_HERMES_FORCE_"# Hermes-managed AWS *inference* credentials for ``auth_type="aws_sdk"`` (Bedrock):-# deliberately only the Bedrock bearer token, which no aws/terraform/boto3 toolchain-# uses. The general AWS credential chain stays inheritable on purpose — the local-# terminal is the user's trusted operator shell (SECURITY.md §3.2) and env_passthrough-# can never re-allow a blocklisted name (GHSA-rhgp-j443-p4rf), so blocking it would-# be unrecoverable for every aws/terraform user.-_AWS_SDK_CREDENTIAL_ENV_VARS = frozenset({- "AWS_BEARER_TOKEN_BEDROCK",-})+# only the Bedrock bearer token, which no aws/terraform/boto3 toolchain uses. The+# general AWS chain stays inheritable on purpose — the local terminal is the user's+# trusted operator shell (SECURITY.md §3.2) and env_passthrough can never re-allow a+# blocklisted name (GHSA-rhgp-j443-p4rf), so blocking it would be unrecoverable.+_AWS_SDK_CREDENTIAL_ENV_VARS = frozenset({"AWS_BEARER_TOKEN_BEDROCK"})_STATIC_PROVIDER_ENV_BLOCKLIST = frozenset({"OPENAI_BASE_URL", "OPENAI_API_KEY", "OPENAI_API_BASE", "OPENAI_ORG_ID",@@ -63,8 +59,7 @@ def _build_provider_env_blocklist() -> frozenset:for name, metadata in OPTIONAL_ENV_VARS.items():category = metadata.get("category")if category in {"tool", "messaging"} or (- category == "setting" and metadata.get("password")- ):+ category == "setting" and metadata.get("password")):blocked.add(name)except ImportError:pass@@ -87,8 +82,8 @@ _HERMES_PROVIDER_ENV_BLOCKLIST = _build_provider_env_blocklist()# env_passthrough registration stay sealed (GHSA-rhgp-j443-p4rf). CONTEXT-GATED via# ``_buzz_terminal_context_active``: a Telegram/CLI/cron session on a host that also# runs a Buzz gateway must not get the signing key. Values are used directly, never-# scope-resolved (UnscopedSecretError under multiplex), and the snapshot treats them-# as profile-scoped. Prefix-based so future BUZZ_* names need no code change.+# scope-resolved (UnscopedSecretError under multiplex); the snapshot treats them as+# profile-scoped. Prefix-based so future BUZZ_* names need no code change._TERMINAL_FIRST_PARTY_ENV_PREFIXES = ("BUZZ_",)@@ -99,11 +94,10 @@ def _matches_terminal_first_party_prefix(name: str) -> bool:def _buzz_terminal_context_active() -> bool:- """True when this process/session operates as a Buzz agent: ``BUZZ_MANAGED_AGENT``- in the process env (set only by Buzz Desktop's buzz-acp harness), or the live- session's platform is ``buzz`` via the gateway ContextVar — authoritative under- a concurrent multi-session host, so a sibling Telegram session resolves its OWN- platform."""+ """True when this process/session operates as a Buzz agent: ``BUZZ_MANAGED_AGENT`` in+ the process env (set only by Buzz Desktop's buzz-acp harness), or the live session's+ platform is ``buzz`` via the gateway ContextVar — authoritative under a concurrent+ multi-session host, so a sibling Telegram session resolves its OWN platform."""if os.environ.get("BUZZ_MANAGED_AGENT"):return Truetry:@@ -120,11 +114,10 @@ def _is_terminal_first_party_env(name: str) -> bool:return _matches_terminal_first_party_prefix(name) and _buzz_terminal_context_active()-# Active-venv markers that must NOT leak: a leaked VIRTUAL_ENV/CONDA_PREFIX makes-# uv/poetry sync ANOTHER project's deps into the Hermes venv (the venv stays-# reachable via PATH so stripping is safe), and PYTHONHOME redirects any child-# interpreter's stdlib to the Hermes venv (version-mismatch crashes). PYTHONPATH is-# handled separately — only Hermes-owned entries are removed.+# Active-venv markers that must NOT leak: VIRTUAL_ENV/CONDA_PREFIX make uv/poetry sync+# ANOTHER project's deps into the Hermes venv (still reachable via PATH, so stripping+# is safe); PYTHONHOME redirects a child interpreter's stdlib to the Hermes venv+# (version-mismatch crashes). PYTHONPATH is handled separately (Hermes-owned entries only)._ACTIVE_VENV_MARKER_VARS = ("VIRTUAL_ENV", "CONDA_PREFIX", "PYTHONHOME")@@ -152,10 +145,9 @@ def _plugin_terminal_env_strip_keys() -> frozenset:return frozenset()-# Tier-1 secrets: stripped from EVERY spawned subprocess even under-# inherit_credentials (claude/codex/gemini). Not provider credentials — no child-# needs them and they are the highest-value secrets to keep from a compromised-# dependency. Provider keys are the conditional Tier-2 strip.+# Tier-1 secrets: stripped from EVERY spawned subprocess even under inherit_credentials+# (claude/codex/gemini). Not provider credentials — no child needs them and they are the+# highest-value secrets to keep from a compromised dependency. Provider keys = Tier 2._ALWAYS_STRIP_KEYS: frozenset[str] = frozenset({# GitHub auth"GH_TOKEN", "GITHUB_TOKEN", "GITHUB_APP_ID", "GITHUB_APP_PRIVATE_KEY_PATH",diff --git a/tools/environments/local_gitbash_probe.py b/tools/environments/local_gitbash_probe.pyindex 5007cb1d4b9..eed5eb083f7 100644--- a/tools/environments/local_gitbash_probe.py+++ b/tools/environments/local_gitbash_probe.py@@ -37,27 +37,19 @@ def _mandatory_aslr_enabled() -> "bool | None":global _mandatory_aslr_enabled_cacheif _mandatory_aslr_enabled_cache is not None:return _mandatory_aslr_enabled_cache+ cmd = [shutil.which("powershell.exe") or "powershell.exe", "-NoProfile", "-NonInteractive",+ "-Command", "(Get-ProcessMitigation -System).Aslr.ForceRelocateImages.ToString()"]try:- result = subprocess.run(- [- shutil.which("powershell.exe") or "powershell.exe",- "-NoProfile", "-NonInteractive", "-Command",- "(Get-ProcessMitigation -System).Aslr.ForceRelocateImages.ToString()",- ],- capture_output=True, text=True, encoding="utf-8", errors="replace",- timeout=10, creationflags=windows_hide_flags(),- )- if result.returncode != 0:- return None- value = (result.stdout or "").strip().upper()- if value == "ON":- _mandatory_aslr_enabled_cache = True- elif value in {"OFF", "NOTSET"}:- _mandatory_aslr_enabled_cache = False- return _mandatory_aslr_enabled_cache+ result = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8",+ errors="replace", timeout=10, creationflags=windows_hide_flags())except Exception as exc:logger.debug("Could not query Windows Mandatory ASLR state: %s", exc)- return None+ return None+ if result.returncode != 0:+ return None+ value = (result.stdout or "").strip().upper()+ _mandatory_aslr_enabled_cache = {"ON": True, "OFF": False, "NOTSET": False}.get(value)+ return _mandatory_aslr_enabled_cachedef _git_root_from_bash(bash: str) -> str:@@ -92,15 +84,13 @@ def _bash_starts(bash: str) -> bool:"""True if *bash* can launch external MSYS programs (cached per path).``--noprofile --norc`` so a broken login post-install (``Directory\\drivers\\etc``) does not falsely condemn an otherwise usable bash."""- cached = _bash_starts_cache.get(bash)- if cached is not None:- return cached+ if bash in _bash_starts_cache:+ return _bash_starts_cache[bash]try:result = subprocess.run([bash, "--noprofile", "--norc", "-c", _BASH_EXTERNAL_PROGRAM_PROBE],capture_output=True, text=True, encoding="utf-8", errors="replace",- timeout=15, creationflags=windows_hide_flags() if _IS_WINDOWS else 0,- )+ timeout=15, creationflags=windows_hide_flags() if _IS_WINDOWS else 0)ok = result.returncode == 0if not ok:combined = f"{result.stdout or ''}{result.stderr or ''}".strip()diff --git a/tools/environments/local_pythonpath.py b/tools/environments/local_pythonpath.pyindex 8874b118724..54a46b7bd2f 100644--- a/tools/environments/local_pythonpath.py+++ b/tools/environments/local_pythonpath.py@@ -1,13 +1,10 @@-"""Hermes-owned PYTHONPATH stripping for child processes.--Launchers prepend the repo root and the Hermes venv's site-packages so the backend-can ``import tools``; leaked into a child Python of a DIFFERENT version they load-the backend's C extensions and crash. Only entries proven Hermes-owned by *path-provenance* are removed — never by a cross-version heuristic. Module state-(``_hermes_repo_root_aliases``, ``_in_venv``, ``_hermes_site_packages``) stays in-``tools.environments.local`` (read via :func:`_state`) so tests monkeypatching it-there keep working.-"""+"""Hermes-owned PYTHONPATH stripping for child processes. Launchers prepend the repo+root and the Hermes venv's site-packages so the backend can ``import tools``; leaked+into a child Python of a DIFFERENT version they load the backend's C extensions and+crash. Only entries proven Hermes-owned by *path provenance* are removed — never by a+cross-version heuristic. Module state (``_hermes_repo_root_aliases``, ``_in_venv``,+``_hermes_site_packages``) lives in ``tools.environments.local`` (via :func:`_state`)+so tests monkeypatching it there keep working."""import loggingimport os@@ -49,7 +46,6 @@ def _build_hermes_repo_root_aliases(home_candidates = [configured_home]if configured_home.parent.name == "profiles":home_candidates.append(configured_home.parent.parent)-for home in home_candidates:try:resolved_home = home.resolve()@@ -58,7 +54,6 @@ def _build_hermes_repo_root_aliases(candidates.append(home / os.path.relpath(str(resolved_root), str(resolved_home)))except (OSError, ValueError):pass-# Repo-level junction recovery (commonpath raises across drives, so the# home-relative mapping above cannot express a cross-drive link).for home in home_candidates:@@ -79,11 +74,9 @@ def _validated_runtime_venv(env: dict) -> Path | None:"""Producer-owned runtime venv identified by VIRTUAL_ENV, or None. The variablealone is not provenance (users carry unrelated venvs): require the legacy Windowsbase-Python producer's exact ``<repo>/venv`` layout AND a real ``pyvenv.cfg``."""- value = env.get("VIRTUAL_ENV")- if not value:- return None- candidate = Path(value)- if not any(_same_path(candidate, root / "venv") for root in _state()._hermes_repo_root_aliases):+ candidate = Path(env.get("VIRTUAL_ENV") or "")+ if not env.get("VIRTUAL_ENV") or not any(+ _same_path(candidate, root / "venv") for root in _state()._hermes_repo_root_aliases):return Nonetry:return candidate if (candidate / "pyvenv.cfg").is_file() else None@@ -120,10 +113,9 @@ def _get_hermes_site_packages(env: dict) -> list[Path]:def _strip_hermes_owned_pythonpath_and_runtime_markers(env: dict) -> None:- """Strip Hermes-owned PYTHONPATH entries, then the runtime marker vars. Ordering- is load-bearing: PYTHONPATH filtering runs BEFORE the markers are removed so a- validated Windows base-interpreter launch (VIRTUAL_ENV -> <repo>/venv) can- still prove ownership."""+ """Strip Hermes-owned PYTHONPATH entries, then the runtime marker vars. Order is+ load-bearing: PYTHONPATH filtering runs BEFORE the markers go so a validated Windows+ base-interpreter launch (VIRTUAL_ENV -> <repo>/venv) can still prove ownership."""_strip_hermes_owned_pythonpath(env)for _marker in _ACTIVE_VENV_MARKER_VARS:env.pop(_marker, None)@@ -137,11 +129,9 @@ def _strip_hermes_owned_pythonpath(env: dict) -> None:if not pp:returnowned_paths = [*_get_hermes_site_packages(env), *_state()._hermes_repo_root_aliases]- kept: list[str] = []- stripped: list[str] = []- for entry in pp.split(os.pathsep):- owned = entry != "" and any(_same_path(Path(entry), p) for p in owned_paths)- (stripped if owned else kept).append(entry)+ entries = pp.split(os.pathsep)+ stripped = [e for e in entries if e and any(_same_path(Path(e), p) for p in owned_paths)]+ kept = [e for e in entries if e not in stripped]if kept:env["PYTHONPATH"] = os.pathsep.join(kept)else: