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

106 lines
2.5 KiB
Go

package server
import (
"context"
"log"
"os"
"os/exec"
"syscall"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// supervise streams one terminal (cmd on terminal) until it ends: its login
// or shell exits, it is closed, or the server stops, which ends it too.
func (m *terminalManager) supervise(p *terminalProcess, cmd *exec.Cmd, terminal *os.File) {
defer m.wg.Done()
defer close(p.done)
titled := func(string) {
p.mu.Lock()
listed := !p.refused && p.login.prompted
p.mu.Unlock()
if listed {
record := p.snapshot(false, 0)
m.save(record)
m.publishRecord(record)
}
}
readDone := make(chan struct{})
go func() {
defer close(readDone)
p.read(terminal, titled)
}()
attachDone := make(chan error, 1)
go func() { attachDone <- p.waitAttachment(cmd, terminal, readDone) }()
var waitErr error
select {
case waitErr = <-attachDone:
case <-m.base.Done():
p.signal(syscall.SIGHUP)
select {
case waitErr = <-attachDone:
case <-time.After(time.Second):
p.signal(syscall.SIGKILL)
waitErr = <-attachDone
}
}
shutdown := m.base.Err() != nil
p.mu.Lock()
// The stream is read: its last incomplete character goes out as is.
p.outDone = true
p.pokeViewsLocked()
// A terminal that never showed its prompt was never listed: Open
// refuses it, so it is forgotten unannounced.
listed := p.login.prompted && !p.refused
switch {
case p.closing:
p.record.Status = protocol.TerminalClosed
default:
p.record.Status = protocol.TerminalExited
p.record.EndedAt = m.now().UnixMilli()
switch {
case shutdown:
p.record.Reason = terminalStopReason
case !p.login.loggedIn:
p.record.Reason = terminalLoginFailedReason
p.record.ExitCode = terminalExitCode(waitErr)
default:
p.record.ExitCode = terminalExitCode(waitErr)
}
}
p.pty, p.cmd = nil, nil
p.mu.Unlock()
record := p.snapshot(false, 0)
switch {
case !listed || record.Status == protocol.TerminalClosed:
m.forget(record.ID)
default:
m.save(record)
}
if !shutdown && listed {
m.publishRecord(record)
}
}
// recover records the terminals a previous server process left running as
// ended: they ended with it.
func (m *terminalManager) recover(ctx context.Context) {
if m.store == nil {
return
}
records, err := m.store.Terminals(ctx)
if err != nil {
log.Printf("terminal recover: %v", err)
return
}
for _, record := range records {
if record.Status != protocol.TerminalRunning {
continue
}
record.Status, record.EndedAt = protocol.TerminalExited, m.now().UnixMilli()
record.Reason = terminalRestartReason
m.save(record)
}
}