Add reliable cross-tab sync for canvas PUT updates - #88
Open
cxxxxxn (cxxxxxn) wants to merge 9 commits into
Open
Add reliable cross-tab sync for canvas PUT updates#88cxxxxxn (cxxxxxn) wants to merge 9 commits into
cxxxxxn (cxxxxxn) wants to merge 9 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds cross-tab synchronization for legacy full-state canvas PUTs.
Changes:
- Adds per-tab client identity and self-echo filtering.
- Broadcasts PUT-derived structural deltas under the canvas mutex.
- Adds SSE heartbeats, reconnection logic, and server tests.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
packages/shared/src/types/api/canvas.ts |
Adds PUT clientId. |
packages/shared/src/types/api/canvas-sync.ts |
Adds broadcast originator identity. |
apps/web/src/store/clientId.ts |
Generates per-tab identity. |
apps/web/src/store/canvasSyncStore.ts |
Filters echoes and reconnects SSE. |
apps/web/src/api/canvas.ts |
Stamps PUT requests with client identity. |
apps/server/src/modules/canvas/sync.route.ts |
Adds SSE heartbeats. |
apps/server/src/modules/canvas/canvas.route.ts |
Serializes and broadcasts PUT changes. |
apps/server/src/modules/canvas/canvas-sync.ts |
Documents PUT broadcasting. |
apps/server/src/modules/canvas/canvas-executor.ts |
Diffs, logs, and publishes PUT updates. |
apps/server/src/modules/canvas/broadcast-canvas-put.test.ts |
Tests PUT diff and publication behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1320
to
+1321
| const hydratedPrev = hydrateNodes(store, prevNodes); | ||
| const hydratedNext = hydrateNodes(store, nextNodes); |
| { nodes: hydratedNext, edges: nextEdges as CanvasEdge[] }, | ||
| ); | ||
|
|
||
| if (deltas.length === 0) return deltas; |
| deltas: deltas as unknown[], | ||
| originator: { source: 'ui', ...(clientId ? { tabId: clientId } : {}) }, | ||
| }; | ||
| store.appendDeltaLogEntry(logEntry); |
Comment on lines
+153
to
+166
| // event.type === 'update' | ||
| // Skip our own PUT echo (P2 / Plan A): the originating tab has | ||
| // already applied this edit optimistically, so re-applying the | ||
| // broadcast would be redundant (and could fight a still-pending | ||
| // local edit). Server-originated writes carry no id and pass. | ||
| if (event.data.originatorClientId === CLIENT_ID) return; | ||
| const { fromVersion, toVersion, deltas, pendingEffects } = event.data; | ||
| let skippedNodeIds: string[] = []; | ||
| if (fromVersion === canvasStore.version) { | ||
| skippedNodeIds = canvasStore.applyDeltasFromAgent( | ||
| deltas as Delta[], | ||
| toVersion, | ||
| pendingEffects as SyncPendingEffects, | ||
| ); |
Comment on lines
+199
to
+202
| // Reconnect loop: SSE streams drop (proxy idle-reap, network blips, | ||
| // server restart). Each reconnect replays the `snapshot` handshake, | ||
| // which re-runs the version reconcile above and heals any gap missed | ||
| // while disconnected. `disconnect()` aborts the signal to break out. |
Comment on lines
+210
to
+216
| if (!response.ok) throw new Error(`sync stream ${response.status}`); | ||
| backoffMs = 1_000; // healthy connection — reset backoff | ||
| await readTypedSSEStream<CanvasSyncEvent>( | ||
| response, | ||
| handleEvent, | ||
| signal, | ||
| ); |
Comment on lines
+223
to
+233
| await new Promise<void>((resolve) => { | ||
| const timer = setTimeout(resolve, backoffMs); | ||
| signal.addEventListener( | ||
| 'abort', | ||
| () => { | ||
| clearTimeout(timer); | ||
| resolve(); | ||
| }, | ||
| { once: true }, | ||
| ); | ||
| }); |
Comment on lines
+15
to
+20
| * in-process via `executeOnServer` (C2), plus user hand-edits via the | ||
| * autosave PUT (P2 / Plan A, `broadcastCanvasStatePut`). The chat SSE | ||
| * tool result no longer applies canvas state, so the initiating tab is a | ||
| * plain receiver that applies its own change once, from this broadcast. | ||
| * User-edit broadcasts carry `originatorClientId` so the originating tab | ||
| * skips its own PUT echo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
clientIdand propagate it through canvas PUT and SSE update payloadsThis is the compatibility sync layer for the field-delta migration: existing full-state PUT writers remain visible to other tabs while newer delta writers are introduced incrementally.
Validation
pnpm exec vitest run src/modules/canvas/broadcast-canvas-put.test.ts(4 passed)git diff --check