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
+29 -1
View File
@@ -134,15 +134,43 @@ func TestChatShellOutputKeepsCharactersWhole(t *testing.T) {
t.Fatalf("partial character emitted: %q %d %d", text, offset, end)
}
out.write([]byte("\xa9cdefghij"))
// The oldest retained bytes go first; the dropped ones are a gap.
text, offset, end, ok := out.pending(3, false)
if !ok || text != "hij" || offset != 9 || end != 12 {
if !ok || text != "ghi" || offset != 8 || end != 11 {
t.Fatalf("bounded chunk = %q %d %d", text, offset, end)
}
if text, offset, end, ok := out.pending(3, false); !ok || text != "j" || offset != 11 || end != 12 {
t.Fatalf("next chunk = %q %d %d", text, offset, end)
}
if tail, start := out.tail(6); tail != "ghij" || start != 8 {
t.Fatalf("tail = %q at %d", tail, start)
}
}
// A chunk never ends inside a character, whether a size bound or a limit
// (a flow window) cuts it; invalid bytes keep their exact offsets.
func TestChatShellOutputChunkBounds(t *testing.T) {
out := newChatShellOutput(1 << 10)
out.write([]byte("a\xe2\x82\xacb\xff\xfec"))
if text, offset, end, ok := out.chunk(0, 3, 64, false); !ok || text != "a" || offset != 0 || end != 1 {
t.Fatalf("limit inside the euro sign = %q %d %d", text, offset, end)
}
if _, _, _, ok := out.chunk(1, 3, 64, false); ok {
t.Fatal("a chunk of only part of a character was sent")
}
if text, offset, end, ok := out.chunk(0, 9, 3, false); !ok || text != "a" || end != 1 || offset != 0 {
t.Fatalf("size bound inside the euro sign = %q %d %d", text, offset, end)
}
text, offset, end, ok := out.chunk(1, 9, 64, false)
if !ok || text != "\u20acb\ufffdc" || offset != 1 || end != 8 {
t.Fatalf("rest = %q %d %d", text, offset, end)
}
out.skipUnemitted(2)
if text, offset, end, ok := out.pending(64, false); !ok || text != "\ufffdc" || offset != 6 || end != 8 {
t.Fatalf("after skipping = %q %d %d", text, offset, end)
}
}
func TestChatShellSharePromptFencesOutput(t *testing.T) {
code := 1
prompt := chatShellSharePrompt(protocol.ChatShellRunRecord{Command: "cat notes.md", Directory: "/w", Status: protocol.ChatShellRunExited, ExitCode: &code, DurationMS: 1500, Output: "```go\nx\n```\n", OutputBytes: 13})