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.

Merge branch 'main' into feat/worktrees-v1

enes5da47f7c1c58
diff --cc src/renderer/bridge/stubs.ts
index 3ee065cd8,6f7c55b79..6cb8ccc65
--- a/src/renderer/bridge/stubs.ts
+++ b/src/renderer/bridge/stubs.ts
@@@ -112,9 -113,7 +113,10 @@@ export function buildStubApi(): Omit
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
+ // Real over the bridge (IPC.appUserDataDir): the worktree dialog's default path is derived from
+ // it, and a '' stub would propose `/worktrees/…` at the filesystem root.
+ | 'userDataDir'
+ | 'presence'
> {
const api = {
ssh: {
@@@ -302,7 -303,7 +305,8 @@@
| 'dialog'
| 'onAgentStatus'
| 'onSubagentActivity'
+ | 'userDataDir'
+ | 'presence'
>
return api
diff --cc src/renderer/canvas/Canvas.tsx
index c84df578d,b5c262deb..40e2b39ea
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@@ -1832,99 -1874,6 +2040,102 @@@ export function Canvas()
})
}, [remoteConnId])
+ /**
+ * Move every node that was living in a worktree directory OFF that (now dead) path — back to the
+ * group's own cwd, else the project's. `displacedByWorktree` decides who those are: descendants
+ * (nested groups included) whose cwd is inside the worktree; anyone else is left alone.
+ *
+ * Leaving a dead `data.cwd` behind is the trap this whole task exists to remove — it is persisted
+ * to project.json, tmux hides it (a warm reattach ignores cwd), and the next machine reboot cold-
+ * starts the terminal into a directory that no longer exists, where pty-manager silently falls
+ * back to $HOME while the dead path stays in the project file forever.
+ *
+ * `respawn` separates the two callers:
+ * - Remove (true): the directory is being deleted under live sessions, so their tmux sessions are
+ * destroyed and the terminals respawn straight into the fallback cwd.
+ * - Stale Unbind (false): unbind touches no process, by definition. The dead path is corrected on
+ * the node (and on disk); the running session keeps going until it is next cold-started, which
+ * is precisely when the corrected cwd is needed.
+ *
+ * Declared HERE (above `deleteNodes`) rather than next to the other worktree code below, because
+ * `releaseWorktreeBinding` — which every binding-dropping path goes through, `deleteNodes`
+ * included — needs it, and a `const` further down would be in its TDZ.
+ */
+ const resetDisplacedCwd = useCallback(
+ (groupId: string, worktreePath: string, respawn: boolean) => {
+ const displaced = displacedByWorktree(nodesRef.current, groupId, worktreePath)
+ if (!displaced.size) return
+ const fallbackCwd =
+ (nodesRef.current.find((n) => n.id === groupId)?.data.cwd as string | undefined) ||
+ useProjects.getState().getProject(activeProjectId ?? '')?.cwd
+ if (respawn) {
+ for (const n of nodesRef.current) {
- if (displaced.has(n.id) && n.type === 'terminal') transport.destroy(n.id)
++ // RECYCLE, not DESTROY: the node is NOT deleted — it stays on the canvas (here and on
++ // every co-viewer's) and respawns into the fallback cwd. `destroy` would cast "closed
++ // by <name>" to co-viewers, permanently bricking their still-present node.
++ if (displaced.has(n.id) && n.type === 'terminal') transport.recycle(n.id)
+ }
+ }
+ setNodes((ns) =>
+ ns.map((n) =>
+ displaced.has(n.id)
+ ? {
+ ...n,
+ data: {
+ ...n.data,
+ cwd: fallbackCwd,
+ ...(respawn && n.type === 'terminal'
+ ? { respawnNonce: ((n.data.respawnNonce as number | undefined) ?? 0) + 1 }
+ : {})
+ }
+ }
+ : n
+ )
+ )
+ markDirty()
+ },
+ [setNodes, markDirty, activeProjectId]
+ )
+
+ /**
+ * Everything a group owes the world when its worktree BINDING is dropped — minus the dropping
+ * itself, which each caller does its own way (clear `data.worktree`, dissolve the frame, delete
+ * the node). THE one place that knowledge lives: every path that can drop a bound group routes
+ * through it, so none of them can quietly skip the two duties below again.
+ *
+ * For a STALE binding (the worktree directory was deleted behind git's back) that means:
+ * a. reset the children's `data.cwd` off the dead path (`resetDisplacedCwd`, no respawn — nothing
+ * here ends a process). Left behind, that dead path is persisted to project.json and tmux
+ * hides it until the next machine reboot cold-starts the terminal into a directory that is not
+ * there; and
+ * b. prune git's stale REGISTRATION, or a later `git worktree add` at the same path fails with
+ * git's raw "missing but already registered worktree". `pruneOnly` guarantees a directory that
+ * still EXISTS is never touched, so a wrongly-stale group can never delete a live checkout.
+ *
+ * A healthy binding owes nothing: the worktree simply becomes an orphan the bind dialog can offer
+ * again (the caller's refresh does that). An SSH project owes nothing either — a legacy binding
+ * there points at a LOCAL path that means nothing on the host, so a local prune and a cwd rewrite
+ * from a local verdict are both lies; plain unbinding is the whole of what we can honestly do.
+ *
+ * The returned promise resolves once the prune (if any) is DONE, so the caller can re-reconcile
+ * after it: a `worktree list` racing an unfinished prune still lists the pruned path, and the
+ * worktree we just cleaned up would pop back as an orphan the dialog offers.
+ */
+ const releaseWorktreeBinding = useCallback(
+ async (groupId: string): Promise<void> => {
+ const wt = nodesRef.current.find((n) => n.id === groupId)?.data.worktree
+ if (!wt || isSshProject) return
+ if (!useWorktrees.getState().staleGroupIds.includes(groupId)) return
+ resetDisplacedCwd(groupId, wt.path, false)
+ // A failed prune must still let the binding go — dropping it is the user's ask, and a
+ // registration we could not clean up is not a reason to trap them in a dead group.
+ await window.nodeTerminal.git
+ .worktreeRemove(wt.repoPath, wt.path, false, true)
+ .catch(() => {})
+ },
+ [isSshProject, resetDisplacedCwd]
+ )
+
// ---- multi-node actions (context menu) ----
const deleteNodes = useCallback(
(ids: string[]) => {
@@@ -2458,49 -2189,17 +2669,55 @@@
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
- // End the old tmux session (recycle, 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.
+ if (!node || !wtPath || node.data.cwd === wtPath) return
+ // A session that runs on another machine must never be moved into a LOCAL worktree: `destroy`
+ // would end its REMOTE tmux session (running processes and all) and respawn it in a directory
+ // that does not exist on that host — pty-manager falls back to the host's $HOME and the dead
+ // path is persisted to project.json. Worktrees are local-only in v1; say so instead of failing
+ // silently (the confirm closing with nothing happening reads as a bug).
+ //
+ // The question is the PROJECT's (does its git — and its tmux — run over ssh?) and the NODE's
+ // (`isRemoteSessionNode`: relay `data.remote` OR an SSH project's `data.ssh`/`data.sshRemoteTmux`).
+ // Guarding `data.remote` alone asked only about relay nodes, which cannot occur in an SSH project
+ // at all — so the one node kind this exists to protect walked straight through it.
+ if (isSshProject || isRemoteSessionNode(node.data)) {
+ setNotice({ kind: 'error', text: WORKTREE_SSH_NOTICE })
+ return
+ }
+ // Re-check at confirm time: the directory can vanish (or the group go stale) while the dialog
+ // is open. `cwdForNewNodeIn` returns the worktree path only for a HEALTHY binding.
+ if (cwdForNewNodeIn(node.parentId) !== wtPath) {
+ setNotice({
+ kind: 'error',
+ text: 'That worktree directory is missing. The terminal was left where it is.'
+ })
+ return
+ }
+ // …and staleness itself only ever arrives by POLL (a 4 s poke against a 4 s throttle, twice
+ // over — so ↪ can still be live ~8-16 s after an external `rm -rf`). Everywhere else that window
+ // is cosmetic; HERE it costs the user a running process. So probe the directory once, right
+ // before the irreversible step. This is the second (and last) sanctioned direct git read outside
+ // the worktrees store — cheap, one-shot, and only on an explicit destructive confirm.
+ const probe = await window.nodeTerminal.git.status(wtPath).catch(() => null)
+ if (!probe?.hasRepo) {
+ setNotice({
+ kind: 'error',
+ text: 'That worktree directory is missing. The terminal was left where it is.'
+ })
+ // Nudge the store (throttled, best effort) so the chip catches up with what we just saw.
+ void useWorktrees.getState().refreshStatus(wtPath, node.parentId)
+ 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)
++ // End the old tmux session 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.
+ //
+ // RECYCLE, not DESTROY: the tmux kill is the same, but this is not a deletion — the node stays
+ // on the canvas (here and on every co-viewer's) and keeps working. `destroy` would tell every
+ // co-viewer "closed by <name>", which is permanent and un-respawnable: their still-present node
+ // would be bricked until they deleted and re-added it. `recycle` tells them to restart onto the
+ // replacement session instead, so they follow the node into its new cwd.
+ transport.recycle(id)
setNodes((ns) =>
ns.map((n) =>
n.id === id