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(shared): harden the presence wire contract before anything imports it
enes99209a81ee80
diff --git a/src/shared/presence.test.ts b/src/shared/presence.test.tsindex 986086ffe..8752e6826 100644--- a/src/shared/presence.test.ts+++ b/src/shared/presence.test.ts@@ -6,7 +6,7 @@ import {peersOnProject,sanitizeIdentity,NAME_MAX_LEN,- CHAT_MAX_LEN,+ type PeerDiff,type PeerState} from './presence'@@ -29,8 +29,35 @@ describe('presence palette', () => {expect(nextFreeColor([])).toBe(PRESENCE_COLORS[0])expect(nextFreeColor([PRESENCE_COLORS[0]])).toBe(PRESENCE_COLORS[1])expect(nextFreeColor([PRESENCE_COLORS[1]])).toBe(PRESENCE_COLORS[0])- // Every color taken → wrap (a 9th peer reuses the 1st color rather than going colorless).- expect(PRESENCE_COLORS).toContain(nextFreeColor([...PRESENCE_COLORS]))+ })++ it('wraps by taken-count once every color is in use (the exact documented rule)', () => {+ // Every color taken → the (n+1)-th peer reuses PRESENCE_COLORS[taken.length % length]+ // rather than going colorless. Pin the exact color: "some palette color" would also pass+ // for a random-color implementation.+ const all = [...PRESENCE_COLORS]+ expect(nextFreeColor(all)).toBe(PRESENCE_COLORS[all.length % PRESENCE_COLORS.length])+ expect(nextFreeColor(all)).toBe(PRESENCE_COLORS[0])+ expect(nextFreeColor([...all, PRESENCE_COLORS[0]])).toBe(PRESENCE_COLORS[1])+ expect(nextFreeColor([...all, PRESENCE_COLORS[0], PRESENCE_COLORS[1]])).toBe(PRESENCE_COLORS[2])+ })++ it('is a readonly palette (no consumer can corrupt color assignment app-wide)', () => {+ // @ts-expect-error PRESENCE_COLORS is readonly — pushing into it must not typecheck.+ expect(() => PRESENCE_COLORS.push('#000000')).toBeDefined()+ // Callers stay unconstrained: a mutable array is still an acceptable `taken` argument.+ const mutable: string[] = [PRESENCE_COLORS[0]]+ expect(nextFreeColor(mutable)).toBe(PRESENCE_COLORS[1])+ })+})++describe('PeerDiff (wire contract)', () => {+ it('cannot rewrite the identity key through an update patch', () => {+ const ok: PeerDiff = { op: 'update', clientId: 3, patch: { focus: 'n1', chat: 'hi' } }+ expect(ok.op).toBe('update')+ // @ts-expect-error an update patch must not be able to reassign clientId.+ const bad: PeerDiff = { op: 'update', clientId: 3, patch: { clientId: 7 } }+ expect(bad).toBeDefined()})})@@ -67,8 +94,21 @@ describe('sanitizeIdentity', () => {expect(sanitizeIdentity({ name: 'Enes', color: 42 }, fallback).color).toBe(fallback.color)})- it('exposes a chat cap so a peer cannot flood the wire', () => {- expect(CHAT_MAX_LEN).toBeGreaterThan(0)+ it('caps by code point, never splitting an astral-plane character in half', () => {+ // 31 ASCII + 2 emoji: the cap lands mid-emoji. Cutting by UTF-16 code unit would leave a+ // lone surrogate, which every peer's facepile renders as "�".+ const name = 'x'.repeat(NAME_MAX_LEN - 1) + '😀😀'+ const out = sanitizeIdentity({ name, color: PRESENCE_COLORS[0] }, fallback).name+ expect(out).toBe('x'.repeat(NAME_MAX_LEN - 1) + '😀')+ expect([...out]).toHaveLength(NAME_MAX_LEN)+ expect(out).not.toMatch(/[\uD800-\uDFFF]/u) // no unpaired surrogate survived+ expect(out).not.toContain('�')+ })++ it('trims after truncating, so a cap landing on a space leaves no trailing space', () => {+ const name = 'a'.repeat(NAME_MAX_LEN - 1) + ' tail'+ const out = sanitizeIdentity({ name, color: PRESENCE_COLORS[0] }, fallback).name+ expect(out).toBe('a'.repeat(NAME_MAX_LEN - 1))})})diff --git a/src/shared/presence.ts b/src/shared/presence.tsindex e17c67f25..ec0f2895c 100644--- a/src/shared/presence.ts+++ b/src/shared/presence.ts@@ -37,11 +37,14 @@ export interface PeerState {export type PeerDiff =| { op: 'join'; peer: PeerState }- | { op: 'update'; clientId: ClientId; patch: Partial<PeerState> }+ /** `clientId` is the identity key and is carried by the diff itself — a patch can never+ * rewrite it (that would silently reassign the peer). */+ | { op: 'update'; clientId: ClientId; patch: Partial<Omit<PeerState, 'clientId'>> }| { op: 'leave'; clientId: ClientId }-/** Peer colors, assigned next-free on join. Picked for contrast on the black canvas. */-export const PRESENCE_COLORS: string[] = [+/** Peer colors, assigned next-free on join. Picked for contrast on the black canvas.+ * `readonly`: color assignment is app-wide state, so no consumer may push/sort/assign into it. */+export const PRESENCE_COLORS: readonly string[] = ['#5ac8fa', // blue'#ff9f0a', // orange'#30d158', // green@@ -58,8 +61,9 @@ export const TYPING_DECAY_MS = 2000export const NAME_MAX_LEN = 32export const CHAT_MAX_LEN = 200-/** First palette color not already in use; wraps by count once every color is taken. */-export function nextFreeColor(taken: string[]): string {+/** First palette color not already in use; wraps by count once every color is taken.+ * `taken` is `readonly` so callers stay unconstrained (a mutable `string[]` is accepted). */+export function nextFreeColor(taken: readonly string[]): string {const used = new Set(taken)const free = PRESENCE_COLORS.find((c) => !used.has(c))return free ?? PRESENCE_COLORS[taken.length % PRESENCE_COLORS.length]@@ -82,12 +86,20 @@ export function peersOnProject(peers: PeerState[], projectId: string | null): Pereturn peers.filter((p) => p.projectId === projectId)}+/** Cap a name at NAME_MAX_LEN *code points*, not UTF-16 code units: `slice` would cut a+ * surrogate pair (emoji, CJK extensions) in half and leave a lone surrogate, which renders as+ * "�" in every peer's facepile. Trim runs again AFTER the cut, so a truncation landing on a+ * space doesn't leave a trailing one. */+function capName(name: string): string {+ return [...name.trim()].slice(0, NAME_MAX_LEN).join('').trim()+}+/** Coerce an untrusted {name, color} off the wire into a safe identity. Names are unverified* (anyone can claim any name — documented trade-off) but they are length-capped, and the color* must be one of ours so it can never be injected into a style attribute as arbitrary text. */export function sanitizeIdentity(raw: unknown, fallback: PeerIdentity): PeerIdentity {const r = (raw ?? {}) as Partial<PeerIdentity>- const name = typeof r.name === 'string' ? r.name.trim().slice(0, NAME_MAX_LEN) : ''+ const name = typeof r.name === 'string' ? capName(r.name) : ''const color =typeof r.color === 'string' && PRESENCE_COLORS.includes(r.color) ? r.color : fallback.colorreturn { name: name || fallback.name, color }