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(relay): a host node id that names a prototype member writes nothing
review38f6c6a06537
diff --git a/docs/hosted-team-relay.md b/docs/hosted-team-relay.mdindex 843756b5b..5a312c7ba 100644--- a/docs/hosted-team-relay.md+++ b/docs/hosted-team-relay.md@@ -653,6 +653,9 @@ holds for a hosted team's tabs and for a Team Access relay tab alike: both openeither, so a read recorded on the host clears nothing here.- **No notification and no sound** for a team tab's sessions. A finished turn, or a node that needsinput, marks the node unread unless this window has focus on that very node.+- **The host's node ids are input.** An event or a snapshot entry whose node id is not a safe id,+ or is a name `Object.prototype` owns (`__proto__`, `constructor`, …), is dropped whole+ (`isHostNodeId`) before it reaches the tab's table.- **Where it shows (v1):** the terminal node (badge, unread dot, session chip), the sessionssidebar, the project tab's unread dot, and kanban cards and the card modal.- **What stays off on a team tab's node** (`relayStatusGates`, `terminal/relay-status-gates.ts`):diff --git a/src/renderer/session/relay-agent-status.test.ts b/src/renderer/session/relay-agent-status.test.tsindex f303e8a4e..c42f7029c 100644--- a/src/renderer/session/relay-agent-status.test.ts+++ b/src/renderer/session/relay-agent-status.test.ts@@ -9,7 +9,7 @@ import { createAgentStatusSession, STALE_WORKING_MS, type AgentNodeStatus } fromimport { applyAgentEvent } from '../state/applyAgentEvent'import { useAgentNodes } from '../state/agentNodes'import { playSfx } from '../lib/sfx'-import { wireRelayAgentStatus } from './relay-agent-status'+import { applyAgentStatusSnapshot, isHostNodeId, wireRelayAgentStatus } from './relay-agent-status'// The chime is an interrupt the relay path must never raise. The wiring does not import it; the// spy makes a future import that plays it fail here.@@ -381,6 +381,81 @@ describe('wireRelayAgentStatus — the snapshot a late joiner starts from', () =})})+describe('wireRelayAgentStatus — a host node id that names a prototype member', () => {+ // The fields a store write would put on the object a bare `byId[id]` lookup answers.+ const FIELDS = ['stateAt', 'lastSeen', 'stateVerified', 'sessionEnded', 'state', 'agentId', 'sessionId', 'unread']+ const PROTO_IDS = ['__proto__', 'constructor']+ // What `byId['__proto__']` / `byId['constructor']` answer with no own entry.+ const targets = (): object[] => [Object.prototype, Object]+ const polluted = (): string[] =>+ targets().flatMap((t) => FIELDS.filter((k) => Object.hasOwn(t, k)).map((k) => `${t === Object ? 'Object' : 'Object.prototype'}.${k}`))++ afterEach(() => {+ // A regression must fail THIS test, not every test that runs after it in the worker.+ for (const t of targets()) for (const k of FIELDS) delete (t as Record<string, unknown>)[k]+ })++ it('isHostNodeId accepts the ids the app mints and refuses prototype names and unsafe ids', () => {+ for (const ok of ['term-node-a', 'term-mh3k2x-12', '3f2a9c1e-7b4d-4e8a-9c0f-1a2b3c4d5e6f', 'a.b_c-d']) {+ expect(isHostNodeId(ok), ok).toBe(true)+ }+ for (const bad of ['__proto__', 'constructor', 'toString', 'hasOwnProperty', 'valueOf', '', '.', '..', 'a b', 'a/b']) {+ expect(isHostNodeId(bad), bad).toBe(false)+ }+ for (const bad of [undefined, null, 42, {}]) expect(isHostNodeId(bad)).toBe(false)+ })++ it('a live event for __proto__ or constructor writes nothing and leaves the prototypes alone', () => {+ const { api, fire } = fakeApi()+ const stop = wireRelayAgentStatus(api, status, { hasFocus: () => false })+ fire(working) // a real node, so the table is not empty+ const before = status.store.getState().byId+ for (const nodeId of PROTO_IDS) {+ // The reviewer's frame: a session event with no agent id takes the store's same-state path.+ fire({ nodeId, kind: 'session', sessionPhase: 'end' } as NormalizedAgentEvent)+ fire({ nodeId, kind: 'session', sessionPhase: 'start', sessionTitle: 'x' } as NormalizedAgentEvent)+ fire(ev({ nodeId, state: 'working', newTurn: true, sessionId: 's-evil' }))+ fire(ev({ nodeId, state: 'done', lastMessage: 'ok' })) // would raise an alert, so a markUnread+ fire(ev({ nodeId, kind: 'background-task' }))+ }+ expect(polluted()).toEqual([])+ expect(Object.hasOwn(Object.prototype, 'stateAt')).toBe(false)+ expect(Object.hasOwn(Object.prototype, 'lastSeen')).toBe(false)+ expect(Object.hasOwn(Object.prototype, 'stateVerified')).toBe(false)+ const byId = status.store.getState().byId+ expect(byId).toBe(before) // not one store write+ for (const nodeId of PROTO_IDS) expect(Object.hasOwn(byId, nodeId), nodeId).toBe(false)+ expect(Object.keys(byId)).toEqual([N])+ stop()+ })++ it('a snapshot entry for __proto__ or constructor is skipped, and the rest is written', async () => {+ const { api, resolve } = fakeApi()+ const stop = wireRelayAgentStatus(api, status, { hasFocus: () => false })+ resolve([+ { nodeId: '__proto__', state: 'working', agentId: 'claude', sessionId: 's-evil', ageMs: 1000 },+ { nodeId: 'constructor', state: 'blocked', agentId: 'codex' },+ { nodeId: 'a', state: 'working', agentId: 'claude' }+ ])+ await flush()+ const byId = status.store.getState().byId+ expect(Object.keys(byId)).toEqual(['a'])+ for (const nodeId of PROTO_IDS) expect(Object.hasOwn(byId, nodeId), nodeId).toBe(false)+ expect(polluted()).toEqual([])+ stop()+ // The applier on its own says how many it wrote: the two refused entries are not counted.+ const other = createAgentStatusSession(undefined)+ const wrote = applyAgentStatusSnapshot(+ other.store,+ [{ nodeId: '__proto__', state: 'done' }, { nodeId: 'constructor' }, { nodeId: 'toString', state: 'working' }],+ new Set()+ )+ expect(wrote).toBe(0)+ expect(other.store.getState().byId).toEqual({})+ expect(polluted()).toEqual([])+ })+})+/** The byId table with only `account.remote` removed — everything else must match exactly. */function stripAccountRemote(byId: Record<string, AgentNodeStatus>): Record<string, AgentNodeStatus> {const out: Record<string, AgentNodeStatus> = {}diff --git a/src/renderer/session/relay-agent-status.ts b/src/renderer/session/relay-agent-status.tsindex 086b5a84a..7527d1068 100644--- a/src/renderer/session/relay-agent-status.ts+++ b/src/renderer/session/relay-agent-status.ts@@ -24,12 +24,32 @@import type { StoreApi } from 'zustand'import type { AgentStatusSnapshotEntry } from '@shared/agent-status-snapshot'import type { NodeTerminalApi } from '@shared/types'+import { isSafeNodeId } from '@shared/safe-id'import type { AgentNodeStatus, AgentStatusSession, AgentStatusStore } from '../state/agentStatus'import { applyAgentEvent } from '../state/applyAgentEvent'/** The Canvas sweep interval for the default store's stale-working decay. */const SWEEP_MS = 60_000+/**+ * Is this host-sent node id one this tab may use as a key of its `byId` table? Every relay write+ * (a live event and a snapshot entry alike) passes through this first, and anything it refuses is+ * dropped whole.+ *+ * `isSafeNodeId` alone is not enough here: its charset admits `__proto__` and `constructor`, and so+ * does every other name `Object.prototype` owns (`toString`, `hasOwnProperty`, …). Looked up on a+ * plain object, such a name answers the PROTOTYPE's value when the table has no own entry, and the+ * store's same-state fast path then writes into that value in place: one host frame+ * `{kind:'session', nodeId:'__proto__', sessionPhase:'end'}` defined `stateAt`, `lastSeen` and+ * `stateVerified` on `Object.prototype` for the whole renderer (every status entry then inherited a+ * `lastSeen`, which the default store's clock save writes to disk), and `constructor` did the same+ * to `Object`. No id the app mints is one of those names, so refusing them costs a real node+ * nothing. The host is another machine, so its ids are input, never trusted.+ */+export function isHostNodeId(nodeId: unknown): nodeId is string {+ return typeof nodeId === 'string' && isSafeNodeId(nodeId) && !Object.hasOwn(Object.prototype, nodeId)+}+export interface RelayAgentStatusDeps {/** Whether this window has focus. Default: `document.hasFocus()` (false without a document). */hasFocus?: () => boolean@@ -39,7 +59,7 @@ export interface RelayAgentStatusDeps {/*** Write the entries no live event has touched; never stamps a TRANSITION clock. Returns how many it- * wrote. `now` is this machine's clock.+ * wrote. `now` is this machine's clock. An entry whose node id `isHostNodeId` refuses is skipped.** One `setState` for the whole snapshot. Each entry merges only the fields it carries (`state`,* `agentId`, `sessionId`, `account`) onto whatever the node already has, so an identity-only entry@@ -71,7 +91,7 @@ export function applyAgentStatusSnapshot(const writes = new Map<string, AgentNodeStatus>()for (const entry of entries) {const nodeId = entry?.nodeId- if (typeof nodeId !== 'string' || !nodeId || liveSeen.has(nodeId)) continue+ if (!isHostNodeId(nodeId) || liveSeen.has(nodeId)) continueconst existing = writes.get(nodeId) ?? (Object.hasOwn(prev, nodeId) ? prev[nodeId] : undefined)const merged: AgentNodeStatus = { ...existing, unread: existing?.unread ?? false }if (entry.state !== undefined) {@@ -109,6 +129,8 @@ export function wireRelayAgentStatus(const unsubscribe = api.onAgentStatus((e) => {if (disposed) return+ // Before anything touches a table keyed by it (see `isHostNodeId`).+ if (!isHostNodeId(e?.nodeId)) returnif (e.kind !== 'subagent-start' && e.kind !== 'subagent-end') liveSeen.add(e.nodeId)const { nodeId, alert } = applyAgentEvent(store, null, e, { observationRemote: true })if (!alert) returndiff --git a/src/renderer/state/agentStatus.test.ts b/src/renderer/state/agentStatus.test.tsindex 2e54efe8d..afd7bd5ed 100644--- a/src/renderer/state/agentStatus.test.ts+++ b/src/renderer/state/agentStatus.test.ts@@ -435,3 +435,43 @@ describe('onHookEvent — a per-node pulse for EVERY hook event, same-state onesunsub()})})++describe('same-state fast path — own entries only', () => {+ // The fields the in-place refresh writes, and the objects a bare `byId[id]` lookup answers for a+ // prototype name with no own entry: `Object.prototype`, `Object`, `Object.prototype.toString`.+ const FIELDS = ['stateAt', 'lastSeen', 'stateVerified']+ const targets = (): object[] => [Object.prototype, Object, Object.prototype.toString]+ const polluted = (): string[] =>+ targets().flatMap((t, i) => FIELDS.filter((k) => Object.hasOwn(t, k)).map((k) => `${i}.${k}`))+ afterEach(() => {+ // A regression must fail THIS test, not every later test in the worker.+ for (const t of targets()) for (const k of FIELDS) delete (t as Record<string, unknown>)[k]+ })++ it('a same-state event for an id with no own entry never writes into the prototype', () => {+ const { store } = createAgentStatusSession(undefined)+ for (const id of ['__proto__', 'constructor', 'toString']) {+ // Unknown → unknown with no agent id: same-state by construction, so the in-place path.+ store.getState().setState(id, undefined, undefined)+ store.getState().setState(id, undefined, undefined, undefined, undefined, true)+ }+ expect(polluted()).toEqual([])+ expect(Object.hasOwn(Object.prototype, 'stateAt')).toBe(false)+ expect(Object.hasOwn(Object.prototype, 'lastSeen')).toBe(false)+ expect(Object.hasOwn(Object.prototype, 'stateVerified')).toBe(false)+ })++ it('a real node still refreshes in place (no new table, freshness and evidence move)', () => {+ const { store } = createAgentStatusSession(undefined)+ const id = nid()+ store.getState().setState(id, 'working', 'claude', true, undefined, true)+ const table = store.getState().byId+ const t0 = table[id].stateAt+ vi.advanceTimersByTime(1000)+ store.getState().setState(id, 'working', 'claude')+ expect(store.getState().byId).toBe(table)+ expect(store.getState().byId[id].stateAt).toBe((t0 ?? 0) + 1000)+ expect(store.getState().byId[id].lastSeen?.at).toBe((t0 ?? 0) + 1000)+ expect(store.getState().byId[id].stateVerified).toBe(false)+ })+})diff --git a/src/renderer/state/agentStatus.ts b/src/renderer/state/agentStatus.tsindex e7776e4f3..b4fe77878 100644--- a/src/renderer/state/agentStatus.ts+++ b/src/renderer/state/agentStatus.ts@@ -810,7 +810,14 @@ export function createAgentStatusSession() {// Same-state event: refresh freshness in place — stateAt is never rendered, and a// new object here would re-render every node header on each tool event.- if (s.byId[id]?.lastSeen?.restored) {+ // OWN entries only: for an id with no entry of its own, a plain lookup of a name+ // `Object.prototype` owns (`__proto__`, `constructor`, …) answers the prototype's value,+ // and the in-place writes below would then land on `Object.prototype` (or `Object`) for+ // the whole renderer. A relay host's ids are refused before they get here+ // (`isHostNodeId`); this is the store's own guard, and for every real id it is the same+ // lookup as before.+ const own = Object.hasOwn(s.byId, id) ? s.byId[id] : undefined+ if (own?.lastSeen?.restored) {// The FIRST event after a restart for a node whose state is still unknown (a// SessionStart from a cold-restore resume, say) is same-state by construction. Not in// place: the row must lose its "restored" label and re-sort NOW, not whenever another@@ -820,20 +827,20 @@ export function createAgentStatusSession(return {byId: {...s.byId,- [id]: { ...s.byId[id], stateAt: now, stateVerified: verified === true, lastSeen: { at: now, state } }+ [id]: { ...own, stateAt: now, stateVerified: verified === true, lastSeen: { at: now, state } }}}}- if (s.byId[id]) {- s.byId[id].stateAt = now+ if (own) {+ own.stateAt = now// "Last seen" is the last hook EVENT, so a same-state one moves it too — in place, like// `stateAt`, and saved on the trailing debounce rather than once per tool event.- s.byId[id].lastSeen = { at: now, state }+ own.lastSeen = { at: now, state }scheduleClockSave()// The evidence rides along, in place and for the same reason: a re-assert of the SAME// state by a legacy POST must not leave an earlier `true` standing, or this copy would// disagree with the mirror the gate actually reads.- s.byId[id].stateVerified = verified === true+ own.stateVerified = verified === true}return s}