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
+67 -17
View File
@@ -42,29 +42,79 @@ func (o *chatShellOutput) tail(max int) (string, int64) {
return validChatShellText(o.data[from:]), o.start + int64(from)
}
// pending returns the output not yet emitted, at most max bytes (the newest
// ones when more is waiting: the skipped bytes are a visible gap to clients).
// An incomplete trailing character is held back unless final is set.
// pending returns the oldest output not yet emitted, at most max bytes, and
// marks it emitted. An incomplete trailing character is held back unless
// final is set.
func (o *chatShellOutput) pending(max int, final bool) (text string, offset, end int64, ok bool) {
from := o.emitted
if from < o.start {
from = o.start
text, offset, end, ok = o.chunk(o.emitted, o.total, max, final)
if ok {
o.emitted = end
}
available := o.data[from-o.start:]
if !final {
available = available[:completeUTF8Prefix(available)]
return text, offset, end, ok
}
// unemitted is how many written bytes no frame has carried yet.
func (o *chatShellOutput) unemitted() int64 { return o.total - max(o.emitted, o.start) }
// skipUnemitted gives up all but the newest max unemitted bytes (watchers
// that stopped draining): the next frame starts after a visible gap.
func (o *chatShellOutput) skipUnemitted(max int) {
o.emitted = o.newestFrom(o.emitted, max)
}
// newestFrom moves from forward so that at most max retained bytes follow
// it, onto a character boundary.
func (o *chatShellOutput) newestFrom(from int64, max int) int64 {
from = maxInt64(from, o.start)
if o.total-from <= int64(max) {
return from
}
if len(available) == 0 {
return o.start + int64(runeStartAfter(o.data, int(o.total-int64(max)-o.start)))
}
// chunk returns the retained bytes from absolute offset from (the oldest
// retained byte when from was dropped) up to limit, at most max of them,
// never splitting a character inside the retained bytes. An incomplete
// character at the newest byte is held back unless final is set. offset is
// where the text starts, which is after from when from was dropped.
func (o *chatShellOutput) chunk(from, limit int64, max int, final bool) (text string, offset, end int64, ok bool) {
from = maxInt64(from, o.start)
limit = min(limit, o.total)
if from >= limit {
return "", 0, 0, false
}
end = from + int64(len(available))
if len(available) > max {
skip := runeStartAfter(available, len(available)-max)
available = available[skip:]
from += int64(skip)
data := o.data[from-o.start : limit-o.start]
if len(data) > max {
data = data[:runeStartBefore(data, max)]
} else if limit < o.total {
data = data[:runeStartBefore(o.data[from-o.start:], len(data))]
} else if !final {
data = data[:completeUTF8Prefix(data)]
}
o.emitted = end
return validChatShellText(available), from, end, true
if len(data) == 0 {
return "", 0, 0, false
}
return validChatShellText(data), from, from + int64(len(data)), true
}
func maxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
// runeStartBefore moves index back (at most three bytes) to a byte that
// starts a character, so a cut at it never splits one (zero when the only
// character there is incomplete); invalid bytes, which start nothing, are
// cut where they are.
func runeStartBefore(data []byte, index int) int {
for back := 0; back < utf8.UTFMax && index-back >= 0; back++ {
if index-back >= len(data) || utf8.RuneStart(data[index-back]) {
return index - back
}
}
return index
}
// runeStartAfter moves index forward (at most three bytes) to the first byte