Run chat shell commands in a pseudo-terminal with typed input and resize

Commands start in a PTY (creack/pty; macOS and Linux) of the client's size
with TERM=xterm-256color in their own session. chat_shell_input writes typed
UTF-8 text in order through a bounded queue; chat_shell_resize sets the size.
Output stays the raw terminal stream with byte offsets as sequence numbers,
chat_shell_run_output resumes from an offset, and a plain-text rendering is
persisted for Copy output and Send to agent. Input is never stored or logged.
This commit is contained in:
2026-11-09 19:57:46 +00:00
parent 8ca5a1fdda
commit 355b722a1d
20 changed files with 865 additions and 54 deletions
+15 -4
View File
@@ -11,7 +11,7 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// supervise reads one command's merged output, streams it in batches, saves
// supervise reads one command's merged output (or terminal stream), streams it in batches, saves
// it periodically and records how the command ended. It owns the reader.
func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
defer r.wg.Done()
@@ -24,7 +24,9 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
n, err := reader.Read(buffer)
if n > 0 {
p.mu.Lock()
p.out.write(buffer[:n])
if !p.closed {
p.out.write(buffer[:n])
}
p.mu.Unlock()
}
if err != nil {
@@ -39,6 +41,7 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
defer flush.Stop()
lastSaved := r.now()
var waitErr error
var drainDone <-chan time.Time
exited, drained, shutdown := false, false, false
base := r.base.Done()
for !exited || !drained {
@@ -51,9 +54,14 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
}
case waitErr = <-waitDone:
exited = true
// Descendants may hold the pipe open; read what they write
// briefly, then stop reading.
// Descendants may hold the pipe or terminal open; read what
// they write briefly, then stop reading. A terminal may not
// support deadlines, so the timer also ends the wait.
_ = reader.SetReadDeadline(time.Now().Add(chatShellDrain))
drainDone = time.After(chatShellDrain + chatShellDrain/2)
case <-drainDone:
drained = true
readDone = nil
case <-readDone:
drained = true
readDone = nil
@@ -65,7 +73,10 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
}
// Nothing in the command's group outlives it.
_ = syscall.Kill(-pid, syscall.SIGKILL)
p.mu.Lock()
reader.Close()
p.closed = true
p.mu.Unlock()
r.emitOutput(p, true)
p.mu.Lock()
finishChatShellRecord(&p.record, waitErr, p.stopped, shutdown || r.base.Err() != nil, r.now())