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(renderer): resolve session rows to titles, projects and orphans

enes9e2ba9be3d2a
diff --git a/src/renderer/lib/sessionMemoryRows.test.ts b/src/renderer/lib/sessionMemoryRows.test.ts
new file mode 100644
index 000000000..2f2edaef7
--- /dev/null
+++ b/src/renderer/lib/sessionMemoryRows.test.ts
@@ -0,0 +1,79 @@
+import { describe, it, expect } from 'vitest'
+import { resolveSessionRows, totalMb } from './sessionMemoryRows'
+import type { SessionMemoryRow } from '@shared/types'
+
+const row = (nodeId: string, mb: number): SessionMemoryRow => ({
+ session: `nt-${nodeId}`,
+ nodeId,
+ panePid: 1,
+ selfMb: mb,
+ childrenMb: 0,
+ childCount: 0,
+ totalMb: mb,
+ command: 'claude'
+})
+
+const project = (id: string, name: string, nodeIds: string[], closed = false): any => ({
+ id,
+ name,
+ color: '#fff',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ closed,
+ nodes: nodeIds.map((n) => ({ id: n, kind: 'terminal', title: `T ${n}`, position: { x: 0, y: 0 } }))
+})
+
+describe('resolveSessionRows', () => {
+ it('resolves a node title and project name from the OPEN project', () => {
+ const [v] = resolveSessionRows([row('a', 100)], [project('p1', 'Web', ['a'])], { a: 'working' })
+ expect(v).toMatchObject({ title: 'T a', projectId: 'p1', projectName: 'Web', orphan: false, state: 'working' })
+ })
+
+ it('resolves across ALL projects, not just the active one', () => {
+ const [v] = resolveSessionRows(
+ [row('b', 100)],
+ [project('p1', 'Web', ['a']), project('p2', 'API', ['b'])],
+ {}
+ )
+ expect(v).toMatchObject({ projectId: 'p2', projectName: 'API', orphan: false })
+ })
+
+ it('a CLOSED project is not an orphan — its nodes are still on disk', () => {
+ // closeProject keeps the project and its nodes; calling these orphans would invite the user to
+ // kill sessions they deliberately parked.
+ const [v] = resolveSessionRows([row('c', 100)], [project('p3', 'Old', ['c'], true)], {})
+ expect(v.orphan).toBe(false)
+ expect(v.projectName).toBe('Old')
+ })
+
+ it('marks a session with no node on any canvas as an orphan with no state', () => {
+ const [v] = resolveSessionRows([row('ghost', 100)], [project('p1', 'Web', ['a'])], {
+ ghost: 'working'
+ })
+ expect(v).toMatchObject({ orphan: true, projectId: null, projectName: null, state: null })
+ expect(v.title).toContain('ghost')
+ })
+
+ it('preserves core ordering (already sorted by size)', () => {
+ const vs = resolveSessionRows([row('big', 900), row('small', 10)], [], {})
+ expect(vs.map((v) => v.row.nodeId)).toEqual(['big', 'small'])
+ })
+})
+
+describe('totalMb', () => {
+ it('sums every row', () => {
+ expect(totalMb(resolveSessionRows([row('a', 100), row('b', 250)], [], {}))).toBe(350)
+ })
+
+ it('sums the row TOTAL, not just the pane process', () => {
+ // childCount counts every descendant of the pane — the agent CLI plus anything it spawned —
+ // and their memory lives in childrenMb, so a header that summed selfMb would under-report by
+ // most of the session. The other fixtures have no children, so only this one pins the field.
+ const withChildren: SessionMemoryRow = {
+ ...row('a', 100),
+ childrenMb: 400,
+ childCount: 3,
+ totalMb: 500
+ }
+ expect(totalMb(resolveSessionRows([withChildren], [], {}))).toBe(500)
+ })
+})
diff --git a/src/renderer/lib/sessionMemoryRows.ts b/src/renderer/lib/sessionMemoryRows.ts
new file mode 100644
index 000000000..b5a6a815c
--- /dev/null
+++ b/src/renderer/lib/sessionMemoryRows.ts
@@ -0,0 +1,76 @@
+// Turn core's session-memory rows into rows the panel can render: whose node is this, which
+// project does it belong to, and is there a node at all?
+//
+// Pure and store-free (the caller passes the projects and the agent states), so the whole
+// orphan/closed-project policy is unit-testable without mounting anything.
+
+import type { AgentState } from '@shared/agents/normalize'
+import type { Project, SessionMemoryRow } from '@shared/types'
+
+export interface SessionMemoryView {
+ row: SessionMemoryRow
+ /** The node's title, or the session name when there is no node to name it. */
+ title: string
+ projectId: string | null
+ projectName: string | null
+ orphan: boolean
+ /** Agent state for the dot. Always null for an orphan — see below. */
+ state: AgentState | null
+}
+
+/**
+ * Resolve each row against EVERY project, not just the active one.
+ *
+ * A CLOSED project is deliberately included: `closeProject` only sets `closed = true` and keeps
+ * the project and its nodes on disk, so its sessions resolve to a real title. Treating them as
+ * orphans would invite the user to kill sessions they deliberately parked.
+ *
+ * Order is preserved — core already sorted by `totalMb` descending, and re-sorting here would let
+ * the panel and the report disagree.
+ */
+export function resolveSessionRows(
+ rows: readonly SessionMemoryRow[],
+ projects: readonly Project[],
+ states: Readonly<Record<string, AgentState | undefined>>
+): SessionMemoryView[] {
+ const index = new Map<string, { projectId: string; projectName: string; title: string }>()
+ for (const p of projects) {
+ for (const n of p.nodes ?? []) {
+ if (index.has(n.id)) continue
+ index.set(n.id, {
+ projectId: p.id,
+ projectName: p.name,
+ title: n.title?.trim() || n.id
+ })
+ }
+ }
+
+ return rows.map((row) => {
+ const hit = index.get(row.nodeId)
+ if (!hit) {
+ // No node on any canvas. A state without a node is meaningless — the panel could not
+ // explain a "working" dot on a row it cannot travel to — so it is dropped, not passed on.
+ return {
+ row,
+ title: row.session,
+ projectId: null,
+ projectName: null,
+ orphan: true,
+ state: null
+ }
+ }
+ return {
+ row,
+ title: hit.title,
+ projectId: hit.projectId,
+ projectName: hit.projectName,
+ orphan: false,
+ state: states[row.nodeId] ?? null
+ }
+ })
+}
+
+/** Grand total across the resolved rows — the panel header's number. */
+export function totalMb(views: readonly SessionMemoryView[]): number {
+ return views.reduce((sum, v) => sum + v.row.totalMb, 0)
+}