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
+14 -3
View File
@@ -42,10 +42,14 @@ type chatShellRunner struct {
base context.Context
store chatShellRunStore
publish func(sessionID string, frame protocol.ChatShellRunEventFrame)
// backlog is the most frames one of the session's watchers has queued
// (nil: nobody is waited for); live output waits while it is high.
backlog func(sessionID string) int
now func() time.Time
shell func() string
// grace separates Stop's SIGINT from its SIGKILL; flush batches live output
// frames; persist is the interval a running run's output is saved at.
// grace separates Stop's SIGINT from its SIGKILL; flush is the interval a
// running run is checked for saving at; persist is the interval a running
// run's output is saved at. Live output is pushed as it arrives.
grace time.Duration
flush time.Duration
persist time.Duration
@@ -69,6 +73,9 @@ type chatShellProcess struct {
// closed is set once the reader is closed; a read still blocked on a
// terminal a descendant holds open records nothing after it.
closed bool
// wake is signalled when output arrives; room when output was pushed,
// which may let a reader waiting on unpushed output go on.
wake, room chan struct{}
}
func newChatShellRunner(base context.Context, store chatShellRunStore, publish func(string, protocol.ChatShellRunEventFrame)) *chatShellRunner {
@@ -170,7 +177,10 @@ func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, comma
if terminal != nil {
record.Terminal, record.Cols, record.Rows = true, cols, rows
}
p := &chatShellProcess{record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, terminal: terminal, done: make(chan struct{})}
p := &chatShellProcess{
record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, terminal: terminal, done: make(chan struct{}),
wake: make(chan struct{}, 1), room: make(chan struct{}, 1),
}
if err := r.store.SaveChatShellRun(ctx, record, protocol.ChatShellRunHistory); err != nil {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
reader.Close()
@@ -263,5 +273,6 @@ func (h *chatHub) startShellRunner(ctx context.Context, store chatShellRunStore)
h.shellRuns = newChatShellRunner(ctx, store, func(sessionID string, frame protocol.ChatShellRunEventFrame) {
h.fanout(sessionID, protocol.Message{Type: protocol.ChatShellRunEvent, ChatShellRunEvent: &frame}, "")
})
h.shellRuns.backlog = h.roomBacklog
h.shellRuns.recover(ctx)
}