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(worktree): refresh the store after a mutation; dialog reflects its entry point

enes0747f1a047a8
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index c61d0f098..8bcfcae48 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 { WorktreeDialog, type WorktreeCreateValue } from '../components/WorktreeDialog'
+import { WorktreeDialog } from '../components/WorktreeDialog'
import { NotifyConsentDialog } from '../components/NotifyConsentDialog'
import { ExplorerPanel } from '../components/ExplorerPanel'
import { SessionsSidebar } from '../components/SessionsSidebar'
@@ -93,10 +93,15 @@ import { LoopNode } from '../nodes/LoopNode'
import type { NormalizedAgentEvent } from '@shared/agents/normalize'
import {
computeWorktreePath,
+ resolveBaseRef,
sanitizeWorktreeBranch,
+ worktreeFromCreate,
+ worktreeFromEntry,
type GroupWorktree,
+ type WorktreeCreateValue,
type WorktreeEntry
} from '@shared/worktree'
+import { normWorktreePath, type BoundGroup } from '@shared/worktree-reconcile'
import { useWorktrees } from '../state/worktrees'
import {
agentConfig,
@@ -365,13 +370,27 @@ export function Canvas() {
// (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('')
+ // git's order — entries[0] is the repo's main checkout, i.e. the real default branch.
+ const worktreeEntries = useWorktrees((s) => s.entries)
+ // Writable base dir for the default worktree path (userData on desktop, the server's data dir
+ // in the browser), fetched once on mount. STATE, not a ref: a dialog opened before the promise
+ // resolves must re-render with the real base, or it would keep suggesting nothing.
+ const [userDataDir, setUserDataDir] = useState('')
useEffect(() => {
- void window.nodeTerminal.userDataDir().then((d) => {
- userDataDirRef.current = d
- })
+ void window.nodeTerminal.userDataDir().then(setUserDataDir)
}, [])
+ // Worktrees already bound to a group on THIS canvas. The store's orphan list is refreshed after
+ // every mutation, but it is also filled asynchronously — filtering against the live nodes is the
+ // guard that stops the dialog from offering a worktree a second group could bind to.
+ const boundWorktreePaths = useMemo(
+ () =>
+ new Set(
+ nodes
+ .filter((n) => n.type === 'group' && n.data.worktree)
+ .map((n) => normWorktreePath((n.data.worktree as GroupWorktree).path))
+ ),
+ [nodes]
+ )
// Clipboard failures reach us as a window event (the bridge stub has no React handle).
useEffect(() => {
const onToast = (e: Event): void => {
@@ -1885,28 +1904,49 @@ export function Canvas() {
[]
)
+ // Re-read git's facts after a mutation, so the store never lies: an adopted worktree must leave
+ // the orphan list (or the dialog would offer it again and a SECOND group could bind the same
+ // path) and a created one must enter `entries`. `extra` is the binding we just made — React's
+ // setNodes has not committed yet, so it is merged in by hand. Fire-and-forget: `refresh` is
+ // epoch-guarded and fails open.
+ const refreshWorktreeStore = useCallback(
+ (extra?: BoundGroup) => {
+ const project = useProjects.getState().getProject(activeProjectId ?? '')
+ if (!project?.cwd || project.ssh) return
+ const bound: BoundGroup[] = nodesRef.current
+ .filter((n) => n.type === 'group' && n.data.worktree && n.id !== extra?.groupId)
+ .map((n) => ({ groupId: n.id, worktree: n.data.worktree as GroupWorktree }))
+ if (extra) bound.push(extra)
+ void useWorktrees.getState().refresh(project.cwd, bound)
+ },
+ [activeProjectId]
+ )
+
// 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) {
+ let groupId = target.groupId
+ if (groupId) {
setNodes((ns) =>
- ns.map((n) =>
- n.id === target.groupId ? { ...n, data: { ...n.data, worktree: wt } } : n
- )
+ ns.map((n) => (n.id === 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[])]
- })
+ const group = createGroupNode(
+ target.at ?? viewCenter() ?? { x: 0, y: 0 },
+ undefined,
+ nodesRef.current.length
+ )
+ group.data = { ...group.data, title: wt.branch, worktree: wt }
+ groupId = group.id
+ // Parents must come first — React Flow requires a group before its children.
+ setNodes((ns) => [group, ...(ns as CanvasNode[])])
}
markDirty()
+ refreshWorktreeStore({ groupId, worktree: wt })
},
- [setNodes, markDirty, viewCenter]
+ [setNodes, markDirty, viewCenter, refreshWorktreeStore]
)
const createWorktreeAndGroup = useCallback(
@@ -1927,14 +1967,8 @@ export function Canvas() {
setWorktreeError(res.message) // inline, never window.alert
return
}
- 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
- })
+ // We created this directory, so `createdByApp` is true — Remove may delete it.
+ attachWorktree(target, worktreeFromCreate(v))
setWorktreeDialog(null)
},
[attachWorktree, worktreeDialog]
@@ -1943,16 +1977,14 @@ export function Canvas() {
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
- })
+ const { repoRoot, entries } = useWorktrees.getState()
+ if (!target || !repoRoot) return
+ // The user (or a previous Unbind) made this one — `createdByApp` is false, so Remove must
+ // not delete it by default. The base ref is the MAIN checkout's branch, not a hardcoded
+ // 'main' (a master/trunk repo would later merge at a ref that does not exist).
+ const wt = worktreeFromEntry(e, repoRoot, resolveBaseRef(entries))
+ if (!wt) return
+ attachWorktree(target, wt)
setWorktreeDialog(null)
},
[attachWorktree, worktreeDialog]
@@ -4266,11 +4298,15 @@ export function Canvas() {
{worktreeDialog && (
<WorktreeDialog
+ // Opened from a group's "Bind to worktree…" (groupId set) vs. the pane/palette's
+ // "New worktree…" — the header and the primary button say which.
+ intent={worktreeDialog.groupId ? 'bind' : 'create'}
repoPath={worktreeRepoRoot ?? ''}
- existing={worktreeOrphans}
+ existing={worktreeOrphans.filter((e) => !boundWorktreePaths.has(normWorktreePath(e.path)))}
+ defaultBaseRef={resolveBaseRef(worktreeEntries)}
defaultPath={(repoPath, branch) =>
computeWorktreePath(
- userDataDirRef.current,
+ userDataDir,
repoPath.split('/').pop() || 'repo',
sanitizeWorktreeBranch(branch)
)
diff --git a/src/renderer/components/WorktreeDialog.tsx b/src/renderer/components/WorktreeDialog.tsx
index 7807c470e..f2ef97393 100644
--- a/src/renderer/components/WorktreeDialog.tsx
+++ b/src/renderer/components/WorktreeDialog.tsx
@@ -1,20 +1,18 @@
import { useEffect, useState } from 'react'
import { createPortal } from 'react-dom'
-import type { WorktreeEntry } from '@shared/worktree'
-
-export interface WorktreeCreateValue {
- repoPath: string
- mode: 'new' | 'existing'
- branch: string
- baseRef: string
- path: string
-}
+import type { WorktreeCreateValue, WorktreeEntry } from '@shared/worktree'
interface Props {
+ /** 'create' = the pane/palette entry point (a new group frame); 'bind' = an existing group's
+ * "Bind to worktree…". Only the wording differs — both can create or adopt a worktree. */
+ intent: 'create' | 'bind'
/** 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. */
+ /** Worktrees that already exist for this repo, excluding the main checkout and bound ones. */
existing: WorktreeEntry[]
+ /** The repo's default branch (the main checkout's), used as the Base default. */
+ defaultBaseRef: string
+ /** Suggested worktree path. Returns '' when no writable base dir is known — see `pathUnknown`. */
defaultPath: (repoPath: string, branch: string) => string
busy: boolean
error: string | null
@@ -25,8 +23,10 @@ interface Props {
/** Create a worktree (and the group frame around it), or bind a group to one that already exists. */
export function WorktreeDialog({
+ intent,
repoPath,
existing,
+ defaultBaseRef,
defaultPath,
busy,
error,
@@ -36,7 +36,7 @@ export function WorktreeDialog({
}: Props) {
const [mode, setMode] = useState<'new' | 'existing'>('new')
const [branch, setBranch] = useState('feature/')
- const [baseRef, setBaseRef] = useState('main')
+ const [baseRef, setBaseRef] = useState(defaultBaseRef)
const [path, setPath] = useState(() => defaultPath(repoPath, 'feature/'))
const [pathEdited, setPathEdited] = useState(false)
@@ -45,6 +45,13 @@ export function WorktreeDialog({
if (!pathEdited) setPath(defaultPath(repoPath, branch || 'work'))
}, [repoPath, branch, pathEdited, defaultPath])
+ // The repo's default branch resolves asynchronously (the store fills after the first render);
+ // adopt it as long as the user has not typed a base of their own.
+ const [baseEdited, setBaseEdited] = useState(false)
+ useEffect(() => {
+ if (!baseEdited) setBaseRef(defaultBaseRef)
+ }, [defaultBaseRef, baseEdited])
+
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
@@ -56,12 +63,17 @@ export function WorktreeDialog({
return () => window.removeEventListener('keydown', onKey)
}, [onCancel])
+ // No writable base dir is known, so we refuse to *suggest* a path — an empty base would
+ // otherwise propose `/worktrees/…` at the filesystem root. The user can still type one.
+ const pathUnknown = !pathEdited && !path.trim()
const valid = !!repoPath.trim() && !!branch.trim() && !!path.trim() && !busy
+ const title = intent === 'bind' ? 'Bind to worktree' : 'New worktree'
+ const createLabel = intent === 'bind' ? 'Create & bind' : 'Create'
return createPortal(
<div className="confirm-overlay" onClick={onCancel}>
<div className="confirm bind-dialog" onClick={(e) => e.stopPropagation()}>
- <p className="confirm__msg">New worktree</p>
+ <p className="confirm__msg">{title}</p>
<div className="bind-repo" title={repoPath}>
{repoPath || 'This project is not a git repository.'}
@@ -107,7 +119,13 @@ export function WorktreeDialog({
{mode === 'new' && (
<label className="bind-field">
Base
- <input value={baseRef} onChange={(e) => setBaseRef(e.target.value)} />
+ <input
+ value={baseRef}
+ onChange={(e) => {
+ setBaseRef(e.target.value)
+ setBaseEdited(true)
+ }}
+ />
</label>
)}
@@ -122,6 +140,11 @@ export function WorktreeDialog({
/>
</label>
+ {pathUnknown && (
+ <div className="bind-error">
+ No default worktree location is available. Enter a full path to create one.
+ </div>
+ )}
{error && <div className="bind-error">{error}</div>}
<div className="confirm__actions">
@@ -141,7 +164,7 @@ export function WorktreeDialog({
})
}
>
- {busy ? 'Creating…' : 'Create'}
+ {busy ? 'Creating…' : createLabel}
</button>
</div>
</div>