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): cross-project "go to node" no longer lands on the canvas origin
enes7e0cede10275
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsxindex d695fc230..e17d6f704 100644--- a/src/renderer/canvas/Canvas.tsx+++ b/src/renderer/canvas/Canvas.tsx@@ -103,6 +103,13 @@ import { PresenceLayer } from '../components/PresenceLayer'import { Facepile } from '../components/Facepile'import { PresenceNamePrompt } from '../components/PresenceNamePrompt'import { nodeTravel, projectTravel } from '../lib/presenceTravel'+import {+ FIT_NODE_OPTIONS,+ isMeasured,+ nodeFitRect,+ viewportForRect,+ type FocusableNode+} from '../lib/nodeFocus'import { RemoteAccessDialog } from '../components/RemoteAccessDialog'import { SshProjectDialog } from '../components/SshProjectDialog'import { transport } from '../terminal/local-transport'@@ -690,6 +697,7 @@ export function Canvas() {screenToFlowPosition,setCenter,getZoom,+ getInternalNode,getNodes,getNodesBounds} = useReactFlow()@@ -3735,15 +3743,31 @@ export function Canvas() {// overshot large terminals (their body never fit the viewport). fitView sizes the zoom// to the node and resolves group-relative positions itself; the clamp keeps a small// node from filling the whole screen and a huge one from being fit microscopic.- void fitView({- nodes: [{ id: node.id }],- duration: 300,- padding: 0.2,- minZoom: 0.25,- maxZoom: 1.15- })+ //+ // …but ONLY once React Flow has MEASURED the node. Its fit set is filtered by `measured`+ // (no width/height fallback in there), so an unmeasured node leaves the set EMPTY, the+ // bounds collapse to {0,0,0,0} and the camera flies to the canvas ORIGIN at max zoom —+ // empty canvas, node off-screen. That is precisely the state a node is in for the first+ // tick after its project loads, i.e. on every CROSS-PROJECT focus (OS-notification click,+ // sessions sidebar, ⌘K jump, presence travel): the load and the focus happen in the same+ // tick, so measuring can lose the race and only a second attempt would work. In that+ // window we frame the node ourselves from its persisted size — see lib/nodeFocus.+ //+ // The measured check must read React Flow's OWN store (`getInternalNode`), not our node+ // object: `measured` only reaches our state one render later, when `onNodesChange` applies+ // the dimensions change, so our copy says "unmeasured" for nodes the store has long sized.+ if (isMeasured(getInternalNode(node.id))) {+ void fitView({ nodes: [{ id: node.id }], duration: 300, ...FIT_NODE_OPTIONS })+ return+ }+ const rect = nodeFitRect(node as FocusableNode, nodesRef.current as FocusableNode[])+ const wrap = flowWrapRef.current?.getBoundingClientRect()+ const viewport = rect && wrap ? viewportForRect(rect, wrap.width, wrap.height) : null+ // Size unknowable / no pane yet: leave the camera where it is. Standing still beats+ // teleporting the user to the origin, which is the bug this branch exists for.+ if (viewport) void setViewport(viewport, { duration: 300 })},- [fitView]+ [fitView, setViewport, getInternalNode])const onNodeDoubleClick = useCallback(diff --git a/src/renderer/lib/nodeFocus.test.ts b/src/renderer/lib/nodeFocus.test.tsnew file mode 100644index 000000000..23df17e56--- /dev/null+++ b/src/renderer/lib/nodeFocus.test.ts@@ -0,0 +1,127 @@+import { describe, it, expect } from 'vitest'+import { getViewportForBounds } from '@xyflow/system'+import { FIT_NODE_OPTIONS, isMeasured, nodeFitRect, viewportForRect } from './nodeFocus'+import type { FocusableNode } from './nodeFocus'++const term = (over: Partial<FocusableNode> = {}): FocusableNode => ({+ id: 'n1',+ position: { x: 4000, y: 3000 },+ width: 600,+ height: 400,+ ...over+})++describe('nodeFitRect', () => {+ it('reads the persisted size of a node React Flow has not measured yet', () => {+ // The regression this guards: a node loaded a tick ago has NO `measured` — and React+ // Flow's own fitView drops such nodes, collapsing its bounds to the canvas origin.+ expect(nodeFitRect(term(), [term()])).toEqual({ x: 4000, y: 3000, width: 600, height: 400 })+ })++ it('prefers the measured size once React Flow has one (a live-resized terminal)', () => {+ const n = term({ measured: { width: 640, height: 512 } })+ expect(nodeFitRect(n, [n])).toEqual({ x: 4000, y: 3000, width: 640, height: 512 })+ })++ it('resolves a grouped node to its ABSOLUTE position', () => {+ const group: FocusableNode = {+ id: 'g',+ position: { x: 5000, y: 200 },+ width: 1400,+ height: 900+ }+ const child = term({ id: 'c', position: { x: 50, y: 60 }, parentId: 'g' })+ expect(nodeFitRect(child, [group, child])).toEqual({+ x: 5050,+ y: 260,+ width: 600,+ height: 400+ })+ })++ it('resolves a nested group chain', () => {+ const outer: FocusableNode = { id: 'o', position: { x: 1000, y: 1000 }, width: 100, height: 100 }+ const inner: FocusableNode = {+ id: 'i',+ position: { x: 100, y: 200 },+ width: 100,+ height: 100,+ parentId: 'o'+ }+ const child = term({ id: 'c', position: { x: 10, y: 20 }, parentId: 'i' })+ expect(nodeFitRect(child, [outer, inner, child])).toMatchObject({ x: 1110, y: 1220 })+ })++ it('survives a broken parent chain (missing parent, self-parent, cycle)', () => {+ const orphan = term({ parentId: 'gone' })+ expect(nodeFitRect(orphan, [orphan])).toMatchObject({ x: 4000, y: 3000 })++ const selfish = term({ id: 's', parentId: 's' })+ expect(nodeFitRect(selfish, [selfish])).toMatchObject({ x: 4000, y: 3000 })++ const a: FocusableNode = { id: 'a', position: { x: 1, y: 1 }, width: 10, height: 10, parentId: 'b' }+ const b: FocusableNode = { id: 'b', position: { x: 2, y: 2 }, width: 10, height: 10, parentId: 'a' }+ expect(nodeFitRect(a, [a, b])).not.toBeNull()+ })++ it('falls back to the style size, then gives up rather than guessing', () => {+ const styled: FocusableNode = {+ id: 'n',+ position: { x: 10, y: 20 },+ style: { width: 300, height: 150 }+ }+ expect(nodeFitRect(styled, [styled])).toEqual({ x: 10, y: 20, width: 300, height: 150 })++ const sizeless: FocusableNode = { id: 'n', position: { x: 10, y: 20 } }+ expect(nodeFitRect(sizeless, [sizeless])).toBeNull()+ // A zero-size node would produce the very origin jump we are fixing.+ expect(nodeFitRect({ id: 'n', position: { x: 5, y: 5 }, width: 0, height: 0 }, [])).toBeNull()+ })+})++describe('viewportForRect', () => {+ it('centres the node in the container instead of the canvas origin', () => {+ const vp = viewportForRect({ x: 4000, y: 3000, width: 600, height: 400 }, 1280, 900)+ // Same maths React Flow's fitView would have used for a MEASURED node.+ expect(vp).toEqual(+ getViewportForBounds(+ { x: 4000, y: 3000, width: 600, height: 400 },+ 1280,+ 900,+ FIT_NODE_OPTIONS.minZoom,+ FIT_NODE_OPTIONS.maxZoom,+ FIT_NODE_OPTIONS.padding+ )+ )+ // The node's centre lands in the middle of the container…+ expect(vp!.x + 4300 * vp!.zoom).toBeCloseTo(640, 0)+ expect(vp!.y + 3200 * vp!.zoom).toBeCloseTo(450, 0)+ // …which is emphatically NOT where an empty fit-set puts it (the bug: 640/450 at maxZoom,+ // i.e. the canvas origin parked in the middle of the screen).+ expect(vp!.x).not.toBeCloseTo(640, 0)+ })++ it('clamps the zoom for tiny and huge nodes', () => {+ expect(viewportForRect({ x: 0, y: 0, width: 20, height: 20 }, 1280, 900)!.zoom).toBe(+ FIT_NODE_OPTIONS.maxZoom+ )+ expect(viewportForRect({ x: 0, y: 0, width: 40000, height: 40000 }, 1280, 900)!.zoom).toBe(+ FIT_NODE_OPTIONS.minZoom+ )+ })++ it('refuses to compute against a container it cannot size', () => {+ expect(viewportForRect({ x: 0, y: 0, width: 600, height: 400 }, 0, 0)).toBeNull()+ })+})++describe('isMeasured', () => {+ it('reads React Flow measurements from either node shape, and tolerates a missing node', () => {+ expect(isMeasured({ measured: { width: 600, height: 400 } })).toBe(true)+ // A freshly deserialized node: sized, but not yet measured — fitView would DROP it.+ expect(isMeasured(term())).toBe(false)+ expect(isMeasured({ measured: { width: 600 } })).toBe(false)+ expect(isMeasured({ measured: { width: 0, height: 0 } })).toBe(false)+ expect(isMeasured(undefined)).toBe(false)+ })+})diff --git a/src/renderer/lib/nodeFocus.ts b/src/renderer/lib/nodeFocus.tsnew file mode 100644index 000000000..5108e5a7e--- /dev/null+++ b/src/renderer/lib/nodeFocus.ts@@ -0,0 +1,99 @@+import { getViewportForBounds, type Rect, type Viewport } from '@xyflow/system'++/**+ * "Zoom to this node" geometry, computed WITHOUT React Flow's measurements.+ *+ * Why this exists: `fitView({ nodes: [{ id }] })` is the natural way to frame one node, and it is+ * what `Canvas.goToNode` uses — but React Flow filters the fit set down to nodes it has already+ * MEASURED (`getFitViewNodes` keys off `measured.width && measured.height`; there is no+ * `width`/`height` fallback there). A node that was handed to React Flow a tick ago has no+ * measurement yet, so the fit set comes out EMPTY, its bounds collapse to `{0,0,0,0}` and the+ * camera flies to the canvas ORIGIN at max zoom — an empty stretch of canvas, nowhere near the+ * node. That is exactly the window a cross-project focus lands in: switch project → load its nodes+ * → focus the target, all before React Flow's mount-time measuring has run. (Clicking an OS+ * notification for a session in another project is the everyday path into it; the same node focuses+ * correctly on a second try, once it is measured.)+ *+ * So for the unmeasured case we do the same maths ourselves from the size the node was persisted+ * with — which needs no layout — and drive the camera with `setViewport`.+ */++/** The subset of a React Flow node this module needs. Loose on purpose: it must accept both a+ * freshly deserialized node (`width`/`height`, no `measured`) and a live measured one. */+export interface FocusableNode {+ id: string+ position: { x: number; y: number }+ parentId?: string+ width?: number | null+ height?: number | null+ measured?: { width?: number | null; height?: number | null }+ style?: { width?: number | string | null; height?: number | string | null }+}++/** Zoom/padding for framing a single node. Kept beside the maths so the fallback path and+ * `fitView` cannot drift apart: the clamp keeps a small node from filling the screen and a+ * huge one from being fit microscopic. */+export const FIT_NODE_OPTIONS = { padding: 0.2, minZoom: 0.25, maxZoom: 1.15 } as const++/** A `parentId` chain longer than this is a data bug (or a cycle) — stop walking. */+const MAX_PARENT_DEPTH = 20++const numeric = (v: unknown): number | null =>+ typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : null++const sizeOf = (n: FocusableNode): { width: number; height: number } | null => {+ const width = numeric(n.measured?.width) ?? numeric(n.width) ?? numeric(n.style?.width)+ const height = numeric(n.measured?.height) ?? numeric(n.height) ?? numeric(n.style?.height)+ return width && height ? { width, height } : null+}++/**+ * The node's rect in ABSOLUTE canvas coordinates, or null when its size is unknowable (in which+ * case the caller must leave the camera alone — a zero-size rect is what produces the origin jump).+ * A grouped node's `position` is relative to its group frame, so the parent chain is walked.+ */+export function nodeFitRect(node: FocusableNode, all: readonly FocusableNode[]): Rect | null {+ const size = sizeOf(node)+ if (!size) return null+ let x = node.position.x+ let y = node.position.y+ let parentId = node.parentId+ const seen = new Set<string>([node.id])+ for (let depth = 0; parentId && depth < MAX_PARENT_DEPTH; depth++) {+ if (seen.has(parentId)) break+ seen.add(parentId)+ const parent = all.find((n) => n.id === parentId)+ if (!parent) break+ x += parent.position.x+ y += parent.position.y+ parentId = parent.parentId+ }+ return { x, y, ...size }+}++/** The viewport that frames `rect` in a `containerWidth × containerHeight` pane, with the same+ * padding/zoom clamp `fitView` would have applied. Null when the container has no size yet. */+export function viewportForRect(+ rect: Rect,+ containerWidth: number,+ containerHeight: number+): Viewport | null {+ if (!(containerWidth > 0) || !(containerHeight > 0)) return null+ return getViewportForBounds(+ rect,+ containerWidth,+ containerHeight,+ FIT_NODE_OPTIONS.minZoom,+ FIT_NODE_OPTIONS.maxZoom,+ FIT_NODE_OPTIONS.padding+ )+}++/** Whether React Flow already knows this node's on-screen size — i.e. whether `fitView` will+ * actually include it in its fit set. Takes the minimal shape so it reads either a user-land+ * node or React Flow's own internal node (the authoritative one; see Canvas.goToNode). */+export function isMeasured(+ node: { measured?: { width?: number | null; height?: number | null } } | null | undefined+): boolean {+ return !!(numeric(node?.measured?.width) && numeric(node?.measured?.height))+}