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>
This commit is contained in:
2026-11-25 23:37:16 +00:00
parent 5ab1ec9e00
commit c3458d8a7f
28 changed files with 1229 additions and 87 deletions
+21 -11
View File
@@ -267,7 +267,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
// Terminal lifecycle changes reach every connection; output only the
// connections that opened or read that terminal.
terminals.clients.attach(clientID, write)
defer terminals.clients.detach(clientID)
defer terminals.detach(clientID)
// A closed connection hands the chats' terminal commands it was
// active in to their next client.
defer func() { hub.shellRunner().leave(clientID) }()
if notes != nil {
defer notes.detach(clientID)
}
@@ -276,16 +279,23 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
// previous, whose reply the client would discard as stale anyway.
cancelChatSearch := func() {}
defer func() { cancelChatSearch() }()
for {
var request protocol.Message
if err := wsjson.Read(connCtx, conn, &request); err != nil {
// Keystrokes and sizes take their own lane (terminal_lane.go), so
// they never wait behind a slow request read before them. The lane
// is drained before the deferred detaches above run.
requests, stopReading := readConnectionRequests(connCtx, conn, func(laneCtx context.Context, request protocol.Message) {
if elevation.refuse(request, elevationKey, write) {
return
}
if request.Type != protocol.ChatShellInput && request.Type != protocol.ChatShellResize &&
request.Type != protocol.TerminalInput && request.Type != protocol.TerminalResize {
// Keystrokes would be one line each; their data is never logged.
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
if isTerminalRequest(request.Type) {
handleTerminalRequest(laneCtx, request, terminals, clientID, write)
return
}
handleChat(laneCtx, request, options.Chat, hub, registry, clientID, write)
})
defer stopReading()
for request := range requests {
// Keystrokes take the terminal lane and are never logged.
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
if request.Type == protocol.Submit && !options.AllowAuthoring {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"})
continue
@@ -327,9 +337,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
continue
}
if isTerminalRequest(request.Type) {
// Closing waits for the shell to go; it never delays input
// to the other terminals behind it.
if request.Type == protocol.TerminalClose {
// Opening waits up to the login's prompt wait and closing for
// the shell to go; neither delays the requests behind it.
if request.Type == protocol.TerminalClose || request.Type == protocol.TerminalOpen {
go handleTerminalRequest(connCtx, request, terminals, clientID, write)
} else {
handleTerminalRequest(connCtx, request, terminals, clientID, write)