package server import ( "context" "log" "strings" "sync" "time" "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/protocol" ) // chatBroker is the server-side OpenCode chat seam. *opencode.Chat implements // it; tests substitute a scripted broker. Keeping it an interface means the // /connect handler never builds a second OpenCode transport. type chatBroker interface { Prompt(ctx context.Context, sessionID, text string) error // Stream subscribes to the session's OpenCode event stream. ready is called // once the subscription is attached, before any frame is read, so the caller // can order the prompt after the subscription. Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error Interrupt(ctx context.Context, sessionID string) error Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) Reply(ctx context.Context, request protocol.ChatReplyRequest) error } // chatRetainedLimit bounds the state one session may retain so a stalled reader // or a runaway turn cannot grow memory without limit. It matches the OpenCode // response bound (1 MiB). const chatRetainedLimit = opencode.MaxResponse // chatMaxTurns bounds how many sessions retain turn state. A long-lived server // cannot accumulate state for every session ever prompted, so the // least-recently-updated turn is evicted once the cap is exceeded. const chatMaxTurns = 128 // chatHub retains the accumulated turn state per session, independently of any // single connection, so a reconnecting client can re-sync what it missed. State // is keyed by session and then by message id (a session holds many turns), and // is capped at chatRetainedLimit. type chatHub struct { mu sync.Mutex turns map[string]*chatTurn now func() time.Time } // chatPart is the retained form of one part. Text and reasoning deltas append to // text (amortized O(1)); a tool frame replaces part outright. type chatPart struct { part protocol.ChatPart text []byte } // chatMessage is one retained assistant turn. Its parts keep the exact ids the // supporting chat_event frames carried, so a resume preserves attribution. type chatMessage struct { id string order []string parts map[string]*chatPart } type chatTurn struct { sessionID string order []string messages map[string]*chatMessage step *protocol.ChatStep usage *protocol.ChatUsage size int overflow bool updated time.Time } func newChatHub() *chatHub { return &chatHub{turns: map[string]*chatTurn{}, now: time.Now} } func (h *chatHub) clock() time.Time { if h.now != nil { return h.now() } return time.Now() } // apply folds one pushed frame into the retained state. Text and reasoning // deltas append to their message's part; a tool frame upserts the full part // (accounting for its whole size) so the latest lifecycle status, input, // content, metadata, error and time are retained under the frame's message id. // Step and usage frames update the session's latest progress. func (h *chatHub) apply(frame protocol.ChatEventFrame) { if frame.SessionID == "" { return } h.mu.Lock() defer h.mu.Unlock() turn := h.turns[frame.SessionID] if turn == nil { h.evictLocked() turn = &chatTurn{sessionID: frame.SessionID, messages: map[string]*chatMessage{}} h.turns[frame.SessionID] = turn } turn.updated = h.clock() switch frame.Kind { case protocol.ChatEventStep: turn.step = frame.Step return case protocol.ChatEventUsage: turn.usage = frame.Usage return } if frame.MessageID == "" { return } message := turn.messages[frame.MessageID] if message == nil { message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}} turn.order = append(turn.order, frame.MessageID) turn.messages[frame.MessageID] = message } switch frame.Kind { case protocol.ChatEventText, protocol.ChatEventReasoning: id := "" if frame.Part != nil { id = frame.Part.ID } if id == "" || frame.Delta == "" { return } if !turn.retain(len(frame.Delta)) { return } part, ok := message.parts[id] if !ok { part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}} message.order = append(message.order, id) message.parts[id] = part } part.text = append(part.text, frame.Delta...) case protocol.ChatEventTool: if frame.Part == nil || frame.Part.ID == "" || turn.overflow { return } // The tool part is retained whole, so size the complete part (input, // content, metadata, error and time) and charge the difference against // the cap on every upsert, not just the first-seen name. incoming := chatPartBytes(frame.Part) part, ok := message.parts[frame.Part.ID] if !ok { if !turn.retain(incoming) { return } part = &chatPart{} message.order = append(message.order, frame.Part.ID) message.parts[frame.Part.ID] = part } else if previous := chatPartBytes(&part.part); incoming > previous { if !turn.retain(incoming - previous) { return } } else { turn.shrink(previous - incoming) } part.part = *frame.Part } } // chatPartBytes estimates the retained size of a part so the per-session cap // accounts for its whole state instead of only the first-seen name. It walks the // fields rather than re-marshalling, so the cost matches the bytes it stores. func chatPartBytes(part *protocol.ChatPart) int { if part == nil { return 0 } n := len(part.Type) + len(part.ID) + len(part.Name) if part.State == nil { return n } n += len(part.State.Status) + len(part.State.Input) for _, content := range part.State.Content { n += len(content.Type) + len(content.Text) + len(content.URI) + len(content.Mime) + len(content.Name) } if part.State.Error != nil { n += len(part.State.Error.Type) + len(part.State.Error.Message) } for key, value := range part.State.Metadata { n += len(key) if text, ok := value.(string); ok { n += len(text) } else { n += 8 } } return n } // evictLocked drops the least-recently-updated turn when the session cap is // reached, keeping retained memory bounded across session churn. func (h *chatHub) evictLocked() { if len(h.turns) < chatMaxTurns { return } oldestID := "" var oldest time.Time for id, turn := range h.turns { if oldestID == "" || turn.updated.Before(oldest) { oldestID, oldest = id, turn.updated } } if oldestID != "" { delete(h.turns, oldestID) } } // retain records n more bytes and reports whether the turn is still within the // bound. The first overflow records one diagnostic and then drops further state // instead of growing without limit. func (t *chatTurn) retain(n int) bool { if t.overflow { return false } if t.size+n > chatRetainedLimit { t.overflow = true log.Printf("chat session %s retained state exceeds %d bytes; dropping further state", t.sessionID, chatRetainedLimit) return false } t.size += n return true } // shrink releases n bytes when a tool upsert replaces a larger retained part. func (t *chatTurn) shrink(n int) { t.size -= n if t.size < 0 { t.size = 0 } } // snapshot returns the accumulated session as one assistant message per // retained message id, with the same stable part ids, or nil when nothing is // retained. It is the resume source: a reconnecting client re-syncs the whole // session once instead of replaying deltas, and each turn keeps its own id. func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage { h.mu.Lock() defer h.mu.Unlock() turn := h.turns[sessionID] if turn == nil { return nil } turn.updated = h.clock() var messages []protocol.ChatMessage for _, id := range turn.order { message := turn.messages[id] if message == nil || len(message.order) == 0 { continue } out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant} for _, partID := range message.order { if part, ok := message.parts[partID]; ok { piece := part.part piece.Text = string(part.text) out.Parts = append(out.Parts, piece) } } messages = append(messages, out) } return messages } // handleChat dispatches one chat_* request. It returns true when the request was // a chat type and has been fully answered (the caller must not fall through to // the generic response). Frames are pushed after the ack, each through the // connection-scoped write function so the read loop and the stream goroutine // never interleave partial websocket writes. func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) bool { switch request.Type { case protocol.ChatPrompt: handleChatPrompt(ctx, request, broker, hub, write) return true case protocol.ChatMessages: handleChatMessages(ctx, request, broker, hub, write) return true case protocol.ChatInterrupt: handleChatSimple(ctx, request, broker, hub, write) return true case protocol.ChatReply: handleChatSimple(ctx, request, broker, hub, write) return true case protocol.ChatSessions, protocol.ChatCreate: // Declared in the protocol for the client but not yet brokered. Answer // explicitly so the client never mistakes the generic Pong for a chat // reply, and the boundary stays visible instead of silently dropping. _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_sessions and chat_create are not yet brokered"}) return true } return false } func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { if broker == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"}) return } prompt := request.ChatPrompt if prompt == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"}) return } if !opencode.ValidSessionID(prompt.SessionID) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) return } if strings.TrimSpace(prompt.Text) == "" { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"}) return } // Reject an oversized prompt before any OpenCode call; the bound matches the // OpenCode request limit. if len(prompt.Text) > opencode.MaxRequest { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"}) return } // Subscribe to the OpenCode firehose before posting the prompt: the stream // has no replay cursor (research 2.4), so a delta emitted between the POST // and the subscription would otherwise be lost. Frames that arrive before the // ack is written are buffered and flushed in order after it. sessionID := prompt.SessionID streamCtx, streamCancel := context.WithCancel(ctx) attached := make(chan struct{}) var attachOnce sync.Once streamResult := make(chan error, 1) var bufferMu sync.Mutex buffering := true var buffered []protocol.ChatEventFrame go func() { streamResult <- broker.Stream(streamCtx, sessionID, func() { attachOnce.Do(func() { close(attached) }) }, func(frame protocol.ChatEventFrame) error { bufferMu.Lock() if buffering { buffered = append(buffered, frame) bufferMu.Unlock() return nil } bufferMu.Unlock() hub.apply(frame) return write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) }) }() select { case <-attached: case err := <-streamResult: streamCancel() if err != nil && ctx.Err() == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()}) } return case <-ctx.Done(): streamCancel() return } if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil { streamCancel() _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } // Acknowledge the accepted prompt with the echoed request id, then stream the // reply. The prompt is sent exactly once and never re-sent on resume. if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil { streamCancel() return } // Flush frames buffered while the prompt was accepted, then resume live // delivery. Holding bufferMu preserves order: the stream goroutine either // buffered before this flush or waits and writes after it. bufferMu.Lock() buffering = false pending := buffered buffered = nil for _, frame := range pending { hub.apply(frame) if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil { bufferMu.Unlock() streamCancel() return } } bufferMu.Unlock() go drainChatStream(ctx, streamResult, streamCancel, sessionID, write) } // drainChatStream reports the terminal error of a chat subscription and releases // its context once the stream has ended. func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, sessionID string, write func(protocol.Message) error) { defer cancel() if err := <-result; err != nil && ctx.Err() == nil { log.Printf("chat stream session=%s: %v", sessionID, err) _ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()}) } } func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { query := request.ChatQuery if query == nil || !opencode.ValidSessionID(query.SessionID) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) return } response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID} // Resume from retained server state first: a reconnecting client gets the // accumulated turns with the same stable ids without re-reading OpenCode. if messages := hub.snapshot(query.SessionID); len(messages) > 0 { response.ChatMessages = messages _ = write(response) return } if broker == nil { _ = write(response) return } messages, err := broker.Messages(ctx, query.SessionID, query.Limit) if err != nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } response.ChatMessages = messages _ = write(response) } func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { if broker == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"}) return } if request.Type == protocol.ChatInterrupt { query := request.ChatQuery if query == nil || !opencode.ValidSessionID(query.SessionID) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) return } if err := broker.Interrupt(ctx, query.SessionID); err != nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } _ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID}) return } reply := request.ChatReply if reply == nil || !opencode.ValidSessionID(reply.SessionID) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) return } if err := broker.Reply(ctx, *reply); err != nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } _ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID}) }