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(canvas-authority): publish the outside-edit diff untrusted (N1)

reviewa78d2d8175a3
diff --git a/CLAUDE.md b/CLAUDE.md
index cc880efb0..a49e81c17 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -7369,7 +7369,10 @@ The invariants, each with its reason:
authority even on an owner's op: a save carries this machine's own values onto the overlaid nodes
(`carryLocalNodeExec`), and that carry is how an armed `--after` node — and server canvas
control's claim/clear of its launch (`savePatches` → `castAndSave`) — reaches the index's
- `localExec` on a governed project. **Server canvas control delivers only what landed**: `open` and
+ `localExec` on a governed project. For the same reason the authority's outside-edit diff is
+ published UNTRUSTED (`publishCanvasMutation(id, m, { trusted: false })`): vouched as a core write,
+ its launch-less upserts read as "cleared" on every owner tab, and a git pull cancelled queued
+ `--after` launches. **Server canvas control delivers only what landed**: `open` and
`run` launch nothing when their write-ahead `castAndSave` was refused (canvas control stopping),
and `refreshArmed` types a held command only when `savePatches` says both that the save landed and
that its claim applied to the fresh read (a teammate may have deleted, re-armed or claimed the node
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.md
index 5bb0cce37..6dc8da340 100644
--- a/docs/hosted-team-relay.md
+++ b/docs/hosted-team-relay.md
@@ -353,7 +353,9 @@ whole-workspace saves still write them.
authority applied, written to disk or not.
4. **Outside edits.** A `git pull` or hand edit of a governed `.nodeterm/project.json`, seen by the
server's file watcher, is adopted: the authority re-applies the ops it has not written yet on top,
- publishes the difference as `canvas:mut` ops, and broadcasts no `workspace:external-change`, so no
+ publishes the difference as `canvas:mut` ops (untrusted: its state holds no launch, so the ops
+ speak for none and every owner tab keeps an armed node's `pendingLaunch`; vouched, a pull that only
+ moved an `--after` node would cancel its launch), and broadcasts no `workspace:external-change`, so no
client gets the Reload / Keep mine bar (`src/server/workspace-external-watch.ts`). The edit's other
fields (name, colour, icon, layouts, the permission default, the capability flags, the board's
`github` and `pullLinks`) follow right after the ops, as the persisted project on
diff --git a/src/core/canvas-authority.ts b/src/core/canvas-authority.ts
index 89a65bed0..e54da5af2 100644
--- a/src/core/canvas-authority.ts
+++ b/src/core/canvas-authority.ts
@@ -20,8 +20,9 @@
// `--after` node's launch, and a delivery's clear of it, reach the index's `localExec`.
//
// 2. ITS OWN PUBLISHED OPS ARE ORDINARY OPS. The diff an outside edit publishes goes out bare (no
-// `src`, no `seen`), is stamped by the reflector and comes back through `onReflected` like any
-// client's cast. It is applied, and it changes nothing, because the content is already there.
+// `src`, no `seen`) and UNTRUSTED (the shell publishes it with `{ trusted: false }`: rule 1 means
+// it holds no launch, so it must not speak for one on an owner tab), is stamped by the reflector
+// and comes back through `onReflected` like any client's cast. It is applied, and it changes nothing, because the content is already there.
// Treating it as "our own echo" instead would diverge from the clients the moment an older op
// landed between the publish and the echo: the clients end on the published value (it has the
// higher seq), and an authority that dropped its echo would keep the older one.
diff --git a/src/core/canvas-sync.pending-launch.test.ts b/src/core/canvas-sync.pending-launch.test.ts
index bb40af179..fc3f172a5 100644
--- a/src/core/canvas-sync.pending-launch.test.ts
+++ b/src/core/canvas-sync.pending-launch.test.ts
@@ -3,6 +3,8 @@ import { testTmpDir } from './test-tmp'
import { initPlatform, resetPlatformForTests, type CorePlatform } from './platform'
import { initCanvasSync, publishCanvasMutation, setReflectedListener, stampMutation } from './canvas-sync'
import { IPC } from '../shared/ipc'
+import { createCanvasAuthority } from './canvas-authority'
+import { applyCanvasMutation } from '../shared/canvas-mutations'
import type { CanvasMutation, CanvasNodeState, PendingLaunch } from '../shared/types'
/**
@@ -147,3 +149,52 @@ describe('the reflected-op listener (the Server Edition canvas authority)', () =
.toEqual([launch, launch])
})
})
+
+describe('the canvas authority\'s outside-edit diff (N1)', () => {
+ // The authority's state carries no launch (rule 1), so every upsert of its diff lacks one. Vouched
+ // as the core's own write, an owner tab reads that absence as "the core cleared it" — on its live
+ // canvas AND in its stored copy — and its next save writes the loss into `localExec`: a git pull
+ // that merely moved an `--after` node cancelled its queued launch. So the authority's publish is
+ // UNTRUSTED (server/index.ts passes `{ trusted: false }`, pinned in hosted-boot.test.ts), and
+ // every owner carries its own launch across.
+ it('an outside edit that moves an armed node reaches owners unvouched, so each keeps its launch', async () => {
+ const authority = createCanvasAuthority({
+ sharedProjectIds: () => new Set(['p1']),
+ readContent: async () => ({ nodes: [node('n1')], bridges: [], ropes: [] }),
+ writeContent: async () => true,
+ publish: (id, m) => {
+ publishCanvasMutation(id, m, { trusted: false })
+ },
+ setTimer: () => null,
+ clearTimer: () => {},
+ log: () => {}
+ })
+ setReflectedListener((id, m) => authority.onReflected(id, m))
+ authority.sharedChanged()
+ await authority.flushAll()
+ await authority.adoptOutsideEdit({
+ id: 'p1',
+ name: 'p',
+ color: '#fff',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: [node('n1', { position: { x: 500, y: 0 } })]
+ })
+ const up = to(1)
+ expect(up.op).toBe('upsert')
+ expect(up.origin).toBeUndefined()
+ // An owner's stored copy of the armed node (the live canvas's `applyMutationToFlow` reads the
+ // same `origin`): the move lands, the launch stays.
+ const [kept] = applyCanvasMutation([node('n1', { pendingLaunch: launch })], up)
+ expect(kept.position.x).toBe(500)
+ expect(kept.pendingLaunch).toEqual(launch)
+ await authority.stop()
+ })
+
+ it('an untrusted core write strips a launch for everybody, and never vouches', () => {
+ publishCanvasMutation('p1', { op: 'upsert', node: node('n1', { pendingLaunch: launch }) }, { trusted: false })
+ for (const id of [1, 2, 9]) {
+ expect(to(id).origin).toBeUndefined()
+ expect(to(id).node.pendingLaunch).toBeUndefined()
+ }
+ })
+})
diff --git a/src/core/canvas-sync.ts b/src/core/canvas-sync.ts
index b6ca07668..288fa0a2b 100644
--- a/src/core/canvas-sync.ts
+++ b/src/core/canvas-sync.ts
@@ -182,17 +182,28 @@ function tellListener(projectId: string, stamped: CanvasMutation): void {
* control request). It takes the same validation, execution-field sanitization and total-order
* stamp as a browser cast, then fans out to every connected canvas. Disk persistence remains the
* caller's responsibility; this function is only the live convergence leg.
+ *
+ * `trusted` (default true) = the op speaks for the held launch (`pendingLaunch`): owner clients get
+ * it vouched (`origin: 'core'`), so its launch, or its ABSENCE, is authoritative there. Server canvas
+ * control needs exactly that (a headless delivery CLEARS the launch on every owner tab). The canvas
+ * authority passes `false`: its state holds no launch at all (core/canvas-authority.ts rule 1), so a
+ * vouched copy of its outside-edit diff would clear every armed node it touched on every owner tab.
+ * Untrusted, it carries no launch to anybody and each owner keeps its own, exactly as for a peer's
+ * cast.
*/
-export function publishCanvasMutation(projectId: string, mutation: CanvasMutation): boolean {
+export function publishCanvasMutation(
+ projectId: string,
+ mutation: CanvasMutation,
+ opts: { trusted?: boolean } = {}
+): boolean {
if (!isRefId(projectId) || !isCanvasMutation(mutation)) return false
- const clean = sanitizeCanvasMutation(mutation, true)
+ const trusted = opts.trusted !== false
+ const clean = sanitizeCanvasMutation(mutation, trusted)
if (!clean) return false
const p = platform()
- // The core's own write: its held launch is authoritative for owner clients (a headless delivery
- // CLEARS it there — without that a browser would keep and re-save the stale launch).
const stamped = stampMutation(clean, ++seq)
tellListener(projectId, stamped)
- fanOutMutation(p, projectId, stamped, true)
+ fanOutMutation(p, projectId, stamped, trusted)
return true
}
diff --git a/src/server/hosted-boot.test.ts b/src/server/hosted-boot.test.ts
index 81f9c33e9..ebef53d28 100644
--- a/src/server/hosted-boot.test.ts
+++ b/src/server/hosted-boot.test.ts
@@ -90,6 +90,10 @@ describe('hosted team boot wiring (source)', () => {
expect(block).toContain('sharedProjectIds: () => hosted.sharedProjectIds()')
expect(block).toContain('workspaceStore.setContentAuthority(')
expect(block).toContain('setReflectedListener(')
+ // Its outside-edit diff is published UNTRUSTED (N1): vouched as a core write, an owner tab would
+ // read each upsert's missing launch as "cleared" and cancel every queued `--after` it touched.
+ expect(block).toMatch(/publish: \(id, m\) => \{\s*publishCanvasMutation\(id, m, \{ trusted: false \}\)/)
+ expect(src.match(/trusted: false/g)?.length).toBe(1)
// Every shared project is adopted ONCE at boot, after the index load and after hosting started
// (start() loads the team file), before any outside edit could be adopted lazily: a lazy adoption
// after a git pull reads the pulled file as its baseline and publishes no diff at all.
diff --git a/src/server/index.ts b/src/server/index.ts
index 1bcb8cd95..aea9272c3 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -960,8 +960,11 @@ export async function startServer(
sharedProjectIds: () => hosted.sharedProjectIds(),
readContent: (id) => workspaceStore.readProjectContent(id),
writeContent: (id, content) => workspaceStore.writeProjectContent(id, content),
+ // UNTRUSTED: the authority's state holds no launch, so its outside-edit diff must not speak
+ // for one. Vouched, an owner tab would read each upsert's missing `pendingLaunch` as "the core
+ // cleared it" and a git pull would cancel every queued `--after` it touched.
publish: (id, m) => {
- publishCanvasMutation(id, m)
+ publishCanvasMutation(id, m, { trusted: false })
},
log: (message) => console.warn(`[canvas-authority] ${message}`)
})