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.

feat(worktree): opt-in move existing terminal into worktree (guarded respawn)

enes4b88be92a5f2
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 0a7139fac..2380e20c2 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -15,7 +15,7 @@ import {
type Viewport
} from '@xyflow/react'
import type { Edge, Node } from '@xyflow/react'
-import { TerminalNode } from '../nodes/TerminalNode'
+import { TerminalNode, setMoveIntoWorktreeHandler } from '../nodes/TerminalNode'
import { StickyNode } from '../nodes/StickyNode'
import { GroupNode, setWorktreeActionHandler } from '../nodes/GroupNode'
import { EditorNode } from '../nodes/EditorNode'
@@ -136,6 +136,8 @@ export function Canvas() {
const [consentOpen, setConsentOpen] = useState(false)
// Group id awaiting a worktree bind (drives BindWorktreeDialog).
const [bindTarget, setBindTarget] = useState<string | null>(null)
+ // Terminal node id awaiting confirmation to move into its group's worktree.
+ const [moveTarget, setMoveTarget] = useState<string | null>(null)
const settings = useSettings((s) => s.settings)
const viewportRef = useRef<Viewport>({ x: 0, y: 0, zoom: 1 })
const nodesRef = useRef<CanvasNode[]>(nodes)
@@ -1038,6 +1040,48 @@ export function Canvas() {
return () => setWorktreeActionHandler(null)
}, [onWorktreeAction])
+ // Move an existing terminal into its group's worktree. The "↪" header action requests it;
+ // confirming respawns the node's session in the worktree cwd. We bump `respawnNonce` (a
+ // transient, non-persisted trigger) so TerminalNode's session-creation effect re-runs —
+ // its cleanup kills the old tmux session (same node id = same target) and create() spawns a
+ // fresh one with the new cwd. Changing cwd alone wouldn't re-run that `[respawnNonce]` effect.
+ const requestMoveIntoWorktree = useCallback((nodeId: string) => setMoveTarget(nodeId), [])
+
+ const confirmMoveIntoWorktree = useCallback(() => {
+ const id = moveTarget
+ setMoveTarget(null)
+ if (!id) return
+ const node = nodesRef.current.find((n) => n.id === id)
+ const parent = nodesRef.current.find((p) => p.id === node?.parentId)
+ const wtPath = parent?.data.worktree?.path as string | undefined
+ if (!node || node.data.remote || !wtPath || node.data.cwd === wtPath) return
+ // Permanently end the old tmux session (destroy, not kill) so the respawned create() opens
+ // a fresh session in the new cwd instead of reattaching to the existing `nt-<id>` session
+ // (which would keep the old working directory). The node id / persistKey is unchanged.
+ transport.destroy(id)
+ setNodes((ns) =>
+ ns.map((n) =>
+ n.id === id
+ ? {
+ ...n,
+ data: {
+ ...n.data,
+ cwd: wtPath,
+ respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1
+ }
+ }
+ : n
+ )
+ )
+ markDirty()
+ }, [moveTarget, setNodes, markDirty])
+
+ // Bridge the move-into-worktree handler to TerminalNode (React Flow owns the instances).
+ useEffect(() => {
+ setMoveIntoWorktreeHandler(requestMoveIntoWorktree)
+ return () => setMoveIntoWorktreeHandler(null)
+ }, [requestMoveIntoWorktree])
+
const toggleMarkdown = useCallback(
(ids: string[]) => {
const set = new Set(ids)
@@ -1943,6 +1987,16 @@ export function Canvas() {
/>
)}
+ {moveTarget && (
+ <ConfirmDialog
+ message="Move this terminal into the worktree? Its session restarts and any running process ends."
+ confirmLabel="Move"
+ danger={false}
+ onConfirm={confirmMoveIntoWorktree}
+ onCancel={() => setMoveTarget(null)}
+ />
+ )}
+
{consentOpen && (
<NotifyConsentDialog
onEnable={() => {
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 73f33bf4a..fa07a5c60 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -34,6 +34,16 @@ function escapeDroppedPath(p: string): string {
return p.replace(/([ \t"'`\\()&;|<>$!*?[\]{}#~])/g, '\\$1')
}
+/**
+ * Move-into-worktree handler bridge. Like GroupNode's worktree-action bridge: React Flow
+ * instantiates custom nodes itself, so Canvas can't pass this callback through props. Canvas
+ * registers its handler here on mount; the "↪" header action calls it with the node id.
+ */
+let moveIntoWorktreeHandler: ((nodeId: string) => void) | null = null
+export function setMoveIntoWorktreeHandler(fn: ((nodeId: string) => void) | null): void {
+ moveIntoWorktreeHandler = fn
+}
+
/**
* A single terminal node: header (collapse + color + title + close), optional tag chips,
* and a real xterm.js terminal. A hover guard delays entering the terminal so the canvas
@@ -41,8 +51,8 @@ function escapeDroppedPath(p: string): string {
* toggles a markdown view of the terminal's output. Files dropped from Finder are pasted
* as their (escaped) paths, like a native terminal — so Claude can read dropped images.
*/
-export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
- const { updateNodeData, deleteElements, getZoom, setNodes } = useReactFlow()
+export function TerminalNode({ id, data, selected, parentId }: NodeProps<CanvasNode>) {
+ const { updateNodeData, deleteElements, getZoom, setNodes, getNode } = useReactFlow()
// Pick the session layer: a remote-bound node (data.remote) talks to a host over the relay
// via RemoteTransport; otherwise the local PTY (LocalTransport). The connectionId is stable
// for a node's lifetime, so the instance is created once and held in a ref.
@@ -83,6 +93,14 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
const showUsage = !!agentId && hasUsage(agentId) // per-node context-window meter
const showChat = !!agentId && canChat(agentId) // Cmd+M opens a chat panel instead of markdown
const agentLabel = (agentId ? agentConfig(agentId) : undefined)?.label ?? 'Agent'
+ // "Move into worktree" affordance: shown only when this terminal is a child of a group that
+ // is bound to a worktree AND its current cwd differs from that worktree path (i.e. it's still
+ // running in the old folder). Reads the parent group from React Flow state (single source of
+ // truth); `parentId` is set by the group reparenting transforms.
+ const parentWtPath = parentId
+ ? ((getNode(parentId) as CanvasNode | undefined)?.data.worktree?.path as string | undefined)
+ : undefined
+ const canMoveIntoWorktree = !!parentWtPath && (data.cwd as string | undefined) !== parentWtPath
const status = useAgentStatus((s) => s.byId[id])
// Use the chat panel only for a chat-capable agent with a known session; otherwise the
// markdown-of-output view (computed in the capture effect below) is shown as a fallback.
@@ -146,7 +164,10 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
if (showLink) updateNodeInternals(id)
}, [showLink, id, updateNodeInternals])
- // Terminal lifecycle — set up exactly once.
+ // Terminal lifecycle — set up once on mount, and again whenever `respawnNonce` is bumped
+ // (e.g. moving this terminal into a worktree). Bumping the nonce runs the cleanup below
+ // (kill the old session + dispose xterm), then recreates the session with the latest
+ // `data.cwd`. The node `id` (= tmux persistKey) is unchanged, so it's the same target.
useEffect(() => {
const container = bodyRef.current
if (!container) return
@@ -288,7 +309,7 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
searchAddonRef.current = null
}
// eslint-disable-next-line react-hooks/exhaustive-deps
- }, [])
+ }, [data.respawnNonce])
// Live-apply font/cursor settings to the running terminal.
useEffect(() => {
@@ -561,6 +582,16 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
</span>
)}
{!editingTitle && <span className="term-node__spacer" />}
+ {canMoveIntoWorktree && (
+ <Tooltip label="Move this terminal into the group's worktree">
+ <button
+ className="term-node__move-worktree nodrag"
+ onClick={() => moveIntoWorktreeHandler?.(id)}
+ >
+ ↪
+ </button>
+ </Tooltip>
+ )}
<Tooltip label={showUsage ? 'Search terminal + conversation' : 'Search this terminal'}>
<button
className="term-node__search nodrag"
diff --git a/src/renderer/state/workspace.ts b/src/renderer/state/workspace.ts
index 143eebcd3..404b58491 100644
--- a/src/renderer/state/workspace.ts
+++ b/src/renderer/state/workspace.ts
@@ -35,6 +35,12 @@ export interface NodeData {
expandedHeight?: number
/** One-shot command run once when the terminal first opens (not persisted). */
initialCommand?: string
+ /**
+ * Transient respawn trigger: bumping this number tears down a terminal node's session and
+ * recreates it (used to move an existing terminal into a worktree cwd). Not persisted —
+ * deliberately absent from flowToNodeStates, like initialCommand/expandedHeight.
+ */
+ respawnNonce?: number
shell?: string
cwd?: string
text?: string
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index 74162117f..89899e6cf 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -642,6 +642,23 @@ body,
cursor: default;
}
+.term-node__move-worktree {
+ background: transparent;
+ border: none;
+ color: var(--muted);
+ cursor: pointer;
+ font-size: 13px;
+ line-height: 1;
+ padding: 2px 5px;
+ border-radius: 4px;
+ flex-shrink: 0;
+}
+
+.term-node__move-worktree:hover {
+ background: rgba(10, 132, 255, 0.18);
+ color: #6cb0ff;
+}
+
.term-node__search {
background: transparent;
border: none;