ADR 0010: Transport-only asyncio/PTY adapters and diagnostic pytest plugin¶
Status¶
Accepted. 2026-11-21.
Context¶
libvt ships an asyncio / POSIX PTY transport layer (libvt.aio), visual
assertion helpers (libvt.testing), and a pytest11 plugin
(libvt.pytest_plugin). Four concrete failure modes emerged during review and
empirical testing:
Import pollution: Importing
asyncio,pty, orpytestinsidelibvt/__init__.pyslows everyimport libtmuxcall.Fixture namespace collisions: Auto-discovered
pytest11plugins that register unprefixed fixture names (terminal,spawn,shell) collide with fixtures inpytest-shell-utilities,pexpect, and downstream test suites, whereas splitting a separatepytest-vtPyPI package creates cross-package version skew relative tolibtmux.pytest_plugin.Wide-glyph box misalignment and terminal-escape injection in failure diffs: Padding
format_screen_boxrows withstr.ljust(w)counts Python codepoints instead of terminal display columns, pushing right│borders out of alignment on every 2-column CJK/emoji character ("日本"), while unescaped C0/C1/BiDi characters in failure messages can corrupt the test runner’s terminal.POSIX vs Windows PTY scope: Both
libtmuxandtmuxpdeclareOperating System :: POSIXandOperating System :: MacOS :: MacOS Xclassifiers only (tmuxitself is POSIX-only). Adding apywinptyC/Rust ConPTY dependency for Windows subprocess PTYs would break ADR 0001’s purepy3-none-any.whlzero-dependency guarantee.
Decision¶
Strict import isolation (
libvt/__init__.py):import libvtnever loadsasyncio,pty,termios, orpytest(enforced by a clean-interpreter subprocess test intests/test_libvt.py).
AsyncTerminalStreamand POSIXopen_pty()(libvt.aio):AsyncTerminalStreamdrivesTerminalfrom(StreamReader, StreamWriter)pairs, flushingdrain_replies()automatically and enforcingsync_timeoutviaTerminal.expire_synchronized_output(sync_gen)in both background-task (async with) and on-demandwait_for()modes.open_pty(argv, *, rows=24, cols=80, env=None, cwd=None, tty_mode="cooked", python_executable=None)spawns a child process attached to a stdlibpty.openpty()master/slave pair via an isolated-I -Schild trampoline (start_new_session=True, strictTIOCSCTTYcontrolling terminal acquisition,SIGPIPE/SIGXFSZreset toSIG_DFL, and a pre-exec marker + bounded stage/errno packet on aCLOEXECerr_fdpipe withoutpreexec_fn), configuresTIOCSWINSZand optional"cbreak"/"raw"termios modes, augments unsetPolicy.osc7_hostswith the local hostname, and reaps owned process groups onaclose()viaos.killpg(SIGHUP->SIGTERM->SIGKILL) after draining final PTY bytes. On Windows (sys.platform == "win32"),open_pty()raisesNotImplementedErrorcleanly while the entire Sans-I/O core works unchanged.
Strictly
vt_-prefixed,Protocol-typedpytest11plugin (libvt.pytest_plugin):Register
vt_terminal,vt_factory(typed viaTerminalFactoryt.Protocolso static type checkers verify keyword arguments),vt_parser,vt_recorder(FlightRecorder), andvt_pty(callingpytest.skip("POSIX pty required")onwin32), with lazy imports inside fixture bodies.
Literal
assert_screen, column-exactassert_screen_box, andFlightRecorder(libvt.testing):assert_screencompares literal screen text without stripping┌──┐borders rendered by applications, whileformat_screen_boxandassert_screen_boxmeasure cells by terminal display width (WidthRole/display_width), sanitize C0/C1 and BiDi overrides, validate outer│Wsoft-wrap markers, and appendFlightRecorderdiagnostics (cursor, active modes,OSC 7501status,Parser.state, and disassembled recent raw bytes viaformat_explain) when aFlightRecorderis attached toTerminal.Snapshot.to_dict()returns a deterministic JSON-compatible dictionary for snapshot testing (inline-snapshot/syrupy).
Alternatives considered¶
Unprefixed
terminal,spawn,shellfixtures: Rejected due to globalpytest11namespace collisions.Separate
pytest-vtPyPI package: Rejected to matchlibtmux.pytest_pluginsingle-package ergonomics.Optional
pywinptydependency: Deferred;libtmuxandtmuxptarget POSIX/macOS exclusively, and keepingdependencies = []preserves universal pure-Python wheels.
Consequences¶
Positive¶
assert_screenaligns CJK, emoji, and combining marks to exact terminal columns and never leaks raw escape or BiDi spoofing sequences into pytest logs.Failed async or PTY tests include the exact disassembled tail of VT bytes that produced the mismatch.
Tradeoffs¶
Live subprocess
vt_ptytests run on Linux/macOS/BSD and skip on native Windows, whereas all in-memoryvt_terminal/AsyncTerminalStreamtests run on every OS.
Risks¶
Leaked PTY file descriptors or child processes if a test fails mid-wait. Mitigation:
PtySession.__aexit__closes the master fd, terminates/reaps the child process, and cancels the pump task insidetry / finally.