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(core): a content write keeps this machine's exec values in the index cache

reviewccedca05a711
diff --git a/src/core/canvas-authority.test.ts b/src/core/canvas-authority.test.ts
index 3dd5fdc37..a1263550f 100644
--- a/src/core/canvas-authority.test.ts
+++ b/src/core/canvas-authority.test.ts
@@ -190,6 +190,28 @@ describe('canvas authority — governing', () => {
expect(byId(h.writes[0].content.nodes, 'b')?.position.x).toBe(10)
})
+ it('5c. rule 4 holds for an edge: a stale re-upsert cannot bring back a removed link', async () => {
+ const e1 = { id: 'e1', source: 'a', target: 'b' }
+ const h = harness({ disk: { P: content([node('a'), node('b')], { bridges: [e1] }) } })
+ h.authority.onReflected('P', { op: 'edge-remove', kind: 'bridge', id: 'e1', seq: 5 })
+ h.authority.onReflected('P', { op: 'edge-upsert', kind: 'bridge', edge: e1, seq: 6, seen: 3 })
+ await h.clock.advance(1000)
+ expect(h.writes).toHaveLength(1)
+ expect(h.writes[0].content.bridges).toEqual([])
+ expect(h.writes[0].content.ropes).toEqual([])
+ })
+
+ it('5d. rule 4 holds for a board column: a stale re-upsert cannot bring back a removed column', async () => {
+ const c1 = { id: 'c1', title: 'One', color: '#fff' }
+ const c2 = { id: 'c2', title: 'Two', color: '#fff' }
+ const h = harness({ disk: { P: content([node('a')], { kanban: { columns: [c1, c2], assignments: [] } }) } })
+ h.authority.onReflected('P', { op: 'kb-column-remove', id: 'c2', seq: 5 })
+ h.authority.onReflected('P', { op: 'kb-column', column: c2, seq: 6, seen: 3 })
+ await h.clock.advance(1000)
+ expect(h.writes).toHaveLength(1)
+ expect(h.writes[0].content.kanban?.columns.map((c) => c.id)).toEqual(['c1'])
+ })
+
it('5b. an op carrying any src tag is judged like every other op (none is taken for its own echo)', async () => {
const h = harness({ disk: { P: content([node('a')]) } })
h.authority.onReflected('P', { op: 'upsert', node: node('b'), src: 'canvas-authority', seq: 1 })
diff --git a/src/core/canvas-authority.ts b/src/core/canvas-authority.ts
index 3de8d8cff..6100809e3 100644
--- a/src/core/canvas-authority.ts
+++ b/src/core/canvas-authority.ts
@@ -30,6 +30,11 @@
//
// 4. A FAILED READ GOVERNS NOTHING. A shared project whose content cannot be read keeps `governs`
// true (the clients must keep publishing), drops its ops, and says so once.
+//
+// One accepted exception to "content comes only from ops": a node too large to travel as an op
+// (over the byte cap every cast is held to) can only ever arrive in a save, so a save may contribute
+// it. That node is outside the total order, and it has known limits, written beside the code that
+// adopts it (`overlayProject`).
import { applyCanvasOp, contentOf, diffContent, type CanvasContent } from '../shared/canvas-content'
import { isCanvasMutation } from '../shared/canvas-mutations'
@@ -177,6 +182,13 @@ export function createCanvasAuthority(deps: CanvasAuthorityDeps): CanvasAuthorit
let stopped = false
const isShared = (id: string): boolean => deps.sharedProjectIds().has(id)
+ // `governs` asks only "shared, and not stopped", on purpose: a shared project whose content cannot
+ // be read must still read as governed, so its clients keep publishing ops (rule 4). That includes a
+ // shared SSH project, which the authority never overlays (`overlayable`) and cannot read (the
+ // store refuses SSH content), so its eager adoption logs once that its content could not be read.
+ // Harmless: the Server Edition has no SSH-project manager, so it never opens an SSH project, and
+ // such an entry can only come from a hand-copied index. Its saves pass through un-overlaid, which
+ // is exactly how an ungoverned project is saved.
const governs = (id: string): boolean => !stopped && isShared(id)
function clearTimers(g: Governed): void {
@@ -328,9 +340,21 @@ export function createCanvasAuthority(deps: CanvasAuthorityDeps): CanvasAuthorit
function overlayProject(p: Project, g: Governed, mode: 'save' | 'load'): Project {
const theirs = Array.isArray(p.nodes) ? p.nodes.filter(isObject) : []
if (mode === 'save') {
- // The one node a save may contribute: one that can never travel as an op. It enters as a
- // synthetic upsert, so the reducer strips its exec fields (rule 1), an unchanged copy is a
- // no-op (two saves of one big sticky write once), and an outside edit re-applies it.
+ // The one node a save may contribute: one that can never travel as an op (the accepted
+ // exception in the header). It enters as a synthetic upsert, so the reducer strips its exec
+ // fields (rule 1), an unchanged copy is a no-op (two saves of one big sticky write once), and
+ // an outside edit re-applies it.
+ //
+ // Its limits, which follow from it having no place in the total order:
+ // (a) A STALE SAVE REVIVES A REMOVED BIG NODE. A client's save issued before that client
+ // applied a `remove` of the node still carries it, and the save is not ordered against
+ // the remove, so the node is adopted back and written. The window is one save debounce.
+ // (b) AN OUTSIDE EDIT OF A BIG NODE REACHES NO CLIENT, AND THE NEXT SAVE UNDOES IT. The
+ // published upsert is refused by the reflector (too large), so every client keeps its old
+ // copy, and the next save from any of them re-adopts that copy: a git-pulled change to a
+ // big node is reverted on disk.
+ // (c) Two clients holding different copies of one big node replace each other's on every
+ // save.
for (const n of theirs) if (tooLargeToSync(n)) applyTo(p.id, g, { op: 'upsert', node: n })
}
// This machine's exec fields live on the incoming copy (and in the index's localExec); carry
diff --git a/src/core/workspace-store.content-authority.test.ts b/src/core/workspace-store.content-authority.test.ts
index fd5c47f62..0956a0da1 100644
--- a/src/core/workspace-store.content-authority.test.ts
+++ b/src/core/workspace-store.content-authority.test.ts
@@ -442,6 +442,71 @@ describe('writeProjectContent — cwd-less canvas', () => {
})
})
+/**
+ * The content handed to `writeProjectContent` carries no exec fields (the authority strips them on
+ * the way in), but a cwd-less canvas's index `project` is where this machine's exec values live: for
+ * a pre-file canvas it is the ONLY place, and for a data-ref it is the cache a missing data file
+ * falls back to. The write keeps the cached values and never takes the incoming ones.
+ */
+describe('writeProjectContent — this machine’s exec values in the index cache', () => {
+ const PRE_FILE = 'pre-file'
+ const sshConn = { host: 'h', user: 'u', extraArgs: '-J jump', execTrusted: true } as NonNullable<CanvasNodeState['ssh']>
+ const withExec = (x = 0): CanvasNodeState => node('a', { shell: '/bin/zsh', ssh: sshConn, position: { x, y: 0 } })
+
+ /** A cwd-less canvas stored IN the index (no `dataFile`): the index is its only storage. */
+ async function seedPreFile(nodes: CanvasNodeState[]): Promise<WorkspaceStore> {
+ await fs.writeFile(path.join(userData, 'workspace.json'), JSON.stringify({
+ version: 3,
+ activeProjectId: PRE_FILE,
+ entries: [{ id: PRE_FILE, name: 'X', color: '#fff', viewport: { x: 0, y: 0, zoom: 1 }, project: project({ id: PRE_FILE, nodes }) }]
+ }))
+ const store = new WorkspaceStore()
+ await store.load()
+ return store
+ }
+
+ it('a pre-file canvas keeps its shell and ssh args across a write of stripped content', async () => {
+ const store = await seedPreFile([withExec(), node('b')])
+ const stripped = node('a', { ssh: { host: 'h', user: 'u' } as NonNullable<CanvasNodeState['ssh']>, position: { x: 5, y: 0 } })
+ expect(await store.writeProjectContent(PRE_FILE, content([stripped, node('b')]))).toBe(true)
+
+ const entry = (await readIndex()).entries[0]
+ expect(entry.dataFile).toBeUndefined()
+ const a = entry.project.nodes.find((n: CanvasNodeState) => n.id === 'a')
+ expect(a.position.x).toBe(5)
+ expect(a.shell).toBe('/bin/zsh')
+ expect(a.ssh.extraArgs).toBe('-J jump')
+ const loaded = (await new WorkspaceStore().load()).projects[0].nodes.find((n) => n.id === 'a')!
+ expect(loaded.position.x).toBe(5)
+ expect(loaded.shell).toBe('/bin/zsh')
+ expect(loaded.ssh?.extraArgs).toBe('-J jump')
+ })
+
+ it('never takes exec values from the incoming content into the index', async () => {
+ const store = await seedPreFile([node('a'), node('b')])
+ expect(await store.writeProjectContent(PRE_FILE, content([node('a', { shell: '/bin/evil' }), node('b')]))).toBe(true)
+ const a = (await readIndex()).entries[0].project.nodes.find((n: CanvasNodeState) => n.id === 'a')
+ expect(a.shell).toBeUndefined()
+ })
+
+ it('a data-ref keeps them in its cache too, so the fallback load (data file gone) still has them', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ id: INLINE_ID, nodes: [withExec(), node('b')] })]))
+ expect(await store.writeProjectContent(INLINE_ID, content([node('a', { position: { x: 5, y: 0 } }), node('b')]))).toBe(true)
+
+ const f = JSON.parse(await fs.readFile(dataFile(), 'utf-8'))
+ expect(f.nodes.find((n: CanvasNodeState) => n.id === 'a').shell).toBeUndefined() // the file stays exec-free
+ const entry = (await readIndex()).entries[0]
+ expect(entry.dataFile).toBe(true)
+ expect(entry.project.nodes.find((n: CanvasNodeState) => n.id === 'a').shell).toBe('/bin/zsh')
+
+ await fs.rm(dataFile())
+ const loaded = (await new WorkspaceStore().load()).projects[0].nodes.find((n) => n.id === 'a')!
+ expect(loaded.position.x).toBe(5)
+ expect(loaded.shell).toBe('/bin/zsh')
+ })
+})
+
describe('writeProjectContent — bookkeeping and refusals', () => {
it('calls onPersist once per landed write', async () => {
const store = new WorkspaceStore()
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 16281c3ba..2de252730 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -27,7 +27,7 @@ import {
} from '../shared/project-settings'
import { readProjectCapabilities, type ProjectCapability } from '../shared/project-capabilities'
import type { CapabilityAckMap } from './project-capability-consent'
-import { hoistLegacyNodeExec, type LocalNodeExecMap } from '../shared/node-exec'
+import { carryLocalNodeExec, hoistLegacyNodeExec, stripSharedNodeExec, type LocalNodeExecMap } from '../shared/node-exec'
import { collisionSeed, derivedProjectId, freshProjectId } from '../shared/project-id'
import {
pruneLayoutViewports,
@@ -1221,14 +1221,23 @@ export class WorkspaceStore {
/** The cwd-less leg: the data file under `writeDataFile`'s rules, then the index cache. A pre-file
* canvas (no `dataFile`, or an id we will not turn into a path) is stored IN the index, so for
- * it the index write is the write. */
+ * it the index write is the write.
+ *
+ * The index `project` also holds this machine's exec values (`shell`, `ssh.extraArgs`): for a
+ * pre-file canvas it is their ONLY store, and for a data-ref it is the cache a missing data file
+ * falls back to. The content carries none (the authority strips them), so each node keeps the
+ * cached node's values, and whatever the content carried is dropped first: the index is the
+ * trusted store, and nothing but this machine's own save may put an exec value there
+ * (@shared/node-exec). The data file is unaffected either way, since `projectToFile` strips. */
private async writeInlineContent(
e: IndexEntryV3,
cached: Project,
content: CanvasContent,
savedAt: string
): Promise<boolean> {
- const updated = withContent(cached, content)
+ const cachedById = new Map((Array.isArray(cached.nodes) ? cached.nodes : []).map((n) => [n.id, n]))
+ const nodes = stripSharedNodeExec(content.nodes).map((n) => carryLocalNodeExec(cachedById.get(n.id), n))
+ const updated = withContent(cached, { ...content, nodes })
const viaFile = e.dataFile === true && isInlineProjectFileId(e.id)
if (viaFile) {
const landed = await this.writeDataFile(e.id, projectToFile(updated, 0, savedAt))