Files
pipelines/internal/server/chat_shell_run_test.go
T
alan 026942b9b7 Chat queue: handle usage refusals only after start-up recovery; fix shell share test ordering
TestAutoSwitchReplacesTheAutoContinue flaked because a usage refusal (delivered
on its own goroutine) was acted on while start-up recovery was still running.
resumeQueued read the durable auto-continue row, the server's account switch
then removed it, and resumeQueued put it back into the room; as the queue head
scheduled an hour out it held the switch's continue prompt behind it. The
refusal handler (and the schedule_followup tool) now wait for the same
recovery gate chat_* requests already wait for, and the refusal watcher is
registered only once the hub is fully wired.

TestChatShellShareQueuesAnOrdinaryPrompt flaked because the turn runner writes
the sender's reply straight to its connection while the echoed user turn goes
through the subscription writer, so the reply can arrive first and the test
discarded it while reading for the echo. The test now accepts either order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:24:22 +00:00

204 lines
8.9 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+"\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+"\r\nout\r\nerr\r\n" || list.ChatShellRuns[0].Text != "" || *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.
// The turn runner writes the sender's reply straight to its connection
// while the echo goes through the sender's subscription writer, so the two
// arrive in either order; neither may be skipped while reading for the
// other.
var reply *protocol.Message
for i, conn := range conns {
user := readShellFrame(t, ctx, conn, func(m protocol.Message) bool {
if i == 0 && m.ID == "share" {
reply = &m
return false
}
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 == nil {
got := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "share" })
reply = &got
}
if reply.Type != protocol.ChatPrompt {
t.Fatalf("share reply = %+v", reply)
}
}