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(sidebar): dedup status-mode rows across the project-switch window (split 1/2)

Darren Kattane91c9ebe5659
diff --git a/src/renderer/lib/sessionList.test.ts b/src/renderer/lib/sessionList.test.ts
index a98a8a781..cd0f88e38 100644
--- a/src/renderer/lib/sessionList.test.ts
+++ b/src/renderer/lib/sessionList.test.ts
@@ -484,4 +484,23 @@ describe('buildStatusList', () => {
const idle = sections.find((s) => s.kind === 'idle')!
expect(idle.rows.map((r) => r.id)).toEqual(['i1'])
})
+
+ it('never emits a node twice during a cross-project switch window', () => {
+ // Simulates the duplication bug: `activeProjectId` has already flipped to p2 (the focus
+ // target), but `liveActiveNodes` still holds p1's nodes (React Flow hasn't flushed yet).
+ // The naive "active = liveActiveNodes" read tagged the stale nodes with p2 while p1's
+ // just-committed p.nodes emitted them again — same node under two project tags.
+ const p1Nodes = [node('a1', { title: 'Align to Grid', agentId: 'claude' })]
+ const proj2: ProjectInput[] = [
+ { id: 'p1', name: 'nodeterm', color: '#111', nodes: p1Nodes },
+ { id: 'p2', name: 'ImmyBot', color: '#222', nodes: [node('b1', { title: 'other' })] }
+ ]
+ // activeProjectId is now p2, but liveActiveNodes still carries p1's a1 (stale window).
+ const sections = buildStatusList(proj2, p1Nodes, 'p2', { a1: { unread: false } }, '')
+ const allRows = sections.flatMap((s) => s.rows)
+ // a1 appears exactly once…
+ expect(allRows.filter((r) => r.id === 'a1')).toHaveLength(1)
+ // …owned by its real project (p1), NOT mis-tagged as p2.
+ expect(allRows.find((r) => r.id === 'a1')!.projectId).toBe('p1')
+ })
})
diff --git a/src/renderer/lib/sessionList.ts b/src/renderer/lib/sessionList.ts
index f38cce749..eb7631e99 100644
--- a/src/renderer/lib/sessionList.ts
+++ b/src/renderer/lib/sessionList.ts
@@ -380,20 +380,62 @@ export function buildStatusList(
const needle = filter.trim().toLowerCase()
const keep = (r: SessionRowVM): boolean => !needle || matches(r, needle)
- // Flatten every project's terminal nodes into status-tagged rows. The active project reads its
- // live React Flow nodes (same rule as buildSessionList); the rest read their serialized nodes.
- // Canvas sub-group frames are ignored here — status mode is flat by design. The project index
- // rides alongside (not on the VM) so we can sort by project store-order without a scratch field.
+ // Flatten every project's terminal nodes into status-tagged rows. Canvas sub-group frames are
+ // ignored here — status mode is flat by design. The project index rides alongside (not on the
+ // VM) so we can sort by project store-order without a scratch field.
+ //
+ // OWNERSHIP & DEDUP: a node belongs to the project whose persisted `p.nodes` contains it. The
+ // active project layers its live React Flow nodes (`liveActiveNodes`) on TOP of its persisted
+ // nodes (live wins for up-to-the-frame title/status), but the persisted set is the source of
+ // truth for which nodes are the active project's. This closes a duplication window during a
+ // cross-project focus: focusNodeById → switchProject flips `activeProjectId` synchronously, but
+ // React Flow's nodes (and thus `liveActiveNodes`) still hold the PREVIOUS project's nodes until
+ // the load effect's setNodes flushes on a later render. In that window a naive "active project =
+ // liveActiveNodes" read would tag the stale nodes with the new project's id AND the previous
+ // project's `p.nodes` (just committed) would emit them again — the same node twice, under two
+ // project tags. Keying off the persisted owner map and unioning live nodes only for the active
+ // project means each node id is emitted at most once, owned by its real project, throughout the
+ // switch. (project mode hides the dupe behind collapse, which is why it only surfaced here.)
+ const ownerById = new Map<string, { p: ProjectInput; pidx: number }>()
+ projects.forEach((p, pidx) => {
+ for (const n of p.nodes) ownerById.set(n.id, { p, pidx })
+ })
+
const tagged: { row: SessionRowVM; pidx: number }[] = []
- projects.forEach((p, projectIndex) => {
+ const seen = new Set<string>()
+ // Live title/status overrides for the active project's nodes (newer than the persisted snapshot).
+ const liveById = new Map<string, SessionNodeInput>()
+ if (liveActiveNodes) for (const n of liveActiveNodes) liveById.set(n.id, n)
+
+ projects.forEach((p, pidx) => {
const isActive = p.id === activeProjectId
- const source = isActive && liveActiveNodes ? liveActiveNodes : p.nodes
- for (const n of source) {
+ for (const n of p.nodes) {
if (n.kind !== 'terminal') continue
- const row = toRow(n, statusById[n.id], p)
- if (keep(row)) tagged.push({ row, pidx: projectIndex })
+ // For the active project, prefer the live node (fresh title/agent) when one exists; for the
+ // rest, the persisted node is already current. This mirrors buildSessionList's live-vs-store
+ // choice without ever dropping the persisted set as the ownership key.
+ const node = isActive && liveById.has(n.id) ? liveById.get(n.id)! : n
+ if (seen.has(node.id)) continue
+ seen.add(node.id)
+ const row = toRow(node, statusById[node.id], p)
+ if (keep(row)) tagged.push({ row, pidx })
}
})
+ // A node in `liveActiveNodes` that isn't in any project's persisted set (brand-new, not yet
+ // committed) would be missed above. Emit it under the active project as a fallback — it's live
+ // on the active canvas, so that's the only project it can belong to.
+ if (liveActiveNodes) {
+ const active = projects.find((p) => p.id === activeProjectId)
+ if (active) {
+ const activePidx = projects.indexOf(active)
+ for (const n of liveActiveNodes) {
+ if (n.kind !== 'terminal' || seen.has(n.id) || ownerById.has(n.id)) continue
+ seen.add(n.id)
+ const row = toRow(n, statusById[n.id], active)
+ if (keep(row)) tagged.push({ row, pidx: activePidx })
+ }
+ }
+ }
// Bucket by status, then stable-sort each bucket by project store-order then title.
const byStatus = new Map<StatusKind, { row: SessionRowVM; pidx: number }[]>()