Files
pipelines/internal/server/chat_stream.go
T
alan 686690f7b4 Server: serve a Claude chat's history page from a per-file line index
Reading a history page parsed the whole session file (tens of MB) on every
chat_messages. A per-file index now records which lines build each shown
message; a page re-reads and replays only its own messages' lines, and the
index follows appends by reading only the new bytes. Results match the
whole-file read (compared page by page on synthetic transcripts); shapes the
index cannot replay exactly fall back to the whole-file read.

chat_subscribe may ask (history: true) for the newest page in its reply,
read once the subscription is attached, saving the client a round trip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:58:57 +00:00

320 lines
14 KiB
Go

package server
import (
"context"
"log"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// 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 a turn starts is surfaced
// without waiting a full interval.
const chatPermissionPollInterval = 2 * time.Second
// handleChatPrompt accepts a prompt from any client. It never rejects a prompt
// because another client is watching the session: the client attaches to the
// session's shared room and the prompt either starts a turn or is queued behind
// the one in flight, with the ack saying which. The prompt is echoed live to
// every other client, attributed to the sending device's label, and the turn's
// output is fanned out to all of them.
func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
admitChatPrompt(ctx, request, broker, hub, registry, clientID, write, false)
}
// admitChatPrompt is handleChatPrompt; echoSender also publishes the user turn
// to the sending client, for a prompt the server composed (chat_shell_share)
// that the sender has no local copy of.
func admitChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error, echoSender bool) {
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 !validChatSessionID(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
}
if len(prompt.Attachments) > 0 {
var err error
if checker, ok := broker.(interface {
CheckAttachments(context.Context, string) error
}); ok {
err = checker.CheckAttachments(ctx, prompt.SessionID)
} else if _, ok := broker.(chatPromptWithFiles); !ok {
err = errChatAttachmentsUnsupported
}
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
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, steered := 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, ChatSteered: steered})
return
} else if hub.autoContinueSuperseded(prompt.SessionID, operationID) {
// The server already continued this refusal on another Claude account:
// acknowledge the auto-continue and take it straight back out, as if
// another device had unchecked it.
hub.attach(prompt.SessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: true})
hub.fanoutQueue(prompt.SessionID, protocol.ChatQueueFrame{Removed: operationID})
return
}
attachmentFiles, attachmentParts, err := prepareChatAttachments(prompt.SessionID, prompt.Attachments)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
accepted := false
defer func() {
if !accepted {
releaseChatAttachments(attachmentFiles)
}
}()
sessionID := prompt.SessionID
sub := hub.attach(sessionID, clientID, sanitizeChatClientLabel(prompt.Client), broker, write)
if sub == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + sessionID})
return
}
// The user turn every *other* client sees, carrying the sender's label. The
// sender's own client appended it locally, so it is excluded below.
userFrame := protocol.ChatEventFrame{
SessionID: sessionID,
MessageID: prompt.NativeMessageID,
Kind: protocol.ChatEventUser,
Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "usr", Text: prompt.Text},
Attachments: attachmentParts,
Client: sub.label,
OperationID: operationID,
}
if userFrame.MessageID == "" {
userFrame.MessageID = hub.nextUserMessageID()
}
queuedPrompt := &chatQueuedPrompt{
text: prompt.Text,
files: attachmentFiles,
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 prompt.SendAt > 0 {
// Only a future time schedules; a past one sends exactly as an
// unscheduled prompt.
if at := time.UnixMilli(prompt.SendAt); at.After(hub.clock()) {
queuedPrompt.sendAt = at
}
}
// 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.
exceptClientID := clientID
if echoSender {
exceptClientID = ""
}
hub.broadcastUser(userFrame, exceptClientID)
if admission == chatAdmitSteered {
hub.markOpSteered(sessionID, operationID)
// The harness took the message into its running turn: it is not queued,
// and the turn already in flight carries its reply.
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatSteered: true})
}
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
// dropping the message. The turn runner acknowledges it once it runs, so
// clear the ack on the queued entry to avoid a second ack for the same id.
_ = write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID, ChatQueued: true})
}
if registry != nil {
// Recency is one UPDATE: it moves the operator session to the front of
// chat_sessions. A session id that is not an operator session updates no
// row and is not an error, so pipeline stage prompts are unaffected.
if err := registry.TouchOperatorSession(ctx, sessionID); err != nil {
// Best-effort; the turn runner also touches on delivery.
_ = err
}
}
}
// handleChatModels lists the models a session's harness can switch to.
func handleChatModels(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
query := request.ChatQuery
if query == nil || !validChatSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
modeler, ok := broker.(chatModeler)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat harness does not support changing models"})
return
}
models, err := modeler.Models(ctx, query.SessionID)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
_ = write(protocol.Message{Type: protocol.ChatModels, ID: request.ID, ChatModels: models, EffortRequest: chatModelsEffort(ctx, broker, query.SessionID)})
}
// handleChatModelSwitch changes one session's model for subsequent turns.
func handleChatModelSwitch(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
body := request.ModelRequest
if body == nil || !validChatSessionID(body.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if strings.TrimSpace(body.Provider) == "" || strings.TrimSpace(body.Model) == "" {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a model and provider are required"})
return
}
modeler, ok := broker.(chatModeler)
if !ok {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "this chat harness does not support changing models"})
return
}
if err := modeler.SwitchModel(ctx, body.SessionID, body.Provider, body.Model); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
reply := *body
reply.Selection = ""
if reporter, ok := broker.(interface {
ModelSelectionMode(context.Context, string) string
}); ok {
reply.Selection = reporter.ModelSelectionMode(ctx, body.SessionID)
}
_ = write(protocol.Message{Type: protocol.ChatModelSelect, ID: request.ID, ModelRequest: &reply})
}
// handleChatSubscribe attaches one connection to a session's shared stream so it
// receives every frame any client's turn produces, its own or another device's.
// It replays the session's still-pending approval requests, if the room holds
// any, so a device that joins late still shows the cards.
func handleChatSubscribe(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
// The wire body of chat_subscribe is the chat_subscribe field (the app's
// subscribeRequest and docs/cli.md both use it), not the chat_query body the
// other session-scoped commands carry.
query := request.ChatSubscribe
if query == nil || !validChatSessionID(query.SessionID) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"})
return
}
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
// A turn already in flight when this client attaches has no `step started`
// frame in its future, so attaching queues the live marker for it. The
// composer's Stop control and the transcript's one activity row read it;
// the later `step ended` frame settles them normally. The harness is asked
// off the hub mutex; the room's own state is read as the client attaches.
reporter, ok := broker.(chatTurnReporter)
busy := ok && reporter.TurnRunning(query.SessionID)
sub, fresh := hub.attachSub(query.SessionID, clientID, "", broker, write, true, busy)
if sub == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "could not attach to session " + query.SessionID})
return
}
// Acknowledge before the stream is allowed to fan out: otherwise an immediately
// available retained shell (or the live-turn marker, or a frame of a turn
// another client starts) can overtake the subscribe reply on this
// connection. 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)
reply := protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)}
if query.History {
// Read after attaching, as a chat_messages sent after this reply would
// be: a frame produced meanwhile waits in the subscription's queue.
// A failed read leaves the page out; the client then asks for it with
// chat_messages, which reports the error.
history := protocol.Message{}
if err := addChatHistory(ctx, &history, broker, hub, protocol.ChatQuery{SessionID: query.SessionID, Limit: query.Limit}); err == nil {
reply.ChatMessages, reply.ChatHistory, reply.ChatTurnDiffList = history.ChatMessages, history.ChatHistory, history.ChatTurnDiffList
} else {
log.Printf("chat session %s: history for the subscribe reply: %v", query.SessionID, err)
}
}
_ = write(reply)
if fresh {
go sub.run()
}
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})
return
}
// Observe requests from CLI-originated turns and recover after reload; do
// not require the browser to send another prompt just to start polling.
hub.startRoomPoller(sub.room, broker)
// Replay every still-unresolved approval request the room holds, not only the
// most recent one, so a client that attaches after two requests were raised
// still sees both cards.
for _, pending := range hub.pendingFrames(query.SessionID) {
frame := pending
sub.send(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame})
}
// Replay a question/form the agent raised before this client attached, so a
// reload or another device still sees the paused tool call.
for _, form := range hub.pendingForms(query.SessionID) {
pending := form
sub.send(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &protocol.ChatEventFrame{
SessionID: query.SessionID,
Kind: protocol.ChatEventForm,
Form: &pending,
}})
}
// Replay the background processes still running, so a device that attaches
// after they started (a reconnect, or a chat opened from another device)
// shows them rather than nothing until they exit. The message snapshot cannot
// carry a shell — a shell outlives the turn and is not a turn — so this is the
// shell half of the resume.
hub.replayShells(sub)
hub.replayCompaction(sub)
hub.replayUsage(sub)
}