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
test: acceptance for agent status on a joined team tab
reviewac9820586817
diff --git a/test/acceptance/hosted-relay-agent-status.test.ts b/test/acceptance/hosted-relay-agent-status.test.tsnew file mode 100644index 000000000..9e7ff5c70--- /dev/null+++ b/test/acceptance/hosted-relay-agent-status.test.ts@@ -0,0 +1,316 @@+/**+ * ACCEPTANCE (bug D): on a team tab, agent status reaches the tab's OWN relay session store.+ *+ * A desktop joined to a Server Edition team over the E2EE relay never showed RUNNING / NEEDS YOU /+ * unread on its agent nodes. The host sends `agent:status`, the joiner's relay RpcClient receives+ * it, and nothing on the joiner listens: the only subscriber is Canvas's listener on the LOCAL+ * `window.nodeTerminal`, which writes the DEFAULT agent-status store. The relay session's own store+ * (`SessionStores.agentStatus`) is never written.+ *+ * The chain, end to end, with every layer REAL except the relay carrier (an in-memory FrameTransport+ * that also plays the host's request handlers for the requests a hosted tab sends at open):+ *+ * claude hook (normalizeClaude) → Server Edition `wireAgentStatus` → `ServerPlatform.broadcast`+ * → the hosted relay peer's sink, filtered by `wrapSinkForRole` (owner, and viewer)+ * → the joiner's `buildRelayApi({hosted:true})` RpcClient → `openRelayTab` session + stores+ *+ * The tests marked `it.fails` state the intended behavior and fail until the fix lands; the+ * default-store test already holds (today nothing writes) and must keep holding after it.+ *+ * Runs in the NODE environment with a real jsdom window installed by hand (`installDom`), not in+ * vitest's jsdom environment: that one loads modules through Vite's client transform, which+ * refuses to bundle `node:sqlite` (imported by `core/cursor-chat.ts`, which the Server Edition's+ * `agent-status.ts` pulls in) on Node 22, whose `builtinModules` does not list prefix-only built-ins.+ * The renderer half still gets a real `document`, `localStorage` and `Storage`.+ */+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'+import { JSDOM } from 'jsdom'+import { ServerPlatform } from '../../src/server/platform-server'+import { wireAgentStatus } from '../../src/server/agent-status'+import { _resetForTest } from '../../src/core/agent-status-mirror'+import { wrapSinkForRole, type AccessContext } from '../../src/core/relay/access-policy'+import { testTmpDir } from '../../src/core/test-tmp'+import { normalizeClaude, type NormalizedAgentEvent } from '../../src/shared/agents/normalize'+import { IPC } from '../../src/shared/ipc'+import type { NodeTerminalApi, Project, RelayClientApi, Workspace } from '../../src/shared/types'+import type { FrameTransport } from '../../src/renderer/bridge/frame-transport'+import { buildRelayApi, type RelayApiHandle } from '../../src/renderer/bridge/relay-api'+import { handleRelayDrop, openRelayTab, type RelayTab } from '../../src/renderer/session/relay-tab'+import { getSessionStores, resetSessionsForTest } from '../../src/renderer/session/session'+import { useAgentStatus } from '../../src/renderer/state/agentStatus'++const NODE = 'term-musuoygd-979507b8'+const PROJECT = 'proj-alpha'++const ctxFor = (role: AccessContext['role']): AccessContext => ({+ role,+ sharedProjects: new Set([PROJECT]),+ projectsOfNode: (id) => (id === NODE ? [PROJECT] : []),+ nodeOfSession: () => undefined,+ projectCwds: () => ['/srv/alpha'],+ hostDataDir: '/var/lib/nodeterm-data',+ realpath: (p) => p,+ isFile: () => false+})++type ClosedListener = Parameters<RelayClientApi['onClosed']>[1]++/** The joiner's carrier for ONE connection. Records every frame the joiner sends (`sent`), answers+ * the requests a hosted tab makes at open the way the host's handlers would (anything else gets+ * `null`), and stands in for the preload's `relayClient.onClosed` for this connection so a test+ * can drop it (`drop`). */+class HostTransport implements FrameTransport {+ sent: Array<{ t: string; id?: number; method?: string }> = []+ private msgCb: ((data: string | Uint8Array) => void) | null = null+ /** Every live `relayClient.onClosed` listener for this connection, in registration order — the+ * RpcClient's carrier-closed hook among them, as `RelayFrameTransport.onClose` rides it. */+ private closedListeners: ClosedListener[] = []+ /** The preload's `relayClient.onClosed`, scoped to this connection. */+ readonly relayClient: Pick<RelayClientApi, 'onClosed'> = {+ onClosed: (_connectionId, listener) => {+ this.closedListeners.push(listener)+ return () => {+ this.closedListeners = this.closedListeners.filter((l) => l !== listener)+ }+ }+ }+ constructor(+ private readonly role: AccessContext['role'],+ private readonly ws: Workspace+ ) {}+ send(json: string): void {+ const m = JSON.parse(json) as { t: string; id?: number; method?: string }+ this.sent.push(m)+ if (m.t !== 'req') return+ const result =+ m.method === IPC.relayHostedSelf+ ? { role: this.role, label: '', hostLabel: 'srv' }+ : m.method === IPC.workspaceLoad+ ? this.ws+ : m.method === IPC.presenceHello+ ? { clientId: 7, peers: [] }+ : m.method === IPC.agentSubagentSnapshot+ ? []+ : null+ queueMicrotask(() => this.emit(JSON.stringify({ t: 'res', id: m.id, ok: true, result })))+ }+ onMessage(cb: (data: string | Uint8Array) => void): void {+ this.msgCb = cb+ }+ onClose(cb: () => void): void {+ this.relayClient.onClosed('', () => cb())+ }+ ready(): Promise<void> {+ return Promise.resolve()+ }+ /** One frame from the host, handed to the RpcClient. */+ emit(json: string): void {+ this.msgCb?.(json)+ }+ /** An involuntary relay drop: main reports the connection closed to every listener it has. The+ * host keeps sending to its sink, so what the joiner does with a late frame is still tested. */+ drop(): void {+ for (const l of [...this.closedListeners]) l()+ }+}++function hostWorkspace(): Workspace {+ const project = {+ id: PROJECT,+ name: 'alpha',+ cwd: '/srv/alpha',+ nodes: [+ {+ id: NODE,+ kind: 'terminal',+ position: { x: 0, y: 0 },+ width: 600,+ height: 400,+ data: { title: 'Claude Code', color: 'clay', agentId: 'claude', cwd: '/srv/alpha' }+ }+ ]+ } as unknown as Project+ return { version: 2, activeProjectId: PROJECT, projects: [project] } as unknown as Workspace+}++/** A minimal LOCAL preload (relay-api.test.ts's shape): only what buildRelayApi reads off it. */+function fakeLocalPreload(): NodeTerminalApi {+ return {+ githubControl: {},+ pty: { onData: () => () => {} },+ claude: { cliCaps: () => Promise.resolve({}), readTranscript: () => Promise.reject(new Error('x')) },+ relayClient: { disconnect: vi.fn() },+ ackDone: vi.fn()+ } as unknown as NodeTerminalApi+}++interface Peer {+ t: HostTransport+ handle: RelayApiHandle+ tab: RelayTab+}++interface Harness {+ /** Fire one claude hook POST at the host core, exactly as the hook server hands it to the shell. */+ hook(payload: Record<string, unknown>): void+ /** Drop the VIEWER's relay connection (main reports it closed). */+ drop(): void+ owner: Peer+ viewer: Peer+}++let h: Harness+let dom: JSDOM++/** The DOM globals the renderer half reads: the preload's `window`, focus, and web storage. */+function installDom(): void {+ dom = new JSDOM('', { url: 'http://localhost/' })+ vi.stubGlobal('window', dom.window)+ vi.stubGlobal('document', dom.window.document)+ vi.stubGlobal('localStorage', dom.window.localStorage)+ vi.stubGlobal('Storage', dom.window.Storage)+}++beforeAll(installDom)++afterAll(() => {+ vi.unstubAllGlobals()+ dom.window.close()+})++async function joinAs(platform: ServerPlatform, role: AccessContext['role'], conn: string): Promise<Peer> {+ const t = new HostTransport(role, hostWorkspace())+ // The host side of the peer: the hosted service attaches the relay sink, wrapped for the role.+ platform.attach(+ wrapSinkForRole(+ { sendText: (json) => t.emit(json), sendBinary: () => {} },+ () => ctxFor(role)+ )+ )+ const handle = buildRelayApi(conn, t, { hosted: true })+ const tab = await openRelayTab(conn, 'Team', {+ relayClient: t.relayClient,+ addProject: () => ({ id: 'placeholder' }),+ setActiveProject: () => {},+ buildApi: () => handle,+ hosted: true,+ activate: false,+ placeProjects: (ps) => ps.map((p) => p.id)+ })+ // What Canvas does once the tab is live: an involuntary drop greys the tab and takes its session+ // offline (`handleRelayDrop` → `takeSessionOffline`). openRelayTab's own `onClosed` listener only+ // guards the approval wait and is released once the connection is approved.+ const un = t.relayClient.onClosed(conn, () => {+ un()+ handleRelayDrop(tab, { setProjectUnavailable: () => {} })+ })+ return { t, handle, tab }+}++beforeEach(async () => {+ _resetForTest()+ resetSessionsForTest()+ ;(window as unknown as { nodeTerminal: NodeTerminalApi }).nodeTerminal = fakeLocalPreload()+ // The window is not focused, so a finished turn is unread.+ vi.spyOn(document, 'hasFocus').mockReturnValue(false)+ const dir = testTmpDir('nt-relay-status-')+ const platform = new ServerPlatform({ userDataDir: dir, appVersion: '0' })+ let normalized: ((e: NormalizedAgentEvent) => void) | undefined+ let raw: ((a: string, n: string, p: Record<string, unknown>, m: { verified: boolean }) => void) | undefined+ wireAgentStatus(platform, {+ hooks: {+ setListener: (cb) => {+ normalized = cb+ },+ setRawListener: (cb) => {+ raw = cb as typeof raw+ }+ }+ })+ const owner = await joinAs(platform, 'owner', 'conn-owner')+ const viewer = await joinAs(platform, 'viewer', 'conn-viewer')+ h = {+ hook: (payload) => {+ raw?.('claude', NODE, payload, { verified: false })+ const e = normalizeClaude({ nodeId: NODE, agentId: 'claude', payload })+ if (e) normalized?.(e)+ },+ drop: () => viewer.t.drop(),+ owner,+ viewer+ }+ // Harness guard: a relay session's store is never the default one, or the default-store test+ // below would contradict the per-session ones.+ for (const peer of [owner, viewer]) expect(relayStore(peer.tab)).not.toBe(useAgentStatus)+})++afterEach(() => {+ h.owner.tab.dispose()+ h.viewer.tab.dispose()+ resetSessionsForTest()+ _resetForTest()+ vi.restoreAllMocks()+ useAgentStatus.setState({ byId: {} })+ localStorage.clear()+ delete (window as unknown as { nodeTerminal?: NodeTerminalApi }).nodeTerminal+})++/** RpcClient delivers on a microtask, the host on a macrotask. */+const settle = async (): Promise<void> => {+ await new Promise((r) => setTimeout(r, 0))+ await new Promise((r) => setTimeout(r, 0))+}++const relayStore = (tab: RelayTab) => getSessionStores(tab.sessionId).agentStatus.store+// The two claude hook payloads: a prompt submitted, then the turn finished.+const working = { hook_event_name: 'UserPromptSubmit', session_id: 's1', prompt: 'hello' }+const done = { hook_event_name: 'Stop', session_id: 's1', last_assistant_message: 'done.' }++describe('hosted team tab: agent status reaches the relay session', () => {+ it.fails('a host hook event reaches each relay session store', async () => {+ h.hook(working)+ await settle()+ expect(relayStore(h.owner.tab).getState().byId[NODE]?.state).toBe('working')+ expect(relayStore(h.viewer.tab).getState().byId[NODE]?.state).toBe('working')+ h.hook(done)+ await settle()+ const v = relayStore(h.viewer.tab).getState().byId[NODE]+ expect(v?.state).toBe('done')+ expect(v?.unread).toBe(true) // window not focused+ expect(v?.sessionId).toBe('s1')+ })++ it('relay events leave the default store and its persisted keys untouched', async () => {+ useAgentStatus.getState().setSessionId(NODE, 'ssh-session-from-the-handed-off-project')+ const before = structuredClone(useAgentStatus.getState().byId[NODE])+ const setItem = vi.spyOn(Storage.prototype, 'setItem')+ h.hook(working)+ h.hook(done)+ await settle()+ expect(useAgentStatus.getState().byId[NODE]).toEqual(before)+ expect(setItem.mock.calls.filter(([k]) => String(k).startsWith('nodeterm.agentStatus'))).toEqual([])+ })++ it.fails('reading a finished relay session acks nothing on the host', async () => {+ h.hook(working)+ h.hook(done)+ await settle()+ expect(relayStore(h.viewer.tab).getState().byId[NODE]?.unread).toBe(true) // fails today: nothing wrote it+ relayStore(h.viewer.tab).getState().clearUnread(NODE)+ await settle()+ expect(h.viewer.t.sent.filter((m) => m.method === IPC.agentAckDone)).toEqual([])+ expect((window.nodeTerminal as unknown as { ackDone: ReturnType<typeof vi.fn> }).ackDone).not.toHaveBeenCalled()+ expect(relayStore(h.viewer.tab).getState().byId[NODE]?.unread).toBe(false)+ })++ it.fails('after a relay drop the tab reads unknown and later events write nothing', async () => {+ h.hook(working)+ await settle()+ expect(relayStore(h.viewer.tab).getState().byId[NODE]?.state).toBe('working')+ h.drop() // handleRelayDrop → takeSessionOffline+ expect(relayStore(h.viewer.tab).getState().byId[NODE]).toBeUndefined()+ h.hook(done)+ await settle()+ expect(relayStore(h.viewer.tab).getState().byId[NODE]).toBeUndefined()+ })+})