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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(canvas-order): release a remove the capped cast record forgets; a forgotten echo matches nothing
reviewf57a832b29ef
diff --git a/docs/team-presence.md b/docs/team-presence.mdindex 331e668c0..9a1c791a0 100644--- a/docs/team-presence.md+++ b/docs/team-presence.md@@ -890,7 +890,11 @@ remove of ours in flight (counted per key, not TTL-bound — a late ack is exactLOST ack does not come with a reconnect: the ui sink drops a single message and keeps the connection(`SINK_FAILURE_LIMIT`). Our echoes come back in the order we cast (FIFO), so the echo of a LATER castof ours proves an earlier one was lost, and releases that remove's gate too; Canvas compares the total-count of pending removes across each `accept` to see it. A reset still clears everything. The+count of pending removes across each `accept` to see it. A reset still clears everything. The record+of our casts that FIFO match reads is capped (`LOCAL_CASTS_MAX`, 4096), which only matters while the+core answers nothing at all: a remove the cap forgets is released as it goes (the pre-gate degrade,+not a gate stuck until a reset), and a forgotten cast's own late echo releases nothing (matched to a+later cast of its key, it would release removes whose echoes are still in flight). Therefusal keeps the op owed in thepublisher, and an adopt in that window keeps an owed EDGE owed as well as an owed node (a teammate'sop is usually what arrives during that round trip). Nothing else would ever cast it — our echo is andiff --git a/src/shared/canvas-order.test.ts b/src/shared/canvas-order.test.tsindex 321f17308..7aa243fa9 100644--- a/src/shared/canvas-order.test.ts+++ b/src/shared/canvas-order.test.ts@@ -7,6 +7,7 @@ import {createCanvasOrder,createReconnectWatch,isRemoveOp,+ LOCAL_CASTS_MAX,mutationKey,mutationNodeId,PENDING_TTL_MS,@@ -610,6 +611,48 @@ describe('createCanvasOrder', () => {expect(o.hasPendingRemove('n:n1')).toBe(false)})+ // The FIFO record is capped (LOCAL_CASTS_MAX), and it only fills while the core answers nothing+ // at all — a long drag against a dead connection that never resets. Two things must hold when+ // it overflows: a remove shifted out is released THEN (it can never be matched again, so kept it+ // would stick until a reset), and an echo whose own record is gone matches nothing (matched to a+ // LATER cast of the same key, it released every remove cast in between, echoes still in flight).+ it('a remove shifted out of the record is released then, and its late echo is not a later remove’s', () => {+ const o = createCanvasOrder('me')+ o.onLocal(rm('n1', 'me', 0)) // its echo is on its way…+ for (let i = 0; i < LOCAL_CASTS_MAX; i++) o.onLocal(up('d', i, 'me', 0)) // …behind a long, unanswered drag+ expect(o.hasPendingRemove('n:n1')).toBe(false) // the pre-gate degrade, not a gate stuck for the session+ expect(o.pendingRemoveCount()).toBe(0)+ o.onLocal(rm('n1', 'me', 0)) // deleted again+ o.accept(rm('n1', 'me', 5)) // the FIRST remove's echo, at last+ expect(o.hasPendingRemove('n:n1')).toBe(true) // …is not the second one's ack+ o.accept(rm('n1', 'me', 6)) // the second one's own echo+ expect(o.hasPendingRemove('n:n1')).toBe(false)+ })++ it('an echo whose record was shifted out releases nothing — not a later remove still in flight', () => {+ const o = createCanvasOrder('me')+ o.onLocal(up('d', 0, 'me', 0)) // the oldest cast: its record is about to be forgotten+ for (let i = 1; i < LOCAL_CASTS_MAX; i++) o.onLocal(up('f', i, 'me', 0))+ o.onLocal(rm('n1', 'me', 0)) // the record is full: `d`'s entry is shifted out+ o.onLocal(up('d', 1, 'me', 0)) // a LATER cast of the same key+ o.accept(up('d', 0, 'me', 7)) // the echo of the forgotten one+ expect(o.hasPendingRemove('n:n1')).toBe(true) // n1's echo has not come back+ expect(o.pendingRemoveCount()).toBe(1)+ o.accept(rm('n1', 'me', 8)) // its own echo (the ones before it proven lost, FIFO)+ expect(o.hasPendingRemove('n:n1')).toBe(false)+ })++ it('an echo matched to a recorded cast proves every forgotten echo lost, so none is waited for', () => {+ const o = createCanvasOrder('me')+ o.onLocal(up('d', 0, 'me', 0)) // the oldest cast — and its echo is lost+ for (let i = 1; i < LOCAL_CASTS_MAX; i++) o.onLocal(up('f', i, 'me', 0))+ o.onLocal(up('x', 0, 'me', 0)) // `d`'s record is shifted out+ o.onLocal(rm('d', 'me', 0))+ o.accept(up('x', 0, 'me', 9)) // a recorded cast's echo: everything older is proven lost (FIFO)+ o.accept(rm('d', 'me', 10)) // so this is the remove's own echo, not the forgotten frame's+ expect(o.hasPendingRemove('n:d')).toBe(false)+ })+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)diff --git a/src/shared/canvas-order.ts b/src/shared/canvas-order.tsindex 40dc766b1..00c8faea2 100644--- a/src/shared/canvas-order.ts+++ b/src/shared/canvas-order.ts@@ -102,8 +102,13 @@ export const REMOVED_MAX = 512/*** How many of our own casts the FIFO record behind the re-creation gate's release keeps (see* `hasPendingRemove`). A cast leaves it the moment its echo, or a later one, comes back, so it only- * grows while the core answers nothing at all — and then the oldest entry is simply forgotten (the- * behaviour before the release existed: that remove's gate waits for its own echo or a reset).+ * grows while the core answers nothing at all — and then the oldest entry is forgotten. A forgotten+ * REMOVE is released as it is forgotten: nothing can match it any more, so kept it would hold its+ * key's gate until a reset. That opens the gate before the echo, which is the degrade the gate+ * exists to prevent (a re-creation cast now can be a stale frame to a peer) — bounded to a client+ * that already has 4096 casts unanswered. A forgotten cast's own echo, when it does come back,+ * releases nothing: matched to a LATER cast of the same key, it would release every remove cast+ * between the two while their echoes are still in flight.*/export const LOCAL_CASTS_MAX = 4096@@ -227,7 +232,8 @@ export interface CanvasOrder {* LOST ack does not come with a reconnect: one `canvas:mut` can be dropped on its way back while* the connection stays up (the ui sink's per-message failure limit). So a remove is also released* when the echo of a LATER cast of ours arrives: echoes come back in the order we cast them (FIFO),- * so that echo proves the earlier one is not coming. A reset still clears everything.+ * so that echo proves the earlier one is not coming. A reset still clears everything. (The record+ * of our casts is capped — LOCAL_CASTS_MAX says what happens to a remove it forgets.)** The PROJECT rides in the key: callers build it with `mutationKey(m, projectId)` — the same* function `onLocal` keys with — so the gate is project-scoped wherever a key is. (Only a@@ -297,6 +303,14 @@ export function createCanvasOrder(/** Our own casts in the order we made them, `{ key, remove }` — what an echo is matched against* (FIFO) to prove an earlier echo was lost. Capped at LOCAL_CASTS_MAX. */const casts: Array<{ key: string; remove: boolean }> = []+ /**+ * Per key, how many of our casts were shifted out of `casts` (the cap) with their echo still to+ * come. A key's forgotten casts are OLDER than any it still has recorded, and echoes return in cast+ * order, so the next echoes of that key are theirs: each is consumed here and matches nothing.+ * Cleared whenever an echo matches a RECORDED cast — every forgotten cast is older than that one,+ * so (FIFO) no echo of theirs can still arrive — and by `reset`.+ */+ const forgotten = new Map<string, number>()/** Set by `reset`: the next stamped `seq` says whether the core restarted (see `accept`). */let rebase = false/**@@ -377,7 +391,13 @@ export function createCanvasOrder(pendingRemoveTotal++}casts.push({ key: id, remove })- if (casts.length > LOCAL_CASTS_MAX) casts.shift()+ if (casts.length > LOCAL_CASTS_MAX) {+ // Forget the oldest (see LOCAL_CASTS_MAX): a remove is released now, since nothing can match+ // it later, and its echo is marked as one that must match nothing.+ const old = casts.shift() as { key: string; remove: boolean }+ if (old.remove) releaseRemove(old.key)+ forgotten.set(old.key, (forgotten.get(old.key) ?? 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.)@@ -432,10 +452,18 @@ export function createCanvasOrder(// this echo is the one for the FIRST cast of this key still recorded, and every cast recorded// before it has had its echo lost (FIFO) — a remove among them will never be acked, so its// gate is released too. An echo we have no record of (straggling in after a reset) releases- // nothing, so it cannot push a later remove's count below one.- const at = casts.findIndex((c) => c.key === id)- if (at !== -1) {- for (const c of casts.splice(0, at + 1)) if (c.remove) releaseRemove(c.key)+ // nothing, so it cannot push a later remove's count below one; nor does the echo of a cast+ // the capped record forgot (`forgotten`), which would otherwise match a LATER cast of its key.+ const lost = forgotten.get(id)+ if (lost) {+ if (lost <= 1) forgotten.delete(id)+ else forgotten.set(id, lost - 1)+ } else {+ const at = casts.findIndex((c) => c.key === id)+ if (at !== -1) {+ for (const c of casts.splice(0, at + 1)) if (c.remove) releaseRemove(c.key)+ forgotten.clear()+ }}// …with one exception: the echo of our LAST order op for a board list (every cast of ours// for that list now acked) is APPLIED. An order op lists only the ids its sender knew, so@@ -481,6 +509,7 @@ export function createCanvasOrder(pendingRemoves.clear()pendingRemoveTotal = 0casts.length = 0+ forgotten.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.removed.clear()