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
+52 -23
View File
@@ -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.