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
+28 -18
View File
@@ -33,8 +33,9 @@ const (
)
// chatShellRunner owns the operator's own commands: each runs `$SHELL -lc` in
// its chat's directory, in its own process group, with stdout and stderr
// merged into one bounded tail that is streamed to the session's watchers and
// its chat's directory, in its own process group, in a pseudo-terminal where
// the platform has one (else with stdout and stderr on one pipe), its output
// kept as one bounded tail that is streamed to the session's watchers and
// persisted as Pipelines' record of the run.
type chatShellRunner struct {
base context.Context
@@ -60,6 +61,13 @@ type chatShellProcess struct {
cmd *exec.Cmd
stopped bool
done chan struct{}
// terminal is the pseudo-terminal's controller side (nil in pipe mode);
// input feeds it in order (chat_shell_terminal.go).
terminal *os.File
input chan []byte
// closed is set once the reader is closed; a read still blocked on a
// terminal a descendant holds open records nothing after it.
closed bool
}
func newChatShellRunner(base context.Context, store chatShellRunStore, publish func(string, protocol.ChatShellRunEventFrame)) *chatShellRunner {
@@ -117,7 +125,8 @@ func newChatShellRunID() (string, error) {
// Start launches command for sessionID in directory and returns its running
// record. Validation and start failures are returned without a record.
func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, command string) (protocol.ChatShellRunRecord, error) {
// cols and rows size its terminal (zero picks the defaults).
func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, command string, cols, rows int) (protocol.ChatShellRunRecord, error) {
if r.store == nil {
return protocol.ChatShellRunRecord{}, errors.New("shell commands need the server's store, which is not configured")
}
@@ -149,23 +158,18 @@ func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, comma
if running >= protocol.ChatShellRunConcurrent {
return protocol.ChatShellRunRecord{}, fmt.Errorf("this chat already runs %d commands; stop one or wait for it to finish", running)
}
reader, writer, err := os.Pipe()
if err != nil {
return protocol.ChatShellRunRecord{}, err
}
cmd := exec.Command(r.shell(), "-lc", command)
cmd.Dir = dir
cmd.Env = os.Environ()
cmd.Stdout, cmd.Stderr = writer, writer
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := cmd.Start(); err != nil {
reader.Close()
writer.Close()
cols, rows = chatShellTerminalSize(cols, rows)
reader, terminal, err := startChatShellCommand(cmd, cols, rows)
if err != nil {
return protocol.ChatShellRunRecord{}, fmt.Errorf("could not start the command: %v", err)
}
writer.Close()
record := protocol.ChatShellRunRecord{ID: id, SessionID: sessionID, Command: command, Directory: dir, Status: protocol.ChatShellRunRunning, StartedAt: r.now().UnixMilli()}
p := &chatShellProcess{record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, done: make(chan struct{})}
if terminal != nil {
record.Terminal, record.Cols, record.Rows = true, cols, rows
}
p := &chatShellProcess{record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, terminal: terminal, done: make(chan struct{})}
if err := r.store.SaveChatShellRun(ctx, record, protocol.ChatShellRunHistory); err != nil {
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
reader.Close()
@@ -175,6 +179,10 @@ func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, comma
r.active[id] = p
r.wg.Add(1)
go r.supervise(p, reader)
if terminal != nil {
p.input = make(chan []byte, protocol.ChatShellInputQueue)
go p.pumpInput()
}
r.publishRun(record)
return record, nil
}
@@ -216,14 +224,16 @@ func (r *chatShellRunner) activeProcess(sessionID, runID string) *chatShellProce
return nil
}
// snapshot is the process's current record carrying at most max output bytes.
// snapshot is the process's current record carrying at most max output bytes
// and their plain-text rendering.
func (p *chatShellProcess) snapshot(max int) protocol.ChatShellRunRecord {
p.mu.Lock()
defer p.mu.Unlock()
record := p.record
record.Output, record.OutputStart = p.out.tail(max)
record.OutputBytes = p.out.total
record.Truncated = record.OutputStart > 0
p.mu.Unlock()
record.Text = chatShellPlainText(record.Output)
return record
}
@@ -231,7 +241,7 @@ func (r *chatShellRunner) publishRun(record protocol.ChatShellRunRecord) {
if r.publish == nil {
return
}
record.Output = ""
record.Output, record.Text = "", ""
r.publish(record.SessionID, protocol.ChatShellRunEventFrame{SessionID: record.SessionID, RunID: record.ID, Run: &record})
}