Send mid-turn chat prompts to the harness and queue the rest durably on the server

Claude Code (stream-json stdin) and Codex (turn/steer pinned to the active
turn) receive a prompt sent while a turn runs immediately. Other harnesses
(OpenCode) get a durable chat_prompt_queue row owned by the session: it
survives the last client detaching and a server restart, is delivered in
order when the turn ends with or without a client, and is deduplicated by
operation id. chat_subscribe carries the queue, chat_queue frames report
delivered/removed/failed changes, and chat_unqueue removes a waiting prompt.
This commit is contained in:
2026-11-01 18:30:20 +00:00
parent d300fdc5a5
commit e42f9cc031
25 changed files with 1568 additions and 78 deletions
+3
View File
@@ -167,6 +167,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
case protocol.ChatSubscribe:
handleChatSubscribe(request, broker, hub, clientID, write)
return true
case protocol.ChatUnqueue:
handleChatUnqueue(ctx, request, hub, write)
return true
case protocol.ChatMessages:
handleChatMessages(ctx, request, broker, hub, write)
return true
+2
View File
@@ -56,6 +56,8 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// A closed chat takes no more turns: prompts it still held are dropped.
hub.clearQueue(ctx, sessionID)
session, found, err := registry.OperatorSession(ctx, sessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
+6
View File
@@ -34,3 +34,9 @@ func TestAdvertisedChatHarnessesExposeInteractiveWebContract(t *testing.T) {
}
}
}
// Claude Code and Codex take a prompt into a running turn; OpenCode does not
// (its prompts are queued durably on the server behind the running turn).
var _ chatMidTurn = (*claude.Chat)(nil)
var _ chatMidTurn = (*codex.Chat)(nil)
var _ chatMidTurn = (*chatManager)(nil)
+7
View File
@@ -51,6 +51,13 @@ type chatHub struct {
attentionSeen map[string]struct{}
attentionOrder []string
attentionWG sync.WaitGroup
// queueStore persists prompts queued behind a running turn; nil keeps the
// queue in memory only (tests and store-less servers).
queueStore chatPromptQueueStore
// recentOps/recentOpOrder are the bounded record of accepted prompt
// operation ids, so a client retry is acknowledged instead of re-sent.
recentOps map[string]struct{}
recentOpOrder []string
}
// chatPart is the retained form of one part. Text and reasoning deltas append to
+20
View File
@@ -388,6 +388,26 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
return errChatAttachmentsUnsupported
}
// SteerPrompt forwards a prompt into the routed harness's running turn when
// that harness accepts mid-turn input (Claude Code, Codex). A harness without
// it reports not delivered, so the hub queues the prompt durably instead.
func (m *chatManager) SteerPrompt(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) (bool, error) {
adapter, err := m.brokerFor(ctx, sessionID)
if err != nil {
return false, err
}
steerer, ok := adapter.(chatMidTurn)
if !ok {
return false, nil
}
if len(files) > 0 {
if _, ok := adapter.(chatPromptWithFiles); !ok {
return false, errChatAttachmentsUnsupported
}
}
return steerer.SteerPrompt(ctx, sessionID, text, messageID, files)
}
var errChatAttachmentsUnsupported = errors.New("this chat harness does not support attachments")
var errChatFormReadbackUnsupported = errors.New("this chat harness does not support pending question readback")
+476
View File
@@ -0,0 +1,476 @@
package server
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"path/filepath"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// A prompt that arrives while its session's turn is running belongs to the
// server, not to the connection that sent it. Where the harness accepts input
// mid-turn (Claude Code's stream-json stdin, Codex turn/steer) the prompt is
// forwarded at once and the harness reads it at its next step. Otherwise it is
// written to the durable chat_prompt_queue and delivered in order when the turn
// ends, whether or not any client is still attached, and after a restart.
// chatQueueBusyPoll is how often a room holding queued prompts re-checks a
// harness that reports its own turn in flight. TurnRunning performs no I/O.
var chatQueueBusyPoll = time.Second
// chatQueueRecoverySettle gives a restarted server's freshly attached stream a
// moment to announce a turn the harness is still running, so a recovered
// prompt is not delivered into a busy harness that only reports activity
// through its stream.
var chatQueueRecoverySettle = 1500 * time.Millisecond
// chatRecentOpsLimit bounds the remembered accepted operation ids used to
// refuse a duplicate delivery of a client retry.
const chatRecentOpsLimit = 256
// chatPromptQueueStore is the durable queue seam; *pipeline.Store implements it.
type chatPromptQueueStore interface {
EnqueueChatPrompt(ctx context.Context, prompt pipeline.ChatQueuedPrompt, limit int) (pipeline.ChatQueuedPrompt, bool, error)
DeleteChatQueuedPrompt(ctx context.Context, sessionID, operationID string) (bool, error)
ClearChatQueuedPrompts(ctx context.Context, sessionID string) error
ChatQueuedPrompts(ctx context.Context, sessionID string) ([]pipeline.ChatQueuedPrompt, error)
ChatQueuedSessions(ctx context.Context) ([]string, error)
}
// chatMidTurn is implemented by a broker that can hand a user message to a
// turn already running. delivered is false (with a nil error) when the session
// has no turn the harness can take input for right now, and the caller then
// queues the prompt instead. An error means the harness refused the message.
type chatMidTurn interface {
SteerPrompt(ctx context.Context, sessionID, text, messageID string, files []opencode.PromptFile) (bool, error)
}
// chatAdmission is the outcome of offering one prompt to a session.
type chatAdmission int
const (
chatAdmitRun chatAdmission = iota
chatAdmitQueued
chatAdmitSteered
chatAdmitRefused
)
type chatStoredFile struct {
URI string `json:"uri"`
Name string `json:"name"`
}
// admitPrompt starts p when the session is idle, forwards it into the running
// turn when the harness supports mid-turn input, and otherwise queues it
// (durably when a store is configured). It never blocks on the turn itself.
func (h *chatHub) admitPrompt(ctx context.Context, sessionID string, broker chatBroker, p *chatQueuedPrompt) (chatAdmission, error) {
h.mu.Lock()
room := h.roomLocked(sessionID)
if room.broker == nil {
room.broker = broker
}
busy := room.running || len(room.queue) > 0 || room.observedTurn
queuedAhead := len(room.queue) > 0
h.mu.Unlock()
if !busy {
busy = brokerTurnRunning(broker, sessionID)
}
if !busy {
h.mu.Lock()
room.queue = append(room.queue, p)
signalWake(room.wake)
h.mu.Unlock()
return chatAdmitRun, nil
}
if steerer, ok := broker.(chatMidTurn); ok && !queuedAhead {
delivered, err := steerer.SteerPrompt(ctx, sessionID, p.text, p.nativeID, p.files)
if err != nil {
return chatAdmitRefused, err
}
if delivered {
releaseChatAttachments(p.files)
return chatAdmitSteered, nil
}
}
p.acked = true
p.queuedAt = h.clock()
if store := h.queueStore; store != nil {
row, created, err := store.EnqueueChatPrompt(ctx, chatQueueRow(sessionID, p), chatRoomQueueLimit)
if errors.Is(err, pipeline.ErrChatQueueFull) {
return chatAdmitRefused, errChatQueueFull(sessionID)
}
if err != nil {
return chatAdmitRefused, fmt.Errorf("queue prompt: %w", err)
}
if !created {
// A retry of a prompt the server already holds: acknowledge it as
// queued again without a second entry.
releaseChatAttachments(p.files)
return chatAdmitQueued, nil
}
p.durable = true
p.queuedAt = row.CreatedAt
}
h.mu.Lock()
if !p.durable && len(room.queue) >= chatRoomQueueLimit {
h.mu.Unlock()
return chatAdmitRefused, errChatQueueFull(sessionID)
}
room.queue = append(room.queue, p)
signalWake(room.wake)
h.mu.Unlock()
h.fanoutQueue(sessionID, protocol.ChatQueueFrame{})
return chatAdmitQueued, nil
}
func errChatQueueFull(sessionID string) error {
return errors.New("session " + sessionID + " already has the maximum number of prompts queued")
}
// brokerTurnRunning asks a harness that knows its own turn state without I/O.
func brokerTurnRunning(broker chatBroker, sessionID string) bool {
reporter, ok := broker.(chatTurnReporter)
return ok && reporter.TurnRunning(sessionID)
}
func chatQueueRow(sessionID string, p *chatQueuedPrompt) pipeline.ChatQueuedPrompt {
files := make([]chatStoredFile, 0, len(p.files))
for _, file := range p.files {
files = append(files, chatStoredFile{URI: file.URI, Name: file.Name})
}
filesJSON, _ := json.Marshal(files)
parts := p.parts
if parts == nil {
parts = []protocol.ChatPart{}
}
partsJSON, _ := json.Marshal(parts)
return pipeline.ChatQueuedPrompt{
SessionID: sessionID,
OperationID: p.operationID,
MessageID: p.messageID,
NativeID: p.nativeID,
Text: p.text,
Client: p.client,
FilesJSON: string(filesJSON),
PartsJSON: string(partsJSON),
CreatedAt: p.queuedAt,
}
}
// promptFromRow rebuilds a queued prompt read back from the store. Its files
// must still name the server's own attachment directory; they are leased again
// so retention cannot collect them before delivery.
func promptFromRow(row pipeline.ChatQueuedPrompt) (*chatQueuedPrompt, error) {
p := &chatQueuedPrompt{
text: row.Text,
operationID: row.OperationID,
messageID: row.MessageID,
nativeID: row.NativeID,
client: row.Client,
acked: true,
durable: true,
queuedAt: row.CreatedAt,
}
_ = json.Unmarshal([]byte(row.PartsJSON), &p.parts)
var stored []chatStoredFile
if err := json.Unmarshal([]byte(row.FilesJSON), &stored); err != nil {
return p, fmt.Errorf("queued prompt attachments are unreadable")
}
if len(stored) == 0 {
return p, nil
}
dir, err := chatAttachmentDir()
if err != nil {
return p, err
}
chatAttachmentStore.Lock()
defer chatAttachmentStore.Unlock()
for _, file := range stored {
u, err := url.Parse(file.URI)
if err != nil || u.Scheme != "file" || filepath.Dir(u.Path) != dir {
return p, fmt.Errorf("queued prompt attachment %q is outside the attachment store", file.Name)
}
chatAttachmentStore.leased[u.Path] = true
p.files = append(p.files, opencode.NewPreparedPromptFile(file.URI, file.Name))
}
return p, nil
}
// queueFrame is the session's current server-held queue. A live room is the
// authority while it exists; otherwise the store answers.
func (h *chatHub) queueFrame(sessionID string) protocol.ChatQueueFrame {
frame := protocol.ChatQueueFrame{SessionID: sessionID, Prompts: []protocol.ChatQueuedPrompt{}}
h.mu.Lock()
room := h.rooms[sessionID]
if room != nil {
for _, p := range room.queue {
if p.acked {
frame.Prompts = append(frame.Prompts, p.wire())
}
}
h.mu.Unlock()
return frame
}
h.mu.Unlock()
if h.queueStore == nil {
return frame
}
rows, err := h.queueStore.ChatQueuedPrompts(h.baseCtx, sessionID)
if err != nil {
log.Printf("chat queue read session=%s: %v", sessionID, err)
return frame
}
for _, row := range rows {
p := &chatQueuedPrompt{text: row.Text, operationID: row.OperationID, messageID: row.MessageID, nativeID: row.NativeID, client: row.Client, queuedAt: row.CreatedAt}
_ = json.Unmarshal([]byte(row.PartsJSON), &p.parts)
frame.Prompts = append(frame.Prompts, p.wire())
}
return frame
}
func (p *chatQueuedPrompt) wire() protocol.ChatQueuedPrompt {
out := protocol.ChatQueuedPrompt{
OperationID: p.operationID,
MessageID: p.messageID,
NativeMessageID: p.nativeID,
Text: p.text,
Client: p.client,
Attachments: p.parts,
}
if !p.queuedAt.IsZero() {
out.QueuedAt = p.queuedAt.UnixMilli()
}
return out
}
// fanoutQueue pushes the session's current queue to every attached client,
// naming the change in change's Delivered/Removed/Failed/Error fields.
func (h *chatHub) fanoutQueue(sessionID string, change protocol.ChatQueueFrame) {
frame := h.queueFrame(sessionID)
frame.Delivered, frame.Removed, frame.Failed, frame.Error = change.Delivered, change.Removed, change.Failed, change.Error
h.fanout(sessionID, protocol.Message{Type: protocol.ChatQueue, ChatQueue: &frame}, "")
}
// forgetQueued drops a delivered or failed prompt's durable row.
func (h *chatHub) forgetQueued(sessionID string, p *chatQueuedPrompt) {
if !p.durable || h.queueStore == nil {
return
}
if _, err := h.queueStore.DeleteChatQueuedPrompt(h.baseCtx, sessionID, p.operationID); err != nil {
log.Printf("chat queue delete session=%s: %v", sessionID, err)
}
}
// unqueue removes one prompt still waiting in the session's queue. It reports
// false when the session holds no such prompt (it was delivered already).
func (h *chatHub) unqueue(ctx context.Context, sessionID, operationID string) (bool, error) {
var removed *chatQueuedPrompt
h.mu.Lock()
room := h.rooms[sessionID]
if room != nil {
// The live room is authoritative: a prompt its runner already took is
// being delivered and is no longer removable, even though its durable row
// is only deleted once the harness accepted it.
for i, p := range room.queue {
if p.acked && p.operationID == operationID {
removed = p
room.queue = append(room.queue[:i:i], room.queue[i+1:]...)
break
}
}
}
h.mu.Unlock()
found := removed != nil
if removed != nil {
releaseChatAttachments(removed.files)
}
if h.queueStore != nil && (room == nil || removed != nil) {
deleted, err := h.queueStore.DeleteChatQueuedPrompt(ctx, sessionID, operationID)
if err != nil {
return found, err
}
found = found || deleted
}
if found {
h.forgetOp(sessionID, operationID)
h.fanoutQueue(sessionID, protocol.ChatQueueFrame{Removed: operationID})
}
return found, nil
}
// clearQueue drops every prompt a closed session still holds.
func (h *chatHub) clearQueue(ctx context.Context, sessionID string) {
var dropped []*chatQueuedPrompt
h.mu.Lock()
if room := h.rooms[sessionID]; room != nil {
kept := room.queue[:0]
for _, p := range room.queue {
if p.acked {
dropped = append(dropped, p)
} else {
kept = append(kept, p)
}
}
room.queue = kept
}
h.mu.Unlock()
for _, p := range dropped {
releaseChatAttachments(p.files)
}
if h.queueStore != nil {
if err := h.queueStore.ClearChatQueuedPrompts(ctx, sessionID); err != nil {
log.Printf("chat queue clear session=%s: %v", sessionID, err)
}
}
if len(dropped) > 0 {
h.fanoutQueue(sessionID, protocol.ChatQueueFrame{})
}
}
// resumeQueued restores every session's durable queue after a restart: each
// session gets a room (with no client attached) whose runner delivers the
// prompts in order once the harness is idle.
func (h *chatHub) resumeQueued(ctx context.Context, broker chatBroker) {
if h.queueStore == nil || broker == nil {
return
}
sessions, err := h.queueStore.ChatQueuedSessions(ctx)
if err != nil {
log.Printf("chat queue recovery: %v", err)
return
}
for _, sessionID := range sessions {
rows, err := h.queueStore.ChatQueuedPrompts(ctx, sessionID)
if err != nil {
log.Printf("chat queue recovery session=%s: %v", sessionID, err)
continue
}
var prompts []*chatQueuedPrompt
for _, row := range rows {
p, err := promptFromRow(row)
if err != nil {
releaseChatAttachments(p.files)
log.Printf("chat queue recovery session=%s: %v", sessionID, err)
_, _ = h.queueStore.DeleteChatQueuedPrompt(ctx, sessionID, row.OperationID)
continue
}
prompts = append(prompts, p)
}
if len(prompts) == 0 {
continue
}
h.mu.Lock()
room := h.roomLocked(sessionID)
if room.broker == nil {
room.broker = broker
}
room.notBefore = time.Now().Add(chatQueueRecoverySettle)
for _, p := range prompts {
if !room.holdsOp(p.operationID) {
room.queue = append(room.queue, p)
h.rememberOpLocked(sessionID, p.operationID)
}
}
h.mu.Unlock()
go func(room *chatRoom) {
// Attach the stream first so a turn the harness is still running is
// observed before the settle window lets the runner deliver.
if err := h.ensureObserving(room, broker); err != nil && room.ctx.Err() == nil {
log.Printf("chat queue recovery observe session=%s: %v", room.sessionID, err)
}
h.mu.Lock()
signalWake(room.wake)
h.mu.Unlock()
}(room)
}
}
func (r *chatRoom) holdsOp(operationID string) bool {
for _, p := range r.queue {
if p.operationID == operationID {
return true
}
}
return false
}
// duplicateOp reports whether operationID is a prompt this session already
// accepted (queued reports it is still waiting in the queue), so a client retry
// is acknowledged again instead of delivered twice.
func (h *chatHub) duplicateOp(sessionID, operationID string) (duplicate, queued bool) {
if operationID == "" {
return false, false
}
h.mu.Lock()
defer h.mu.Unlock()
if room := h.rooms[sessionID]; room != nil {
for _, p := range room.queue {
if p.operationID == operationID {
return true, p.acked
}
}
}
_, seen := h.recentOps[sessionID+"\x00"+operationID]
return seen, false
}
func (h *chatHub) rememberOp(sessionID, operationID string) {
h.mu.Lock()
defer h.mu.Unlock()
h.rememberOpLocked(sessionID, operationID)
}
func (h *chatHub) rememberOpLocked(sessionID, operationID string) {
if operationID == "" {
return
}
key := sessionID + "\x00" + operationID
if h.recentOps == nil {
h.recentOps = map[string]struct{}{}
}
if _, ok := h.recentOps[key]; ok {
return
}
h.recentOps[key] = struct{}{}
h.recentOpOrder = append(h.recentOpOrder, key)
for len(h.recentOpOrder) > chatRecentOpsLimit {
delete(h.recentOps, h.recentOpOrder[0])
h.recentOpOrder = h.recentOpOrder[1:]
}
}
// forgetOp lets a prompt that never reached the harness be retried.
func (h *chatHub) forgetOp(sessionID, operationID string) {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.recentOps, sessionID+"\x00"+operationID)
}
// handleChatUnqueue removes one queued prompt and replies with the queue.
func handleChatUnqueue(ctx context.Context, request protocol.Message, hub *chatHub, write func(protocol.Message) error) {
body := request.ChatUnqueue
if body == nil || !validChatSessionID(body.SessionID) || body.OperationID == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid session id and operation id are required"})
return
}
found, err := hub.unqueue(ctx, body.SessionID, body.OperationID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
if !found {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "that message is no longer queued; it was already sent"})
return
}
frame := hub.queueFrame(body.SessionID)
frame.Removed = body.OperationID
_ = write(protocol.Message{Type: protocol.ChatUnqueue, ID: request.ID, ChatQueue: &frame})
}
+263
View File
@@ -0,0 +1,263 @@
package server
import (
"context"
"net/http/httptest"
"path/filepath"
"slices"
"sync"
"testing"
"time"
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"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()
store, err := pipeline.Open(path)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { store.Close() })
return store
}
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 := wsjson.Read(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 := wsjson.Read(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
}
}
}
+134 -48
View File
@@ -101,14 +101,25 @@ func (s *chatSub) close() {
// turn runner does not send a second (success) acknowledgement for the same
// request id; write is kept either way so a later broker failure is still
// reported to the client that sent it.
//
// durable marks a prompt written to the chat_prompt_queue store: it outlives
// the connection that sent it (write may be nil after a restart) and its row is
// deleted once the harness accepted it. messageID, client and parts let the
// server re-publish the user turn when it is finally delivered, so a client
// that attached after it was queued still sees it enter the transcript.
type chatQueuedPrompt struct {
text string
files []opencode.PromptFile
operationID string
nativeID string
messageID string
client string
parts []protocol.ChatPart
requestID string
write func(protocol.Message) error
acked bool
durable bool
queuedAt time.Time
}
type chatObservation struct {
@@ -153,6 +164,9 @@ type chatRoom struct {
wake chan struct{}
turnDone chan struct{}
nativeTurnID string
// notBefore holds back a recovered queue until the restarted stream had a
// chance to report a turn the harness is still running.
notBefore time.Time
}
func newChatRoom(base context.Context, sessionID string) *chatRoom {
@@ -295,8 +309,11 @@ func (h *chatHub) attach(sessionID, clientID, label string, broker chatBroker, w
}
// detach drops one subscription. The room (and its broker stream and queue) is
// released once its last client leaves, so a session with no watchers holds no
// goroutine.
// released once its last client leaves and it has no prompt left to deliver,
// so a session with no watchers holds no goroutine. A room holding queued
// prompts stays (with its stream) until its runner has delivered them: the
// queue belongs to the session, not to the clients watching it. A running turn
// with nothing queued behind it needs no room; the harness finishes it alone.
func (h *chatHub) detach(sessionID string, sub *chatSub) {
h.mu.Lock()
room := h.rooms[sessionID]
@@ -305,19 +322,27 @@ func (h *chatHub) detach(sessionID string, sub *chatSub) {
return
}
delete(room.subs, sub.clientID)
if len(room.subs) == 0 {
h.clearCompactionLocked(room)
delete(h.rooms, sessionID)
room.cancel()
// ReleaseShells is a local identity-cache operation (no broker I/O).
// Complete it before a reconnect can acquire this session's new room.
if releaser, ok := room.broker.(chatShellReleaser); ok {
releaser.ReleaseShells(sessionID)
}
if len(room.subs) == 0 && len(room.queue) == 0 {
h.releaseRoomLocked(room)
}
h.mu.Unlock()
}
// releaseRoomLocked destroys an unwatched, idle room. Callers hold h.mu.
func (h *chatHub) releaseRoomLocked(room *chatRoom) {
if h.rooms[room.sessionID] != room {
return
}
h.clearCompactionLocked(room)
delete(h.rooms, room.sessionID)
room.cancel()
// ReleaseShells is a local identity-cache operation (no broker I/O).
// Complete it before a reconnect can acquire this session's new room.
if releaser, ok := room.broker.(chatShellReleaser); ok {
releaser.ReleaseShells(room.sessionID)
}
}
// detachClient drops every chat subscription one connection holds; the read loop
// calls it when the connection ends so no subscriber goroutine outlives it.
func (h *chatHub) detachClient(clientID string) {
@@ -530,6 +555,8 @@ func (h *chatHub) finishTurnLocked(room *chatRoom) {
close(room.turnDone)
room.turnDone = nil
}
// A queue held behind a turn this room only observed advances now.
signalWake(room.wake)
}
// turnInFlight reports whether sessionID has a turn in flight, so chat_close
@@ -576,32 +603,6 @@ func (h *chatHub) flushBuffered(sessionID string) {
}
}
// submitPrompt accepts one prompt: it starts the turn runner immediately when no
// turn is in flight, or queues it (bounded) when one is. It returns whether the
// prompt was queued or refused.
func (h *chatHub) submitPrompt(sessionID string, p *chatQueuedPrompt) (queued bool, refused bool) {
h.mu.Lock()
room := h.roomLocked(sessionID)
if room.running || len(room.queue) > 0 {
if len(room.queue) >= chatRoomQueueLimit {
h.mu.Unlock()
return false, true
}
// The client is acknowledged as queued by the handler, so the turn runner
// must not acknowledge the same request again once it runs. The write is
// still kept so a later broker failure reaches the client that sent it.
p.acked = true
room.queue = append(room.queue, p)
signalWake(room.wake)
h.mu.Unlock()
return true, false
}
room.queue = append(room.queue, p)
signalWake(room.wake)
h.mu.Unlock()
return false, false
}
// turnRunning reports whether the session has a turn in flight. A client that
// subscribes after a turn started (for example the operator's browser while the
// CLI harness is working) needs this to show Stop and the live activity line;
@@ -707,30 +708,68 @@ func (h *chatHub) runRoom(room *chatRoom) {
return
}
h.runTurn(room, p, done)
// The last client may have left while this turn ran; an unwatched room
// stays only while it still has prompts to deliver.
h.mu.Lock()
if len(room.subs) == 0 && !room.running && len(room.queue) == 0 {
h.releaseRoomLocked(room)
}
h.mu.Unlock()
}
}
// nextPrompt pops the next queued prompt, blocking until one arrives or the room
// is released. It marks the room running and creates the turn's completion
// channel so a stale signal from a previous turn can never end the next one.
// is released. A queued prompt also waits while the harness is still running a
// turn this room did not start (one announced by the stream, or one a harness
// reports in flight after a reload or restart). It marks the room running and
// creates the turn's completion channel so a stale signal from a previous turn
// can never end the next one.
func (h *chatHub) nextPrompt(room *chatRoom) (*chatQueuedPrompt, chan struct{}, bool) {
for {
h.mu.Lock()
if len(room.queue) > 0 {
p := room.queue[0]
room.queue = room.queue[1:]
room.running = true
room.nativeTurnID = ""
room.turnDone = make(chan struct{})
done := room.turnDone
if room.ctx.Err() != nil {
h.mu.Unlock()
return p, done, true
return nil, nil, false
}
var wait time.Duration
if len(room.queue) > 0 {
switch {
case room.observedTurn:
wait = chatQueueBusyPoll
case time.Until(room.notBefore) > 0:
wait = time.Until(room.notBefore)
default:
broker := room.broker
h.mu.Unlock()
busy := brokerTurnRunning(broker, room.sessionID)
h.mu.Lock()
if busy {
wait = chatQueueBusyPoll
} else if len(room.queue) > 0 && !room.observedTurn && room.ctx.Err() == nil {
p := room.queue[0]
room.queue = room.queue[1:]
room.running = true
room.nativeTurnID = ""
room.turnDone = make(chan struct{})
done := room.turnDone
h.mu.Unlock()
return p, done, true
} else {
h.mu.Unlock()
continue
}
}
}
h.mu.Unlock()
var timer <-chan time.Time
if wait > 0 {
timer = time.After(wait)
}
select {
case <-room.ctx.Done():
return nil, nil, false
case <-room.wake:
case <-timer:
}
}
}
@@ -751,6 +790,7 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{
if broker == nil {
h.failPrompt(room, p, errors.New("chat is not configured on this server"))
h.endRoomTurn(room)
return
}
if first {
@@ -760,6 +800,7 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{
return
}
}
h.publishDelivery(room, p)
var promptErr error
if withFiles, ok := broker.(chatPromptWithFiles); ok && len(p.files) > 0 {
promptErr = withFiles.PromptWithMessageIDFiles(room.ctx, room.sessionID, p.text, p.nativeID, p.files)
@@ -781,6 +822,13 @@ func (h *chatHub) runTurn(room *chatRoom, p *chatQueuedPrompt, done chan struct{
if p.write != nil && !p.acked {
_ = p.write(protocol.Message{Type: protocol.ChatPrompt, ID: p.requestID})
}
if p.acked {
// The harness has it now: drop the durable row and tell every client the
// queued prompt was sent.
h.rememberOp(room.sessionID, p.operationID)
h.forgetQueued(room.sessionID, p)
h.fanoutQueue(room.sessionID, protocol.ChatQueueFrame{Delivered: p.operationID})
}
h.flushBuffered(room.sessionID)
// Start the approval poller once, after the first ack, so a pending request
// is surfaced after the prompt is acknowledged and shared with every client.
@@ -804,13 +852,51 @@ func (h *chatHub) endRoomTurn(room *chatRoom) {
// that never reached the agent is visible on the device that sent it rather than
// leaving its composer waiting. It is reported for a queued prompt too, whose
// success ack was already sent but whose delivery may still fail.
//
// A queued prompt's durable row is dropped (the harness refused it, so it is
// not retried blindly) and every attached client is told through chat_queue,
// including a client that attached after the sender left.
func (h *chatHub) failPrompt(room *chatRoom, p *chatQueuedPrompt, err error) {
if p.write == nil || err == nil {
if err == nil {
return
}
h.forgetOp(room.sessionID, p.operationID)
if p.acked {
h.forgetQueued(room.sessionID, p)
h.fanoutQueue(room.sessionID, protocol.ChatQueueFrame{Failed: p.operationID, Error: err.Error()})
}
if p.write == nil {
return
}
_ = p.write(protocol.Message{Type: protocol.Error, ID: p.requestID, Error: err.Error()})
}
// publishDelivery re-publishes a queued prompt's user turn as it is handed to
// the harness, so a client that attached after it was queued (a reload, or the
// sender's other device) sees it enter the transcript before the reply. Clients
// that already hold it recognise the same message and operation ids.
func (h *chatHub) publishDelivery(room *chatRoom, p *chatQueuedPrompt) {
if !p.acked || p.messageID == "" {
return
}
frame := protocol.ChatEventFrame{
SessionID: room.sessionID,
MessageID: p.messageID,
Kind: protocol.ChatEventUser,
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: p.text},
Attachments: p.parts,
Client: p.client,
OperationID: p.operationID,
}
h.mu.Lock()
turn := h.turnLocked(room.sessionID)
if _, retained := turn.messages[p.messageID]; !retained {
h.applyLocked(frame)
}
h.mu.Unlock()
h.fanout(room.sessionID, protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}, "")
}
// roomBroker returns the broker bound to the room.
func (h *chatHub) roomBroker(room *chatRoom) chatBroker {
h.mu.Lock()
+36 -7
View File
@@ -59,6 +59,18 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
return
}
}
operationID := prompt.OperationID
if operationID == "" {
// Every server-held prompt is addressable (chat_unqueue) and unique in
// its session's durable queue, so mint an id for a client that sent none.
operationID = "srv-" + strings.TrimPrefix(chatClientToken(), "conn-")
} else if duplicate, queued := hub.duplicateOp(prompt.SessionID, operationID); duplicate {
// A retry of a prompt this session already accepted: acknowledge it the
// same way again instead of delivering it a second time.
hub.attach(prompt.SessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: queued})
return
}
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
@@ -85,29 +97,43 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
Attachments: attachmentParts,
Client: sub.label,
OperationID: prompt.OperationID,
OperationID: operationID,
}
if userFrame.MessageID == "" {
userFrame.MessageID = hub.nextUserMessageID()
}
queued, refused := hub.submitPrompt(sessionID, &chatQueuedPrompt{
queuedPrompt := &chatQueuedPrompt{
text: prompt.Text,
files: attachmentFiles,
operationID: prompt.OperationID,
operationID: operationID,
// Only the caller's real OpenCode message id is forwarded. The hub's own
// `usr_` display id is not a valid OpenCode `msg_` id, so a client that
// omitted native_message_id lets OpenCode generate one instead.
nativeID: prompt.NativeMessageID,
messageID: userFrame.MessageID,
client: sub.label,
parts: attachmentParts,
requestID: request.ID,
write: write,
})
if refused {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " already has the maximum number of prompts queued"})
}
// Claim the operation before admission so a concurrent retry of the same
// prompt is acknowledged rather than delivered twice.
hub.rememberOp(sessionID, operationID)
admission, admitErr := hub.admitPrompt(ctx, sessionID, broker, queuedPrompt)
if admission == chatAdmitRefused {
hub.forgetOp(sessionID, operationID)
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: admitErr.Error()})
return
}
accepted = true
queued := admission == chatAdmitQueued
// Publish the user turn to every other client; the sender already has it.
hub.broadcastUser(userFrame, clientID)
if admission == chatAdmitSteered {
// The harness took the message into its running turn: it is sent, not
// queued, and the turn already in flight carries its reply.
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID})
}
if queued {
// The client that sent it is told the prompt is waiting for the in-flight
// turn, so it shows queued/steering instead of blocking or silently
@@ -200,7 +226,10 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
}
// Acknowledge before the stream is allowed to fan out: otherwise an immediately
// available retained shell can overtake the subscribe reply on this connection.
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID})
// The reply carries the prompts the server still holds for the session, so
// a reloaded client draws them as Queued without any device storage.
queue := hub.queueFrame(query.SessionID)
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue})
if err := hub.ensureObserving(sub.room, broker); err != nil {
sub.close()
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not observe session " + query.SessionID})
+8
View File
@@ -118,6 +118,14 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
settingsStore = stores[0]
}
hub.settings = settingsStore
if len(stores) > 0 {
// Prompts queued behind a running turn are durable and owned by their
// session; resume delivering any a previous process left queued.
hub.queueStore = stores[0]
if options.Chat != nil {
go hub.resumeQueued(ctx, options.Chat)
}
}
acks := newAttentionAckHub()
var ackStore attentionAckStore
var notes *appNotifier