Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

3.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 BoxLite AI

/*
 * Post-deploy registration of extra runners with the control plane.
 *
 * The API only auto-seeds the single default runner (from DEFAULT_RUNNER_*), so
 * additional runners declared via RUNNERS must be registered through the admin
 * API. This script is invoked by the `RegisterExtraRunners` command in
 * stack/runners.ts after the extra runner EC2s and the API service are up.
 *
 * Pairing is token-based: the runner row's apiKey must equal the
 * BOXLITE_RUNNER_TOKEN baked into the matching EC2's user-data. SST mints one
 * token per runner and passes the (name, token) pairs here via RUNNERS.
 *
 * Idempotent: a 409 (runner already exists in the region) is treated as
 * success, so redeploys are safe.
 *
 * Env:
 *   API_URL        base URL of the API service (e.g. https://api.example.com)
 *   ADMIN_API_KEY  admin-scoped API key (Bearer) for POST /api/admin/runners
 *   REGION_ID      region to register the runners in (default "us")
 *   RUNNERS        JSON array of { name, apiKey }
 */

const { API_URL, ADMIN_API_KEY, REGION_ID = 'us', RUNNERS } = process.env

const runners = JSON.parse(RUNNERS || '[]')
if (runners.length === 0) {
  process.exit(0)
}
if (!API_URL || !ADMIN_API_KEY) {
  console.error('register-runners: API_URL and ADMIN_API_KEY are required')
  process.exit(1)
}

const base = API_URL.replace(/\/+$/, '')
const sleep = (ms: any) => new Promise((resolve) => setTimeout(resolve, ms))
const REQUEST_TIMEOUT_MS = 10_000
const isRetryableStatus = (status: number) => status === 429 || status >= 500

// Wait for the API to start serving. /api/health returns 200 only after the
// HTTP server is listening, which (in onApplicationBootstrap) is after the
// default region and admin user have been seeded — both prerequisites for the
// admin POST below.
async function waitForApi() {
  for (let attempt = 1; attempt <= 60; attempt++) {
    try {
      const res = await fetch(`${base}/api/health`, { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })
      if (res.ok) return
    } catch {
      // not up yet
    }
    await sleep(5000)
  }
  throw new Error(`register-runners: ${base}/api/health not ready after 5 minutes`)
}

async function register({ name, apiKey }: any) {
  // 201 = created, 409 = already exists (idempotent). A 429, 5xx, or network error is
  // transient — the API passed /api/health but a single request can still blip — so
  // retry with backoff before failing the deploy. A 4xx (bad payload / auth) is a real
  // client error and fails immediately; no retry would help.
  const maxAttempts = 4
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    let res
    try {
      res = await fetch(`${base}/api/admin/runners`, {
        method: 'POST',
        signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
        headers: {
          'content-type': 'application/json',
          authorization: `Bearer ${ADMIN_API_KEY}`,
        },
        body: JSON.stringify({ name, apiKey, apiVersion: '2', regionId: REGION_ID }),
      })
    } catch (err) {
      if (attempt === maxAttempts) throw new Error(`register-runners: ${name} failed (network): ${err}`)
      await sleep(attempt * 2000)
      continue
    }

    if (res.status === 201) {
      console.log(`register-runners: ${name} registered`)
      return
    }
    if (res.status === 409) {
      console.log(`register-runners: ${name} already registered`)
      return
    }
    const body = await res.text().catch(() => '')
    if (isRetryableStatus(res.status) && attempt < maxAttempts) {
      console.warn(`register-runners: ${name} attempt ${attempt} got ${res.status}; retrying`)
      await sleep(attempt * 2000)
      continue
    }
    throw new Error(`register-runners: ${name} failed (${res.status}): ${body}`)
  }
}

await waitForApi()
for (const runner of runners) {
  await register(runner)
}
console.log(`register-runners: done (${runners.length} runner(s))`)