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:
2026-11-25 23:57:18 +00:00
parent 5ab1ec9e00
commit 0963a071d0
30 changed files with 1958 additions and 182 deletions
+10 -4
View File
@@ -86,6 +86,7 @@ func terminalReply(t *testing.T, ctx context.Context, conn *websocket.Conn, requ
// readTerminalUntil reads output frames of id from end on, in order, until
// the collected text contains want; it returns the text and the new end.
// A frame ending by end (sent before a read that covered it) is skipped.
func readTerminalUntil(t *testing.T, ctx context.Context, conn *websocket.Conn, id string, end int64, want string) (string, int64) {
t.Helper()
var seen strings.Builder
@@ -93,6 +94,9 @@ func readTerminalUntil(t *testing.T, ctx context.Context, conn *websocket.Conn,
frame := readShellFrame(t, ctx, conn, func(m protocol.Message) bool {
return m.Type == protocol.TerminalEvent && m.TerminalEvent.TerminalID == id && m.TerminalEvent.Terminal == nil
}).TerminalEvent
if frame.End <= end {
continue
}
if frame.Offset != end {
t.Fatalf("frame at %d after %d", frame.Offset, end)
}
@@ -113,10 +117,12 @@ func TestTerminalOverWebsocket(t *testing.T) {
t.Fatalf("open reply = %+v", opened)
}
terminalLogIn(t, ctx, conns[0], term.ID)
if got := terminalReply(t, ctx, conns[0], terminalRequest(protocol.TerminalInput, "in", protocol.TerminalRequest{TerminalID: term.ID, Data: "pwd; echo sum-$((40+2))\r"})); got.Type != protocol.TerminalInput {
t.Fatalf("input reply = %+v", got)
}
_, end := readTerminalUntil(t, ctx, conns[0], term.ID, 0, "sum-42")
// The replies above skipped the login's frames: read up to here, and
// the frames continue from the reply's end.
synced := terminalReply(t, ctx, conns[0], terminalRequest(protocol.TerminalOutput, "sync", protocol.TerminalRequest{TerminalID: term.ID})).Terminal
// Its reply is not awaited: that would skip the echo's frames.
writeChat(t, ctx, conns[0], terminalRequest(protocol.TerminalInput, "in", protocol.TerminalRequest{TerminalID: term.ID, Data: "pwd; echo sum-$((40+2))\r"}))
_, end := readTerminalUntil(t, ctx, conns[0], term.ID, synced.OutputEnd, "sum-42")
resized := terminalReply(t, ctx, conns[1], terminalRequest(protocol.TerminalResize, "size", protocol.TerminalRequest{TerminalID: term.ID, Cols: 120, Rows: 33}))
if resized.Type != protocol.TerminalResize || resized.Terminal.Cols != 120 || resized.Terminal.Rows != 33 {
t.Fatalf("resize reply = %+v", resized)