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): team store rejects repeated keys and never writes what it cannot read back
reviewbfcf868cee94
diff --git a/src/core/relay/team-store.test.ts b/src/core/relay/team-store.test.tsindex 9f6f4ddb3..080acc324 100644--- a/src/core/relay/team-store.test.ts+++ b/src/core/relay/team-store.test.ts@@ -25,6 +25,11 @@ describe('team doc', () => {expect(parseTeam(null)).toBeNull()expect(parseTeam({ v: 1, peers: [owner], sharedProjects: ['p1'] })).toEqual({ v: 1, peers: [owner], sharedProjects: ['p1'] })})+ it('rejects a key listed twice, so file ORDER can never decide a role', () => {+ expect(parseTeam({ v: 1, peers: [{ ...owner, role: 'viewer' }, owner], sharedProjects: [] })).toBeNull()+ expect(parseTeam({ v: 1, peers: [owner, { ...owner, role: 'viewer' }], sharedProjects: [] })).toBeNull()+ expect(parseTeam({ v: 1, peers: [owner, { ...owner, label: 'same role' }], sharedProjects: [] })).toBeNull()+ })it('upsert replaces by key; remove refuses the last owner without force', () => {let d = upsertPeer(emptyTeam(), owner)d = upsertPeer(d, { ...owner, label: 'E2' })@@ -34,8 +39,8 @@ describe('team doc', () => {expect(removePeer(d, 'O', true)).toEqual({ ...d, peers: [] })})it('remove refuses when a key listed twice as owner is the only owner key', () => {- // A hand-edited file can list one key twice. Counting ENTRIES would see two owners and let- // the removal (which drops every entry for that key) leave the team with none.+ // parseTeam refuses a repeated key, so this doc can only exist in memory. The guard still+ // counts owner KEYS, not entries, so even such a doc cannot be left without an owner.const doubled = { ...emptyTeam(), peers: [owner, { ...owner, label: 'dup' }] }expect(removePeer(doubled, 'O', false)).toBe('last-owner')// A non-owner leaving a team that has no owner at all is not a last-owner removal.@@ -50,7 +55,8 @@ describe('team doc', () => {})describe('TeamStore', () => {- it('persists 0600 and reloads', async () => {+ // POSIX permission bits: Windows reports its own mode bits here, not the 0600 we asked for.+ it.skipIf(process.platform === 'win32')('persists 0600 and reloads', async () => {const dir = tmp()const s = new TeamStore(dir)await s.load()@@ -66,6 +72,36 @@ describe('TeamStore', () => {expect((await s.load()).peers).toEqual([])expect(fs.readdirSync(dir).some((f) => f.startsWith('team.json.corrupt-'))).toBe(true)})+ it('a file listing one key twice is set aside and the store starts CLOSED', async () => {+ const dir = tmp()+ writeDoc(dir, { v: 1, peers: [{ ...owner, role: 'viewer' }, owner], sharedProjects: [] })+ const s = new TeamStore(dir)+ expect((await s.load()).peers).toEqual([])+ expect(fs.readdirSync(dir).some((f) => f.startsWith('team.json.corrupt-'))).toBe(true)+ })+ it('refuses to persist a doc its own reader would reject, leaving file and memory untouched', async () => {+ // Otherwise the write lands and the NEXT load sets the whole team aside as corrupt.+ const dir = tmp()+ const s = new TeamStore(dir)+ await s.update((d) => upsertPeer(d, owner))+ const file = path.join(dir, 'team.json')+ const before = fs.readFileSync(file, 'utf-8')+ const mem = s.current()+ await expect(s.update((d) => upsertPeer(d, { ...owner, pubkeyB64: 'k'.repeat(65) }))).rejects.toThrow()+ await expect(s.update((d) => setShared(d, '', true))).rejects.toThrow()+ await expect(s.update((d) => upsertPeer(d, { ...owner, pubkeyB64: 'X', addedBy: '' }))).rejects.toThrow()+ expect(fs.readFileSync(file, 'utf-8')).toBe(before)+ expect(s.current()).toBe(mem)+ expect((await new TeamStore(dir).load()).peers).toEqual([owner])+ })+ // POSIX permission bits: chmod is skipped on Windows, where the bits do not apply.+ it.skipIf(process.platform === 'win32')('tightens an existing, looser directory to 0700 on write', async () => {+ const dir = path.join(tmp(), 'relay')+ fs.mkdirSync(dir, { mode: 0o755 })+ fs.chmodSync(dir, 0o755) // the umask may have narrowed it; make it genuinely loose+ await new TeamStore(dir).update((d) => upsertPeer(d, owner))+ expect((fs.statSync(dir).mode & 0o777).toString(8)).toBe('700')+ })it('concurrent updates never lose a write', async () => {const s = new TeamStore(tmp())await s.load()diff --git a/src/core/relay/team-store.ts b/src/core/relay/team-store.tsindex 32bd59a94..cde4dc2ca 100644--- a/src/core/relay/team-store.ts+++ b/src/core/relay/team-store.ts@@ -7,7 +7,7 @@// Corrupt ⇒ CLOSED: the file is set aside and the store starts with no peers, so nobody// auto-reconnects. The owner recovers with `team add-owner` over SSH, which is also the// root of trust.-import { promises as fs, existsSync, mkdirSync } from 'node:fs'+import { promises as fs, existsSync, mkdirSync, chmodSync } from 'node:fs'import path from 'node:path'import { writeFileAtomic, renameAtomic } from '../fs-atomic'@@ -28,12 +28,17 @@ export function parseTeam(raw: unknown): TeamDoc | null {const o = raw as Record<string, unknown>if (o.v !== 1 || !Array.isArray(o.peers) || !Array.isArray(o.sharedProjects)) return nullconst peers: TeamPeer[] = []+ // A repeated key is corrupt, not deduped: `peerFor` takes the first entry, so keeping both would+ // let file ORDER decide whether a key is a viewer or an owner.+ const seen = new Set<string>()for (const p of o.peers) {if (!p || typeof p !== 'object') return nullconst q = p as Record<string, unknown>if (!isStr(q.pubkeyB64, 64) || typeof q.label !== 'string' || q.label.length > LABEL_MAX) return nullif (!(TEAM_ROLES as readonly unknown[]).includes(q.role)) return nullif (!isStr(q.addedAt, 40) || !isStr(q.addedBy, 64)) return null+ if (seen.has(q.pubkeyB64)) return null+ seen.add(q.pubkeyB64)peers.push({ pubkeyB64: q.pubkeyB64, label: q.label, role: q.role as TeamRole, addedAt: q.addedAt, addedBy: q.addedBy })}if (!o.sharedProjects.every((s) => isStr(s, 128))) return null@@ -52,9 +57,9 @@ export function upsertPeer(doc: TeamDoc, p: TeamPeer): TeamDoc {export function removePeer(doc: TeamDoc, pubkeyB64: string, force: boolean): TeamDoc | 'last-owner' {if (!peerFor(doc, pubkeyB64)) return docconst rest = doc.peers.filter((p) => p.pubkeyB64 !== pubkeyB64)- // "Would an owner remain?", not "is this the only owner ENTRY?": a hand-edited file can list one- // key twice, and counting entries would let this removal (which drops every entry for the key)- // leave a team that had an owner with none.+ // "Would an owner remain?", not "is this the only owner ENTRY?". `parseTeam` refuses a repeated+ // key, so this only guards an in-memory doc that lists one: counting entries would let this+ // removal (which drops every entry for the key) leave a team that had an owner with none.const hadOwner = doc.peers.some((p) => p.role === 'owner')if (hadOwner && !rest.some((p) => p.role === 'owner') && !force) return 'last-owner'return { ...doc, peers: rest }@@ -89,7 +94,15 @@ export class TeamStore {if (!this.loaded) await this.read()const next = fn(this.doc)if (next === 'last-owner') return next+ // Never publish what our own reader rejects: the write would land, and the NEXT load would set+ // the whole team aside as corrupt. Throwing here leaves both the file and `this.doc` as they were.+ if (!parseTeam(next)) throw new Error('team.json: refusing to write a team doc that fails validation')mkdirSync(this.dir, { recursive: true, mode: 0o700 })+ // mkdir's mode applies only when it CREATES the directory; tighten one that already existed.+ // Best-effort, and POSIX only: the bits mean nothing on Windows.+ if (process.platform !== 'win32') {+ try { chmodSync(this.dir, 0o700) } catch { /* not ours to fix: the 0600 file is the guard */ }+ }await writeFileAtomic(this.file(), JSON.stringify(next, null, 2) + '\n', { mode: 0o600 })this.doc = nextreturn next