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:
@@ -35,7 +35,6 @@ type terminalManager struct {
|
||||
promptWait time.Duration
|
||||
home func() (string, error)
|
||||
now func() time.Time
|
||||
flush time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
terms map[string]*terminalProcess
|
||||
@@ -45,7 +44,7 @@ type terminalManager struct {
|
||||
func newTerminalManager(base context.Context, store terminalStore) *terminalManager {
|
||||
return &terminalManager{
|
||||
base: base, store: store, clients: &terminalClients{},
|
||||
login: terminalLoginCommand, promptWait: terminalPromptWait, home: OperatorHomeDirectory, now: time.Now, flush: 50 * time.Millisecond,
|
||||
login: terminalLoginCommand, promptWait: terminalPromptWait, home: OperatorHomeDirectory, now: time.Now,
|
||||
terms: map[string]*terminalProcess{},
|
||||
}
|
||||
}
|
||||
@@ -60,9 +59,10 @@ func newTerminalID() (string, error) {
|
||||
|
||||
// Open starts a terminal in cwd at cols x rows at the machine's login prompt
|
||||
// and returns its record once the prompt shows. clientID (when set) is
|
||||
// subscribed to its output before the login writes. A login that cannot ask
|
||||
// for the password is refused.
|
||||
func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols, rows int) (protocol.TerminalRecord, error) {
|
||||
// subscribed to its output before the login writes (flow: acknowledged, and
|
||||
// the record carries the window). A login that cannot ask for the password
|
||||
// is refused.
|
||||
func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols, rows int, flow bool) (protocol.TerminalRecord, error) {
|
||||
if m.store == nil {
|
||||
return protocol.TerminalRecord{}, errors.New("terminals need the server's store, which is not configured")
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols,
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
p, err := m.start(clientID, id, dir, cmd, cols, rows)
|
||||
p, err := m.start(clientID, id, dir, cmd, cols, rows, flow)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
@@ -100,12 +100,15 @@ func (m *terminalManager) Open(ctx context.Context, clientID, cwd string, cols,
|
||||
}
|
||||
record = p.snapshot(false, 0)
|
||||
m.publishRecord(record)
|
||||
if flow && clientID != "" && m.clients.view(clientID, id) != nil {
|
||||
record.Window = protocol.TerminalFlowWindow
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// start runs cmd in a new terminal's PTY and supervises it; the terminal
|
||||
// counts against the limit from here on.
|
||||
func (m *terminalManager) start(clientID, id, dir string, cmd *exec.Cmd, cols, rows int) (*terminalProcess, error) {
|
||||
func (m *terminalManager) start(clientID, id, dir string, cmd *exec.Cmd, cols, rows int, flow bool) (*terminalProcess, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.base.Err() != nil {
|
||||
@@ -124,7 +127,12 @@ func (m *terminalManager) start(clientID, id, dir string, cmd *exec.Cmd, cols, r
|
||||
}
|
||||
m.terms[id] = p
|
||||
if clientID != "" {
|
||||
m.clients.subscribe(clientID, id)
|
||||
// Its frames follow the open reply (the handler releases them).
|
||||
if v := m.clients.subscribe(clientID, p, flow); v != nil {
|
||||
p.mu.Lock()
|
||||
v.paused = true
|
||||
p.mu.Unlock()
|
||||
}
|
||||
}
|
||||
m.wg.Add(1)
|
||||
go m.supervise(p, cmd, terminal)
|
||||
@@ -166,6 +174,16 @@ func (m *terminalManager) refuse(p *terminalProcess) {
|
||||
p.mu.Unlock()
|
||||
p.signal(syscall.SIGKILL)
|
||||
<-p.done
|
||||
p.mu.Lock()
|
||||
views := make([]*terminalView, 0, len(p.views))
|
||||
for v := range p.views {
|
||||
views = append(views, v)
|
||||
}
|
||||
id := p.record.ID
|
||||
p.mu.Unlock()
|
||||
for _, v := range views {
|
||||
v.client.unsubscribe(id)
|
||||
}
|
||||
}
|
||||
|
||||
// terminalLastLine is the last non-empty line of a terminal's output, for a
|
||||
@@ -269,11 +287,18 @@ func (m *terminalManager) Resize(id string, cols, rows int) (protocol.TerminalRe
|
||||
return p.snapshot(false, 0), nil
|
||||
}
|
||||
|
||||
// Output is one terminal with its retained output from byte after on. A
|
||||
// terminal ended before this process started has no output.
|
||||
func (m *terminalManager) Output(ctx context.Context, id string, after int64) (protocol.TerminalRecord, error) {
|
||||
// Output is one terminal with its retained output from byte after on,
|
||||
// subscribing clientID (when set) to the output that follows it (flow:
|
||||
// acknowledged); the caller releases that subscription once the reply is
|
||||
// written (m.clients.view(...).release). A terminal ended before this
|
||||
// process started has no output.
|
||||
func (m *terminalManager) Output(ctx context.Context, clientID, id string, after int64, flow bool) (protocol.TerminalRecord, error) {
|
||||
if p := m.process(id); p != nil {
|
||||
return p.snapshot(true, after), nil
|
||||
var v *terminalView
|
||||
if clientID != "" {
|
||||
v = m.clients.subscribe(clientID, p, flow)
|
||||
}
|
||||
return p.readFor(after, v), nil
|
||||
}
|
||||
records, err := m.List(ctx)
|
||||
if err != nil {
|
||||
@@ -287,6 +312,16 @@ func (m *terminalManager) Output(ctx context.Context, id string, after int64) (p
|
||||
return protocol.TerminalRecord{}, errors.New("this machine has no such terminal")
|
||||
}
|
||||
|
||||
// Ack records that clientID consumed terminal id's output up to end.
|
||||
func (m *terminalManager) Ack(clientID, id string, end int64) error {
|
||||
v := m.clients.view(clientID, id)
|
||||
if v == nil {
|
||||
return errors.New("this connection does not read that terminal")
|
||||
}
|
||||
v.ack(end)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close ends a terminal (killing its login and shell) and forgets
|
||||
// it. It waits briefly for the shell to go and returns the closed record.
|
||||
func (m *terminalManager) Close(ctx context.Context, id string) (protocol.TerminalRecord, error) {
|
||||
@@ -307,7 +342,7 @@ func (m *terminalManager) Close(ctx context.Context, id string) (protocol.Termin
|
||||
}
|
||||
return p.snapshot(false, 0), nil
|
||||
}
|
||||
record, err := m.Output(ctx, id, 0)
|
||||
record, err := m.Output(ctx, "", id, 0, false)
|
||||
if err != nil {
|
||||
return protocol.TerminalRecord{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user