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

76 lines
2.3 KiB
Go

package server
import (
"context"
"sync"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// connectionQueue bounds the requests read ahead of their handling on one
// connection, per lane. The app sends one ordinary request at a time, so
// the ordinary lane rarely holds more than a few.
const connectionQueue = 256
// isTerminalLaneRequest reports the requests a connection handles on its
// terminal lane: what a terminal's keystrokes and size become. They are
// quick (a queued write, an ioctl) and ordered among themselves, and must
// never wait behind an ordinary request read before them on the same
// connection (an elevation's password check, a chat backend call).
func isTerminalLaneRequest(kind string) bool {
switch kind {
case protocol.TerminalInput, protocol.TerminalResize, protocol.ChatShellInput, protocol.ChatShellResize:
return true
}
return false
}
// readConnectionRequests reads conn's requests until it fails or ctx ends.
// Terminal-lane requests are handled in arrival order by lane on a goroutine
// of their own; the others are returned, in arrival order, for the
// connection's ordinary loop, and the channel closes when reading stops.
// Order is kept within each lane, not across them: a client that needs a
// terminal request to follow an ordinary one (input after terminal_open)
// waits for its reply first. stop ends the reading and waits for the lane
// to finish what it took.
func readConnectionRequests(ctx context.Context, conn *websocket.Conn, lane func(context.Context, protocol.Message)) (requests <-chan protocol.Message, stop func()) {
ctx, cancel := context.WithCancel(ctx)
ordinary := make(chan protocol.Message, connectionQueue)
terminal := make(chan protocol.Message, connectionQueue)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for request := range terminal {
lane(ctx, request)
}
}()
go func() {
defer wg.Done()
defer close(ordinary)
defer close(terminal)
for {
var request protocol.Message
if err := wsjson.Read(ctx, conn, &request); err != nil {
return
}
queue := ordinary
if isTerminalLaneRequest(request.Type) {
queue = terminal
}
select {
case queue <- request:
case <-ctx.Done():
return
}
}
}()
return ordinary, func() {
cancel()
wg.Wait()
}
}