Files
pipelines/internal/server/chat.go
T

610 lines
21 KiB
Go

package server
import (
"context"
"errors"
"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)
// Permissions lists the session's pending OpenCode approval requests so the
// handler can push chat_permission frames.
Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error)
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
}
// chatPermissionPollInterval is the fixed interval between permission polls: one
// GET per poll through the shared Connection bounds, never a busy loop. The first
// poll runs immediately so a request pending when the prompt is sent is surfaced
// without waiting a full interval.
const chatPermissionPollInterval = 2 * time.Second
// chatPermissionRetainedLimit bounds the dedup set of already-pushed permission
// request ids one session retains, so a long turn cannot grow it without limit.
const chatPermissionRetainedLimit = 256
// 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
// permissions holds the request ids already pushed to the client so a
// repeated poll cannot push the same pending request twice; permissionOrder
// bounds it to chatPermissionRetainedLimit.
permissions map[string]struct{}
permissionOrder []string
}
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.turnLocked(frame.SessionID)
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)
}
}
// turnLocked returns the session's retained turn, creating it (and evicting the
// least-recently-updated turn first when the cap is reached) and refreshing its
// update time. It is the shared turn-initialization path for apply and
// seenPermission, so the two cannot drift. Callers must hold h.mu.
func (h *chatHub) turnLocked(sessionID string) *chatTurn {
turn := h.turns[sessionID]
if turn == nil {
h.evictLocked()
turn = &chatTurn{sessionID: sessionID, messages: map[string]*chatMessage{}}
h.turns[sessionID] = turn
}
turn.updated = h.clock()
return turn
}
// 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
}
// seenPermission records requestID as pushed for the session and reports whether
// it is newly seen (true) or a duplicate (false). The retained ids are bounded:
// the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so
// the same pending request is pushed at most once while the state stays bounded.
func (h *chatHub) seenPermission(sessionID, requestID string) bool {
if sessionID == "" || requestID == "" {
return false
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turnLocked(sessionID)
if turn.permissions == nil {
turn.permissions = map[string]struct{}{}
}
if _, ok := turn.permissions[requestID]; ok {
return false
}
turn.permissions[requestID] = struct{}{}
turn.permissionOrder = append(turn.permissionOrder, requestID)
if len(turn.permissionOrder) > chatPermissionRetainedLimit {
oldest := turn.permissionOrder[0]
turn.permissionOrder = turn.permissionOrder[1:]
delete(turn.permissions, oldest)
}
return true
}
// forgetPermission releases a request id reserved by seenPermission when its
// frame could not be written, so a later poll re-pushes it instead of the id
// staying "seen" while the phone never received the request.
func (h *chatHub) forgetPermission(sessionID, requestID string) {
if sessionID == "" || requestID == "" {
return
}
h.mu.Lock()
defer h.mu.Unlock()
turn := h.turns[sessionID]
if turn == nil {
return
}
delete(turn.permissions, requestID)
for i, id := range turn.permissionOrder {
if id == requestID {
turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...)
break
}
}
}
// 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)
// Poll for pending approval requests alongside the event stream, sharing
// streamCtx so the poller stops when the stream ends or the client
// disconnects. The first poll runs immediately.
go pollChatPermissions(streamCtx, broker, hub, sessionID, write)
}
// pollChatPermissions polls the broker's pending-approval list on a fixed
// interval, sharing the chat subscription's context so it stops on disconnect or
// when the turn's stream ends. The hub dedups the request ids, so each pending
// request is pushed at most once; a poll failure is surfaced once through the
// same write closure instead of staying silent. A backend that does not expose
// the approval surface (opencode.ErrPermissionUnsupported) stops the poller
// quietly, so the documented limitation is not reported as a per-prompt error.
func pollChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) {
ticker := time.NewTicker(chatPermissionPollInterval)
defer ticker.Stop()
reported := false
for {
if err := pushChatPermissions(ctx, broker, hub, sessionID, write); err != nil {
if ctx.Err() != nil {
return
}
if errors.Is(err, opencode.ErrPermissionUnsupported) {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
return
}
if reported {
log.Printf("chat permission poll session=%s: %v", sessionID, err)
} else {
reported = true
_ = write(protocol.Message{Type: protocol.Error, Error: "chat permission poll: " + err.Error()})
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// pushChatPermissions performs one poll and writes a chat_permission frame for
// every newly seen request of the session, through the same connection-scoped
// write closure as the chat stream. It never re-pushes a request id, and releases
// the reservation on a write failure so the frame is not silently dropped.
func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) error {
pending, err := broker.Permissions(ctx, sessionID)
if err != nil {
return err
}
for _, request := range pending {
if request.SessionID != sessionID || request.RequestID == "" {
continue
}
if !hub.seenPermission(sessionID, request.RequestID) {
continue
}
frame := request
if err := write(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame}); err != nil {
hub.forgetPermission(sessionID, request.RequestID)
return err
}
}
return nil
}
// 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 strings.TrimSpace(reply.RequestID) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"})
return
}
switch reply.Decision {
case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject:
default:
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision 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})
}