The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Core boundary, part 15: every socket slot bound (step 14), and coming back (#744)
Dee91450f1ed446
diff --git a/cli/e2e/recovery.e2e.ts b/cli/e2e/recovery.e2e.tsnew file mode 100644index 000000000..c2fac03ab--- /dev/null+++ b/cli/e2e/recovery.e2e.ts@@ -0,0 +1,121 @@+/**+ * Coming back, for Claude Code and Codex, on the real daemon: restarted again and again while agents+ * work, its master killed outright, an engine missing from this machine, and a registry lock left by+ * a daemon that crashed holding it. Agents live in tmux, so every way back must find them again.+ */+import { mkdirSync, rmSync, writeFileSync } from 'node:fs'+import { join } from 'node:path'+import { afterEach, describe, expect, it, onTestFailed } from 'vitest'+import { LocalClient, type Frame } from './harness/client.js'+import { IsolatedDaemon, until } from './harness/daemon.js'++type Engine = 'claude' | 'codex'++const row = async (client: LocalClient, agentId: string) =>+ ((await client.request<{ agents: Array<Record<string, any>> }>('agents_list', { includeStopped: true }, 30_000)).agents)+ .find((agent) => agent.id === agentId)+async function create(daemon: IsolatedDaemon, client: LocalClient, engine: Engine, folder: string): Promise<Record<string, any>> {+ const cwd = join(daemon.projectsDir, folder)+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine, cwd, bypassPermission: true }, 90_000)+ expect(created.error, `${folder}: ${JSON.stringify(created)}`).toBeUndefined()+ return until(`${folder} to bind its conversation`, async () => {+ const agent = await row(client, created.agent.id)+ return agent?.sessionId && agent.status === 'active' ? agent : null+ }, 60_000, 500)+}+const back = (client: LocalClient, agent: Record<string, any>) =>+ until(`${agent.id.slice(0, 8)} to be back`, async () => {+ const now = await row(client, agent.id)+ return now?.status === 'active' && now.sessionId === agent.sessionId ? now : null+ }, 60_000, 500)+const isTurn = (type: string, agentId: string) => (frame: Frame) => frame.type === type && frame.agentId === agentId+async function turn(client: LocalClient, agentId: string, content: string): Promise<void> {+ const ended = client.next(isTurn('turn_ended', agentId), 45_000, `turn_ended (${content})`)+ client.send('message', { agentId, content })+ await ended+}++describe('coming back', () => {+ let daemon: IsolatedDaemon | undefined+ afterEach(async () => { await daemon?.close(); daemon = undefined })+ const fresh = async () => {+ const d = await IsolatedDaemon.create()+ daemon = d+ onTestFailed(() => { console.log(`---- daemon log\n${d.log().split('\n').slice(-120).join('\n')}`) })+ return d+ }++ it('restarted five times in a row while agents work: they are back and take a turn every time', async () => {+ const d = await fresh()+ await d.start()+ let client = await LocalClient.connect(d)+ const agents = [await create(d, client, 'claude', 'restarts-claude'), await create(d, client, 'codex', 'restarts-codex')]+ for (let restart = 1; restart <= 5; restart++) {+ // A turn in flight as the daemon goes, every other time.+ if (restart % 2) client.send('message', { agentId: agents[0].id, content: '!slow 3000' })+ client.close()+ await d.restart()+ client = await LocalClient.connect(d)+ for (const agent of agents) await back(client, agent)+ await Promise.all(agents.map((agent) => turn(client, agent.id, `after restart ${restart}`)))+ }+ client.close()+ })++ it('its master killed outright: the core stops too, and the next start finds every agent', async () => {+ const d = await fresh()+ await d.start()+ const client = await LocalClient.connect(d)+ const agents = [await create(d, client, 'claude', 'killed-claude'), await create(d, client, 'codex', 'killed-codex')]+ const core = d.corePid()+ client.close()+ await d.kill()+ await until('the core to stop once its master is gone', () => !IsolatedDaemon.alive(core) || null, 30_000, 250)+ await d.start()+ const again = await LocalClient.connect(d)+ for (const agent of agents) await back(again, agent)+ await Promise.all(agents.map((agent) => turn(again, agent.id, 'after the master was killed')))+ again.close()+ })++ it('an engine missing from this machine fails its launch with the reason, and the other engine works', async () => {+ const d = await fresh()+ rmSync(join(d.root, 'bin', 'codex'), { force: true })+ await d.start()+ const client = await LocalClient.connect(d)+ const cwd = join(d.projectsDir, 'no-codex')+ mkdirSync(cwd, { recursive: true })+ // The pane opens and the launch fails in it, so the person sees why: the agent says it, and so does+ // its terminal.+ const created = await client.request('agent_create', { engine: 'codex', cwd, bypassPermission: true }, 90_000)+ const failed = await until('the launch to fail with its reason', async () => {+ const agent = created.agent?.id ? await row(client, created.agent.id) : undefined+ return agent?.launch?.state === 'failed' ? agent : null+ }, 30_000, 250)+ expect(failed.launch.error).toBe('ENGINE_NOT_INSTALLED')+ expect(failed.launch.detail).toContain('codex is not installed')+ expect(failed.status).not.toBe('active')+ const claude = await create(d, client, 'claude', 'still-claude')+ await turn(client, claude.id, 'claude is still here')+ expect(d.coresStarted()).toBe(1)+ client.close()+ })++ it('a registry lock left by a daemon that crashed holding it does not stop the next one', async () => {+ const d = await fresh()+ // A lock whose owner is a process that no longer exists, as a crash leaves it.+ const lock = join(d.dataDir, 'registry.json.lock')+ mkdirSync(lock, { recursive: true, mode: 0o700 })+ writeFileSync(join(lock, 'owner.json'), JSON.stringify({ pid: 999_999, token: 'crashed', at: Date.now() - 60_000 }), { mode: 0o600 })+ await d.start()+ const client = await LocalClient.connect(d)+ const agent = await create(d, client, 'codex', 'after-a-stale-lock')+ await turn(client, agent.id, 'the registry took the write')+ await d.restart()+ const again = await LocalClient.connect(d)+ await back(again, agent)+ again.close()+ client.close()+ })+})diff --git a/cli/src/socketBindings.spec.ts b/cli/src/socketBindings.spec.tsnew file mode 100644index 000000000..97e312b14--- /dev/null+++ b/cli/src/socketBindings.spec.ts@@ -0,0 +1,63 @@+/**+ * Every slot BackendSocket calls into the core through is bound by the composition root (cli.ts) — the+ * core boundary's step 14 (docs/design/2026-10-03-harnessd.md). A slot left null answers its requests+ * UNSUPPORTED or does nothing at all, and nothing says so: that is how `onLocalClient` came to be+ * declared and never wired. The bindings stay where start-up puts them (startupOrder.spec.ts pins that+ * order); this proves none is missing, and that a new slot cannot be added without binding it.+ */+import { readFileSync } from 'node:fs'+import { join } from 'node:path'+import ts from 'typescript'+import { describe, expect, it } from 'vitest'++const SRC = join(__dirname)++/** Slots left unbound on purpose, and why. */+const NOT_BOUND_ON_PURPOSE: Record<string, string> = {+ orchestratorCommand: 'a test override: null runs the daemon\'s own `orchestrator` command',+ teamCommand: 'a test override: null runs the daemon\'s own `team` command',+ onLocalClient: 'declared and never wired (the design doc\'s "Found while mapping")',+}++/** BackendSocket's public slots: properties that start null, and optional callbacks. */+function socketSlots(): string[] {+ const file = ts.createSourceFile('backendSocket.ts', readFileSync(join(SRC, 'backendSocket.ts'), 'utf8'), ts.ScriptTarget.Latest, true)+ const slots: string[] = []+ const visit = (node: ts.Node): void => {+ if (ts.isClassDeclaration(node) && node.name?.text === 'BackendSocket') {+ for (const member of node.members) {+ if (!ts.isPropertyDeclaration(member) || !ts.isIdentifier(member.name)) continue+ if (member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword || modifier.kind === ts.SyntaxKind.ReadonlyKeyword)) continue+ const startsNull = member.initializer?.kind === ts.SyntaxKind.NullKeyword+ const optionalCallback = !!member.questionToken && !member.initializer && !!member.type && ts.isFunctionTypeNode(member.type)+ if (startsNull || optionalCallback) slots.push(member.name.text)+ }+ }+ ts.forEachChild(node, visit)+ }+ visit(file)+ return slots.sort()+}++/** What cli.ts assigns on the socket: `backend.<slot> =`. */+function boundInCli(): Set<string> {+ const cli = readFileSync(join(SRC, 'cli.ts'), 'utf8')+ return new Set([...cli.matchAll(/\bbackend\.([A-Za-z]+)\s*=(?!=)/g)].map((match) => match[1]))+}++describe('the socket\'s slots into the core', () => {+ it('are all bound by the composition root, but for the few left unbound on purpose', () => {+ const bound = boundInCli()+ const unbound = socketSlots().filter((slot) => !bound.has(slot))+ expect(unbound).toEqual(Object.keys(NOT_BOUND_ON_PURPOSE).sort())+ })++ it('found by reading the class, so a new slot cannot slip past', () => {+ const slots = socketSlots()+ // Both kinds: one that starts null, and an optional callback with no initializer.+ expect(slots).toContain('onCreateAgent')+ expect(slots).toContain('onOutboundCommander')+ expect(slots.length).toBeGreaterThan(50)+ for (const name of Object.keys(NOT_BOUND_ON_PURPOSE)) expect(slots, name).toContain(name)+ })+})diff --git a/docs/design/2026-10-03-harnessd.md b/docs/design/2026-10-03-harnessd.mdindex 9f7dfc693..81cdcdf40 100644--- a/docs/design/2026-10-03-harnessd.md+++ b/docs/design/2026-10-03-harnessd.md@@ -489,7 +489,11 @@ start-up lint stay as they are; each is checked by `tsc`, the unit suites and th12. Engine hooks.13. Services in process, each behind `start(core, ports)`: search, viewers, models and workspacesfirst, then relay, sharing, teams and devices.-14. One `bindBackendSocket` for all 53 assignments.+14. Every slot `BackendSocket` calls into the core through is bound, and stays so:+ `socketBindings.spec.ts` reads the class's slots and fails on any that `cli.ts` leaves unbound+ (three are unbound on purpose, each with its reason). The bindings stay where start-up puts+ them: gathering them into one call would reorder a start-up `startupOrder.spec.ts` pins, for+ nothing a test cannot give.Moving the request switch's case bodies into a file per owner comes after.@@ -552,6 +556,9 @@ Found while mapping, and fixed on their own rather than inside a move:- Part 14: a permission prompt the same way (Yes in the window runs the command, No does not), anda soak (`e2e/soak.e2e.ts`): 2,400 turns across two Claude Code and two Codex agents left thecore's memory flat (246.7 → 244.7 MiB) and its open files at 33.+ - Part 15: step 14 as described above, and coming back (`e2e/recovery.e2e.ts`): five restarts in a+ row while agents work, the master killed outright, an engine missing from the machine (its launch+ fails with `ENGINE_NOT_INSTALLED` and the reason), and a registry lock left by a crash.## Testing