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): displacedByWorktree sweeps editor/diff nodes too

enes8ada907ea231
diff --git a/src/shared/worktree.test.ts b/src/shared/worktree.test.ts
index c93db69f5..42db22d88 100644
--- a/src/shared/worktree.test.ts
+++ b/src/shared/worktree.test.ts
@@ -275,20 +275,53 @@ describe('displacedByWorktree', () => {
{ id: 'nocwd', type: 'terminal', parentId: 'g', data: {} },
{ id: 'sticky', type: 'sticky', parentId: 'g', data: { cwd: wt } },
// Same cwd, but not in the group: not this group's business.
- { id: 'stranger', type: 'terminal', data: { cwd: wt } }
+ { id: 'stranger', type: 'terminal', data: { cwd: wt } },
+ // Editor/diff nodes never get a parentId (they float free on the canvas — see
+ // createEditorNode/createDiffNode), so group membership can't identify them. Path
+ // containment is the only signal: editor stores the ABSOLUTE path in `filePath`; diff
+ // stores the repo root in `cwd` and the file's path RELATIVE to it in `filePath`.
+ { id: 'editor-in', type: 'editor', data: { filePath: `${wt}/src/index.ts` } },
+ { id: 'editor-out', type: 'editor', data: { filePath: '/repo/src/index.ts' } },
+ // Shares the prefix but is a sibling directory, not really inside the worktree.
+ { id: 'editor-prefix', type: 'editor', data: { filePath: '/wt/feature/index.ts' } },
+ { id: 'diff-in', type: 'diff', data: { cwd: wt, filePath: 'src/index.ts' } },
+ { id: 'diff-out', type: 'diff', data: { cwd: '/repo', filePath: 'src/index.ts' } }
]
it('collects every descendant terminal and chat living in the worktree, nested ones included', () => {
- expect([...displacedByWorktree(nodes, 'g', wt)].sort()).toEqual(['chat', 'nested', 'term'])
+ expect([...displacedByWorktree(nodes, 'g', wt)].sort()).toEqual(
+ ['chat', 'diff-in', 'editor-in', 'nested', 'term'].sort()
+ )
})
it('leaves nodes outside the group, outside the path, or without a cwd alone', () => {
const got = displacedByWorktree(nodes, 'g', wt)
- for (const id of ['elsewhere', 'prefix', 'nocwd', 'sticky', 'stranger', 'g', 'inner']) {
+ for (const id of [
+ 'elsewhere',
+ 'prefix',
+ 'nocwd',
+ 'sticky',
+ 'stranger',
+ 'g',
+ 'inner',
+ 'editor-out',
+ 'editor-prefix',
+ 'diff-out'
+ ]) {
expect(got.has(id)).toBe(false)
}
})
+ it('collects an editor/diff node by path even though it has no parentId at all', () => {
+ // Same as 'editor-in'/'diff-in' above, spelled out: neither carries a parentId, and neither
+ // is a descendant of 'g' by any group-membership test — only the path says they're displaced.
+ expect(nodes.find((n) => n.id === 'editor-in')?.parentId).toBeUndefined()
+ expect(nodes.find((n) => n.id === 'diff-in')?.parentId).toBeUndefined()
+ const got = displacedByWorktree(nodes, 'g', wt)
+ expect(got.has('editor-in')).toBe(true)
+ expect(got.has('diff-in')).toBe(true)
+ })
+
it('displaces nothing for an empty worktree path (never sweeps the whole canvas)', () => {
expect(displacedByWorktree(nodes, 'g', '').size).toBe(0)
})
diff --git a/src/shared/worktree.ts b/src/shared/worktree.ts
index 6136305b3..94bedb404 100644
--- a/src/shared/worktree.ts
+++ b/src/shared/worktree.ts
@@ -230,12 +230,13 @@ interface WorktreeNodeLike {
id: string
type?: string
parentId?: string
- data?: { cwd?: unknown }
+ data?: { cwd?: unknown; filePath?: unknown }
}
/**
* The nodes a worktree teardown DISPLACES: every descendant of the bound group that carries a
- * working directory (a terminal or a chat) inside the worktree path.
+ * working directory (a terminal or a chat) inside the worktree path, PLUS any editor/diff node
+ * anywhere on the canvas whose file lives inside it.
*
* BOTH teardown paths derive from this — Remove (which also ends their sessions and respawns them)
* and a stale group's Unbind (which touches no process at all). Unbind is the documented recovery
@@ -248,6 +249,15 @@ interface WorktreeNodeLike {
* Nodes whose cwd was never inside the worktree (pointed elsewhere by hand, a sibling directory
* that merely shares the prefix, no cwd at all) are NOT displaced — they were never affected, and
* rewriting them would be a change the user never asked for.
+ *
+ * Terminal/chat displacement is GROUP-scoped (`under.has`) because a cwd match alone is too broad —
+ * plenty of terminals legitimately share a cwd with a worktree without living inside its frame.
+ * Editor/diff nodes get no such scoping: `createEditorNode`/`createDiffNode` never set a
+ * `parentId` (they float free on the canvas, `group: null`), so they are never a "descendant" of
+ * anything — path containment is the only signal there is. A node that opened a file out of a
+ * worktree is displaced by that worktree going away no matter where it happens to sit visually.
+ * Editor stores the file's ABSOLUTE path in `filePath`; diff stores the repo root in `cwd` and the
+ * file's path RELATIVE to it in `filePath`, so the two are joined before the containment check.
*/
export function displacedByWorktree(
nodes: readonly WorktreeNodeLike[],
@@ -258,10 +268,16 @@ export function displacedByWorktree(
const under = descendantIds(nodes, groupId)
const out = new Set<string>()
for (const n of nodes) {
- if (!under.has(n.id)) continue
- if (n.type !== 'terminal' && n.type !== 'chat') continue
- const cwd = typeof n.data?.cwd === 'string' ? n.data.cwd : undefined
- if (isInsideDir(cwd, worktreePath)) out.add(n.id)
+ if (n.type === 'terminal' || n.type === 'chat') {
+ if (!under.has(n.id)) continue
+ const cwd = typeof n.data?.cwd === 'string' ? n.data.cwd : undefined
+ if (isInsideDir(cwd, worktreePath)) out.add(n.id)
+ } else if (n.type === 'editor' || n.type === 'diff') {
+ const filePath = typeof n.data?.filePath === 'string' ? n.data.filePath : undefined
+ const cwd = typeof n.data?.cwd === 'string' ? n.data.cwd : undefined
+ const abs = n.type === 'diff' && cwd && filePath ? `${cwd}/${filePath}` : filePath
+ if (isInsideDir(abs, worktreePath)) out.add(n.id)
+ }
}
return out
}