4f8e99f316
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.
165 lines
6.3 KiB
Go
165 lines
6.3 KiB
Go
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(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 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
|
|
}
|
|
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 {
|
|
open, err := openOperatorChat(ctx, registry, entry.SessionID)
|
|
if err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
if !open {
|
|
stale = append(stale, entry.SessionID)
|
|
}
|
|
}
|
|
if len(stale) == 0 {
|
|
return snapshot, false, nil
|
|
}
|
|
return store.RemoveChatOpenWorkspaceSessions(ctx, chatWorkspaceScope, stale)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
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{}
|
|
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)
|
|
}
|
|
seen[id] = true
|
|
open, err := openOperatorChat(ctx, registry, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if open {
|
|
valid = append(valid, protocol.ChatOpenWorkspaceEntry{SessionID: id, Floating: entry.Floating})
|
|
}
|
|
}
|
|
return valid, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
if err != nil {
|
|
fail(err)
|
|
return
|
|
}
|
|
_ = write(protocol.Message{Type: protocol.ChatOpenWorkspace, ID: request.ID, ChatOpenWorkspace: &snapshot})
|
|
if changed {
|
|
changes.broadcastWorkspace(clientID, snapshot)
|
|
}
|
|
}
|