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): a strike streak dies with its binding, and never with a cross-repo one

enes4e02e60c78d4
diff --git a/src/renderer/state/worktrees.test.ts b/src/renderer/state/worktrees.test.ts
index a365da68d..be42641f8 100644
--- a/src/renderer/state/worktrees.test.ts
+++ b/src/renderer/state/worktrees.test.ts
@@ -353,6 +353,60 @@ describe('useWorktrees.refreshStatus', () => {
expect(useWorktrees.getState().staleGroupIds).toEqual([])
})
+ // The miss-streak is keyed by PATH and `computeWorktreePath` is deterministic, so a streak that
+ // survives its binding lands on the NEXT worktree at the same path: unbind a struck-out group,
+ // create a worktree for the same branch again, and the brand-new healthy group would render
+ // "· missing" (Merge/Remove/↪ hidden) until the next poll.
+ it('forgets a path´s strike streak once nothing is bound to it any more (unbind → rebind is healthy)', async () => {
+ const t0 = 1_700_000_000_000
+ vi.useFakeTimers()
+ vi.setSystemTime(t0)
+ gitMock.repoRoot.mockResolvedValue('/repo')
+ gitMock.status.mockResolvedValue(okStatus({ hasRepo: false, branch: '' }))
+ await useWorktrees.getState().refreshStatus('/wt/feat', 'group-1')
+ vi.setSystemTime(t0 + WORKTREE_STATUS_THROTTLE_MS + 1)
+ await useWorktrees.getState().refreshStatus('/wt/feat', 'group-1')
+ expect(useWorktrees.getState().staleGroupIds).toEqual(['group-1'])
+
+ // Unbind: the group is gone from `bound`, and the dead worktree's registration was pruned.
+ gitMock.worktreeList.mockResolvedValue([{ path: '/repo', branch: 'main', head: 'a', isBare: false }])
+ await useWorktrees.getState().refresh('/repo', [])
+ expect(useWorktrees.getState().staleGroupIds).toEqual([])
+
+ // The user creates a worktree for the same branch → git hands out the SAME path → a NEW group.
+ gitMock.worktreeList.mockResolvedValue([
+ { path: '/repo', branch: 'main', head: 'a', isBare: false },
+ { path: '/wt/feat', branch: 'feat', head: 'b', isBare: false }
+ ])
+ await useWorktrees.getState().refresh('/repo', [
+ { groupId: 'group-2', worktree: { path: '/wt/feat', repoPath: '/repo' } as never }
+ ])
+
+ expect(useWorktrees.getState().staleGroupIds).toEqual([])
+ })
+
+ // A cross-repo binding is excluded from RECONCILIATION on purpose (it would be compared against
+ // the wrong entry list), but its strike streak is a fact about its own path — dropping it on
+ // every refresh made a dead cross-repo worktree look healthy again until the next poll tick.
+ it('keeps a cross-repo binding´s strike staleness across a refresh', async () => {
+ const t0 = 1_700_000_000_000
+ vi.useFakeTimers()
+ vi.setSystemTime(t0)
+ gitMock.status.mockResolvedValue(okStatus({ hasRepo: false, branch: '' }))
+ await useWorktrees.getState().refreshStatus('/other-wt/feat', 'other')
+ vi.setSystemTime(t0 + WORKTREE_STATUS_THROTTLE_MS + 1)
+ await useWorktrees.getState().refreshStatus('/other-wt/feat', 'other')
+ expect(useWorktrees.getState().staleGroupIds).toEqual(['other'])
+
+ gitMock.repoRoot.mockResolvedValue('/repo')
+ gitMock.worktreeList.mockResolvedValue([{ path: '/repo', branch: 'main', head: 'a', isBare: false }])
+ await useWorktrees.getState().refresh('/repo', [
+ { groupId: 'other', worktree: { path: '/other-wt/feat', repoPath: '/other-repo' } as never }
+ ])
+
+ expect(useWorktrees.getState().staleGroupIds).toEqual(['other'])
+ })
+
it('leaves staleness alone when no group id is given', async () => {
gitMock.status.mockResolvedValue(okStatus({ hasRepo: false, branch: '' }))
diff --git a/src/renderer/state/worktrees.ts b/src/renderer/state/worktrees.ts
index 31a6d27d8..25c8db875 100644
--- a/src/renderer/state/worktrees.ts
+++ b/src/renderer/state/worktrees.ts
@@ -105,6 +105,16 @@ export const useWorktrees = create<WorktreesState>((set) => ({
// `entries` stays in git's order — reconcileWorktrees identifies the main checkout positionally.
const entries = await git.worktreeList(root)
if (mineEpoch !== epoch) return
+ // A miss-streak is a fact about a BINDING, not about a path forever. It is keyed by path and
+ // `computeWorktreePath` is deterministic, so a streak that outlives its binding comes back to
+ // haunt the next one: delete a worktree dir → the group strikes out → Unbind (which prunes)
+ // → create a worktree for the same branch → the SAME path → the union below drags the dead
+ // streak onto a brand-new, healthy group, which renders "· missing" (Merge/Remove/↪ hidden)
+ // until the next successful poll. So the moment a path is no longer bound to any group
+ // (unbind / remove / ungroup / delete — all of which re-refresh), forget its streak.
+ // Only BOUND paths are ever consulted below, so nothing else can depend on it.
+ const boundPaths = new Set(bound.map((b) => normWorktreePath(b.worktree.path)))
+ for (const key of [...missStreak.keys()]) if (!boundPaths.has(key)) missStreak.delete(key)
// Reconcile only the groups bound to THIS repo. A group bound to another repo's worktree
// (legacy binding, or hand-typed) would otherwise be compared against the wrong entry list
// and falsely reported stale.
@@ -118,7 +128,11 @@ export const useWorktrees = create<WorktreesState>((set) => ({
// to reconcile's answer alone would ERASE, on any of those, a group the poll had already
// proven dead: the chip goes healthy again and "↪ Move into worktree" reopens the very window
// in which it kills a live session into a directory that no longer exists.
- const struck = mine.filter((b) => struckOut(b.worktree.path)).map((b) => b.groupId)
+ // Struck-out over ALL bindings, not just `mine`: a group bound to ANOTHER repo (a legacy or
+ // hand-edited binding) is excluded from reconciliation by design, but its miss-streak is
+ // still a fact about its own path. Filtering it out here dropped it from `staleGroupIds` on
+ // every refresh, so a dead cross-repo worktree looked healthy again until the next poll tick.
+ const struck = bound.filter((b) => struckOut(b.worktree.path)).map((b) => b.groupId)
const staleIds = [...new Set([...stale, ...struck])]
set({ repoRoot: root, entries, orphans, staleGroupIds: staleIds })
} catch {