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
fix(session): brand placeholder store types, make createSession idempotent per id
enesca39b912521c
diff --git a/src/renderer/session/localSession.ts b/src/renderer/session/localSession.tsindex 354afbb0e..6ed87d706 100644--- a/src/renderer/session/localSession.ts+++ b/src/renderer/session/localSession.ts@@ -1,5 +1,12 @@import { createSession, setActiveSession, type WorkspaceSession } from './session'-/** The single local session: its api is the preload object by identity. Built once at boot. */+/** The single local session: its api is the preload object by identity. Built once at boot.+ *+ * Reading `window.nodeTerminal` at module load time is safe ONLY because of the boot order in+ * main.tsx: on desktop the preload defines it before any renderer script runs, and on the Server+ * Edition main.tsx installs the WS bridge and only then `await import('./boot')` — and boot is+ * the only importer of App (which imports this module). A static import of App from main.tsx+ * would hoist this line ahead of the bridge install and silently capture `undefined` by identity+ * in the browser. Keep the dynamic import. */export const localSession: WorkspaceSession = createSession('local', window.nodeTerminal, 'This Mac')setActiveSession(localSession.id)diff --git a/src/renderer/session/session.test.ts b/src/renderer/session/session.test.tsindex 13ab125e2..0930463f0 100644--- a/src/renderer/session/session.test.ts+++ b/src/renderer/session/session.test.ts@@ -1,10 +1,21 @@-import { describe, it, expect } from 'vitest'-import { createSession, getSessionStores, getActiveSession, setActiveSession, activeSessionApi } from './session'+import { describe, it, expect, beforeEach } from 'vitest'+import {+ createSession,+ getSessionStores,+ getActiveSession,+ setActiveSession,+ activeSessionApi,+ resetSessionsForTest,+} from './session'import type { NodeTerminalApi } from '@shared/types'// A distinctive object we can assert is preserved BY IDENTITY (the behavior-unchanged guarantee).const fakeApi = { marker: 'preload' } as unknown as NodeTerminalApi+beforeEach(() => {+ resetSessionsForTest()+})+describe('createSession', () => {it('returns the pinned 5-field shape and preserves the api object by identity', () => {const s = createSession('local', fakeApi, 'This Mac')@@ -24,6 +35,31 @@ describe('createSession', () => {expect(stores.agentStatus).toBeDefined()})+ it('is idempotent per id: a duplicate createSession returns the existing session and does NOT rebuild its stores', () => {+ // After Task 2 buildStores() constructs a REAL presence store with a live subscription.+ // A second createSession('local', …) (hot reload, a test helper, a reconnect path) must+ // NOT build a second store — that would violate Stage 1's "exactly one subscriber" invariant.+ const first = createSession('local', fakeApi, 'This Mac')+ const storesBefore = getSessionStores(first.id)+ const again = createSession('local', { other: true } as unknown as NodeTerminalApi, 'Renamed')+ expect(again).toBe(first) // the existing session object, by identity+ expect(again.api).toBe(fakeApi) // the first registration wins — nothing was overwritten+ expect(getSessionStores(first.id)).toBe(storesBefore) // stores were not rebuilt+ expect(getSessionStores(first.id).presence).toBe(storesBefore.presence)+ })++ it('gives distinct remote sessions distinct ids and distinct store objects', () => {+ // The property Tasks 2/4 and 4c depend on: per-session stores are never shared.+ const relay = createSession('relay', fakeApi, "Ayşe's Mac")+ const server = createSession('server', fakeApi, 'prod-box')+ expect(relay.id).not.toBe(server.id)+ const relayStores = getSessionStores(relay.id)+ const serverStores = getSessionStores(server.id)+ expect(relayStores).not.toBe(serverStores)+ expect(relayStores.presence).not.toBe(serverStores.presence)+ expect(relayStores.agentStatus).not.toBe(serverStores.agentStatus)+ })+it('tracks the active session and exposes its api to non-components', () => {const s = createSession('local', fakeApi, 'This Mac')setActiveSession(s.id)diff --git a/src/renderer/session/session.ts b/src/renderer/session/session.tsindex bd7ef4c35..1d7ecdd50 100644--- a/src/renderer/session/session.ts+++ b/src/renderer/session/session.ts@@ -5,8 +5,15 @@ import type { NodeTerminalApi } from '@shared/types'// presence store type (`../state/presence`) and Task 4 does the same for `AgentStatusSession`// (`../state/agentStatus`) — those stores are not per-instance factories yet, so importing them// here would be wrong. Deliberately not exported: nothing may depend on the placeholder shape.-type PresenceSession = object-type AgentStatusSession = object+// BRANDED on purpose: a bare `object` would accept a real store, so wiring one into+// `buildStores()` while forgetting to swap the alias would typecheck cleanly and leave the+// slot typed as a placeholder forever. The unique-symbol brand makes that assignment a compile+// error at the exact line — replacing the alias is the only way to satisfy the compiler.+declare const PLACEHOLDER: unique symbol+/** Task 2 replaces this with the real per-session presence store type. */+type PresenceSession = { readonly [PLACEHOLDER]: 'replace in Task 2' }+/** Task 4 replaces this with the real per-session agent-status store type. */+type AgentStatusSession = { readonly [PLACEHOLDER]: 'replace in Task 4' }export type SessionSource = 'local' | 'relay' | 'server'@@ -39,16 +46,31 @@ let remoteSeq = 0/** Task 2/4 replace this with the real default instances for the local session. Until then a* minimal placeholder keeps the registry shape honest without pulling in the stores. */function buildStores(): SessionStores {- return { presence: {}, agentStatus: {} }+ return { presence: {} as PresenceSession, agentStatus: {} as AgentStatusSession }}+/** Idempotent per id: if the id already exists, the EXISTING session is returned and nothing is+ * rebuilt. After Task 2 `buildStores()` constructs a real presence store with a live+ * subscription — a duplicate call (hot reload, a test helper, a future reconnect path) must not+ * build a second one beside the first (Stage 1's "exactly one subscriber" invariant is per+ * session). Return-existing rather than throw: the duplicate caller wants "the session for this+ * id", and throwing would turn a benign HMR re-run of localSession.ts into a renderer crash. */export function createSession(source: SessionSource, api: NodeTerminalApi, label: string): WorkspaceSession {const id = source === 'local' ? 'local' : `${source}-${++remoteSeq}`+ const existing = SESSIONS.get(id)+ if (existing) return existing.sessionconst session: WorkspaceSession = { id, source, label, api, status: 'connected' }SESSIONS.set(id, { session, stores: buildStores() })return session}+/** Test-only: clears the registry so each test starts with no sessions. */+export function resetSessionsForTest(): void {+ SESSIONS.clear()+ activeId = null+ remoteSeq = 0+}+export function getSessionStores(sessionId: string): SessionStores {const e = SESSIONS.get(sessionId)if (!e) throw new Error(`[session] no session ${sessionId}`)