Skip to content

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.

5.5 KB
Raw Blame
// The daemon's pipe as its clients find and call it (daemonPipe.ts, daemonEndpoint.ts `localDaemonStatus` and
// `localDaemonCall`). On Windows these are real pipes (\\.\pipe\harness-<id>), and the Windows workflow runs this
// file with src/lib/windows; elsewhere Unix sockets named alike.
import { randomBytes } from 'node:crypto'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { localDaemonCall, localDaemonStatus, saveDaemonPort } from '../daemonEndpoint.js'
import { listenLocalSocket } from '../localSocket.js'
import { daemonPipe } from './daemonPipe.js'
import { ensureDaemonToken, newPipePath } from './pipeAuth.js'
import { PipeUnproven } from './pipeClient.js'

const cleanup: Array<() => void | Promise<void>> = []
afterEach(async () => { for (const close of cleanup.splice(0).reverse()) await close() })

/** A data folder for the token: /tmp's short path where sockets need one, the temp folder (in the profile) on Windows. */
function pipeDir(): string {
  const dir = mkdtempSync(process.platform === 'win32' ? join(tmpdir(), 'hpipe-') : '/tmp/hn-owner-')
  cleanup.push(() => rmSync(dir, { recursive: true, force: true }))
  return dir
}
/** A pipe name nothing has taken: a real one on Windows, a socket in [dir] elsewhere. */
function pipeName(dir: string): string {
  return process.platform === 'win32' ? newPipePath() : join(dir, `harness-${randomBytes(16).toString('hex')}`)
}

describe('the daemon over its Windows pipe', () => {
  const alive = { alive: () => true }
  /** A data folder as the Windows core leaves it: a token, a record naming its pipe and pid, and the pipe served. */
  async function windowsDaemon(handler: Parameters<typeof listenLocalSocket>[0], port = 19418) {
    const dir = pipeDir()
    const token = ensureDaemonToken(dir)
    const pipe = pipeName(dir)
    const server = await listenLocalSocket(handler, pipe, { token })
    cleanup.push(server.close)
    saveDaemonPort(dir, port, 24567, { pipe, pid: process.pid })
    return { dir, token, pipe, port }
  }
  const options = { platform: 'win32' as const, pipe: (dir: string, port: number) => daemonPipe(dir, port, alive) }

  it('is found by its record and token, unless its process is proven gone', async () => {
    const { dir, token, pipe, port } = await windowsDaemon((_req, res) => res.end('{}'))
    const found = await daemonPipe(dir, port, alive)
    expect(found?.pipe).toBe(pipe)
    expect(found?.token.equals(token)).toBe(true)
    expect(await daemonPipe(dir, port, { alive: () => false })).toBeNull()
    expect(await daemonPipe(dir, port + 1, alive)).toBeNull()
    saveDaemonPort(dir, port, 24567)
    expect(await daemonPipe(dir, port, alive)).toBeNull()
    expect(await daemonPipe(pipeDir(), port, alive)).toBeNull()
  })

  // A Harness Team account holds the configured port, so the daemon is given another, which `daemonPort` hands every command.
  it('is found by the port the daemon was given, as well as the one it was configured with', async () => {
    const { dir, pipe } = await windowsDaemon((_req, res) => res.end('{}'))
    expect((await daemonPipe(dir, 24567, alive))?.pipe).toBe(pipe)
    expect(await daemonPipe(dir, 24567, { alive: () => false })).toBeNull()
  })

  it('answers status over the pipe, and nothing for an answer without the proof', async () => {
    const { dir, port } = await windowsDaemon((_req, res) => res.end(JSON.stringify({ machineId: 'account-a' })))
    expect(await localDaemonStatus(dir, port, options)).toEqual({ machineId: 'account-a' })
    expect(await localDaemonStatus(pipeDir(), port, options)).toBeNull()
    const foreign = await localDaemonStatus(dir, port, { platform: 'win32', pipe: async (d, p) => ({ ...(await daemonPipe(d, p, alive))!, token: randomBytes(32) }) })
    expect(foreign).toBeNull()
  })

  // Audit W1 (docs/security/2026-11-02-team-account-windows.md): with nothing on the pipe it fell back to TCP, which the Team
  // account's daemon, started at boot, can hold. Now it fails there as a daemon that is down.
  it('makes control calls over the pipe, never TCP: not with nothing there, not after an unproven answer', async () => {
    let got = ''
    const { dir, port, pipe } = await windowsDaemon((req, res) => {
      got = `${req.method} ${req.url} ${req.headers['x-adapter-local']}`
      res.writeHead(200, { 'content-type': 'application/json' })
      res.end('{"ok":true}')
    })
    const tcp = vi.fn(async () => new Response('{"tcp":true}', { status: 200 }))
    const call = (o: Partial<Parameters<typeof localDaemonCall>[5]> = {}) =>
      localDaemonCall(dir, port, 'POST', '/api/pair', { code: '1' }, { ...options, tcpPort: 1, fetch: tcp as typeof fetch, ...o })
    const over = await call()
    expect(await over.json()).toEqual({ ok: true })
    expect(got).toBe('POST /api/pair 1')
    // No pipe to reach, or a recorded pipe nothing listens on any more: the daemon is down.
    await expect(call({ pipe: async () => null })).rejects.toThrow('fetch failed')
    const gone = pipeName(pipeDir())
    await expect(call({ pipe: async () => ({ pipe: gone, token: randomBytes(32) }) })).rejects.toThrow('fetch failed')
    await expect(call({ pipe: async () => ({ pipe, token: randomBytes(32) }) })).rejects.toBeInstanceOf(PipeUnproven)
    expect(tcp).not.toHaveBeenCalled()
    // Elsewhere the call is the transport's: here the test's stand-in for it.
    expect(await (await call({ platform: 'darwin' })).json()).toEqual({ tcp: true })
  })
})