Files
pipelines/internal/server/chat_shell_run_output.go
T
alan 0963a071d0 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>
2026-11-25 23:57:18 +00:00

147 lines
4.6 KiB
Go

package server
import (
"strings"
"unicode/utf8"
)
// chatShellOutput is the bounded tail of one operator command's merged output.
// Offsets are absolute bytes of everything the command wrote, so a client can
// tell a continuation from a gap. The buffer may grow to twice its limit before
// compacting, which keeps a flooding command from copying the tail per write.
type chatShellOutput struct {
limit int
data []byte
start int64
total int64
emitted int64
}
func newChatShellOutput(limit int) *chatShellOutput {
return &chatShellOutput{limit: limit}
}
func (o *chatShellOutput) write(p []byte) {
o.data = append(o.data, p...)
o.total += int64(len(p))
if len(o.data) <= 2*o.limit {
return
}
cut := runeStartAfter(o.data, len(o.data)-o.limit)
o.data = append([]byte(nil), o.data[cut:]...)
o.start += int64(cut)
}
// tail returns at most max retained bytes ending at the newest byte, starting
// on a character boundary, with the absolute offset of its first byte.
func (o *chatShellOutput) tail(max int) (string, int64) {
from := 0
if len(o.data) > max {
from = runeStartAfter(o.data, len(o.data)-max)
}
return validChatShellText(o.data[from:]), o.start + int64(from)
}
// 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) {
text, offset, end, ok = o.chunk(o.emitted, o.total, max, final)
if ok {
o.emitted = end
}
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
}
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
}
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)]
}
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
// that starts a character, so a cut never splits one.
func runeStartAfter(data []byte, index int) int {
for step := 0; step < utf8.UTFMax-1 && index < len(data) && !utf8.RuneStart(data[index]); step++ {
index++
}
return index
}
// completeUTF8Prefix is the length of data without a trailing partial
// character that a later read may complete.
func completeUTF8Prefix(data []byte) int {
for back := 1; back <= utf8.UTFMax-1 && back <= len(data); back++ {
if utf8.RuneStart(data[len(data)-back]) {
if !utf8.FullRune(data[len(data)-back:]) {
return len(data) - back
}
break
}
}
return len(data)
}
// validChatShellText keeps binary output representable in a JSON frame.
func validChatShellText(data []byte) string {
return strings.ToValidUTF8(string(data), "�")
}