feat(@wterm/dom): add PredictiveEcho helper for mosh-style local echo#80
Open
vmxmy wants to merge 1 commit into
Open
feat(@wterm/dom): add PredictiveEcho helper for mosh-style local echo#80vmxmy wants to merge 1 commit into
vmxmy wants to merge 1 commit into
Conversation
PredictiveEcho is an opt-in utility for terminals connected to a remote PTY where the round-trip latency is large enough to feel sluggish (cloudflared tunnels, SSH-in-browser, cross-region WebSocket). - Predicts printable ASCII into the local terminal at typing latency - Reconciles with server output via a FIFO byte-match queue; matching bytes are silently consumed so there are no duplicate glyphs - Any unmatched byte (escape sequence, prompt redraw) rolls back the prediction queue with \x1b[ND\x1b[NX and lets the server stream take over — the ghostty / wterm core remains the source of truth - Disabled in alt-screen mode (vim, less, htop, claude, ...) so full-screen apps render exactly as the server sent them, with a custom shouldPredict() escape hatch No public API changes; existing consumers are unaffected. Tested against an examples/local backend deployed behind Cloudflare Tunnel + Cloudflare Access; round-trip ~80-150ms reduced to perceived typing latency for ASCII characters in zsh.
|
@vmxmy is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
Add
PredictiveEchoto@wterm/domas an opt-in utility that gives aterminal connected to a remote PTY the feeling of a local one — mosh
"client-side prediction" applied to the wterm/ghostty render pipeline.
No existing public API is changed. Consumers that don't import
PredictiveEchoare unaffected.Why
When wterm is used to drive a remote PTY (cloudflared tunnel, SSH in
browser, cross-region WebSocket), the round trip between keystroke and
echo is visible — typing feels sluggish even on a fast network. Mosh
solves this with client-side prediction over its own SSP protocol.
We can't replicate mosh's protocol (we run raw PTY bytes over WebSocket
by design), but we can approximate the client-side half:
from the queue (no duplicate glyphs).
mid-prompt redraws), roll back the queue with
\x1b[ND\x1b[NXand letthe server stream take over — the ghostty / wterm core remains the
source of truth.
code, ...) so full-screen applications render exactly as the server
sent them.
In practice this turns a perceived ~100ms typing latency over a
Cloudflare Tunnel into something that feels local for typing-only flows,
without breaking anything app-level. Worst-case behaviour on a
Powerlevel10k prompt is a brief flicker as the server redraws the prompt
on top of the prediction (the rollback path) — acceptable trade-off and
in line with what mosh does on the same prompt.
Public API
PredictiveEchoOptions.shouldPredict?: (data, term) => booleanis theescape hatch — return
truefor whatever you want to predict (default:single printable ASCII byte, off in alt-screen).
Implementation notes
packages/@wterm/dom/src/predictive-echo.ts, ~115 LOC.packages/@wterm/dom/src/index.ts.WebSocketTransportsection.term.write(),term.bridge?.usingAltScreen(), and the caller'ssendcallback.Field tested
Deployed against the
examples/localbackend over a Cloudflare Tunnel(QUIC) + Cloudflare Access, with ghostty as the core. Used daily for
Claude Code / zsh + Powerlevel10k / tmux / vim — typing feels native,
rollback flicker on prompt redraws is rare and brief.
Happy to add tests in
__tests__/predictive-echo.test.tsif you wantthis merged — let me know what kind of coverage you'd prefer (unit
against a fake
WTerm, or behavior-level via a realWasmBridge).