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): pure reconciliation of bindings against git worktree list

enes6c02ab423606
diff --git a/src/shared/worktree-reconcile.test.ts b/src/shared/worktree-reconcile.test.ts
new file mode 100644
index 000000000..03956433f
--- /dev/null
+++ b/src/shared/worktree-reconcile.test.ts
@@ -0,0 +1,62 @@
+import { describe, it, expect } from 'vitest'
+import { reconcileWorktrees, type BoundGroup } from './worktree-reconcile'
+import type { WorktreeEntry } from './worktree'
+
+const wt = (path: string, branch: string | null): WorktreeEntry => ({
+ path,
+ branch,
+ head: 'abc123',
+ isBare: false
+})
+
+const bound = (groupId: string, path: string, branch: string): BoundGroup => ({
+ groupId,
+ worktree: { repoPath: '/repo', branch, baseRef: 'main', path, createdByApp: true }
+})
+
+describe('reconcileWorktrees', () => {
+ it('reports a binding whose worktree still exists as live', () => {
+ const r = reconcileWorktrees([bound('g1', '/wt/feat', 'feat')], [wt('/repo', 'main'), wt('/wt/feat', 'feat')])
+ expect(r.live).toEqual(['g1'])
+ expect(r.stale).toEqual([])
+ })
+
+ it('reports a binding whose worktree is gone from disk as stale', () => {
+ const r = reconcileWorktrees([bound('g1', '/wt/feat', 'feat')], [wt('/repo', 'main')])
+ expect(r.stale).toEqual(['g1'])
+ expect(r.live).toEqual([])
+ })
+
+ it('ignores a trailing slash when matching paths', () => {
+ const r = reconcileWorktrees([bound('g1', '/wt/feat/', 'feat')], [wt('/repo', 'main'), wt('/wt/feat', 'feat')])
+ expect(r.live).toEqual(['g1'])
+ })
+
+ it('reports a worktree on disk that no group is bound to as an orphan', () => {
+ const r = reconcileWorktrees([], [wt('/repo', 'main'), wt('/wt/loose', 'loose')])
+ expect(r.orphans.map((o) => o.path)).toEqual(['/wt/loose'])
+ })
+
+ // The main checkout is itself an entry in `git worktree list`. Offering it as a
+ // recoverable "orphan" would invite the user to delete their own repo.
+ it('never reports the main checkout as an orphan', () => {
+ const r = reconcileWorktrees([], [wt('/repo', 'main')])
+ expect(r.orphans).toEqual([])
+ })
+
+ it('skips bare entries', () => {
+ const entries: WorktreeEntry[] = [{ path: '/repo.git', branch: null, head: null, isBare: true }]
+ const r = reconcileWorktrees([], entries)
+ expect(r.orphans).toEqual([])
+ })
+
+ it('classifies a mixed workspace in one pass', () => {
+ const r = reconcileWorktrees(
+ [bound('g1', '/wt/a', 'a'), bound('g2', '/wt/gone', 'gone')],
+ [wt('/repo', 'main'), wt('/wt/a', 'a'), wt('/wt/loose', 'loose')]
+ )
+ expect(r.live).toEqual(['g1'])
+ expect(r.stale).toEqual(['g2'])
+ expect(r.orphans.map((o) => o.path)).toEqual(['/wt/loose'])
+ })
+})
diff --git a/src/shared/worktree-reconcile.ts b/src/shared/worktree-reconcile.ts
new file mode 100644
index 000000000..c3cb35f10
--- /dev/null
+++ b/src/shared/worktree-reconcile.ts
@@ -0,0 +1,44 @@
+import type { GroupWorktree, WorktreeEntry } from './worktree'
+
+/** A group node that carries a worktree binding. */
+export interface BoundGroup {
+ groupId: string
+ worktree: GroupWorktree
+}
+
+/**
+ * live — group ids whose bound worktree is still registered with git.
+ * stale — group ids whose worktree is gone from disk (deleted outside the app).
+ * orphans — worktrees on disk that no group is bound to (every Unbind makes one).
+ */
+export interface Reconciliation {
+ live: string[]
+ stale: string[]
+ orphans: WorktreeEntry[]
+}
+
+const norm = (p: string): string => p.trim().replace(/\/+$/, '')
+
+/**
+ * Compare the persisted bindings against `git worktree list`. The FIRST entry git prints is the
+ * main checkout — it is never an orphan (offering it as one would invite deleting the repo), and
+ * bare entries are not worktrees a group can own.
+ */
+export function reconcileWorktrees(bound: BoundGroup[], entries: WorktreeEntry[]): Reconciliation {
+ const usable = entries.filter((e) => !e.isBare)
+ const mainCheckout = usable[0]?.path
+ const onDisk = new Set(usable.map((e) => norm(e.path)))
+ const boundPaths = new Set(bound.map((b) => norm(b.worktree.path)))
+
+ const live: string[] = []
+ const stale: string[] = []
+ for (const b of bound) {
+ ;(onDisk.has(norm(b.worktree.path)) ? live : stale).push(b.groupId)
+ }
+
+ const orphans = usable.filter(
+ (e) => norm(e.path) !== norm(mainCheckout ?? '') && !boundPaths.has(norm(e.path))
+ )
+
+ return { live, stale, orphans }
+}