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(workspace): pure split/join module for per-project files
enes3cd1108be2e5
diff --git a/src/core/workspace-files.test.ts b/src/core/workspace-files.test.tsnew file mode 100644index 000000000..9f38370bc--- /dev/null+++ b/src/core/workspace-files.test.ts@@ -0,0 +1,99 @@+import { describe, it, expect } from 'vitest'+import type { CanvasNodeState, Project, Workspace } from '../shared/types'+import {+ toPortableNodes, resolveNodes, projectToFile, fileToProject,+ sameProjectContent, splitWorkspace, serializeProjectFile+} from './workspace-files'++const node = (over: Partial<CanvasNodeState> = {}): CanvasNodeState => ({+ id: 'term-abc', kind: 'terminal', position: { x: 0, y: 0 },+ size: { width: 400, height: 300 }, title: 't', color: '#fff', group: null, ...over+})+const project = (over: Partial<Project> = {}): Project => ({+ id: 'p1', name: 'foo', color: '#7aa2f7', viewport: { x: 0, y: 0, zoom: 1 },+ nodes: [node()], ...over+})++describe('portable node cwds', () => {+ it('relativizes cwds under the root with a ./ prefix and resolves them back', () => {+ const nodes = [node({ cwd: '/Users/enes/projects/foo/sub' })]+ const portable = toPortableNodes(nodes, '/Users/enes/projects/foo')+ expect(portable[0].cwd).toBe('./sub')+ expect(resolveNodes(portable, '/mnt/other/foo')[0].cwd).toBe('/mnt/other/foo/sub')+ })+ it('the root itself becomes "." ', () => {+ const portable = toPortableNodes([node({ cwd: '/a/b' })], '/a/b')+ expect(portable[0].cwd).toBe('.')+ expect(resolveNodes(portable, '/x')[0].cwd).toBe('/x')+ })+ it('paths outside the root stay absolute, cwd-less nodes untouched', () => {+ const portable = toPortableNodes([node({ cwd: '/elsewhere' }), node({ id: 'n2' })], '/a/b')+ expect(portable[0].cwd).toBe('/elsewhere')+ expect(portable[1].cwd).toBeUndefined()+ expect(resolveNodes(portable, '/a/b')[0].cwd).toBe('/elsewhere')+ })+})++describe('projectToFile / fileToProject round-trip', () => {+ it('drops cwd/closed/unavailable, keeps nodes/session state, restores from base', () => {+ const p = project({+ cwd: '/a/b', closed: true, defaultAccountId: 'acct1', dinoHighScore: 7,+ nodes: [node({ cwd: '/a/b/x', agentId: 'claude', accountId: 'acct1' })],+ bridges: [{ id: 'e1', source: 's', target: 't' }]+ })+ const f = projectToFile(p, 3, '2026-07-11T00:00:00.000Z')+ expect(f.version).toBe(1); expect(f.rev).toBe(3)+ expect((f as any).cwd).toBeUndefined(); expect((f as any).closed).toBeUndefined()+ expect(f.nodes[0].cwd).toBe('./x')+ const back = fileToProject(f, { cwd: '/new/root', closed: true })+ expect(back).toMatchObject({ id: 'p1', cwd: '/new/root', closed: true, defaultAccountId: 'acct1', dinoHighScore: 7 })+ expect(back.nodes[0]).toMatchObject({ cwd: '/new/root/x', agentId: 'claude', accountId: 'acct1' })+ expect(back.unavailable).toBeUndefined()+ })+})++describe('sameProjectContent', () => {+ it('ignores rev and savedAt, sees node changes', () => {+ const a = projectToFile(project(), 1, '2026-01-01T00:00:00.000Z')+ const b = projectToFile(project(), 9, '2026-02-02T00:00:00.000Z')+ expect(sameProjectContent(a, b)).toBe(true)+ const c = projectToFile(project({ nodes: [node({ title: 'renamed' })] }), 1, a.savedAt)+ expect(sameProjectContent(a, c)).toBe(false)+ })+})++describe('splitWorkspace', () => {+ const ws: Workspace = {+ version: 2, activeProjectId: 'p1',+ projects: [+ project({ id: 'p1', cwd: '/a/foo' }),+ project({ id: 'p2', name: 'canvas-only' }),+ project({ id: 'p3', ssh: { server: { host: 'h', user: 'u' } as any, remoteCwd: '~/app' } })+ ]+ }+ it('local-cwd → ref entry + file; cwd-less → inline; ssh → ssh entry + cache', () => {+ const { index, files } = splitWorkspace(ws, () => 1, '2026-07-11T00:00:00.000Z')+ expect(index.version).toBe(3)+ expect(index.activeProjectId).toBe('p1')+ expect(index.entries[0]).toMatchObject({ id: 'p1', name: 'foo', cwd: '/a/foo' })+ expect(index.entries[0].project).toBeUndefined()+ expect(files.get('/a/foo')!.id).toBe('p1')+ expect(index.entries[1].project!.id).toBe('p2')+ expect(index.entries[2].ssh!.remoteCwd).toBe('~/app')+ expect(index.entries[2].cache!.id).toBe('p3')+ expect(files.has('~/app')).toBe(false) // ssh files are not local writes+ })+ it('never persists the runtime unavailable flag inline', () => {+ const { index } = splitWorkspace(+ { version: 2, activeProjectId: 'x', projects: [project({ id: 'x', unavailable: true })] },+ () => 1, '2026-07-11T00:00:00.000Z')+ expect(index.entries[0].project!.unavailable).toBeUndefined()+ })+})++describe('serializeProjectFile', () => {+ it('is pretty-printed with version first (stable git diffs)', () => {+ const s = serializeProjectFile(projectToFile(project(), 1, '2026-07-11T00:00:00.000Z'))+ expect(s.startsWith('{\n "version": 1,\n "rev": 1,')).toBe(true)+ })+})diff --git a/src/core/workspace-files.ts b/src/core/workspace-files.tsnew file mode 100644index 000000000..e0489190b--- /dev/null+++ b/src/core/workspace-files.ts@@ -0,0 +1,139 @@+import path from 'path'+import type { BridgeLink, CanvasNodeState, Project, Viewport, Workspace } from '../shared/types'++export const PROJECT_DIR = '.nodeterm'+export const PROJECT_FILE = 'project.json'++/** On-disk shape of <cwd>/.nodeterm/project.json. No `cwd` field — the containing+ * folder IS the cwd (that's what makes the folder relocatable). Node cwds inside+ * the root are stored relative ("./sub"). Session/account state is included by+ * design: the file is a single shared document (see the spec). */+export interface ProjectFileV1 {+ version: 1+ /** Monotonic save counter; picks a winner when an offline cache and the file diverge (SSH). */+ rev: number+ savedAt: string+ id: string+ name: string+ color: string+ viewport: Viewport+ nodes: CanvasNodeState[]+ bridges?: BridgeLink[]+ ropes?: BridgeLink[]+ defaultAccountId?: string+ dinoHighScore?: number+}++/** One workspace.json v3 entry. Exactly one of: `cwd` (local ref), `ssh` (remote ref),+ * `project` (inline, cwd-less canvas). name/color are a cached header so an+ * unavailable ref still renders a labeled grey tab. `cache` (ssh only) is the last+ * ProjectFileV1 seen/written — used while the server is unreachable. */+export interface IndexEntryV3 {+ id: string+ name: string+ color: string+ closed?: boolean+ cwd?: string+ ssh?: Project['ssh']+ cache?: ProjectFileV1+ project?: Project+}++export interface WorkspaceIndexV3 {+ version: 3+ activeProjectId: string+ entries: IndexEntryV3[]+}++const isUnder = (p: string, root: string): boolean => {+ const rel = path.relative(root, p)+ return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))+}++/** Rewrites node cwds under `root` to portable "./…" form; other paths untouched. */+export function toPortableNodes(nodes: CanvasNodeState[], root: string): CanvasNodeState[] {+ return nodes.map((n) => {+ if (!n.cwd || !isUnder(n.cwd, root)) return n+ const rel = path.relative(root, n.cwd)+ return { ...n, cwd: rel === '' ? '.' : `./${rel.split(path.sep).join('/')}` }+ })+}++/** Resolves portable "./…" node cwds against `root`; absolute cwds pass through. */+export function resolveNodes(nodes: CanvasNodeState[], root: string): CanvasNodeState[] {+ return nodes.map((n) => {+ if (!n.cwd || !(n.cwd === '.' || n.cwd.startsWith('./'))) return n+ return { ...n, cwd: n.cwd === '.' ? root : path.join(root, n.cwd.slice(2)) }+ })+}++export function projectToFile(p: Project, rev: number, savedAt: string): ProjectFileV1 {+ return {+ version: 1,+ rev,+ savedAt,+ id: p.id,+ name: p.name,+ color: p.color,+ viewport: p.viewport,+ nodes: p.cwd ? toPortableNodes(p.nodes, p.cwd) : p.nodes,+ ...(p.bridges ? { bridges: p.bridges } : {}),+ ...(p.ropes ? { ropes: p.ropes } : {}),+ ...(p.defaultAccountId ? { defaultAccountId: p.defaultAccountId } : {}),+ ...(p.dinoHighScore ? { dinoHighScore: p.dinoHighScore } : {})+ }+}++export function fileToProject(+ f: ProjectFileV1,+ base: { cwd?: string; ssh?: Project['ssh']; closed?: boolean }+): Project {+ return {+ id: f.id,+ name: f.name,+ color: f.color,+ viewport: f.viewport,+ nodes: base.cwd ? resolveNodes(f.nodes, base.cwd) : f.nodes,+ ...(f.bridges ? { bridges: f.bridges } : {}),+ ...(f.ropes ? { ropes: f.ropes } : {}),+ ...(f.defaultAccountId ? { defaultAccountId: f.defaultAccountId } : {}),+ ...(f.dinoHighScore ? { dinoHighScore: f.dinoHighScore } : {}),+ ...(base.cwd ? { cwd: base.cwd } : {}),+ ...(base.ssh ? { ssh: base.ssh } : {}),+ ...(base.closed ? { closed: true } : {})+ }+}++/** Content equality ignoring the bookkeeping fields — decides whether a save must bump rev + rewrite. */+export function sameProjectContent(a: ProjectFileV1, b: ProjectFileV1): boolean {+ const strip = ({ rev: _r, savedAt: _s, ...rest }: ProjectFileV1) => rest+ return JSON.stringify(strip(a)) === JSON.stringify(strip(b))+}++/** Splits an in-memory workspace into the v3 index + the local project files to write. */+export function splitWorkspace(+ ws: Workspace,+ revOf: (projectId: string) => number,+ savedAt: string+): { index: WorkspaceIndexV3; files: Map<string, ProjectFileV1> } {+ const entries: IndexEntryV3[] = []+ const files = new Map<string, ProjectFileV1>()+ for (const p of ws.projects) {+ const header = { id: p.id, name: p.name, color: p.color, ...(p.closed ? { closed: true } : {}) }+ if (p.ssh) {+ entries.push({ ...header, ssh: p.ssh, cache: projectToFile(p, revOf(p.id), savedAt) })+ } else if (p.cwd) {+ files.set(p.cwd, projectToFile(p, revOf(p.id), savedAt))+ entries.push({ ...header, cwd: p.cwd })+ } else {+ const { unavailable: _u, ...inline } = p+ entries.push({ ...header, project: inline })+ }+ }+ return { index: { version: 3, activeProjectId: ws.activeProjectId, entries }, files }+}++/** Pretty, stable-order JSON for the project file (git-diffable; the index stays compact). */+export function serializeProjectFile(f: ProjectFileV1): string {+ return JSON.stringify(f, null, 2)+}