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
|
||||
}
|
||||
|
||||
@@ -2,58 +2,104 @@ package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
func TestChatOpenWorkspacePersistsAndSeedsOnce(t *testing.T) {
|
||||
path := t.TempDir() + "/workspace.db"
|
||||
ctx := context.Background()
|
||||
func openWorkspaceStore(t *testing.T, path string) *Store {
|
||||
t.Helper()
|
||||
store, err := Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
func TestChatOpenWorkspacePersistsAndSeedsOnce(t *testing.T) {
|
||||
path := t.TempDir() + "/workspace.db"
|
||||
ctx := context.Background()
|
||||
store := openWorkspaceStore(t, path)
|
||||
first := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a", Floating: true}}
|
||||
snapshot, err := store.SaveChatOpenWorkspace(ctx, "default", first, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
snapshot, changed, err := store.SaveChatOpenWorkspace(ctx, "default", first, true, 0)
|
||||
if err != nil || !changed {
|
||||
t.Fatalf("seed: changed=%v err=%v", changed, err)
|
||||
}
|
||||
if snapshot.Revision != 1 || len(snapshot.Entries) != 1 || !snapshot.Entries[0].Floating {
|
||||
t.Fatalf("unexpected snapshot: %+v", snapshot)
|
||||
}
|
||||
second, err := store.SaveChatOpenWorkspace(ctx, "default", []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_b"}}, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
second, changed, err := store.SaveChatOpenWorkspace(ctx, "default", []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_b"}}, true, 0)
|
||||
if err != nil || changed || second.Rejected {
|
||||
t.Fatalf("second seed: changed=%v rejected=%v err=%v", changed, second.Rejected, err)
|
||||
}
|
||||
if len(second.Entries) != 1 || second.Entries[0].SessionID != "ses_a" {
|
||||
t.Fatalf("seed replaced existing workspace: %+v", second)
|
||||
}
|
||||
// An emptied workspace is still a stored workspace: an old device-local
|
||||
// snapshot must not seed its chats back.
|
||||
if _, _, err := store.SaveChatOpenWorkspace(ctx, "default", nil, false, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reseed, changed, err := store.SaveChatOpenWorkspace(ctx, "default", first, true, 0)
|
||||
if err != nil || changed || len(reseed.Entries) != 0 || reseed.Revision != 2 {
|
||||
t.Fatalf("seed restored an emptied workspace: %+v changed=%v err=%v", reseed, changed, err)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err = Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store = openWorkspaceStore(t, path)
|
||||
defer store.Close()
|
||||
loaded, err := store.ChatOpenWorkspace(ctx, "default")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Revision != 1 || loaded.Entries[0].SessionID != "ses_a" {
|
||||
if loaded.Revision != 2 || len(loaded.Entries) != 0 {
|
||||
t.Fatalf("workspace did not survive restart: %+v", loaded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatOpenWorkspaceRejectsDuplicatesAndBounds(t *testing.T) {
|
||||
store, err := Open(t.TempDir() + "/workspace.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
func TestChatOpenWorkspaceRejectsStaleRevision(t *testing.T) {
|
||||
store := openWorkspaceStore(t, t.TempDir()+"/workspace.db")
|
||||
defer store.Close()
|
||||
entries := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a"}, {SessionID: "ses_a"}}
|
||||
if _, err := store.SaveChatOpenWorkspace(context.Background(), "default", entries, false); err == nil {
|
||||
t.Fatal("duplicate workspace entries accepted")
|
||||
ctx := context.Background()
|
||||
a := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a"}}
|
||||
ab := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a"}, {SessionID: "ses_b"}}
|
||||
if s, _, err := store.SaveChatOpenWorkspace(ctx, "default", a, false, 0); err != nil || s.Revision != 1 {
|
||||
t.Fatalf("first write: %+v %v", s, err)
|
||||
}
|
||||
if s, _, err := store.SaveChatOpenWorkspace(ctx, "default", ab, false, 1); err != nil || s.Revision != 2 {
|
||||
t.Fatalf("second write: %+v %v", s, err)
|
||||
}
|
||||
stale, changed, err := store.SaveChatOpenWorkspace(ctx, "default", nil, false, 1)
|
||||
if err != nil || changed || !stale.Rejected || stale.Revision != 2 || len(stale.Entries) != 2 {
|
||||
t.Fatalf("stale write = %+v changed=%v err=%v", stale, changed, err)
|
||||
}
|
||||
same, changed, err := store.SaveChatOpenWorkspace(ctx, "default", ab, false, 2)
|
||||
if err != nil || changed || same.Revision != 2 {
|
||||
t.Fatalf("identical write stored a revision: %+v changed=%v", same, changed)
|
||||
}
|
||||
removed, changed, err := store.RemoveChatOpenWorkspaceSessions(ctx, "default", []string{"ses_a", "ses_x"})
|
||||
if err != nil || !changed || removed.Revision != 3 || len(removed.Entries) != 1 || removed.Entries[0].SessionID != "ses_b" {
|
||||
t.Fatalf("remove = %+v changed=%v err=%v", removed, changed, err)
|
||||
}
|
||||
if _, changed, _ := store.RemoveChatOpenWorkspaceSessions(ctx, "default", []string{"ses_a"}); changed {
|
||||
t.Fatal("removing an absent chat stored a revision")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatOpenWorkspaceRejectsDuplicatesAndBounds(t *testing.T) {
|
||||
store := openWorkspaceStore(t, t.TempDir()+"/workspace.db")
|
||||
defer store.Close()
|
||||
entries := []protocol.ChatOpenWorkspaceEntry{{SessionID: "ses_a"}, {SessionID: "ses_a"}}
|
||||
if _, _, err := store.SaveChatOpenWorkspace(context.Background(), "default", entries, false, 0); err == nil {
|
||||
t.Fatal("duplicate workspace entries accepted")
|
||||
}
|
||||
many := make([]protocol.ChatOpenWorkspaceEntry, ChatOpenWorkspaceLimit+1)
|
||||
for i := range many {
|
||||
many[i].SessionID = fmt.Sprintf("ses_%d", i)
|
||||
}
|
||||
if _, _, err := store.SaveChatOpenWorkspace(context.Background(), "default", many, false, 0); err == nil {
|
||||
t.Fatal("oversized workspace accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,27 @@ type ChatOpenWorkspaceEntry struct {
|
||||
Floating bool `json:"floating,omitempty"`
|
||||
}
|
||||
|
||||
// ChatOpenWorkspaceRequest replaces the open-chat set. A chat_open_workspace
|
||||
// without a request only reads the current snapshot.
|
||||
type ChatOpenWorkspaceRequest struct {
|
||||
Entries []ChatOpenWorkspaceEntry `json:"entries"`
|
||||
// Seed is honored only when the server has no saved workspace yet. It makes
|
||||
// migration from the old device-local snapshot race-safe.
|
||||
// BaseRevision is the snapshot revision the client's entries were derived
|
||||
// from. A write whose base is not the current revision is refused, and the
|
||||
// reply carries the current snapshot with Rejected set, so a late write
|
||||
// can never overwrite a newer change.
|
||||
BaseRevision int64 `json:"base_revision"`
|
||||
// Seed is honored only when the server has never stored a workspace. It
|
||||
// makes migration from the old device-local snapshot race-safe; otherwise
|
||||
// the reply is the existing snapshot, unchanged.
|
||||
Seed bool `json:"seed,omitempty"`
|
||||
}
|
||||
|
||||
// ChatOpenWorkspaceSnapshot is the complete open-chat set. Revision grows by
|
||||
// one with every stored change.
|
||||
type ChatOpenWorkspaceSnapshot struct {
|
||||
Revision int64 `json:"revision"`
|
||||
Entries []ChatOpenWorkspaceEntry `json:"entries"`
|
||||
// Rejected marks a reply to a write whose base revision was stale: the
|
||||
// write was not applied and the snapshot is the current state.
|
||||
Rejected bool `json:"rejected,omitempty"`
|
||||
}
|
||||
|
||||
+2
-10
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user