Files
pipelines/internal/server/terminal_active.go
T
alan c3458d8a7f Terminals: one active client per terminal; keystrokes skip the request queue
Several clients drawing one terminal (machine terminal or chat terminal
command) no longer interfere. The server tracks, per terminal, the
connections drawing it, most recently active first: the active client
(the opener, then whoever last typed or focused it) owns the PTY size and
is the only one whose emulator's automatic query answers reach the
process; other clients' answers are acknowledged and dropped, and their
sizes recorded but not applied. When the active connection closes the next
most recent client's size is applied.

Protocol (backwards compatible; all new fields optional):
- terminal_input / chat_shell_input: reply (emulator answer), cols/rows
  (the typist's size, applied before the input).
- terminal_resize / chat_shell_resize: activate (the client focused it).

Latency: PipelinesClient sends terminal_input, terminal_resize,
chat_shell_input and chat_shell_resize at once rather than behind the
previous request's reply. The server reads those onto a per-connection
terminal lane handled in order beside the ordinary request loop, and
terminal_open (up to 5 s waiting for su) now runs off the read loop.

Client: ChatShellTerminalSession routes what the emulator emits while
drawing output to onReply and reports focus through activate();
ChatShellInputPipe keeps answers and typing in separate requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:37:16 +00:00

161 lines
5.2 KiB
Go

package server
import (
"errors"
"log"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// The active-client policy (terminal_viewers.go) applied to machine
// terminals and to chats' terminal commands: requests name their
// connection, and a connection that closes hands the terminal to the next.
// ClientInput is Input from clientID: typing makes it the active client
// (its size, cols x rows when it sent one, else its last, is applied
// first); an automatic reply (reply) from any other client is acknowledged
// and dropped.
func (m *terminalManager) ClientInput(clientID, id, data string, reply bool, cols, rows int) error {
if p := m.process(id); p != nil && p.running() {
if reply && !p.viewers.answers(clientID) {
return nil
}
if !reply {
if size := p.viewers.typedAt(clientID, cols, rows); size.ok && !p.sized(size) {
if err := p.applySize(size.cols, size.rows); err != nil {
log.Printf("terminal %s resize: %v", id, err)
}
}
}
}
return m.Input(id, data)
}
// ClientResize records the size clientID draws terminal id at and applies
// it when clientID is (or, with activate, becomes) the active client. The
// record carries the size the terminal has.
func (m *terminalManager) ClientResize(clientID, id string, cols, rows int, activate bool) (protocol.TerminalRecord, error) {
if cols <= 0 || rows <= 0 {
return protocol.TerminalRecord{}, errors.New("positive cols and rows are required")
}
p, err := m.live(id)
if err != nil {
return protocol.TerminalRecord{}, err
}
if size := p.viewers.resize(clientID, cols, rows, activate); size.ok {
return m.Resize(id, size.cols, size.rows)
}
return p.snapshot(false, 0), nil
}
// attachViewer records clientID as drawing terminal id (it read it).
func (m *terminalManager) attachViewer(clientID, id string) {
if p := m.process(id); p != nil {
p.viewers.attach(clientID)
}
}
// detach drops clientID's connection: its output frames, and its part in
// every terminal it drew; a terminal it was active in takes the next
// client's size.
func (m *terminalManager) detach(clientID string) {
m.clients.detach(clientID)
m.mu.Lock()
terms := make(map[string]*terminalProcess, len(m.terms))
for id, p := range m.terms {
terms[id] = p
}
m.mu.Unlock()
for id, p := range terms {
if size := p.viewers.leave(clientID); size.ok && p.running() {
if err := p.applySize(size.cols, size.rows); err != nil {
log.Printf("terminal %s resize: %v", id, err)
}
}
}
}
// ClientInput is Input from clientID, under the same policy as the machine
// terminals' ClientInput.
func (r *chatShellRunner) ClientInput(clientID, sessionID, runID, data string, reply bool, cols, rows int) error {
if p := r.activeProcess(sessionID, runID); p != nil && p.terminal != nil {
if reply && !p.viewers.answers(clientID) {
return nil
}
if !reply {
if size := p.viewers.typedAt(clientID, cols, rows); size.ok && !p.sized(size) {
if _, err := r.Resize(sessionID, runID, size.cols, size.rows); err != nil {
log.Printf("chat shell %s resize: %v", runID, err)
}
}
}
}
return r.Input(sessionID, runID, data)
}
// ClientResize is Resize from clientID, applied only when it is (or, with
// activate, becomes) the active client.
func (r *chatShellRunner) ClientResize(clientID, sessionID, runID string, cols, rows int, activate bool) (protocol.ChatShellRunRecord, error) {
if cols <= 0 || rows <= 0 {
return protocol.ChatShellRunRecord{}, errors.New("positive cols and rows are required")
}
p, err := r.terminalProcess(sessionID, runID)
if err != nil {
return protocol.ChatShellRunRecord{}, err
}
if size := p.viewers.resize(clientID, cols, rows, activate); size.ok {
return r.Resize(sessionID, runID, size.cols, size.rows)
}
p.mu.Lock()
defer p.mu.Unlock()
return p.record, nil
}
// attachViewer makes clientID, which started runID at cols x rows, its
// active client.
func (r *chatShellRunner) attachViewer(clientID, sessionID, runID string, cols, rows int) {
if p := r.activeProcess(sessionID, runID); p != nil && p.terminal != nil && cols > 0 && rows > 0 {
p.viewers.resize(clientID, cols, rows, true)
}
}
// leave drops clientID's part in every running terminal command it drew.
func (r *chatShellRunner) leave(clientID string) {
if r == nil {
return
}
type run struct{ sessionID, runID string }
r.mu.Lock()
procs := make(map[run]*chatShellProcess, len(r.active))
for id, p := range r.active {
procs[run{p.record.SessionID, id}] = p
}
r.mu.Unlock()
for key, p := range procs {
if p.terminal == nil {
continue
}
if size := p.viewers.leave(clientID); size.ok {
if _, err := r.Resize(key.sessionID, key.runID, size.cols, size.rows); err != nil {
log.Printf("chat shell %s resize: %v", key.runID, err)
}
}
}
}
// sized reports whether the terminal already has size (clamped).
func (p *terminalProcess) sized(size terminalSize) bool {
cols, rows := chatShellTerminalSize(size.cols, size.rows)
p.mu.Lock()
defer p.mu.Unlock()
return p.record.Cols == cols && p.record.Rows == rows
}
// sized reports whether the terminal command already has size (clamped).
func (p *chatShellProcess) sized(size terminalSize) bool {
cols, rows := chatShellTerminalSize(size.cols, size.rows)
p.mu.Lock()
defer p.mu.Unlock()
return p.record.Cols == cols && p.record.Rows == rows
}