c3458d8a7f
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>
156 lines
4.7 KiB
Go
156 lines
4.7 KiB
Go
package server
|
|
|
|
import "sync"
|
|
|
|
// terminalViewers is the set of client connections drawing one terminal (a
|
|
// machine terminal or a chat's terminal command), most recently active
|
|
// first. The first is the active client: the terminal's size is the size it
|
|
// draws at, and only its emulator's automatic answers to terminal queries
|
|
// (cursor position, device attributes) reach the process. Every client's
|
|
// emulator answers each query it draws, so without this a query got one
|
|
// answer per client, and the size was whichever client resized last, even
|
|
// after it left.
|
|
//
|
|
// A client becomes active by typing, by a resize marked activate (the
|
|
// client focused the terminal), or by being the first to attach or resize.
|
|
// When the active client leaves, the next most recent one becomes active
|
|
// and its last size is applied. The zero value is ready to use.
|
|
type terminalViewers struct {
|
|
mu sync.Mutex
|
|
list []terminalViewer
|
|
}
|
|
|
|
type terminalViewer struct {
|
|
client string
|
|
cols, rows int
|
|
}
|
|
|
|
// terminalSize is a size to apply to the terminal, when ok.
|
|
type terminalSize struct {
|
|
cols, rows int
|
|
ok bool
|
|
}
|
|
|
|
func (v *terminalViewers) indexLocked(client string) int {
|
|
for i, viewer := range v.list {
|
|
if viewer.client == client {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
// frontLocked moves client to the front, adding it when absent, and
|
|
// returns its entry.
|
|
func (v *terminalViewers) frontLocked(client string) *terminalViewer {
|
|
viewer := terminalViewer{client: client}
|
|
if i := v.indexLocked(client); i >= 0 {
|
|
viewer = v.list[i]
|
|
v.list = append(v.list[:i], v.list[i+1:]...)
|
|
}
|
|
v.list = append([]terminalViewer{viewer}, v.list...)
|
|
return &v.list[0]
|
|
}
|
|
|
|
// attach records client as drawing the terminal (it opened or read it): the
|
|
// active one when no client is.
|
|
func (v *terminalViewers) attach(client string) {
|
|
if client == "" {
|
|
return
|
|
}
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
if v.indexLocked(client) < 0 {
|
|
v.list = append(v.list, terminalViewer{client: client})
|
|
}
|
|
}
|
|
|
|
// resize records the size client draws the terminal at and reports the
|
|
// size to apply: client's, when it is (or, with activate, becomes) the
|
|
// active client, or when the active client has not reported a size yet.
|
|
// An unknown client (empty) always applies its size, as before clients
|
|
// were told apart.
|
|
func (v *terminalViewers) resize(client string, cols, rows int, activate bool) terminalSize {
|
|
if client == "" {
|
|
return terminalSize{cols, rows, true}
|
|
}
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
i := v.indexLocked(client)
|
|
switch {
|
|
case activate || len(v.list) == 0 || i == 0:
|
|
viewer := v.frontLocked(client)
|
|
viewer.cols, viewer.rows = cols, rows
|
|
return terminalSize{cols, rows, true}
|
|
case i < 0:
|
|
v.list = append(v.list, terminalViewer{client: client, cols: cols, rows: rows})
|
|
default:
|
|
v.list[i].cols, v.list[i].rows = cols, rows
|
|
}
|
|
front := v.list[0]
|
|
return terminalSize{cols, rows, front.cols == 0 || front.rows == 0}
|
|
}
|
|
|
|
// typedAt is typed from a client drawing the terminal at cols x rows
|
|
// (zero when it did not say): it becomes the active client at that size.
|
|
func (v *terminalViewers) typedAt(client string, cols, rows int) terminalSize {
|
|
if client != "" && cols > 0 && rows > 0 {
|
|
return v.resize(client, cols, rows, true)
|
|
}
|
|
return v.typed(client)
|
|
}
|
|
|
|
// typed makes client, which typed into the terminal, the active client and
|
|
// reports its last size when that changes who is active.
|
|
func (v *terminalViewers) typed(client string) terminalSize {
|
|
if client == "" {
|
|
return terminalSize{}
|
|
}
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
if len(v.list) > 0 && v.list[0].client == client {
|
|
return terminalSize{}
|
|
}
|
|
viewer := v.frontLocked(client)
|
|
return terminalSize{viewer.cols, viewer.rows, viewer.cols > 0 && viewer.rows > 0}
|
|
}
|
|
|
|
// answers reports whether client's emulator's automatic answers reach the
|
|
// process: only the active client's do, or anyone's while no client is
|
|
// known.
|
|
func (v *terminalViewers) answers(client string) bool {
|
|
if client == "" {
|
|
return true
|
|
}
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
return len(v.list) == 0 || v.list[0].client == client
|
|
}
|
|
|
|
// leave forgets client (its connection closed) and, when it was the active
|
|
// client, reports the size of the next one, which becomes active.
|
|
func (v *terminalViewers) leave(client string) terminalSize {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
i := v.indexLocked(client)
|
|
if i < 0 {
|
|
return terminalSize{}
|
|
}
|
|
v.list = append(v.list[:i], v.list[i+1:]...)
|
|
if i != 0 || len(v.list) == 0 {
|
|
return terminalSize{}
|
|
}
|
|
next := v.list[0]
|
|
return terminalSize{next.cols, next.rows, next.cols > 0 && next.rows > 0}
|
|
}
|
|
|
|
// active is the active client, or empty (tests).
|
|
func (v *terminalViewers) active() string {
|
|
v.mu.Lock()
|
|
defer v.mu.Unlock()
|
|
if len(v.list) == 0 {
|
|
return ""
|
|
}
|
|
return v.list[0].client
|
|
}
|