From 3ff274aaa8e718009e61386c507feb8de71afb6a Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Tue, 19 May 2026 17:17:59 -0700 Subject: [PATCH 01/11] test: cover bash integration job notifications --- .github/workflows/ci.yml | 1 + ..._shell_bash_background_helpers_disowned.py | 217 ++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 tests/test_shell_bash_background_helpers_disowned.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8bc1173ae14a..0c82d07f113e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -317,6 +317,7 @@ jobs: CMUX_CLI_BIN="$CLI_BIN" python3 tests/test_cli_socket_operation_deadline.py CMUX_CLI_BIN="$CLI_BIN" python3 tests/test_cli_omo_openagent_plugin_migration.py CMUX_CLI_BIN="$CLI_BIN" python3 tests/test_cli_socket_autodiscovery.py + python3 tests/test_shell_bash_background_helpers_disowned.py python3 tests/test_claude_wrapper_hooks.py CMUX_CLI_BIN="$CLI_BIN" python3 tests/test_claude_hook_stop_last_assistant.py CMUX_CLI_BIN="$CLI_BIN" python3 tests/test_claude_hook_clear_running_status.py diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py new file mode 100644 index 000000000000..9877a1b42c99 --- /dev/null +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +""" +Regression: bash shell integration background helpers must not leak job +completion notices into the user's terminal output. +""" + +from __future__ import annotations + +import os +import re +import select +import shutil +import socket +import stat +import subprocess +import tempfile +import time +from pathlib import Path + + +ROOT = Path(__file__).resolve().parents[1] +SHELL_DIR = ROOT / "Resources" / "shell-integration" +ANSI_ESCAPE_RE = re.compile(r"\x1b\[[0-?]*[ -/]*[@-~]") +DONE_LINE_RE = re.compile(r"^\[[0-9]+\](?:[+-]|\s)\s+Done\b") + + +def _bash_version(path: str) -> tuple[int, ...] | None: + try: + result = subprocess.run( + [path, "--version"], + capture_output=True, + text=True, + timeout=4, + check=False, + ) + except (OSError, subprocess.TimeoutExpired): + return None + + if result.returncode != 0: + return None + + match = re.search(r"version\s+([0-9]+(?:\.[0-9]+)*)", result.stdout) + if not match: + return None + return tuple(int(part) for part in match.group(1).split(".")) + + +def _find_bash_with_inline_ps0() -> str | None: + candidates = [ + os.environ.get("CMUX_TEST_BASH", ""), + shutil.which("bash") or "", + "/opt/homebrew/bin/bash", + "/usr/local/bin/bash", + "/bin/bash", + ] + seen: set[str] = set() + for candidate in candidates: + if not candidate or candidate in seen: + continue + seen.add(candidate) + version = _bash_version(candidate) + if version is not None and version >= (5, 3): + return candidate + return None + + +class BoundUnixSocket: + def __init__(self, path: Path) -> None: + self.path = path + self.sock: socket.socket | None = None + + def __enter__(self) -> "BoundUnixSocket": + self.path.parent.mkdir(parents=True, exist_ok=True) + self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.sock.bind(str(self.path)) + self.sock.listen(16) + self.sock.setblocking(False) + return self + + def accept_pending(self) -> None: + if self.sock is None: + return + while True: + try: + conn, _ = self.sock.accept() + except BlockingIOError: + return + conn.close() + + def __exit__(self, exc_type, exc, tb) -> None: + if self.sock is not None: + self.sock.close() + try: + self.path.unlink() + except FileNotFoundError: + pass + + +def _write_executable(path: Path, contents: str) -> None: + path.write_text(contents, encoding="utf-8") + path.chmod(path.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) + + +def _clean_line(raw: str) -> str: + return ANSI_ESCAPE_RE.sub("", raw).replace("\r", "").strip() + + +def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: + socket_path = tmp / "cmux.sock" + bin_dir = tmp / "bin" + bin_dir.mkdir(parents=True, exist_ok=True) + + fake_send_tool = "#!/bin/sh\nsleep 0.05\nexit 0\n" + for tool in ("ncat", "socat", "nc"): + _write_executable(bin_dir / tool, fake_send_tool) + + env = dict(os.environ) + env.update( + { + "PATH": f"{bin_dir}:/usr/bin:/bin:/usr/sbin:/sbin", + "CMUX_SOCKET_PATH": str(socket_path), + "CMUX_WORKSPACE_ID": "workspace-bash-done-noise", + "CMUX_TAB_ID": "tab-bash-done-noise", + "CMUX_PANEL_ID": "panel-bash-done-noise", + "PS1": "CMUX_TEST_PROMPT> ", + "PROMPT_COMMAND": "", + } + ) + + with BoundUnixSocket(socket_path) as bound_socket: + pid, fd = os.forkpty() + if pid == 0: + os.chdir(str(ROOT)) + os.execve(bash_path, [bash_path, "--noprofile", "--norc", "-i"], env) + + commands = "\n".join( + [ + f'source "{SHELL_DIR / "cmux-bash-integration.bash"}"', + "echo OK", + "sleep 0.2", + "echo AFTER", + "sleep 0.2", + "exit 0", + "", + ] + ) + os.write(fd, commands.encode()) + + output = bytearray() + deadline = time.time() + 8 + exit_status = 1 + while time.time() < deadline: + bound_socket.accept_pending() + readable, _, _ = select.select([fd], [], [], 0.05) + if fd in readable: + try: + chunk = os.read(fd, 4096) + except OSError: + break + if not chunk: + break + output.extend(chunk) + + waited_pid, status = os.waitpid(pid, os.WNOHANG) + if waited_pid == pid: + exit_status = os.waitstatus_to_exitcode(status) + break + else: + try: + os.kill(pid, 15) + except ProcessLookupError: + pass + try: + os.waitpid(pid, 0) + except ChildProcessError: + pass + return 124, output.decode(errors="replace") + + return exit_status, output.decode(errors="replace") + + +def main() -> int: + bash_path = _find_bash_with_inline_ps0() + if bash_path is None: + print("SKIP: no Bash >= 5.3 found for inline PS0 regression coverage") + return 0 + + with tempfile.TemporaryDirectory(prefix="cmux-bash-done-noise-") as td: + rc, output = _run_interactive_bash(bash_path, Path(td)) + + if rc == 124: + print("FAIL: timed out waiting for interactive bash to exit") + print(output) + return 1 + + if "OK" not in output or "AFTER" not in output: + print(f"FAIL: interactive bash did not run the probe commands, rc={rc}") + print(output) + return 1 + + done_lines = [ + line + for line in (_clean_line(raw) for raw in output.splitlines()) + if DONE_LINE_RE.match(line) + ] + if done_lines: + print("FAIL: cmux bash integration leaked background job completion notices") + for line in done_lines: + print(line) + return 1 + + print("PASS: bash integration background helpers stay out of the user-visible job table") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) From 5c0cf65752efd98c5e0d9e5d926716fe22eb9150 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Tue, 19 May 2026 17:18:55 -0700 Subject: [PATCH 02/11] fix: hide bash integration helper jobs --- .../cmux-bash-integration.bash | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/Resources/shell-integration/cmux-bash-integration.bash b/Resources/shell-integration/cmux-bash-integration.bash index 6d5c0f65c542..5be0508cdf14 100644 --- a/Resources/shell-integration/cmux-bash-integration.bash +++ b/Resources/shell-integration/cmux-bash-integration.bash @@ -32,6 +32,26 @@ _cmux_send() { esac } +_CMUX_BASH_PS0_INLINE_ACTIVE="${_CMUX_BASH_PS0_INLINE_ACTIVE:-0}" +_cmux_run_bg() { + if [[ "${_CMUX_BASH_PS0_INLINE_ACTIVE:-0}" == "1" ]]; then + # Bash 5.3's inline PS0 expansion leaks background jobs into the + # interactive job table. Start the helper from a command-substitution + # subshell so the user shell never owns the job notification. + : "$("$@" >/dev/null 2>&1 & disown "$!" 2>/dev/null || true)" + return 0 + fi + + "$@" >/dev/null 2>&1 & + local bg_pid=$! + disown "$bg_pid" 2>/dev/null || disown 2>/dev/null || true +} + +_cmux_send_bg() { + local payload="$1" + _cmux_run_bg _cmux_send "$payload" +} + _cmux_socket_is_unix() { [[ -n "$CMUX_SOCKET_PATH" && -S "$CMUX_SOCKET_PATH" ]] } @@ -72,10 +92,7 @@ _cmux_relay_rpc_bg() { local relay_cli="" _cmux_socket_uses_remote_relay || return 1 relay_cli="$(_cmux_relay_cli_path)" || return 1 - { - "$relay_cli" rpc "$method" "$params" >/dev/null 2>&1 || true - } >/dev/null 2>&1 & - disown 2>/dev/null || true + _cmux_run_bg "$relay_cli" rpc "$method" "$params" } _cmux_relay_rpc() { @@ -380,9 +397,7 @@ _cmux_report_tty_once() { payload="$(_cmux_report_tty_payload)" [[ -n "$payload" ]] || return 0 _CMUX_TTY_REPORTED=1 - { - _cmux_send "$payload" - } >/dev/null 2>&1 & disown + _cmux_send_bg "$payload" else [[ -n "$_CMUX_TTY_NAME" ]] || return 0 # Keep the first relay TTY report synchronous so the server can resolve @@ -400,9 +415,7 @@ _cmux_report_shell_activity_state() { [[ -n "$CMUX_PANEL_ID" ]] || return 0 [[ "$_CMUX_SHELL_ACTIVITY_LAST" == "$state" ]] && return 0 _CMUX_SHELL_ACTIVITY_LAST="$state" - { - _cmux_send "report_shell_state $state --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID" - } >/dev/null 2>&1 & disown + _cmux_send_bg "report_shell_state $state --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID" } _cmux_reset_terminal_keyboard_protocols() { @@ -423,9 +436,7 @@ _cmux_ports_kick() { fi _CMUX_PORTS_LAST_RUN="$(_cmux_now)" if _cmux_socket_is_unix; then - { - _cmux_send "ports_kick --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID --reason=$reason" - } >/dev/null 2>&1 & disown + _cmux_send_bg "ports_kick --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID --reason=$reason" else _cmux_ports_kick_via_relay "$reason" fi @@ -521,9 +532,7 @@ _cmux_emit_pr_command_hint() { local quoted_target="${_CMUX_LAST_PR_TARGET//\"/\\\"}" payload+=" --target=\"$quoted_target\"" fi - { - _cmux_send "$payload" - } >/dev/null 2>&1 & disown + _cmux_send_bg "$payload" _CMUX_LAST_PR_ACTION="" _CMUX_LAST_PR_TARGET="" } @@ -1012,10 +1021,8 @@ _cmux_prompt_command() { # CWD: keep the app in sync with the actual shell directory. if [[ "$pwd" != "$_CMUX_PWD_LAST_PWD" ]]; then _CMUX_PWD_LAST_PWD="$pwd" - { - local qpwd="${pwd//\"/\\\"}" - _cmux_send "report_pwd \"${qpwd}\" --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID" - } >/dev/null 2>&1 & disown + local qpwd="${pwd//\"/\\\"}" + _cmux_send_bg "report_pwd \"${qpwd}\" --tab=$CMUX_TAB_ID --panel=$CMUX_PANEL_ID" fi # Branch can change via aliases/tools while an older probe is still in flight. @@ -1074,7 +1081,7 @@ _cmux_prompt_command() { fi } >/dev/null 2>&1 & _CMUX_GIT_JOB_PID=$! - disown + disown "$_CMUX_GIT_JOB_PID" 2>/dev/null || disown 2>/dev/null || true _CMUX_GIT_JOB_STARTED_AT=$now fi @@ -1123,9 +1130,9 @@ _cmux_install_prompt_command() { esac fi - if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then + if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) )); then - builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_hook "$BASH_COMMAND"; }' + builtin readonly _CMUX_BASH_PS0='${ _CMUX_BASH_PS0_INLINE_ACTIVE=1; _cmux_bash_preexec_hook "$BASH_COMMAND"; _CMUX_BASH_PS0_INLINE_ACTIVE=0; }' else builtin readonly _CMUX_BASH_PS0='$(_cmux_bash_preexec_hook "$BASH_COMMAND" >/dev/null)' fi From c806464dcf080b435537acadb41b973b69d298fa Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Tue, 19 May 2026 17:24:52 -0700 Subject: [PATCH 03/11] test: require modern bash coverage in ci --- .github/workflows/ci.yml | 25 +++++++++++++++ ..._shell_bash_background_helpers_disowned.py | 31 ++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0c82d07f113e..01d5b9ecca53 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -296,6 +296,31 @@ jobs: - name: Setup Bun uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + - name: Ensure Bash 5.3 for shell integration regression + run: | + set -euo pipefail + + candidates=("/opt/homebrew/bin/bash" "/usr/local/bin/bash" "$(command -v bash || true)") + for candidate in "${candidates[@]}"; do + if [ -x "$candidate" ] && "$candidate" -c '(( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) ))'; then + echo "CMUX_TEST_BASH=$candidate" >> "$GITHUB_ENV" + "$candidate" --version | head -1 + exit 0 + fi + done + + brew install bash + for candidate in /opt/homebrew/bin/bash /usr/local/bin/bash; do + if [ -x "$candidate" ] && "$candidate" -c '(( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) ))'; then + echo "CMUX_TEST_BASH=$candidate" >> "$GITHUB_ENV" + "$candidate" --version | head -1 + exit 0 + fi + done + + echo "Bash >= 5.3 unavailable after brew install" >&2 + exit 1 + - name: Run CLI no-socket regressions run: | set -euo pipefail diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index 9877a1b42c99..9008092f6fb3 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -15,6 +15,7 @@ import subprocess import tempfile import time +from contextlib import suppress from pathlib import Path @@ -69,7 +70,7 @@ def __init__(self, path: Path) -> None: self.path = path self.sock: socket.socket | None = None - def __enter__(self) -> "BoundUnixSocket": + def __enter__(self) -> BoundUnixSocket: self.path.parent.mkdir(parents=True, exist_ok=True) self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sock.bind(str(self.path)) @@ -105,6 +106,22 @@ def _clean_line(raw: str) -> str: return ANSI_ESCAPE_RE.sub("", raw).replace("\r", "").strip() +def _drain_pty(fd: int, output: bytearray, *, quiet_after: float = 0.2) -> None: + deadline = time.time() + quiet_after + while time.time() < deadline: + readable, _, _ = select.select([fd], [], [], 0.05) + if fd not in readable: + continue + try: + chunk = os.read(fd, 4096) + except OSError: + return + if not chunk: + return + output.extend(chunk) + deadline = time.time() + quiet_after + + def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: socket_path = tmp / "cmux.sock" bin_dir = tmp / "bin" @@ -164,16 +181,13 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: waited_pid, status = os.waitpid(pid, os.WNOHANG) if waited_pid == pid: exit_status = os.waitstatus_to_exitcode(status) + _drain_pty(fd, output) break else: - try: + with suppress(ProcessLookupError): os.kill(pid, 15) - except ProcessLookupError: - pass - try: + with suppress(ChildProcessError): os.waitpid(pid, 0) - except ChildProcessError: - pass return 124, output.decode(errors="replace") return exit_status, output.decode(errors="replace") @@ -182,6 +196,9 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: def main() -> int: bash_path = _find_bash_with_inline_ps0() if bash_path is None: + if os.environ.get("CI"): + print("FAIL: CI must provide Bash >= 5.3 for inline PS0 regression coverage") + return 1 print("SKIP: no Bash >= 5.3 found for inline PS0 regression coverage") return 0 From 4129f50fd42d43c26d61bde13d7dd6a74120b818 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Tue, 26 May 2026 11:57:56 -0700 Subject: [PATCH 04/11] fix: harden bash integration regression harness --- .../cmux-bash-integration.bash | 10 ++++++- ..._shell_bash_background_helpers_disowned.py | 27 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Resources/shell-integration/cmux-bash-integration.bash b/Resources/shell-integration/cmux-bash-integration.bash index ce63816c792c..b02a245af543 100644 --- a/Resources/shell-integration/cmux-bash-integration.bash +++ b/Resources/shell-integration/cmux-bash-integration.bash @@ -1212,6 +1212,14 @@ _cmux_bash_preexec_hook() { _cmux_preexec_command "$@" } +_cmux_bash_preexec_inline_ps0() { + _CMUX_BASH_PS0_INLINE_ACTIVE=1 + { + _cmux_bash_preexec_hook "$BASH_COMMAND" + _CMUX_BASH_PS0_INLINE_ACTIVE=0 + } || _CMUX_BASH_PS0_INLINE_ACTIVE=0 +} + _cmux_prompt_command() { local last_status=$? _cmux_tmux_sync_cmux_environment @@ -1393,7 +1401,7 @@ _cmux_install_prompt_command() { if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) )); then - builtin readonly _CMUX_BASH_PS0='${ _CMUX_BASH_PS0_INLINE_ACTIVE=1; _cmux_bash_preexec_hook "$BASH_COMMAND"; _CMUX_BASH_PS0_INLINE_ACTIVE=0; }' + builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_inline_ps0; }' else builtin readonly _CMUX_BASH_PS0='$(_cmux_bash_preexec_hook "$BASH_COMMAND" >/dev/null)' fi diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index 9008092f6fb3..ce33e0e69bd4 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -10,6 +10,7 @@ import re import select import shutil +import signal import socket import stat import subprocess @@ -122,6 +123,26 @@ def _drain_pty(fd: int, output: bytearray, *, quiet_after: float = 0.2) -> None: deadline = time.time() + quiet_after +def _terminate_child(pid: int, *, grace: float = 0.5) -> None: + with suppress(ProcessLookupError): + os.kill(pid, signal.SIGTERM) + + deadline = time.time() + grace + while time.time() < deadline: + try: + waited_pid, _ = os.waitpid(pid, os.WNOHANG) + except ChildProcessError: + return + if waited_pid == pid: + return + time.sleep(0.05) + + with suppress(ProcessLookupError): + os.kill(pid, signal.SIGKILL) + with suppress(ChildProcessError): + os.waitpid(pid, 0) + + def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: socket_path = tmp / "cmux.sock" bin_dir = tmp / "bin" @@ -184,10 +205,8 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: _drain_pty(fd, output) break else: - with suppress(ProcessLookupError): - os.kill(pid, 15) - with suppress(ChildProcessError): - os.waitpid(pid, 0) + _terminate_child(pid) + _drain_pty(fd, output, quiet_after=0.05) return 124, output.decode(errors="replace") return exit_status, output.decode(errors="replace") From f4b586ab1f2b96462368855ccc5b96f885dfd6cf Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sat, 6 Jun 2026 02:16:29 -0700 Subject: [PATCH 05/11] fix: preserve bash ps0 command capture --- .../cmux-bash-integration.bash | 5 +-- ..._shell_bash_background_helpers_disowned.py | 33 ++++++++++++++++--- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Resources/shell-integration/cmux-bash-integration.bash b/Resources/shell-integration/cmux-bash-integration.bash index a272bd677564..3c4c839ee118 100644 --- a/Resources/shell-integration/cmux-bash-integration.bash +++ b/Resources/shell-integration/cmux-bash-integration.bash @@ -1366,9 +1366,10 @@ _cmux_bash_preexec_hook_subshell() { } _cmux_bash_preexec_inline_ps0() { + local cmd="${1:-${BASH_COMMAND:-}}" _CMUX_BASH_PS0_INLINE_ACTIVE=1 { - _cmux_bash_preexec_hook "$BASH_COMMAND" + _cmux_bash_preexec_hook "$cmd" _CMUX_BASH_PS0_INLINE_ACTIVE=0 } || _CMUX_BASH_PS0_INLINE_ACTIVE=0 } @@ -1547,7 +1548,7 @@ _cmux_install_prompt_command() { if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) )); then - builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_inline_ps0; }' + builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_inline_ps0 "$BASH_COMMAND"; }' else builtin readonly _CMUX_BASH_PS0='$(_cmux_bash_preexec_hook_subshell >/dev/null)' fi diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index ce33e0e69bd4..5ba2d19beb4b 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -143,14 +143,22 @@ def _terminate_child(pid: int, *, grace: float = 0.5) -> None: os.waitpid(pid, 0) -def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: +def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str, str]: socket_path = tmp / "cmux.sock" + send_log_path = tmp / "send.log" bin_dir = tmp / "bin" bin_dir.mkdir(parents=True, exist_ok=True) - fake_send_tool = "#!/bin/sh\nsleep 0.05\nexit 0\n" + fake_send_tool = ( + "#!/bin/sh\n" + 'cat >> "$CMUX_TEST_SEND_LOG"\n' + 'printf "\\n" >> "$CMUX_TEST_SEND_LOG"\n' + "sleep 0.05\n" + "exit 0\n" + ) for tool in ("ncat", "socat", "nc"): _write_executable(bin_dir / tool, fake_send_tool) + _write_executable(bin_dir / "gh", "#!/bin/sh\nexit 0\n") env = dict(os.environ) env.update( @@ -160,6 +168,7 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: "CMUX_WORKSPACE_ID": "workspace-bash-done-noise", "CMUX_TAB_ID": "tab-bash-done-noise", "CMUX_PANEL_ID": "panel-bash-done-noise", + "CMUX_TEST_SEND_LOG": str(send_log_path), "PS1": "CMUX_TEST_PROMPT> ", "PROMPT_COMMAND": "", } @@ -174,7 +183,9 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: commands = "\n".join( [ f'source "{SHELL_DIR / "cmux-bash-integration.bash"}"', + "_cmux_bash_history_command() { return 1; }", "echo OK", + "gh pr view 123", "sleep 0.2", "echo AFTER", "sleep 0.2", @@ -207,9 +218,12 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str]: else: _terminate_child(pid) _drain_pty(fd, output, quiet_after=0.05) - return 124, output.decode(errors="replace") + return 124, output.decode(errors="replace"), "" - return exit_status, output.decode(errors="replace") + send_log = "" + if send_log_path.exists(): + send_log = send_log_path.read_text(encoding="utf-8", errors="replace") + return exit_status, output.decode(errors="replace"), send_log def main() -> int: @@ -222,7 +236,7 @@ def main() -> int: return 0 with tempfile.TemporaryDirectory(prefix="cmux-bash-done-noise-") as td: - rc, output = _run_interactive_bash(bash_path, Path(td)) + rc, output, send_log = _run_interactive_bash(bash_path, Path(td)) if rc == 124: print("FAIL: timed out waiting for interactive bash to exit") @@ -234,6 +248,15 @@ def main() -> int: print(output) return 1 + expected_pr_action = ( + 'report_pr_action view --tab=tab-bash-done-noise ' + '--panel=panel-bash-done-noise --target="123"' + ) + if expected_pr_action not in send_log: + print("FAIL: Bash 5.3 PS0 preexec did not preserve the user command") + print(send_log) + return 1 + done_lines = [ line for line in (_clean_line(raw) for raw in output.splitlines()) From 9ca3ac72a29a52cf261b76d45b73cf9e3699dff7 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sat, 6 Jun 2026 02:19:12 -0700 Subject: [PATCH 06/11] fix: narrow ci workflow permissions --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 192219450b02..0f2afd36a980 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,6 @@ on: permissions: contents: read - actions: write concurrency: group: ${{ github.workflow }}-${{ github.ref }} From 10b87deef26ea45e461fc29fc9ce277bb8c90ee8 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sat, 6 Jun 2026 02:50:44 -0700 Subject: [PATCH 07/11] fix: use history for bash inline ps0 commands --- .../cmux-bash-integration.bash | 17 ++++++++++++----- ...st_shell_bash_background_helpers_disowned.py | 3 +-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Resources/shell-integration/cmux-bash-integration.bash b/Resources/shell-integration/cmux-bash-integration.bash index 3c4c839ee118..54542be3641c 100644 --- a/Resources/shell-integration/cmux-bash-integration.bash +++ b/Resources/shell-integration/cmux-bash-integration.bash @@ -1300,7 +1300,12 @@ _cmux_command_starts_nested_shell() { } _cmux_preexec_command() { - local cmd="${1:-${BASH_COMMAND:-}}" + local cmd="" + if (( $# > 0 )); then + cmd="$1" + else + cmd="${BASH_COMMAND:-}" + fi _cmux_tmux_sync_cmux_environment local cmux_has_unix_socket=0 @@ -1353,6 +1358,9 @@ _cmux_bash_history_command() { _cmux_bash_preexec_hook() { local cmd="${1:-}" local history_cmd="" + # Bash 5.3's inline `${ ...; }` PS0 updates BASH_COMMAND to cmux's + # own helper command before function arguments are expanded. Interactive + # history is the stable source for the user command on that path. history_cmd="$(_cmux_bash_history_command 2>/dev/null || true)" if [[ -n "$history_cmd" ]]; then cmd="$history_cmd" @@ -1366,10 +1374,9 @@ _cmux_bash_preexec_hook_subshell() { } _cmux_bash_preexec_inline_ps0() { - local cmd="${1:-${BASH_COMMAND:-}}" _CMUX_BASH_PS0_INLINE_ACTIVE=1 { - _cmux_bash_preexec_hook "$cmd" + _cmux_bash_preexec_hook _CMUX_BASH_PS0_INLINE_ACTIVE=0 } || _CMUX_BASH_PS0_INLINE_ACTIVE=0 } @@ -1548,9 +1555,9 @@ _cmux_install_prompt_command() { if (( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 4) )); then if (( BASH_VERSINFO[0] > 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 3) )); then - builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_inline_ps0 "$BASH_COMMAND"; }' + builtin readonly _CMUX_BASH_PS0='${ _cmux_bash_preexec_inline_ps0; }' else - builtin readonly _CMUX_BASH_PS0='$(_cmux_bash_preexec_hook_subshell >/dev/null)' + builtin readonly _CMUX_BASH_PS0='$(_cmux_bash_preexec_hook_subshell "$BASH_COMMAND" >/dev/null)' fi if [[ "$PS0" != *"${_CMUX_BASH_PS0}"* ]]; then PS0=$PS0"${_CMUX_BASH_PS0}" diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index 5ba2d19beb4b..75bc8d1dcdb2 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -183,7 +183,6 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str, str]: commands = "\n".join( [ f'source "{SHELL_DIR / "cmux-bash-integration.bash"}"', - "_cmux_bash_history_command() { return 1; }", "echo OK", "gh pr view 123", "sleep 0.2", @@ -253,7 +252,7 @@ def main() -> int: '--panel=panel-bash-done-noise --target="123"' ) if expected_pr_action not in send_log: - print("FAIL: Bash 5.3 PS0 preexec did not preserve the user command") + print("FAIL: Bash 5.3 PS0 preexec did not report the gh pr command") print(send_log) return 1 From c1aae09cd2caeebb22c172ccd80a47bc69971625 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sun, 14 Jun 2026 12:36:45 -0700 Subject: [PATCH 08/11] chore: regenerate Swift file length budget --- .github/swift-file-length-budget.tsv | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/swift-file-length-budget.tsv b/.github/swift-file-length-budget.tsv index ea13c6d70701..8f540e740bd7 100644 --- a/.github/swift-file-length-budget.tsv +++ b/.github/swift-file-length-budget.tsv @@ -20,8 +20,8 @@ 6074 Sources/TextBoxInput.swift 5925 cmuxTests/TerminalAndGhosttyTests.swift 5522 cmuxTests/BrowserConfigTests.swift -4921 Sources/cmuxApp.swift 5113 Packages/CmuxMobileShell/Sources/CmuxMobileShell/MobileShellComposite.swift +4921 Sources/cmuxApp.swift 4460 Sources/Panels/FilePreviewPanel.swift 4400 cmuxTests/BrowserPanelTests.swift 4227 Sources/BrowserWindowPortal.swift @@ -32,7 +32,7 @@ 3664 Packages/CmuxMobileTerminal/Sources/CmuxMobileTerminal/GhosttySurfaceView.swift 3397 Sources/CmuxConfig.swift 3331 cmuxTests/TabManagerSessionSnapshotTests.swift -3204 Sources/Update/UpdateTitlebarAccessory.swift +3200 Sources/Update/UpdateTitlebarAccessory.swift 2878 Sources/SessionIndexView.swift 2871 cmuxTests/CMUXOpenCommandTests.swift 2573 Sources/KeyboardShortcutSettings.swift @@ -59,11 +59,11 @@ 1652 cmuxTests/CMUXCLIErrorOutputRegressionTests.swift 1574 cmuxTests/MarkdownPanelTests.swift 1560 cmuxTests/TextBoxMentionCompletionTests.swift -1498 cmuxTests/OmnibarAndToolsTests.swift +1497 cmuxTests/OmnibarAndToolsTests.swift 1496 cmuxUITests/MultiWindowNotificationsUITests.swift 1446 Sources/FileExplorerStore.swift -1380 cmuxUITests/MenuKeyEquivalentRoutingUITests.swift 1382 cmuxTests/KeyboardShortcutSettingsFileStoreStartupTests.swift +1380 cmuxUITests/MenuKeyEquivalentRoutingUITests.swift 1373 cmuxTests/AppDelegateIssue2907RoutingTests.swift 1366 Sources/Feed/FeedButtonStyleDebugWindowController.swift 1362 Sources/CMUXInstalledExtensionSidebarHostView.swift @@ -92,9 +92,9 @@ 937 Sources/TextBoxMentionIndexStore.swift 934 Sources/App/ShortcutRoutingSupport.swift 926 Sources/DockPanelView.swift +920 Sources/CommandPalette/CommandPaletteSettingsToggle.swift 919 Packages/CmuxTerminal/Sources/CmuxTerminal/Surface/TerminalSurface+RuntimeLifecycle.swift 918 cmuxTests/WorkspaceGroupTests.swift -920 Sources/CommandPalette/CommandPaletteSettingsToggle.swift 905 Sources/CmuxSSHURLRequest.swift 901 Packages/CmuxSettingsUI/Sources/CmuxSettingsUI/Sections/AppSection.swift 893 Sources/WorkspaceContentView.swift @@ -117,13 +117,14 @@ 752 cmuxUITests/CloseWorkspaceCmdDUITests.swift 746 Sources/App/MenuBarExtraController.swift 738 Packages/CMUXProjectModel/Sources/CMUXProjectModel/XcodeProjectAdapter.swift -738 Packages/CmuxAuthRuntime/Sources/CmuxAuthRuntime/Coordinator/AuthCoordinator.swift +736 Packages/CmuxAuthRuntime/Sources/CmuxAuthRuntime/Coordinator/AuthCoordinator.swift +726 cmuxTests/CLICodexHookTimeoutRegressionTests.swift 716 Sources/TaskManagerSnapshot.swift 715 Packages/CmuxTerminal/Sources/CmuxTerminal/Surface/TerminalSurface+Input.swift 715 Sources/AppleScriptSupport.swift 710 Sources/TerminalSSHSessionDetector.swift -706 CLI/CMUXCLI+Config.swift 707 CLI/CMUXCLI+AgentHookDefinitions.swift +706 CLI/CMUXCLI+Config.swift 699 Sources/RightSidebarPanelView.swift 699 cmuxTests/TerminalNotificationClearAllTests.swift 698 cmuxTests/RestorableAgentHookProviderResumeTests.swift @@ -187,7 +188,6 @@ 528 cmuxTests/CLINotifyProcessTestSupport.swift 528 cmuxUITests/AutomationSocketUITests.swift 527 CLI/CLISocketPathResolver.swift -726 cmuxTests/CLICodexHookTimeoutRegressionTests.swift 523 Packages/CmuxRemoteSession/Sources/CmuxRemoteSession/Session/RemoteSessionCoordinator+PortScan.swift 520 CLI/CMUXCLI+AmpExtension.swift 520 cmuxTests/MainWindowVisibilityControllerTests.swift From 31bb0da11fe1536bbc45b3d4ecc1cc52e38d834b Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sun, 14 Jun 2026 13:21:53 -0700 Subject: [PATCH 09/11] test: cover bash helper background pid preservation --- ..._shell_bash_background_helpers_disowned.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index 75bc8d1dcdb2..5065e6b91c09 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -183,10 +183,20 @@ def _run_interactive_bash(bash_path: str, tmp: Path) -> tuple[int, str, str]: commands = "\n".join( [ f'source "{SHELL_DIR / "cmux-bash-integration.bash"}"', + "sleep 5 & cmux_user_bg_pid=$!", "echo OK", "gh pr view 123", "sleep 0.2", "echo AFTER", + "cmux_current_bang=$!", + 'echo "CMUX_TEST_USER_BG_PID=$cmux_user_bg_pid"', + 'echo "CMUX_TEST_CURRENT_BANG=$cmux_current_bang"', + ( + 'if [[ "$cmux_current_bang" != "$cmux_user_bg_pid" ]]; then ' + 'echo "CMUX_TEST_BANG_CHANGED"; fi' + ), + 'kill "$cmux_user_bg_pid" 2>/dev/null || true', + 'wait "$cmux_user_bg_pid" 2>/dev/null || true', "sleep 0.2", "exit 0", "", @@ -247,6 +257,17 @@ def main() -> int: print(output) return 1 + user_bg_matches = re.findall(r"CMUX_TEST_USER_BG_PID=([0-9]+)", output) + current_bang_matches = re.findall(r"CMUX_TEST_CURRENT_BANG=([0-9]+)", output) + if not user_bg_matches or not current_bang_matches: + print("FAIL: interactive bash did not report $! preservation markers") + print(output) + return 1 + if user_bg_matches[-1] != current_bang_matches[-1] or "CMUX_TEST_BANG_CHANGED" in output: + print("FAIL: cmux bash prompt helpers changed the user's last background PID") + print(output) + return 1 + expected_pr_action = ( 'report_pr_action view --tab=tab-bash-done-noise ' '--panel=panel-bash-done-noise --target="123"' From 58d5222d247625ec4859f174eff54d2f3206cc52 Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sun, 14 Jun 2026 13:23:17 -0700 Subject: [PATCH 10/11] fix: preserve bash user background pid --- Resources/shell-integration/cmux-bash-integration.bash | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Resources/shell-integration/cmux-bash-integration.bash b/Resources/shell-integration/cmux-bash-integration.bash index ec2db7a9327e..111fd8ad35ad 100644 --- a/Resources/shell-integration/cmux-bash-integration.bash +++ b/Resources/shell-integration/cmux-bash-integration.bash @@ -42,9 +42,7 @@ _cmux_run_bg() { return 0 fi - "$@" >/dev/null 2>&1 & - local bg_pid=$! - disown "$bg_pid" 2>/dev/null || disown 2>/dev/null || true + ( "$@" >/dev/null 2>&1 & disown "$!" 2>/dev/null || true ) } _cmux_detach_bg() { From 3fc363d8a46890044bc898d0db428a2da4a86dcd Mon Sep 17 00:00:00 2001 From: austinpower1258 Date: Sun, 14 Jun 2026 14:27:40 -0700 Subject: [PATCH 11/11] test: avoid matching echoed bash marker text --- tests/test_shell_bash_background_helpers_disowned.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_shell_bash_background_helpers_disowned.py b/tests/test_shell_bash_background_helpers_disowned.py index 5065e6b91c09..391b52f4b9a6 100644 --- a/tests/test_shell_bash_background_helpers_disowned.py +++ b/tests/test_shell_bash_background_helpers_disowned.py @@ -257,13 +257,14 @@ def main() -> int: print(output) return 1 + cleaned_lines = [_clean_line(raw) for raw in output.splitlines()] user_bg_matches = re.findall(r"CMUX_TEST_USER_BG_PID=([0-9]+)", output) current_bang_matches = re.findall(r"CMUX_TEST_CURRENT_BANG=([0-9]+)", output) if not user_bg_matches or not current_bang_matches: print("FAIL: interactive bash did not report $! preservation markers") print(output) return 1 - if user_bg_matches[-1] != current_bang_matches[-1] or "CMUX_TEST_BANG_CHANGED" in output: + if user_bg_matches[-1] != current_bang_matches[-1] or "CMUX_TEST_BANG_CHANGED" in cleaned_lines: print("FAIL: cmux bash prompt helpers changed the user's last background PID") print(output) return 1 @@ -279,7 +280,7 @@ def main() -> int: done_lines = [ line - for line in (_clean_line(raw) for raw in output.splitlines()) + for line in cleaned_lines if DONE_LINE_RE.match(line) ] if done_lines: