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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user