Files
pipelines/internal/server/chat_session_changes.go
T
alan 4f8e99f316 Serialize open-chat sync writes by server revision and fan out prunes
Server: chat_open_workspace is a compare-and-swap on base_revision; a stale
write is refused and answered with the current snapshot (rejected: true).
Each stored change increments the revision; identical writes store nothing.
A request with no body reads the set. Seeds apply only to a never-stored set.
Validation uses the operator registry (recorded, not closed). chat_close and
connection-time pruning store a new revision and push it to every
connection; otherwise a new connection gets the current set to catch up.

App: ChatOpenWorkspaceController keeps one write in flight, coalesces later
changes, sends from the last replied revision and ignores lower revisions;
pushes and rejected replies win over unsent changes. It starts after the
local layout restores (seed or read), keeps a deep-linked chat, and is
disposed on server/gateway rebind. Remote membership is applied by
reconcileChatWorkspaceTree, keeping pane ids and split sizes. Federated
lists keep their local layout.

Tests: Go store/handler fan-out tests; app controller, reconcile and screen
sync tests. Notes record a live two-client scratch check.
2026-11-12 03:23:22 +00:00

110 lines
3.5 KiB
Go

package server
import (
"context"
"sync"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatSessionChanges fans a chat's shared list metadata (colour, star, title)
// out to every open connection, so a change on one device updates the chat
// list on the others live. Unlike a chat room it is not keyed by session: every
// connection shows the list. It holds each connection's serialized write
// function, never the socket. The zero value is ready to use.
type chatSessionChanges struct {
mu sync.Mutex
clients map[string]func(protocol.Message) error
}
func (c *chatSessionChanges) attach(clientID string, write func(protocol.Message) error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.clients == nil {
c.clients = map[string]func(protocol.Message) error{}
}
c.clients[clientID] = write
}
func (c *chatSessionChanges) detach(clientID string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.clients, clientID)
}
// changes returns the hub's connection fan-out; nil for a nil hub, which
// broadcasts nothing.
func (h *chatHub) changes() *chatSessionChanges {
if h == nil {
return nil
}
return &h.sessionChanges
}
// broadcast pushes the updated row to every connection except the sender,
// which already has it in its reply. Each write runs on its own goroutine
// (bounded by the connection's write timeout), so a slow client cannot delay
// the sender or the others.
func (c *chatSessionChanges) broadcast(sender string, session protocol.ChatSession) {
c.mu.Lock()
targets := make([]func(protocol.Message) error, 0, len(c.clients))
for id, write := range c.clients {
if id != sender {
targets = append(targets, write)
}
}
c.mu.Unlock()
for _, write := range targets {
row := session
go func() {
_ = write(protocol.Message{Type: protocol.ChatSessionChanged, ChatSession: &row})
}()
}
}
// broadcastWorkspace pushes an open-chat snapshot to every connection except
// sender ("" reaches all of them). Writes run on their own goroutines, so they
// may arrive out of order; clients keep only the highest revision.
func (c *chatSessionChanges) broadcastWorkspace(sender string, snapshot protocol.ChatOpenWorkspaceSnapshot) {
if c == nil {
return
}
c.mu.Lock()
targets := make([]func(protocol.Message) error, 0, len(c.clients))
for id, write := range c.clients {
if id != sender {
targets = append(targets, write)
}
}
c.mu.Unlock()
for _, write := range targets {
copy := snapshot
copy.Entries = append([]protocol.ChatOpenWorkspaceEntry(nil), snapshot.Entries...)
go func() { _ = write(protocol.Message{Type: protocol.ChatOpenWorkspaceChanged, ChatOpenWorkspace: &copy}) }()
}
}
// echoOperatorSession answers a metadata change with the updated registry row
// and pushes the same row to every other connection.
func echoOperatorSession(ctx context.Context, request protocol.Message, sessionID, reply string, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
session, found, err := registry.OperatorSession(ctx, sessionID)
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: "session " + sessionID + " is no longer an operator session"})
return
}
wire := chatSessionFromOperator(session)
_ = write(protocol.Message{
Type: reply,
ID: request.ID,
ChatSession: &wire,
ChatSessions: []protocol.ChatSession{wire},
})
if changes != nil {
changes.broadcast(clientID, wire)
}
}