Skip to content

fix: persist VT stream parser state across writes#63

Merged
sreya merged 3 commits into
mainfrom
debug-mac-demo-issue
Nov 25, 2025
Merged

fix: persist VT stream parser state across writes#63
sreya merged 3 commits into
mainfrom
debug-mac-demo-issue

Conversation

@sreya
Copy link
Copy Markdown
Contributor

@sreya sreya commented Nov 25, 2025

  • Also updates the demo to use 'ws' instead of rolling our own implementation

This fixes tab completion corruption on macOS (and likely other platforms)
when escape sequences are split across WebSocket message boundaries.

Root cause: Each call to write() was creating a new VT parser stream,
which lost parser state between calls. When zsh sends cursor positioning
sequences like ESC[9A (cursor up 9 lines), they could be split as:
- Message 1: '...\x1b[9'
- Message 2: 'A...'

The parser would see '\x1b[9' as incomplete, then 'A' as a literal character.

Fix: Store the VT stream in TerminalWrapper and reuse it across writes,
so incomplete escape sequences are properly buffered and parsed when
the remaining bytes arrive.

Changes to WASM patch:
- Add 'stream: ReadonlyStream' field to TerminalWrapper
- Initialize stream once in newWithConfig() after terminal creation
- Reuse wrapper.stream in write() instead of creating new stream
- Call stream.deinit() in free()
Removed ~115 lines of hand-rolled WebSocket frame parsing code in favor
of the battle-tested ws package. This simplifies the codebase and ensures
proper WebSocket protocol handling.

Changes:
- Add ws@8.18.0 dependency
- Replace manual WebSocket handshake and frame parsing with WebSocketServer
- Simplify PTY<->WebSocket data flow using ws.send() instead of sendWebSocketFrame()
- Remove crypto import (was only used for WebSocket handshake)
The patch file had incorrect metadata (604 lines vs actual 611 lines)
which could cause git apply to fail or truncate the file.

Regenerated the patch cleanly from the modified ghostty submodule.
@sreya sreya merged commit 7b2dd99 into main Nov 25, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant