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:
@@ -30,6 +30,7 @@ require (
|
||||
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
|
||||
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
|
||||
github.com/charmbracelet/x/term v0.2.1 // indirect
|
||||
github.com/creack/pty v1.1.24 // indirect
|
||||
github.com/dlclark/regexp2 v1.11.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.11.2 // indirect
|
||||
|
||||
@@ -36,6 +36,8 @@ github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQ
|
||||
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
||||
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
||||
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
||||
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
|
||||
@@ -10,7 +10,9 @@ import (
|
||||
|
||||
// initChatShellRuns creates the additive table of operator shell commands. A
|
||||
// run belongs to its chat session and is Pipelines' own record: it is never
|
||||
// part of the harness transcript. Output holds only the retained tail.
|
||||
// part of the harness transcript. Output holds only the retained tail (the raw
|
||||
// terminal stream of a terminal run); output_text is its plain-text rendering.
|
||||
// Typed input is never stored.
|
||||
func (s *Store) initChatShellRuns() error {
|
||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_shell_runs (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -28,7 +30,20 @@ output TEXT NOT NULL DEFAULT '',
|
||||
output_start INTEGER NOT NULL DEFAULT 0,
|
||||
output_bytes INTEGER NOT NULL DEFAULT 0);
|
||||
CREATE INDEX IF NOT EXISTS chat_shell_runs_session ON chat_shell_runs(session_id, started_at);`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, column := range [][2]string{
|
||||
{"terminal", "INTEGER NOT NULL DEFAULT 0"},
|
||||
{"cols", "INTEGER NOT NULL DEFAULT 0"},
|
||||
{"rows", "INTEGER NOT NULL DEFAULT 0"},
|
||||
{"output_text", "TEXT NOT NULL DEFAULT ''"},
|
||||
} {
|
||||
if err := s.addColumnIfMissing("chat_shell_runs", column[0], column[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveChatShellRun inserts or replaces one run. Inserting a new run prunes the
|
||||
@@ -44,13 +59,16 @@ func (s *Store) SaveChatShellRun(ctx context.Context, run protocol.ChatShellRunR
|
||||
exit = *run.ExitCode
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_shell_runs
|
||||
(id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
(id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes,
|
||||
terminal, cols, rows, output_text)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET status = excluded.status, exit_code = excluded.exit_code, signal = excluded.signal,
|
||||
error = excluded.error, ended_at = excluded.ended_at, duration_ms = excluded.duration_ms, output = excluded.output,
|
||||
output_start = excluded.output_start, output_bytes = excluded.output_bytes`,
|
||||
output_start = excluded.output_start, output_bytes = excluded.output_bytes, terminal = excluded.terminal,
|
||||
cols = excluded.cols, rows = excluded.rows, output_text = excluded.output_text`,
|
||||
run.ID, run.SessionID, run.Command, run.Directory, run.Status, exit, run.Signal, run.Error,
|
||||
run.StartedAt, run.EndedAt, run.DurationMS, run.Output, run.OutputStart, run.OutputBytes); err != nil {
|
||||
run.StartedAt, run.EndedAt, run.DurationMS, run.Output, run.OutputStart, run.OutputBytes,
|
||||
run.Terminal, run.Cols, run.Rows, run.Text); err != nil {
|
||||
return err
|
||||
}
|
||||
if keep > 0 {
|
||||
@@ -104,13 +122,14 @@ duration_ms = CASE WHEN ? > started_at THEN ? - started_at ELSE 0 END WHERE stat
|
||||
return result.RowsAffected()
|
||||
}
|
||||
|
||||
const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes FROM chat_shell_runs`
|
||||
const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes, terminal, cols, rows, output_text FROM chat_shell_runs`
|
||||
|
||||
func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) {
|
||||
var run protocol.ChatShellRunRecord
|
||||
var exit sql.NullInt64
|
||||
err := row.Scan(&run.ID, &run.SessionID, &run.Command, &run.Directory, &run.Status, &exit, &run.Signal, &run.Error,
|
||||
&run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes)
|
||||
&run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes,
|
||||
&run.Terminal, &run.Cols, &run.Rows, &run.Text)
|
||||
if err != nil {
|
||||
return protocol.ChatShellRunRecord{}, err
|
||||
}
|
||||
|
||||
@@ -18,14 +18,18 @@ const (
|
||||
// body is ChatQuery.
|
||||
ChatShellRuns = "chat_shell_runs"
|
||||
// ChatShellRunOutput reads one run with its whole retained output (at most
|
||||
// ChatShellRunRetained bytes). The body is ChatShellRunRequest.
|
||||
// ChatShellRunRetained bytes) and its plain-text rendering. The body is
|
||||
// ChatShellRunRequest; a nonzero After asks only for the output from that
|
||||
// absolute byte on (a reconnecting client resuming its copy).
|
||||
ChatShellRunOutput = "chat_shell_run_output"
|
||||
// ChatShellShare queues an ordinary prompt carrying the run's command, exit
|
||||
// status and bounded output tail. The body is ChatShellRunRequest; the
|
||||
// reply is the chat_prompt acknowledgement.
|
||||
ChatShellShare = "chat_shell_share"
|
||||
// ChatShellRunEvent is pushed to every client watching the session: a
|
||||
// lifecycle change (Run set) or an output chunk (Output set).
|
||||
// lifecycle change (Run set) or an output chunk (Output set). Output
|
||||
// chunks of one run are pushed in order; their byte offsets are the
|
||||
// stream's sequence numbers.
|
||||
ChatShellRunEvent = "chat_shell_run_event"
|
||||
)
|
||||
|
||||
@@ -60,11 +64,18 @@ const (
|
||||
|
||||
// ChatShellRunRequest names one session and either a new command or a run.
|
||||
// OperationID is the optional retry identity of chat_shell_share's prompt.
|
||||
// Cols and Rows size a new run's terminal (chat_shell_run) or resize a running
|
||||
// one (chat_shell_resize); Data is chat_shell_input's typed text; After is
|
||||
// chat_shell_run_output's resume offset.
|
||||
type ChatShellRunRequest struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Command string `json:"command,omitempty"`
|
||||
RunID string `json:"run_id,omitempty"`
|
||||
OperationID string `json:"operation_id,omitempty"`
|
||||
Cols int `json:"cols,omitempty"`
|
||||
Rows int `json:"rows,omitempty"`
|
||||
Data string `json:"data,omitempty"`
|
||||
After int64 `json:"after,omitempty"`
|
||||
}
|
||||
|
||||
// ChatShellRunRecord is one operator command and what it produced. Output is
|
||||
@@ -91,6 +102,16 @@ type ChatShellRunRecord struct {
|
||||
// Truncated is true when Output does not hold everything the command
|
||||
// wrote (OutputStart > 0).
|
||||
Truncated bool `json:"truncated,omitempty"`
|
||||
// Terminal is true when the command runs in a pseudo-terminal of Cols x
|
||||
// Rows: Output is then the raw terminal stream, and input is accepted
|
||||
// while it runs.
|
||||
Terminal bool `json:"terminal,omitempty"`
|
||||
Cols int `json:"cols,omitempty"`
|
||||
Rows int `json:"rows,omitempty"`
|
||||
// Text is the plain-text rendering of Output (escape sequences removed,
|
||||
// carriage-return redraws applied): what Copy output copies and Send to
|
||||
// agent shares. Only chat_shell_run_output carries it.
|
||||
Text string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
// ChatShellRunEventFrame is one live change to a run. A lifecycle frame sets
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package protocol
|
||||
|
||||
// Interactive operator shell commands. On macOS and Linux every chat_shell_run
|
||||
// runs inside a pseudo-terminal (ChatShellRunRecord.Terminal): the command sees
|
||||
// a TTY of the requested size with TERM=xterm-256color, its output is the raw
|
||||
// terminal stream (escape sequences included) and any authenticated client
|
||||
// watching the chat may type into it until it ends. Other platforms keep
|
||||
// plain pipes and refuse input.
|
||||
const (
|
||||
// ChatShellInput writes Data to a running terminal command's input. The
|
||||
// body is ChatShellRunRequest (session_id, run_id, data). Data is the
|
||||
// UTF-8 text of what was typed or pasted, control characters included
|
||||
// (Ctrl-C is "\u0003", Enter is "\r", arrow keys are their escape
|
||||
// sequences); the bytes written are exactly its UTF-8 encoding. Input is
|
||||
// never persisted or logged; the reply carries no body.
|
||||
ChatShellInput = "chat_shell_input"
|
||||
// ChatShellResize sets a running terminal command's size (cols, rows); the
|
||||
// process receives SIGWINCH. The last resize from any client wins. The
|
||||
// reply echoes the run.
|
||||
ChatShellResize = "chat_shell_resize"
|
||||
)
|
||||
|
||||
// Bounds of interactive terminals.
|
||||
const (
|
||||
// ChatShellInputLimit bounds the UTF-8 bytes of one chat_shell_input.
|
||||
// Clients split a longer paste into several requests.
|
||||
ChatShellInputLimit = 16 << 10
|
||||
// ChatShellInputQueue bounds the input requests waiting for the command
|
||||
// to read them; beyond it chat_shell_input is refused.
|
||||
ChatShellInputQueue = 64
|
||||
// Terminal sizes are clamped to these bounds; a run started without a
|
||||
// size gets the defaults.
|
||||
ChatShellTerminalMinCols = 20
|
||||
ChatShellTerminalMaxCols = 500
|
||||
ChatShellTerminalMinRows = 4
|
||||
ChatShellTerminalMaxRows = 200
|
||||
ChatShellTerminalDefaultCols = 100
|
||||
ChatShellTerminalDefaultRows = 30
|
||||
)
|
||||
|
||||
// ChatShellTerminalUnsupported is the refusal of chat_shell_input and
|
||||
// chat_shell_resize on a server or run without a pseudo-terminal.
|
||||
const ChatShellTerminalUnsupported = "interactive terminals are not supported on this server"
|
||||
@@ -255,7 +255,8 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatShellOutput:
|
||||
handleChatShellOutput(ctx, request, broker, registry, write)
|
||||
return true
|
||||
case protocol.ChatShellRun, protocol.ChatShellStop, protocol.ChatShellRuns, protocol.ChatShellRunOutput, protocol.ChatShellShare:
|
||||
case protocol.ChatShellRun, protocol.ChatShellStop, protocol.ChatShellRuns, protocol.ChatShellRunOutput, protocol.ChatShellShare,
|
||||
protocol.ChatShellInput, protocol.ChatShellResize:
|
||||
handleChatShellRunRequest(ctx, request, broker, hub, registry, clientID, write)
|
||||
return true
|
||||
case protocol.ChatAttachmentImage:
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatShellPlainText renders a raw terminal stream as plain text, as Copy
|
||||
// output and Send to agent show it: escape sequences (colours, cursor moves,
|
||||
// titles, hyperlinks) are removed, a carriage return or backspace redraws the
|
||||
// current line the way a terminal would, and the line erase is applied. It is
|
||||
// not a terminal emulator: full-screen programs render as their final lines.
|
||||
// The result is bounded by protocol.ChatShellRunRetained bytes (its tail).
|
||||
func chatShellPlainText(raw string) string {
|
||||
var r chatShellPlainRenderer
|
||||
for _, c := range raw {
|
||||
r.feed(c)
|
||||
}
|
||||
r.out.WriteString(strings.TrimRight(string(r.line), " "))
|
||||
text := r.out.String()
|
||||
if len(text) > protocol.ChatShellRunRetained {
|
||||
cut := runeStartAfter([]byte(text), len(text)-protocol.ChatShellRunRetained)
|
||||
text = text[cut:]
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
type chatShellPlainState int
|
||||
|
||||
const (
|
||||
plainText chatShellPlainState = iota
|
||||
plainEscape
|
||||
plainEscapeIntermediate
|
||||
plainCSI
|
||||
// plainString is an OSC, DCS, SOS, PM or APC string, ended by BEL or ST.
|
||||
plainString
|
||||
plainStringEscape
|
||||
)
|
||||
|
||||
type chatShellPlainRenderer struct {
|
||||
out strings.Builder
|
||||
line []rune
|
||||
col int
|
||||
state chatShellPlainState
|
||||
params []rune
|
||||
}
|
||||
|
||||
func (r *chatShellPlainRenderer) feed(c rune) {
|
||||
switch r.state {
|
||||
case plainEscape:
|
||||
r.state = plainText
|
||||
switch {
|
||||
case c == '[':
|
||||
r.state, r.params = plainCSI, r.params[:0]
|
||||
case c == ']' || c == 'P' || c == 'X' || c == '^' || c == '_':
|
||||
r.state = plainString
|
||||
case c >= 0x20 && c <= 0x2f:
|
||||
r.state = plainEscapeIntermediate
|
||||
}
|
||||
case plainEscapeIntermediate:
|
||||
if c < 0x20 || c > 0x2f {
|
||||
r.state = plainText
|
||||
}
|
||||
case plainCSI:
|
||||
switch {
|
||||
case c >= 0x40 && c <= 0x7e:
|
||||
r.state = plainText
|
||||
r.csi(c)
|
||||
case c >= 0x20 && c <= 0x3f:
|
||||
if len(r.params) < 32 {
|
||||
r.params = append(r.params, c)
|
||||
}
|
||||
default:
|
||||
r.state = plainText
|
||||
}
|
||||
case plainString:
|
||||
switch c {
|
||||
case 0x07:
|
||||
r.state = plainText
|
||||
case 0x1b:
|
||||
r.state = plainStringEscape
|
||||
}
|
||||
case plainStringEscape:
|
||||
r.state = plainText
|
||||
default:
|
||||
r.text(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *chatShellPlainRenderer) text(c rune) {
|
||||
switch {
|
||||
case c == 0x1b:
|
||||
r.state = plainEscape
|
||||
case c == 0x9b:
|
||||
r.state, r.params = plainCSI, r.params[:0]
|
||||
case c == '\n':
|
||||
r.out.WriteString(strings.TrimRight(string(r.line), " "))
|
||||
r.out.WriteByte('\n')
|
||||
r.line, r.col = r.line[:0], 0
|
||||
case c == '\r':
|
||||
r.col = 0
|
||||
case c == '\b':
|
||||
r.col = max(r.col-1, 0)
|
||||
case c == '\t':
|
||||
r.put('\t')
|
||||
case c < 0x20 || (c >= 0x7f && c < 0xa0):
|
||||
// Other control characters draw nothing.
|
||||
default:
|
||||
r.put(c)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *chatShellPlainRenderer) put(c rune) {
|
||||
for len(r.line) < r.col {
|
||||
r.line = append(r.line, ' ')
|
||||
}
|
||||
if r.col < len(r.line) {
|
||||
r.line[r.col] = c
|
||||
} else {
|
||||
r.line = append(r.line, c)
|
||||
}
|
||||
r.col++
|
||||
}
|
||||
|
||||
// csi applies the few control sequences that change a line's text.
|
||||
func (r *chatShellPlainRenderer) csi(final rune) {
|
||||
params := string(r.params)
|
||||
n, err := strconv.Atoi(params)
|
||||
if err != nil || n < 1 {
|
||||
n = 1
|
||||
}
|
||||
switch final {
|
||||
case 'K':
|
||||
switch params {
|
||||
case "", "0":
|
||||
r.line = r.line[:min(r.col, len(r.line))]
|
||||
case "2":
|
||||
r.line = r.line[:0]
|
||||
}
|
||||
case 'G':
|
||||
r.col = min(n-1, protocol.ChatShellTerminalMaxCols)
|
||||
case 'C':
|
||||
r.col = min(r.col+n, protocol.ChatShellTerminalMaxCols)
|
||||
case 'D':
|
||||
r.col = max(r.col-n, 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestChatShellPlainTextRendersTerminalStream(t *testing.T) {
|
||||
for _, tc := range []struct{ raw, want string }{
|
||||
{"plain\nlines\n", "plain\nlines\n"},
|
||||
{"\x1b[1;32mok\x1b[0m done\r\n", "ok done\n"},
|
||||
{"10%\r50%\r100%\r\n", "100%\n"},
|
||||
{"long line\rab\x1b[K\r\n", "ab\n"},
|
||||
{"abc\b\bX\n", "aXc\n"},
|
||||
{"\x1b]0;window title\x07shown\n", "shown\n"},
|
||||
{"\x1b]8;;https://x.test\x1b\\link\x1b]8;;\x1b\\\n", "link\n"},
|
||||
{"\x1b(Bcharset\x1b=\n", "charset\n"},
|
||||
{"col\x1b[10Gx\n", "col x\n"},
|
||||
{"bell\x07 and nul\x00\n", "bell and nul\n"},
|
||||
{"\x1b[2Kgone\r\x1b[2Kkept", "kept"},
|
||||
{"unterminated \x1b[31", "unterminated"},
|
||||
} {
|
||||
if got := chatShellPlainText(tc.raw); got != tc.want {
|
||||
t.Errorf("plain(%q) = %q, want %q", tc.raw, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellPlainTextIsBounded(t *testing.T) {
|
||||
raw := strings.Repeat("\x1b[500Gx\n", protocol.ChatShellRunRetained/64)
|
||||
if got := chatShellPlainText(raw); len(got) > protocol.ChatShellRunRetained || !strings.HasSuffix(got, "x\n") {
|
||||
t.Fatalf("rendered %d bytes", len(got))
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ import (
|
||||
// handleChatShellRunRequest answers the operator shell-command requests. Every
|
||||
// one names an operator chat session the registry records: the command runs
|
||||
// only in that chat's own directory on this server's machine, and a run is
|
||||
// only readable or stoppable through the session that started it.
|
||||
// only readable, stoppable or typed into through the session that started it.
|
||||
// Any authenticated client of that session may type into a running command,
|
||||
// not only the one that started it.
|
||||
func handleChatShellRunRequest(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
|
||||
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
|
||||
runner := hub.shellRunner()
|
||||
@@ -49,7 +51,7 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br
|
||||
}
|
||||
switch request.Type {
|
||||
case protocol.ChatShellRun:
|
||||
record, err := runner.Start(ctx, sessionID, session.Directory, body.Command)
|
||||
record, err := runner.Start(ctx, sessionID, session.Directory, body.Command, body.Cols, body.Rows)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
@@ -69,12 +71,26 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs})
|
||||
case protocol.ChatShellInput:
|
||||
if err := runner.Input(sessionID, body.RunID, body.Data); err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellInput, ID: request.ID})
|
||||
case protocol.ChatShellResize:
|
||||
record, err := runner.Resize(sessionID, body.RunID, body.Cols, body.Rows)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellResize, ID: request.ID, ChatShellRun: &record})
|
||||
case protocol.ChatShellRunOutput:
|
||||
record, err := runner.Read(ctx, sessionID, body.RunID)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
return
|
||||
}
|
||||
record = chatShellRecordAfter(record, body.After)
|
||||
_ = write(protocol.Message{Type: protocol.ChatShellRunOutput, ID: request.ID, ChatShellRun: &record})
|
||||
case protocol.ChatShellShare:
|
||||
record, err := runner.Read(ctx, sessionID, body.RunID)
|
||||
@@ -104,10 +120,11 @@ func (r *chatShellRunner) List(ctx context.Context, sessionID string) ([]protoco
|
||||
for i, run := range runs {
|
||||
if p := r.activeProcess(sessionID, run.ID); p != nil {
|
||||
runs[i] = p.snapshot(protocol.ChatShellRunListOutput)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput)
|
||||
}
|
||||
runs[i].Text = ""
|
||||
}
|
||||
return runs, nil
|
||||
}
|
||||
|
||||
@@ -123,9 +140,27 @@ func (r *chatShellRunner) Read(ctx context.Context, sessionID, runID string) (pr
|
||||
if !found {
|
||||
return protocol.ChatShellRunRecord{}, errors.New("this chat has no such command")
|
||||
}
|
||||
if record.Text == "" && record.Output != "" {
|
||||
// A run recorded before plain-text renderings were stored.
|
||||
record.Text = chatShellPlainText(record.Output)
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// chatShellRecordAfter keeps only the output from absolute byte after on; an
|
||||
// offset before the retained tail keeps all of it (OutputStart says where it
|
||||
// starts, so the reader sees the gap).
|
||||
func chatShellRecordAfter(record protocol.ChatShellRunRecord, after int64) protocol.ChatShellRunRecord {
|
||||
if after <= record.OutputStart {
|
||||
return record
|
||||
}
|
||||
skip := min(after-record.OutputStart, int64(len(record.Output)))
|
||||
cut := runeStartAfter([]byte(record.Output), int(skip))
|
||||
record.Output = record.Output[cut:]
|
||||
record.OutputStart += int64(cut)
|
||||
return record
|
||||
}
|
||||
|
||||
// chatShellRecordTail trims a stored record's output to its last max bytes.
|
||||
func chatShellRecordTail(record protocol.ChatShellRunRecord, max int) protocol.ChatShellRunRecord {
|
||||
if len(record.Output) <= max {
|
||||
|
||||
@@ -99,13 +99,13 @@ func TestChatShellRunStreamsToEveryWatcherAndPersists(t *testing.T) {
|
||||
if final.Status != protocol.ChatShellRunExited || final.ExitCode == nil || *final.ExitCode != 3 || final.EndedAt < final.StartedAt {
|
||||
t.Fatalf("final record = %+v", final)
|
||||
}
|
||||
if output != dir+"\nout\nerr\n" {
|
||||
t.Fatalf("streamed output = %q", output)
|
||||
if output != dir+"\r\nout\r\nerr\r\n" || !final.Terminal || final.Cols != protocol.ChatShellTerminalDefaultCols || final.Rows != protocol.ChatShellTerminalDefaultRows {
|
||||
t.Fatalf("streamed output = %q, final %+v", output, final)
|
||||
}
|
||||
}
|
||||
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatShellRuns, ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
|
||||
list := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "list" })
|
||||
if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].Output != dir+"\nout\nerr\n" || *list.ChatShellRuns[0].ExitCode != 3 {
|
||||
if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].Output != dir+"\r\nout\r\nerr\r\n" || list.ChatShellRuns[0].Text != "" || *list.ChatShellRuns[0].ExitCode != 3 {
|
||||
t.Fatalf("persisted runs = %+v", list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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})
|
||||
}
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ func testShellRunner(t *testing.T, ctx context.Context) (*chatShellRunner, *shel
|
||||
|
||||
func TestChatShellStopKillsAfterGrace(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `trap "" INT; echo ready; while :; do sleep 0.05; done`)
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `trap "" INT; echo ready; while :; do sleep 0.05; done`, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -65,7 +65,7 @@ func TestChatShellStopKillsAfterGrace(t *testing.T) {
|
||||
func TestChatShellRetainsBoundedTail(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
total := protocol.ChatShellRunRetained + 200_000
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `head -c 1248576 /dev/zero | tr '\0' a; printf END`)
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `head -c 1248576 /dev/zero | tr '\0' a; printf END`, 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func TestChatShellRetainsBoundedTail(t *testing.T) {
|
||||
func TestChatShellShutdownAndRestartInterrupt(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
runner, frames := testShellRunner(t, ctx)
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "sleep 30")
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "sleep 30", 0, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func TestChatShellShutdownAndRestartInterrupt(t *testing.T) {
|
||||
if final := frames.final(run.ID); final == nil || final.Status != protocol.ChatShellRunInterrupted {
|
||||
t.Fatalf("shutdown final = %+v", final)
|
||||
}
|
||||
if _, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "true"); err == nil {
|
||||
if _, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "true", 0, 0); err == nil {
|
||||
t.Fatal("a stopping server started a command")
|
||||
}
|
||||
// A record a crashed process left running is interrupted on start.
|
||||
|
||||
@@ -9,27 +9,34 @@ import (
|
||||
)
|
||||
|
||||
// chatShellSharePrompt is the ordinary prompt chat_shell_share queues: the
|
||||
// command, where it ran, how it ended and a bounded tail of its output. It is
|
||||
// the only path by which an operator command reaches the agent.
|
||||
// command, where it ran, how it ended and a bounded tail of the plain-text
|
||||
// rendering of its output (no terminal escape sequences). It is the only path
|
||||
// by which an operator command reaches the agent.
|
||||
func chatShellSharePrompt(record protocol.ChatShellRunRecord) string {
|
||||
output := chatShellRecordTail(record, protocol.ChatShellRunShareOutput)
|
||||
text := record.Text
|
||||
if text == "" {
|
||||
text = chatShellPlainText(record.Output)
|
||||
}
|
||||
shown := text
|
||||
if len(shown) > protocol.ChatShellRunShareOutput {
|
||||
shown = shown[runeStartAfter([]byte(shown), len(shown)-protocol.ChatShellRunShareOutput):]
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "I ran this shell command in %s:\n\n", record.Directory)
|
||||
fence := chatShellFence(record.Command)
|
||||
fmt.Fprintf(&b, "%ssh\n$ %s\n%s\n\n", fence, record.Command, fence)
|
||||
fmt.Fprintf(&b, "%s.\n\n", chatShellOutcome(record))
|
||||
if output.Output == "" {
|
||||
if strings.TrimSpace(shown) == "" {
|
||||
b.WriteString("It printed no output.")
|
||||
return b.String()
|
||||
}
|
||||
if output.OutputStart > 0 {
|
||||
fmt.Fprintf(&b, "Output (the last %s of %s):\n\n", chatShellBytes(int64(len(output.Output))), chatShellBytes(output.OutputBytes))
|
||||
if len(shown) < len(text) || record.OutputStart > 0 {
|
||||
fmt.Fprintf(&b, "Output (the last %s of the %s it wrote, as plain text):\n\n", chatShellBytes(int64(len(shown))), chatShellBytes(record.OutputBytes))
|
||||
} else {
|
||||
b.WriteString("Output:\n\n")
|
||||
}
|
||||
fence = chatShellFence(output.Output)
|
||||
text := strings.TrimRight(output.Output, "\n")
|
||||
fmt.Fprintf(&b, "%stext\n%s\n%s", fence, text, fence)
|
||||
fence = chatShellFence(shown)
|
||||
fmt.Fprintf(&b, "%stext\n%s\n%s", fence, strings.TrimRight(shown, "\n"), fence)
|
||||
return b.String()
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
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())
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unicode/utf8"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// startChatShellCommand starts cmd with the server's environment, in a
|
||||
// pseudo-terminal of cols x rows where the platform has one (TERM is then
|
||||
// xterm-256color) and otherwise in its own process group with stdout and
|
||||
// stderr on one pipe. It returns the output reader and, in a terminal, the
|
||||
// terminal itself (the same file).
|
||||
func startChatShellCommand(cmd *exec.Cmd, cols, rows int) (reader, terminal *os.File, err error) {
|
||||
if chatShellTerminals {
|
||||
cmd.Env = chatShellTerminalEnv(os.Environ())
|
||||
terminal, err = startChatShellTerminal(cmd, cols, rows)
|
||||
return terminal, terminal, err
|
||||
}
|
||||
reader, writer, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Stdout, cmd.Stderr = writer, writer
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
err = cmd.Start()
|
||||
writer.Close()
|
||||
if err != nil {
|
||||
reader.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
return reader, nil, nil
|
||||
}
|
||||
|
||||
// chatShellTerminalEnv is env with TERM naming the terminal the clients draw.
|
||||
func chatShellTerminalEnv(env []string) []string {
|
||||
out := make([]string, 0, len(env)+1)
|
||||
for _, entry := range env {
|
||||
if !strings.HasPrefix(entry, "TERM=") {
|
||||
out = append(out, entry)
|
||||
}
|
||||
}
|
||||
return append(out, "TERM=xterm-256color")
|
||||
}
|
||||
|
||||
// chatShellTerminalSize clamps a requested size; zero picks the default.
|
||||
func chatShellTerminalSize(cols, rows int) (int, int) {
|
||||
if cols <= 0 {
|
||||
cols = protocol.ChatShellTerminalDefaultCols
|
||||
}
|
||||
if rows <= 0 {
|
||||
rows = protocol.ChatShellTerminalDefaultRows
|
||||
}
|
||||
return min(max(cols, protocol.ChatShellTerminalMinCols), protocol.ChatShellTerminalMaxCols),
|
||||
min(max(rows, protocol.ChatShellTerminalMinRows), protocol.ChatShellTerminalMaxRows)
|
||||
}
|
||||
|
||||
// terminalProcess is runID's running terminal command of sessionID.
|
||||
func (r *chatShellRunner) terminalProcess(sessionID, runID string) (*chatShellProcess, error) {
|
||||
p := r.activeProcess(sessionID, runID)
|
||||
if p == nil {
|
||||
return nil, errors.New("this command is not running")
|
||||
}
|
||||
if p.terminal == nil {
|
||||
return nil, errors.New(protocol.ChatShellTerminalUnsupported)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Input queues data for a running terminal command, in arrival order. It is
|
||||
// written as typed and kept nowhere else: what the process echoes is output.
|
||||
func (r *chatShellRunner) Input(sessionID, runID, data string) error {
|
||||
if data == "" {
|
||||
return errors.New("input is required")
|
||||
}
|
||||
if len(data) > protocol.ChatShellInputLimit {
|
||||
return errors.New("the input exceeds 16 KiB; send it in parts")
|
||||
}
|
||||
if !utf8.ValidString(data) {
|
||||
return errors.New("input must be UTF-8 text")
|
||||
}
|
||||
p, err := r.terminalProcess(sessionID, runID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-p.done:
|
||||
return errors.New("this command is not running")
|
||||
case p.input <- []byte(data):
|
||||
return nil
|
||||
default:
|
||||
return errors.New("the command is not reading its input; wait for it or stop it")
|
||||
}
|
||||
}
|
||||
|
||||
// Resize sets a running terminal command's size and returns its record.
|
||||
func (r *chatShellRunner) Resize(sessionID, runID string, cols, rows int) (protocol.ChatShellRunRecord, error) {
|
||||
if cols <= 0 || rows <= 0 {
|
||||
return protocol.ChatShellRunRecord{}, errors.New("positive cols and rows are required")
|
||||
}
|
||||
p, err := r.terminalProcess(sessionID, runID)
|
||||
if err != nil {
|
||||
return protocol.ChatShellRunRecord{}, err
|
||||
}
|
||||
cols, rows = chatShellTerminalSize(cols, rows)
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.closed {
|
||||
return protocol.ChatShellRunRecord{}, errors.New("this command is not running")
|
||||
}
|
||||
if err := resizeChatShellTerminal(p.terminal, cols, rows); err != nil {
|
||||
return protocol.ChatShellRunRecord{}, err
|
||||
}
|
||||
p.record.Cols, p.record.Rows = cols, rows
|
||||
record := p.record
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// pumpInput writes queued input to the terminal until the command ends. A
|
||||
// write blocks while the command does not read, which is what fills the queue.
|
||||
func (p *chatShellProcess) pumpInput() {
|
||||
for {
|
||||
select {
|
||||
case <-p.done:
|
||||
return
|
||||
case data := <-p.input:
|
||||
if _, err := p.terminal.Write(data); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build !(darwin || linux)
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatShellTerminals is false here: operator commands keep plain pipes, and
|
||||
// input and resize are refused with protocol.ChatShellTerminalUnsupported.
|
||||
const chatShellTerminals = false
|
||||
|
||||
func startChatShellTerminal(*exec.Cmd, int, int) (*os.File, error) {
|
||||
return nil, errors.New(protocol.ChatShellTerminalUnsupported)
|
||||
}
|
||||
|
||||
func resizeChatShellTerminal(*os.File, int, int) error {
|
||||
return errors.New(protocol.ChatShellTerminalUnsupported)
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// startTerminal starts command in a real pseudo-terminal of cols x rows.
|
||||
func startTerminal(t *testing.T, runner *chatShellRunner, command string, cols, rows int) protocol.ChatShellRunRecord {
|
||||
t.Helper()
|
||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), command, cols, rows)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !run.Terminal {
|
||||
t.Fatalf("run is not in a terminal: %+v", run)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = runner.Stop("ses_1", run.ID)
|
||||
runner.Wait()
|
||||
})
|
||||
return run
|
||||
}
|
||||
|
||||
// waitOutput waits until the run's retained output contains want.
|
||||
func waitOutput(t *testing.T, runner *chatShellRunner, runID, want string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
record, err := runner.Read(context.Background(), "ses_1", runID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(record.Output, want) {
|
||||
return
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
record, _ := runner.Read(context.Background(), "ses_1", runID)
|
||||
t.Fatalf("output never contained %q: %q", want, record.Output)
|
||||
}
|
||||
|
||||
func finishedTerminal(t *testing.T, runner *chatShellRunner, frames *shellFrames, runID string) protocol.ChatShellRunRecord {
|
||||
t.Helper()
|
||||
runner.Wait()
|
||||
if final := frames.final(runID); final == nil {
|
||||
t.Fatal("no final lifecycle frame")
|
||||
}
|
||||
stored, err := runner.Read(context.Background(), "ses_1", runID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return stored
|
||||
}
|
||||
|
||||
func TestChatShellTerminalReadsTypedLine(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run := startTerminal(t, runner, `[ -t 0 ] && echo tty:$TERM; printf 'name? '; read -r x; echo got:$x`, 0, 0)
|
||||
waitOutput(t, runner, run.ID, "name? ")
|
||||
if err := runner.Input("ses_2", run.ID, "intruder\r"); err == nil {
|
||||
t.Fatal("another session typed into the run")
|
||||
}
|
||||
if err := runner.Input("ses_1", run.ID, "hello world\r"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||
if !strings.Contains(stored.Output, "tty:xterm-256color") || !strings.Contains(stored.Output, "got:hello world") || stored.ExitCode == nil || *stored.ExitCode != 0 {
|
||||
t.Fatalf("stored = %+v", stored)
|
||||
}
|
||||
if err := runner.Input("ses_1", run.ID, "late\r"); err == nil {
|
||||
t.Fatal("input reached a finished run")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellTerminalHiddenInputIsNotRecorded(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run := startTerminal(t, runner, `stty -echo; printf 'Password: '; read -r pw; stty echo; echo; echo length:${#pw}`, 0, 0)
|
||||
waitOutput(t, runner, run.ID, "Password: ")
|
||||
if err := runner.Input("ses_1", run.ID, "s3cret-token\r"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||
if !strings.Contains(stored.Text, "length:12") {
|
||||
t.Fatalf("text = %q", stored.Text)
|
||||
}
|
||||
if strings.Contains(stored.Output, "s3cret") || strings.Contains(stored.Text, "s3cret") {
|
||||
t.Fatalf("hidden input reached the record: %q", stored.Output)
|
||||
}
|
||||
frames.mu.Lock()
|
||||
defer frames.mu.Unlock()
|
||||
for _, frame := range frames.frames {
|
||||
if strings.Contains(frame.Output, "s3cret") {
|
||||
t.Fatalf("hidden input reached a frame: %q", frame.Output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellTerminalControlCInterrupts(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run := startTerminal(t, runner, `echo ready; sleep 30; echo never`, 0, 0)
|
||||
waitOutput(t, runner, run.ID, "ready")
|
||||
started := time.Now()
|
||||
if err := runner.Input("ses_1", run.ID, "\x03"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||
if time.Since(started) > 5*time.Second || strings.Contains(stored.Output, "never") || stored.Status != protocol.ChatShellRunExited {
|
||||
t.Fatalf("after Ctrl-C = %+v", stored)
|
||||
}
|
||||
if stored.Signal != "interrupt" && (stored.ExitCode == nil || *stored.ExitCode != 130) {
|
||||
t.Fatalf("Ctrl-C ended it with %q / %v", stored.Signal, stored.ExitCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellTerminalResizeReachesTheProcess(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run := startTerminal(t, runner, `stty size; echo ready; read -r x; stty size`, 90, 25)
|
||||
if run.Cols != 90 || run.Rows != 25 {
|
||||
t.Fatalf("started at %dx%d", run.Cols, run.Rows)
|
||||
}
|
||||
waitOutput(t, runner, run.ID, "ready")
|
||||
if _, err := runner.Resize("ses_1", run.ID, 0, 10); err == nil {
|
||||
t.Fatal("a zero size was accepted")
|
||||
}
|
||||
record, err := runner.Resize("ses_1", run.ID, 132, 41)
|
||||
if err != nil || record.Cols != 132 || record.Rows != 41 {
|
||||
t.Fatalf("resize = %+v, %v", record, err)
|
||||
}
|
||||
// Sizes are clamped to the protocol's bounds.
|
||||
if record, err = runner.Resize("ses_1", run.ID, 5000, 1); err != nil || record.Cols != protocol.ChatShellTerminalMaxCols || record.Rows != protocol.ChatShellTerminalMinRows {
|
||||
t.Fatalf("clamped resize = %+v, %v", record, err)
|
||||
}
|
||||
if _, err = runner.Resize("ses_1", run.ID, 132, 41); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := runner.Input("ses_1", run.ID, "\r"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||
if !strings.Contains(stored.Text, "25 90\n") || !strings.Contains(stored.Text, "41 132\n") || stored.Cols != 132 || stored.Rows != 41 {
|
||||
t.Fatalf("sizes seen = %q (%dx%d)", stored.Text, stored.Cols, stored.Rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellTerminalPersistsRawAndPlainText(t *testing.T) {
|
||||
runner, frames := testShellRunner(t, context.Background())
|
||||
run := startTerminal(t, runner, `printf '\033[31mred\033[0m\n'; printf 'progress 10%%\rprogress 100%%\n'; printf '\033]8;;https://example.com\033\\link\033]8;;\033\\\n'`, 0, 0)
|
||||
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||
if !strings.Contains(stored.Output, "\x1b[31mred\x1b[0m\r\n") || !strings.Contains(stored.Output, "progress 10%\rprogress 100%") {
|
||||
t.Fatalf("raw output = %q", stored.Output)
|
||||
}
|
||||
if stored.Text != "red\nprogress 100%\nlink\n" {
|
||||
t.Fatalf("plain text = %q", stored.Text)
|
||||
}
|
||||
if !stored.Terminal || stored.Cols != protocol.ChatShellTerminalDefaultCols {
|
||||
t.Fatalf("stored terminal facts = %+v", stored)
|
||||
}
|
||||
list, err := runner.List(context.Background(), "ses_1")
|
||||
if err != nil || len(list) != 1 || list[0].Text != "" || list[0].Output != stored.Output {
|
||||
t.Fatalf("list = %+v, %v", list, err)
|
||||
}
|
||||
if prompt := chatShellSharePrompt(stored); strings.Contains(prompt, "\x1b") || !strings.Contains(prompt, "red\nprogress 100%\nlink\n```") {
|
||||
t.Fatalf("shared prompt = %q", prompt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellTerminalInputBounds(t *testing.T) {
|
||||
runner, _ := testShellRunner(t, context.Background())
|
||||
// The command never reads, so queued input piles up.
|
||||
run := startTerminal(t, runner, `stty -icanon -echo min 1; sleep 30`, 0, 0)
|
||||
if err := runner.Input("ses_1", run.ID, strings.Repeat("x", protocol.ChatShellInputLimit+1)); err == nil {
|
||||
t.Fatal("oversized input was accepted")
|
||||
}
|
||||
if err := runner.Input("ses_1", run.ID, ""); err == nil {
|
||||
t.Fatal("empty input was accepted")
|
||||
}
|
||||
if err := runner.Input("ses_1", run.ID, "\xff"); err == nil {
|
||||
t.Fatal("invalid UTF-8 was accepted")
|
||||
}
|
||||
chunk := strings.Repeat("y", protocol.ChatShellInputLimit)
|
||||
refused := false
|
||||
for range protocol.ChatShellInputQueue + 64 {
|
||||
if err := runner.Input("ses_1", run.ID, chunk); err != nil {
|
||||
refused = strings.Contains(err.Error(), "not reading")
|
||||
break
|
||||
}
|
||||
}
|
||||
if !refused {
|
||||
t.Fatal("input to a command that never reads was never refused")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatShellRecordAfterResumes(t *testing.T) {
|
||||
record := protocol.ChatShellRunRecord{Output: "héllo world", OutputStart: 100, OutputBytes: 112}
|
||||
if got := chatShellRecordAfter(record, 107); got.Output != "world" || got.OutputStart != 107 {
|
||||
t.Fatalf("resume = %q at %d", got.Output, got.OutputStart)
|
||||
}
|
||||
if got := chatShellRecordAfter(record, 102); got.Output != "llo world" || got.OutputStart != 103 {
|
||||
t.Fatalf("resume inside a character = %q at %d", got.Output, got.OutputStart)
|
||||
}
|
||||
if got := chatShellRecordAfter(record, 50); got.Output != record.Output || got.OutputStart != 100 {
|
||||
t.Fatalf("resume before the tail = %q at %d", got.Output, got.OutputStart)
|
||||
}
|
||||
if got := chatShellRecordAfter(record, 500); got.Output != "" || got.OutputStart != 112 {
|
||||
t.Fatalf("resume after the end = %q at %d", got.Output, got.OutputStart)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/creack/pty"
|
||||
)
|
||||
|
||||
// chatShellTerminals is true where operator commands run in a pseudo-terminal.
|
||||
const chatShellTerminals = true
|
||||
|
||||
// startChatShellTerminal starts cmd as the leader of a new session whose
|
||||
// controlling terminal is a fresh pseudo-terminal of cols x rows, and returns
|
||||
// the terminal's controller side: the command's merged output is read from
|
||||
// it, typed input written to it. The session id is also its process group,
|
||||
// so Stop's group signals reach everything it started.
|
||||
func startChatShellTerminal(cmd *exec.Cmd, cols, rows int) (*os.File, error) {
|
||||
return pty.StartWithAttrs(cmd, chatShellWinsize(cols, rows), &syscall.SysProcAttr{Setsid: true, Setctty: true})
|
||||
}
|
||||
|
||||
// resizeChatShellTerminal sets the terminal's size; the kernel signals the
|
||||
// foreground process group with SIGWINCH.
|
||||
func resizeChatShellTerminal(terminal *os.File, cols, rows int) error {
|
||||
return pty.Setsize(terminal, chatShellWinsize(cols, rows))
|
||||
}
|
||||
|
||||
func chatShellWinsize(cols, rows int) *pty.Winsize {
|
||||
return &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// One client starts an interactive command; the other client watching the
|
||||
// chat types into it, resizes it, and resumes its output by offset.
|
||||
func TestChatShellTerminalOverWebsocket(t *testing.T) {
|
||||
ctx, conns, _ := shellChat(t)
|
||||
start := shellRequest(protocol.ChatShellRun, "r1", "", `printf 'token? '; read -r tok; stty size; echo got:$tok`)
|
||||
start.ChatShellRunRequest.Cols, start.ChatShellRunRequest.Rows = 80, 24
|
||||
writeChat(t, ctx, conns[0], start)
|
||||
reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "r1" })
|
||||
run := reply.ChatShellRun
|
||||
if reply.Type != protocol.ChatShellRun || run == nil || !run.Terminal || run.Cols != 80 || run.Rows != 24 {
|
||||
t.Fatalf("start reply = %+v", reply)
|
||||
}
|
||||
// The prompt arrives as an output frame before anything is typed.
|
||||
var seen strings.Builder
|
||||
var end int64
|
||||
for !strings.Contains(seen.String(), "token? ") {
|
||||
frame := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool {
|
||||
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent.RunID == run.ID && m.ChatShellRunEvent.Run == nil
|
||||
}).ChatShellRunEvent
|
||||
if frame.Offset != end {
|
||||
t.Fatalf("frame at %d after %d", frame.Offset, end)
|
||||
}
|
||||
seen.WriteString(frame.Output)
|
||||
end = frame.End
|
||||
}
|
||||
resize := shellRequest(protocol.ChatShellResize, "size", run.ID, "")
|
||||
resize.ChatShellRunRequest.Cols, resize.ChatShellRunRequest.Rows = 120, 33
|
||||
writeChat(t, ctx, conns[1], resize)
|
||||
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "size" }); got.Type != protocol.ChatShellResize || got.ChatShellRun.Cols != 120 {
|
||||
t.Fatalf("resize reply = %+v", got)
|
||||
}
|
||||
input := shellRequest(protocol.ChatShellInput, "in", run.ID, "")
|
||||
input.ChatShellRunRequest.Data = "abc123\r"
|
||||
writeChat(t, ctx, conns[1], input)
|
||||
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.ChatShellInput {
|
||||
t.Fatalf("input reply = %+v", got)
|
||||
}
|
||||
final, _ := finishedShell(t, ctx, conns[0], run.ID)
|
||||
if final.Status != protocol.ChatShellRunExited {
|
||||
t.Fatalf("final = %+v", final)
|
||||
}
|
||||
// A reconnecting client asks only for what follows its copy.
|
||||
resume := shellRequest(protocol.ChatShellRunOutput, "resume", run.ID, "")
|
||||
resume.ChatShellRunRequest.After = end
|
||||
writeChat(t, ctx, conns[1], resume)
|
||||
got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "resume" }).ChatShellRun
|
||||
if got == nil || got.OutputStart != end || strings.Contains(got.Output, "token?") || !strings.Contains(got.Output, "33 120\r\ngot:abc123\r\n") || !strings.Contains(got.Text, "got:abc123") {
|
||||
t.Fatalf("resumed = %+v", got)
|
||||
}
|
||||
// Input to a finished run is refused.
|
||||
writeChat(t, ctx, conns[1], input)
|
||||
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.Error {
|
||||
t.Fatalf("late input reply = %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -250,7 +250,10 @@ 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 {
|
||||
// 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 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
|
||||
|
||||
Reference in New Issue
Block a user