diff --git a/tests/agent/test_runner_injections.py b/tests/agent/test_runner_injections.py index f4a44661f..31344270c 100644 --- a/tests/agent/test_runner_injections.py +++ b/tests/agent/test_runner_injections.py @@ -1176,22 +1176,25 @@ async def test_unified_websocket_followup_admits_effective_session(tmp_path): pending = asyncio.Queue(maxsize=20) loop._pending_queues[UNIFIED_SESSION_KEY] = pending - run_task = asyncio.create_task(loop.run()) msg = InboundMessage( channel="websocket", sender_id="u", chat_id="chat", content="new request", ) - await loop.bus.publish_inbound(msg) - - queued_msg = await asyncio.wait_for(pending.get(), timeout=2) - admitted_msg = admission.admit.await_args.args[0] - assert admitted_msg.session_key == UNIFIED_SESSION_KEY - assert queued_msg.session_key == UNIFIED_SESSION_KEY - - loop.stop() - await asyncio.wait_for(run_task, timeout=2) + try: + # Exercise the real routing/journaling boundary to completion. This is + # a session-key contract, not a two-second SQLite fsync benchmark. + await loop._dispatch_inbound(msg) + queued_msg = pending.get_nowait() + admission.admit.assert_awaited_once() + admitted_msg = admission.admit.await_args.args[0] + assert admitted_msg.session_key == UNIFIED_SESSION_KEY + assert queued_msg.session_key == UNIFIED_SESSION_KEY + assert queued_msg.content == msg.content + assert queued_msg.metadata["_recovery_followup_id"] + finally: + await loop.aclose() @pytest.mark.asyncio diff --git a/tui/scripts/pty_smoke.py b/tui/scripts/pty_smoke.py index 706a04f0c..5cbdf195d 100644 --- a/tui/scripts/pty_smoke.py +++ b/tui/scripts/pty_smoke.py @@ -16,6 +16,7 @@ import select import shutil import signal import struct +import sys import termios import time from pathlib import Path @@ -59,13 +60,15 @@ def _wait_for(fd: int, needle: bytes, timeout: float) -> bytes: return bytes(output) -def _wait_for_exit(pid: int, timeout: float) -> int: +def _wait_for_exit(pid: int, fd: int, output: bytearray, timeout: float) -> int: deadline = time.monotonic() + timeout while time.monotonic() < deadline: + # Drain while waiting: a controlling terminal can hold child exit until + # the parent consumes its final output (notably on macOS). + output.extend(_read(fd, min(0.05, deadline - time.monotonic()))) child, status = os.waitpid(pid, os.WNOHANG) if child: return os.waitstatus_to_exitcode(status) - time.sleep(0.05) os.kill(pid, signal.SIGKILL) os.waitpid(pid, 0) raise AssertionError("TUI did not exit after the exit command") @@ -98,6 +101,11 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float: started = time.perf_counter() pid, master = pty.fork() if pid == 0: + if sys.platform == "darwin": + # forkpty alone can lose unread output when the last slave closes + # on macOS. Opening the controlling tty establishes S_CTTYREF so + # exit drains its output, as a real interactive shell would. + os.close(os.open("/dev/tty", os.O_RDWR)) os.chdir(cwd) os.execvpe(command[0], command, env) @@ -131,7 +139,7 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float: output.extend(_read(master, 0.2)) os.write(master, b"exit\r") output.extend(_wait_for(master, LEAVE_ALT_SCREEN, 5)) - exit_code = _wait_for_exit(pid, 5) + exit_code = _wait_for_exit(pid, master, output, 5) output.extend(_read(master, 0.5)) reaped = True finally: @@ -154,7 +162,10 @@ def smoke(executable: Path | None = None, *, cwd: Path = ROOT) -> float: if "Traceback" in text or "Task exception was never retrieved" in text: raise AssertionError("TUI emitted an exception during shutdown") if output.count(RESUME_COMMAND) != 1: - raise AssertionError("TUI did not print exactly one reusable session command") + raise AssertionError( + f"TUI printed {output.count(RESUME_COMMAND)} reusable session commands; " + f"recent output: {output[-1000:]!r}" + ) if output.index(RESUME_COMMAND) < output.index(LEAVE_ALT_SCREEN): raise AssertionError("TUI printed the session command before restoring the terminal") # The UI must inherit the host terminal background. Fixed RGB/indexed