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:
@@ -0,0 +1,122 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// outputFrames is runID's output frames so far, in order.
|
||||
func (f *shellFrames) outputFrames(runID string) []protocol.ChatShellRunEventFrame {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
var out []protocol.ChatShellRunEventFrame
|
||||
for _, frame := range f.frames {
|
||||
if frame.RunID == runID && frame.Run == nil && !frame.Dismissed {
|
||||
out = append(out, frame)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// joined is the frames' text and the bytes skipped between them.
|
||||
func joined(t *testing.T, frames []protocol.ChatShellRunEventFrame) (string, int64) {
|
||||
t.Helper()
|
||||
var text strings.Builder
|
||||
var end, skipped int64
|
||||
for _, frame := range frames {
|
||||
if frame.Offset < end {
|
||||
t.Fatalf("frame [%d, %d) overlaps %d", frame.Offset, frame.End, end)
|
||||
}
|
||||
skipped += frame.Offset - end
|
||||
text.WriteString(frame.Output)
|
||||
end = frame.End
|
||||
}
|
||||
return text.String(), skipped
|
||||
}
|
||||
|
||||
// A command writing more than the retained tail to watchers that keep up
|
||||
// is streamed whole, oldest first, without a gap.
|
||||
func TestChatShellFlowStreamsEveryByte(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
runner.flush = time.Hour // output must not wait for a tick
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "seq 1 300000", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner.Wait()
|
||||
text, skipped := joined(t, frames.outputFrames(run.ID))
|
||||
if skipped != 0 {
|
||||
t.Fatalf("%d bytes skipped", skipped)
|
||||
}
|
||||
sep := "\n"
|
||||
if run.Terminal {
|
||||
sep = "\r\n"
|
||||
}
|
||||
var want strings.Builder
|
||||
for i := 1; i <= 300000; i++ {
|
||||
want.WriteString(itoa(i) + sep)
|
||||
}
|
||||
if text != want.String() {
|
||||
t.Fatalf("streamed %d bytes, want %d", len(text), want.Len())
|
||||
}
|
||||
}
|
||||
|
||||
// Output is pushed when it arrives, not on the flush tick.
|
||||
func TestChatShellFlowPushesAtOnce(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
runner.flush = time.Hour
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "echo early; sleep 5", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = runner.Stop("ses_1", run.ID); runner.Wait() })
|
||||
for deadline := time.Now().Add(3 * time.Second); ; time.Sleep(5 * time.Millisecond) {
|
||||
if text, _ := joined(t, frames.outputFrames(run.ID)); strings.Contains(text, "early") {
|
||||
return
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("no output frame while the command runs")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Watchers that stop draining hold the command back only for
|
||||
// chatShellStall; then the output skips ahead and the command finishes.
|
||||
func TestChatShellFlowStalledWatchersDoNotHoldForever(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
runner.backlog = func(string) int { return chatShellBacklog }
|
||||
start := time.Now()
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "seq 1 400000", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner.Wait()
|
||||
if elapsed := time.Since(start); elapsed < chatShellStall || elapsed > 6*chatShellStall {
|
||||
t.Fatalf("finished after %s", elapsed)
|
||||
}
|
||||
if _, skipped := joined(t, frames.outputFrames(run.ID)); skipped == 0 {
|
||||
t.Fatal("stalled watchers were sent everything")
|
||||
}
|
||||
if final := frames.final(run.ID); final == nil || final.Status != protocol.ChatShellRunExited {
|
||||
t.Fatalf("final = %+v", final)
|
||||
}
|
||||
}
|
||||
|
||||
func itoa(i int) string {
|
||||
var b [20]byte
|
||||
n := len(b)
|
||||
for {
|
||||
n--
|
||||
b[n] = byte('0' + i%10)
|
||||
i /= 10
|
||||
if i == 0 {
|
||||
return string(b[n:])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user