Skip to content

Add reliable cross-tab sync for canvas PUT updates - #88

Open
cxxxxxn (cxxxxxn) wants to merge 9 commits into
microsoft:mainfrom
cxxxxxn:sync-put-broadcast
Open

Add reliable cross-tab sync for canvas PUT updates#88
cxxxxxn (cxxxxxn) wants to merge 9 commits into
microsoft:mainfrom
cxxxxxn:sync-put-broadcast

Conversation

@cxxxxxn

Copy link
Copy Markdown
Contributor

Summary

  • identify each browser tab with a stable clientId and propagate it through canvas PUT and SSE update payloads
  • diff successful canvas PUTs under the canvas mutex and broadcast structural changes to other tabs
  • filter self-originated updates on the client
  • add SSE heartbeats, reconnect backoff, and fast recovery after a stalled connection
  • cover PUT diff, publish, deletion, no-op, and originator behavior

This 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)
  • server test suite (894 passed, 2 todo)
  • shared, server, and web typechecks
  • Prettier and git diff --check

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants