Add persistent machine terminals with tmux-backed reattach

This commit is contained in:
2026-11-09 21:30:15 +00:00
parent 8f06f12d49
commit 277384c864
16 changed files with 1757 additions and 1 deletions
+36 -1
View File
@@ -66,6 +66,12 @@ type Options struct {
// False keeps the previous behaviour (tests, embedded servers): a stopping
// server interrupts its runs and the next start marks them interrupted.
DetachSessions bool
// TerminalTmuxSocket names the Pipelines-owned tmux server machine
// terminals live in (empty: "pipelines"); TerminalWithoutTmux runs
// shells as direct children even where tmux is installed. Tests set
// them so they never share the real server's tmux sessions.
TerminalTmuxSocket string
TerminalWithoutTmux bool
}
// Handler serves the persistent connection with default options. Canceling ctx
@@ -143,6 +149,20 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
} else if store, ok := registry.(chatShellRunStore); ok {
hub.startShellRunner(ctx, store)
}
// Machine terminals are recorded beside the operator sessions, in the
// same store; a previous process's tmux-backed shells are reattached.
var terminalRecords terminalStore
if len(stores) > 0 {
terminalRecords = stores[0]
} else if store, ok := registry.(terminalStore); ok {
terminalRecords = store
}
var terminalTmuxServer *terminalTmux
if !options.TerminalWithoutTmux {
terminalTmuxServer = findTerminalTmux(options.TerminalTmuxSocket)
}
terminals := newTerminalManager(ctx, terminalRecords, terminalTmuxServer)
terminals.recover(ctx)
acks := newAttentionAckHub()
var ackStore attentionAckStore
var notes *appNotifier
@@ -237,6 +257,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
// connection the same way.
hub.sessionChanges.attach(clientID, write)
defer hub.sessionChanges.detach(clientID)
// 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)
if notes != nil {
defer notes.detach(clientID)
}
@@ -250,7 +274,8 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if err := wsjson.Read(connCtx, conn, &request); err != nil {
return
}
if request.Type != protocol.ChatShellInput && request.Type != protocol.ChatShellResize {
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)
}
@@ -291,6 +316,16 @@ 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 {
go handleTerminalRequest(connCtx, request, terminals, clientID, write)
} else {
handleTerminalRequest(connCtx, request, terminals, clientID, write)
}
continue
}
if handleDirectoryListing(request, write) {
continue
}