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:
@@ -11,7 +11,8 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
const chatOpenWorkspaceLimit = 64
|
||||
// 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 (
|
||||
@@ -22,13 +23,13 @@ updated_at INTEGER NOT NULL DEFAULT 0
|
||||
}
|
||||
|
||||
func validateOpenWorkspace(entries []protocol.ChatOpenWorkspaceEntry) error {
|
||||
if len(entries) > chatOpenWorkspaceLimit {
|
||||
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 == "" || len(id) > 200 {
|
||||
if id == "" || id != entry.SessionID || len(id) > 200 {
|
||||
return fmt.Errorf("invalid open chat session id")
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
@@ -50,10 +51,32 @@ func decodeOpenWorkspace(raw string) ([]protocol.ChatOpenWorkspaceEntry, error)
|
||||
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
|
||||
var snapshot protocol.ChatOpenWorkspaceSnapshot
|
||||
err := s.db.QueryRowContext(ctx, `SELECT entries_json,revision FROM chat_open_workspaces WHERE scope=?`, scope).Scan(&raw, &snapshot.Revision)
|
||||
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
|
||||
}
|
||||
@@ -64,62 +87,93 @@ func (s *Store) ChatOpenWorkspace(ctx context.Context, scope string) (protocol.C
|
||||
return snapshot, err
|
||||
}
|
||||
|
||||
// SaveChatOpenWorkspace atomically applies a full snapshot. Seed never replaces
|
||||
// a workspace that another device has already created.
|
||||
func (s *Store) SaveChatOpenWorkspace(ctx context.Context, scope string, entries []protocol.ChatOpenWorkspaceEntry, seed bool) (protocol.ChatOpenWorkspaceSnapshot, error) {
|
||||
if strings.TrimSpace(scope) == "" {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, fmt.Errorf("workspace scope is required")
|
||||
}
|
||||
if err := validateOpenWorkspace(entries); err != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, 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 protocol.ChatOpenWorkspaceSnapshot{}, err
|
||||
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 protocol.ChatOpenWorkspaceSnapshot{}, err
|
||||
return snapshot, false, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var old string
|
||||
var revision int64
|
||||
err = tx.QueryRowContext(ctx, `SELECT entries_json,revision FROM chat_open_workspaces WHERE scope=?`, scope).Scan(&old, &revision)
|
||||
if err == sql.ErrNoRows {
|
||||
revision = 1
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO chat_open_workspaces(scope,entries_json,revision,updated_at) VALUES(?,?,?,?)`, scope, string(raw), revision, time.Now().UnixMilli())
|
||||
} else if err == nil {
|
||||
if seed {
|
||||
current, decodeErr := decodeOpenWorkspace(old)
|
||||
if decodeErr != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, decodeErr
|
||||
}
|
||||
if len(current) != 0 {
|
||||
entries = current
|
||||
raw, _ = json.Marshal(entries)
|
||||
} else {
|
||||
revision++
|
||||
}
|
||||
} else {
|
||||
revision++
|
||||
}
|
||||
if len(entries) == 0 && seed && old != "" {
|
||||
// An explicitly empty seed is still a valid seed; only a non-empty
|
||||
// existing list wins the race above.
|
||||
var current []protocol.ChatOpenWorkspaceEntry
|
||||
current, _ = decodeOpenWorkspace(old)
|
||||
if len(current) != 0 {
|
||||
entries = current
|
||||
raw, _ = json.Marshal(entries)
|
||||
}
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE chat_open_workspaces SET entries_json=?,revision=?,updated_at=? WHERE scope=?`, string(raw), revision, time.Now().UnixMilli(), scope)
|
||||
}
|
||||
current, err := readOpenWorkspace(ctx, tx, scope)
|
||||
if err != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
||||
return snapshot, false, err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return protocol.ChatOpenWorkspaceSnapshot{}, err
|
||||
if seed && current.Revision > 0 {
|
||||
return current, false, nil
|
||||
}
|
||||
return protocol.ChatOpenWorkspaceSnapshot{Revision: revision, Entries: entries}, 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user