Terminals: flow-controlled output, immediate echo, sliced emulator writes
Machine terminals no longer skip output for live viewers. Each connection's subscription is a cursor into the 1 MiB retained tail, drained by the connection's writer as soon as the PTY reader writes (no 50 ms tick; a streaming frame gathers for at most 2 ms). A subscription opened with flow: true gets window (256 KiB) in its reply and acknowledges drawn output with terminal_ack; while one is full the reader stops reading the PTY, so the program waits. A subscription full for 3 s without acking stops holding the terminal and later sees a gap; a gone connection drops its subscriptions. Clients that never ask for flow keep the old pacing (newest 64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames that follow them, which start exactly at the reply's output_end. Chat shell runs share the buffer code: output is pushed as it arrives, oldest first, while the chat's watchers keep short queues; 256 KiB unpushed stops the reader, and watchers behind for 3 s get the old pacing. The app asks for flow, takes offsets from the server (frame end, reply output_end) instead of re-encoded text length, acks per quarter window, and writes replays and bursts into the emulator in slices that yield to the event loop (ChatShellTerminalFeed, shared with chat shells). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -115,8 +115,55 @@ data?, after?}`.
|
||||
loop: it waits up to 3 s for the shell).
|
||||
- Pushed `terminal_event`: output `{terminal_id, output, offset, end}` to
|
||||
subscribed connections, lifecycle `{terminal_id, terminal}` (opened,
|
||||
title, exited, closed) to every connection. A slow connection's queue
|
||||
(256 frames) drops output; the client sees the offset gap and re-reads.
|
||||
title, exited, closed) to every connection. Output flow is described
|
||||
under "Output flow" below; offsets are stream bytes, taken from the
|
||||
server (`end`, `output_end`), never from the text's re-encoded length.
|
||||
|
||||
## Output flow (2026-11-25)
|
||||
|
||||
Server (`terminal_flow.go`, `terminal_clients.go`): each connection's
|
||||
subscription is a cursor into the terminal's 1 MiB retained tail; the
|
||||
connection's writer goroutine sends from it as soon as the PTY reader has
|
||||
written (no tick), coalescing what arrived during the previous write and,
|
||||
while output streams, letting a frame gather for at most 2 ms. The app asks
|
||||
for `flow: true` on `terminal_open`/`terminal_output`; those replies carry
|
||||
`window` (256 KiB) and the app sends `terminal_ack {terminal_id, after}`
|
||||
each time it has drawn a quarter window. A flow subscription is sent at most
|
||||
`window` bytes past its last ack, and while one is full the PTY reader stops
|
||||
(the program blocks on its writes) instead of skipping. One that stays full
|
||||
for 3 s without acknowledging stops holding the terminal; on its next ack it
|
||||
gets a frame after a gap and re-reads. A gone connection drops its
|
||||
subscriptions at once. A subscription without `flow` (an older app build) is
|
||||
never waited for and keeps the old pacing (newest 64 KiB, one frame per
|
||||
50 ms). `terminal_open` and `terminal_output` replies are written before the
|
||||
frames that follow them, which start exactly at the reply's `output_end`.
|
||||
|
||||
Client: `ChatShellTerminalFeed` (shared with chat shells) writes output into
|
||||
the emulator in 16 Ki-unit slices and yields to the event loop after 6 ms,
|
||||
so a 1 MiB replay or a burst never holds a UI frame; `drawn` is the server
|
||||
byte end of what has been written, which `TerminalTab` acknowledges.
|
||||
|
||||
Chat shell runs (`chat_shell_supervise.go`) share the output buffer code:
|
||||
output is pushed as it arrives, oldest first, while every watcher of the
|
||||
chat has fewer than 8 queued frames; 256 KiB unpushed stops the reader. A
|
||||
watcher behind for 3 s gets the old pacing (newest chunk per flush tick, a
|
||||
gap) until it catches up. The DB snapshot every 2 s is unchanged.
|
||||
|
||||
Measured (`PIPELINES_TERMINAL_MEASURE=1 go test ./internal/server -run
|
||||
TestMeasureTerminalStream -v`, loopback, fast reading client; before = the
|
||||
old server or a client without `flow`):
|
||||
|
||||
| workload | before | after |
|
||||
| --- | --- | --- |
|
||||
| `seq 1 500000` (3.9 MB) | 3 frames, 3.69 MB skipped, 1.3 MB/s | 70 frames, 0 skipped, 27 MB/s |
|
||||
| `cat` 2.1 MB | 1 frame, 2.04 MB skipped | 34 frames, 0 skipped, 117 MB/s |
|
||||
| `yes \| head -1000000` (3.0 MB) | 5 frames, 2.67 MB skipped | 126 frames, 0 skipped, 12 MB/s |
|
||||
| keystroke echo (median, 40 keys) | 43.3 ms | 0.17 ms |
|
||||
| longest gap between frames while streaming | 50 ms | 2.5-4.2 ms |
|
||||
|
||||
Emulator (`chat_shell_terminal_feed_test.dart`, 896 KiB replay): one
|
||||
synchronous write 160 ms; sliced, the same 161 ms over 20 turns, the
|
||||
longest turn 12.5 ms.
|
||||
|
||||
## Login and lifetime (server)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user