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 node snapshot/recreate for reopen-last-closed
Mina Samehb28c863321ab
diff --git a/src/renderer/lib/reopenNode.test.ts b/src/renderer/lib/reopenNode.test.tsnew file mode 100644index 000000000..6956329fc--- /dev/null+++ b/src/renderer/lib/reopenNode.test.ts@@ -0,0 +1,127 @@+import { describe, it, expect, vi, beforeEach } from 'vitest'+import { snapshotNode, recreateNodeFromSnapshot, type ReopenNodeSnapshot } from './reopenNode'++// createAgentNode reads settings/CLI-caps singletons — none of that matters for these tests,+// so stub the pieces snapshotNode/recreateNodeFromSnapshot actually touch.+vi.mock('@renderer/state/settings', () => ({+ useSettings: { getState: () => ({ settings: { customAgents: [], defaultNodeWidth: undefined, defaultNodeHeight: undefined, agentLaunchCommands: {} } }) }+}))+vi.mock('@renderer/state/permissionMode', () => ({ claudeCliCapsNow: () => ({ sessionIdFlag: false }) }))+vi.mock('@renderer/state/codexIdentity', () => ({ codexSharedIdentity: () => undefined }))++const baseCtx = () => ({+ liveNodeIds: new Set<string>(),+ project: undefined,+ resolveAccountId: (id: string | undefined) => id,+ permissionModeFor: () => undefined+})++describe('snapshotNode', () => {+ it('captures a leaf node, resolving absolute position through its parent chain', () => {+ const all = [+ { id: 'group-1', position: { x: 100, y: 200 }, parentId: undefined },+ { id: 'sticky-1', position: { x: 10, y: 20 }, parentId: 'group-1' }+ ]+ const node = {+ type: 'sticky' as const,+ position: { x: 10, y: 20 },+ parentId: 'group-1',+ extent: 'parent' as const,+ width: 240,+ height: 200,+ data: { title: 'Note', color: '#ffd60a', group: null, text: 'hi' }+ }+ const snap = snapshotNode(node, all)+ expect(snap).toEqual({+ type: 'sticky',+ position: { x: 10, y: 20 },+ absolutePosition: { x: 110, y: 220 },+ parentId: 'group-1',+ extent: 'parent',+ size: { width: 240, height: 200 },+ data: node.data+ })+ })++ it('returns null for group/subagent/loop nodes', () => {+ for (const type of ['group', 'subagent', 'loop'] as const) {+ const node = { type, position: { x: 0, y: 0 }, data: { title: 'x', color: '#fff', group: null } }+ expect(snapshotNode(node, [])).toBeNull()+ }+ })++ it('returns null for an account-login node', () => {+ const node = {+ type: 'terminal' as const,+ position: { x: 0, y: 0 },+ data: { title: 'Claude login', color: '#fff', group: null, initialCommand: 'claude /login' }+ }+ expect(snapshotNode(node, [])).toBeNull()+ })+})++describe('recreateNodeFromSnapshot', () => {+ const snap = (over: Partial<ReopenNodeSnapshot>): ReopenNodeSnapshot => ({+ type: 'sticky',+ position: { x: 10, y: 20 },+ absolutePosition: { x: 10, y: 20 },+ data: { title: 'Note', color: '#ffd60a', group: null, text: 'hi' },+ ...over+ })++ it('recreates a sticky at its absolute position when the parent is gone', () => {+ const node = recreateNodeFromSnapshot(snap({ parentId: 'dead-group' }), baseCtx())+ expect(node).not.toBeNull()+ expect(node!.type).toBe('sticky')+ expect(node!.position).toEqual({ x: 10, y: 20 })+ expect(node!.parentId).toBeUndefined()+ expect(node!.data.text).toBe('hi')+ expect(node!.data.title).toBe('Note')+ })++ it('reattaches to the parent group when it still exists, using the RELATIVE position', () => {+ const ctx = { ...baseCtx(), liveNodeIds: new Set(['group-1']) }+ const node = recreateNodeFromSnapshot(+ snap({ parentId: 'group-1', extent: 'parent', position: { x: 5, y: 5 }, absolutePosition: { x: 105, y: 205 } }),+ ctx+ )+ expect(node!.parentId).toBe('group-1')+ expect(node!.extent).toBe('parent')+ expect(node!.position).toEqual({ x: 5, y: 5 })+ })++ it('carries over size when the snapshot has one', () => {+ const node = recreateNodeFromSnapshot(snap({ size: { width: 900, height: 500 } }), baseCtx())+ expect(node!.width).toBe(900)+ expect(node!.height).toBe(500)+ })++ it('recreates an editor node from filePath, carrying the custom title', () => {+ const node = recreateNodeFromSnapshot(+ snap({+ type: 'editor',+ data: { title: 'My renamed editor', color: '#6ac4dc', group: null, filePath: '/tmp/a.ts' }+ }),+ baseCtx()+ )+ expect(node!.type).toBe('editor')+ expect(node!.data.filePath).toBe('/tmp/a.ts')+ expect(node!.data.title).toBe('My renamed editor')+ })++ it('returns null for an editor snapshot missing filePath (never a silently blank node)', () => {+ const node = recreateNodeFromSnapshot(+ snap({ type: 'editor', data: { title: 'x', color: '#fff', group: null } }),+ baseCtx()+ )+ expect(node).toBeNull()+ })++ it('recreates a dino node carrying its high score', () => {+ const node = recreateNodeFromSnapshot(+ snap({ type: 'dino', data: { title: 'Dino', color: '#a2a2a2', group: null, highScore: 42 } }),+ baseCtx()+ )+ expect(node!.data.highScore).toBe(42)+ })+})diff --git a/src/renderer/lib/reopenNode.ts b/src/renderer/lib/reopenNode.tsindex 5a82b87f6..79458ae5f 100644--- a/src/renderer/lib/reopenNode.ts+++ b/src/renderer/lib/reopenNode.ts@@ -1,6 +1,192 @@+import type { NodeKind, Project } from '@shared/types'+import type { AgentId, AgentPermissionMode } from '@shared/agents/config'+import {+ type CanvasNode,+ type NodeData,+ createTerminalNode,+ createAgentNode,+ createEditorNode,+ createVideoNode,+ createWebNode,+ createBrowserNode,+ createDiffNode,+ createStickyNode,+ createDinoNode,+ isAccountLoginNode+} from '@renderer/state/workspace'+import { absolutePosition, type FocusableNode } from './nodeFocus'++export type RestorableNodeKind = Exclude<NodeKind, 'group' | 'subagent' | 'loop'>+export interface ReopenNodeSnapshot {- type: string+ type: RestorableNodeKind+ /** Raw position as stored on the node — relative to `parentId` when one is set. */position: { x: number; y: number }+ /** Precomputed via `absolutePosition` at snapshot time, for when the parent is gone by+ * restore time (its own raw position would then be meaningless on its own). */absolutePosition: { x: number; y: number }- data: Record<string, unknown>+ parentId?: string+ extent?: 'parent'+ size?: { width: number; height: number }+ data: NodeData+}++type SnapshotSource = {+ type?: string+ position: { x: number; y: number }+ parentId?: string+ /** @xyflow/react's `Node.extent` is `'parent' | CoordinateExtent | undefined` — only the+ * `'parent'` literal matters here, so this stays loosely typed rather than importing that+ * union just to narrow it. */+ extent?: unknown+ width?: number | null+ height?: number | null+ data: NodeData+}++const UNRESTORABLE: ReadonlySet<string> = new Set(['group', 'subagent', 'loop'])++/** Captures a node right before deletion. `all` must be the FULL live tree (before any+ * mutation), so the parent chain is still walkable. Returns null for kinds this feature+ * doesn't cover: group/subagent/loop nodes, and the account-login node (deleting it is a+ * distinct "remove this account's node" action, not a close a user wants back). */+export function snapshotNode(+ node: SnapshotSource,+ all: readonly FocusableNode[]+): ReopenNodeSnapshot | null {+ const type = node.type ?? 'terminal'+ if (UNRESTORABLE.has(type)) return null+ if (isAccountLoginNode(node.data)) return null+ const size =+ typeof node.width === 'number' && typeof node.height === 'number'+ ? { width: node.width, height: node.height }+ : undefined+ return {+ type: type as RestorableNodeKind,+ position: node.position,+ absolutePosition: absolutePosition({ id: '__snapshot__', position: node.position, parentId: node.parentId }, all),+ ...(node.parentId ? { parentId: node.parentId } : {}),+ ...(node.extent === 'parent' ? { extent: 'parent' as const } : {}),+ ...(size ? { size } : {}),+ data: node.data+ }+}++export interface RecreateContext {+ /** ids present in the tree right now — decides whether the original parent group is still+ * around to reattach to. Groups are never recreated by this feature, so this only ever+ * needs to reflect nodes that were never deleted. */+ liveNodeIds: ReadonlySet<string>+ project: { ssh?: Project['ssh'] } | undefined+ /** Validates/redirects an account id against the accounts that exist right now (a removed+ * account must not be stamped onto a new node). */+ resolveAccountId: (accountId: string | undefined) => string | undefined+ permissionModeFor: (agentId: AgentId) => AgentPermissionMode | undefined+}++const COSMETIC_KEYS = [+ 'title', 'titleAuto', 'color', 'group', 'tags', 'collapsed', 'expandedHeight', 'shell', 'agentModel'+] as const++function withCosmetics(node: CanvasNode, data: NodeData): CanvasNode {+ const cosmetics: Partial<NodeData> = {}+ for (const key of COSMETIC_KEYS) {+ const value = data[key]+ if (value !== undefined) (cosmetics as Record<string, unknown>)[key] = value+ }+ return { ...node, data: { ...node.data, ...cosmetics } }+}++function buildBase(snapshot: ReopenNodeSnapshot, ctx: RecreateContext): CanvasNode | null {+ const d = snapshot.data+ switch (snapshot.type) {+ case 'terminal': {+ if (d.sshRemoteTmux && d.ssh) {+ // `remoteCwd` is non-optional on `Project['ssh']` — a remote node always had a cwd, but+ // fall back to '' (server default `~`) rather than asserting the type away.+ const sshBinding: Project['ssh'] = { server: d.ssh, remoteCwd: d.cwd ?? '' }+ return d.agentId+ ? createAgentNode(+ d.agentId,+ 0,+ d.cwd,+ undefined,+ undefined,+ sshBinding,+ ctx.resolveAccountId(d.accountId),+ ctx.permissionModeFor(d.agentId)+ )+ : createTerminalNode(0, d.cwd, undefined, undefined, sshBinding)+ }+ if (d.ssh) {+ // Standalone "SSH terminal" node (local `ssh …`, not a remote-tmux project node).+ // Deliberately NOT routed through `createSshTerminalNode(server, …)`: that factory+ // RECOMPUTES `execTrusted: server.extraArgs ? true : undefined` from whatever server+ // object it's handed, which would mint trust=true for `extraArgs` of unknown+ // provenance (e.g. hydrated some other way). `d.ssh` is the LIVE node's own data+ // object, captured at snapshot time — its `execTrusted` already reflects genuine+ // local provenance (see `SshConnection.execTrusted`'s doc in shared/ssh.ts), so it is+ // copied verbatim, never re-derived. Build off `createTerminalNode` for id/size/position+ // scaffolding, then stamp `data.ssh` directly (no `sshRemoteTmux` — that flag means the+ // OTHER, remote-tmux-project case above).+ const scaffold = createTerminalNode(0)+ return { ...scaffold, data: { ...scaffold.data, ssh: d.ssh } }+ }+ if (d.agentId) {+ return createAgentNode(+ d.agentId,+ 0,+ d.cwd,+ undefined,+ undefined,+ ctx.project?.ssh,+ ctx.resolveAccountId(d.accountId),+ ctx.permissionModeFor(d.agentId)+ )+ }+ return createTerminalNode(0, d.cwd, undefined, undefined, ctx.project?.ssh)+ }+ case 'sticky': {+ const node = createStickyNode(0)+ return { ...node, data: { ...node.data, text: d.text ?? '' } }+ }+ case 'editor':+ return d.filePath ? createEditorNode(0, d.filePath, undefined, d.sshFs) : null+ case 'video':+ return d.filePath ? createVideoNode(0, d.filePath, undefined, d.sshFs) : null+ case 'diff':+ return d.cwd && d.filePath ? createDiffNode(0, d.cwd, d.filePath, !!d.diffStaged, undefined, d.commitOid) : null+ case 'web':+ return createWebNode(0, { url: d.url, filePath: d.filePath })+ case 'browser':+ return createBrowserNode(0, d.url ?? '', undefined, d.partition)+ case 'dino':+ return createDinoNode(0, undefined, d.highScore ?? 0)+ default:+ return null+ }+}++/** Rebuilds a fresh node from a snapshot: same kind, position, parent group (if it still+ * exists), and cosmetic metadata — but a NEW id/session, assembled through the same factories+ * "New terminal/agent/…" uses, so e.g. an agent node's launch command is computed correctly+ * for the CURRENT permission mode rather than replaying a stale one. Returns null for a kind+ * this feature doesn't cover, or when required data (e.g. an editor's filePath) is missing —+ * never a silently wrong node. */+export function recreateNodeFromSnapshot(snapshot: ReopenNodeSnapshot, ctx: RecreateContext): CanvasNode | null {+ const base = buildBase(snapshot, ctx)+ if (!base) return null+ const node = withCosmetics(base, snapshot.data)+ const reattach = !!snapshot.parentId && ctx.liveNodeIds.has(snapshot.parentId)+ node.position = reattach ? snapshot.position : snapshot.absolutePosition+ node.parentId = reattach ? snapshot.parentId : undefined+ // `snapshot.extent` is loosely typed (see SnapshotSource) — narrow back to the one literal+ // this codebase ever sets on a child node.+ node.extent = reattach && snapshot.extent === 'parent' ? 'parent' : undefined+ if (snapshot.size) {+ node.width = snapshot.size.width+ node.height = snapshot.size.height+ node.style = { ...node.style, width: snapshot.size.width, height: snapshot.size.height }+ }+ return node}