diff --git a/src/main/ipc/worktrees.ts b/src/main/ipc/worktrees.ts index c7225107d..3b23dbcdf 100644 --- a/src/main/ipc/worktrees.ts +++ b/src/main/ipc/worktrees.ts @@ -174,8 +174,12 @@ export function registerWorktreeHandlers(mainWindow: BrowserWindow, store: Store 'worktrees:updateMeta', (_event, args: { worktreeId: string; updates: Partial }) => { const meta = store.setWorktreeMeta(args.worktreeId, args.updates) - const { repoId } = parseWorktreeId(args.worktreeId) - notifyWorktreesChanged(mainWindow, repoId) + // Do NOT call notifyWorktreesChanged here. The renderer applies meta + // updates optimistically before calling this IPC, so a notification + // would trigger a redundant fetchWorktrees round-trip that bumps + // sortEpoch and reorders the sidebar — the exact bug PR #209 tried + // to fix (clicking a card would clear isUnread → updateMeta → + // worktrees:changed → fetchWorktrees → sortEpoch++ → re-sort). return meta } ) diff --git a/src/renderer/src/store/slices/terminals.ts b/src/renderer/src/store/slices/terminals.ts index 130b92f29..f65ee46ae 100644 --- a/src/renderer/src/store/slices/terminals.ts +++ b/src/renderer/src/store/slices/terminals.ts @@ -151,6 +151,7 @@ export const createTerminalSlice: StateCreator updateTabTitle: (tabId, title) => { set((s) => { let changed = false + let ownerWorktreeId: string | null = null const next = { ...s.tabsByWorktree } for (const wId of Object.keys(next)) { next[wId] = next[wId].map((t) => { @@ -158,12 +159,24 @@ export const createTerminalSlice: StateCreator return t } changed = true + ownerWorktreeId = wId return { ...t, title } }) } + if (!changed) { + return s + } // Agent status is derived from terminal titles and affects sort scoring, - // so a title change is a meaningful event that should allow re-sort. - return changed ? { tabsByWorktree: next, sortEpoch: s.sortEpoch + 1 } : s + // so a title change is a meaningful event that should allow re-sort — + // but only for background worktrees. Title changes in the active + // worktree are side-effects of PTY reconnection during worktree + // activation (generation bump → TerminalPane remount → new shell → + // title update). Bumping sortEpoch here would reorder the sidebar + // on click — the exact bug PR #209 intended to fix. + const isActive = ownerWorktreeId === s.activeWorktreeId + return isActive + ? { tabsByWorktree: next } + : { tabsByWorktree: next, sortEpoch: s.sortEpoch + 1 } }) }, diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index 7be16e19e..b456d2b0e 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -194,11 +194,21 @@ export const createWorktreeSlice: StateCreator if (!worktree) { return {} } + // Skip sortEpoch bump for the active worktree. Terminal events + // (PTY spawn, PTY exit) in the active worktree are side-effects of + // the user clicking the card or interacting with the terminal — + // re-sorting the sidebar in response would cause the exact reorder- + // on-click bug PR #209 intended to fix (e.g. dead-PTY reconnection + // after generation bump triggers updateTabPtyId → here). + // The lastActivityAt timestamp is still persisted so that the NEXT + // meaningful sortEpoch bump (from a background worktree event) will + // include this worktree's updated score. + const isActive = s.activeWorktreeId === worktreeId return { worktreesByRepo: applyWorktreeUpdates(s.worktreesByRepo, worktreeId, { lastActivityAt: now }), - sortEpoch: s.sortEpoch + 1 + ...(isActive ? {} : { sortEpoch: s.sortEpoch + 1 }) } })