Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:

permissions:
contents: read
actions: write

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -447,6 +446,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
Expand All @@ -469,6 +493,7 @@ jobs:
python3 tests/test_stress_cli_socket_api.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
python3 tests/test_shell_git_branch_stale_cwd.py
python3 tests/test_shell_git_config_remote_url_parsing.py
Expand Down
39 changes: 28 additions & 11 deletions Resources/shell-integration/cmux-bash-integration.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,28 @@ _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_detach_bg() {
( "$@" >/dev/null 2>&1 & ) >/dev/null 2>&1
_cmux_run_bg "$@"
}

_cmux_send_bg() {
local payload="$1"
if [[ "${_CMUX_IN_PREEXEC:-}" == "1" ]]; then
{
_cmux_send "$payload"
} >/dev/null 2>&1 &
disown
return 0
fi
_cmux_detach_bg _cmux_send "$payload"
_cmux_run_bg _cmux_send "$payload"
}

_cmux_start_tracked_bg() {
Expand Down Expand Up @@ -1357,6 +1365,15 @@ _cmux_bash_preexec_hook_subshell() {
_cmux_bash_preexec_hook "$@"
}

_cmux_bash_preexec_inline_ps0() {
local cmd="${1:-${BASH_COMMAND:-}}"
_CMUX_BASH_PS0_INLINE_ACTIVE=1
{
_cmux_bash_preexec_hook "$cmd"
_CMUX_BASH_PS0_INLINE_ACTIVE=0
} || _CMUX_BASH_PS0_INLINE_ACTIVE=0
}
Comment thread
austinywang marked this conversation as resolved.
Resolved

_cmux_prompt_command() {
local last_status=$?
_cmux_tmux_sync_cmux_environment
Expand Down Expand Up @@ -1529,9 +1546,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; }'
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
Expand Down
276 changes: 276 additions & 0 deletions tests/test_shell_bash_background_helpers_disowned.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
#!/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 signal
import socket
import stat
import subprocess
import tempfile
import time
from contextlib import suppress
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 _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 _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, 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\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(
{
"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",
"CMUX_TEST_SEND_LOG": str(send_log_path),
"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"}"',
"_cmux_bash_history_command() { return 1; }",
"echo OK",
"gh pr view 123",
"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)
_drain_pty(fd, output)
break
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved
else:
_terminate_child(pid)
_drain_pty(fd, output, quiet_after=0.05)
return 124, 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:
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
Comment thread
lawrencecchen marked this conversation as resolved.
Resolved

with tempfile.TemporaryDirectory(prefix="cmux-bash-done-noise-") as 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")
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

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())
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())
Loading