Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(team): a re-creation waits for our own pending remove of the same id

reviewc4485fda6a2e
diff --git a/docs/team-presence.md b/docs/team-presence.md
index 94d9124ec..ced4598e6 100644
--- a/docs/team-presence.md
+++ b/docs/team-presence.md
@@ -772,6 +772,18 @@ the receiving side: **a `remove` is never held off by rule 2's suppression** —
unacked mutation wins everywhere", which under rule 4 it no longer does, so suppressing the remove
would be the one way to disagree with our peers.
+**A re-creation waits for our OWN remove's echo** (`CanvasOrder.hasPendingRemove`). Our remove enters
+our `seen` only when its echo comes back, so a re-creation of the same id cast before that — a link
+deleted and redrawn, a node deleted and ⌘Z'd, inside one round trip — carried a `seen` below the
+remove: every peer dropped it as a stale frame while we kept showing it, and the next whole-file save
+became last-writer-wins on disk. Canvas's send callback now refuses any non-remove op for a key with a
+remove of ours in flight (counted per key, not TTL-bound — a late ack is exactly when it matters, and a
+lost one comes with a reconnect, whose `reset` clears it). The refusal keeps the op owed in the
+publisher, and an adopt in that window keeps an owed EDGE owed as well as an owed node (a teammate's
+op is usually what arrives during that round trip). Nothing else would ever cast it — our echo is an
+ack and changes no React state — so the echo that clears the gate re-publishes, after the handler and
+only when the publisher `hasOwed()`.
+
`seen` is client-supplied and it *decides* something, so the reflector **bounds** it
(`stampMutation`): it can never legitimately reach the order the mutation is being given, so it is
clamped there, and a non-integer/negative one is dropped. A mutation with **no** `seen` (an older
diff --git a/src/core/canvas-sync.convergence.test.ts b/src/core/canvas-sync.convergence.test.ts
index 06bc0ca55..82a18bac9 100644
--- a/src/core/canvas-sync.convergence.test.ts
+++ b/src/core/canvas-sync.convergence.test.ts
@@ -26,7 +26,13 @@ import {
isCanvasMutation,
type CanvasScene
} from '../shared/canvas-mutations'
-import { createCanvasOrder, createReconnectWatch, PENDING_TTL_MS } from '../shared/canvas-order'
+import {
+ createCanvasOrder,
+ createReconnectWatch,
+ isRemoveOp,
+ mutationKey,
+ PENDING_TTL_MS
+} from '../shared/canvas-order'
import { createCanvasPublisher, publishableScene } from '../shared/canvas-publish'
import { IPC } from '../shared/ipc'
import type { BridgeLink, CanvasMutation, CanvasNodeState } from '../shared/types'
@@ -179,6 +185,9 @@ class Client {
// Stamp our causal position FIRST, so the guard judges the exact payload that is cast
// (canvas-order rule 4 — `seen` is what lets a delete beat a concurrent drag frame).
const stamped = this.order.stamp(m)
+ // The re-creation gate, as Canvas has it: a re-creation of a key whose remove of ours is
+ // still unacked would carry a `seen` below that remove — held (owed) until the echo lands.
+ if (!isRemoveOp(stamped) && this.order.hasPendingRemove(mutationKey(stamped))) return false
if (!isCanvasMutation(stamped)) {
this.refused++
return false
@@ -191,14 +200,22 @@ class Client {
)
bus.deliver.set(id, (projectId, m) => {
if (projectId !== PROJECT) return
- if (!this.order.accept(m)) return
- this.applied++
- this.states = applyCanvasMutation(this.states, m)
- // One id is one edge across both lists — what Canvas and the projects store apply.
- const edges = applyEdgeMutationToScene({ bridges: this.bridges, ropes: this.ropes }, m)
- this.bridges = edges.bridges
- this.ropes = edges.ropes
- this.pub.adopt(this.publishable()) // loop guard — never re-publish someone else's change
+ // Mirrors Canvas: our own remove coming back RELEASES a re-creation the gate held — cast it now,
+ // after this mutation is handled (a repaired remove is applied first), or it waits for an edit.
+ const key = mutationKey(m)
+ const held = this.order.hasPendingRemove(key)
+ const apply = this.order.accept(m)
+ const released = held && !this.order.hasPendingRemove(key)
+ if (apply) {
+ this.applied++
+ this.states = applyCanvasMutation(this.states, m)
+ // One id is one edge across both lists — what Canvas and the projects store apply.
+ const edges = applyEdgeMutationToScene({ bridges: this.bridges, ropes: this.ropes }, m)
+ this.bridges = edges.bridges
+ this.ropes = edges.ropes
+ this.pub.adopt(this.publishable()) // loop guard — never re-publish someone else's change
+ }
+ if (released && this.pub.hasOwed()) this.pub.publish(this.publishable())
})
}
@@ -455,6 +472,87 @@ describe('canvas convergence (async bus)', () => {
})
})
+ // THE RE-CREATION GATE (port map §6.5). Our own remove enters `seen` only when its echo comes back,
+ // so a re-creation of the same id cast before that — a link deleted and redrawn, a node deleted and
+ // ⌘Z'd, inside one round trip — carries a `seen` below the remove, and every PEER drops it as a
+ // stale frame (rule 4) while we keep showing it. Held back until the echo lands, then cast, it
+ // carries the remove in its `seen` and is a re-creation everywhere.
+ describe('a re-creation waits for our own pending remove of the same id', () => {
+ const bridge = { id: 'bridge-n1-n2', source: 'n1', target: 'n2' }
+ const byId = (es: BridgeLink[]) => [...es].sort((x, y) => x.id.localeCompare(y.id))
+
+ it('a link deleted and redrawn before our echo returns reaches the peer', () => {
+ a.edit([node('n1', 0), node('n2', 0)])
+ a.editEdges({ bridges: [bridge] })
+ bus.settle()
+
+ bus.stall(a.id) // our socket is backed up: our acks are late
+ a.editEdges({ bridges: [] }) // A removes the link…
+ a.editEdges({ bridges: [bridge] }) // …and draws it again before the remove's echo is back
+ bus.unstall(a.id)
+ bus.settle()
+
+ expect(canon(a)).toEqual(canon(b))
+ expect(byId(a.persisted().bridges)).toEqual(byId(b.persisted().bridges))
+ expect(a.bridges).toEqual([bridge])
+ expect(b.bridges).toEqual([bridge]) // was: [] — B dropped the redraw as a stale frame
+ })
+
+ // The echo is rarely the only thing in flight: a teammate's op for ANOTHER node is ordered first
+ // and adopted while the redraw is still held. The adopt must not take the held link into the
+ // baseline, or the release finds nothing to cast.
+ it('…even when a teammate’s op is adopted while the redraw is held', () => {
+ a.edit([node('n1', 0), node('n2', 0), node('n3', 0)])
+ a.editEdges({ bridges: [bridge] })
+ bus.settle()
+
+ bus.stall(a.id)
+ b.edit(b.states.map((n) => (n.id === 'n3' ? node('n3', 30) : n))) // B's move is ordered first
+ a.editEdges({ bridges: [] })
+ a.editEdges({ bridges: [bridge] })
+ bus.unstall(a.id)
+ bus.settle()
+
+ expect(a.x('n3')).toBe(30)
+ expect(b.bridges).toEqual([bridge])
+ expect(a.persisted()).toEqual(b.persisted())
+ })
+
+ it('a node deleted and ⌘Z’d before our echo returns comes back on the peer too', () => {
+ a.edit([node('n1', 0), node('n2', 0)])
+ bus.settle()
+
+ bus.stall(a.id)
+ const before = a.states
+ a.edit(a.states.filter((n) => n.id !== 'n1')) // delete…
+ a.edit(before) // …undo
+ bus.unstall(a.id)
+ bus.settle()
+
+ expect(b.ids()).toEqual(['n1', 'n2']) // was: ['n2'] — B dropped the undo as a stale frame
+ expect(canon(a)).toEqual(canon(b))
+ })
+
+ it('the release casts the held re-creation once, and nothing after it', () => {
+ a.edit([node('n1', 0), node('n2', 0)])
+ a.editEdges({ bridges: [bridge] })
+ bus.settle()
+
+ bus.stall(a.id)
+ a.editEdges({ bridges: [] })
+ a.editEdges({ bridges: [bridge] })
+ const beforeRelease = bus.castCount
+ bus.unstall(a.id)
+ bus.settle()
+ expect(bus.castCount - beforeRelease).toBe(1) // the held redraw, cast on the echo
+
+ const settled = bus.castCount
+ a.edit([...a.states]) // an unrelated publish
+ bus.settle()
+ expect(bus.castCount - settled).toBe(0)
+ })
+ })
+
// A stale frame from a client whose socket was stalled must not resurrect the node either — this
// is the "disconnected peer erases / revives what everyone else settled" hazard, and it is the one
// a TIME-based delete-wins window would get wrong (the frame arrives long after any window).
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index fdfbeb09e..bd9e184ff 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -630,7 +630,13 @@ import {
publishableScene,
type CanvasPublisher
} from '@shared/canvas-publish'
-import { createCanvasOrder, createReconnectWatch, type CanvasOrder } from '@shared/canvas-order'
+import {
+ createCanvasOrder,
+ createReconnectWatch,
+ isRemoveOp,
+ mutationKey,
+ type CanvasOrder
+} from '@shared/canvas-order'
import {
applyEdgeMutationToScene,
createMutationGuard,
@@ -3716,6 +3722,12 @@ export function Canvas() {
// so the size guard below judges the EXACT payload that goes on the wire. `stamp` is pure:
// a refusal still records no pending entry.
const stamped = order.stamp(m)
+ // THE RE-CREATION GATE (canvas-order `hasPendingRemove`): our own remove of this key has not
+ // come back yet, so it is not in our `seen` — a re-creation cast now (a link redrawn, a node
+ // ⌘Z'd, within one round trip) would be dropped by every peer as a stale frame while we keep
+ // showing it. Held = owed: the publisher keeps it, and the receive handler's release casts it
+ // the moment the remove's echo lands. Never a remove itself (a remove is never stale).
+ if (!isRemoveOp(stamped) && order.hasPendingRemove(mutationKey(stamped))) return false
// The reflector REFUSES an oversized / malformed mutation at ingest, silently: no peer ever
// sees it and there is no negative ack. Ask the same predicate FIRST, so a refusal costs us
// neither a pending entry (which would deafen this node to its peers for the whole TTL — a
@@ -3807,9 +3819,27 @@ export function Canvas() {
// mount-time local one (the bug). Byte-identical on a local tab (`activeSession.api` IS
// `window.nodeTerminal`). Re-keyed on the api OBJECT below, in lockstep with the publisher, so a
// tab switch tears down + re-binds both together (and a local→local switch does neither).
+ //
+ // The re-creation gate's release: the send callback holds a re-creation of a key whose remove of
+ // ours is unacked, and nothing else would ever cast it — our own echo is an ack, it changes no
+ // React state, so the [nodes] publish effect does not run. So the echo that clears the gate
+ // publishes, AFTER this handler (a repaired remove is applied first), and only when something is
+ // actually owed (a bulk delete's acks must not each serialize the canvas).
+ const releaseHeld = (): void => {
+ const pub = publisherRef.current
+ if (!pub || !pub.hasOwed() || loadingRef.current) return
+ pub.publish(publishableLater(nodesRef.current))
+ }
return activeSession.api.canvas.onMutation((projectId, mutation) => {
hasPeersRef.current = true // proof of a peer, whatever the presence table says
- if (!orderRef.current?.accept(mutation)) return
+ const order = orderRef.current
+ if (!order) return
+ const key = mutationKey(mutation)
+ const held = order.hasPendingRemove(key)
+ const apply = order.accept(mutation)
+ const released = held && !order.hasPendingRemove(key)
+ if (released) queueMicrotask(releaseHeld)
+ if (!apply) return
// ---- edges (context links + "spawned by" ropes) ----
// They live outside React Flow's `nodes` array, in their own state, so they take their own
// apply path — but everything around it is the node path's contract, unchanged: the ordering
diff --git a/src/renderer/canvas/canvas-sync-ordering.source.test.ts b/src/renderer/canvas/canvas-sync-ordering.source.test.ts
new file mode 100644
index 000000000..52b83517c
--- /dev/null
+++ b/src/renderer/canvas/canvas-sync-ordering.source.test.ts
@@ -0,0 +1,52 @@
+// Source pins for the ordering hardening of canvas sync (Task 3). Canvas.tsx cannot be rendered in
+// the node test environment, so the load-bearing SHAPES are pinned here; the behaviour behind each is
+// tested where it lives (canvas-order.test.ts, canvas-publish.test.ts,
+// canvas-sync.convergence.test.ts, nodesEpoch.test.tsx).
+import { readFileSync } from 'node:fs'
+import { describe, it, expect } from 'vitest'
+const src = readFileSync(new URL('./Canvas.tsx', import.meta.url), 'utf8').replace(/\r\n/g, '\n')
+
+/** The publisher's send callback, from its creation to the cast. */
+function sendCallback(): string {
+ const start = src.indexOf('const pub = createCanvasPublisher(')
+ return src.slice(start, src.indexOf('activeSession.api.canvas.mutate(projectId, stamped)', start))
+}
+
+/** The peer-mutation receive handler. */
+function receiveHandler(): string {
+ const start = src.indexOf('return activeSession.api.canvas.onMutation((projectId, mutation) => {')
+ return src.slice(start, src.indexOf('}, [activeSession.api, setNodes, setLinkEdges', start))
+}
+
+describe('the re-creation gate (canvas-order hasPendingRemove)', () => {
+ // A re-creation cast before our own remove's echo carries a `seen` below that remove, and every
+ // peer drops it as a stale frame (rule 4) while we keep showing it.
+ it('the send callback holds a non-remove op whose key has our remove in flight', () => {
+ const body = sendCallback()
+ const gate = body.indexOf('if (!isRemoveOp(stamped) && order.hasPendingRemove(mutationKey(stamped))) return false')
+ expect(gate).toBeGreaterThan(-1)
+ // …before anything records or casts it: a held op must not leave a pending entry behind.
+ expect(gate).toBeLessThan(body.indexOf('order.onLocal(stamped)'))
+ })
+
+ // Held = owed, but nothing re-publishes on its own: our echo is an ack, it changes no React state,
+ // so the [nodes] publish effect never runs. The release has to publish.
+ it('our own remove coming back releases what the gate held', () => {
+ const body = receiveHandler()
+ const held = body.indexOf('const held = order.hasPendingRemove(key)')
+ const accept = body.indexOf('order.accept(mutation)')
+ expect(held).toBeGreaterThan(-1)
+ expect(held).toBeLessThan(accept) // asked BEFORE the ack draws the count down
+ expect(body).toMatch(/const released = held && !order\.hasPendingRemove\(key\)/)
+ expect(body).toMatch(/if \(released\) queueMicrotask\(releaseHeld\)/)
+ })
+
+ it('the release publishes only when something is owed, and never during a load', () => {
+ const start = src.indexOf('const releaseHeld = (): void => {')
+ expect(start).toBeGreaterThan(-1)
+ const body = src.slice(start, src.indexOf('\n }', start))
+ expect(body).toMatch(/!pub\.hasOwed\(\)/)
+ expect(body).toMatch(/loadingRef\.current/)
+ expect(body).toMatch(/pub\.publish\(publishableLater\(nodesRef\.current\)\)/)
+ })
+})
diff --git a/src/shared/canvas-order.test.ts b/src/shared/canvas-order.test.ts
index 466e97303..60575f669 100644
--- a/src/shared/canvas-order.test.ts
+++ b/src/shared/canvas-order.test.ts
@@ -6,6 +6,7 @@ import { describe, it, expect } from 'vitest'
import {
createCanvasOrder,
createReconnectWatch,
+ isRemoveOp,
mutationKey,
mutationNodeId,
PENDING_TTL_MS
@@ -288,6 +289,90 @@ describe('createCanvasOrder', () => {
})
})
+ // THE RE-CREATION GATE (port map §6.5). Rule 4 judges an upsert by what its sender had applied
+ // when it cast (`seen`). Our OWN remove is applied locally at once, but it enters `seen` only when
+ // its echo comes back — so a re-creation of the same id cast before that echo (a link deleted and
+ // redrawn, a node deleted and ⌘Z'd, inside one round trip) carries a `seen` below the remove and
+ // every peer drops it as a stale frame, while we keep showing it: a split. The caller holds such a
+ // re-creation back while our remove is unacked; this is the question it asks.
+ describe('a pending local remove (the re-creation gate)', () => {
+ it('reports a pending local remove until its echo returns', () => {
+ const o = createCanvasOrder('a')
+ const rm: CanvasMutation = { op: 'edge-remove', kind: 'bridge', id: 'b1', src: 'a' }
+ o.onLocal(rm)
+ expect(o.hasPendingRemove('e:b1')).toBe(true)
+ o.accept({ ...rm, seq: 7 })
+ expect(o.hasPendingRemove('e:b1')).toBe(false)
+ })
+
+ it('covers a node key the same way', () => {
+ const o = createCanvasOrder('me')
+ o.onLocal(o.stamp(rm('n1', 'me', 0)))
+ expect(o.hasPendingRemove('n:n1')).toBe(true)
+ expect(o.hasPendingRemove('e:n1')).toBe(false) // one key space, two prefixes
+ o.accept(rm('n1', 'me', 3))
+ expect(o.hasPendingRemove('n:n1')).toBe(false)
+ })
+
+ it('counts removes: one ack does not release a key with a second remove still in flight', () => {
+ const o = createCanvasOrder('me')
+ o.onLocal(rm('n1', 'me', 0))
+ o.onLocal(rm('n1', 'me', 0))
+ o.accept(rm('n1', 'me', 4))
+ expect(o.hasPendingRemove('n:n1')).toBe(true)
+ o.accept(rm('n1', 'me', 5))
+ expect(o.hasPendingRemove('n:n1')).toBe(false)
+ })
+
+ it('only a REMOVE counts: an unacked edit of the key is not a pending remove', () => {
+ const o = createCanvasOrder('me')
+ o.onLocal(up('n1', 1, 'me', 0))
+ expect(o.hasPendingRemove('n:n1')).toBe(false)
+ })
+
+ it('a peer’s remove of the same key is not our ack', () => {
+ const o = createCanvasOrder('me')
+ o.onLocal(rm('n1', 'me', 0))
+ o.accept(rm('n1', 'peer', 6))
+ expect(o.hasPendingRemove('n:n1')).toBe(true)
+ })
+
+ it('an ack for a remove we no longer track (after a reset) releases nothing twice', () => {
+ const o = createCanvasOrder('me')
+ o.onLocal(rm('n1', 'me', 0))
+ o.reset()
+ expect(o.hasPendingRemove('n:n1')).toBe(false) // a reconnect forgets the in-flight casts
+ o.accept(rm('n1', 'me', 2)) // the old echo straggling in
+ o.onLocal(rm('n1', 'me', 0))
+ expect(o.hasPendingRemove('n:n1')).toBe(true) // a later remove still counts from one
+ })
+
+ it('isRemoveOp is the order’s own remove predicate, nodes and edges alike', () => {
+ expect(isRemoveOp(rm('n1', 'me', 0))).toBe(true)
+ expect(isRemoveOp({ op: 'edge-remove', kind: 'rope', id: 'r1' })).toBe(true)
+ expect(isRemoveOp(up('n1', 0, 'me', 0))).toBe(false)
+ expect(
+ isRemoveOp({ op: 'edge-upsert', kind: 'bridge', edge: { id: 'b1', source: 'a', target: 'b' } })
+ ).toBe(false)
+ })
+
+ // WHY the gate lives in the caller: the whole failure is visible on a PEER'S order. Cast before
+ // our remove's echo, the re-creation is a stale frame there; cast after it, it is a re-creation.
+ it('a re-creation cast before our remove’s echo is stale on a peer; cast after it, it applies', () => {
+ const me = createCanvasOrder('me')
+ const peer = createCanvasOrder('peer')
+ me.accept(up('n2', 0, 'peer', 5)) // we have applied the order up to 5
+ const remove = me.stamp(rm('n1', 'me', 0))
+ me.onLocal(remove)
+ expect(peer.accept({ ...remove, seq: 6 })).toBe(true) // the peer tombstones n1 at 6
+ const early = me.stamp(up('n1', 1, 'me', 0)) // ⌘Z before our echo is back: seen 5
+ expect(peer.accept({ ...early, seq: 7 })).toBe(false) // dropped as a stale frame
+ me.accept({ ...remove, seq: 6 }) // our echo lands…
+ const late = me.stamp(up('n1', 1, 'me', 0)) // …so the re-creation now carries seen 6
+ expect(peer.accept({ ...late, seq: 8 })).toBe(true)
+ })
+ })
+
it('reset forgets the order and the pending edits (project switch / reconnect)', () => {
const o = createCanvasOrder('me')
o.onLocal(up('n1', 1, 'me', 0))
diff --git a/src/shared/canvas-order.ts b/src/shared/canvas-order.ts
index dd1ca01a0..51ac49d39 100644
--- a/src/shared/canvas-order.ts
+++ b/src/shared/canvas-order.ts
@@ -134,6 +134,10 @@ function isRemove(m: CanvasMutation): boolean {
return m.op === 'remove' || m.op === 'edge-remove'
}
+/** The order's own "drops its subject" predicate, for a caller that has to ask the same question —
+ * the re-creation gate (`hasPendingRemove`) holds back everything EXCEPT a remove. */
+export const isRemoveOp = isRemove
+
export interface CanvasOrder {
/**
* Stamp a mutation we are ABOUT to cast with our causal position (`seen`) — rule 4.
@@ -155,6 +159,23 @@ export interface CanvasOrder {
* or a peer's edit to a node whose newer local edit of ours is still in flight.
*/
accept(m: CanvasMutation): boolean
+ /**
+ * True while one of OUR remove-class casts (`remove` / `edge-remove`) for `key` (a `mutationKey`)
+ * is unacked — THE RE-CREATION GATE.
+ *
+ * Rule 4 judges an upsert by the `seen` its sender stamped, and our own remove enters `seen` only
+ * when its ECHO comes back. So a re-creation of that key cast before the echo — a link deleted and
+ * redrawn, a node deleted and ⌘Z'd, inside one round trip — carries a `seen` below the remove, and
+ * every peer drops it as a stale frame while this client keeps showing it: a split that the next
+ * whole-file save turns into last-writer-wins on disk. The caller therefore refuses to cast a
+ * non-remove op for a key this reports (the publisher keeps it owed) and casts it once the echo
+ * lands, when `stamp` puts the remove in its `seen`.
+ *
+ * Counted per key, not a flag: two removes in flight (delete, undo, delete) release on the second
+ * ack. Not TTL-bound, unlike rule 2's suppression: a late ack is exactly when the gate matters, and
+ * a lost one comes with a reconnect, whose `reset` clears it.
+ */
+ hasPendingRemove(key: string): boolean
/** Forget everything (project switch / disconnect). */
reset(): void
}
@@ -205,6 +226,9 @@ export function createCanvasOrder(
* is applied (which also clears the entry). Insertion-ordered, capped at REMOVED_MAX.
*/
const removed = new Map<string, number>()
+ /** The re-creation gate (`hasPendingRemove`): per key, how many of our remove-class casts are
+ * still unacked. Kept apart from `pending`, which counts EVERY cast of ours for the key. */
+ const pendingRemoves = new Map<string, number>()
/**
* The highest `seq` we have applied or deliberately dropped, across ALL nodes — our causal
* position in the total order, and what `stamp` puts on every mutation we cast. Global, not per
@@ -267,6 +291,7 @@ export function createCanvasOrder(
} else {
pending.set(id, { count: 1, since: now() })
}
+ if (isRemove(m)) pendingRemoves.set(id, (pendingRemoves.get(id) ?? 0) + 1)
// A fresh local edit IS an optimistic value on our canvas again, so rule 1 is sound for this
// node once more and an older echo of ours must not be replayed over it. (This one's own echo
// will be dropped as the ack it is; it carries what we already show.)
@@ -306,6 +331,14 @@ export function createCanvasOrder(
pending.delete(id)
superseded.delete(id) // every cast of ours is accounted for; the node is settled
}
+ // The re-creation gate: this remove is now in our `seen` (above), so a re-creation of the
+ // key stamped from here on carries it. Only an entry we are tracking is drawn down — an echo
+ // straggling in after a reset must not push a later remove's count below one.
+ const r = isRemove(m) ? pendingRemoves.get(id) : undefined
+ if (r !== undefined) {
+ if (r <= 1) pendingRemoves.delete(id)
+ else pendingRemoves.set(id, r - 1)
+ }
return repair
}
// A straggler: a mutation the total order has already superseded on this client (applied, or
@@ -328,10 +361,15 @@ export function createCanvasOrder(
return true
},
+ hasPendingRemove(key) {
+ return (pendingRemoves.get(key) ?? 0) > 0
+ },
+
reset() {
seen.clear()
pending.clear()
superseded.clear()
+ pendingRemoves.clear()
// The core may have restarted at seq 0 — a `removed` entry stamped with the OLD counter would
// then outrank every new mutation and blackhole that node, and a stale `lastSeq` would put a
// causal position on our casts that the new order has not reached.
diff --git a/src/shared/canvas-publish.test.ts b/src/shared/canvas-publish.test.ts
index 4e9aada5c..8591d142c 100644
--- a/src/shared/canvas-publish.test.ts
+++ b/src/shared/canvas-publish.test.ts
@@ -621,3 +621,131 @@ describe('an adopt does not swallow what is still owed', () => {
expect(snapshot).not.toHaveBeenCalled()
})
})
+
+// THE RE-CREATION GATE (canvas-order `hasPendingRemove`). The send callback refuses a re-creation of
+// an id whose REMOVE of ours is still unacked — cast now, it would carry a `seen` below that remove
+// and every peer would drop it as stale. A refusal keeps it owed, and it is cast when the remove's
+// echo lands. That refusal is TRANSIENT and usually lasts one round trip, which is exactly when a
+// teammate's op arrives and is adopted. An owed EDGE therefore has to survive an adopt just like an
+// owed node does — otherwise the adopt takes the redrawn link into the baseline, the release finds
+// nothing to cast, and the peer never gets it (their next save deletes it on disk).
+describe('a re-creation held by the send gate', () => {
+ const link = (id: string, source: string, target: string): BridgeLink => ({ id, source, target })
+ const scene = (nodes: CanvasNodeState[], bridges: BridgeLink[] = [], ropes: BridgeLink[] = []) => ({
+ nodes,
+ bridges,
+ ropes
+ })
+ const label = (m: CanvasMutation): string =>
+ `${m.op}:${mutationNodeId(m) ?? (m.op === 'edge-remove' ? m.id : m.op === 'edge-upsert' ? m.edge.id : '')}`
+ /** A send that refuses every non-remove op for the gated keys — what Canvas's gate does. */
+ function gated(keys: Set<string>) {
+ const sent: CanvasMutation[] = []
+ const send = (m: CanvasMutation): boolean => {
+ const key = mutationNodeId(m) !== null ? `n:${mutationNodeId(m)}` : `e:${m.op === 'edge-remove' ? m.id : m.op === 'edge-upsert' ? m.edge.id : ''}`
+ if (m.op !== 'remove' && m.op !== 'edge-remove' && keys.has(key)) return false
+ sent.push(m)
+ return true
+ }
+ return { sent, send }
+ }
+
+ it('a redrawn link is cast once the gate opens, even with an adopt in between', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')])) // all cast
+ p.publish(scene([node('n1'), node('n2')])) // the user deletes the link: edge-remove cast
+ gate.add('e:b1') // …its echo is not back yet
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')])) // …and redraws it: held
+ p.adopt(scene([node('n1'), node('n2', 5)], [link('b1', 'n1', 'n2')])) // a teammate moved n2
+ gate.clear() // our remove's echo lands
+ const cast = c.sent.length
+ p.publish(scene([node('n1'), node('n2', 5)], [link('b1', 'n1', 'n2')]))
+ expect(c.sent.slice(cast).map(label)).toEqual(['edge-upsert:b1'])
+ })
+
+ it('holds for a lazy adopt thunk too (what Canvas actually passes)', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ p.publish(() => scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.publish(() => scene([node('n1'), node('n2')]))
+ gate.add('e:b1')
+ p.publish(() => scene([node('n1'), node('n2')], [], [link('b1', 'n1', 'n2')])) // redrawn as a rope
+ p.adopt(() => scene([node('n1'), node('n2', 5)], [], [link('b1', 'n1', 'n2')]))
+ gate.clear()
+ const cast = c.sent.length
+ p.publish(() => scene([node('n1'), node('n2', 5)], [], [link('b1', 'n1', 'n2')]))
+ expect(c.sent.slice(cast).map(label)).toEqual(['edge-upsert:b1'])
+ })
+
+ it('casts it exactly once: a later publish casts nothing', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ const now = () => scene([node('n1'), node('n2', 5)], [link('b1', 'n1', 'n2')])
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.publish(scene([node('n1'), node('n2')]))
+ gate.add('e:b1')
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.adopt(now())
+ gate.clear()
+ p.publish(now())
+ const cast = c.sent.length
+ p.publish(now())
+ expect(c.sent.slice(cast)).toEqual([])
+ expect(c.sent.filter((m) => m.op === 'edge-upsert')).toHaveLength(2) // the draw and the redraw
+ })
+
+ // The Fix-round-2 rule, for an owed edge: what the adopted scene no longer holds is not ours to
+ // re-emit. The teammate deleted the redrawn link too; the adopt must not echo it back as an add.
+ it('a held link the adopted scene dropped is not re-emitted', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.publish(scene([node('n1'), node('n2')]))
+ gate.add('e:b1')
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.adopt(scene([node('n1'), node('n2')])) // a teammate's remove of b1 applied locally
+ gate.clear()
+ const cast = c.sent.length
+ p.publish(scene([node('n1'), node('n2')]))
+ expect(c.sent.slice(cast)).toEqual([])
+ expect(p.hasOwed()).toBe(false)
+ })
+
+ it('a node re-creation held by the gate survives an adopt (and its link waits for it)', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ p.publish(scene([node('n1'), node('t')], [link('b1', 'n1', 't')]))
+ p.publish(scene([node('t')])) // n1 deleted: edge-remove + remove cast
+ gate.add('n:n1') // ⌘Z before the remove's echo is back
+ p.publish(scene([node('n1'), node('t')], [link('b1', 'n1', 't')]))
+ p.adopt(scene([node('n1'), node('t', 5)], [link('b1', 'n1', 't')]))
+ gate.clear()
+ const cast = c.sent.length
+ p.publish(scene([node('n1'), node('t', 5)], [link('b1', 'n1', 't')]))
+ expect(c.sent.slice(cast).map(label)).toEqual(['upsert:n1', 'edge-upsert:b1'])
+ })
+
+ it('hasOwed says whether anything refused is still owed, node or edge', () => {
+ const gate = new Set<string>()
+ const c = gated(gate)
+ const p = createCanvasPublisher(c.send)
+ expect(p.hasOwed()).toBe(false)
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ p.publish(scene([node('n1'), node('n2')]))
+ gate.add('e:b1')
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ expect(p.hasOwed()).toBe(true)
+ gate.clear()
+ p.publish(scene([node('n1'), node('n2')], [link('b1', 'n1', 'n2')]))
+ expect(p.hasOwed()).toBe(false)
+ gate.add('n:n3')
+ p.publish(scene([node('n1'), node('n2'), node('n3')], [link('b1', 'n1', 'n2')]))
+ expect(p.hasOwed()).toBe(true)
+ })
+})
diff --git a/src/shared/canvas-publish.ts b/src/shared/canvas-publish.ts
index 7fce7833f..ff2b471f5 100644
--- a/src/shared/canvas-publish.ts
+++ b/src/shared/canvas-publish.ts
@@ -65,6 +65,10 @@ export interface CanvasPublisher {
* project) and is still owed — the node half of the refusal rebase. An `edge-upsert` naming one
* of them is held back (see `emit`), because the peer does not have that node. A copy. */
refusedNodeIds(): ReadonlySet<string>
+ /** Is anything REFUSED still owed — a node (`refusedNodeIds`) or an edge whose own cast was
+ * refused? A caller that just removed the reason for a refusal (the re-creation gate's remove was
+ * acked) re-publishes only when this says there is something to cast. */
+ hasOwed(): boolean
}
/**
@@ -151,6 +155,11 @@ export function createCanvasPublisher(
* emits and adopts: the peer still does not have the node, so an edge to it must keep waiting,
* and an adopt keeps both owed in the baseline (`adoptBaseline`). */
const owedNodes = new Set<string>()
+ /** Edge ids whose OWN cast was refused (not held for an owed endpoint — those ride `owedNodes`)
+ * and not yet made good. Kept across adopts for the same reason: the refusal is transient (the
+ * re-creation gate holds a redrawn link for one round trip — exactly when a teammate's op is
+ * adopted), and an adopt that took the link into the baseline would never cast it. */
+ const owedEdges = new Set<string>()
const resolve = (s: CanvasSnapshot): CanvasScene =>
asScene(typeof s === 'function' ? s() : s)
@@ -187,8 +196,11 @@ export function createCanvasPublisher(
* older version where it did. Dropping them outright instead would lose that older version, and a
* later local delete of the node would diff to nothing.
*
- * Only for what the ADOPTED scene still holds: an owed node present in it, and edges in it that
- * touch one. An owed node the scene no longer holds is not ours to re-emit — a project switch
+ * The same for an edge whose OWN cast was refused (`owedEdges` — the re-creation gate holds a
+ * redrawn link for one round trip, and a teammate's op adopted in that window must not swallow it).
+ *
+ * Only for what the ADOPTED scene still holds: an owed node present in it, edges in it that touch
+ * one, and owed edges present in it. An owed node the scene no longer holds is not ours to re-emit — a project switch
* adopts ANOTHER project's scene (one publisher serves every local project), where keeping the old
* entry would cast a `remove` of the previous project's node under the new project's id; and a
* peer's delete arrives here as an adopt, where it would echo their remove back. The same goes for
@@ -200,20 +212,21 @@ export function createCanvasPublisher(
* nothing owed an adopt is exactly what it was: a thunk stored unresolved.
*/
const adoptBaseline = (s: CanvasSnapshot): void => {
- if (!owedNodes.size) {
+ if (!owedNodes.size && !owedEdges.size) {
setBaseline(s)
return
}
const owed = new Set(owedNodes)
+ const owedE = new Set(owedEdges)
const prev = baseline()
const keepOwed = (next: CanvasScene): CanvasScene => {
const here = new Set(next.nodes.filter((n) => owed.has(n.id)).map((n) => n.id))
- if (!here.size) return next
const keys = new Set<string>()
for (const id of here) keys.add('n:' + id)
for (const e of [...next.bridges, ...next.ropes]) {
- if (here.has(e.source) || here.has(e.target)) keys.add('e:' + e.id)
+ if (here.has(e.source) || here.has(e.target) || owedE.has(e.id)) keys.add('e:' + e.id)
}
+ if (!keys.size) return next
return rebaseRefused(prev, next, keys)
}
if (typeof s === 'function') {
@@ -234,6 +247,7 @@ export function createCanvasPublisher(
const mutations = diffToMutations(prev, next)
const refused = new Set<string>()
const refusedNow = new Set<string>()
+ const refusedEdgesNow = new Set<string>()
for (const m of mutations) {
// An edge whose endpoint the peer does not have is HELD, not cast: the peer's link prune would
// drop it and cast an `edge-remove`, deleting the link on our canvas too. Held = refused, so
@@ -247,7 +261,16 @@ export function createCanvasPublisher(
const cast = send(opts.src ? { ...m, src: opts.src } : m) !== false
if (!cast) refused.add(mutationKey(m))
const nodeId = mutationNodeId(m)
- if (nodeId === null) continue
+ if (nodeId === null) {
+ const edgeId = m.op === 'edge-remove' ? m.id : m.op === 'edge-upsert' ? m.edge.id : null
+ if (edgeId === null) continue
+ if (cast) owedEdges.delete(edgeId)
+ else {
+ owedEdges.add(edgeId)
+ refusedEdgesNow.add(edgeId)
+ }
+ continue
+ }
if (cast) owedNodes.delete(nodeId)
else {
owedNodes.add(nodeId)
@@ -260,6 +283,11 @@ export function createCanvasPublisher(
const live = new Set(next.nodes.map((n) => n.id))
for (const id of owedNodes) if (!live.has(id) && !refusedNow.has(id)) owedNodes.delete(id)
}
+ // Same for an owed edge (the scene no longer draws it, and its remove was not the refusal).
+ if (owedEdges.size) {
+ const live = new Set([...next.bridges, ...next.ropes].map((e) => e.id))
+ for (const id of owedEdges) if (!live.has(id) && !refusedEdgesNow.has(id)) owedEdges.delete(id)
+ }
last = refused.size ? rebaseRefused(prev, next, refused) : next
lastLazy = null
}
@@ -321,6 +349,9 @@ export function createCanvasPublisher(
},
refusedNodeIds() {
return new Set(owedNodes)
+ },
+ hasOwed() {
+ return owedNodes.size > 0 || owedEdges.size > 0
}
}
}