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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat: add pure closed-session build/convert/merge helpers
Mina Sameh7c45179dce9b
diff --git a/src/renderer/lib/closedHistory.test.ts b/src/renderer/lib/closedHistory.test.tsnew file mode 100644index 000000000..dde8ae093--- /dev/null+++ b/src/renderer/lib/closedHistory.test.ts@@ -0,0 +1,105 @@+import { describe, it, expect } from 'vitest'+import { buildClosedSessionEntries, stateToReopenSnapshot, mergeClosedHistory } from './closedHistory'+import type { CanvasNode } from '@renderer/state/workspace'+import type { ClosedSessionEntry, Project } from '@shared/types'++const node = (over: Partial<CanvasNode> = {}): CanvasNode =>+ ({+ id: 'n1', type: 'terminal', position: { x: 5, y: 5 }, width: 400, height: 300,+ data: { title: 'shell', color: '#fff', group: null, cwd: '/tmp/x' },+ ...over+ }) as CanvasNode++describe('buildClosedSessionEntries', () => {+ it('builds one entry per deleted, restorable node', () => {+ const nodes = [node({ id: 'n1' }), node({ id: 'n2' })]+ const entries = buildClosedSessionEntries(new Set(['n1']), nodes, 999, () => 'fresh-id')+ expect(entries).toHaveLength(1)+ expect(entries[0]).toMatchObject({ id: 'fresh-id', closedAt: 999 })+ expect(entries[0].node.id).toBe('n1')+ expect(entries[0].node.cwd).toBe('/tmp/x')+ expect(entries[0].absolutePosition).toEqual({ x: 5, y: 5 })+ })++ it('excludes group/subagent/loop nodes and the account-login node, same as snapshotNode', () => {+ const groupNode = node({ id: 'g1', type: 'group' })+ const loginNode = node({ id: 'login', data: { title: 'Claude login', color: '#fff', group: null, initialCommand: 'claude /login' } })+ const entries = buildClosedSessionEntries(new Set(['g1', 'login']), [groupNode, loginNode], 1, () => 'x')+ expect(entries).toHaveLength(0)+ })++ it('only builds entries for ids actually in deletedIds', () => {+ const nodes = [node({ id: 'n1' }), node({ id: 'n2' })]+ const entries = buildClosedSessionEntries(new Set(['n2']), nodes, 1, () => 'x')+ expect(entries.map((e) => e.node.id)).toEqual(['n2'])+ })+})++describe('stateToReopenSnapshot', () => {+ it('carries position/parent/size/data through for recreateNodeFromSnapshot', () => {+ const entry: ClosedSessionEntry = {+ id: 'e1', closedAt: 1,+ node: {+ id: 'n1', kind: 'terminal', position: { x: 1, y: 2 }, size: { width: 10, height: 20 },+ title: 'shell', color: '#fff', group: null, parentId: 'grp-1', cwd: '/tmp/x', agentId: 'claude'+ },+ absolutePosition: { x: 100, y: 200 }+ }+ const snap = stateToReopenSnapshot(entry)+ expect(snap.type).toBe('terminal')+ expect(snap.position).toEqual({ x: 1, y: 2 })+ expect(snap.absolutePosition).toEqual({ x: 100, y: 200 })+ expect(snap.parentId).toBe('grp-1')+ expect(snap.extent).toBe('parent')+ expect(snap.size).toEqual({ width: 10, height: 20 })+ expect(snap.data.cwd).toBe('/tmp/x')+ expect(snap.data.agentId).toBe('claude')+ })++ it('omits parentId/extent when the node was never parented', () => {+ const entry: ClosedSessionEntry = {+ id: 'e1', closedAt: 1,+ node: { id: 'n1', kind: 'sticky', position: { x: 0, y: 0 }, size: { width: 10, height: 10 }, title: 'note', color: '#fff', group: null, text: 'hi' },+ absolutePosition: { x: 0, y: 0 }+ }+ const snap = stateToReopenSnapshot(entry)+ expect(snap.parentId).toBeUndefined()+ expect(snap.extent).toBeUndefined()+ expect(snap.data.text).toBe('hi')+ })+})++describe('mergeClosedHistory', () => {+ const proj = (over: Partial<Project>): Project =>+ ({ id: 'p', name: 'p', color: '#fff', viewport: { x: 0, y: 0, zoom: 1 }, nodes: [], ...over }) as Project++ it('merges closed projects and closed sessions across all projects, sorted newest-first', () => {+ const entry = (id: string, closedAt: number): ClosedSessionEntry => ({+ id, closedAt,+ node: { id: 'n', kind: 'terminal', position: { x: 0, y: 0 }, size: { width: 1, height: 1 }, title: 't', color: '#fff', group: null },+ absolutePosition: { x: 0, y: 0 }+ })+ const projects = [+ proj({ id: 'a', closed: true, closedAt: 50 }),+ proj({ id: 'b', closedSessions: [entry('s1', 100), entry('s2', 10)] })+ ]+ const rows = mergeClosedHistory(projects)+ expect(rows.map((r) => (r.kind === 'project' ? r.projectId : r.entry.id))).toEqual(['s1', 'a', 's2'])+ })++ it('sorts a project with no closedAt last', () => {+ const projects = [proj({ id: 'a', closed: true }), proj({ id: 'b', closed: true, closedAt: 5 })]+ const rows = mergeClosedHistory(projects)+ expect(rows.map((r) => (r.kind === 'project' ? r.projectId : ''))).toEqual(['b', 'a'])+ })++ it('ignores an open project with no closedSessions', () => {+ const rows = mergeClosedHistory([proj({ id: 'a' })])+ expect(rows).toHaveLength(0)+ })++ it('excludes an unavailable closed project', () => {+ const rows = mergeClosedHistory([proj({ id: 'a', closed: true, unavailable: true, closedAt: 5 })])+ expect(rows).toHaveLength(0)+ })+})diff --git a/src/renderer/lib/closedHistory.ts b/src/renderer/lib/closedHistory.tsnew file mode 100644index 000000000..f2776477b--- /dev/null+++ b/src/renderer/lib/closedHistory.ts@@ -0,0 +1,102 @@+import type { CanvasNodeState, ClosedSessionEntry, Project } from '@shared/types'+import { flowToNodeStates, type CanvasNode } from '@renderer/state/workspace'+import { snapshotNode, type ReopenNodeSnapshot, type RestorableNodeKind } from './reopenNode'+import { absolutePosition, type FocusableNode } from './nodeFocus'++/**+ * Builds one `ClosedSessionEntry` per node in `deletedIds` that `snapshotNode` would also accept+ * for the in-memory `Cmd+Shift+T` history — the two histories must agree on what's restorable+ * (group/subagent/loop/account-login nodes are excluded from both). `allNodes` must be the FULL+ * live tree from BEFORE the deletion, so parent-chain absolute positions are still resolvable.+ * `now`/`makeId` are injected so this stays a pure, deterministically testable function; call+ * sites pass `Date.now()`/`crypto.randomUUID`.+ */+export function buildClosedSessionEntries(+ deletedIds: ReadonlySet<string>,+ allNodes: readonly CanvasNode[],+ now: number,+ makeId: () => string+): ClosedSessionEntry[] {+ return allNodes+ .filter((n) => deletedIds.has(n.id))+ .filter((n) => snapshotNode(n, allNodes as readonly FocusableNode[]) !== null)+ .map((n) => ({+ id: makeId(),+ closedAt: now,+ node: flowToNodeStates([n])[0],+ absolutePosition: absolutePosition(+ { id: n.id, position: n.position, parentId: n.parentId },+ allNodes as readonly FocusableNode[]+ )+ }))+}++/**+ * Converts a persisted `ClosedSessionEntry` back into the shape `recreateNodeFromSnapshot`+ * already accepts. `CanvasNodeState` and `NodeData` share field names for everything both+ * track (by construction of `nodeStatesToFlow`/`flowToNodeStates`), so this is a direct field+ * copy, not a lossy remap.+ */+export function stateToReopenSnapshot(entry: ClosedSessionEntry): ReopenNodeSnapshot {+ const n = entry.node+ return {+ type: n.kind as RestorableNodeKind,+ position: n.position,+ absolutePosition: entry.absolutePosition,+ ...(n.parentId ? { parentId: n.parentId, extent: 'parent' as const } : {}),+ size: n.size,+ data: {+ title: n.title,+ titleAuto: n.titleAuto,+ color: n.color,+ group: n.group,+ tags: n.tags,+ collapsed: n.collapsed,+ hideFanout: n.hideFanout,+ shell: n.shell,+ cwd: n.cwd,+ text: n.text,+ textUpdatedAt: n.textUpdatedAt,+ textUpdatedBy: n.textUpdatedBy,+ filePath: n.filePath,+ fileMissing: n.fileMissing,+ url: n.url,+ partition: n.partition,+ diffStaged: n.diffStaged,+ commitOid: n.commitOid,+ highScore: n.highScore,+ agentId: n.agentId,+ agentModel: n.agentModel,+ accountId: n.accountId,+ agentSessionId: n.agentSessionId,+ ssh: n.ssh,+ sshRemoteTmux: n.sshRemoteTmux,+ sshFs: n.sshFs+ }+ }+}++export type ClosedHistoryRow =+ | { kind: 'project'; projectId: string; closedAt: number; project: Project }+ | { kind: 'session'; projectId: string; closedAt: number; entry: ClosedSessionEntry }++/**+ * Merges every project's closed-session entries with every closed, AVAILABLE project into one+ * list, sorted newest-first. `unavailable` (a ref whose folder is missing / server unreachable —+ * same check Canvas's own `closedProjects` selector already applies) is excluded: reopening it+ * would activate an empty placeholder, and it has no history worth showing. An entry/project with+ * no known `closedAt` (pre-existing data from before that field existed) sorts last via the `-1`+ * sentinel — never `NaN` from subtracting `undefined`.+ */+export function mergeClosedHistory(projects: readonly Project[]): ClosedHistoryRow[] {+ const rows: ClosedHistoryRow[] = []+ for (const p of projects) {+ if (p.closed && !p.unavailable) {+ rows.push({ kind: 'project', projectId: p.id, closedAt: p.closedAt ?? -1, project: p })+ }+ for (const entry of p.closedSessions ?? []) {+ rows.push({ kind: 'session', projectId: p.id, closedAt: entry.closedAt, entry })+ }+ }+ return rows.sort((a, b) => b.closedAt - a.closedAt)+}