Files
pipelines/internal/server/terminal_handlers.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

79 lines
2.5 KiB
Go

package server
import (
"context"
"errors"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// isTerminalRequest reports the request types handleTerminalRequest answers.
func isTerminalRequest(kind string) bool {
switch kind {
case protocol.TerminalOpen, protocol.TerminalList, protocol.TerminalInput, protocol.TerminalResize,
protocol.TerminalOutput, protocol.TerminalClose, protocol.TerminalAck:
return true
}
return false
}
// handleTerminalRequest answers one machine-terminal request from an
// authenticated connection. Opening or reading a terminal subscribes the
// connection to its output frames.
func handleTerminalRequest(ctx context.Context, request protocol.Message, m *terminalManager, clientID string, write func(protocol.Message) error) {
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
if m == nil {
fail(errors.New("terminals are not configured on this server"))
return
}
body := request.TerminalRequest
if body == nil {
body = &protocol.TerminalRequest{}
}
if request.Type != protocol.TerminalOpen && request.Type != protocol.TerminalList && body.TerminalID == "" {
fail(errors.New("a terminal_id is required"))
return
}
reply := protocol.Message{Type: request.Type, ID: request.ID}
var record protocol.TerminalRecord
var err error
switch request.Type {
case protocol.TerminalOpen:
record, err = m.Open(ctx, clientID, body.Cwd, body.Cols, body.Rows, body.Flow)
case protocol.TerminalList:
var records []protocol.TerminalRecord
records, err = m.List(ctx)
reply.Terminals = records
case protocol.TerminalInput:
err = m.Input(body.TerminalID, body.Data)
case protocol.TerminalResize:
record, err = m.Resize(body.TerminalID, body.Cols, body.Rows)
case protocol.TerminalOutput:
// The output frames that follow the reply wait until it is written.
defer func() {
if v := m.clients.view(clientID, body.TerminalID); v != nil {
v.release()
}
}()
record, err = m.Output(ctx, clientID, body.TerminalID, body.After, body.Flow)
case protocol.TerminalAck:
err = m.Ack(clientID, body.TerminalID, body.After)
case protocol.TerminalClose:
record, err = m.Close(ctx, body.TerminalID)
}
if err != nil {
fail(err)
return
}
if record.ID != "" {
reply.Terminal = &record
}
_ = write(reply)
if request.Type == protocol.TerminalOpen {
// The login's output frames follow the reply.
if v := m.clients.view(clientID, record.ID); v != nil {
v.release()
}
}
}