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 reconnect keeps our causal position (reset no longer zeroes lastSeq)

review01dfced2eb09
diff --git a/docs/team-presence.md b/docs/team-presence.md
index ced4598e6..de08ff839 100644
--- a/docs/team-presence.md
+++ b/docs/team-presence.md
@@ -388,7 +388,11 @@ independently on each client from the already-broadcast `agent:status` stream; a
`order.reset()` is called whenever our presence clientId changes (i.e. on every (re)connect): if
the core restarted, its `seq` restarted at 0 while our `seen` map still held high values, and we
would silently drop every new mutation as a straggler. Correctness no longer depends on the
- ws-bridge's `location.reload()`.
+ ws-bridge's `location.reload()`. The reset keeps one thing: our own causal position (`lastSeq`,
+ what rule 4's `seen` stamps). A reconnect to the SAME core also resets, and a first cast stamped
+ `seen: 0` there — ⌘Z of a node deleted before the drop — was a stale frame to every peer holding
+ the tombstone. After a real restart the kept value is above every new `seq`, and the reflector
+ clamps it to `seq - 1`, so our casts read as "never stale": the pre-rule-4 verdict, not a split.
- **A cast the reflector would refuse is never made** — the publisher validates with the **same**
predicate the reflector's ingest uses (`isCanvasMutation`, moved to `src/shared/canvas-mutations.ts`
so both ends share one verdict) *before* recording a pending entry and *before* casting. A refusal
diff --git a/src/core/canvas-sync.convergence.test.ts b/src/core/canvas-sync.convergence.test.ts
index 82a18bac9..2b32be093 100644
--- a/src/core/canvas-sync.convergence.test.ts
+++ b/src/core/canvas-sync.convergence.test.ts
@@ -821,6 +821,26 @@ describe('canvas convergence (async bus)', () => {
expect(a.x('n1')).toBe(9)
})
+ // …and a reconnect to the SAME core must not throw away our causal position. The reset fires on
+ // any new clientId, and the core's `seq` carries on; a first cast stamped `seen: 0` — ⌘Z on a node
+ // deleted before the drop — is a stale frame to every peer holding the tombstone, and our own echo
+ // of it is no repair. A persistent split, until the next edit of that node.
+ it('a re-creation after a same-core reconnect reaches the peer (reset keeps our `seen`)', () => {
+ a.presence('cl-a')
+ a.edit([node('n1', 0), node('n2', 0)])
+ bus.settle()
+ const before = a.states
+ a.edit(a.states.filter((n) => n.id !== 'n1')) // A deletes n1…
+ bus.settle() // …and everyone has applied it
+
+ a.presence('cl-a2') // the socket dropped and came back: new clientId, same core, `seq` goes on
+ a.edit(before) // ⌘Z
+ bus.settle()
+
+ expect(b.ids()).toEqual(['n1', 'n2']) // was: ['n2'] — dropped as a stale frame
+ expect(canon(a)).toEqual(canon(b))
+ })
+
// A REFUSED CAST. The reflector drops a malformed / oversized mutation at ingest (silently — there
// is no negative ack). If the publisher has already advanced its baseline it never retries, and if
// the ordering state has already recorded a pending entry the node goes DEAF to its peers for the
diff --git a/src/core/canvas-sync.test.ts b/src/core/canvas-sync.test.ts
index 6ffbdcf60..7da931db1 100644
--- a/src/core/canvas-sync.test.ts
+++ b/src/core/canvas-sync.test.ts
@@ -12,6 +12,7 @@ import {
MUTATION_MAX_BYTES
} from './canvas-sync'
import { IPC } from '../shared/ipc'
+import { createCanvasOrder } from '../shared/canvas-order'
import type { CanvasMutation, CanvasNodeState } from '../shared/types'
const node = (id: string, x = 0): CanvasNodeState =>
@@ -122,6 +123,21 @@ describe('stampMutation', () => {
}
expect(stampMutation({ op: 'upsert', node: node('n1') }, 4).seen).toBeUndefined()
})
+
+ // canvas-order's reset() keeps a client's causal position (a same-core reconnect needs it). If the
+ // core REALLY restarted, that kept value is above every `seq` the new core hands out, and this
+ // clamp is what makes it harmless: the cast can only read as "never stale" on a peer — the
+ // pre-rule-4 verdict. A degrade, never a split.
+ it('a causal position kept across a real core restart is clamped into a verdict that applies', () => {
+ const me = createCanvasOrder('me')
+ me.accept({ op: 'upsert', node: node('n2'), src: 'x', seq: 40 }) // the old core had reached 40
+ me.reset() // …and restarted at 0
+ const peer = createCanvasOrder('peer')
+ expect(peer.accept({ op: 'remove', id: 'n1', src: 'x', seq: 3 })).toBe(true) // the new core's delete
+ const cast = stampMutation(me.stamp({ op: 'upsert', node: node('n1') }), 5)
+ expect(cast.seen).toBe(4)
+ expect(peer.accept(cast)).toBe(true)
+ })
})
describe('isCanvasMutation', () => {
diff --git a/src/shared/canvas-order.test.ts b/src/shared/canvas-order.test.ts
index 60575f669..fdc74d291 100644
--- a/src/shared/canvas-order.test.ts
+++ b/src/shared/canvas-order.test.ts
@@ -287,6 +287,21 @@ describe('createCanvasOrder', () => {
// outrank every new mutation and blackhole this node for the rest of the session.
expect(o.accept({ ...up('n1', 1, 'b', 1), seen: 0 })).toBe(true)
})
+
+ // …but NOT our causal position. A reset also fires on a reconnect to the SAME core (a new
+ // clientId, `seq` carrying on), and a `seen` of 0 on our first cast after it — say, ⌘Z on a node
+ // deleted before the drop — made every peer that still holds the tombstone drop the re-creation
+ // as a stale frame, while our echo is no repair: a persistent split.
+ it('reset keeps our causal position: a re-creation after a reconnect is applied by a peer', () => {
+ const me = createCanvasOrder('me')
+ const peer = createCanvasOrder('peer')
+ expect(peer.accept(rm('n1', 'x', 10))).toBe(true) // the peer tombstones n1 at 10
+ me.accept(rm('n1', 'x', 10)) // …and so did we
+ me.reset() // a new connection to the same core
+ const redo = me.stamp(up('n1', 1, 'me', 0))
+ expect(redo.seen).toBe(10) // was: 0
+ expect(peer.accept({ ...redo, seq: 11 })).toBe(true) // a re-creation, not a stale frame
+ })
})
// THE RE-CREATION GATE (port map §6.5). Rule 4 judges an upsert by what its sender had applied
diff --git a/src/shared/canvas-order.ts b/src/shared/canvas-order.ts
index 51ac49d39..81a017942 100644
--- a/src/shared/canvas-order.ts
+++ b/src/shared/canvas-order.ts
@@ -176,7 +176,8 @@ export interface CanvasOrder {
* a lost one comes with a reconnect, whose `reset` clears it.
*/
hasPendingRemove(key: string): boolean
- /** Forget everything (project switch / disconnect). */
+ /** Forget the per-connection order state on a genuine reconnect (see `createReconnectWatch`) —
+ * everything except our causal position (`lastSeq`, what `stamp` puts in `seen`). */
reset(): void
}
@@ -234,6 +235,7 @@ export function createCanvasOrder(
* position in the total order, and what `stamp` puts on every mutation we cast. Global, not per
* node: rule 4 compares a sender's knowledge of the ORDER against a remove's place in it, and
* "what did this client know when it cast" has nothing to do with which node it addressed.
+ * It survives `reset()` (see there): a same-core reconnect must not rewind it.
*/
let lastSeq = 0
@@ -371,10 +373,15 @@ export function createCanvasOrder(
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.
+ // then outrank every new mutation and blackhole that node.
removed.clear()
- lastSeq = 0
+ // `lastSeq` is deliberately KEPT. A reset also fires on a reconnect to the SAME core (a new
+ // clientId, `seq` carrying on), and zeroing it stamped our first cast after it `seen: 0` — a
+ // ⌘Z of a node deleted before the drop was then a stale frame to every peer holding the
+ // tombstone, and our own echo of it is no repair: a persistent split. If the core REALLY
+ // restarted, the kept value is above every `seq` it hands out and the reflector clamps it to
+ // `seq - 1` (canvas-sync `stampMutation`), so our casts read as "never stale" there — the
+ // pre-rule-4 verdict. A degrade, never a split.
}
}
}