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): spawn Duplicate/Branch/Transfer nodes where the user clicked

enes04dfa6c53a88
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 769446634..72e3e2862 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -106,6 +106,7 @@ import { PresenceNamePrompt } from '../components/PresenceNamePrompt'
import { nodeTravel, projectTravel } from '../lib/presenceTravel'
import {
FIT_NODE_OPTIONS,
+ absolutePosition,
isMeasured,
nodeFitRect,
viewportForRect,
@@ -245,6 +246,10 @@ const isMac = /Mac/i.test(navigator.platform || navigator.userAgent)
const GRID = 24
+/** Diagonal offset for a copy made without a cursor position (⌘K, agent CLI) — the classic
+ * "duplicate appears slightly off the original" nudge. */
+const DUPLICATE_NUDGE = 28
+
/** How long a successful worktree notice stays on screen before fading itself out. */
const NOTICE_MS = 6000
@@ -2059,6 +2064,47 @@ export function Canvas() {
}
}, [])
+ /** The group frame under an absolute canvas point, or undefined. Later entries win, so the
+ * frame drawn on top of another is the one that takes the node. */
+ const groupAtPoint = useCallback((pt: { x: number; y: number }): string | undefined => {
+ let hit: string | undefined
+ const all = nodesRef.current as FocusableNode[]
+ for (const n of nodesRef.current) {
+ if (n.type !== 'group') continue
+ const r = nodeFitRect(n as FocusableNode, all)
+ if (!r) continue
+ if (pt.x >= r.x && pt.x <= r.x + r.width && pt.y >= r.y && pt.y <= r.y + r.height) hit = n.id
+ }
+ return hit
+ }, [])
+
+ /** Where a node spawned FROM another node (Duplicate / Branch / Transfer) goes when the action
+ * carried no cursor position (⌘K, an agent CLI call): just right of its source.
+ * Read in ABSOLUTE coordinates on purpose — a grouped node's `position` is relative to its
+ * group frame, and using it raw threw the new node the group's own x/y away from the source. */
+ const besideNode = useCallback((source: CanvasNode): { x: number; y: number } => {
+ const p = absolutePosition(source as FocusableNode, nodesRef.current as FocusableNode[])
+ const width =
+ (source.measured?.width as number | undefined) ?? (source.width as number | undefined) ?? 600
+ return { x: p.x + width + 32, y: p.y }
+ }, [])
+
+ /** Put a spawned node at an ABSOLUTE canvas point — the point the user right-clicked, so the
+ * node appears where the menu was opened. Landing inside a group frame parents it into that
+ * frame (`parentInto` converts to group-relative), which is what keeps it from sitting on top
+ * of the group as an unrelated top-level node. */
+ const placeSpawned = useCallback(
+ (node: CanvasNode, pos: { x: number; y: number }): CanvasNode => {
+ const placed = { ...node, position: pos, parentId: undefined, extent: undefined }
+ // A group frame is never nested into another (the model is one level deep — see
+ // groupSelectedNodes/ungroupNodes); it just lands where it was dropped.
+ if (placed.type === 'group') return placed
+ const groupId = groupAtPoint(pos)
+ return groupId ? parentInto(placed, groupId) : placed
+ },
+ [groupAtPoint, parentInto]
+ )
+
const addTerminal = useCallback(
(
center?: { x: number; y: number },
@@ -3547,16 +3593,28 @@ export function Canvas() {
[setNodes]
)
+ /** `at` is the right-click position in flow coordinates: the copies land where the menu was
+ * opened. A multi-node duplicate keeps its arrangement — the selection's top-left is what
+ * lands on the cursor. Without a cursor (⌘K / an agent CLI call) the classic diagonal nudge
+ * applies, in ABSOLUTE space so a grouped node's copy still appears beside it. */
const duplicateNodes = useCallback(
- (ids: string[]) => {
+ (ids: string[], at?: { x: number; y: number }) => {
const set = new Set(ids)
setNodes((ns) => {
- const copies = ns.filter((n) => set.has(n.id)).map((n) => duplicateNode(n))
+ const sources = ns.filter((n) => set.has(n.id))
+ if (!sources.length) return ns
+ const all = ns as FocusableNode[]
+ const abs = sources.map((n) => absolutePosition(n as FocusableNode, all))
+ const dx = at ? at.x - Math.min(...abs.map((p) => p.x)) : DUPLICATE_NUDGE
+ const dy = at ? at.y - Math.min(...abs.map((p) => p.y)) : DUPLICATE_NUDGE
+ const copies = sources.map((n, i) =>
+ placeSpawned(duplicateNode(n), { x: abs[i].x + dx, y: abs[i].y + dy })
+ )
return [...ns.map((n) => ({ ...n, selected: false })), ...copies]
})
markDirty()
},
- [setNodes, markDirty]
+ [setNodes, markDirty, placeSpawned]
)
// Reload a terminal in place: bump `respawnNonce`, which re-runs TerminalNode's lifecycle
@@ -3586,7 +3644,10 @@ export function Canvas() {
// We already know the current session id from the hooks; only fall back to parsing the
// terminal output if it's unknown.
const branchClaude = useCallback(
- async (nodeId: string, opts?: { interactive?: boolean }): Promise<{ ok: boolean; error?: string; newNodeId?: string }> => {
+ async (
+ nodeId: string,
+ opts?: { interactive?: boolean; at?: { x: number; y: number } }
+ ): Promise<{ ok: boolean; error?: string; newNodeId?: string }> => {
const source = nodesRef.current.find((n) => n.id === nodeId) as CanvasNode | undefined
if (!source) return { ok: false, error: `no node with id ${nodeId}` }
const known = useAgentStatus.getState().byId[nodeId]?.sessionId
@@ -3616,23 +3677,22 @@ export function Canvas() {
),
title: `${source.data.title} (original)`
}
- copy.position = {
- x: source.position.x + ((source.width as number) ?? 600) + 32,
- y: source.position.y
- }
copy.selected = true
- setNodes((ns) => [...ns.map((n) => ({ ...n, selected: false })), copy])
+ // Where the user right-clicked when the action came from the node menu; beside the source
+ // otherwise (the agent-CLI `branch` verb and the header action have no cursor).
+ const placed = placeSpawned(copy, opts?.at ?? besideNode(source))
+ setNodes((ns) => [...ns.map((n) => ({ ...n, selected: false })), placed])
markDirty()
- return { ok: true, newNodeId: copy.id }
+ return { ok: true, newNodeId: placed.id }
},
- [api, setNodes, markDirty]
+ [api, setNodes, markDirty, placeSpawned, besideNode]
)
// Transfer this agent's full conversation to a different agent. We render the source
// agent's native transcript to a handoff file (main) and open a target node that reads it
// and continues. The source node stays. Mirrors branchClaude's placement.
const transferConversation = useCallback(
- async (sourceNodeId: string, targetAgentId: AgentId) => {
+ async (sourceNodeId: string, targetAgentId: AgentId, at?: { x: number; y: number }) => {
const source = nodesRef.current.find((n) => n.id === sourceNodeId) as CanvasNode | undefined
if (!source) return
const sourceAgentId = source.data.agentId
@@ -3686,15 +3746,12 @@ export function Canvas() {
source.data.accountId,
activePermissionMode()
)
- node.position = {
- x: source.position.x + ((source.width as number) ?? 600) + 32,
- y: source.position.y
- }
node.selected = true
- setNodes((ns) => [...ns.map((n) => ({ ...n, selected: false })), node])
+ const placed = placeSpawned(node, at ?? besideNode(source))
+ setNodes((ns) => [...ns.map((n) => ({ ...n, selected: false })), placed])
markDirty()
},
- [setNodes, markDirty]
+ [setNodes, markDirty, placeSpawned, besideNode]
)
const setNodesColor = useCallback(
@@ -3845,8 +3902,10 @@ export function Canvas() {
return node.selected && selected.length > 0 ? selected : [node.id]
}, [])
+ /** `at` is where the menu was opened, in flow coordinates: every entry that SPAWNS a node
+ * (Duplicate / Branch / Transfer) puts it there, instead of somewhere the user never pointed. */
const selectionItems = useCallback(
- (ids: string[]): MenuItem[] => [
+ (ids: string[], at?: { x: number; y: number }): MenuItem[] => [
{ type: 'label', label: ids.length > 1 ? `${ids.length} nodes` : '1 node' },
...((): MenuItem[] => {
// "Group …" only when something is actually groupable (top-level, not itself a group —
@@ -3877,7 +3936,7 @@ export function Canvas() {
})(),
{ type: 'colors', onPick: (c) => setNodesColor(ids, c) },
{ type: 'separator' },
- { label: 'Duplicate', icon: <IconDuplicate />, onClick: () => duplicateNodes(ids) },
+ { label: 'Duplicate', icon: <IconDuplicate />, onClick: () => duplicateNodes(ids, at) },
...(ids.length === 1 && (() => {
const a = agentIdOf(ids[0])
return !!a && canBranch(a)
@@ -3886,7 +3945,7 @@ export function Canvas() {
{
label: 'Branch conversation',
icon: <IconBranch />,
- onClick: () => void branchClaude(ids[0])
+ onClick: () => void branchClaude(ids[0], { at })
}
] as MenuItem[])
: []),
@@ -3913,7 +3972,7 @@ export function Canvas() {
(tg): MenuItem => ({
label: tg.label,
icon: <AgentIcon agentId={tg.id} />,
- onClick: () => void transferConversation(ids[0], tg.id)
+ onClick: () => void transferConversation(ids[0], tg.id, at)
})
)
] as MenuItem[]
@@ -4140,7 +4199,7 @@ export function Canvas() {
const items =
node.type === 'group'
? groupItems(node.id, screenToFlowPosition({ x: e.clientX, y: e.clientY }))
- : selectionItems(targetIds(node))
+ : selectionItems(targetIds(node), screenToFlowPosition({ x: e.clientX, y: e.clientY }))
setMenu({ x: e.clientX, y: e.clientY, items })
},
[groupItems, selectionItems, targetIds, screenToFlowPosition]
@@ -4149,9 +4208,16 @@ export function Canvas() {
const onSelectionContextMenu = useCallback(
(e: React.MouseEvent, selected: Node[]) => {
e.preventDefault()
- setMenu({ x: e.clientX, y: e.clientY, items: selectionItems(selected.map((n) => n.id)) })
+ setMenu({
+ x: e.clientX,
+ y: e.clientY,
+ items: selectionItems(
+ selected.map((n) => n.id),
+ screenToFlowPosition({ x: e.clientX, y: e.clientY })
+ )
+ })
},
- [selectionItems]
+ [selectionItems, screenToFlowPosition]
)
// Title/color/text edits go through updateNodeData; watch them so they persist too.
diff --git a/src/renderer/lib/nodeFocus.test.ts b/src/renderer/lib/nodeFocus.test.ts
index 23df17e56..9d58f569a 100644
--- a/src/renderer/lib/nodeFocus.test.ts
+++ b/src/renderer/lib/nodeFocus.test.ts
@@ -1,6 +1,12 @@
import { describe, it, expect } from 'vitest'
import { getViewportForBounds } from '@xyflow/system'
-import { FIT_NODE_OPTIONS, isMeasured, nodeFitRect, viewportForRect } from './nodeFocus'
+import {
+ FIT_NODE_OPTIONS,
+ absolutePosition,
+ isMeasured,
+ nodeFitRect,
+ viewportForRect
+} from './nodeFocus'
import type { FocusableNode } from './nodeFocus'
const term = (over: Partial<FocusableNode> = {}): FocusableNode => ({
@@ -11,6 +17,33 @@ const term = (over: Partial<FocusableNode> = {}): FocusableNode => ({
...over
})
+describe('absolutePosition', () => {
+ it('returns the position of a top-level node unchanged', () => {
+ expect(absolutePosition(term(), [term()])).toEqual({ x: 4000, y: 3000 })
+ })
+
+ it('adds the group origin for a child (what node PLACEMENT needs)', () => {
+ // The regression this guards: Duplicate / Branch / Transfer positioned the new node from the
+ // source's raw `position`, which for a grouped node is relative to its frame — so a copy made
+ // top-level landed the group's own x/y away from the node it came from.
+ const group: FocusableNode = { id: 'g', position: { x: 5000, y: 200 } }
+ const child = term({ id: 'c', position: { x: 50, y: 60 }, parentId: 'g' })
+ expect(absolutePosition(child, [group, child])).toEqual({ x: 5050, y: 260 })
+ })
+
+ it('answers even when the node has no size at all', () => {
+ const n: FocusableNode = { id: 'x', position: { x: 12, y: 34 } }
+ expect(absolutePosition(n, [n])).toEqual({ x: 12, y: 34 })
+ expect(nodeFitRect(n, [n])).toBeNull()
+ })
+
+ it('stops on a parent cycle instead of looping', () => {
+ const a: FocusableNode = { id: 'a', position: { x: 10, y: 10 }, parentId: 'b' }
+ const b: FocusableNode = { id: 'b', position: { x: 20, y: 20 }, parentId: 'a' }
+ expect(absolutePosition(a, [a, b])).toEqual({ x: 30, y: 30 })
+ })
+})
+
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
diff --git a/src/renderer/lib/nodeFocus.ts b/src/renderer/lib/nodeFocus.ts
index 5108e5a7e..979b72f77 100644
--- a/src/renderer/lib/nodeFocus.ts
+++ b/src/renderer/lib/nodeFocus.ts
@@ -48,13 +48,16 @@ const sizeOf = (n: FocusableNode): { width: number; height: number } | 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.
+ * The node's top-left in ABSOLUTE canvas coordinates. A grouped node's `position` is relative to
+ * its group frame, so the parent chain is walked. Needs no size — unlike `nodeFitRect` this always
+ * answers, which is what placement (as opposed to framing) needs: a node spawned next to a grouped
+ * source must be positioned in the same space the source really occupies, not at its raw
+ * group-relative `position`.
*/
-export function nodeFitRect(node: FocusableNode, all: readonly FocusableNode[]): Rect | null {
- const size = sizeOf(node)
- if (!size) return null
+export function absolutePosition(
+ node: FocusableNode,
+ all: readonly FocusableNode[]
+): { x: number; y: number } {
let x = node.position.x
let y = node.position.y
let parentId = node.parentId
@@ -68,7 +71,17 @@ export function nodeFitRect(node: FocusableNode, all: readonly FocusableNode[]):
y += parent.position.y
parentId = parent.parentId
}
- return { x, y, ...size }
+ return { x, y }
+}
+
+/**
+ * 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).
+ */
+export function nodeFitRect(node: FocusableNode, all: readonly FocusableNode[]): Rect | null {
+ const size = sizeOf(node)
+ if (!size) return null
+ return { ...absolutePosition(node, all), ...size }
}
/** The viewport that frames `rect` in a `containerWidth × containerHeight` pane, with the same