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(session): WorkspaceSession + context + registry, mount local session

enesa4c9634ed416
diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx
index a48a085cd..99d2a214e 100644
--- a/src/renderer/App.tsx
+++ b/src/renderer/App.tsx
@@ -1,13 +1,17 @@
import { ReactFlowProvider } from '@xyflow/react'
import { Canvas } from './canvas/Canvas'
import { PromptDialogHost } from './components/promptDialog'
+import { SessionProvider } from './session/session'
+import { localSession } from './session/localSession'
export default function App() {
return (
- <ReactFlowProvider>
- <Canvas />
- {/* In-app window.prompt replacement (Electron has no prompt); driven by promptDialog(). */}
- <PromptDialogHost />
- </ReactFlowProvider>
+ <SessionProvider session={localSession}>
+ <ReactFlowProvider>
+ <Canvas />
+ {/* In-app window.prompt replacement (Electron has no prompt); driven by promptDialog(). */}
+ <PromptDialogHost />
+ </ReactFlowProvider>
+ </SessionProvider>
)
}
diff --git a/src/renderer/session/localSession.ts b/src/renderer/session/localSession.ts
new file mode 100644
index 000000000..354afbb0e
--- /dev/null
+++ b/src/renderer/session/localSession.ts
@@ -0,0 +1,5 @@
+import { createSession, setActiveSession, type WorkspaceSession } from './session'
+
+/** The single local session: its api is the preload object by identity. Built once at boot. */
+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.ts
new file mode 100644
index 000000000..13ab125e2
--- /dev/null
+++ b/src/renderer/session/session.test.ts
@@ -0,0 +1,37 @@
+import { describe, it, expect } from 'vitest'
+import { createSession, getSessionStores, getActiveSession, setActiveSession, activeSessionApi } 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
+
+describe('createSession', () => {
+ it('returns the pinned 5-field shape and preserves the api object by identity', () => {
+ const s = createSession('local', fakeApi, 'This Mac')
+ expect(s).toMatchObject({ source: 'local', label: 'This Mac', status: 'connected' })
+ expect(typeof s.id).toBe('string')
+ expect(s.api).toBe(fakeApi) // identity, not a copy — this is the whole guarantee
+ })
+
+ it('gives the local session the fixed id "local"', () => {
+ expect(createSession('local', fakeApi, 'This Mac').id).toBe('local')
+ })
+
+ it('registers per-session stores addressable by id', () => {
+ const s = createSession('local', fakeApi, 'This Mac')
+ const stores = getSessionStores(s.id)
+ expect(stores.presence).toBeDefined()
+ expect(stores.agentStatus).toBeDefined()
+ })
+
+ it('tracks the active session and exposes its api to non-components', () => {
+ const s = createSession('local', fakeApi, 'This Mac')
+ setActiveSession(s.id)
+ expect(getActiveSession()).toBe(s)
+ expect(activeSessionApi()).toBe(fakeApi)
+ })
+
+ it('throws for an unknown session id', () => {
+ expect(() => getSessionStores('nope')).toThrow()
+ })
+})
diff --git a/src/renderer/session/session.ts b/src/renderer/session/session.ts
new file mode 100644
index 000000000..bd7ef4c35
--- /dev/null
+++ b/src/renderer/session/session.ts
@@ -0,0 +1,86 @@
+import { createContext, createElement, useContext, type ReactNode } from 'react'
+import type { NodeTerminalApi } from '@shared/types'
+
+// Placeholder store-instance types. Task 2 replaces `PresenceSession` with the real per-session
+// 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
+
+export type SessionSource = 'local' | 'relay' | 'server'
+
+/** A connection to one core plus that core's API. Every project tab belongs to a session.
+ * For the local session `api` IS window.nodeTerminal — the same function references used today. */
+export interface WorkspaceSession {
+ id: string
+ source: SessionSource
+ label: string
+ api: NodeTerminalApi
+ status: 'connected' | 'connecting' | 'offline'
+}
+
+/** The per-session renderer store instances (the multiplayer tables that must not be shared
+ * across sessions). Held in the registry beside the session, addressable by session id. */
+export interface SessionStores {
+ presence: PresenceSession
+ agentStatus: AgentStatusSession
+}
+
+interface Entry {
+ session: WorkspaceSession
+ stores: SessionStores
+}
+
+const SESSIONS = new Map<string, Entry>()
+let activeId: string | null = null
+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: {} }
+}
+
+export function createSession(source: SessionSource, api: NodeTerminalApi, label: string): WorkspaceSession {
+ const id = source === 'local' ? 'local' : `${source}-${++remoteSeq}`
+ const session: WorkspaceSession = { id, source, label, api, status: 'connected' }
+ SESSIONS.set(id, { session, stores: buildStores() })
+ return session
+}
+
+export function getSessionStores(sessionId: string): SessionStores {
+ const e = SESSIONS.get(sessionId)
+ if (!e) throw new Error(`[session] no session ${sessionId}`)
+ return e.stores
+}
+
+export function setActiveSession(sessionId: string): void {
+ if (!SESSIONS.has(sessionId)) throw new Error(`[session] no session ${sessionId}`)
+ activeId = sessionId
+}
+
+export function getActiveSession(): WorkspaceSession {
+ if (!activeId) throw new Error('[session] no active session')
+ return SESSIONS.get(activeId)!.session
+}
+
+export function activeSessionApi(): NodeTerminalApi {
+ return getActiveSession().api
+}
+
+export const SessionContext = createContext<WorkspaceSession | null>(null)
+
+export function useSession(): WorkspaceSession {
+ const s = useContext(SessionContext)
+ if (!s) throw new Error('[session] useSession() outside a SessionProvider')
+ return s
+}
+
+export function useSessionStores(): SessionStores {
+ return getSessionStores(useSession().id)
+}
+
+export function SessionProvider({ session, children }: { session: WorkspaceSession; children: ReactNode }) {
+ return createElement(SessionContext.Provider, { value: session }, children)
+}