xterm.js icon indicating copy to clipboard operation
xterm.js copied to clipboard

Parser swallows the first printable character after a BEL-terminated APC sequence

Open xichan96 opened this issue 1 month ago • 4 comments

Details

  • xterm.js version: 5.5.0 (verified with @xterm/headless; browser build shows the same behavior)
  • Node version: v23.7.0
  • OS: macOS 15 (parser-level issue, platform-independent)

Summary

When an APC sequence (ESC _ ... ) is terminated by BEL (0x07) instead of ST (ESC \), the parser swallows the next printable character written to the terminal. If the next data is an escape sequence (e.g. ESC[), everything works fine.

Per VT520/xterm's original semantics, both BEL and ST terminate string-type sequences (OSC, DCS, APC, PM, SOS), and xterm.js already accepts BEL as a terminator for OSC. APC terminated by ST behaves correctly, so the BEL path looks like an inconsistent terminator handling rather than an intentional difference.

Reproduction

Minimal repro with @xterm/headless:

const { Terminal } = require('@xterm/headless')

function feed(term, data) {
  return new Promise((resolve) => term.write(data, () => setTimeout(resolve, 10)))
}

async function main() {
  // Case 1: printable char right after a BEL-terminated APC — swallowed
  const t1 = new Terminal({ cols: 20, rows: 5, allowProposedApi: true })
  await feed(t1, 'X\x1b_pi:c\x07Y')
  console.log(t1.buffer.active.getLine(0).translateToString(true)) // -> "X"  (Y lost)

  // Case 2: same APC terminated by ST — works
  const t2 = new Terminal({ cols: 20, rows: 5, allowProposedApi: true })
  await feed(t2, 'X\x1b_pi:c\x1b\\Y')
  console.log(t2.buffer.active.getLine(0).translateToString(true)) // -> "XY"

  // Case 3: ESC sequence right after a BEL-terminated APC — works
  const t3 = new Terminal({ cols: 20, rows: 5, allowProposedApi: true })
  await feed(t3, 'X\x1b_pi:c\x07\x1b[7mY\x1b[27m')
  console.log(t3.buffer.active.getLine(0).translateToString(true)) // -> "XY"
}

main()
  • Case 1 writes X, then ESC _ pi:c BEL, then Y. Only X reaches the buffer — Y is silently dropped.
  • Case 2 (ST-terminated) and Case 3 (followed by an escape sequence) both print XY as expected.

Analysis

My guess from reading the parser: after the BEL terminator the state machine transitions to the same "string end" state used for OSC, where subsequent printable characters are treated as part of the (ignored) payload and discarded, and the parser only returns to ground on the next ESC. With an ST terminator the parser returns to ground immediately, so case 2 is unaffected.

Why this matters

Any terminal-embedded client that consumes an app's APC markers and re-emits the stream (or any passthrough that normalizes ESC \ to BEL) can silently drop user-visible text. I hit this in the wild while debugging an IME anchoring issue: a TUI (the pi coding agent) emits an APC marker terminated by BEL followed by an SGR — which is why the swallowed-character case never showed up there, but a plain-text payload right after the marker would have been dropped.

Happy to turn the repro into a parser unit test if that helps.

xichan96 avatar Oct 20 '26 11:10 xichan96

Imho your assumption is not correct - if I remember right BEL termination was an xterm invention for OSC only. It never applied to other string payload sequence types. Furthermore DEC devices never implemented APC on their own, but gave the general parsing rule for them in 3.5.4.2 (thanks @j4james for putting this online).

We had several changes in APC handling throughout v6 and current master, please also check against newer versions.

jerch avatar Oct 20 '26 11:10 jerch

Any terminal-embedded client that consumes an app's APC markers and re-emits the stream (or any passthrough that normalizes ESC \ to BEL)

Yeah, this is insane! BEL is absolutely not a valid string terminator! I believe Xterm added it as a hack back in the 1990s when it was difficult to produce a backslash in the shell (or something like that). And because of that legacy, most modern terminals feel obliged to support it. But that hack was never used by Xterm for anything other than OSC sequences. Any app that is producing APC sequences with a BEL terminator is going to break in a lot of terminals (Xterm, VTE, Konsole, MLTerm, Windows Terminal...) That's a bug that needs to be fixed in the app.

As a rule of thumb, when you see an LLM starting a comment with "Per the spec..." or "Per the standard...", it's just making shit up.

j4james avatar Oct 20 '26 14:10 j4james

FYI, it looks like this bug was already reported to the pi repository last month: https://github.com/earendil-works/pi/issues/9332

They've got a pending PR that fixes the issue by stripping those sequences from the output - apparently they're only meant for internal use anyway (at least that's what the PR comments say).

j4james avatar Oct 21 '26 21:10 j4james

@j4james Interesting find. And it shows imho a fundamental issue with current AI coding - The fixing attempts never touch the initial markers but evolve patches around the slightly off initial marker definition. It seems the AIs do not ascend the model to question it on a higher level, but stay on the already found "happy path" and just create more patches. And ofc none of the developers brings the broader mental model to the table. Because AI coding "don't make the programmer think" is a thing.

apparently they're only meant for internal use anyway (at least that's what the PR comments say).

Yupp, but then the question is - why use a sequence style at all? If they only need a proper sentinel token to be used in their TUI screen diff updates, then a loud token barking at you would help debugging it. By using a slightly wrong sequence the error gets subtle and slips through. And if the intention was to allow passing it through, then they have to use a proper sequence format. There is no middle ground here.

jerch avatar Oct 22 '26 11:10 jerch