From 70f7312bcda31a1721c047aadea23a66ae0994c2 Mon Sep 17 00:00:00 2001 From: Cynthia Toporowski Date: Sun, 22 Nov 2026 20:47:41 -0700 Subject: [PATCH] fix(droid-control): close recorded tctl sessions on macOS Stock macOS date emits a literal 3N for GNU date's millisecond format. Use a shared millisecond clock backed by system Perl on macOS and keep the existing clock elsewhere. Exercise the real close command with a live recorder on stock macOS, covering both termination and escalation. Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> --- .github/workflows/droid-control-tests.yml | 7 +++ plugins/droid-control/bin/tctl | 37 +++++++++------ plugins/droid-control/tests/test_tctl.py | 58 ++++++++++++++++++++++- 3 files changed, 86 insertions(+), 16 deletions(-) diff --git a/.github/workflows/droid-control-tests.yml b/.github/workflows/droid-control-tests.yml index c9b6838..90b357f 100644 --- a/.github/workflows/droid-control-tests.yml +++ b/.github/workflows/droid-control-tests.yml @@ -22,6 +22,13 @@ env: CARGO_TERM_COLOR: always jobs: + tctl-macos: + name: tctl close with stock macOS tools + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - run: python3 plugins/droid-control/tests/test_tctl.py -v TctlCloseTest + showcase: name: Showcase composition, render helper, real renders runs-on: ubuntu-latest diff --git a/plugins/droid-control/bin/tctl b/plugins/droid-control/bin/tctl index 9c9e020..cf84d14 100755 --- a/plugins/droid-control/bin/tctl +++ b/plugins/droid-control/bin/tctl @@ -54,6 +54,15 @@ require_cmd() { command -v "$1" >/dev/null 2>&1 || die "required command not found: $1" } +now_ms() { + if [[ "$OSTYPE" == darwin* ]]; then + # macOS date does not support %N. Use the system Perl for millisecond precision. + /usr/bin/perl -MTime::HiRes=time -e 'printf "%.0f\n", int(time() * 1000)' + else + date +%s%3N + fi +} + runtime_dir_value() { if [[ -n "${XDG_RUNTIME_DIR:-}" ]]; then printf '%s\n' "$XDG_RUNTIME_DIR" @@ -74,9 +83,9 @@ wait_for_wayland_socket() { local pid="$1" local socket_path="$2" local timeout_ms="$3" - local deadline=$(( $(date +%s%3N) + timeout_ms )) + local deadline=$(( $(now_ms) + timeout_ms )) - while (( $(date +%s%3N) <= deadline )); do + while (( $(now_ms) <= deadline )); do [[ -S "$socket_path" ]] && return 0 kill -0 "$pid" >/dev/null 2>&1 || return 1 sleep 0.05 @@ -124,9 +133,9 @@ terminate_true_input_stack() { kill -TERM "$pid" >/dev/null 2>&1 || true done - local deadline=$(( $(date +%s%3N) + grace_ms )) + local deadline=$(( $(now_ms) + grace_ms )) local alive=1 - while (( $(date +%s%3N) <= deadline )); do + while (( $(now_ms) <= deadline )); do alive=0 kill -0 -- "-$root" >/dev/null 2>&1 && alive=1 if (( ! alive )); then @@ -155,8 +164,8 @@ terminate_recorder_pid() { local grace_ms="${2:-3000}" [[ -n "$pid" ]] || return 0 kill -INT "$pid" >/dev/null 2>&1 || true - local deadline=$(( $(date +%s%3N) + grace_ms )) - while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do + local deadline=$(( $(now_ms) + grace_ms )) + while kill -0 "$pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do sleep 0.05 done if kill -0 "$pid" >/dev/null 2>&1; then @@ -538,8 +547,8 @@ cleanup_tuistory_asciinema() { if [[ -n "$pid" ]] && kill -0 "$pid" >/dev/null 2>&1; then if pid_is_expected_asciinema_recording "$pid" "$record_path"; then kill -TERM "$pid" >/dev/null 2>&1 || true - local deadline=$(( $(date +%s%3N) + 2000 )) - while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do + local deadline=$(( $(now_ms) + 2000 )) + while kill -0 "$pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do sleep 0.05 done if kill -0 "$pid" >/dev/null 2>&1; then @@ -562,8 +571,8 @@ cleanup_tuistory_asciinema() { [[ "$orphan_args" == *"$record_path"* ]] || continue kill -TERM "$orphan_pid" >/dev/null 2>&1 || true - local deadline=$(( $(date +%s%3N) + 2000 )) - while kill -0 "$orphan_pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do + local deadline=$(( $(now_ms) + 2000 )) + while kill -0 "$orphan_pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do sleep 0.05 done if kill -0 "$orphan_pid" >/dev/null 2>&1; then @@ -708,8 +717,8 @@ wait_for_pattern_in_log() { local log_file="$1" local pattern="$2" local timeout_ms="$3" - local deadline=$(( $(date +%s%3N) + timeout_ms )) - while (( $(date +%s%3N) <= deadline )); do + local deadline=$(( $(now_ms) + timeout_ms )) + while (( $(now_ms) <= deadline )); do local snapshot snapshot="$(strip_log "$log_file")" if SNAPSHOT_TEXT="$snapshot" python3 - "$pattern" <<'PY' @@ -741,10 +750,10 @@ PY wait_for_idle_in_log() { local log_file="$1" local timeout_ms="$2" - local deadline=$(( $(date +%s%3N) + timeout_ms )) + local deadline=$(( $(now_ms) + timeout_ms )) local stable_for=0 local last_size=-1 - while (( $(date +%s%3N) <= deadline )); do + while (( $(now_ms) <= deadline )); do local size=0 [[ -f "$log_file" ]] && size=$(stat -c %s "$log_file") if [[ "$size" == "$last_size" ]]; then diff --git a/plugins/droid-control/tests/test_tctl.py b/plugins/droid-control/tests/test_tctl.py index d50aa83..1255405 100644 --- a/plugins/droid-control/tests/test_tctl.py +++ b/plugins/droid-control/tests/test_tctl.py @@ -1,6 +1,6 @@ -"""tctl's native input boundary. +"""tctl's native input and session cleanup boundaries. -The default suite runs the real tctl against a recording wtype executable. +The default suite uses lightweight stand-ins for wtype, tuistory and asciinema. TCTL_NATIVE_E2E=1 also checks incoming bytes in real headless Ghostty, without a CLI, login fixture, clipboard, or extra script(1) wrapper. TCTL_SH selects an alternate tctl for negative controls. @@ -8,8 +8,10 @@ import json import os +import select import shlex import shutil +import signal import subprocess import sys import tempfile @@ -52,6 +54,58 @@ """ +class TctlCloseTest(unittest.TestCase): + def assert_recording_close(self, term_handler, expected_signal): + with tempfile.TemporaryDirectory(prefix="tctl-close-test-") as tmp: + root = Path(tmp) + session = f"close-test-{uuid.uuid4().hex}" + session_dir = Path("/tmp/tctl-sessions") / session + session_dir.mkdir(parents=True) + self.addCleanup(shutil.rmtree, session_dir, ignore_errors=True) + tuistory = root / "tuistory" + tuistory.write_text(f'#!/bin/sh\n[ "$*" = "-s {session} close" ]\n') + tuistory.chmod(0o755) + # No GNU coreutils or newer Bash ahead of the stock macOS tools. + env = {"PATH": f"{root}:/usr/bin:/bin"} + cast = root / "recording.cast" + recording = b'{"version": 2, "width": 80, "height": 24}\n' + cast.write_bytes(recording) + (session_dir / "meta").write_text( + f"BACKEND=tuistory\nRECORD_PATH={shlex.quote(str(cast))}\n" + ) + probe = root / "asciinema" + probe.write_text( + "import signal\n" + f"signal.signal(signal.SIGTERM, signal.{term_handler})\n" + "print('ready', flush=True)\n" + "signal.pause()\n" + ) + with subprocess.Popen( + [sys.executable, str(probe), "rec", str(cast)], stdout=subprocess.PIPE, + ) as recorder: + try: + self.assertTrue(select.select([recorder.stdout], [], [], 5)[0]) + self.assertEqual(recorder.stdout.readline(), b"ready\n") + (session_dir / "asciinema.pid").write_text(f"{recorder.pid}\n") + result = subprocess.run( + ["/bin/bash", str(TCTL), "-s", session, "close"], + env=env, capture_output=True, text=True, timeout=10, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertEqual(recorder.wait(timeout=5), -expected_signal) + self.assertFalse(session_dir.exists()) + self.assertEqual(cast.read_bytes(), recording) + finally: + if recorder.poll() is None: + recorder.kill() + + def test_close_cleans_up_active_recorder_and_keeps_cast(self): + self.assert_recording_close("SIG_DFL", signal.SIGTERM) + + def test_close_escalates_for_recorder_that_ignores_term(self): + self.assert_recording_close("SIG_IGN", signal.SIGKILL) + + class TctlInputTest(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory(prefix="tctl-input-test-")