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(canvas-control): open-worktree / close-worktree verbs

enes2bd1ee0219a5
diff --git a/src/main/canvas-control-core.test.ts b/src/main/canvas-control-core.test.ts
index d784e107f..742da23fa 100644
--- a/src/main/canvas-control-core.test.ts
+++ b/src/main/canvas-control-core.test.ts
@@ -72,6 +72,21 @@ describe('parseControlRequest', () => {
expect(isDestructiveVerb('open-agent')).toBe(false)
})
+ it('open-worktree requires --branch, close-worktree requires --group; neither destructive', () => {
+ expect(parseControlRequest('open-worktree', {})).toEqual({ error: 'open-worktree requires --branch <name>' })
+ expect(parseControlRequest('open-worktree', { branch: 'feat/x' })).toEqual({
+ verb: 'open-worktree',
+ args: { branch: 'feat/x' }
+ })
+ expect(parseControlRequest('close-worktree', {})).toEqual({ error: 'close-worktree requires --group <id>' })
+ expect(parseControlRequest('close-worktree', { group: 'g1' })).toEqual({
+ verb: 'close-worktree',
+ args: { group: 'g1' }
+ })
+ expect(isDestructiveVerb('open-worktree')).toBe(false)
+ expect(isDestructiveVerb('close-worktree')).toBe(false)
+ })
+
it('branch requires --node, and is not destructive', () => {
expect(parseControlRequest('branch', {})).toEqual({ error: 'branch requires --node <id>' })
expect(parseControlRequest('branch', { node: 'n1' })).toEqual({
diff --git a/src/main/canvas-control-core.ts b/src/main/canvas-control-core.ts
index b84937655..e671db61c 100644
--- a/src/main/canvas-control-core.ts
+++ b/src/main/canvas-control-core.ts
@@ -15,6 +15,8 @@ export type ControlVerb =
| 'arrange'
| 'align'
| 'spawn-team'
+ | 'open-worktree'
+ | 'close-worktree'
| 'branch'
| 'rename'
| 'write'
@@ -38,6 +40,8 @@ const VERBS: ControlVerb[] = [
'arrange',
'align',
'spawn-team',
+ 'open-worktree',
+ 'close-worktree',
'branch',
'rename',
'write',
@@ -72,6 +76,8 @@ export function parseControlRequest(
if (v === 'align' && !args.nodes) return { error: 'align requires --nodes <id,id>' }
if (v === 'align' && !args.edge) return { error: 'align requires --edge' }
if (v === 'spawn-team' && !args.team) return { error: 'spawn-team requires --team <json>' }
+ if (v === 'open-worktree' && !args.branch) return { error: 'open-worktree requires --branch <name>' }
+ if (v === 'close-worktree' && !args.group) return { error: 'close-worktree requires --group <id>' }
if (v === 'branch' && !args.node) return { error: 'branch requires --node <id>' }
if (v === 'rename' && !args.node) return { error: 'rename requires --node <id>' }
if (v === 'rename' && !args.title) return { error: 'rename requires --title' }
@@ -125,6 +131,10 @@ export function buildCanvasControlInstructions(shimPath: string): string {
' `align --nodes <id,id> --edge left|right|top|bottom|hcenter|vcenter` — organize the canvas.',
'- `spawn-team --label L --team \'[{"title":"UI","prompt":"...","agent":"claude"}]\'` — one agent per',
' role (max 8), arranged in a grid, wrapped in a labeled group, each connected to you.',
+ '- `open-worktree --branch <name> [--base <ref>] [--path P] [--group <id>]` — create a git worktree',
+ ' wrapped in a bound group frame (terminals inside it run in the worktree). Local projects only.',
+ '- `close-worktree --group <id> [--mode unbind|remove]` — unbind keeps the directory; remove asks',
+ ' the user to confirm deletion.',
'- `branch --node <id>` — branch a Claude node\'s conversation (Claude nodes only).',
'- `rename --node <id> --title "New Name"` — rename any node (terminals, groups, stickies…).',
'- `write --node <id> --text "..."` / `close --node <id>` — type into / close a node.',
diff --git a/src/main/canvas-control.ts b/src/main/canvas-control.ts
index 293789dd2..e36d0e74e 100644
--- a/src/main/canvas-control.ts
+++ b/src/main/canvas-control.ts
@@ -74,6 +74,12 @@ Verbs:
- \`spawn-team --label "Frontend Team" --team '[{"title":"UI","prompt":"...","agent":"claude"}]'\` —
open one agent per role (each prompt starts that member working), arrange them in a grid,
wrap them in a labeled group, and connect each to you. Max 8 roles per call.
+- \`open-worktree --branch <name> [--base <ref>] [--path P] [--group <id>]\` — create a git
+ worktree (new branch off base, default: the repo's default branch) and wrap it in a bound
+ group frame (or bind it to an existing empty group). Terminals created inside the group
+ run in the worktree. Local projects only.
+- \`close-worktree --group <id> [--mode unbind|remove]\` — unbind (default) drops the binding
+ and keeps the directory; remove asks the user to confirm deleting the worktree.
- \`branch --node <id>\` — branch a Claude node's conversation: the node stays on the new
branch and a new node opens resuming the original. Target must be a Claude agent node.
- \`rename --node <id> --title "New Name"\` — rename any node (terminals, groups, stickies…).
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 7d43dadab..a163f9ff5 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -2455,7 +2455,7 @@ export function Canvas() {
// carries no cwd of its own — the worktree's path is what its children inherit
// (`cwdForNewNodeIn`), so the frame IS the binding.
const attachWorktree = useCallback(
- (target: { groupId: string | null; at?: { x: number; y: number } }, wt: GroupWorktree) => {
+ (target: { groupId: string | null; at?: { x: number; y: number } }, wt: GroupWorktree): string => {
let groupId = target.groupId
if (groupId) {
setNodes((ns) =>
@@ -2474,6 +2474,9 @@ export function Canvas() {
}
markDirty()
refreshWorktreeStore({ bind: { groupId, worktree: wt } })
+ // The bound group's id (fresh one when created here) — nodesRef lags setNodes, so
+ // callers that need the id (agent-control's open-worktree reply) take it from here.
+ return groupId
},
[setNodes, markDirty, viewCenter, refreshWorktreeStore]
)
@@ -2750,6 +2753,25 @@ export function Canvas() {
return () => setWorktreeActionHandler(null)
}, [onWorktreeAction])
+ // Latest worktree callbacks for the agent-control handler. That effect mounts ONCE (empty
+ // deps) and these callbacks' identities change with the active project (activeProjectId /
+ // isSshProject in their deps) — calling the first-render closures would run against project
+ // '' (refresh no-ops, wrong SSH gate). The ref always holds this render's instances.
+ const worktreeControlRef = useRef({
+ attachWorktree,
+ releaseWorktreeBinding,
+ clearWorktreeBinding,
+ requestRemoveWorktree
+ })
+ useEffect(() => {
+ worktreeControlRef.current = {
+ attachWorktree,
+ releaseWorktreeBinding,
+ clearWorktreeBinding,
+ requestRemoveWorktree
+ }
+ })
+
// 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 —
@@ -3874,6 +3896,97 @@ export function Canvas() {
})
return
}
+ case 'open-worktree': {
+ // Mirrors createWorktreeAndGroup/attachWorktree minus the dialog: create the git
+ // worktree (new branch off base), then wrap a bound group frame below the source
+ // (or bind an existing empty group via --group).
+ const projStore = useProjects.getState()
+ const project = projStore.getProject(projStore.activeProjectId ?? '')
+ if (project?.ssh) {
+ reply({ ok: false, error: WORKTREE_SSH_NOTICE })
+ return
+ }
+ const branch = sanitizeWorktreeBranch(args.branch ?? '')
+ if (!branch) {
+ reply({ ok: false, error: `open-worktree: invalid branch name "${args.branch}"` })
+ return
+ }
+ const { repoRoot, entries } = useWorktrees.getState()
+ if (!repoRoot) {
+ reply({ ok: false, error: 'open-worktree: this project has no git repository (repo root unknown)' })
+ return
+ }
+ let bindGroupId: string | null = null
+ if (args.group) {
+ const g = nodesRef.current.find((nd) => nd.id === args.group)
+ if (!g || g.type !== 'group' || g.data.worktree) {
+ reply({ ok: false, error: 'open-worktree: --group must name an existing group without a worktree' })
+ return
+ }
+ bindGroupId = g.id
+ }
+ const baseRef = args.base?.trim() || resolveBaseRef(entries)
+ const wtPath =
+ args.path?.trim() ||
+ computeWorktreePath(
+ await window.nodeTerminal.userDataDir(),
+ repoRoot.split('/').pop() || 'repo',
+ branch
+ )
+ if (!wtPath) {
+ reply({ ok: false, error: 'open-worktree: could not derive a worktree path — pass --path' })
+ return
+ }
+ const res = await window.nodeTerminal.git
+ .worktreeAdd(repoRoot, wtPath, branch, baseRef, true)
+ .catch((e: unknown) => ({
+ ok: false as const,
+ message: e instanceof Error ? e.message : String(e)
+ }))
+ if (!res.ok) {
+ reply({ ok: false, error: `open-worktree: ${res.message}` })
+ return
+ }
+ const groupId = worktreeControlRef.current.attachWorktree(
+ { groupId: bindGroupId, at: placeBelow() },
+ worktreeFromCreate({ repoPath: repoRoot, mode: 'new', branch, baseRef, path: wtPath })
+ )
+ reply({
+ ok: true,
+ message: `opened worktree ${branch} at ${wtPath} in group ${groupId}`,
+ result: { groupId, branch, path: wtPath, baseRef }
+ })
+ return
+ }
+ case 'close-worktree': {
+ const id = args.group ?? ''
+ const g = nodesRef.current.find((nd) => nd.id === id)
+ if (!g || g.type !== 'group' || !g.data.worktree) {
+ reply({ ok: false, error: `close-worktree: ${id} is not a worktree-bound group` })
+ return
+ }
+ const mode = args.mode ?? 'unbind'
+ const sshProject = !!useProjects.getState().getProject(useProjects.getState().activeProjectId ?? '')?.ssh
+ if (mode !== 'unbind' && sshProject) {
+ reply({ ok: false, error: WORKTREE_SSH_NOTICE })
+ return
+ }
+ const ctl = worktreeControlRef.current
+ if (mode === 'unbind') {
+ // Non-destructive: drops the binding, the worktree stays on disk as an orphan.
+ await ctl.releaseWorktreeBinding(id).finally(() => ctl.clearWorktreeBinding(id))
+ reply({ ok: true, message: `unbound worktree from ${id} (directory kept on disk)` })
+ return
+ }
+ if (mode === 'remove') {
+ // Destructive → the existing ask-first safety dialog decides; reply now (once).
+ void ctl.requestRemoveWorktree(id)
+ reply({ ok: true, message: 'removal confirmation shown to the user — they decide' })
+ return
+ }
+ reply({ ok: false, error: `close-worktree: unknown --mode ${mode} (unbind|remove)` })
+ return
+ }
case 'branch': {
const id = args.node ?? ''
const target = nodesRef.current.find((nd) => nd.id === id)