Windows: ESC-stripped escape tails ([A, [B, [I, [O) leak into the composer on wheel scroll and focus changes
Summary
On Windows, escape sequences that are delivered with their leading ESC separated from the tail are inserted into the composer as literal text. In practice, wheel scrolling and focus changes fill the prompt with [A[A[B[B... and [I[O fragments.
Measured on this host: the terminal delivers complete, ESC-intact packets on every input path (keyboard, focus, wheel, and under burst load). The corruption therefore happens in jcode's input path, not in the terminal.
Environment
- jcode v0.88.0 (ee4cd3db3), Windows build
- OS: Windows 11 Pro, version 10.0.26200, build 26200, x64
- Terminal: Orca ADE 1.4.212 embedded terminal (Electron/ConPTY host, xterm.js front end, WebGL renderer);
TERM=xterm-256color,TERM_PROGRAM=Orca - Shell in the pane: Git Bash (MINGW64)
- Observed during normal use in a workspace pane; the minimal repro below was confirmed in a floating-workspace pane
Steps to reproduce (minimal, terminal independent)
- Start jcode so its composer is empty.
- Write
ESC(0x1B) to the PTY on its own. - In a separate write, send
[B. - Read the composer.
Observed: the composer shows 1> [B.
Control: sending ESC [ A (3 bytes in a single write) inserts nothing, and the arrow is handled normally.
So the defect only needs the ESC and its tail to arrive as separate reads. It does not require any particular terminal.
Steps to reproduce (real world)
- Run jcode in an Orca ADE terminal pane on Windows.
- Scroll the mouse wheel over the pane while the composer is empty.
- Observe the composer accumulate
[A/[B(arrow tails) and[I/[O(focus in/out tails).
Actual composer content after scrolling in one session:
[... [A[A[B[B[B[B[A[A[A[A[B[B[B[B[A[[A[OI[I[O[I[B[B[A[B[B[A[B[A[A[B[B[B[B[A[A[A[B[B[B[A[A[A[B[B[A[A[B[B[A[A[B[B[B[A[A[A[B[B[B[A[A[A[B[B[A[B[B[A[A[B[B[BIf that line is submitted, the agent receives the garbage.
Expected behavior
Escape tails are never inserted as user text. A lone ESC should either be held until the rest of the sequence arrives, or be discarded; the bytes that follow must not be treated as typed characters.
Terminal-side measurements (rules out the terminal)
Method: a small PowerShell probe running in an Orca pane enabled ENABLE_VIRTUAL_TERMINAL_INPUT on the console input handle (clearing processed/line/echo input) and logged every byte received as hex. The app requested the same modes jcode requests (?1000h ?1006h, plus ?1004h focus reporting).
| Input path | Bytes received | ESC present | Split |
|---|---|---|---|
| Keyboard ArrowUp / ArrowDown | 1B 5B 41 / 1B 5B 42 |
yes | no, single 3-byte write |
| Focus in / focus out | 1B 5B 49 / 1B 5B 4F |
yes | no |
| Wheel, SGR mouse tracking on | 1B 5B 3C 36 34 3B <col> 3B <row> 4D, 36 35 for wheel down |
yes | no |
| 40-notch wheel burst | batches of 11 / 40 / 44 / 48 / 50 / 55 bytes | yes | no, every batch ended on a full sequence boundary |
| Wheel, mouse tracking off | nothing sent (the terminal moves its own scrollback) | n/a | n/a |
This is why I am filing against jcode rather than the terminal: on this host the input path is clean, including under burst load.
Root-cause hypothesis
Matches root cause 3 of #540: jcode mixes crossterm's asynchronous EventStream with synchronous poll/read calls, which can split terminal packets, and a partially consumed escape can be lost across cancellation in select, leaving the tail to be read as characters.
Likely amplified on Windows: crates/jcode-tui/src/tui/app/local.rs and remote.rs call crossterm::event::read(), and crossterm's Windows input reads Win32 key records rather than decoding escape input (as analysed in #1064). On Windows there is no escape-sequence parser to reassemble the split packet.
Also relevant from source: crates/jcode-tui/src/tui/mod.rs reapply_terminal_modes_to re-asserts ?1000h ?1002h ?1003h ?1015h ?1006h (plus keyboard enhancement) on FocusGained, so focus churn keeps re-arming modes and produces more focus events, which are exactly the [I / [O tails seen above.
Suggested fix
- Keep a lone
ESCand its tail in the same buffer across cancellation (the single bounded reader in #540's commit8c530734may already cover this; see question below). - Defensive rule in the input path: never insert bytes that form a CSI/SS3 tail (
^\[[A-Z0-9;<]*$) as user text, even when a partial escape was lost. - On Windows, read raw VT input bytes (enable
ENABLE_VIRTUAL_TERMINAL_INPUT, read the console input handle) instead of Win32 key records, so a packet cannot be reduced to characters. - Regression test: feed
ESC, then[Bas separate writes, and assert the composer stays empty.
Question: does 8c530734 in #540 cover the Windows path, or is the Windows reader still on the crossterm key-record path in that branch?
Workaround
- Clear the composer with Ctrl+U before typing.
- Scroll with Ctrl+Shift+K / Ctrl+Shift+J (scroll up/down) or Alt+U / Alt+D (page up/down) instead of the wheel.
- Related: #956 covers the Windows wheel-goes-to-terminal-scrollback half of the behavior; #1064 covers Windows crossterm decode limits.
Source: 1jehuang/jcode