From 4f8e99f316a2ba7b1a26bc8bf8db8d4b4bb2019a Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Thu, 12 Nov 2026 03:23:22 +0000 Subject: [PATCH] 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. --- app/lib/features/chat/chat_screen_float.dart | 12 +- .../features/chat/chat_screen_workspace.dart | 91 +++++++- .../chat_open_workspace_controller.dart | 106 +++++++-- .../chat_open_workspace_gateway.dart | 41 +++- .../workspace/chat_workspace_controller.dart | 42 +--- .../workspace/chat_workspace_reconcile.dart | 118 ++++++++++ app/notes/chat-open-workspace-sync.md | 75 +++++-- .../chat/chat_open_workspace_sync_test.dart | 205 +++++++++++++++++ .../chat_open_workspace_controller_test.dart | 211 ++++++++++++++++++ .../chat_open_workspace_gateway_test.dart | 5 +- .../chat_workspace_reconcile_test.dart | 56 +++++ internal/pipeline/chat_open_workspace.go | 160 ++++++++----- internal/pipeline/chat_open_workspace_test.go | 90 ++++++-- internal/protocol/chat_open_workspace.go | 17 +- internal/server/chat.go | 12 +- internal/server/chat_close.go | 21 +- internal/server/chat_open_workspace.go | 180 +++++++++------ internal/server/chat_open_workspace_test.go | 147 ++++++++++++ internal/server/chat_session_changes.go | 6 + internal/server/websocket.go | 14 +- 20 files changed, 1333 insertions(+), 276 deletions(-) create mode 100644 app/lib/features/chat/workspace/chat_workspace_reconcile.dart create mode 100644 app/test/features/chat/chat_open_workspace_sync_test.dart create mode 100644 app/test/features/chat/workspace/chat_open_workspace_controller_test.dart create mode 100644 app/test/features/chat/workspace/chat_workspace_reconcile_test.dart create mode 100644 internal/server/chat_open_workspace_test.go diff --git a/app/lib/features/chat/chat_screen_float.dart b/app/lib/features/chat/chat_screen_float.dart index 40761a95..56e9638b 100644 --- a/app/lib/features/chat/chat_screen_float.dart +++ b/app/lib/features/chat/chat_screen_float.dart @@ -46,9 +46,7 @@ mixin _ChatScreenFloat on State, _ChatScreenWorkspace { final pane = _workspace.detach(paneId); if (pane == null) return; widget.windows?.float(pane.sessionId); - if (!_applyingServerWorkspace) { - unawaited(_openWorkspace?.replace(_serverWorkspaceEntries())); - } + _syncOpenWorkspace(); // Floating the last tiled chat leaves the list, as closing it does. if (_workspace.paneCount == 0) _back(); } @@ -64,9 +62,7 @@ mixin _ChatScreenFloat on State, _ChatScreenWorkspace { _floatPane(pane.id); } else { windows.float(id); - if (!_applyingServerWorkspace) { - unawaited(_openWorkspace?.replace(_serverWorkspaceEntries())); - } + _syncOpenWorkspace(); } } @@ -75,9 +71,7 @@ mixin _ChatScreenFloat on State, _ChatScreenWorkspace { _workspaceVisible = true; if (!_pageOpen && showsListBesideDetail(context)) _usedWorkspace = true; _workspace.dock(sessionId); - if (!_applyingServerWorkspace) { - unawaited(_openWorkspace?.replace(_serverWorkspaceEntries())); - } + _syncOpenWorkspace(); _composerFocus.request(sessionId); } diff --git a/app/lib/features/chat/chat_screen_workspace.dart b/app/lib/features/chat/chat_screen_workspace.dart index e1b6d7e3..65cea8d2 100644 --- a/app/lib/features/chat/chat_screen_workspace.dart +++ b/app/lib/features/chat/chat_screen_workspace.dart @@ -101,12 +101,42 @@ mixin _ChatScreenWorkspace on State { } } + /// Sends this device's open chats to the server, unless the change is the + /// server's own snapshot being applied. + void _syncOpenWorkspace() { + if (_applyingServerWorkspace) return; + _openWorkspace?.replace(_serverWorkspaceEntries()); + } + + /// Binds the server's open-chat set for [widget.gateway], disposing the + /// previous binding (its subscription and queued writes) first. A + /// federated list spans several servers, so it keeps its local layout. + void _bindOpenWorkspace({required Future after}) { + _openWorkspace?.dispose(); + _openWorkspace = null; + _openWorkspaceApplied = false; + if (!widget.persistWorkspace || widget.gateway is FederatedChatGateway) { + return; + } + final open = ChatOpenWorkspaceController(gateway: widget.gateway) + ..onChanged = _applyOpenWorkspace; + _openWorkspace = open; + // Seed from the restored local layout once it is restored. + unawaited( + after.then((_) async { + if (!mounted || open.disposed) return; + await open.start(local: _serverWorkspaceEntries()); + }), + ); + } + void _bindWorkspacePersistence() { - if (!widget.persistWorkspace) return; + if (!widget.persistWorkspace) { + _bindOpenWorkspace(after: Future.value()); + return; + } final scope = serverStorageScope(serverIdentity(widget.gateway)); _workspaceScope = scope; - _openWorkspace = ChatOpenWorkspaceController(gateway: widget.gateway) - ..onChanged = _applyOpenWorkspace; _workspaceBinding = ChatWorkspaceBinding( controller: _workspace, store: widget.gateway is FederatedChatGateway @@ -132,14 +162,35 @@ mixin _ChatScreenWorkspace on State { _publishWorkspaceRoute(); }, ); - unawaited( - _workspaceBinding!.start(initialSessionId: widget.initialSessionId), + _bindOpenWorkspace( + after: _workspaceBinding!.start( + initialSessionId: widget.initialSessionId, + ), ); - unawaited(_openWorkspace!.start(legacy: _serverWorkspaceEntries())); } + /// Whether a server snapshot was applied since binding. The first one keeps + /// the chat this visit was opened on (a deep link) and adds it if absent. + bool _openWorkspaceApplied = false; + + /// Applies the server's membership: windows and panes for chats it no + /// longer lists close, floating state follows it, and tiled chats take its + /// order while this device's split geometry stays. void _applyOpenWorkspace(ChatOpenWorkspaceSnapshot snapshot) { if (!mounted) return; + final initial = widget.initialSessionId ?? ''; + final first = !_openWorkspaceApplied; + final keepInitial = + first && + initial.isNotEmpty && + !snapshot.entries.any((entry) => entry.sessionId == initial); + _openWorkspaceApplied = true; + if (keepInitial) { + snapshot = ChatOpenWorkspaceSnapshot(snapshot.revision, [ + ...snapshot.entries, + ChatOpenWorkspaceEntry(initial), + ]); + } _applyingServerWorkspace = true; try { final windows = widget.windows; @@ -172,20 +223,44 @@ mixin _ChatScreenWorkspace on State { .where((entry) => !entry.floating) .map((entry) => entry.sessionId), ); + if (keepInitial) { + final pane = _workspace.panes + .where((pane) => pane.sessionId == initial) + .firstOrNull; + if (pane != null) _workspace.activate(pane.id); + } if (_workspace.paneCount == 0 && _workspaceVisible) _back(); } finally { _applyingServerWorkspace = false; } + // The first snapshot shows the server's chats as a restored layout does; + // later ones never pull this device into a chat another one opened. + if (first && _workspace.paneCount > 0 && !_workspaceVisible) { + setState(() { + _usedWorkspace = true; + _workspaceVisible = true; + }); + _publishWorkspaceRoute(); + } + if (keepInitial) _syncOpenWorkspace(); } bool _updateWorkspacePersistence(ChatScreen oldWidget) { final scope = serverStorageScope(serverIdentity(widget.gateway)); if (oldWidget.persistWorkspace == widget.persistWorkspace && (!widget.persistWorkspace || scope == _workspaceScope)) { + // Another connection to the same server: only the open-chat sync + // follows the gateway. + if (widget.persistWorkspace && + !identical(oldWidget.gateway, widget.gateway)) { + _bindOpenWorkspace(after: Future.value()); + } return false; } _workspaceBinding?.dispose(); _workspaceBinding = null; + _openWorkspace?.dispose(); + _openWorkspace = null; if (_workspaceScope != null && scope != _workspaceScope) { _workspace.restoreLayout(ChatWorkspaceSnapshot.fromLayout(null, null)!); _workspaceVisible = false; @@ -261,9 +336,7 @@ mixin _ChatScreenWorkspace on State { '${_workspace.activePaneId}:${_workspace.panes.map((p) => '${p.id}=${p.sessionId}').join(',')}'; if (_workspaceSignature == signature) return; _workspaceSignature = signature; - if (!_applyingServerWorkspace) { - unawaited(_openWorkspace?.replace(_serverWorkspaceEntries())); - } + _syncOpenWorkspace(); void update() { if (!mounted) return; setState(() {}); diff --git a/app/lib/features/chat/workspace/chat_open_workspace_controller.dart b/app/lib/features/chat/workspace/chat_open_workspace_controller.dart index e1ebaba8..3448b9fa 100644 --- a/app/lib/features/chat/workspace/chat_open_workspace_controller.dart +++ b/app/lib/features/chat/workspace/chat_open_workspace_controller.dart @@ -1,43 +1,115 @@ import 'dart:async'; -import '../../../../core/api_gateway.dart'; +import '../../../core/api_gateway.dart'; import 'chat_open_workspace_gateway.dart'; +/// Keeps one device's open chats in step with the server's open-chat set. +/// +/// Writes are serialized: one is in flight at a time, and changes made while +/// it is pending coalesce into the latest full snapshot, sent next from the +/// revision the reply reported. Every reply and push is reconciled by server +/// revision, so a late or reordered one never overwrites newer state. A +/// snapshot from another device (or a refused stale write) wins over local +/// changes not yet sent, and reaches [onChanged]. class ChatOpenWorkspaceController { ChatOpenWorkspaceController({required ApiGateway gateway}) : _gateway = ChatOpenWorkspaceGateway(gateway); final ChatOpenWorkspaceGateway _gateway; StreamSubscription? _subscription; - int revision = 0; bool _disposed = false; + bool _writing = false; + List? _pending; + ChatOpenWorkspaceSnapshot? _current; + + /// Applies a server snapshot to the device's panes and windows. void Function(ChatOpenWorkspaceSnapshot snapshot)? onChanged; + + /// The latest server revision this device has seen; 0 before any. + int get revision => _current?.revision ?? 0; + + /// Whether the server answered, so local changes are sent. Before that (or + /// against a server without the request) the device keeps its own layout. + bool get ready => _current != null; + + bool get disposed => _disposed; + + /// Subscribes to pushes, then seeds the server from [local] (the device's + /// restored layout, migrated once from the old local snapshot) or, with no + /// local chats, reads the server's set. The server's answer is applied. Future start({ - Iterable legacy = const [], + Iterable local = const [], }) async { if (_subscription != null || _disposed) return; - _subscription = _gateway.changes.listen((snapshot) { - if (_disposed || snapshot.revision < revision) return; - revision = snapshot.revision; - onChanged?.call(snapshot); - }); - if (legacy.isNotEmpty) { - final snapshot = await _gateway.replace(legacy, seed: true); - if (snapshot != null && snapshot.revision >= revision) { - revision = snapshot.revision; - onChanged?.call(snapshot); + _subscription = _gateway.changes.listen(_accept); + final entries = local.toList(growable: false); + ChatOpenWorkspaceSnapshot? snapshot; + try { + snapshot = entries.isEmpty + ? await _gateway.read() + : await _gateway.replace(entries, seed: true); + } on Object { + return; + } + if (snapshot != null) _accept(snapshot, force: true); + } + + /// Sends the device's full open-chat set. Ignored until [ready]; coalesced + /// while a write is in flight. + void replace(Iterable entries) { + if (_disposed || !ready) return; + _pending = entries.toList(growable: false); + if (!_writing) unawaited(_drain()); + } + + Future _drain() async { + _writing = true; + try { + while (!_disposed) { + final sent = _pending; + _pending = null; + if (sent == null) return; + final current = _current; + if (current != null && current.sameEntries(sent)) continue; + ChatOpenWorkspaceSnapshot? reply; + try { + reply = await _gateway.replace(sent, baseRevision: revision); + } on Object { + reply = null; + } + if (_disposed || reply == null || reply.revision < revision) continue; + if (reply.rejected) { + // Another change landed first: the server's state wins over this + // write and anything queued behind it. + _accept(reply, force: true); + } else { + // The server may have left out closed or unknown chats. This device + // keeps showing them (a closed chat can still be read here); the + // other devices never receive them. + _current = reply; + } } + } finally { + _writing = false; } } - Future replace(Iterable entries) async { + void _accept(ChatOpenWorkspaceSnapshot snapshot, {bool force = false}) { if (_disposed) return; - final snapshot = await _gateway.replace(entries); - if (snapshot != null && snapshot.revision >= revision) { - revision = snapshot.revision; + final current = _current; + if (current != null && + (snapshot.revision < current.revision || + (!force && snapshot.revision == current.revision))) { + return; } + _current = snapshot; + _pending = null; + onChanged?.call(snapshot); } void dispose() { _disposed = true; + _pending = null; + onChanged = null; unawaited(_subscription?.cancel()); + _subscription = null; } } diff --git a/app/lib/features/chat/workspace/chat_open_workspace_gateway.dart b/app/lib/features/chat/workspace/chat_open_workspace_gateway.dart index 95405440..b5bf1df1 100644 --- a/app/lib/features/chat/workspace/chat_open_workspace_gateway.dart +++ b/app/lib/features/chat/workspace/chat_open_workspace_gateway.dart @@ -1,5 +1,7 @@ -import '../../../../core/api_gateway.dart'; +import '../../../core/api_gateway.dart'; +/// One chat in the server-owned open-chat set: its session and whether it is +/// floating. Geometry stays on the device. class ChatOpenWorkspaceEntry { const ChatOpenWorkspaceEntry(this.sessionId, {this.floating = false}); final String sessionId; @@ -17,10 +19,31 @@ class ChatOpenWorkspaceEntry { } } +/// The complete open-chat set at one server revision. [rejected] marks the +/// reply to a write from a stale base: the write was not applied and this is +/// the current state. class ChatOpenWorkspaceSnapshot { - const ChatOpenWorkspaceSnapshot(this.revision, this.entries); + const ChatOpenWorkspaceSnapshot( + this.revision, + this.entries, { + this.rejected = false, + }); final int revision; final List entries; + final bool rejected; + + bool sameEntries(Iterable other) { + final list = other.toList(growable: false); + if (list.length != entries.length) return false; + for (var i = 0; i < list.length; i++) { + if (list[i].sessionId != entries[i].sessionId || + list[i].floating != entries[i].floating) { + return false; + } + } + return true; + } + static ChatOpenWorkspaceSnapshot? fromJson(Object? value) { if (value is! Map || value['entries'] is! List) return null; final entries = []; @@ -36,15 +59,28 @@ class ChatOpenWorkspaceSnapshot { return ChatOpenWorkspaceSnapshot( (value['revision'] as num?)?.toInt() ?? 0, List.unmodifiable(entries), + rejected: value['rejected'] == true, ); } } +/// The `chat_open_workspace` request and its `chat_open_workspace_changed` +/// push, decoded. class ChatOpenWorkspaceGateway { ChatOpenWorkspaceGateway(this.gateway); final ApiGateway gateway; + + /// The current snapshot, without changing it. + Future read() async { + final response = await gateway.call('chat_open_workspace'); + return ChatOpenWorkspaceSnapshot.fromJson(response['chat_open_workspace']); + } + + /// Replaces the set, derived from [baseRevision]. A [seed] applies only + /// when the server never stored a set and otherwise returns the stored one. Future replace( Iterable entries, { + int baseRevision = 0, bool seed = false, }) async { final response = await gateway.call('chat_open_workspace', { @@ -52,6 +88,7 @@ class ChatOpenWorkspaceGateway { 'entries': entries .map((entry) => entry.toJson()) .toList(growable: false), + 'base_revision': baseRevision, if (seed) 'seed': true, }, }); diff --git a/app/lib/features/chat/workspace/chat_workspace_controller.dart b/app/lib/features/chat/workspace/chat_workspace_controller.dart index 0a7dc270..fdc825a5 100644 --- a/app/lib/features/chat/workspace/chat_workspace_controller.dart +++ b/app/lib/features/chat/workspace/chat_workspace_controller.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'chat_workspace_nodes.dart'; +import 'chat_workspace_reconcile.dart'; import 'chat_workspace_snapshot.dart'; export 'chat_workspace_nodes.dart'; @@ -203,45 +204,20 @@ class ChatWorkspaceController extends ChangeNotifier { _changed(); } - /// Reconciles tiled membership and order without discarding the local split - /// geometry. Floating membership is handled by the window host. + /// Tiles exactly [sessionIds] in order, as the server's open-chat set + /// names them, keeping this device's geometry (see + /// [reconcileChatWorkspaceTree]). Floating membership is the window host's. void reconcileSessions(Iterable sessionIds) { final wanted = []; final seen = {}; for (final id in sessionIds) { if (id.isNotEmpty && seen.add(id)) wanted.add(id); } - final current = panes; - if (wanted.isEmpty) { - if (_root == null) return; - _root = null; - _active = null; - _changed(); - return; - } - final retained = current.take(wanted.length).toList(); - for (var i = 0; i < retained.length; i++) { - final pane = retained[i]; - final id = wanted[i]; - if (pane.sessionId != id) { - _root = _replace(_root!, pane.id, ChatWorkspacePane(pane.id, id)); - } - } - for (final pane in current.skip(wanted.length).toList()) { - _root = _remove(_root!, pane.id); - } - if (retained.isEmpty) { - final pane = ChatWorkspacePane(_id('pane'), wanted.first); - _root = pane; - _active = pane.id; - } else { - for (final id in wanted.skip(retained.length)) { - splitActive(id, ChatSplitAxis.horizontal); - } - } - if (_active == null || !panes.any((pane) => pane.id == _active)) { - _active = panes.first.id; - } + final next = reconcileChatWorkspaceTree(_root, wanted, _id); + if (identical(next, _root)) return; + _root = next; + final ids = panes.map((pane) => pane.id).toSet(); + if (!ids.contains(_active)) _active = ids.firstOrNull; _changed(); } diff --git a/app/lib/features/chat/workspace/chat_workspace_reconcile.dart b/app/lib/features/chat/workspace/chat_workspace_reconcile.dart new file mode 100644 index 00000000..537cccf9 --- /dev/null +++ b/app/lib/features/chat/workspace/chat_workspace_reconcile.dart @@ -0,0 +1,118 @@ +import 'chat_workspace_nodes.dart'; + +/// Returns [root] tiled with exactly [wanted] (distinct session ids) in +/// order, keeping the local geometry: panes whose chat left are removed with +/// their splits, surviving panes keep their ids and split sizes and move +/// between slots to follow the order, and new chats split off the last pane. +/// Returns [root] itself when it already matches. +ChatWorkspaceNode? reconcileChatWorkspaceTree( + ChatWorkspaceNode? root, + List wanted, + String Function(String kind) newId, +) { + if (wanted.isEmpty) return null; + final keep = wanted.toSet(); + final seen = {}; + var node = root == null ? null : _retain(root, keep, seen); + final leaves = node == null ? [] : _leaves(node).toList(); + final bySession = {for (final pane in leaves) pane.sessionId: pane}; + final order = wanted.where(bySession.containsKey).toList(); + var slot = 0; + if (node != null) { + node = _reorder(node, () => bySession[order[slot++]]!); + } + for (final id in wanted.where((id) => !bySession.containsKey(id))) { + final added = ChatWorkspacePane(newId('pane'), id); + if (node == null) { + node = added; + continue; + } + final last = _leaves(node).last; + node = _swapLeaf( + node, + last.id, + ChatWorkspaceSplit(newId('split'), ChatSplitAxis.horizontal, last, added), + ); + } + return _same(node, root) ? root : node; +} + +Iterable _leaves(ChatWorkspaceNode node) sync* { + if (node is ChatWorkspacePane) yield node; + if (node is ChatWorkspaceSplit) { + yield* _leaves(node.first); + yield* _leaves(node.second); + } +} + +/// Drops panes outside [keep] and repeats of a session already kept. +ChatWorkspaceNode? _retain( + ChatWorkspaceNode node, + Set keep, + Set seen, +) { + if (node is ChatWorkspacePane) { + return keep.contains(node.sessionId) && seen.add(node.sessionId) + ? node + : null; + } + final split = node as ChatWorkspaceSplit; + final first = _retain(split.first, keep, seen); + final second = _retain(split.second, keep, seen); + if (first == null) return second; + if (second == null) return first; + return ChatWorkspaceSplit( + split.id, + split.axis, + first, + second, + ratio: split.ratio, + ); +} + +ChatWorkspaceNode _reorder( + ChatWorkspaceNode node, + ChatWorkspacePane Function() next, +) { + if (node is ChatWorkspacePane) return next(); + final split = node as ChatWorkspaceSplit; + final first = _reorder(split.first, next); + return ChatWorkspaceSplit( + split.id, + split.axis, + first, + _reorder(split.second, next), + ratio: split.ratio, + ); +} + +ChatWorkspaceNode _swapLeaf( + ChatWorkspaceNode node, + String id, + ChatWorkspaceNode replacement, +) { + if (node.id == id) return replacement; + if (node is! ChatWorkspaceSplit) return node; + return ChatWorkspaceSplit( + node.id, + node.axis, + _swapLeaf(node.first, id, replacement), + _swapLeaf(node.second, id, replacement), + ratio: node.ratio, + ); +} + +bool _same(ChatWorkspaceNode? a, ChatWorkspaceNode? b) { + if (a == null || b == null) return a == b; + if (a is ChatWorkspacePane && b is ChatWorkspacePane) { + return a.id == b.id && a.sessionId == b.sessionId; + } + if (a is ChatWorkspaceSplit && b is ChatWorkspaceSplit) { + return a.id == b.id && + a.axis == b.axis && + a.ratio == b.ratio && + _same(a.first, b.first) && + _same(a.second, b.second); + } + return false; +} diff --git a/app/notes/chat-open-workspace-sync.md b/app/notes/chat-open-workspace-sync.md index a170cfcb..cd21250a 100644 --- a/app/notes/chat-open-workspace-sync.md +++ b/app/notes/chat-open-workspace-sync.md @@ -2,33 +2,62 @@ ## Behaviour -The authenticated `/connect` protocol now stores one bounded ordered workspace -per server scope. Entries contain only operator session ids and a floating bit. -Opening, closing, reordering, docking and floating publish a full authoritative -snapshot; other connections receive `chat_open_workspace_changed`. The first -client may seed an empty server snapshot from its legacy local layout. Split -ratios, pane ids, window rectangles and other presentation geometry remain local. +The authenticated `/connect` protocol stores one bounded ordered open-chat set +per server scope (`default`). Entries hold only an operator session id and a +floating bit; split ratios, pane ids and window rectangles stay on the device. -The server drops closed, missing, unowned, or no-longer-live broker sessions -before saving. The store caps membership at 64 entries and rejects duplicates or -invalid ids. Protocol and application parsers apply the same bounds. +* `chat_open_workspace` without `chat_open_workspace_request` reads the set. + With a request it replaces the set as a compare-and-swap on `base_revision`: + a write from a stale base is not applied and the reply carries the current + set with `rejected: true`. Each stored change increments `revision`; an + identical write stores nothing. +* `seed: true` applies only when the server never stored a set (even an + emptied set wins over an old device-local snapshot), so the migration from + the old local layout happens once. +* Stored changes are pushed to every other connection as + `chat_open_workspace_changed`. Pushes run on separate goroutines and can + arrive out of order; clients keep the highest revision. +* Only recorded, not-closed operator chats are kept (registry check; the broker + is not asked, so validation never starts a harness). `chat_close` drops the + chat and pushes the new revision to every connection. A new connection + prunes stale entries and pushes the result to all connections, else sends + the current set to the new connection only (reconnect catch-up). -## Security and limits +App (`ChatOpenWorkspaceController`): one write in flight at a time; later +changes coalesce into the newest full set, sent from the revision of the last +reply. Pushes and replies with a lower (or, for pushes, equal) revision are +ignored. A push or a rejected reply wins over unsent local changes. After the +local layout is restored, the device seeds (or, with no chats, reads) and +applies the answer; the first answer keeps a deep-linked chat. Server drops in +a successful reply are not applied locally, so a closed chat can still be read +on the device that opened it, but no other device receives it. Remote +membership is applied with `reconcileChatWorkspaceTree`: removed chats' panes +go, survivors keep pane ids and split sizes and move to follow the order, new +chats split off the last pane. The controller is disposed on a server or +gateway rebind. A federated (all-servers) list keeps its local layout. -Workspace writes use the existing authenticated websocket. The server requires -both an operator-session registry row and a live broker session, so pipeline-owned -or another user's ids cannot be retained. No credentials, transcripts, drafts, -tokens or geometry are persisted. The current scope is the authenticated local -server's single operator (`default`); multi-user identity is not yet modelled. +Ctrl+Tab: main's behaviour (floating chats when shown, else visible panes) is +kept; the single-pane recent-chat swap is removed there. ## Verification -* `go test ./internal/protocol -run 'Test.*Chat|Test.*Workspace'` — passed. -* `go test ./internal/pipeline -run 'TestChatOpenWorkspace'` — passed. -* `go test ./internal/server -run 'TestChat(Open|Close|NewSession)|TestEmbeddedSchedulesReadOnlyOverWebSocket|TestStorageWebsocketProtocolAndNoStartupCleanup|TestTemplatesDoNotSeedDatabaseAndCreateOnlyPreservesExisting'` — passed. -* `go build ./cmd/pipelines-server` — passed. -* `cd app && tool/memwatch/full.sh 'test/features/chat/workspace/chat_open_workspace_gateway_test.dart test/features/chat/workspace/chat_workspace_binding_test.dart'` — 7 passed, 0 failed, 0 skipped; peak 800 MB; exit 0. -* `cd app && tool/analyze.sh` — clean; 0 errors, 0 warnings, 2 infos before the brace cleanup; rerun after cleanup was not captured. +* Go: `internal/pipeline` `TestChatOpenWorkspace*` (seed once, stale revision, + remove, bounds); `internal/server` `TestChatOpenWorkspaceSyncsEveryConnection` + (two connections get the same snapshot, stale write rejected, reorder, + close pruned and pushed to both, closed chat not restorable) and + `TestChatOpenWorkspacePrunesOnConnectForEveryone`. +* App: `test/features/chat/workspace/chat_open_workspace_controller_test.dart` + (serialized writes, late reply vs newer push, rejected write, dispose), + `chat_workspace_reconcile_test.dart`, and + `test/features/chat/chat_open_workspace_sync_test.dart` (seed migration, + remote removal drops the pane, server set wins over an old local snapshot, + rebind disposes the old sync). +* Live two-client check (2026-11-12): server built from this worktree, run on + an OS-assigned loopback port with a scratch HOME/XDG and database, three + scratch operator rows; a Node script drove two websocket clients through + read, open, reorder, stale write, float, pane close, `chat_close`, write-back + of the closed chat, and an out-of-band close pruned on a third connection. + All 16 checks passed (`RESULT: PASS`); every connection received the same + revisions. The scratch server and its own OpenCode child were stopped. -Not tested: a live two-client scratch websocket/broker check and rendered UI -captures. The shared server was not restarted or contacted. +Not verified: rendered UI captures and on-device behaviour. diff --git a/app/test/features/chat/chat_open_workspace_sync_test.dart b/app/test/features/chat/chat_open_workspace_sync_test.dart new file mode 100644 index 00000000..8b7c1379 --- /dev/null +++ b/app/test/features/chat/chat_open_workspace_sync_test.dart @@ -0,0 +1,205 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/core/servers/server_scope.dart'; +import 'package:pipelines_app/features/chat/screen.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace_store.dart'; +import 'package:pipelines_app/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; + +import 'conversation/chat_thread_fixtures.dart'; + +/// A chat server that keeps the open-chat set with the real revision rules: +/// a seed applies only to a never-stored set, a write from a stale base is +/// refused with the current set. +class _Server extends FakeChatGateway { + _Server({this.revision = 0, List open = const []}) + : open = List.of(open); + int revision; + List open; + final writes = >[]; + + @override + Future call(String type, [JsonMap fields = const {}]) { + if (type == 'chat_sessions') { + return Future.value({ + 'chat_sessions': [ + for (final id in ['a', 'b', 'c', 'd']) + {'id': id, 'title': 'Chat $id'}, + ], + }); + } + if (type == 'chat_shells') return Future.value({'chat_shells': []}); + if (type == 'chat_open_workspace') return Future.value(_workspace(fields)); + return super.call(type, fields); + } + + JsonMap _workspace(JsonMap fields) { + final request = fields['chat_open_workspace_request'] as Map?; + var rejected = false; + if (request != null) { + final ids = [ + for (final entry in request['entries'] as List) + '${entry['session_id']}', + ]; + writes.add(ids); + if (request['seed'] == true) { + if (revision == 0) { + open = ids; + revision = 1; + } + } else if (request['base_revision'] != revision) { + rejected = true; + } else { + open = ids; + revision++; + } + } + return { + 'type': 'chat_open_workspace', + 'chat_open_workspace': _body(rejected), + }; + } + + JsonMap _body([bool rejected = false]) => { + 'revision': revision, + 'entries': [ + for (final id in open) {'session_id': id}, + ], + if (rejected) 'rejected': true, + }; + + /// Another device (or the server) changed the set. + void change(List ids) { + open = List.of(ids); + revision++; + emitRaw({ + 'type': 'chat_open_workspace_changed', + 'chat_open_workspace': _body(), + }); + } +} + +ChatWorkspaceController _workspace(WidgetTester tester) => + tester.widget(find.byType(ChatWorkspace)).controller; + +List _open(WidgetTester tester) => + _workspace(tester).panes.map((pane) => pane.sessionId).toList(); + +Future _mount(WidgetTester tester, _Server server) async { + await tester.pumpWidget( + MaterialApp( + home: AdaptiveShell( + destination: AppDestination.chat, + destinationBodies: { + AppDestination.chat: (_) => + ChatScreen(gateway: server, persistWorkspace: true), + }, + ), + ), + ); + await tester.pumpAndSettle(); +} + +_Server _server( + String identity, { + int revision = 0, + List open = const [], +}) { + final server = _Server(revision: revision, open: open) + ..history = (_) async => chatPage([]); + bindServerIdentity(server, identity); + addTearDown(server.close); + return server; +} + +void _saveLocal(String identity, List ids) { + final saved = ChatWorkspaceController()..open(ids.first); + for (final id in ids.skip(1)) { + saved.splitActive(id, ChatSplitAxis.horizontal); + } + SharedPreferences.setMockInitialValues({ + scopedServerKey(chatWorkspacePreferenceKey, identity): saved + .snapshot()! + .encode(), + }); +} + +void main() { + testWidgets( + 'the local layout seeds a new server and remote removal drops its pane', + (tester) async { + tester.view.physicalSize = const Size(1800, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + _saveLocal('server-a', ['a', 'b']); + final server = _server('server-a'); + await _mount(tester, server); + expect(server.revision, 1); + expect(server.open, ['a', 'b']); + expect(_open(tester), ['a', 'b']); + + server.change(['b']); + await tester.pumpAndSettle(); + expect(_open(tester), ['b']); + // Applying the server's change is not written back. + expect(server.writes, hasLength(1)); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpAndSettle(); + }, + ); + + testWidgets('an existing server set wins over an old local snapshot', ( + tester, + ) async { + tester.view.physicalSize = const Size(1800, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + _saveLocal('server-a', ['a']); + final server = _server('server-a', revision: 3, open: ['b', 'c']); + await _mount(tester, server); + expect(_open(tester), ['b', 'c']); + expect(server.open, ['b', 'c']); + expect(server.revision, 3); + // A local change is sent from the server's revision and accepted. + _workspace(tester).close(_workspace(tester).panes.first.id); + await tester.pumpAndSettle(); + expect(server.open, ['c']); + expect(server.revision, 4); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpAndSettle(); + }); + + testWidgets('rebinding to another server disposes the old sync', ( + tester, + ) async { + tester.view.physicalSize = const Size(1800, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + SharedPreferences.setMockInitialValues({}); + final first = _server('server-a', revision: 1, open: ['a']); + final second = _server('server-b', revision: 1, open: ['c']); + await _mount(tester, first); + expect(_open(tester), ['a']); + await _mount(tester, second); + expect(_open(tester), ['c']); + + // The old server's pushes no longer reach this screen. + first.change(['a', 'b', 'd']); + await tester.pumpAndSettle(); + expect(_open(tester), ['c']); + // The new server's do, and local changes go only to it. + second.change(['c', 'd']); + await tester.pumpAndSettle(); + expect(_open(tester), ['c', 'd']); + final firstWrites = first.writes.length; + _workspace(tester).close(_workspace(tester).panes.last.id); + await tester.pumpAndSettle(); + expect(second.open, ['c']); + expect(first.writes, hasLength(firstWrites)); + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpAndSettle(); + }); +} diff --git a/app/test/features/chat/workspace/chat_open_workspace_controller_test.dart b/app/test/features/chat/workspace/chat_open_workspace_controller_test.dart new file mode 100644 index 00000000..d3d33438 --- /dev/null +++ b/app/test/features/chat/workspace/chat_open_workspace_controller_test.dart @@ -0,0 +1,211 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_open_workspace_controller.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_open_workspace_gateway.dart'; + +/// Answers each chat_open_workspace call only when the test completes it. +class _Gateway implements ApiGateway { + final requests = <({JsonMap fields, Completer reply})>[]; + final _frames = StreamController.broadcast(); + + @override + Future call(String type, [JsonMap fields = const {}]) { + final reply = Completer(); + requests.add((fields: fields, reply: reply)); + return reply.future; + } + + @override + Stream get frames => _frames.stream; + + void push(int revision, List ids) => _frames.add({ + 'type': 'chat_open_workspace_changed', + 'chat_open_workspace': _snapshot(revision, ids), + }); + + void answer( + int index, + int revision, + List ids, { + bool rejected = false, + }) => requests[index].reply.complete({ + 'type': 'chat_open_workspace', + 'chat_open_workspace': _snapshot(revision, ids, rejected: rejected), + }); + + List sentIds(int index) => [ + for (final entry + in (requests[index].fields['chat_open_workspace_request'] + as Map)['entries'] + as List) + '${entry['session_id']}', + ]; + + int base(int index) => + (requests[index].fields['chat_open_workspace_request'] + as Map)['base_revision']; +} + +JsonMap _snapshot(int revision, List ids, {bool rejected = false}) => { + 'revision': revision, + 'entries': [ + for (final id in ids) {'session_id': id}, + ], + if (rejected) 'rejected': true, +}; + +List _entries(List ids) => [ + for (final id in ids) ChatOpenWorkspaceEntry(id), +]; + +Future<(ChatOpenWorkspaceController, _Gateway, List>)> _started({ + int revision = 1, + List ids = const ['a'], +}) async { + final gateway = _Gateway(); + final applied = >[]; + final controller = ChatOpenWorkspaceController(gateway: gateway) + ..onChanged = (snapshot) => + applied.add(snapshot.entries.map((e) => e.sessionId).toList()); + final started = controller.start(local: _entries(ids)); + await pumpEventQueue(); + expect(gateway.requests.single.fields['chat_open_workspace_request'], { + 'entries': [ + for (final id in ids) {'session_id': id}, + ], + 'base_revision': 0, + 'seed': true, + }); + gateway.answer(0, revision, ids); + await started; + return (controller, gateway, applied); +} + +void main() { + test('reads instead of seeding when the device has no chats', () async { + final gateway = _Gateway(); + final controller = ChatOpenWorkspaceController(gateway: gateway); + final started = controller.start(); + await pumpEventQueue(); + expect(gateway.requests.single.fields, isEmpty); + gateway.answer(0, 4, ['x']); + await started; + expect(controller.revision, 4); + expect(controller.ready, isTrue); + }); + + test( + 'serializes writes and sends the latest from the replied revision', + () async { + final (controller, gateway, _) = await _started(); + controller.replace(_entries(['a', 'b'])); + controller.replace(_entries(['a', 'b', 'c'])); + controller.replace(_entries(['c', 'a', 'b'])); + await pumpEventQueue(); + // One write in flight; the two later ones coalesced behind it. + expect(gateway.requests, hasLength(2)); + expect(gateway.sentIds(1), ['a', 'b']); + expect(gateway.base(1), 1); + gateway.answer(1, 2, ['a', 'b']); + await pumpEventQueue(); + expect(gateway.requests, hasLength(3)); + expect(gateway.sentIds(2), ['c', 'a', 'b']); + expect(gateway.base(2), 2); + gateway.answer(2, 3, ['c', 'a', 'b']); + await pumpEventQueue(); + expect(controller.revision, 3); + // An unchanged set is not sent again. + controller.replace(_entries(['c', 'a', 'b'])); + await pumpEventQueue(); + expect(gateway.requests, hasLength(3)); + }, + ); + + test('a late reply never overwrites a newer pushed revision', () async { + final (controller, gateway, applied) = await _started(); + controller.replace(_entries(['a', 'b'])); + await pumpEventQueue(); + // Another device's change (revision 3) is pushed before this write's + // reply (revision 2) arrives. + gateway.push(3, ['z']); + await pumpEventQueue(); + gateway.answer(1, 2, ['a', 'b']); + await pumpEventQueue(); + expect(controller.revision, 3); + expect(applied.last, ['z']); + // Older and repeated pushes are ignored. + gateway.push(2, ['a', 'b']); + gateway.push(3, ['z']); + await pumpEventQueue(); + expect(applied, [ + ['a'], + ['z'], + ]); + }); + + test( + 'a refused stale write applies the server state and drops queued ones', + () async { + final (controller, gateway, applied) = await _started(); + controller.replace(_entries(['a', 'b'])); + await pumpEventQueue(); + controller.replace(_entries(['a', 'b', 'c'])); + gateway.answer(1, 5, ['q'], rejected: true); + await pumpEventQueue(); + expect(applied.last, ['q']); + expect(controller.revision, 5); + expect(gateway.requests, hasLength(2)); + // The next change is derived from the server's state. + controller.replace(_entries(['q', 'r'])); + await pumpEventQueue(); + expect(gateway.base(2), 5); + }, + ); + + test('a push applied while writing replaces the queued write', () async { + final (controller, gateway, applied) = await _started(); + controller.replace(_entries(['a', 'b'])); + await pumpEventQueue(); + controller.replace(_entries(['a', 'b', 'c'])); + gateway.push(4, ['b']); + await pumpEventQueue(); + expect(applied.last, ['b']); + gateway.answer(1, 4, ['b'], rejected: true); + await pumpEventQueue(); + expect(gateway.requests, hasLength(2)); + }); + + test('dispose stops pushes and writes', () async { + final (controller, gateway, applied) = await _started(); + controller.dispose(); + gateway.push(9, ['late']); + controller.replace(_entries(['x'])); + await pumpEventQueue(); + expect(applied, [ + ['a'], + ]); + expect(gateway.requests, hasLength(1)); + }); + + test( + 'stays local-only when the server does not answer the request', + () async { + final gateway = _Gateway(); + final controller = ChatOpenWorkspaceController(gateway: gateway); + final started = controller.start(local: _entries(['a'])); + await pumpEventQueue(); + gateway.requests.single.reply.completeError(StateError('unknown type')); + await started; + expect(controller.ready, isFalse); + controller.replace(_entries(['a', 'b'])); + await pumpEventQueue(); + expect(gateway.requests, hasLength(1)); + // A server push (a reconnect) brings it up to date and enables writes. + gateway.push(2, ['a']); + await pumpEventQueue(); + expect(controller.ready, isTrue); + }, + ); +} diff --git a/app/test/features/chat/workspace/chat_open_workspace_gateway_test.dart b/app/test/features/chat/workspace/chat_open_workspace_gateway_test.dart index f646d215..6d54ee9a 100644 --- a/app/test/features/chat/workspace/chat_open_workspace_gateway_test.dart +++ b/app/test/features/chat/workspace/chat_open_workspace_gateway_test.dart @@ -11,7 +11,10 @@ void main() { ], }); expect(snapshot?.revision, 7); - expect(snapshot?.entries.map((entry) => entry.sessionId), ['ses_a', 'ses_b']); + expect(snapshot?.entries.map((entry) => entry.sessionId), [ + 'ses_a', + 'ses_b', + ]); expect(snapshot?.entries.first.floating, isTrue); }); diff --git a/app/test/features/chat/workspace/chat_workspace_reconcile_test.dart b/app/test/features/chat/workspace/chat_workspace_reconcile_test.dart new file mode 100644 index 00000000..4ee23320 --- /dev/null +++ b/app/test/features/chat/workspace/chat_workspace_reconcile_test.dart @@ -0,0 +1,56 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/workspace/chat_workspace_controller.dart'; + +List _sessions(ChatWorkspaceController c) => + c.panes.map((pane) => pane.sessionId).toList(); + +ChatWorkspaceController _three() { + final c = ChatWorkspaceController()..open('a'); + c.splitActive('b', ChatSplitAxis.vertical); + c.splitActive('c', ChatSplitAxis.horizontal); + c.resize(c.root!.id, .3); + return c; +} + +void main() { + test('a remote removal drops only that pane and keeps the geometry', () { + final c = _three(); + final paneA = c.panes.first.id; + final paneC = c.panes.last.id; + c.reconcileSessions(['a', 'c']); + expect(_sessions(c), ['a', 'c']); + expect(c.panes.map((pane) => pane.id), [paneA, paneC]); + expect((c.root as ChatWorkspaceSplit).ratio, .3); + }); + + test('a reorder moves panes with their ids and keeps the splits', () { + final c = _three(); + final ids = {for (final pane in c.panes) pane.sessionId: pane.id}; + final splitId = c.root!.id; + c.reconcileSessions(['c', 'a', 'b']); + expect(_sessions(c), ['c', 'a', 'b']); + expect(c.panes.map((pane) => pane.id), [ids['c'], ids['a'], ids['b']]); + expect(c.root!.id, splitId); + expect((c.root as ChatWorkspaceSplit).ratio, .3); + }); + + test('new chats split off and an unchanged set notifies nothing', () { + final c = ChatWorkspaceController()..open('a'); + c.reconcileSessions(['a', 'b']); + expect(_sessions(c), ['a', 'b']); + var notified = 0; + c.addListener(() => notified++); + c.reconcileSessions(['a', 'b']); + expect(notified, 0); + c.reconcileSessions(const []); + expect(c.panes, isEmpty); + expect(c.activePane, isNull); + }); + + test('the active pane moves on when its chat is removed', () { + final c = _three(); + expect(c.activePane?.sessionId, 'c'); + c.reconcileSessions(['b']); + expect(c.activePane?.sessionId, 'b'); + }); +} diff --git a/internal/pipeline/chat_open_workspace.go b/internal/pipeline/chat_open_workspace.go index 71aac61f..8ee73fab 100644 --- a/internal/pipeline/chat_open_workspace.go +++ b/internal/pipeline/chat_open_workspace.go @@ -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 } diff --git a/internal/pipeline/chat_open_workspace_test.go b/internal/pipeline/chat_open_workspace_test.go index f183be96..877ca1e9 100644 --- a/internal/pipeline/chat_open_workspace_test.go +++ b/internal/pipeline/chat_open_workspace_test.go @@ -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") } } diff --git a/internal/protocol/chat_open_workspace.go b/internal/protocol/chat_open_workspace.go index c16ea661..4a30096b 100644 --- a/internal/protocol/chat_open_workspace.go +++ b/internal/protocol/chat_open_workspace.go @@ -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"` } diff --git a/internal/server/chat.go b/internal/server/chat.go index 82d3536e..6ae310b0 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -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) diff --git a/internal/server/chat_close.go b/internal/server/chat_close.go index a5cab98d..d5eb98c6 100644 --- a/internal/server/chat_close.go +++ b/internal/server/chat_close.go @@ -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 diff --git a/internal/server/chat_open_workspace.go b/internal/server/chat_open_workspace.go index d9752197..dc707809 100644 --- a/internal/server/chat_open_workspace.go +++ b/internal/server/chat_open_workspace.go @@ -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) } } diff --git a/internal/server/chat_open_workspace_test.go b/internal/server/chat_open_workspace_test.go new file mode 100644 index 00000000..96818c11 --- /dev/null +++ b/internal/server/chat_open_workspace_test.go @@ -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)) + } +} diff --git a/internal/server/chat_session_changes.go b/internal/server/chat_session_changes.go index 609a6e19..0b2cba75 100644 --- a/internal/server/chat_session_changes.go +++ b/internal/server/chat_session_changes.go @@ -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 { diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 71ed4ba0..2fa57fe8 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -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)