ADR 0009: Cold-path sequence disassembler and python -m libvt CLI¶
Status¶
Accepted. 2026-11-21.
Context¶
When debugging a failing tmux, TUI, or shell test, raw byte dumps such as
b"\x1b[?2026h\x1b[38:2::40:120:200m\x1b]7501;state=working\x1b\\" are tedious
to decode by hand. Outsider architecture reviews highlighted the value of a
declarative sequence explainer (libvt.explain(data) and python -m libvt explain), while performance profiling showed that routing Terminal.feed()’s
hot loop through boxed Token -> Registry -> Command objects degrades
throughput by 4-8x.
Decision¶
Keep hot-path emulation and cold-path sequence disassembly cleanly separated:
Cold-path
libvt.codec(explainandformat_explain):explain(data: Buffer | str) -> tuple[ExplainedSpan, ...]drivesParserwith a cold-path_ExplainSinkand maps each action (Print,Execute,EscDispatch,CsiDispatch,OscDispatch,DcsDispatch,ApcDispatch) through compact lookup tables to produce frozenExplainedSpan(kind, mnemonic, summary, raw)records (e.g.CSI,"DECSET","?2026 (SYNCHRONIZED_OUTPUT) = set").format_explain(data: Buffer | str) -> strformats one human-readable line per span for terminal debugging and pytest failure reports.
Zero-dependency CLI (
src/libvt/__main__.py):python -m libvt explainreads stdin (or a file path) and printsformat_explainoutput.python -m libvt render [--rows 24] [--cols 80]renders final plain-text screen state.python -m libvt stripstrips VT escape sequences from stdin/file.
Hot path untouched:
Terminal.feed()never importslibvt.codecand never instantiatesExplainedSpanobjects.
Alternatives considered¶
Unified hot/cold
@csiCommandregistry: Rejected because allocating aCommanddataclass perSGRorCUPsequence penalizes everyTerminal.feedcall for metadata only humans read during debugging.Registering a global
vtconsole-script binary in[project.scripts]: Omitted to avoid colliding with externalvtbinaries on userPATH;python -m libvtis zero-collision and always matches the active virtualenv.
Consequences¶
Positive¶
Developers and pytest failure hooks (
FlightRecorder) disassemble arbitrary VT traces withlibvt.explain()orpython -m libvt explainwith zero impact onTerminal.feed()speed.
Tradeoffs¶
Sequence mnemonics in
libvt.codecare maintained alongsideTerminal’s dispatch handlers, tested for parity intests/test_codec.py.
Risks¶
Raw untrusted bytes inside
format_explainemitting live escape sequences to the developer’s own terminal. Mitigation:format_explainescapes all C0, C1,ESC, and BiDi override characters in bothrawandsummarycolumns.