fix(tui): whole frames, and stop repainting for a phrase nobody can see - #243
Merged
Conversation
Reported on Windows 10 / v0.4.1: "UI is shaking and some line broken". **The shaking is frame tearing.** Ink paints by writing a whole frame to stdout — cursor home, every line, the trailing clear. On a terminal that renders as bytes arrive, the interval between the first line and the last is an interval where the screen holds half of the old frame and half of the new. That is not a Windows bug; macOS terminals just coalesce hard enough to hide it, which is how a cross-platform behaviour arrived as a Windows report. DEC private mode 2026 is the fix the terminal side already implemented: `CSI ? 2026 h` holds rendering until `CSI ? 2026 l`, so a frame appears whole or not at all. Every frame is now bracketed in one write — one, not three, because three would put the stream's own chunking between a marker and the frame it is meant to bracket. Sent unconditionally on a TTY, and safe to: an unrecognised DEC private mode is ignored, which is the whole point of "private", and it is why terminals without 2026 have taken these bytes from other TUIs for years. No probe, for the same reason `alt-screen.ts` does not probe — asking costs a round trip on a stdin something else is reading, and the fallback is exactly today's behaviour. `ATOMIC_AGENT_NO_SYNC_OUTPUT=1` opts out. Installed by patching `stdout.write` rather than by handing Ink a wrapper stream: Ink reads `columns`, `rows` and `resize` off that same object, and a proxy that forwards those subtly wrong breaks resize handling in a way far harder to see than tearing. **And there was a repaint going nowhere.** `useRotatingPlaceholder` ran its interval unconditionally, but the phrase is drawn only while the composer is empty. From the first character typed, the timer went on firing every four seconds — a `setState` at the root, a full Ink frame, for a string nobody could see — for the rest of the session. It is now gated on visibility, the same way `useSpinner` and `useAtomField` already were. The cheapest frame is the one that is never painted, and on a tearing terminal it is also the one that cannot flicker. **The broken line is not this.** That is glyph width under a non-UTF-8 Windows console code page, which PR #236 (`fix(cli): switch Windows console to UTF-8 on startup`) addresses directly. Deliberately left alone here so the two do not collide. I do not have a Windows 10 machine, so the tearing fix is reasoned and unit-tested rather than observed. The exact bytes reaching the terminal are asserted; whether Windows Terminal stops shaking needs someone on Windows to run the branch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The shaking is frame tearing
Ink paints by writing a whole frame to stdout — cursor home, every line, the trailing clear. On a terminal that renders as bytes arrive, the interval between the first line and the last is an interval where the screen holds half the old frame and half the new.
This is not a Windows bug. macOS terminals coalesce hard enough to hide it, which is how a cross-platform behaviour arrived as a Windows report.
DEC private mode 2026 ("synchronized output") is the fix the terminal side already implemented:
CSI ? 2026 hholds rendering untilCSI ? 2026 l, so a frame appears whole or not at all. Windows Terminal has supported it since 1.15.Every frame is now bracketed in one write — one, not three, because three would put the stream's own chunking between a marker and the frame it is meant to bracket, which is precisely what this exists to prevent.
Why unconditional is safe. An unrecognised DEC private mode is ignored — that is what "private" buys, and it is why terminals without 2026 (including conhost) have taken these bytes from other TUIs for years. There is no probe here for the same reason
alt-screen.tshas none: asking costs a round trip on a stdin something else is reading, and the fallback is exactly today's behaviour.ATOMIC_AGENT_NO_SYNC_OUTPUT=1opts out.Why patch
stdout.writerather than wrap the stream. Ink readscolumns,rowsand theresizeevent off that same object. A proxy would have to forward all of it correctly, and getting it subtly wrong breaks resize handling in a way much harder to spot than tearing.alt-screen.tsandmouse-tracking.tsalready own the terminal this way.And a repaint that was going nowhere
useRotatingPlaceholderran its interval unconditionally — but the phrase is only drawn while the composer is empty:So from the first character typed, the timer kept firing every four seconds — a
setStateat the root of the tree, a full Ink frame, to advance a string nobody could see — for the rest of the session. On a terminal that tears, that is a visible flicker every four seconds while you type, which matches the report closely.Now gated on visibility, the same way
useSpinneranduseAtomFieldalready were. The cheapest frame is the one never painted; on a tearing terminal it is also the one that cannot flicker.The broken line is not this
That is glyph width under a non-UTF-8 Windows console code page, and #236 (
fix(cli): switch Windows console to UTF-8 on startup) addresses it directly. Deliberately left alone here so the two don't collide — they're complementary.Verification — and its limit
I do not have a Windows 10 machine. The tearing fix is reasoned and unit-tested, not observed. What the tests pin is the exact bytes reaching the terminal:
BSU + frame + ESUin a singlewriteESC[?25l) is left alone — no markers spent rendering nothingrestore()puts the originalwriteback, emits a closing ESU as insurance against a crash mid-update, is safe to call twice, and does not clobber a patch installed after oursPlus, for the placeholder: nothing is scheduled while inactive, the timer stops and restarts across the transition, and the parameter defaults to active so existing callers are unchanged.
Both failures are pre-existing on
mainin this sandbox and unrelated (fs-glob-real, and anEACCESspawning a stubllama-server).Someone on Windows needs to run this branch to confirm the shaking is gone. If it isn't, the next thing I'd look at is the 250 ms elapsed-clock tick in
thinking-indicator.tsx, which repaints during a turn.