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(sessions): pure session-list grouping helper + tests

enesba0ef1ae6420
diff --git a/src/renderer/lib/sessionList.test.ts b/src/renderer/lib/sessionList.test.ts
new file mode 100644
index 000000000..1a813a94a
--- /dev/null
+++ b/src/renderer/lib/sessionList.test.ts
@@ -0,0 +1,73 @@
+import { describe, it, expect } from 'vitest'
+import { buildSessionList, sessionStatusKind, type ProjectInput } from './sessionList'
+import type { AgentNodeStatus } from '../state/agentStatus'
+
+const node = (id: string, over: Partial<ProjectInput['nodes'][number]> = {}) => ({
+ id,
+ kind: 'terminal' as const,
+ title: id,
+ color: '#888',
+ ...over
+})
+
+const projects = (): ProjectInput[] => [
+ { id: 'p1', name: 'Alpha', color: '#111', cwd: '/a', nodes: [node('t1'), node('a1', { agentId: 'claude' })] },
+ { id: 'p2', name: 'Beta', color: '#222', nodes: [node('t2'), node('s1', { kind: 'sticky' }), node('e1', { kind: 'editor' })] }
+]
+
+describe('sessionStatusKind', () => {
+ it('maps agent states to status kinds', () => {
+ expect(sessionStatusKind('working')).toBe('working')
+ expect(sessionStatusKind('waiting')).toBe('attention')
+ expect(sessionStatusKind('blocked')).toBe('attention')
+ expect(sessionStatusKind('done')).toBe('done')
+ expect(sessionStatusKind(undefined)).toBe('idle')
+ })
+})
+
+describe('buildSessionList', () => {
+ it('groups by project with the active project first', () => {
+ const groups = buildSessionList(projects(), null, 'p2', {}, '')
+ expect(groups.map((g) => g.projectId)).toEqual(['p2', 'p1'])
+ expect(groups[0].isActive).toBe(true)
+ })
+
+ it('keeps only terminal/agent nodes and flags agents', () => {
+ const groups = buildSessionList(projects(), null, 'p1', {}, '')
+ const p2 = groups.find((g) => g.projectId === 'p2')!
+ expect(p2.sessions.map((s) => s.id)).toEqual(['t2']) // sticky + editor dropped
+ const p1 = groups.find((g) => g.projectId === 'p1')!
+ expect(p1.sessions.find((s) => s.id === 'a1')!.isAgent).toBe(true)
+ expect(p1.sessions.find((s) => s.id === 't1')!.isAgent).toBe(false)
+ })
+
+ it('attaches status and unread from the status map', () => {
+ const status: Record<string, AgentNodeStatus> = {
+ a1: { unread: true, state: 'working', agentId: 'claude', session: 'fix bug', sessionId: 'sess-1' }
+ }
+ const groups = buildSessionList(projects(), null, 'p1', status, '')
+ const a1 = groups[0].sessions.find((s) => s.id === 'a1')!
+ expect(a1.statusKind).toBe('working')
+ expect(a1.unread).toBe(true)
+ expect(a1.session).toBe('fix bug')
+ expect(a1.sessionId).toBe('sess-1')
+ expect(a1.usesContext).toBe(true) // claude is USAGE_CAPABLE
+ })
+
+ it('uses live nodes for the active project instead of serialized ones', () => {
+ const live = [node('t1', { title: 'renamed live' })]
+ const groups = buildSessionList(projects(), live, 'p1', {}, '')
+ const p1 = groups.find((g) => g.projectId === 'p1')!
+ expect(p1.sessions.map((s) => s.title)).toEqual(['renamed live'])
+ })
+
+ it('filters by title and session name, hiding empty groups only when filtering', () => {
+ const status: Record<string, AgentNodeStatus> = { a1: { unread: false, session: 'special' } }
+ const filtered = buildSessionList(projects(), null, 'p1', status, 'spec')
+ expect(filtered.map((g) => g.projectId)).toEqual(['p1'])
+ expect(filtered[0].sessions.map((s) => s.id)).toEqual(['a1'])
+
+ const unfiltered = buildSessionList(projects(), null, 'p1', {}, '')
+ expect(unfiltered.length).toBe(2) // empty groups kept when no filter
+ })
+})
diff --git a/src/renderer/lib/sessionList.ts b/src/renderer/lib/sessionList.ts
new file mode 100644
index 000000000..815387877
--- /dev/null
+++ b/src/renderer/lib/sessionList.ts
@@ -0,0 +1,129 @@
+import type { AgentNodeStatus } from '../state/agentStatus'
+import type { AgentId } from '@shared/agents/config'
+import type { NodeKind } from '@shared/types'
+import { hasUsage } from '@shared/agents/config'
+import type { SshConnection } from '@shared/ssh'
+
+export interface SessionNodeInput {
+ id: string
+ kind: NodeKind
+ title: string
+ color: string
+ agentId?: AgentId
+ cwd?: string
+ ssh?: SshConnection
+}
+
+export interface ProjectInput {
+ id: string
+ name: string
+ color: string
+ cwd?: string
+ nodes: SessionNodeInput[]
+}
+
+export type StatusKind = 'working' | 'attention' | 'done' | 'idle'
+
+const STATE_LABEL: Record<StatusKind, string> = {
+ working: 'Running',
+ attention: 'Needs you',
+ done: 'Done',
+ idle: 'Idle'
+}
+
+export function sessionStatusKind(state: AgentNodeStatus['state']): StatusKind {
+ switch (state) {
+ case 'working':
+ return 'working'
+ case 'waiting':
+ case 'blocked':
+ return 'attention'
+ case 'done':
+ return 'done'
+ default:
+ return 'idle'
+ }
+}
+
+export interface SessionRowVM {
+ id: string
+ title: string
+ color: string
+ agentId?: AgentId
+ isAgent: boolean
+ statusKind: StatusKind
+ stateLabel: string
+ unread: boolean
+ session?: string
+ loop?: { kind: 'loop' | 'schedule' | 'cron'; count: number }
+ cwd?: string
+ sshHost?: string
+ sessionId?: string
+ usesContext: boolean
+}
+
+export interface SessionGroup {
+ projectId: string
+ projectName: string
+ projectColor: string
+ cwd?: string
+ isActive: boolean
+ sessions: SessionRowVM[]
+}
+
+function toRow(n: SessionNodeInput, status: AgentNodeStatus | undefined): SessionRowVM {
+ const statusKind = sessionStatusKind(status?.state)
+ return {
+ id: n.id,
+ title: n.title,
+ color: n.color,
+ agentId: n.agentId,
+ isAgent: !!n.agentId,
+ statusKind,
+ stateLabel: STATE_LABEL[statusKind],
+ unread: !!status?.unread,
+ session: status?.session,
+ loop: status?.loop ? { kind: status.loop.kind, count: status.loop.count } : undefined,
+ cwd: n.cwd,
+ sshHost: n.ssh?.host,
+ sessionId: status?.sessionId,
+ usesContext: n.agentId ? hasUsage(n.agentId) : false
+ }
+}
+
+function matches(row: SessionRowVM, needle: string): boolean {
+ const hay = `${row.title} ${row.session ?? ''}`.toLowerCase()
+ return hay.includes(needle)
+}
+
+export function buildSessionList(
+ projects: ProjectInput[],
+ liveActiveNodes: SessionNodeInput[] | null,
+ activeProjectId: string,
+ statusById: Record<string, AgentNodeStatus>,
+ filter: string
+): SessionGroup[] {
+ const needle = filter.trim().toLowerCase()
+ const groups: SessionGroup[] = projects.map((p) => {
+ const isActive = p.id === activeProjectId
+ const source = isActive && liveActiveNodes ? liveActiveNodes : p.nodes
+ let sessions = source
+ .filter((n) => n.kind === 'terminal')
+ .map((n) => toRow(n, statusById[n.id]))
+ if (needle) sessions = sessions.filter((r) => matches(r, needle))
+ return {
+ projectId: p.id,
+ projectName: p.name,
+ projectColor: p.color,
+ cwd: p.cwd,
+ isActive,
+ sessions
+ }
+ })
+
+ const ordered = [
+ ...groups.filter((g) => g.isActive),
+ ...groups.filter((g) => !g.isActive)
+ ]
+ return needle ? ordered.filter((g) => g.sessions.length > 0) : ordered
+}