0
Fork 0
mirror of https://github.com/zeronsh/comet.git synced 2026-11-16 18:57:19 +00:00
comet/crates/rpc/src/server.rs
Wing 40e79949c3 A terminal's writes reach it in the order they were sent
The RPC server ran each call on a task of its own, and tasks spawned back
to back start in no set order: tokio runs the newest first (its LIFO
slot). Two WriteTerminal calls in a row reached the PTY swapped. zgx's
terminal (Ghostty on macOS) answers a program's queries through the
bridge, each answer its own write: termenv (gh, glamour) asks for the
background (OSC 11), then the cursor (CSI 6n), took the cursor report
first as "no colour" and stopped reading, and gh's prompt read the colour
answer after it as typed keys ("unexpected escape sequence from
terminal: ['\x1b' ']']"), the cursor reports left in the shell (`;22R`).
Keys typed or pasted fast went out of order the same way, and a remote
viewer's numbered input lost the older of two (write_sequenced took it
for a replay).

A terminal's WriteTerminal and ResizeTerminal calls now run one after
another, in the order the connection sent them, on a lane of their own:
one per terminal, so a write that blocks (a paste into a program that
doesn't read) holds back no other terminal on the connection. A call's
answer is sent beside its lane, so a client slow to read answers doesn't
hold the next write back. When the connection ends, input already sent
still goes in; every other call is aborted as before.

tests/terminal_query_order.rs (unix; the probe is Perl): over the
engine's own dispatch, a program in the PTY asks as termenv does, 300
times, and a viewer answers as Ghostty does (two writes, at once).
Before: 284 of 300 read the cursor first (298 in an earlier run); after:
300 of 300 in order, the colour's answer in 2.64 ms median, 4.29 ms p99.
rpc's tests: two terminals' interleaved writes and resizes reach the
service each in order, a stuck terminal holds back no other, and input
sent before the connection ends goes in, in order (all three fail on
the base).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: zeronsh[bot] <346184242+zeronsh[bot]@users.noreply.github.com>
2026-11-11 15:07:42 -07:00

453 lines
18 KiB