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(projects): registerProject — the non-activating create/find for open-project

enes423f934f3470
diff --git a/src/renderer/state/projects.register.test.ts b/src/renderer/state/projects.register.test.ts
new file mode 100644
index 000000000..0dac2a548
--- /dev/null
+++ b/src/renderer/state/projects.register.test.ts
@@ -0,0 +1,137 @@
+import { describe, it, expect, beforeEach } from 'vitest'
+import { useProjects } from './projects'
+import type { Project } from '@shared/types'
+
+/**
+ * Issue #338 Task 2.1 — `registerProject`, the NON-ACTIVATING create/find used by the
+ * `open-project` control verb (spec §2.1 steps 2–4).
+ *
+ * The one property every test here re-asserts is spec P6: `registerProject` NEVER writes
+ * `activeProjectId`. The human paths (`openFolderProject`/`adoptProject`/`reopenProject`) all
+ * activate — deliberately untouched (Decision 4) — so the agent verb needs its own store action,
+ * and the mutation this suite is checked against is exactly "reuse the activating path".
+ */
+
+beforeEach(() => {
+ useProjects.getState().hydrate({ version: 2, activeProjectId: '', projects: [] })
+})
+
+const probed = (id: string): Project => ({
+ id,
+ name: 'shared-app',
+ color: '#7aa2f7',
+ cwd: '/Users/me/dev/shared-app',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: [
+ {
+ id: 'term-a',
+ kind: 'terminal' as const,
+ position: { x: 0, y: 0 },
+ size: { width: 1, height: 1 },
+ title: 'a',
+ color: '#fff',
+ group: null
+ }
+ ]
+})
+
+describe('registerProject — idempotent hit (spec B1)', () => {
+ it('returns the same project twice without duplicating, and never activates', () => {
+ const active = useProjects.getState().addProject('elsewhere', '/tmp/elsewhere')
+ useProjects.getState().setActive(active.id)
+
+ const first = useProjects.getState().registerProject({ resolvedCwd: '/Users/me/dev/my-app' })
+ expect(first.created).toBe(true)
+ expect(first.adopted).toBe(false)
+ const second = useProjects.getState().registerProject({ resolvedCwd: '/Users/me/dev/my-app' })
+ expect(second.created).toBe(false)
+ expect(second.adopted).toBe(false)
+ expect(second.project.id).toBe(first.project.id)
+ expect(useProjects.getState().projects.filter((p) => p.cwd === '/Users/me/dev/my-app')).toHaveLength(1)
+ // P6 — the visible tab never moved.
+ expect(useProjects.getState().activeProjectId).toBe(active.id)
+ })
+
+ it('dedupes on the trailing-slash-normalized cwd', () => {
+ const first = useProjects.getState().registerProject({ resolvedCwd: '/Users/me/dev/my-app' })
+ const second = useProjects.getState().registerProject({ resolvedCwd: '/Users/me/dev/my-app/' })
+ expect(second.created).toBe(false)
+ expect(second.project.id).toBe(first.project.id)
+ })
+
+ it('un-closes a closed project WITHOUT activating it', () => {
+ const active = useProjects.getState().addProject('elsewhere', '/tmp/elsewhere')
+ useProjects.getState().setActive(active.id)
+ const closedOne = useProjects.getState().addProject('my-app', '/Users/me/dev/my-app')
+ useProjects.getState().closeProject(closedOne.id)
+
+ const hit = useProjects.getState().registerProject({ resolvedCwd: '/Users/me/dev/my-app' })
+ expect(hit.created).toBe(false)
+ expect(hit.project.id).toBe(closedOne.id)
+ const s = useProjects.getState()
+ // The tab is back (closed cleared)…
+ expect(s.projects.find((p) => p.id === closedOne.id)?.closed).toBeFalsy()
+ // …but the view did not travel (P6): reopenProject would have activated it.
+ expect(s.activeProjectId).toBe(active.id)
+ })
+
+ it('an idempotent hit never mutates the existing name or color on the agent’s say-so', () => {
+ const first = useProjects.getState().registerProject({ resolvedCwd: '/x/app', name: 'Real Name', color: '#123456' })
+ const hit = useProjects
+ .getState()
+ .registerProject({ resolvedCwd: '/x/app', name: 'Impostor', color: '#ff0000' })
+ expect(hit.created).toBe(false)
+ expect(hit.project.name).toBe('Real Name')
+ expect(hit.project.color).toBe('#123456')
+ expect(first.project.id).toBe(hit.project.id)
+ })
+})
+
+describe('registerProject — create (spec §2.1 step 4)', () => {
+ it('creates with the given name and color, defaulting the name to the folder basename', () => {
+ const named = useProjects.getState().registerProject({ resolvedCwd: '/a/b', name: 'Named', color: '#abcdef' })
+ expect(named.created).toBe(true)
+ expect(named.project.name).toBe('Named')
+ expect(named.project.color).toBe('#abcdef')
+ expect(named.project.cwd).toBe('/a/b')
+
+ const unnamed = useProjects.getState().registerProject({ resolvedCwd: '/a/some-repo' })
+ expect(unnamed.project.name).toBe('some-repo')
+ })
+
+ it('never activates the created project — activeProjectId is untouched in every branch', () => {
+ const active = useProjects.getState().addProject('elsewhere', '/tmp/elsewhere')
+ useProjects.getState().setActive(active.id)
+ useProjects.getState().registerProject({ resolvedCwd: '/a/b' })
+ expect(useProjects.getState().activeProjectId).toBe(active.id)
+ })
+})
+
+describe('registerProject — adopt (spec §2.1 step 3)', () => {
+ it('adopts a probed project keeping its minted id, without activating', () => {
+ const active = useProjects.getState().addProject('elsewhere', '/tmp/elsewhere')
+ useProjects.getState().setActive(active.id)
+
+ const r = useProjects
+ .getState()
+ .registerProject({ resolvedCwd: '/Users/me/dev/shared-app', probed: probed('project-minted') })
+ expect(r.adopted).toBe(true)
+ expect(r.created).toBe(false)
+ expect(r.project.id).toBe('project-minted')
+ expect(r.project.nodes).toHaveLength(1)
+ expect(useProjects.getState().activeProjectId).toBe(active.id)
+ })
+
+ it('defends against an id collision exactly as adoptProject does (derived id, nodes kept)', () => {
+ const taken = useProjects.getState().addProject('other', '/tmp/other')
+ const r = useProjects
+ .getState()
+ .registerProject({ resolvedCwd: '/Users/me/dev/shared-app', probed: probed(taken.id) })
+ expect(r.adopted).toBe(true)
+ expect(r.project.id).not.toBe(taken.id)
+ expect(r.project.nodes.map((n) => n.id)).toEqual(['term-a'])
+ // Both projects present, ids unique.
+ const ids = useProjects.getState().projects.map((p) => p.id)
+ expect(new Set(ids).size).toBe(ids.length)
+ })
+})
diff --git a/src/renderer/state/projects.ts b/src/renderer/state/projects.ts
index f12be87a6..93585bab8 100644
--- a/src/renderer/state/projects.ts
+++ b/src/renderer/state/projects.ts
@@ -140,6 +140,30 @@ interface ProjectsState {
/** Restores a closed project and makes it active. No-op if the id is unknown. */
reopenProject(id: string): void
+ /**
+ * Registers (or finds) the project for a local directory WITHOUT activating it — the store half
+ * of the `open-project` control verb (issue #338, spec §2.1 steps 2–4). The human paths
+ * (`openFolderProject`/`adoptProject`/`reopenProject`) all set `activeProjectId`; an agent verb
+ * must not travel the user's view (spec P6), so this action NEVER writes it, in any branch.
+ *
+ * `resolvedCwd` is main's already-validated, `path.resolve`d form (spec P7) — this action only
+ * re-applies the trailing-slash normalization so the exact-string dedupe cannot be split by a
+ * cosmetic slash. Branches, in order:
+ * - idempotent hit (B1): a project with this cwd is returned as-is; a `closed` one is
+ * un-closed (its tab reappears) without activation. `name`/`color` are NOT applied — an
+ * existing project's identity is never mutated on an agent's say-so.
+ * - adopt: `probed` (the folder's own .nodeterm/project.json, from `probeFolder`) is added,
+ * defending against an id collision exactly as `adoptProject` does (derived id, nodes kept).
+ * - create: the same `createProject` factory `addProject` uses; `name` defaults to the folder
+ * basename, `color` applies on create only.
+ */
+ registerProject(input: {
+ resolvedCwd: string
+ name?: string
+ color?: string
+ probed?: Project
+ }): { project: Project; created: boolean; adopted: boolean }
+
toWorkspace(): Workspace
}
@@ -557,6 +581,49 @@ export const useProjects = create<ProjectsState>((set, get) => ({
})
},
+ registerProject({ resolvedCwd, name, color, probed }) {
+ // The same exact-match rule as `openFolderProject` (a folder maps to one project), with the
+ // trailing slash stripped so `/a/b/` and `/a/b` cannot mint two tabs for one directory.
+ // Root stays '/': stripping it to '' would match every cwd-less project.
+ const cwd = resolvedCwd.length > 1 ? resolvedCwd.replace(/\/+$/, '') : resolvedCwd
+ const existing = get().projects.find((p) => p.cwd === cwd)
+ if (existing) {
+ if (existing.closed) {
+ // Un-close WITHOUT activation — reopenProject also activates, which is the exact
+ // mutation the register tests are checked against (spec P6).
+ set((s) => ({
+ projects: s.projects.map((p) => (p.id === existing.id ? { ...p, closed: false } : p))
+ }))
+ }
+ return {
+ project: get().projects.find((p) => p.id === existing.id) ?? existing,
+ created: false,
+ adopted: false
+ }
+ }
+ if (probed) {
+ // adoptProject's collision defense verbatim (deterministic in (id, folder)) — but appended
+ // WITHOUT the `activeProjectId` write that makes adoptProject a human path.
+ const taken = get().projects.some((p) => p.id === probed.id)
+ const adopted = taken
+ ? {
+ ...probed,
+ id: derivedProjectId(probed.id, collisionSeed(probed), (c) =>
+ get().projects.some((p) => p.id === c))
+ }
+ : probed
+ set((s) => ({ projects: [...s.projects, adopted] }))
+ return { project: adopted, created: false, adopted: true }
+ }
+ const fallbackName = cwd.split('/').filter(Boolean).pop() || 'Project'
+ const project = {
+ ...createProject(get().projects.length, name ?? fallbackName, cwd),
+ ...(color ? { color } : {})
+ }
+ set((s) => ({ projects: [...s.projects, project] }))
+ return { project, created: true, adopted: false }
+ },
+
toWorkspace() {
const { projects, activeProjectId } = get()
// A relay tab (`remote`) is a live connection to another machine's project, never a
diff --git a/src/renderer/state/workspace.cold-open.test.ts b/src/renderer/state/workspace.cold-open.test.ts
index 20e7ed8ae..95ff60792 100644
--- a/src/renderer/state/workspace.cold-open.test.ts
+++ b/src/renderer/state/workspace.cold-open.test.ts
@@ -54,7 +54,7 @@ describe('cold-open pin (a): pendingLaunch survives the store↔live round-trip'
live.data.initialCommand = 'claude "do the thing"'
const state = flowToNodeStates([live])[0]
expect(state).not.toHaveProperty('initialCommand')
- expect((state as Record<string, unknown>).initialCommand).toBeUndefined()
+ expect((state as unknown as Record<string, unknown>).initialCommand).toBeUndefined()
})
})