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:
2026-11-12 03:23:22 +00:00
parent f8e081825f
commit 4f8e99f316
20 changed files with 1333 additions and 276 deletions
@@ -101,12 +101,42 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
}
}
/// 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<void> 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<ChatScreen> {
_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<ChatScreen> {
.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<ChatScreen> {
'${_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(() {});