Files
pipelines/internal/server/chat_hub.go
T
alan c1652e2bc6 Split chat/model/run files by concern (pure move)
Pure move: split the three multi-feature server files into per-concern files,
with no behaviour change - same package, same exported and unexported
identifiers, same call graph, same tests.

- internal/server/chat.go -> chat.go (broker seam, client token, dispatcher),
  chat_hub.go, chat_claims.go, chat_permissions.go, chat_sessions.go,
  chat_stream.go, chat_commands.go
- internal/stage/model.go -> model.go, model_run.go, model_failure.go,
  model_validation.go
- internal/pipeline/runs.go -> runs.go (read side), runs_schema.go,
  runs_create.go, runs_mutate.go, runs_sessions.go

Checked before committing:
- Packaged templates and internal/author/skill/references: neither refers to
  internal/server/chat.go, internal/stage/model.go or internal/pipeline/runs.go,
  so nothing there needed updating (the change is a move with no behaviour
  change).
- gofmt -l on the three packages: clean. go build ./..., go vet ./... and
  go test ./... pass. go.mod/go.sum unchanged.
- No test, docs, android/, install or Docker files touched.

Known stale path pointers deliberately left to the single docs writer (shared
docs) and out of scope (android/): docs/chat-verification.md and
android/README.md name the old file paths.
2026-10-17 21:09:08 +01:00

269 lines
7.9 KiB
Go

package server
import (
"log"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/opencode"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// 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
// claims maps a session to the client currently writing it so a second
// client is rejected; claimOrder bounds the map like chatMaxTurns.
claims map[string]string
claimOrder []string
}
// 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
}