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}) }