1b98877d88
chat_sessions rows carry activity (turn_running, background_tasks, seq) from the hub's own state, and chat_activity is pushed to every connection when a session's turn starts or ends or its running background-task count changes. A room whose last viewer left stays observed while background tasks run, bounded by the unattended limits; a Claude Code process exit ends its tasks.
272 lines
10 KiB
Go
272 lines
10 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// steeringChat is a channelChat whose harness takes input mid-turn, the way
|
|
// Claude Code's stream-json stdin and Codex turn/steer do.
|
|
type steeringChat struct {
|
|
*channelChat
|
|
steerMu sync.Mutex
|
|
steered []string
|
|
}
|
|
|
|
func (c *steeringChat) SteerPrompt(_ context.Context, _, text, _ string, _ []opencode.PromptFile) (bool, error) {
|
|
c.steerMu.Lock()
|
|
defer c.steerMu.Unlock()
|
|
c.steered = append(c.steered, text)
|
|
return true, nil
|
|
}
|
|
|
|
type queueServer struct {
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
srv *httptest.Server
|
|
}
|
|
|
|
func startQueueServer(t *testing.T, broker chatBroker, store *pipeline.Store) *queueServer {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker}, store))
|
|
s := &queueServer{ctx: ctx, cancel: cancel, srv: srv}
|
|
t.Cleanup(s.stop)
|
|
return s
|
|
}
|
|
|
|
func (s *queueServer) stop() {
|
|
s.srv.CloseClientConnections()
|
|
s.srv.Close()
|
|
s.cancel()
|
|
}
|
|
|
|
func openQueueStore(t *testing.T, path string) *pipeline.Store {
|
|
t.Helper()
|
|
// A just-stopped server's background workers may still be finishing a
|
|
// write when the database is reopened; wait out that brief lock.
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for {
|
|
store, err := pipeline.Open(path)
|
|
if err == nil {
|
|
t.Cleanup(func() { store.Close() })
|
|
return store
|
|
}
|
|
if !strings.Contains(err.Error(), "SQLITE_BUSY") || time.Now().After(deadline) {
|
|
t.Fatal(err)
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func queuedOps(t *testing.T, store *pipeline.Store) []string {
|
|
t.Helper()
|
|
rows, err := store.ChatQueuedPrompts(context.Background(), "ses_1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var ops []string
|
|
for _, row := range rows {
|
|
ops = append(ops, row.OperationID)
|
|
}
|
|
return ops
|
|
}
|
|
|
|
func sendPrompt(t *testing.T, ctx context.Context, conn *websocket.Conn, id, text, operation string) protocol.Message {
|
|
t.Helper()
|
|
writeChat(t, ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: id, ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text, OperationID: operation, Client: "Phone"}})
|
|
return readUntil(t, ctx, conn, protocol.ChatPrompt)
|
|
}
|
|
|
|
func waitFor(t *testing.T, what string, check func() bool) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if check() {
|
|
return
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
t.Fatalf("timed out waiting for %s", what)
|
|
}
|
|
|
|
func endTurn(broker *channelChat) {
|
|
broker.push(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
|
|
}
|
|
|
|
// A prompt sent while a turn runs on a harness with mid-turn input is handed
|
|
// to that turn at once: it is acknowledged as sent, never queued or stored.
|
|
func TestChatPromptDuringTurnIsSteeredImmediately(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := &steeringChat{channelChat: newChannelChat()}
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
if ack := sendPrompt(t, server.ctx, conn, "p1", "one", "op-1"); ack.ChatQueued {
|
|
t.Fatalf("first prompt queued: %+v", ack)
|
|
}
|
|
waitFor(t, "first prompt", func() bool { return broker.promptCount() == 1 })
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); ack.ChatQueued {
|
|
t.Fatalf("mid-turn prompt was queued instead of forwarded: %+v", ack)
|
|
}
|
|
broker.steerMu.Lock()
|
|
steered := slices.Clone(broker.steered)
|
|
broker.steerMu.Unlock()
|
|
if !slices.Equal(steered, []string{"two"}) || broker.promptCount() != 1 {
|
|
t.Fatalf("steered=%v prompts=%v", steered, broker.promptTexts())
|
|
}
|
|
if ops := queuedOps(t, store); len(ops) != 0 {
|
|
t.Fatalf("steered prompt was stored: %v", ops)
|
|
}
|
|
// A retry of the same operation is acknowledged, never forwarded again.
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); ack.ChatQueued {
|
|
t.Fatalf("retry ack = %+v", ack)
|
|
}
|
|
broker.steerMu.Lock()
|
|
defer broker.steerMu.Unlock()
|
|
if len(broker.steered) != 1 {
|
|
t.Fatalf("retry steered again: %v", broker.steered)
|
|
}
|
|
}
|
|
|
|
// Without mid-turn input the prompt is queued on the server: it is stored
|
|
// once (a retry of the same operation id is not a second entry), outlives the
|
|
// last client leaving, and is delivered when the turn ends with nobody
|
|
// watching.
|
|
func TestChatQueuedPromptSurvivesDetachAndDeliversUnwatched(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
sendPrompt(t, server.ctx, conn, "p1", "one", "op-1")
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); !ack.ChatQueued {
|
|
t.Fatalf("second prompt not queued: %+v", ack)
|
|
}
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2-retry", "two", "op-2"); !ack.ChatQueued {
|
|
t.Fatalf("retry of a queued prompt not acknowledged as queued: %+v", ack)
|
|
}
|
|
if ops := queuedOps(t, store); !slices.Equal(ops, []string{"op-2"}) {
|
|
t.Fatalf("stored queue = %v, want op-2 once", ops)
|
|
}
|
|
conn.CloseNow()
|
|
time.Sleep(100 * time.Millisecond) // let the server notice the disconnect
|
|
endTurn(broker)
|
|
waitFor(t, "queued prompt delivered with no client", func() bool {
|
|
return slices.Equal(broker.promptTexts(), []string{"one", "two"})
|
|
})
|
|
waitFor(t, "delivered row deleted", func() bool { return len(queuedOps(t, store)) == 0 })
|
|
}
|
|
|
|
// The durable queue survives a server restart: the reopened store's prompts
|
|
// are delivered once the harness is idle, with no client connected.
|
|
func TestChatQueuedPromptSurvivesRestart(t *testing.T) {
|
|
previous := chatQueueRecoverySettle
|
|
chatQueueRecoverySettle = 10 * time.Millisecond
|
|
t.Cleanup(func() { chatQueueRecoverySettle = previous })
|
|
path := filepath.Join(t.TempDir(), "queue.db")
|
|
first := openQueueStore(t, path)
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, first)
|
|
conn := dialChatConn(t, server.ctx, server.srv)
|
|
sendPrompt(t, server.ctx, conn, "p1", "one", "op-1")
|
|
if ack := sendPrompt(t, server.ctx, conn, "p2", "two", "op-2"); !ack.ChatQueued {
|
|
t.Fatalf("second prompt not queued: %+v", ack)
|
|
}
|
|
server.stop()
|
|
first.Close()
|
|
|
|
reopened := openQueueStore(t, path)
|
|
if ops := queuedOps(t, reopened); !slices.Equal(ops, []string{"op-2"}) {
|
|
t.Fatalf("queue after restart = %v", ops)
|
|
}
|
|
restarted := newChannelChat()
|
|
startQueueServer(t, restarted, reopened)
|
|
waitFor(t, "recovered prompt delivered", func() bool {
|
|
return slices.Equal(restarted.promptTexts(), []string{"two"})
|
|
})
|
|
waitFor(t, "recovered row deleted", func() bool { return len(queuedOps(t, reopened)) == 0 })
|
|
}
|
|
|
|
// A client that attaches later (a reload) is told what the server still holds
|
|
// in chat_subscribe, can remove a queued prompt, and a removed prompt is never
|
|
// delivered.
|
|
func TestChatSubscribeListsQueuedPromptsAndUnqueueRemoves(t *testing.T) {
|
|
store := openQueueStore(t, filepath.Join(t.TempDir(), "queue.db"))
|
|
broker := newChannelChat()
|
|
server := startQueueServer(t, broker, store)
|
|
phone := dialChatConn(t, server.ctx, server.srv)
|
|
sendPrompt(t, server.ctx, phone, "p1", "one", "op-1")
|
|
sendPrompt(t, server.ctx, phone, "p2", "two", "op-2")
|
|
sendPrompt(t, server.ctx, phone, "p3", "three", "op-3")
|
|
phone.CloseNow()
|
|
|
|
reloaded := dialChatConn(t, server.ctx, server.srv)
|
|
writeChat(t, server.ctx, reloaded, protocol.Message{Type: protocol.ChatSubscribe, ID: "sub", ChatSubscribe: &protocol.ChatQuery{SessionID: "ses_1"}})
|
|
ack := readUntil(t, server.ctx, reloaded, protocol.ChatSubscribe)
|
|
if ack.ChatQueue == nil || len(ack.ChatQueue.Prompts) != 2 {
|
|
t.Fatalf("subscribe queue = %+v", ack.ChatQueue)
|
|
}
|
|
got := ack.ChatQueue.Prompts[0]
|
|
if got.OperationID != "op-2" || got.Text != "two" || got.Client != "Phone" || got.MessageID == "" || ack.ChatQueue.Prompts[1].OperationID != "op-3" {
|
|
t.Fatalf("queued prompts = %+v", ack.ChatQueue.Prompts)
|
|
}
|
|
writeChat(t, server.ctx, reloaded, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: "op-2"}})
|
|
removed := readUntil(t, server.ctx, reloaded, protocol.ChatUnqueue)
|
|
if removed.ChatQueue == nil || removed.ChatQueue.Removed != "op-2" || len(removed.ChatQueue.Prompts) != 1 {
|
|
t.Fatalf("unqueue reply = %+v", removed.ChatQueue)
|
|
}
|
|
if ops := queuedOps(t, store); !slices.Equal(ops, []string{"op-3"}) {
|
|
t.Fatalf("stored queue after unqueue = %v", ops)
|
|
}
|
|
endTurn(broker)
|
|
// Delivery re-publishes the user turn and reports it sent to the client
|
|
// that attached after it was queued.
|
|
sawUser := false
|
|
for {
|
|
var message protocol.Message
|
|
if err := readTestFrame(server.ctx, reloaded, &message); err != nil {
|
|
t.Fatalf("no delivery reported: %v", err)
|
|
}
|
|
if event := message.ChatEvent; event != nil && event.Kind == protocol.ChatEventUser && event.OperationID == "op-3" {
|
|
sawUser = true
|
|
}
|
|
if message.Type == protocol.ChatQueue && message.ChatQueue.Delivered != "" {
|
|
if message.ChatQueue.Delivered != "op-3" || len(message.ChatQueue.Prompts) != 0 || !sawUser {
|
|
t.Fatalf("delivery frame = %+v (user turn seen: %v)", message.ChatQueue, sawUser)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if texts := broker.promptTexts(); !slices.Equal(texts, []string{"one", "three"}) {
|
|
t.Fatalf("delivered prompts = %v, want the removed one skipped", texts)
|
|
}
|
|
if ops := queuedOps(t, store); len(ops) != 0 {
|
|
t.Fatalf("stored queue after delivery = %v", ops)
|
|
}
|
|
// A prompt already delivered can no longer be removed.
|
|
writeChat(t, server.ctx, reloaded, protocol.Message{Type: protocol.ChatUnqueue, ID: "rm-late", ChatUnqueue: &protocol.ChatUnqueueRequest{SessionID: "ses_1", OperationID: "op-3"}})
|
|
for {
|
|
var message protocol.Message
|
|
if err := readTestFrame(server.ctx, reloaded, &message); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if message.ID == "rm-late" {
|
|
if message.Type != protocol.Error {
|
|
t.Fatalf("late unqueue = %+v", message)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|