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(workspace): a corrupt node position must not poison the committed viewport

Claude Coded96572e6e5cd
diff --git a/src/core/workspace-files.test.ts b/src/core/workspace-files.test.ts
index 2c1f6d1fb..1fa5eaec5 100644
--- a/src/core/workspace-files.test.ts
+++ b/src/core/workspace-files.test.ts
@@ -134,8 +134,8 @@ describe('splitWorkspace', () => {
expect(index.entries[1].id).not.toBe('dup')
expect(files.size).toBe(2)
// Neither file names a project any more — only the folder they sit in does.
- expect(files.get('/a/foo')!.id).not.toBe('dup')
- expect(files.get('/a/bar')!.id).not.toBe(index.entries[1].id)
+ expect(files.get('/a/foo')!.id).toBe(legacyFileId('first'))
+ expect(files.get('/a/bar')!.id).toBe(legacyFileId('second'))
expect(files.get('/a/foo')!.nodes.map((n) => n.id)).toEqual(['term-a'])
expect(files.get('/a/bar')!.nodes.map((n) => n.id)).toEqual(['term-b'])
})
@@ -437,4 +437,40 @@ describe('framingViewport', () => {
expect(framingViewport([node({ parentId: 'group-1', position: { x: 10, y: 10 } })]))
.toEqual({ x: 0, y: 0, zoom: 1 })
})
+
+ // This value is written into the user's COMMITTED file on every save. The field it replaced was
+ // the user's own viewport and could not be poisoned by node data; this one can, so a corrupt or
+ // hand-edited position must not reach the file as NaN — which JSON.stringify writes as `null`,
+ // and which a reader then hands to React Flow as a camera.
+ it('a corrupt node position never reaches the file (no NaN, and the good axis still counts)', () => {
+ const corrupt = [
+ { ...node({ id: 'bad' }), position: { x: null, y: 75 } as unknown as { x: number; y: number } },
+ node({ id: 'ok', position: { x: 300, y: 900 } })
+ ]
+ expect(framingViewport(corrupt)).toEqual({ x: 80 - 300, y: 80 - 75, zoom: 1 })
+ // …and what actually lands on disk: JSON.stringify turns NaN into `null`, so the round trip
+ // is the assertion that matters (the node's own corrupt position is preserved verbatim —
+ // this file is not the place to silently rewrite the user's data).
+ const written = JSON.parse(
+ serializeProjectFile(projectToFile(project({ nodes: corrupt }), 1, 'now'))
+ ) as ProjectFileV1
+ expect(Number.isFinite(written.viewport!.x)).toBe(true)
+ expect(Number.isFinite(written.viewport!.y)).toBe(true)
+ expect(written.viewport).toEqual({ x: -220, y: 5, zoom: 1 })
+ })
+
+ it('every position corrupt (or missing) falls back to the origin, never NaN', () => {
+ const junk = [
+ { ...node({ id: 'a' }), position: undefined as unknown as { x: number; y: number } },
+ { ...node({ id: 'b' }), position: { x: 'left', y: NaN } as unknown as { x: number; y: number } }
+ ]
+ expect(framingViewport(junk)).toEqual({ x: 0, y: 0, zoom: 1 })
+ })
+
+ // It runs inside projectToFile → splitWorkspace → save(), so a throw here fails the WHOLE save,
+ // not one node. `Math.min(...nodes.map(…))` overflowed the call stack around 200k arguments.
+ it('a canvas far past the argument limit still frames (a loop, not a spread)', () => {
+ const many = Array.from({ length: 250_000 }, (_, i) => node({ id: `t${i}`, position: { x: i + 5, y: i + 9 } }))
+ expect(framingViewport(many)).toEqual({ x: 75, y: 71, zoom: 1 })
+ })
})
diff --git a/src/core/workspace-files.ts b/src/core/workspace-files.ts
index 0eb2d0039..a91dd444c 100644
--- a/src/core/workspace-files.ts
+++ b/src/core/workspace-files.ts
@@ -254,13 +254,29 @@ export function fileToProject(
* Without it a shared canvas whose nodes live at x=4000 opened onto empty space — the file no
* longer carries the author's camera, so the default {0,0,1} would have been a blank screen and
* "commit it to share the canvas" would have been a lie.
+ *
+ * A plain loop with a finiteness guard per AXIS, not `Math.min(...map())`, because this now runs
+ * inside `projectToFile` — i.e. inside every `save()` — where the old shape had two ways to ruin
+ * one: a hand-edited/corrupt `position` made `Math.min` return NaN, which `JSON.stringify` writes
+ * into the COMMITTED file as `"x": null` (a field that used to be the user's own viewport and
+ * could not be poisoned by node data), and spreading a large enough node array blows the call
+ * stack, which would now fail the whole save rather than one node.
*/
export function framingViewport(nodes: CanvasNodeState[]): Viewport {
- const rooted = nodes.filter((n) => !n.parentId)
- if (!rooted.length) return { x: 0, y: 0, zoom: 1 }
- const minX = Math.min(...rooted.map((n) => n.position.x))
- const minY = Math.min(...rooted.map((n) => n.position.y))
- return { x: 80 - minX, y: 80 - minY, zoom: 1 }
+ let minX = Infinity
+ let minY = Infinity
+ for (const n of nodes) {
+ // A child's position is relative to its frame, so it cannot anchor the camera.
+ if (n.parentId) continue
+ const pos = n.position as { x?: unknown; y?: unknown } | undefined
+ if (typeof pos?.x === 'number' && Number.isFinite(pos.x) && pos.x < minX) minX = pos.x
+ if (typeof pos?.y === 'number' && Number.isFinite(pos.y) && pos.y < minY) minY = pos.y
+ }
+ return {
+ x: Number.isFinite(minX) ? 80 - minX : 0,
+ y: Number.isFinite(minY) ? 80 - minY : 0,
+ zoom: 1
+ }
}
/**
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index 76ddb775c..5a054f92d 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -360,6 +360,46 @@ describe('unavailable projects never overwrite real data on save', () => {
const after = JSON.parse(await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8'))
expect(after.entries[0].cache).toEqual(cache) // good offline cache survived verbatim
})
+
+ // The rest of the entry's MACHINE-LOCAL half has the same problem as the cache: a placeholder
+ // carries none of it (its viewport is the {0,0,1} of an empty stand-in canvas, its nodes are
+ // gone), so an index rewrite while a folder is briefly unmounted would forget where the user was
+ // looking, which account this project runs on, and their own custom shell — none of which is
+ // recoverable from the project file, because none of it is IN the project file any more.
+ it('save() of an unavailable ref keeps the entry\'s camera, account and exec values', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({
+ cwd: projRoot,
+ viewport: { x: -640, y: 55, zoom: 1.75 },
+ defaultAccountId: 'acct-7',
+ nodes: [{
+ id: 'term-1', kind: 'terminal', position: { x: 0, y: 0 }, size: { width: 1, height: 1 },
+ title: 't', color: '#fff', group: null, shell: '/bin/zsh'
+ }]
+ })]))
+ const readEntry = async () =>
+ JSON.parse(await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8')).entries[0]
+ expect(await readEntry()).toMatchObject({
+ viewport: { x: -640, y: 55, zoom: 1.75 },
+ defaultAccountId: 'acct-7',
+ localExec: { 'term-1': { shell: '/bin/zsh' } }
+ })
+
+ // The folder goes away: the next load hands the renderer a grey placeholder, and the renderer
+ // hands it straight back to save().
+ await fs.rm(projRoot, { recursive: true, force: true })
+ const store2 = new WorkspaceStore()
+ const loaded = await store2.load()
+ expect(loaded.projects[0].unavailable).toBe(true)
+ expect(loaded.projects[0].viewport).toEqual({ x: 0, y: 0, zoom: 1 }) // the placeholder's
+ await store2.save(loaded)
+
+ expect(await readEntry()).toMatchObject({
+ viewport: { x: -640, y: 55, zoom: 1.75 },
+ defaultAccountId: 'acct-7',
+ localExec: { 'term-1': { shell: '/bin/zsh' } }
+ })
+ })
})
describe('probeFolder', () => {
@@ -1299,9 +1339,10 @@ describe('a git-shared project.json must not give two folders one project id', (
expect(fileA.id).toBe(fileB.id)
})
- // The repair writes each project file first and the index last, so a crash (or a kill, or a power
- // loss) between them leaves a re-keyed file under an un-re-keyed index. The deterministic
- // derivation is what makes that survivable: the next load derives the SAME id again.
+ // The repair is one index write now, so there is no half-applied state to crash into — but the
+ // property that made the old two-write repair survivable still has to hold, because a lost index
+ // write leaves the SAME collision on disk: the derivation is deterministic, so the next load
+ // derives the same ids again rather than inventing new ones.
it('survives a crash mid-repair (file re-keyed, index write lost) and converges on the same ids', async () => {
await seedCollision()
const indexBefore = await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8')