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.
180 lines
5.8 KiB
Go
180 lines
5.8 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// ChatOpenWorkspaceLimit bounds the open-chat set.
|
|
const ChatOpenWorkspaceLimit = 64
|
|
|
|
func (s *Store) initChatOpenWorkspace() error {
|
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_open_workspaces (
|
|
scope TEXT PRIMARY KEY, entries_json TEXT NOT NULL, revision INTEGER NOT NULL DEFAULT 0,
|
|
updated_at INTEGER NOT NULL DEFAULT 0
|
|
);`)
|
|
return err
|
|
}
|
|
|
|
func validateOpenWorkspace(entries []protocol.ChatOpenWorkspaceEntry) error {
|
|
if len(entries) > ChatOpenWorkspaceLimit {
|
|
return fmt.Errorf("too many open chats")
|
|
}
|
|
seen := make(map[string]struct{}, len(entries))
|
|
for _, entry := range entries {
|
|
id := strings.TrimSpace(entry.SessionID)
|
|
if id == "" || id != entry.SessionID || len(id) > 200 {
|
|
return fmt.Errorf("invalid open chat session id")
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
return fmt.Errorf("duplicate open chat session id %q", id)
|
|
}
|
|
seen[id] = struct{}{}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func decodeOpenWorkspace(raw string) ([]protocol.ChatOpenWorkspaceEntry, error) {
|
|
var entries []protocol.ChatOpenWorkspaceEntry
|
|
if err := json.Unmarshal([]byte(raw), &entries); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := validateOpenWorkspace(entries); err != nil {
|
|
return nil, err
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func sameOpenWorkspace(a, b []protocol.ChatOpenWorkspaceEntry) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ChatOpenWorkspace reads the stored snapshot; revision 0 means none was ever
|
|
// stored.
|
|
func (s *Store) ChatOpenWorkspace(ctx context.Context, scope string) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
|
return readOpenWorkspace(ctx, s.db, scope)
|
|
}
|
|
|
|
type rowQuerier interface {
|
|
QueryRowContext(context.Context, string, ...any) *sql.Row
|
|
}
|
|
|
|
func readOpenWorkspace(ctx context.Context, db rowQuerier, scope string) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
|
var raw string
|
|
snapshot := protocol.ChatOpenWorkspaceSnapshot{Entries: []protocol.ChatOpenWorkspaceEntry{}}
|
|
err := db.QueryRowContext(ctx, `SELECT entries_json,revision FROM chat_open_workspaces WHERE scope=?`, scope).Scan(&raw, &snapshot.Revision)
|
|
if err == sql.ErrNoRows {
|
|
return snapshot, nil
|
|
}
|
|
if err != nil {
|
|
return snapshot, err
|
|
}
|
|
snapshot.Entries, err = decodeOpenWorkspace(raw)
|
|
return snapshot, err
|
|
}
|
|
|
|
func writeOpenWorkspace(ctx context.Context, tx *sql.Tx, scope string, revision int64, entries []protocol.ChatOpenWorkspaceEntry) error {
|
|
raw, err := json.Marshal(entries)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO chat_open_workspaces(scope,entries_json,revision,updated_at) VALUES(?,?,?,?)
|
|
ON CONFLICT(scope) DO UPDATE SET entries_json=excluded.entries_json,revision=excluded.revision,updated_at=excluded.updated_at`,
|
|
scope, string(raw), revision, time.Now().UnixMilli())
|
|
return err
|
|
}
|
|
|
|
// SaveChatOpenWorkspace applies a full snapshot as one compare-and-swap. A
|
|
// write whose baseRevision is not the stored revision is not applied: the
|
|
// current snapshot is returned with Rejected set. A seed applies only when no
|
|
// workspace was ever stored and otherwise returns the stored one. changed
|
|
// reports whether a new revision was stored; an identical write stores none.
|
|
func (s *Store) SaveChatOpenWorkspace(ctx context.Context, scope string, entries []protocol.ChatOpenWorkspaceEntry, seed bool, baseRevision int64) (snapshot protocol.ChatOpenWorkspaceSnapshot, changed bool, err error) {
|
|
if strings.TrimSpace(scope) == "" {
|
|
return snapshot, false, fmt.Errorf("workspace scope is required")
|
|
}
|
|
if entries == nil {
|
|
entries = []protocol.ChatOpenWorkspaceEntry{}
|
|
}
|
|
if err := validateOpenWorkspace(entries); err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
defer tx.Rollback()
|
|
current, err := readOpenWorkspace(ctx, tx, scope)
|
|
if err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
if seed && current.Revision > 0 {
|
|
return current, false, nil
|
|
}
|
|
if !seed && baseRevision != current.Revision {
|
|
current.Rejected = true
|
|
return current, false, nil
|
|
}
|
|
if current.Revision > 0 && sameOpenWorkspace(current.Entries, entries) {
|
|
return current, false, nil
|
|
}
|
|
next := protocol.ChatOpenWorkspaceSnapshot{Revision: current.Revision + 1, Entries: entries}
|
|
if err := writeOpenWorkspace(ctx, tx, scope, next.Revision, entries); err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
return next, true, nil
|
|
}
|
|
|
|
// RemoveChatOpenWorkspaceSessions drops the named sessions from the stored
|
|
// snapshot in one transaction, storing a new revision only when one was
|
|
// present. The server uses it for closed and no-longer-valid chats.
|
|
func (s *Store) RemoveChatOpenWorkspaceSessions(ctx context.Context, scope string, sessionIDs []string) (snapshot protocol.ChatOpenWorkspaceSnapshot, changed bool, err error) {
|
|
drop := make(map[string]bool, len(sessionIDs))
|
|
for _, id := range sessionIDs {
|
|
drop[id] = true
|
|
}
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
defer tx.Rollback()
|
|
current, err := readOpenWorkspace(ctx, tx, scope)
|
|
if err != nil || current.Revision == 0 {
|
|
return current, false, err
|
|
}
|
|
kept := make([]protocol.ChatOpenWorkspaceEntry, 0, len(current.Entries))
|
|
for _, entry := range current.Entries {
|
|
if !drop[entry.SessionID] {
|
|
kept = append(kept, entry)
|
|
}
|
|
}
|
|
if len(kept) == len(current.Entries) {
|
|
return current, false, nil
|
|
}
|
|
next := protocol.ChatOpenWorkspaceSnapshot{Revision: current.Revision + 1, Entries: kept}
|
|
if err := writeOpenWorkspace(ctx, tx, scope, next.Revision, kept); err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
if err := tx.Commit(); err != nil {
|
|
return snapshot, false, err
|
|
}
|
|
return next, true, nil
|
|
}
|