Files
pipelines/internal/server/chat_shell_run_test.go
T
alan e6bc57c901 Run operator shell commands in a chat's directory on the server
chat_shell_run starts $SHELL -lc in the chat session's directory in its own
process group, streams merged output to every watcher of the session, keeps a
1 MiB tail and persists each run; chat_shell_stop interrupts the group and
kills it after a grace period. chat_shell_share queues an ordinary prompt
with the command and a bounded output tail.
2026-11-09 15:55:14 +00:00

191 lines
8.3 KiB
Go

package server
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
"github.com/coder/websocket"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// shellChat is one server with a recorded operator session whose directory is
// a fresh temporary folder, and two connections watching that session.
func shellChat(t *testing.T) (context.Context, []*websocket.Conn, string) {
t.Helper()
store := newOperatorStore(t)
broker := scriptedChat()
broker.block = true
ctx, _, conns := dialChatConns(t, Options{Chat: broker, OperatorRegistry: store}, 2)
dir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
if err := store.RecordOperatorSession(ctx, "ses_1", dir, "Shell", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
for i, conn := range conns {
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatSubscribe, ID: "sub", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}})
if got := readShellFrame(t, ctx, conn, func(m protocol.Message) bool { return m.ID == "sub" }); got.Type != protocol.ChatSubscribe {
t.Fatalf("subscribe %d = %+v", i, got)
}
}
return ctx, conns, dir
}
func readShellFrame(t *testing.T, ctx context.Context, conn *websocket.Conn, match func(protocol.Message) bool) protocol.Message {
t.Helper()
for {
var message protocol.Message
if err := readTestFrame(ctx, conn, &message); err != nil {
t.Fatal(err)
}
if match(message) {
return message
}
}
}
func shellRequest(kind, id, runID, command string) protocol.Message {
return protocol.Message{Type: kind, ID: id, ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "ses_1", Command: command, RunID: runID}}
}
// runShell starts command and returns its running record.
func runShell(t *testing.T, ctx context.Context, conn *websocket.Conn, id, command string) protocol.ChatShellRunRecord {
t.Helper()
writeChat(t, ctx, conn, shellRequest(protocol.ChatShellRun, id, "", command))
reply := readShellFrame(t, ctx, conn, func(m protocol.Message) bool { return m.ID == id })
if reply.Type != protocol.ChatShellRun || reply.ChatShellRun == nil || reply.ChatShellRun.Status != protocol.ChatShellRunRunning {
t.Fatalf("chat_shell_run reply = %+v", reply)
}
return *reply.ChatShellRun
}
// finishedShell reads conn's frames until runID's final lifecycle frame and
// returns it with the live output joined from its output frames.
func finishedShell(t *testing.T, ctx context.Context, conn *websocket.Conn, runID string) (protocol.ChatShellRunRecord, string) {
t.Helper()
var output strings.Builder
var end int64
for {
frame := readShellFrame(t, ctx, conn, func(m protocol.Message) bool {
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent != nil && m.ChatShellRunEvent.RunID == runID
}).ChatShellRunEvent
if frame.Run == nil {
if frame.Offset != end {
t.Fatalf("output frame at %d after %d", frame.Offset, end)
}
output.WriteString(frame.Output)
end = frame.End
continue
}
if frame.Run.Status != protocol.ChatShellRunRunning {
return *frame.Run, output.String()
}
}
}
func TestChatShellRunStreamsToEveryWatcherAndPersists(t *testing.T) {
ctx, conns, dir := shellChat(t)
run := runShell(t, ctx, conns[0], "r1", `pwd; echo out; echo err >&2; exit 3`)
if run.Directory != dir || run.Command == "" || run.ID == "" {
t.Fatalf("running record = %+v", run)
}
for _, conn := range conns {
final, output := finishedShell(t, ctx, conn, run.ID)
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)
}
}
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 {
t.Fatalf("persisted runs = %+v", list)
}
}
func TestChatShellStopInterruptsTheGroup(t *testing.T) {
ctx, conns, _ := shellChat(t)
// The child sleep is in the shell's group, so the stop reaches it too.
run := runShell(t, ctx, conns[0], "r1", `echo started; sleep 30; echo never`)
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellStop, "stop", run.ID, ""))
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "stop" }); reply.Type != protocol.ChatShellStop {
t.Fatalf("stop reply = %+v", reply)
}
final, output := finishedShell(t, ctx, conns[1], run.ID)
if final.Status != protocol.ChatShellRunStopped || strings.Contains(output, "never") || final.DurationMS > 10_000 {
t.Fatalf("stopped record = %+v output=%q", final, output)
}
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellStop, "again", run.ID, ""))
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "again" }); reply.Type != protocol.Error {
t.Fatalf("stopping a finished run = %+v", reply)
}
}
func TestChatShellRunLimitsAndRefusals(t *testing.T) {
ctx, conns, dir := shellChat(t)
var runs []protocol.ChatShellRunRecord
for i := 0; i < protocol.ChatShellRunConcurrent; i++ {
runs = append(runs, runShell(t, ctx, conns[0], "r"+string(rune('a'+i)), "sleep 30"))
}
refusals := []protocol.Message{
shellRequest(protocol.ChatShellRun, "fifth", "", "sleep 30"),
shellRequest(protocol.ChatShellRun, "blank", "", " "),
shellRequest(protocol.ChatShellRun, "huge", "", strings.Repeat("x", protocol.ChatShellRunCommandLimit+1)),
{Type: protocol.ChatShellRun, ID: "nobody", ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "ses_unknown", Command: "true"}},
{Type: protocol.ChatShellRun, ID: "badid", ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "../ses", Command: "true"}},
{Type: protocol.ChatShellRun, ID: "nobody-body"},
{Type: protocol.ChatShellStop, ID: "foreign", ChatShellRunRequest: &protocol.ChatShellRunRequest{SessionID: "ses_unknown", RunID: runs[0].ID}},
shellRequest(protocol.ChatShellShare, "share-running", runs[0].ID, ""),
}
for _, request := range refusals {
writeChat(t, ctx, conns[0], request)
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == request.ID }); reply.Type != protocol.Error {
t.Fatalf("%s was not refused: %+v", request.ID, reply)
}
}
for _, run := range runs {
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellStop, "stop-"+run.ID, run.ID, ""))
readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "stop-"+run.ID })
}
for range runs {
readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool {
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent.Run != nil && m.ChatShellRunEvent.Run.Status == protocol.ChatShellRunStopped
})
}
// A deleted working directory is refused rather than run somewhere else.
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellRun, "gone", "", "true"))
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "gone" }); reply.Type != protocol.Error || !strings.Contains(reply.Error, "not available") {
t.Fatalf("missing directory = %+v", reply)
}
}
func TestChatShellShareQueuesAnOrdinaryPrompt(t *testing.T) {
ctx, conns, dir := shellChat(t)
run := runShell(t, ctx, conns[0], "r1", "echo shared-output")
finishedShell(t, ctx, conns[0], run.ID)
writeChat(t, ctx, conns[0], shellRequest(protocol.ChatShellShare, "share", run.ID, ""))
// The composed prompt is echoed to the sender too: it has no local copy.
for _, conn := range conns {
user := readShellFrame(t, ctx, conn, func(m protocol.Message) bool {
return m.Type == protocol.ChatEvent && m.ChatEvent != nil && m.ChatEvent.Kind == protocol.ChatEventUser
}).ChatEvent
text := user.Part.Text
if !strings.Contains(text, "$ echo shared-output") || !strings.Contains(text, "shared-output\n```") || !strings.Contains(text, dir) || !strings.Contains(text, "exited with status 0") {
t.Fatalf("shared prompt = %q", text)
}
}
if reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "share" }); reply.Type != protocol.ChatPrompt {
t.Fatalf("share reply = %+v", reply)
}
}