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(relay): useActiveSessionPresence — the active session's presence store (collab Task 1)

enes4e1fca654d12
diff --git a/src/renderer/session/session.test.ts b/src/renderer/session/session.test.ts
index a66e5539e..07314b353 100644
--- a/src/renderer/session/session.test.ts
+++ b/src/renderer/session/session.test.ts
@@ -14,7 +14,9 @@ import {
setMeAll,
setSessionStatus,
takeSessionOffline,
+ activeSessionPresence,
} from './session'
+import { defaultPresence } from '../state/presence'
import type { NodeTerminalApi } from '@shared/types'
import type { PeerIdentity } from '@shared/presence'
@@ -242,6 +244,37 @@ describe('takeSessionOffline (Stage 4 Task 7 — an INVOLUNTARY drop, not a user
})
})
+describe('activeSessionPresence (Task 1 — the ACTIVE session presence, non-hook accessor)', () => {
+ it('returns the ACTIVE relay session\'s presence — a distinct instance from local', () => {
+ const local = createSession('local', fakeApi, 'This Mac')
+ const relay = createSession('relay', { marker: 'relay' } as unknown as NodeTerminalApi, "Ayşe's Mac")
+ setActiveSession(relay.id)
+ const p = activeSessionPresence()
+ expect(p).toBe(getSessionStores(relay.id).presence)
+ expect(p).not.toBe(getSessionStores(local.id).presence)
+ })
+
+ it('returns the LOCAL session\'s presence when local is active', () => {
+ const local = createSession('local', fakeApi, 'This Mac')
+ createSession('relay', { marker: 'relay' } as unknown as NodeTerminalApi, "Ayşe's Mac")
+ setActiveSession(local.id)
+ expect(activeSessionPresence()).toBe(getSessionStores(local.id).presence)
+ })
+
+ it('falls back to the local session\'s presence when no session is active (never throws)', () => {
+ const local = createSession('local', fakeApi, 'This Mac')
+ // activeId is null (setActiveSession never called) — must not throw, resolves to local.
+ expect(() => activeSessionPresence()).not.toThrow()
+ expect(activeSessionPresence()).toBe(getSessionStores(local.id).presence)
+ })
+
+ it('falls back to defaultPresence when there is neither an active nor a local session', () => {
+ // node-env safety: with an empty registry the accessor must still return a usable presence.
+ expect(() => activeSessionPresence()).not.toThrow()
+ expect(activeSessionPresence()).toBe(defaultPresence)
+ })
+})
+
describe('setMeAll (obligation 2 — a rename re-helloes EVERY live session)', () => {
it('re-helloes and updates identity on all registered sessions, not just the active one', () => {
const a = apiWithHelloSpy('a')
diff --git a/src/renderer/session/session.ts b/src/renderer/session/session.ts
index d053cdece..fdce80050 100644
--- a/src/renderer/session/session.ts
+++ b/src/renderer/session/session.ts
@@ -1,7 +1,7 @@
import { createContext, createElement, useContext, type ReactNode } from 'react'
import type { NodeTerminalApi } from '@shared/types'
import type { PeerIdentity } from '@shared/presence'
-import { createPresenceSession, type PresenceSession } from '../state/presence'
+import { createPresenceSession, defaultPresence, type PresenceSession } from '../state/presence'
import { agentStatusForApi, type AgentStatusSession } from '../state/agentStatus'
export type SessionSource = 'local' | 'relay' | 'server'
@@ -218,6 +218,33 @@ export function useSessionStores(): SessionStores {
return getSessionStores(useSession().id)
}
+/** The presence store for a session id, with the render-safe fallback the two accessors below share:
+ * an unknown/not-yet-registered id (a transient render before its session exists) resolves to the
+ * LOCAL session's presence — which in the real app IS `defaultPresence` (the exact object the ~40
+ * static `state/presence` imports use), so a local tab is byte-identical. Never throws. */
+function presenceForSession(sessionId: string | null): PresenceSession {
+ if (sessionId) {
+ const e = SESSIONS.get(sessionId)
+ if (e) return e.stores.presence
+ }
+ const local = SESSIONS.get('local')
+ return local ? local.stores.presence : defaultPresence
+}
+
+/** The ACTIVE session's presence store, for components rendered UNDER the Canvas `SessionProvider`
+ * (so `useSession()` resolves the active session, not the app-chrome default). A local tab yields
+ * `defaultPresence`; a relay tab yields the relay session's presence. Allocation-free — returns the
+ * memoized `PresenceSession` directly — and never throws in render (see `presenceForSession`). */
+export function useActiveSessionPresence(): PresenceSession {
+ return presenceForSession(useSession().id)
+}
+
+/** Non-hook counterpart of `useActiveSessionPresence` for imperative (non-render) Canvas use: the
+ * ACTIVE session's presence, read straight from the registry (`activeId`), with the same fallback. */
+export function activeSessionPresence(): PresenceSession {
+ return presenceForSession(activeId)
+}
+
export function SessionProvider({ session, children }: { session: WorkspaceSession; children: ReactNode }) {
return createElement(SessionContext.Provider, { value: session }, children)
}