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): one-step creation with the repo pre-filled and existing worktrees listed

enes40b16919a697
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index c86a7a3e0..c61d0f098 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -72,7 +72,7 @@ import { ConfirmDialog } from '../components/ConfirmDialog'
import { promptDialog } from '../components/promptDialog'
import { UpgradeDialog } from '../components/UpgradeDialog'
import { RemotePicker } from '../components/RemotePicker'
-import { BindWorktreeDialog, type BindWorktreeValue } from '../components/BindWorktreeDialog'
+import { WorktreeDialog, type WorktreeCreateValue } from '../components/WorktreeDialog'
import { NotifyConsentDialog } from '../components/NotifyConsentDialog'
import { ExplorerPanel } from '../components/ExplorerPanel'
import { SessionsSidebar } from '../components/SessionsSidebar'
@@ -91,7 +91,13 @@ import { useAgentNodes } from '../state/agentNodes'
import { SubagentNode } from '../nodes/SubagentNode'
import { LoopNode } from '../nodes/LoopNode'
import type { NormalizedAgentEvent } from '@shared/agents/normalize'
-import { computeWorktreePath, sanitizeWorktreeBranch } from '@shared/worktree'
+import {
+ computeWorktreePath,
+ sanitizeWorktreeBranch,
+ type GroupWorktree,
+ type WorktreeEntry
+} from '@shared/worktree'
+import { useWorktrees } from '../state/worktrees'
import {
agentConfig,
hasHooks,
@@ -139,6 +145,7 @@ import {
createDinoNode,
createDiffNode,
createEditorNode,
+ createGroupNode,
createSshTerminalNode,
createStickyNode,
createTerminalNode,
@@ -346,8 +353,18 @@ export function Canvas() {
// Node to center once its project finishes loading (cross-project notification click).
const pendingFocusRef = useRef<string | null>(null)
const [consentOpen, setConsentOpen] = useState(false)
- // Group id awaiting a worktree bind (drives BindWorktreeDialog).
- const [bindTarget, setBindTarget] = useState<string | null>(null)
+ // Drives WorktreeDialog. `groupId` null = create the group frame around the new worktree;
+ // set = bind an existing group (the group context menu). `at` is the pane cursor, if any.
+ const [worktreeDialog, setWorktreeDialog] = useState<{
+ groupId: string | null
+ at?: { x: number; y: number }
+ } | null>(null)
+ const [worktreeBusy, setWorktreeBusy] = useState(false)
+ const [worktreeError, setWorktreeError] = useState<string | null>(null)
+ // The store is filled asynchronously by the active-project effect, so the dialog subscribes
+ // (rather than reading getState() once) — the repo may resolve after it's already open.
+ const worktreeRepoRoot = useWorktrees((s) => s.repoRoot)
+ const worktreeOrphans = useWorktrees((s) => s.orphans)
// Writable base dir for the default worktree path (Electron userData), fetched once on mount.
const userDataDirRef = useRef('')
useEffect(() => {
@@ -766,6 +783,18 @@ export function Canvas() {
loadingRef.current = true
const flow = nodeStatesToFlow(project.nodes)
setNodes(flow)
+ // Worktree facts are per project: drop the previous project's (reset also clears its
+ // statuses), then re-resolve from this project's cwd. SSH projects are skipped — local git
+ // cannot reason about a remote path. Fire-and-forget: the store is epoch-guarded + fails open.
+ useWorktrees.getState().reset()
+ if (project.cwd && !project.ssh) {
+ void useWorktrees.getState().refresh(
+ project.cwd,
+ flow
+ .filter((n) => n.type === 'group' && n.data.worktree)
+ .map((n) => ({ groupId: n.id, worktree: n.data.worktree as GroupWorktree }))
+ )
+ }
setLinkEdges((project.bridges ?? []).map((b) => ({ id: b.id, source: b.source, target: b.target })))
// Restore control ropes with the source agent's color (falls back to the browser blue).
setControlEdges(
@@ -1848,39 +1877,85 @@ export function Canvas() {
[]
)
- const bindGroupToWorktree = useCallback((groupId: string) => setBindTarget(groupId), [])
+ const openWorktreeDialog = useCallback(
+ (groupId: string | null, at?: { x: number; y: number }) => {
+ setWorktreeError(null)
+ setWorktreeDialog({ groupId, at })
+ },
+ []
+ )
- const confirmBind = useCallback(
- async (v: BindWorktreeValue) => {
- const git = window.nodeTerminal.git
- const res = await git.worktreeAdd(v.repoPath, v.path, v.branch, v.baseRef, v.mode === 'new')
+ // Bind the worktree to an EXISTING group, or create a group around a new one. A group node
+ // 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) => {
+ if (target.groupId) {
+ setNodes((ns) =>
+ ns.map((n) =>
+ n.id === target.groupId ? { ...n, data: { ...n.data, worktree: wt } } : n
+ )
+ )
+ } else {
+ setNodes((ns) => {
+ const group = createGroupNode(target.at ?? viewCenter() ?? { x: 0, y: 0 }, undefined, ns.length)
+ group.data = { ...group.data, title: wt.branch, worktree: wt }
+ // Parents must come first — React Flow requires a group before its children.
+ return [group, ...(ns as CanvasNode[])]
+ })
+ }
+ markDirty()
+ },
+ [setNodes, markDirty, viewCenter]
+ )
+
+ const createWorktreeAndGroup = useCallback(
+ async (v: WorktreeCreateValue) => {
+ const target = worktreeDialog
+ if (!target) return
+ setWorktreeBusy(true)
+ setWorktreeError(null)
+ const res = await window.nodeTerminal.git.worktreeAdd(
+ v.repoPath,
+ v.path,
+ v.branch,
+ v.baseRef,
+ v.mode === 'new'
+ )
+ setWorktreeBusy(false)
if (!res.ok) {
- window.alert(res.message)
+ setWorktreeError(res.message) // inline, never window.alert
return
}
- setNodes((ns) =>
- ns.map((n) =>
- n.id === bindTarget
- ? {
- ...n,
- data: {
- ...n.data,
- worktree: {
- repoPath: v.repoPath,
- branch: v.branch,
- baseRef: v.baseRef,
- path: v.path,
- createdByApp: true
- }
- }
- }
- : n
- )
- )
- setBindTarget(null)
- markDirty()
+ attachWorktree(target, {
+ repoPath: v.repoPath,
+ branch: v.branch,
+ baseRef: v.baseRef,
+ path: v.path,
+ // We created this directory, so Remove may delete it.
+ createdByApp: true
+ })
+ setWorktreeDialog(null)
},
- [bindTarget, setNodes, markDirty]
+ [attachWorktree, worktreeDialog]
+ )
+
+ const bindExistingWorktree = useCallback(
+ (e: WorktreeEntry) => {
+ const target = worktreeDialog
+ const root = useWorktrees.getState().repoRoot
+ if (!target || !root || !e.branch) return
+ attachWorktree(target, {
+ repoPath: root,
+ branch: e.branch,
+ baseRef: 'main',
+ path: e.path,
+ // The user (or a previous Unbind) made this one — Remove must not delete it by default.
+ createdByApp: false
+ })
+ setWorktreeDialog(null)
+ },
+ [attachWorktree, worktreeDialog]
)
// Ask-first worktree removal (Task 9). Gather any uncommitted-work info, then open a safety
@@ -2452,7 +2527,7 @@ export function Canvas() {
{
label: 'Bind to worktree…',
icon: <IconBranch />,
- onClick: () => bindGroupToWorktree(groupId)
+ onClick: () => openWorktreeDialog(groupId)
} as MenuItem
]),
{ label: 'Ungroup', icon: <IconUngroup />, onClick: () => ungroup(groupId) },
@@ -2462,7 +2537,7 @@ export function Canvas() {
setNodesColor,
ungroup,
groupHasWorktree,
- bindGroupToWorktree,
+ openWorktreeDialog,
addTerminal,
agentCreationItems,
addSticky
@@ -2492,6 +2567,9 @@ export function Canvas() {
{ label: 'New dino game', icon: <IconDino />, onClick: () => addDino(at) },
{ label: 'Open file…', icon: <IconEditor />, onClick: () => void openFileDialog(at) },
{ type: 'separator' },
+ // A worktree lands as a group frame bound to it; nodes created inside inherit its path.
+ { label: 'New worktree…', icon: <IconBranch />, onClick: () => openWorktreeDialog(null, at) },
+ { type: 'separator' },
// Canvas actions.
{ label: 'Select all', icon: <IconSelectAll />, onClick: selectAll },
{ label: 'Fit view', icon: <IconFit />, onClick: () => fitView({ padding: 0.2, duration: 300 }) }
@@ -2507,6 +2585,7 @@ export function Canvas() {
addBrowser,
openFileDialog,
openRemotePicker,
+ openWorktreeDialog,
selectAll,
fitView
]
@@ -3728,6 +3807,12 @@ export function Canvas() {
)
})
),
+ {
+ id: 'worktree-new',
+ label: 'New worktree…',
+ icon: <IconBranch />,
+ run: () => openWorktreeDialog(null)
+ },
{ id: 'new-project', label: 'New project', icon: <IconProject />, run: () => addProject() },
{ id: 'clone-repo', label: 'Clone repository…', icon: <IconProject />, run: () => setCloneDialogOpen(true) },
{
@@ -3796,6 +3881,7 @@ export function Canvas() {
addWebView,
addBrowser,
openFileDialog,
+ openWorktreeDialog,
addProject,
fitView,
persist,
@@ -4178,11 +4264,10 @@ export function Canvas() {
/>
)}
- {bindTarget && (
- <BindWorktreeDialog
- initialRepoPath={
- (nodesRef.current.find((n) => n.id === bindTarget)?.data.cwd as string) || ''
- }
+ {worktreeDialog && (
+ <WorktreeDialog
+ repoPath={worktreeRepoRoot ?? ''}
+ existing={worktreeOrphans}
defaultPath={(repoPath, branch) =>
computeWorktreePath(
userDataDirRef.current,
@@ -4190,8 +4275,14 @@ export function Canvas() {
sanitizeWorktreeBranch(branch)
)
}
- onConfirm={confirmBind}
- onCancel={() => setBindTarget(null)}
+ busy={worktreeBusy}
+ error={worktreeError}
+ onCreate={createWorktreeAndGroup}
+ onBindExisting={bindExistingWorktree}
+ onCancel={() => {
+ setWorktreeDialog(null)
+ setWorktreeError(null)
+ }}
/>
)}
diff --git a/src/renderer/components/BindWorktreeDialog.tsx b/src/renderer/components/WorktreeDialog.tsx
similarity index 55%
rename from src/renderer/components/BindWorktreeDialog.tsx
rename to src/renderer/components/WorktreeDialog.tsx
index 908d7debd..7807c470e 100644
--- a/src/renderer/components/BindWorktreeDialog.tsx
+++ b/src/renderer/components/WorktreeDialog.tsx
@@ -1,7 +1,8 @@
import { useEffect, useState } from 'react'
import { createPortal } from 'react-dom'
+import type { WorktreeEntry } from '@shared/worktree'
-export interface BindWorktreeValue {
+export interface WorktreeCreateValue {
repoPath: string
mode: 'new' | 'existing'
branch: string
@@ -10,20 +11,33 @@ export interface BindWorktreeValue {
}
interface Props {
- initialRepoPath: string
- /** Suggests an on-disk worktree path from the repo + branch. */
+ /** Repo root, resolved from the project cwd. Empty only when the project is not a git repo. */
+ repoPath: string
+ /** Worktrees that already exist for this repo, excluding the main checkout. */
+ existing: WorktreeEntry[]
defaultPath: (repoPath: string, branch: string) => string
- onConfirm: (v: BindWorktreeValue) => void
+ busy: boolean
+ error: string | null
+ onCreate: (v: WorktreeCreateValue) => void
+ onBindExisting: (e: WorktreeEntry) => void
onCancel: () => void
}
-/** Modal to bind a group to a git worktree (new or existing branch). Esc cancels. */
-export function BindWorktreeDialog({ initialRepoPath, defaultPath, onConfirm, onCancel }: Props) {
- const [repoPath, setRepoPath] = useState(initialRepoPath)
+/** Create a worktree (and the group frame around it), or bind a group to one that already exists. */
+export function WorktreeDialog({
+ repoPath,
+ existing,
+ defaultPath,
+ busy,
+ error,
+ onCreate,
+ onBindExisting,
+ onCancel
+}: Props) {
const [mode, setMode] = useState<'new' | 'existing'>('new')
const [branch, setBranch] = useState('feature/')
const [baseRef, setBaseRef] = useState('main')
- const [path, setPath] = useState(() => defaultPath(initialRepoPath, 'feature/'))
+ const [path, setPath] = useState(() => defaultPath(repoPath, 'feature/'))
const [pathEdited, setPathEdited] = useState(false)
// Keep the path in sync with the branch until the user edits it by hand.
@@ -42,34 +56,61 @@ export function BindWorktreeDialog({ initialRepoPath, defaultPath, onConfirm, on
return () => window.removeEventListener('keydown', onKey)
}, [onCancel])
- const valid = !!repoPath.trim() && !!branch.trim() && !!path.trim()
+ const valid = !!repoPath.trim() && !!branch.trim() && !!path.trim() && !busy
return createPortal(
<div className="confirm-overlay" onClick={onCancel}>
<div className="confirm bind-dialog" onClick={(e) => e.stopPropagation()}>
- <p className="confirm__msg">Bind group to worktree</p>
- <label className="bind-field">
- Repo
- <input value={repoPath} onChange={(e) => setRepoPath(e.target.value)} />
- </label>
+ <p className="confirm__msg">New worktree</p>
+
+ <div className="bind-repo" title={repoPath}>
+ {repoPath || 'This project is not a git repository.'}
+ </div>
+
+ {existing.length > 0 && (
+ <div className="bind-existing">
+ <div className="bind-existing__title">Existing worktrees</div>
+ {existing.map((e) => (
+ <button
+ key={e.path}
+ className="bind-existing__row"
+ disabled={busy}
+ onClick={() => onBindExisting(e)}
+ title={e.path}
+ >
+ <span className="bind-existing__branch">⎇ {e.branch ?? '(detached)'}</span>
+ <span className="bind-existing__path">{e.path}</span>
+ </button>
+ ))}
+ </div>
+ )}
+
<div className="bind-mode">
<label>
<input type="radio" checked={mode === 'new'} onChange={() => setMode('new')} /> New branch
</label>
<label>
- <input type="radio" checked={mode === 'existing'} onChange={() => setMode('existing')} /> Existing branch
+ <input
+ type="radio"
+ checked={mode === 'existing'}
+ onChange={() => setMode('existing')}
+ />{' '}
+ Existing branch
</label>
</div>
+
<label className="bind-field">
Branch
<input value={branch} onChange={(e) => setBranch(e.target.value)} />
</label>
+
{mode === 'new' && (
<label className="bind-field">
Base
<input value={baseRef} onChange={(e) => setBaseRef(e.target.value)} />
</label>
)}
+
<label className="bind-field">
Worktree path
<input
@@ -80,15 +121,18 @@ export function BindWorktreeDialog({ initialRepoPath, defaultPath, onConfirm, on
}}
/>
</label>
+
+ {error && <div className="bind-error">{error}</div>}
+
<div className="confirm__actions">
- <button className="confirm__btn" onClick={onCancel}>
+ <button className="confirm__btn" onClick={onCancel} disabled={busy}>
Cancel
</button>
<button
className="confirm__btn primary"
disabled={!valid}
onClick={() =>
- onConfirm({
+ onCreate({
repoPath: repoPath.trim(),
mode,
branch: branch.trim(),
@@ -97,7 +141,7 @@ export function BindWorktreeDialog({ initialRepoPath, defaultPath, onConfirm, on
})
}
>
- Bind
+ {busy ? 'Creating…' : 'Create'}
</button>
</div>
</div>
diff --git a/src/renderer/styles.css b/src/renderer/styles.css
index 31a33bba5..6c5490420 100644
--- a/src/renderer/styles.css
+++ b/src/renderer/styles.css
@@ -3056,6 +3056,87 @@ body,
border-color: var(--accent);
}
+/* Repo root, resolved from the project cwd — read-only context, not a field. */
+.bind-repo {
+ margin: 0 0 12px;
+ padding: 6px 8px;
+ border: 1px solid var(--border);
+ border-radius: 6px;
+ background: var(--panel-header);
+ color: var(--muted);
+ font-family: var(--mono, ui-monospace, SFMono-Regular, Menlo, monospace);
+ font-size: 11px;
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+
+.bind-existing {
+ margin: 0 0 12px;
+}
+
+.bind-existing__title {
+ margin: 0 0 6px;
+ font-size: 11px;
+ text-transform: uppercase;
+ letter-spacing: 0.04em;
+ color: var(--muted);
+}
+
+.bind-existing__row {
+ display: flex;
+ align-items: baseline;
+ gap: 8px;
+ width: 100%;
+ padding: 6px 8px;
+ border: 1px solid var(--border);
+ border-radius: 6px;
+ margin-bottom: 4px;
+ background: transparent;
+ color: var(--text);
+ font-size: 12px;
+ text-align: left;
+ cursor: pointer;
+}
+
+.bind-existing__row:hover:not(:disabled) {
+ background: var(--panel-header);
+ border-color: var(--accent);
+}
+
+.bind-existing__row:disabled {
+ opacity: 0.45;
+ cursor: default;
+}
+
+.bind-existing__branch {
+ flex: none;
+}
+
+.bind-existing__path {
+ flex: 1;
+ min-width: 0;
+ color: var(--muted);
+ font-size: 11px;
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+
+/* Inline creation error (same red as .scm-error) — worktree paths never use window.alert. */
+.bind-error {
+ margin: 0 0 12px;
+ padding: 8px 10px;
+ border: 1px solid rgba(255, 69, 58, 0.4);
+ border-radius: 6px;
+ background: rgba(255, 69, 58, 0.12);
+ color: #ff8a80;
+ font-size: 11px;
+ line-height: 1.4;
+ white-space: pre-wrap;
+ word-break: break-word;
+}
+
.bind-mode {
display: flex;
gap: 18px;