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
fix(cli): welcome a service before its first queued request (#940)
Deebd7ca8334485
diff --git a/cli/e2e/serviceWelcome.e2e.ts b/cli/e2e/serviceWelcome.e2e.tsnew file mode 100644index 000000000..bd6974e41--- /dev/null+++ b/cli/e2e/serviceWelcome.e2e.ts@@ -0,0 +1,91 @@+/** Found by QA on a quiet machine: a cold Share request reached its process before `connected`, so its+ * agent read ran without a core connection and said HARNESS_NOT_FOUND. Separate the real wire's frames+ * in time, preserving their order: socket coalescing must not decide whether an agent can be shared. */+import { EventEmitter } from 'node:events'+import { mkdirSync } from 'node:fs'+import { join } from 'node:path'+import WebSocket from 'ws'+import { expect, it, onTestFailed } from 'vitest'+import { LocalClient } from './harness/client.js'+import { IsolatedDaemon, until } from './harness/daemon.js'+import { runServiceProcess } from '../src/services/process.js'+import { runSharingService } from '../src/services/sharingProcess.js'++/** The real socket, with successive received frames delivered in separate turns, in their original order. */+class SeparateFrames extends EventEmitter {+ private readonly socket: WebSocket+ private readonly queued: Array<[WebSocket.RawData, boolean]> = []+ private timer: ReturnType<typeof setTimeout> | null = null+ readonly types: string[] = []+ constructor(url: string) {+ super()+ this.socket = new WebSocket(url)+ this.socket.on('open', () => this.emit('open'))+ this.socket.on('error', (error) => this.emit('error', error))+ this.socket.on('close', (code) => {+ if (this.timer) clearTimeout(this.timer)+ this.timer = null+ this.queued.length = 0+ this.emit('close', code)+ })+ this.socket.on('message', (raw, binary) => { this.queued.push([raw, binary]); this.pump() })+ }+ private pump(): void {+ if (this.timer || !this.queued.length) return+ this.timer = setTimeout(() => {+ this.timer = null+ const [raw, binary] = this.queued.shift()!+ if (!binary) this.types.push(JSON.parse(raw.toString()).type)+ this.emit('message', raw, binary)+ this.pump()+ }, 20)+ }+ get readyState(): number { return this.socket.readyState }+ send(value: string): void { this.socket.send(value) }+ close(): void { this.socket.close() }+}++it('a cold Share reads its live agent after being welcomed, even when each wire frame arrives separately', async () => {+ const token = 'isolated-service-welcome-token'+ // The fixture supplies the one service, so the core knows its private boot token but no real master+ // starts a competing Share. Every other service runs inline; no real daemon or credential is involved.+ const d = await IsolatedDaemon.create({ noMaster: true, env: {+ HARNESS_NO_MASTER: '1', HARNESSD_SUPERVISED: '1', HARNESSD_SERVICE_TOKEN: token,+ HARNESSD_SERVICE_PROCESSES: 'sharing', HARNESSD_SERVICES: 'sharing',+ } })+ let client: LocalClient | undefined+ let service: ReturnType<typeof runSharingService> | undefined+ let wire: SeparateFrames | undefined+ onTestFailed(() => console.log(`---- daemon log\n${d.log()}`))+ try {+ await d.start()+ const core = d.corePid()+ client = await LocalClient.connect(d)+ const cwd = join(d.projectsDir, 'share-first-request')+ mkdirSync(cwd, { recursive: true })+ const created = await client.request('agent_create', { engine: 'claude', cwd, bypassPermission: true }, 60_000)+ expect(created.error, JSON.stringify(created)).toBeUndefined()+ await until('the agent to bind', async () => {+ const rows = (await client!.request('agents_list', {})).agents as Array<Record<string, any>>+ return rows.some((row) => row.id === created.agent.id && row.sessionId) || null+ }, 60_000, 100)+ const first = client.request('harness_share_list', { agentId: created.agent.id }, 30_000)+ // This later reply on the same ordered connection proves the first request reached the core.+ await client.request('agents_list', {})+ service = runSharingService({ dataDir: d.dataDir, socketPath: d.socketPath, machineId: d.computerId, token,+ run: (options) => runServiceProcess({ ...options, connect: (url) => {+ wire = new SeparateFrames(url)+ return wire as unknown as WebSocket+ } }),+ })+ const result = await first+ expect(result.error, JSON.stringify(result)).toBeUndefined()+ expect(result).toMatchObject({ shares: [], link: null, collaboration: true })+ expect(wire!.types.slice(0, 2)).toEqual(['connected', 'harness_share_list'])+ expect(d.corePid()).toBe(core)+ } finally {+ await service?.stop()+ client?.close()+ await d.close()+ }+})diff --git a/cli/src/core/serviceLinks.spec.ts b/cli/src/core/serviceLinks.spec.tsindex 495185858..eb15a42b3 100644--- a/cli/src/core/serviceLinks.spec.ts+++ b/cli/src/core/serviceLinks.spec.ts@@ -121,6 +121,23 @@ describe('service links', () => {expect(stays).not.toHaveBeenCalled()})+ it('acknowledges an accepted service before held events and the requests that woke it', () => {+ // Found by QA on a quiet machine: the first Share request saw no agent when it arrived before connected.+ const links = make({ owned: { sharing: ['harness_share_link'] }, onDemand: new Set(['sharing']) })+ links.notify('sharing', { type: 'service_event', payload: { kind: 'linkDown' } }, { untilDelivered: true })+ const reply = vi.fn()+ links.route('harness_share_link', { agentId: 'a1' }, ASKER, reply)+ const sharing = sink()+ const accepted = vi.fn(() => { sharing.sendFrame({ type: 'connected', payload: {} }) })+ expect(links.accept('sharing', 'wrong', sharing, vi.fn(), accepted)).toBeNull()+ expect(accepted).not.toHaveBeenCalled()+ const link = links.accept('sharing', TOKEN, sharing, vi.fn(), accepted)!+ expect(sharing.sent.map((frame) => frame.type)).toEqual(['connected', 'service_event', 'harness_share_link'])+ expect(accepted).toHaveBeenCalledOnce()+ link.receive({ type: 'harness_share_link_result', payload: { requestId: 'route-1', link: 'ready' } })+ expect(reply).toHaveBeenCalledWith({ link: 'ready' })+ })+it('answers SERVICE_UNAVAILABLE for an experiment that does not come in time, or will not take the request', () => {const links = make({ owned: { orchestrator: ['orchestrator'], search: ['session_search'] }, onDemand: new Set(['orchestrator']) })const late = vi.fn()diff --git a/cli/src/core/serviceLinks.ts b/cli/src/core/serviceLinks.tsindex 891198207..133938ee9 100644--- a/cli/src/core/serviceLinks.ts+++ b/cli/src/core/serviceLinks.ts@@ -142,8 +142,9 @@ export function createServiceLinks(options: ServiceLinksOptions) {}return {- /** A service connecting. Null — and the socket closes it — unless the master started it. */- accept(service: string, token: string, sink: ServiceSink, close: (code: number, reason: string) => void): ServiceLink | null {+ /** A service connecting. Null — and the socket closes it — unless the master started it.+ * `accepted` acknowledges the authenticated connection before its queued traffic is delivered. */+ accept(service: string, token: string, sink: ServiceSink, close: (code: number, reason: string) => void, accepted?: () => void): ServiceLink | null {if (!Object.hasOwn(options.owned, service) || !tokenMatches(token)) {log(`[services] a connection as service "${service.slice(0, 40)}" was refused`)return null@@ -154,6 +155,9 @@ export function createServiceLinks(options: ServiceLinksOptions) {links.set(service, connected)seen.add(service)log(`[services] ${service} connected`)+ // Found by QA on a quiet machine: a cold Share received its request before `connected`, so it+ // read agents without a core connection and answered HARNESS_NOT_FOUND. Welcome it first.+ accepted?.()const owed = held.get(service) ?? []held.delete(service)for (const frame of owed) sink.sendFrame(frame)diff --git a/cli/src/localWsServer.spec.ts b/cli/src/localWsServer.spec.tsindex 10e0349b8..f639de270 100644--- a/cli/src/localWsServer.spec.ts+++ b/cli/src/localWsServer.spec.ts@@ -1106,10 +1106,11 @@ describe('local CLI WebSocket', () => {const receiveBinary = vi.fn()let buffered: (() => number) | undefinedconst services = {- accept: (service: string, token: string, sink: { buffered(): number }) => {+ accept: (service: string, token: string, sink: { buffered(): number }, _close: (code: number, reason: string) => void, welcome: () => void) => {if (token !== 'boot-token') return nullaccepted.push(service)buffered = sink.buffered+ welcome()return { receive: vi.fn(), receiveBinary, closed: vi.fn() }},}diff --git a/cli/src/localWsServer.ts b/cli/src/localWsServer.tsindex a37a099cd..5e3d02d3d 100644--- a/cli/src/localWsServer.ts+++ b/cli/src/localWsServer.ts@@ -118,7 +118,7 @@ export interface LocalWsServerOptions {/** The core's end of its out-of-process services (core/serviceLinks.ts): a `machine_select` with* `role: "service"` is handed here, and the connection is the service's from then on. */services?: {- accept(service: string, token: string, sink: LocalClientSink & { buffered(): number }, close: (code: number, reason: string) => void):+ accept(service: string, token: string, sink: LocalClientSink & { buffered(): number }, close: (code: number, reason: string) => void, accepted: () => void):{ receive(frame: Frame): void; receiveBinary(bytes: Uint8Array): void; closed(): void } | null}/**@@ -397,11 +397,12 @@ export function attachLocalWsServer(server: http.Server, options: LocalWsServerO// account's machine id while a service names this computer's, and matching them refused every// service of every signed-in daemon (found by the release rehearsal, signed in).if (payload.role === 'service') {- const link = options.services?.accept(String(payload.service ?? ''), String(payload.token ?? ''), { ...sink, buffered: () => ws.bufferedAmount }, close) ?? null+ const link = options.services?.accept(String(payload.service ?? ''), String(payload.token ?? ''), { ...sink, buffered: () => ws.bufferedAmount }, close, () => {+ sink.sendFrame({ type: 'connected', payload: { machineId: options.machineId, transport: 'local', localProtocolVersion: LOCAL_WS_PROTOCOL_VERSION, service: payload.service } })+ }) ?? nullif (!link) { close(4401, 'service refused'); return }serviceLink = linkselected = true- sink.sendFrame({ type: 'connected', payload: { machineId: options.machineId, transport: 'local', localProtocolVersion: LOCAL_WS_PROTOCOL_VERSION, service: payload.service } })return}if (requestedMachineId === options.machineId) {