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.

fix(server): hosted team review round 1 — memoized node lookup, no raw bidi, safe shutdown and duplicates

reviewf0796de85cd8
diff --git a/src/core/relay/team-admin.test.ts b/src/core/relay/team-admin.test.ts
index 4cc10225a..2a572ac43 100644
--- a/src/core/relay/team-admin.test.ts
+++ b/src/core/relay/team-admin.test.ts
@@ -117,6 +117,22 @@ function rawExchange(dataDir: string, payload: string): Promise<string> {
})
}
+// Trojan Source: a raw bidi control character in source is invisible in review and can make code read
+// differently from how it runs. The admin/CLI sources name these characters, so they must do it with
+// \u escapes only.
+describe('no raw bidi control characters in the admin and CLI sources', () => {
+ it('every one is written as an escape', () => {
+ const bidi = new RegExp('[\\u061C\\u200E\\u200F\\u202A-\\u202E\\u2066-\\u2069]')
+ const files = [
+ path.join(__dirname, 'team-admin.ts'),
+ path.join(__dirname, 'team-admin.test.ts'),
+ path.join(__dirname, '../../server/team-cli.ts'),
+ path.join(__dirname, '../../server/team-cli.test.ts')
+ ]
+ for (const f of files) expect(bidi.test(fs.readFileSync(f, 'utf8')), f).toBe(false)
+ })
+})
+
describe('validation helpers (platform-neutral)', () => {
it('accepts a canonical 32-byte base64 key and says what is wrong with anything else', () => {
expect(ownerKeyProblem(validKey())).toBeNull()
@@ -136,7 +152,7 @@ describe('validation helpers (platform-neutral)', () => {
expect(ownerLabelProblem('x'.repeat(61))).toMatch(/61.*60/)
expect(ownerLabelProblem('a\nb')).toMatch(/control/)
expect(ownerLabelProblem('a\u001b[31m')).toMatch(/control/)
- expect(ownerLabelProblem('a‮b')).toMatch(/control/)
+ expect(ownerLabelProblem('a\u202eb')).toMatch(/control/)
})
it('project ids are non-empty, bounded and control-free', () => {
@@ -361,7 +377,10 @@ describe.skipIf(process.platform === 'win32')('team admin socket (unix socket, P
it('refuses to steal the socket from a live admin server', async () => {
const dataDir = tmp()
await boot(dataDir, fakeService().svc)
- await expect(startTeamAdmin(dataDir, fakeService().svc)).rejects.toThrow(/already answering/)
+ await expect(startTeamAdmin(dataDir, fakeService().svc)).rejects.toMatchObject({
+ code: 'E_ADMIN_SOCKET_BUSY',
+ message: expect.stringMatching(/already answering/)
+ })
// The first one is untouched.
expect((await callTeamAdmin(dataDir, { cmd: 'status' })).ok).toBe(true)
})
@@ -370,7 +389,9 @@ describe.skipIf(process.platform === 'win32')('team admin socket (unix socket, P
const dataDir = tmp()
fs.mkdirSync(relayDir(dataDir), { recursive: true })
fs.writeFileSync(adminSocketPath(dataDir), 'mine')
- await expect(startTeamAdmin(dataDir, fakeService().svc)).rejects.toThrow(/not a socket/)
+ const err = await startTeamAdmin(dataDir, fakeService().svc).catch((e: unknown) => e)
+ expect(err).toMatchObject({ message: expect.stringMatching(/not a socket/) })
+ expect((err as { code?: string }).code).not.toBe('E_ADMIN_SOCKET_BUSY')
expect(fs.readFileSync(adminSocketPath(dataDir), 'utf8')).toBe('mine')
})
@@ -383,6 +404,31 @@ describe.skipIf(process.platform === 'win32')('team admin socket (unix socket, P
expect(await callTeamAdmin(dataDir, { cmd: 'status' })).toEqual({ ok: false, error: expect.stringMatching(/shorter data directory/) })
})
+ it('an init still inside svc.init() when close() starts never starts hosting', async () => {
+ const dataDir = tmp()
+ const { svc, calls } = fakeService()
+ let release!: () => void
+ const gate = new Promise<void>((r) => (release = r))
+ let entered!: () => void
+ const inInit = new Promise<void>((r) => (entered = r))
+ const realInit = svc.init.bind(svc)
+ svc.init = async () => {
+ entered()
+ await gate
+ return realInit()
+ }
+ const admin = await startTeamAdmin(dataDir, svc)
+ const reply = callTeamAdmin(dataDir, { cmd: 'init' })
+ await inInit
+ const closed = admin.close()
+ release()
+ await closed
+ await reply
+ // Let the in-flight handler run to completion.
+ await new Promise((r) => setTimeout(r, 20))
+ expect(calls).toEqual(['init'])
+ })
+
it('close() removes the socket and does not hang on a connection that never sends a request', async () => {
const dataDir = tmp()
const admin = await startTeamAdmin(dataDir, fakeService().svc)
diff --git a/src/core/relay/team-admin.ts b/src/core/relay/team-admin.ts
index 8046a42f1..dd3b6b36c 100644
--- a/src/core/relay/team-admin.ts
+++ b/src/core/relay/team-admin.ts
@@ -79,6 +79,17 @@ const REMOVE_KEY_MAX = 256
const NO_TEAM = 'There is no hosted team on this server yet. Run `team init` first.'
+/** Something may already be serving this admin socket: another server answered on it, or it could
+ * not be told apart from one. Typed, because the caller's answer differs from every other admin
+ * failure: a second server on the same data dir must not ALSO host the team on the same host key. */
+export class AdminSocketBusyError extends Error {
+ readonly code = 'E_ADMIN_SOCKET_BUSY'
+ constructor(message: string) {
+ super(message)
+ this.name = 'AdminSocketBusyError'
+ }
+}
+
export function adminSocketPath(dataDir: string): string {
return path.join(relayDirOf(dataDir), 'admin.sock')
}
@@ -130,8 +141,9 @@ export function ownerKeyProblem(key: string): string | null {
}
// C0/C1 controls and DEL (\p{Cc}), plus the text-direction controls that let a label reorder the
-// text around it when printed in a terminal.
-const CONTROL_RE = /[\p{Cc}؜‎‏‪-‮⁦-⁩]/u
+// text around it when printed in a terminal. Written as \u escapes, never as the characters
+// themselves: a raw bidi control in source is invisible in review (Trojan Source).
+export const CONTROL_RE = /[\p{Cc}\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/u
/** Why `label` cannot be a member label, or null. The same limit `team.json`'s reader enforces. */
export function ownerLabelProblem(label: string): string | null {
@@ -224,13 +236,26 @@ const fail = (error: string): AdminReply => ({ ok: false, error })
const address = (svc: HostedService, running: boolean): { info: HostedInfo | null; joinCode: string | null } =>
running ? { info: svc.info(), joinCode: svc.joinCode() } : { info: null, joinCode: null }
-async function handle(relayDir: string, svc: HostedService, req: AdminRequest): Promise<AdminReply> {
+const SHUTTING_DOWN = 'The nodeterm server is shutting down.'
+
+async function handle(
+ relayDir: string,
+ svc: HostedService,
+ req: AdminRequest,
+ closing: () => boolean
+): Promise<AdminReply> {
if (req.cmd !== 'init' && req.cmd !== 'status' && req.cmd !== 'info' && !(await teamIsSetUp(relayDir, svc))) {
return fail(NO_TEAM)
}
switch (req.cmd) {
case 'init': {
const { created } = await svc.init()
+ // The admin closes BEFORE the server stops hosting. A `start()` issued after that stop would
+ // bring a scheduler up on a server that is going away, so an init still inside `svc.init()`
+ // when close() began does not start hosting.
+ if (closing()) {
+ return fail(`${SHUTTING_DOWN} ${created ? 'The team was created but hosting was not started.' : 'Hosting was not started.'}`)
+ }
const start = await svc.start()
const result: AdminInitResult = { created, start, ...address(svc, start === 'started') }
return ok(result)
@@ -264,7 +289,8 @@ async function handle(relayDir: string, svc: HostedService, req: AdminRequest):
}
}
-async function answer(relayDir: string, svc: HostedService, line: string): Promise<AdminReply> {
+async function answer(relayDir: string, svc: HostedService, line: string, closing: () => boolean): Promise<AdminReply> {
+ if (closing()) return fail(SHUTTING_DOWN)
let raw: unknown
try {
raw = JSON.parse(line)
@@ -274,7 +300,7 @@ async function answer(relayDir: string, svc: HostedService, line: string): Promi
const req = parseAdminRequest(raw)
if (typeof req === 'string') return fail(req)
try {
- return await handle(relayDir, svc, req)
+ return await handle(relayDir, svc, req, closing)
} catch (err) {
return fail(err instanceof Error ? err.message : String(err))
}
@@ -318,11 +344,11 @@ async function clearStaleSocket(sock: string): Promise<void> {
return
}
if (verdict === 'live') {
- throw new Error(
+ throw new AdminSocketBusyError(
`Another nodeterm server is already answering on ${sock} (two servers sharing one data directory?). Not replacing it.`
)
}
- throw new Error(`Could not tell whether ${sock} is still in use (${verdict}); not replacing it.`)
+ throw new AdminSocketBusyError(`Could not tell whether ${sock} is still in use (${verdict}); not replacing it.`)
}
export async function startTeamAdmin(dataDir: string, svc: HostedService): Promise<{ close(): Promise<void> }> {
@@ -334,6 +360,8 @@ export async function startTeamAdmin(dataDir: string, svc: HostedService): Promi
await clearStaleSocket(sock)
const conns = new Set<net.Socket>()
+ let closing: Promise<void> | null = null
+ const isClosing = (): boolean => closing !== null
const server = net.createServer((c) => {
conns.add(c)
c.on('close', () => conns.delete(c))
@@ -354,7 +382,7 @@ export async function startTeamAdmin(dataDir: string, svc: HostedService): Promi
if (nl < 0) return
taken = true
c.setTimeout(0)
- void answer(relayDir, svc, buf.slice(0, nl)).then((reply) => {
+ void answer(relayDir, svc, buf.slice(0, nl), isClosing).then((reply) => {
if (!c.destroyed) c.end(JSON.stringify(reply) + '\n')
})
})
@@ -373,7 +401,6 @@ export async function startTeamAdmin(dataDir: string, svc: HostedService): Promi
throw err
}
- let closing: Promise<void> | null = null
return {
close: () =>
(closing ??= new Promise<void>((resolve) => {
diff --git a/src/core/workspace-store.node-project.test.ts b/src/core/workspace-store.node-project.test.ts
new file mode 100644
index 000000000..f968246b8
--- /dev/null
+++ b/src/core/workspace-store.node-project.test.ts
@@ -0,0 +1,122 @@
+// `projectIdForNode` is asked once per access decision for every hosted-team viewer (every
+// agent:status event, every subagent-activity chunk, every unread-clear, every snapshot element).
+// `persistedCanvases()` re-parses every local project's cached project.json on each call, so the
+// answer is memoized — and the memo must never serve a stale answer after ANY writer changed a
+// project's nodes, including the ones that change the cache without going through save()/load().
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { promises as fs } from 'fs'
+import os from 'os'
+import path from 'path'
+import { initPlatform, resetPlatformForTests } from './platform'
+import { fakePlatform } from './platform-fake'
+import { WorkspaceStore } from './workspace-store'
+import type { CanvasNodeState, Project, Workspace } from '../shared/types'
+
+let userData: string
+const roots: string[] = []
+
+const node = (id: string): CanvasNodeState =>
+ ({ id, kind: 'terminal', position: { x: 0, y: 0 }, size: { width: 1, height: 1 }, title: id, color: '#fff', group: null }) as CanvasNodeState
+const project = (id: string, cwd: string | undefined, nodeIds: string[]): Project => ({
+ id,
+ name: id,
+ color: '#7aa2f7',
+ viewport: { x: 0, y: 0, zoom: 1 },
+ nodes: nodeIds.map(node),
+ ...(cwd ? { cwd } : {})
+})
+const ws = (projects: Project[]): Workspace => ({ version: 2, activeProjectId: projects[0]?.id ?? '', projects })
+const newRoot = async (): Promise<string> => {
+ const d = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-np-'))
+ roots.push(d)
+ return d
+}
+
+beforeEach(async () => {
+ userData = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-np-ws-'))
+ initPlatform(fakePlatform({ userDataDir: userData }))
+})
+afterEach(async () => {
+ resetPlatformForTests()
+ await fs.rm(userData, { recursive: true, force: true })
+ for (const d of roots.splice(0)) await fs.rm(d, { recursive: true, force: true })
+})
+
+/** How many times `JSON.parse` runs inside `fn` (synchronous, so nothing else runs meanwhile). */
+function parses(fn: () => void): number {
+ const spy = vi.spyOn(JSON, 'parse')
+ try {
+ fn()
+ return spy.mock.calls.length
+ } finally {
+ spy.mockRestore()
+ }
+}
+
+describe('WorkspaceStore.projectIdForNode', () => {
+ it('answers exactly what a persistedCanvases scan answers', async () => {
+ const [a, b] = [await newRoot(), await newRoot()]
+ const store = new WorkspaceStore()
+ await store.save(ws([project('pa', a, ['n1', 'n2']), project('pb', b, ['n3']), project('inline', undefined, ['n4'])]))
+ const scan = (id: string): string | undefined => store.persistedCanvases().find((c) => c.nodes.some((n) => n.id === id))?.id
+ for (const id of ['n1', 'n2', 'n3', 'n4', 'nope']) expect(store.projectIdForNode(id)).toBe(scan(id))
+ expect(store.projectIdForNode('n3')).toBe('pb')
+ expect(store.projectIdForNode('nope')).toBeUndefined()
+ })
+
+ it('parses each project file at most once for any number of lookups', async () => {
+ const dirs = await Promise.all(Array.from({ length: 5 }, () => newRoot()))
+ const store = new WorkspaceStore()
+ await store.save(ws(dirs.map((d, i) => project(`p${i}`, d, Array.from({ length: 50 }, (_, j) => `p${i}-n${j}`)))))
+ const n = parses(() => {
+ for (let k = 0; k < 1000; k++) store.projectIdForNode(`p${k % 5}-n${k % 50}`)
+ })
+ // One rebuild after the save (one parse per local project), then pure map lookups.
+ expect(n).toBeLessThanOrEqual(5)
+ // And a further burst with nothing changed parses nothing at all.
+ expect(parses(() => { for (let k = 0; k < 1000; k++) store.projectIdForNode('p1-n1') })).toBe(0)
+ })
+
+ it('a save that adds a node or moves one to another project is seen at once', async () => {
+ const [a, b] = [await newRoot(), await newRoot()]
+ const store = new WorkspaceStore()
+ await store.save(ws([project('pa', a, ['n1']), project('pb', b, ['n2'])]))
+ expect(store.projectIdForNode('n1')).toBe('pa')
+ expect(store.projectIdForNode('n9')).toBeUndefined()
+ await store.save(ws([project('pa', a, ['n9']), project('pb', b, ['n2', 'n1'])]))
+ expect(store.projectIdForNode('n9')).toBe('pa')
+ expect(store.projectIdForNode('n1')).toBe('pb')
+ // Into and out of an inline (cwd-less) project, whose nodes live in the index entry itself.
+ await store.save(ws([project('pa', a, ['n9']), project('pb', b, ['n2']), project('inline', undefined, ['n1'])]))
+ expect(store.projectIdForNode('n1')).toBe('inline')
+ })
+
+ it('a write that bypasses save() (a phone-registered node) is seen at once', async () => {
+ const a = await newRoot()
+ const store = new WorkspaceStore()
+ await store.save(ws([project('pa', a, ['n1'])]))
+ expect(store.projectIdForNode('term-abc123-phone1')).toBeUndefined()
+ expect(await store.appendRemoteNode('pa', { id: 'term-abc123-phone1', title: 'from the phone' })).toBe(true)
+ expect(store.projectIdForNode('term-abc123-phone1')).toBe('pa')
+ })
+
+ it('an outside edit picked up by load() (git pull, hand edit) is seen at once', async () => {
+ const [a, b] = [await newRoot(), await newRoot()]
+ const store = new WorkspaceStore()
+ await store.save(ws([project('pa', a, ['n1']), project('pb', b, ['n2'])]))
+ expect(store.projectIdForNode('n1')).toBe('pa')
+ // Move n1 from pa's file to pb's file behind the store's back, then reload.
+ const fa = path.join(a, '.nodeterm', 'project.json')
+ const fb = path.join(b, '.nodeterm', 'project.json')
+ const ja = JSON.parse(await fs.readFile(fa, 'utf-8'))
+ const jb = JSON.parse(await fs.readFile(fb, 'utf-8'))
+ jb.nodes.push(...ja.nodes)
+ ja.nodes = []
+ ja.rev += 1
+ jb.rev += 1
+ await fs.writeFile(fa, JSON.stringify(ja, null, 2))
+ await fs.writeFile(fb, JSON.stringify(jb, null, 2))
+ await store.load()
+ expect(store.projectIdForNode('n1')).toBe('pb')
+ })
+})
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index ffd0ee7ca..a65eefefb 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -134,6 +134,8 @@ export class WorkspaceStore {
* its last write on EVERY autosave; re-parsing each file each time was pure waste. Keyed by the
* raw string, so any `lastWritten.set` elsewhere invalidates it by construction. */
private lastWrittenParsed = new Map<string, { raw: string; parsed: ProjectFileV1 }>()
+ /** `projectIdForNode`'s node → project map, and the `canvasInputs` snapshot it was built from. */
+ private nodeProjectMemo: { inputs: unknown[]; map: Map<string, string> } | null = null
/** The index bytes we last wrote and the file's size/mtime/inode right after, so an unchanged
* index is not rewritten on every autosave — but one another writer changed on disk still is.
* The inode is what catches a same-size rewrite on a coarse-mtime filesystem: every writer
@@ -1504,6 +1506,45 @@ export class WorkspaceStore {
return undefined
}
+ /**
+ * The project a node belongs to — the first project that has it, exactly as a `persistedCanvases`
+ * scan answers. Memoized, because a hosted-team relay asks it once per access decision for every
+ * viewer (each agent:status event, subagent-activity chunk, unread-clear, snapshot element) and a
+ * `persistedCanvases()` call re-parses every local project's cached file.
+ *
+ * The memo is validated against the IDENTITY of every input `persistedCanvases` reads
+ * (`canvasInputs`), not a version counter: a counter is only as good as the writers that remember
+ * to bump it, and this class changes those inputs from a dozen places — `lastWritten.set`, index
+ * reassignment, and in-place entry updates (`e.cache = …`, `e.project = …`, `e.id = …` on a
+ * re-key). Every one of them replaces a reference, so a snapshot comparison sees all of them,
+ * including writers added later. It rests on one rule this file already keeps: a project's node
+ * array is replaced, never mutated in place.
+ */
+ projectIdForNode(nodeId: string): string | undefined {
+ const inputs = this.canvasInputs()
+ const memo = this.nodeProjectMemo
+ const fresh =
+ memo !== null && memo.inputs.length === inputs.length && memo.inputs.every((v, i) => v === inputs[i])
+ if (fresh) return memo.map.get(nodeId)
+ const map = new Map<string, string>()
+ for (const c of this.persistedCanvases()) {
+ for (const n of c.nodes) if (!map.has(n.id)) map.set(n.id, c.id)
+ }
+ this.nodeProjectMemo = { inputs, map }
+ return map.get(nodeId)
+ }
+
+ /** Every input `persistedCanvases` reads, by identity: the index object, and per entry the entry,
+ * its id, its inline project, its ssh cache, its cwd and the text last written/read for that
+ * cwd's project file. */
+ private canvasInputs(): unknown[] {
+ const out: unknown[] = [this.index]
+ for (const e of this.index?.entries ?? []) {
+ out.push(e, e.id, e.project, e.cache, e.cwd, e.cwd ? this.lastWritten.get(projectFilePath(e.cwd)) : undefined)
+ }
+ return out
+ }
+
/**
* Every persisted canvas as {id, nodes, bridges} — the raw material the Server Edition derives
* its context-link map from (src/server/context-link.ts). Same three-entry-kind scan as
diff --git a/src/server/hosted-boot-duplicate.test.ts b/src/server/hosted-boot-duplicate.test.ts
new file mode 100644
index 000000000..1f637e064
--- /dev/null
+++ b/src/server/hosted-boot-duplicate.test.ts
@@ -0,0 +1,64 @@
+// Two servers on one data dir must not both host the team: they would register listeners for the
+// same host key and write the same team.json. The second one learns about the first from the admin
+// socket — which is why the socket opens BEFORE hosting starts, and why "another server answers
+// there" skips hosting. Its own file: startServer memoizes some paths per process (hosted-boot.test.ts).
+import { describe, it, expect } from 'vitest'
+import fs from 'node:fs'
+import net from 'node:net'
+import os from 'node:os'
+import path from 'node:path'
+import { startServer } from './index'
+import { adminSocketPath } from '../core/relay/team-admin'
+
+const SHORT_BASE = fs.existsSync('/var/tmp') ? '/var/tmp' : os.tmpdir()
+
+// A unix socket stands in for the other server: POSIX only.
+describe.skipIf(process.platform === 'win32')('a second server on the same data dir', () => {
+ it('does not host, says why, and leaves the live admin socket alone', async () => {
+ const dataDir = fs.mkdtempSync(path.join(SHORT_BASE, 'nthd-'))
+ const relay = path.join(dataDir, 'relay')
+ fs.mkdirSync(relay, { recursive: true, mode: 0o700 })
+ // A team whose key cannot be read: if hosted.start() RUNS, it logs the unreadable-key line.
+ // That makes "start was skipped" observable without any network.
+ fs.writeFileSync(path.join(relay, 'team.json'), JSON.stringify({ v: 1, peers: [], sharedProjects: [] }))
+ fs.writeFileSync(path.join(relay, 'host-key.json'), 'not a key')
+ // The "other server": something live on the admin socket.
+ const other = net.createServer((c) => c.end())
+ await new Promise<void>((r) => other.listen(adminSocketPath(dataDir), () => r()))
+ const logs: string[] = []
+ const orig = { log: console.log, error: console.error }
+ console.log = (...a: unknown[]) => void logs.push(a.map(String).join(' '))
+ console.error = (...a: unknown[]) => void logs.push(a.map(String).join(' '))
+ try {
+ const srv = await startServer({
+ port: 0,
+ host: '127.0.0.1',
+ dataDir,
+ rendererDir: path.join(dataDir, 'no-renderer'),
+ insecureHttp: false,
+ headless: true,
+ // Never touch the developer's real agent configs (see ServerConfig.installHooks).
+ installHooks: false
+ })
+ await srv.close()
+ const text = logs.join('\n')
+ expect(text).toMatch(/Hosted team relay: NOT started — another nodeterm server/)
+ expect(text).not.toMatch(/host key could not be read/)
+ // The other server's socket is still there and still answering.
+ expect(fs.lstatSync(adminSocketPath(dataDir)).isSocket()).toBe(true)
+ const answered = await new Promise<boolean>((resolve) => {
+ const c = net.connect(adminSocketPath(dataDir), () => {
+ c.destroy()
+ resolve(true)
+ })
+ c.on('error', () => resolve(false))
+ })
+ expect(answered).toBe(true)
+ } finally {
+ console.log = orig.log
+ console.error = orig.error
+ await new Promise<void>((r) => other.close(() => r()))
+ fs.rmSync(dataDir, { recursive: true, force: true })
+ }
+ }, 30_000)
+})
diff --git a/src/server/hosted-boot.test.ts b/src/server/hosted-boot.test.ts
index b187d8649..25556f2bd 100644
--- a/src/server/hosted-boot.test.ts
+++ b/src/server/hosted-boot.test.ts
@@ -28,6 +28,25 @@ describe('hosted team boot wiring (source)', () => {
}
})
+ it('opens the admin socket BEFORE hosting, and skips hosting when another server holds it', () => {
+ const admin = src.indexOf('startTeamAdmin(')
+ const start = src.indexOf('hosted.start()')
+ expect(admin).toBeGreaterThan(0)
+ expect(admin).toBeLessThan(start)
+ expect(src.slice(admin, start)).toContain("'E_ADMIN_SOCKET_BUSY'")
+ })
+
+ it('main.cjs dispatches `team` through teamArgv (server flags may precede it) before any boot', () => {
+ const main = fs.readFileSync(path.join(__dirname, 'main.ts'), 'utf8').replace(/\r\n/g, '\n')
+ expect(main.indexOf('teamArgv(argv)')).toBeGreaterThan(0)
+ expect(main.indexOf('teamArgv(argv)')).toBeLessThan(main.indexOf('resolveConfig(process.env'))
+ expect(main).not.toMatch(/argv\[0\] === 'team'/)
+ })
+
+ it('projectOfNode uses the store\'s memoized lookup, not a persistedCanvases scan per call', () => {
+ expect(src).toMatch(/projectOfNode: \(nodeId\) => workspaceStore\.projectIdForNode\(nodeId\)/)
+ })
+
it('boots after the workspace index is loaded (the access policy reads it)', () => {
expect(src.indexOf('await workspaceStore.load(')).toBeGreaterThan(0)
expect(src.indexOf('await workspaceStore.load(')).toBeLessThan(src.indexOf('createHostedService('))
@@ -43,6 +62,8 @@ describe('hosted team boot wiring (source)', () => {
const body = src.slice(at, src.indexOf('await ptyManager.killAll()', at))
expect(body).toContain('hosted.stop()')
expect(body).toContain('await teamAdmin.close()')
+ // Admin first: once it is closing, an in-flight `team init` can no longer start hosting.
+ expect(body.indexOf('await teamAdmin.close()')).toBeLessThan(body.indexOf('hosted.stop()'))
}
})
diff --git a/src/server/index.ts b/src/server/index.ts
index 1a16eebb2..e4072e50b 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -860,25 +860,39 @@ export async function startServer(
dispatch: (id, req) => platform.dispatch(id, req),
cast: (id, method, args) => platform.cast(id, method, args)
},
- projectOfNode: (nodeId) =>
- workspaceStore.persistedCanvases().find((c) => c.nodes.some((n) => n.id === nodeId))?.id,
+ // Memoized in the store: asked once per access decision for every viewer, and a
+ // persistedCanvases() scan re-parses every local project's file (measured 4.5 ms per call at
+ // 20 projects x 100 nodes).
+ projectOfNode: (nodeId) => workspaceStore.projectIdForNode(nodeId),
projectCwd: (projectId) => workspaceStore.localCwdForProject(projectId)
})
- const hostedStart = await hosted.start().catch((err: unknown) => {
- console.error('[hosted-team] start failed:', err)
- return null
- })
- if (hostedStart === 'started') console.log('Hosted team relay: ON (see `team status`).')
- else if (hostedStart === 'host-key-unreadable') {
- console.error('Hosted team relay: OFF — the host key could not be read (see above; `team status`).')
- }
- // The local admin channel for the `team` CLI. Never fatal: a data dir too long for a unix socket,
- // or a second server on the same data dir, disables administration — it must not take the rest of
- // the Server Edition down with it.
+ // The local admin channel for the `team` CLI, opened BEFORE hosting starts: it is also how this
+ // server learns that another one already runs on this data dir (someone answers on its socket).
+ // Two servers hosting one team would register relay listeners for the same host key and both write
+ // team.json, so a busy socket skips hosting here. Otherwise never fatal: a data dir too long for a
+ // unix socket, or Windows, disables administration — it must not take the rest of the Server
+ // Edition down with it.
+ let otherServerHere = false
const teamAdmin = await startTeamAdmin(config.dataDir, hosted).catch((err: unknown) => {
+ if ((err as { code?: unknown } | null)?.code === 'E_ADMIN_SOCKET_BUSY') otherServerHere = true
console.error(`[hosted-team] team admin socket disabled: ${err instanceof Error ? err.message : String(err)}`)
return { close: async (): Promise<void> => {} }
})
+ if (otherServerHere) {
+ console.error(
+ 'Hosted team relay: NOT started — another nodeterm server is already running on this data ' +
+ `directory (${config.dataDir}). Stop it, or give this server its own --data-dir.`
+ )
+ } else {
+ const hostedStart = await hosted.start().catch((err: unknown) => {
+ console.error('[hosted-team] start failed:', err)
+ return null
+ })
+ if (hostedStart === 'started') console.log('Hosted team relay: ON (see `team status`).')
+ else if (hostedStart === 'host-key-unreadable') {
+ console.error('Hosted team relay: OFF — the host key could not be read (see above; `team status`).')
+ }
+ }
// Headless notification host: every core service above (incl. the loopback hook server, which
// is its own listener and MUST run) is booted, but we bind NO public HTTP/WS listener — no
diff --git a/src/server/main.ts b/src/server/main.ts
index 55d21cd12..2deb30606 100644
--- a/src/server/main.ts
+++ b/src/server/main.ts
@@ -1,6 +1,6 @@
import { resolveConfig, resolveDataDir } from './config'
import { startServer } from './index'
-import { runTeamCli } from './team-cli'
+import { runTeamCli, teamArgv } from './team-cli'
/**
* Script entry point for the headless server. Kept separate from index.ts so that
@@ -13,12 +13,14 @@ process.on('unhandledRejection', (e) => console.error('[nodeterm-server] unhandl
async function main(): Promise<void> {
const argv = process.argv.slice(2)
- if (argv[0] === 'team') {
- // `main.cjs team <command>`: the hosted team's admin CLI, not a server boot. It talks to the
- // RUNNING server over <dataDir>/relay/admin.sock, so it needs the data dir and nothing else —
- // `resolveDataDir`, not `resolveConfig`, whose serving-only refusals do not apply here.
- // `--data-dir` on the team command line overrides it (runTeamCli reads that itself).
- const code = await runTeamCli(argv.slice(1), resolveDataDir(process.env, []), (s) => console.log(s), (s) => console.error(s))
+ const team = teamArgv(argv)
+ if (team) {
+ // `main.cjs [server flags] team <command>`: the hosted team's admin CLI, not a server boot —
+ // including with server flags before `team` (`--data-dir X team status` must not boot a second
+ // server on X). It talks to the RUNNING server over <dataDir>/relay/admin.sock, so it needs the
+ // data dir and nothing else: `resolveDataDir`, not `resolveConfig`, whose serving-only refusals
+ // do not apply here. A `--data-dir` on the line overrides it (runTeamCli reads that itself).
+ const code = await runTeamCli(team, resolveDataDir(process.env, []), (s) => console.log(s), (s) => console.error(s))
// Let buffered output reach a pipe before exiting (stdout to a pipe is asynchronous on macOS).
await new Promise<void>((resolve) => process.stdout.write('', () => resolve()))
await new Promise<void>((resolve) => process.stderr.write('', () => resolve()))
diff --git a/src/server/team-cli.test.ts b/src/server/team-cli.test.ts
index fc669aaf3..4b97bb5e9 100644
--- a/src/server/team-cli.test.ts
+++ b/src/server/team-cli.test.ts
@@ -2,7 +2,7 @@ import { describe, it, expect, afterEach } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
-import { parseTeamArgv, runTeamCli, describeStatus } from './team-cli'
+import { parseTeamArgv, runTeamCli, describeStatus, teamArgv } from './team-cli'
import { startTeamAdmin, adminSocketPath, type AdminStatusResult } from '../core/relay/team-admin'
import { genKeyPair, publicKeyToB64 } from '../core/relay/e2ee'
import type { HostedService, HostedStatus } from '../core/relay/hosted-service'
@@ -52,6 +52,26 @@ describe('team argv', () => {
})
})
+describe('teamArgv (main.cjs dispatch)', () => {
+ it('finds `team` as the first non-flag argument, after server flags and their values', () => {
+ expect(teamArgv(['team', 'status'])).toEqual(['status'])
+ expect(teamArgv(['--data-dir', '/x', 'team', 'status'])).toEqual(['status', '--data-dir', '/x'])
+ expect(teamArgv(['--data-dir=/x', 'team', 'info', '--json'])).toEqual(['info', '--json', '--data-dir=/x'])
+ expect(teamArgv(['--insecure-http', '--port', '9000', 'team', 'status'])).toEqual(['status'])
+ // Only --data-dir matters to the CLI; the other server flags configure a boot that is not happening.
+ expect(teamArgv(['--port', '9000', '--data-dir', '/x', 'team', 'init'])).toEqual(['init', '--data-dir', '/x'])
+ })
+
+ it('anything else is a server boot', () => {
+ expect(teamArgv([])).toBeNull()
+ expect(teamArgv(['--port', '9000'])).toBeNull()
+ expect(teamArgv(['--insecure-http'])).toBeNull()
+ // A flag value named "team" is a value, exactly as the server's own parser reads it.
+ expect(teamArgv(['--data-dir', 'team'])).toBeNull()
+ expect(teamArgv(['serve', 'team'])).toBeNull()
+ })
+})
+
const idleStatus = (patch: Partial<NonNullable<HostedStatus['scheduler']>> = {}, rest: Partial<AdminStatusResult> = {}): AdminStatusResult => ({
enabled: true,
scheduler: { state: 'running', lastError: null, mintsLastHour: 3, idle: 1, bridged: 0, ...patch },
@@ -125,6 +145,20 @@ describe('describeStatus', () => {
expect(describeStatus({ enabled: false, off: { reason: 'constructor' } } as never).join('\n')).toMatch(/Hosting: OFF — hosting is not running\./)
})
+ it('never prints control or bidi characters from lastError, the off detail or a SAS', () => {
+ const evil = 'x\u001b]0;t\u0007\u202eY\u2066'
+ const running = describeStatus(idleStatus({ idle: 0, lastError: evil })).join('\n')
+ const refused = describeStatus(idleStatus({ state: 'backend-refused', idle: 0, lastError: evil })).join('\n')
+ const recovering = describeStatus(idleStatus({ lastError: evil })).join('\n')
+ const off = describeStatus({ enabled: false, scheduler: null, peers: [], pending: [], off: { reason: 'host-key-unreadable', detail: evil } }).join('\n')
+ const sas = describeStatus(idleStatus({}, { pending: [{ pendingId: 'p', sas: evil, peerKeyB64: 'k', since: 0 }] })).join('\n')
+ const bad = new RegExp('[\\p{Cc}\\u202E\\u2066]', 'u')
+ for (const text of [running, refused, recovering, off, sas]) {
+ expect(text.replace(/\n/g, ' '), text).not.toMatch(bad)
+ expect(text).toContain('x?]0;t??Y?')
+ }
+ })
+
it('never prints control characters from a label', () => {
const text = describeStatus({
enabled: false,
@@ -291,6 +325,33 @@ describe.skipIf(process.platform === 'win32')('runTeamCli over the admin socket
expect(miss.err).toMatch(/--data-dir/)
})
+ it('--json output escapes control and bidi characters (still valid JSON, nothing raw on the terminal)', async () => {
+ const dataDir = tmp()
+ fs.mkdirSync(path.join(dataDir, 'relay'), { recursive: true, mode: 0o700 })
+ const f = fake({ running: true })
+ const evilLabel = 'box\u202e\u007f'
+ f.svc.info = () => ({ ...INFO, label: evilLabel })
+ const admin = await startTeamAdmin(dataDir, f.svc)
+ closers.push(() => admin.close())
+ const r = await run(['info', '--json'], dataDir)
+ // Nothing raw but the pretty-printing newlines.
+ expect(r.out).not.toMatch(new RegExp('[\\u0000-\\u0009\\u000B-\\u001F\\u007F-\\u009F\\u202E]'))
+ expect(JSON.parse(r.out).info.label).toBe(evilLabel)
+ })
+
+ it('an error from the server is printed without control characters', async () => {
+ const dataDir = tmp()
+ const f = fake({ running: true })
+ f.svc.init = async () => {
+ throw new Error('bad\u001b[2Jthing\u202e')
+ }
+ const admin = await startTeamAdmin(dataDir, f.svc)
+ closers.push(() => admin.close())
+ const r = await run(['init'], dataDir)
+ expect(r.code).toBe(1)
+ expect(r.err).toContain('bad?[2Jthing?')
+ })
+
it('usage errors exit 2; help exits 0', async () => {
const dataDir = tmp()
expect((await run([], dataDir)).code).toBe(2)
diff --git a/src/server/team-cli.ts b/src/server/team-cli.ts
index 6909f9d29..4b5679d6b 100644
--- a/src/server/team-cli.ts
+++ b/src/server/team-cli.ts
@@ -6,6 +6,7 @@
// Exit codes: 0 done, 1 the server refused or could not be reached, 2 the command line is wrong.
import path from 'node:path'
import {
+ CONTROL_RE,
callTeamAdmin,
ownerKeyProblem,
ownerLabelProblem,
@@ -17,6 +18,18 @@ import {
type AdminStatusResult
} from '../core/relay/team-admin'
+// One rule for what never reaches the admin's terminal: the same characters a label may not
+// contain (C0/C1 controls, DEL, and the text-direction controls). Server-supplied strings can carry
+// them — a hand-edited team.json, an error message quoting a path, a peer-influenced lastError.
+const CONTROL_G = new RegExp(CONTROL_RE.source, 'gu')
+const clean = (s: string): string => s.replace(CONTROL_G, '?')
+/** JSON for the terminal. JSON.stringify already escapes C0 controls inside strings, but leaves
+ * DEL, the C1 controls and the bidi controls raw; those become \u escapes here. Still valid JSON:
+ * they only ever occur inside strings (the raw newlines are the pretty-printing, left alone). */
+const JSON_RAW_G = /[\u007F-\u009F\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g
+const safeJson = (v: unknown): string =>
+ JSON.stringify(v, null, 2).replace(JSON_RAW_G, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`)
+
const USAGE_ROWS: Array<[string, string]> = [
['init', 'create the host key and the team, and start hosting'],
['add-owner <device-key> [--label <name>]', 'make a device an owner (its 44-character public key)'],
@@ -61,7 +74,7 @@ export function parseTeamArgv(argv: string[]): AdminRequest | { error: string }
const [cmd, ...rest] = argv
if (cmd === undefined) return usageError('A command is required.')
const spec = Object.hasOwn(COMMANDS, cmd) ? COMMANDS[cmd] : undefined
- if (!spec) return usageError(`unknown command "${cmd}"`)
+ if (!spec) return usageError(`unknown command "${clean(cmd)}"`)
const positionals: string[] = []
const flags: Record<string, string | true> = {}
for (let i = 0; i < rest.length; i++) {
@@ -73,7 +86,7 @@ export function parseTeamArgv(argv: string[]): AdminRequest | { error: string }
const eq = tok.indexOf('=')
const name = tok.slice(2, eq < 0 ? undefined : eq)
const kind = Object.hasOwn(spec.flags, name) ? spec.flags[name] : undefined
- if (!kind) return usageError(`unknown option --${name} for "team ${cmd}"`)
+ if (!kind) return usageError(`unknown option --${clean(name)} for "team ${cmd}"`)
if (kind === 'bool') {
if (eq >= 0) return usageError(`--${name} takes no value`)
flags[name] = true
@@ -109,7 +122,34 @@ export function parseTeamArgv(argv: string[]): AdminRequest | { error: string }
return problem ? { error: problem } : { cmd: 'share', projectId: positionals[0], on: cmd === 'share' }
}
}
- return usageError(`unknown command "${cmd}"`)
+ return usageError(`unknown command "${clean(cmd)}"`)
+}
+
+/**
+ * `main.cjs [server flags] team <command> …`: the team CLI's own argv, or null for a server boot.
+ * `team` is recognised as the first NON-FLAG argument, skipping flag values the way the server's own
+ * argv parser reads them (a `--flag` followed by a non-flag token takes it as its value, except
+ * `--insecure-http`). So `main.cjs --data-dir X team status` runs the CLI rather than booting a whole
+ * second server on X. Of the flags before `team`, only `--data-dir` means anything to the CLI; it is
+ * carried over (runTeamCli reads it anywhere), and the rest configure a boot that is not happening.
+ */
+export function teamArgv(argv: string[]): string[] | null {
+ const carried: string[] = []
+ for (let i = 0; i < argv.length; i++) {
+ const tok = argv[i]
+ if (!tok.startsWith('--')) return tok === 'team' ? [...argv.slice(i + 1), ...carried] : null
+ if (tok.startsWith('--data-dir=')) {
+ carried.push(tok)
+ continue
+ }
+ if (tok.includes('=') || tok === '--insecure-http') continue
+ const next = argv[i + 1]
+ if (next !== undefined && !next.startsWith('--')) {
+ if (tok === '--data-dir') carried.push(tok, next)
+ i++
+ }
+ }
+ return null
}
/** Take a global `--data-dir <dir>` / `--data-dir=<dir>` off the command line, wherever it sits. */
@@ -134,9 +174,8 @@ function takeDataDir(argv: string[]): { dataDir?: string; argv: string[] } | { e
const obj = (v: unknown): Record<string, unknown> =>
v !== null && typeof v === 'object' && !Array.isArray(v) ? (v as Record<string, unknown>) : {}
-// Control characters never reach the admin's terminal: a label in a hand-edited team.json could
-// otherwise carry escape sequences.
-const str = (v: unknown, fallback = ''): string => (typeof v === 'string' ? v.replace(/\p{Cc}/gu, '?') : fallback)
+// Every server-supplied string is printed through `str` (see `clean`).
+const str = (v: unknown, fallback = ''): string => (typeof v === 'string' ? clean(v) : fallback)
const num = (v: unknown): number => (typeof v === 'number' && Number.isFinite(v) ? v : 0)
const plural = (n: number, one: string, many = `${one}s`): string => `${n} ${n === 1 ? one : many}`
@@ -170,7 +209,7 @@ export function describeStatus(result: AdminStatusResult): string[] {
const s = obj(result)
const lines: string[] = []
const sched = obj(s.scheduler)
- const lastError = typeof sched.lastError === 'string' ? sched.lastError : null
+ const lastError = typeof sched.lastError === 'string' ? str(sched.lastError) : null
const idle = num(sched.idle)
const bridged = num(sched.bridged)
if (s.enabled !== true) {
@@ -256,7 +295,7 @@ function render(req: AdminRequest, result: unknown, json: boolean): { lines: str
case 'info': {
const r = obj(result) as Partial<AdminInfoResult>
const has = r.info !== null && r.info !== undefined
- if (json) return { lines: [JSON.stringify(result, null, 2)], code: has ? 0 : 1 }
+ if (json) return { lines: [safeJson(result)], code: has ? 0 : 1 }
if (!has) {
return {
lines: ['Hosting has not started on this server, so there is no team address yet. Run `team init` (see `team status` for why).'],
@@ -268,7 +307,7 @@ function render(req: AdminRequest, result: unknown, json: boolean): { lines: str
return { lines, code: 0 }
}
case 'status':
- return { lines: json ? [JSON.stringify(result, null, 2)] : describeStatus(result as AdminStatusResult), code: 0 }
+ return { lines: json ? [safeJson(result)] : describeStatus(result as AdminStatusResult), code: 0 }
case 'rotate-key': {
const r = obj(result) as Partial<AdminRotateResult>
if (r.result === 'not-running') {
@@ -316,7 +355,7 @@ export async function runTeamCli(
}
const r = await callTeamAdmin(dd.dataDir ?? dataDir, req)
if (!r.ok) {
- err(r.error)
+ err(clean(r.error))
return 1
}
// The reply is printed to `out` even when the exit code is 1: the command ran, and what it found