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.
This commit is contained in:
2026-11-12 03:23:22 +00:00
parent f8e081825f
commit 4f8e99f316
20 changed files with 1333 additions and 276 deletions
+2 -10
View File
@@ -214,11 +214,7 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
handleChatDirectories(ctx, request, registry, write)
return true
case protocol.ChatClose:
var store chatOpenWorkspaceStore
if candidate, ok := registry.(chatOpenWorkspaceStore); ok {
store = candidate
}
handleChatClose(ctx, request, broker, hub, registry, store, write)
handleChatClose(ctx, request, broker, hub, registry, write)
return true
case protocol.ChatReopen:
handleChatReopen(ctx, request, registry, write)
@@ -230,11 +226,7 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
handleChatColor(ctx, request, registry, hub.changes(), clientID, write)
return true
case protocol.ChatOpenWorkspace:
var store chatOpenWorkspaceStore
if candidate, ok := registry.(chatOpenWorkspaceStore); ok {
store = candidate
}
handleChatOpenWorkspace(ctx, request, broker, registry, store, hub.changes(), clientID, write)
handleChatOpenWorkspace(ctx, request, registry, hub.changes(), clientID, write)
return true
case protocol.ChatRename:
handleChatRename(ctx, request, broker, registry, hub.changes(), clientID, write)
+4 -17
View File
@@ -17,7 +17,7 @@ import (
// registry, not on one device. A session id that is not a recorded operator
// session is refused before anything is written, so a pipeline-owned run session
// is never touched.
func handleChatClose(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, store chatOpenWorkspaceStore, write func(protocol.Message) error) {
func handleChatClose(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, 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
@@ -92,22 +92,9 @@ func handleChatClose(ctx context.Context, request protocol.Message, broker chatB
// chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{wire}
_ = write(response)
if store != nil {
workspace, workspaceErr := store.ChatOpenWorkspace(ctx, chatWorkspaceScope)
if workspaceErr == nil {
kept := workspace.Entries[:0]
for _, entry := range workspace.Entries {
if entry.SessionID != sessionID {
kept = append(kept, entry)
}
}
if len(kept) != len(workspace.Entries) {
if updated, saveErr := store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, kept, false); saveErr == nil && hub != nil {
hub.changes().broadcastWorkspace("", updated)
}
}
}
}
// A closed chat leaves every device's open-chat set, so no client can keep
// or restore it.
dropFromChatWorkspace(ctx, registry, hub.changes(), sessionID)
}
// handleChatReopen clears the closed state of an operator chat session so it
+113 -67
View File
@@ -3,116 +3,162 @@ package server
import (
"context"
"fmt"
"log"
"strings"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// chatWorkspaceScope keys the one operator's open-chat set: the authenticated
// local server has a single operator.
const chatWorkspaceScope = "default"
// chatOpenWorkspaceStore persists the server-authoritative open-chat set (see
// pipeline.Store.SaveChatOpenWorkspace for the revision rules).
type chatOpenWorkspaceStore interface {
ChatOpenWorkspace(context.Context, string) (protocol.ChatOpenWorkspaceSnapshot, error)
SaveChatOpenWorkspace(context.Context, string, []protocol.ChatOpenWorkspaceEntry, bool) (protocol.ChatOpenWorkspaceSnapshot, error)
SaveChatOpenWorkspace(ctx context.Context, scope string, entries []protocol.ChatOpenWorkspaceEntry, seed bool, baseRevision int64) (protocol.ChatOpenWorkspaceSnapshot, bool, error)
RemoveChatOpenWorkspaceSessions(ctx context.Context, scope string, sessionIDs []string) (protocol.ChatOpenWorkspaceSnapshot, bool, error)
}
func reconcileChatWorkspace(ctx context.Context, snapshot protocol.ChatOpenWorkspaceSnapshot, broker chatBroker, registry operatorRegistry, store chatOpenWorkspaceStore) (protocol.ChatOpenWorkspaceSnapshot, error) {
if broker == nil || registry == nil || store == nil {
return snapshot, nil
func openWorkspaceStoreOf(registry operatorRegistry) chatOpenWorkspaceStore {
store, _ := registry.(chatOpenWorkspaceStore)
return store
}
// openOperatorChat reports whether id is a recorded operator chat that is not
// closed. Only those may be in the open-chat set, so a pipeline-owned run
// session, a closed chat or an unknown id is never kept. The registry is the
// authority; the broker is not asked, so validation never starts a harness.
func openOperatorChat(ctx context.Context, registry operatorRegistry, id string) (bool, error) {
row, found, err := registry.OperatorSession(ctx, id)
if err != nil {
return false, err
}
valid := make([]protocol.ChatOpenWorkspaceEntry, 0, len(snapshot.Entries))
return found && row.ClosedAt == 0, nil
}
// pruneChatWorkspace drops every stored entry that is no longer an open
// operator chat. changed reports a new stored revision, which the caller
// broadcasts to every connection.
func pruneChatWorkspace(ctx context.Context, registry operatorRegistry, store chatOpenWorkspaceStore) (protocol.ChatOpenWorkspaceSnapshot, bool, error) {
snapshot, err := store.ChatOpenWorkspace(ctx, chatWorkspaceScope)
if err != nil || snapshot.Revision == 0 {
return snapshot, false, err
}
var stale []string
for _, entry := range snapshot.Entries {
row, found, err := registry.OperatorSession(ctx, entry.SessionID)
open, err := openOperatorChat(ctx, registry, entry.SessionID)
if err != nil {
return snapshot, err
return snapshot, false, err
}
if !found || row.ClosedAt != 0 {
continue
}
if _, live, err := broker.Session(ctx, entry.SessionID); err != nil {
return snapshot, err
} else if live {
valid = append(valid, entry)
if !open {
stale = append(stale, entry.SessionID)
}
}
if len(valid) == len(snapshot.Entries) {
return snapshot, nil
if len(stale) == 0 {
return snapshot, false, nil
}
return store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, valid, false)
return store.RemoveChatOpenWorkspaceSessions(ctx, chatWorkspaceScope, stale)
}
func validateChatWorkspace(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry) ([]protocol.ChatOpenWorkspaceEntry, error) {
if request.ChatOpenWorkspaceRequest == nil {
return nil, fmt.Errorf("chat_open_workspace_request is required")
// announceChatWorkspace runs when a connection opens: it prunes stale entries
// and, when that stored a new revision, pushes it to every connection;
// otherwise it sends the current snapshot to the new connection alone, so a
// reconnecting client catches up with changes it missed.
func announceChatWorkspace(ctx context.Context, registry operatorRegistry, changes *chatSessionChanges, write func(protocol.Message) error) {
store := openWorkspaceStoreOf(registry)
if store == nil {
return
}
entries := request.ChatOpenWorkspaceRequest.Entries
if len(entries) > 64 {
snapshot, changed, err := pruneChatWorkspace(ctx, registry, store)
if err != nil {
log.Printf("reconcile open chat workspace: %v", err)
return
}
if changed {
changes.broadcastWorkspace("", snapshot)
} else if snapshot.Revision > 0 {
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspaceChanged, ChatOpenWorkspace: &snapshot})
}
}
// dropFromChatWorkspace removes a closed chat from the open-chat set and pushes
// the new revision to every connection, the closing one included.
func dropFromChatWorkspace(ctx context.Context, registry operatorRegistry, changes *chatSessionChanges, sessionID string) {
store := openWorkspaceStoreOf(registry)
if store == nil {
return
}
snapshot, changed, err := store.RemoveChatOpenWorkspaceSessions(ctx, chatWorkspaceScope, []string{sessionID})
if err != nil {
log.Printf("drop closed chat %s from open workspace: %v", sessionID, err)
return
}
if changed {
changes.broadcastWorkspace("", snapshot)
}
}
// validChatWorkspaceEntries checks a client's snapshot and keeps only its open
// operator chats. Malformed or duplicate ids are refused; closed or unknown
// chats are dropped, so a stale client can never restore them.
func validChatWorkspaceEntries(ctx context.Context, request protocol.ChatOpenWorkspaceRequest, registry operatorRegistry) ([]protocol.ChatOpenWorkspaceEntry, error) {
if len(request.Entries) > pipeline.ChatOpenWorkspaceLimit {
return nil, fmt.Errorf("too many open chats")
}
seen := map[string]bool{}
for i := range entries {
id := strings.TrimSpace(entries[i].SessionID)
valid := make([]protocol.ChatOpenWorkspaceEntry, 0, len(request.Entries))
for _, entry := range request.Entries {
id := strings.TrimSpace(entry.SessionID)
if !validChatSessionID(id) || seen[id] {
return nil, fmt.Errorf("invalid or duplicate open chat session %q", id)
}
entries[i].SessionID = id
seen[id] = true
if registry == nil || broker == nil {
return nil, fmt.Errorf("chat workspace validation unavailable")
}
row, found, err := registry.OperatorSession(ctx, id)
open, err := openOperatorChat(ctx, registry, id)
if err != nil {
return nil, err
}
if !found || row.ClosedAt != 0 {
continue
}
if _, live, err := broker.Session(ctx, id); err != nil {
return nil, err
} else if !live {
continue
if open {
valid = append(valid, protocol.ChatOpenWorkspaceEntry{SessionID: id, Floating: entry.Floating})
}
}
return entries, nil
return valid, nil
}
func handleChatOpenWorkspace(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, store chatOpenWorkspaceStore, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
if store == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat workspace persistence unavailable"})
// handleChatOpenWorkspace reads (no request) or replaces the open-chat set.
// The reply always carries the resulting snapshot; a stored change is pushed
// to every other connection as chat_open_workspace_changed.
func handleChatOpenWorkspace(ctx context.Context, request protocol.Message, registry operatorRegistry, changes *chatSessionChanges, clientID string, write func(protocol.Message) error) {
fail := func(err error) {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
}
store := openWorkspaceStoreOf(registry)
if registry == nil || store == nil {
fail(fmt.Errorf("chat workspace persistence unavailable"))
return
}
entries, err := validateChatWorkspace(ctx, request, broker, registry)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// Invalid, closed, deleted, and replaced sessions are dropped rather than
// returned as errors: clients can never restore them from a stale snapshot.
valid := entries[:0]
for _, entry := range entries {
row, found, rowErr := registry.OperatorSession(ctx, entry.SessionID)
if rowErr != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: rowErr.Error()})
return
}
if !found || row.ClosedAt != 0 {
continue
}
_, live, sessionErr := broker.Session(ctx, entry.SessionID)
if sessionErr != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: sessionErr.Error()})
return
}
if live {
valid = append(valid, entry)
var (
snapshot protocol.ChatOpenWorkspaceSnapshot
changed bool
err error
)
if request.ChatOpenWorkspaceRequest == nil {
snapshot, changed, err = pruneChatWorkspace(ctx, registry, store)
} else {
var entries []protocol.ChatOpenWorkspaceEntry
entries, err = validChatWorkspaceEntries(ctx, *request.ChatOpenWorkspaceRequest, registry)
if err == nil {
snapshot, changed, err = store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, entries, request.ChatOpenWorkspaceRequest.Seed, request.ChatOpenWorkspaceRequest.BaseRevision)
}
}
snapshot, err := store.SaveChatOpenWorkspace(ctx, chatWorkspaceScope, valid, request.ChatOpenWorkspaceRequest.Seed)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
fail(err)
return
}
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspace, ID: request.ID, ChatOpenWorkspace: &snapshot})
if changes != nil {
if changed {
changes.broadcastWorkspace(clientID, snapshot)
}
}
+147
View File
@@ -0,0 +1,147 @@
package server
import (
"context"
"testing"
"github.com/coder/websocket"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func workspaceWrite(id string, base int64, entries ...protocol.ChatOpenWorkspaceEntry) protocol.Message {
return protocol.Message{Type: protocol.ChatOpenWorkspace, ID: id, ChatOpenWorkspaceRequest: &protocol.ChatOpenWorkspaceRequest{Entries: entries, BaseRevision: base}}
}
func workspaceIDs(snapshot *protocol.ChatOpenWorkspaceSnapshot) []string {
ids := []string{}
for _, entry := range snapshot.Entries {
id := entry.SessionID
if entry.Floating {
id += "*"
}
ids = append(ids, id)
}
return ids
}
func sameIDs(a []string, b ...string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// readWorkspacePush reads chat_open_workspace_changed pushes until one carries
// revision; lower revisions (pushed out of order) are skipped.
func readWorkspacePush(t *testing.T, ctx context.Context, conn *websocket.Conn, revision int64) *protocol.ChatOpenWorkspaceSnapshot {
t.Helper()
for {
message := readUntil(t, ctx, conn, protocol.ChatOpenWorkspaceChanged)
got := message.ChatOpenWorkspace
if got == nil {
t.Fatal("push without a snapshot")
}
if got.Revision == revision {
return got
}
if got.Revision > revision {
t.Fatalf("push revision %d, want %d", got.Revision, revision)
}
}
}
// TestChatOpenWorkspaceSyncsEveryConnection is the fan-out evidence: a write
// from one connection is stored at the next revision and pushed to the other;
// a write from a stale base is refused with the current snapshot; closing a
// chat drops it and pushes the result to both connections; and a later write
// cannot put the closed chat back.
func TestChatOpenWorkspaceSyncsEveryConnection(t *testing.T) {
store := newOperatorStore(t)
ctx, _, conns := dialChatConns(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 2)
for _, id := range []string{"ses_a", "ses_b"} {
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
}
a, b := protocol.ChatOpenWorkspaceEntry{SessionID: "ses_a"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b", Floating: true}
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, a, b, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_unknown"}))
reply := readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace).ChatOpenWorkspace
if reply.Revision != 1 || reply.Rejected || !sameIDs(workspaceIDs(reply), "ses_a", "ses_b*") {
t.Fatalf("first write reply = %+v", reply)
}
if push := readWorkspacePush(t, ctx, conns[1], 1); !sameIDs(workspaceIDs(push), "ses_a", "ses_b*") {
t.Fatalf("second connection got %v", workspaceIDs(push))
}
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatOpenWorkspace, ID: "r1"})
if read := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; read.Revision != 1 || !sameIDs(workspaceIDs(read), "ses_a", "ses_b*") {
t.Fatalf("read = %+v", read)
}
// A write derived from revision 0 lands after revision 1: refused.
writeChat(t, ctx, conns[1], workspaceWrite("w2", 0, b))
stale := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace
if !stale.Rejected || stale.Revision != 1 || !sameIDs(workspaceIDs(stale), "ses_a", "ses_b*") {
t.Fatalf("stale write reply = %+v", stale)
}
// Reorder from the second connection reaches the first.
b.Floating = false
writeChat(t, ctx, conns[1], workspaceWrite("w3", 1, b, a))
if got := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 2 {
t.Fatalf("reorder reply = %+v", got)
}
if push := readWorkspacePush(t, ctx, conns[0], 2); !sameIDs(workspaceIDs(push), "ses_b", "ses_a") {
t.Fatalf("reorder push = %v", workspaceIDs(push))
}
// Closing ses_a drops it and pushes revision 3 to both connections.
writeChat(t, ctx, conns[0], protocol.Message{Type: protocol.ChatClose, ID: "c1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_a"}})
readUntil(t, ctx, conns[0], protocol.ChatClose)
for i, conn := range conns {
if push := readWorkspacePush(t, ctx, conn, 3); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("connection %d close push = %v", i, workspaceIDs(push))
}
}
// A client still showing the closed chat cannot restore it.
writeChat(t, ctx, conns[1], workspaceWrite("w4", 3, a, b))
if got := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 3 || !sameIDs(workspaceIDs(got), "ses_b") {
t.Fatalf("closed chat restored: %+v", got)
}
}
// TestChatOpenWorkspacePrunesOnConnectForEveryone pins that a chat closed
// behind the workspace's back is pruned when a connection opens, and that the
// pruned snapshot reaches the connections already open, not only the new one.
func TestChatOpenWorkspacePrunesOnConnectForEveryone(t *testing.T) {
store := newOperatorStore(t)
ctx, _, srv, conns := dialChatServer(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 1)
for _, id := range []string{"ses_a", "ses_b"} {
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
}
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_a"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b"}))
readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace)
if err := store.CloseOperatorSession(ctx, "ses_a"); err != nil {
t.Fatal(err)
}
late := dialChatConn(t, ctx, srv)
for i, conn := range []*websocket.Conn{conns[0], late} {
if push := readWorkspacePush(t, ctx, conn, 2); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("connection %d prune push = %v", i, workspaceIDs(push))
}
}
// With nothing to prune, a new connection alone is brought up to date.
third := dialChatConn(t, ctx, srv)
if push := readWorkspacePush(t, ctx, third, 2); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("catch-up push = %v", workspaceIDs(push))
}
}
+6
View File
@@ -62,7 +62,13 @@ func (c *chatSessionChanges) broadcast(sender string, session protocol.ChatSessi
}
}
// 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 {
+3 -11
View File
@@ -261,17 +261,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
// connection the same way.
hub.sessionChanges.attach(clientID, write)
defer hub.sessionChanges.detach(clientID)
if workspaceStore, ok := registry.(chatOpenWorkspaceStore); ok {
if snapshot, err := workspaceStore.ChatOpenWorkspace(connCtx, chatWorkspaceScope); err == nil && snapshot.Revision > 0 {
snapshot, err = reconcileChatWorkspace(connCtx, snapshot, options.Chat, registry, workspaceStore)
if err != nil {
log.Printf("reconcile open chat workspace: %v", err)
}
if err == nil {
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspaceChanged, ChatOpenWorkspace: &snapshot})
}
}
}
// The open-chat set: prune entries that are no longer open chats and
// push the result to every connection, else bring this one up to date.
announceChatWorkspace(connCtx, registry, hub.changes(), write)
// Terminal lifecycle changes reach every connection; output only the
// connections that opened or read that terminal.
terminals.clients.attach(clientID, write)