The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
import { setTimeout as sleep } from 'node:timers/promises'
import { text } from 'node:stream/consumers'
class RegistryError extends Error {}
const execFileAsync = promisify(execFile)
async function waitForApi(api) {
const endpoint = new URL('/api/health', api)
const deadline = performance.now() + 300_000
for (;;) {
const remaining = Math.ceil(deadline - performance.now())
if (remaining <= 0) break
try {
const response = await fetch(endpoint, {
redirect: 'manual',
signal: AbortSignal.timeout(Math.min(5000, remaining)),
})
await response.body?.cancel()
if (response.ok && performance.now() < deadline) return
} catch {
// A cold public endpoint can refuse connections before serving its first revision.
}
await sleep(Math.max(0, Math.min(5000, deadline - performance.now())))
}
throw new RegistryError('Public API /api/health not ready after 5 minutes')
}
async function currentInstanceId(project, zone, vmName) {
let id
try {
const { stdout } = await execFileAsync(
'gcloud',
['compute', 'instances', 'describe', vmName, `--project=${project}`, `--zone=${zone}`, '--format=value(id)'],
{ timeout: 30_000 },
)
id = stdout.trim()
} catch {
throw new RegistryError(`GCE lookup failed for VM ${vmName}`)
}
if (!/^[1-9][0-9]{0,19}$/.test(id)) throw new RegistryError(`Invalid GCE instance ID for VM ${vmName}`)
return id
}
async function registeredId(endpoint, { name, token }) {
for (let attempt = 1; attempt <= 6; attempt++) {
let response
try {
response = await fetch(endpoint, {
headers: { authorization: `Bearer ${token}` },
redirect: 'manual',
signal: AbortSignal.timeout(5000),
})
} catch {
// The API can still be starting on the first deployment.
}
if (response?.status === 200) {
// The full response contains the Runner API key. Only the UUID leaves this process.
let runner
try {
runner = await response.json()
} catch {
throw new RegistryError(`Invalid identity response for Runner ${name}`)
}
if (
runner?.name !== name ||
runner.apiVersion !== '2' ||
typeof runner.id !== 'string' ||
!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(runner.id)
) {
throw new RegistryError(`Unexpected registered identity for Runner ${name}`)
}
return runner.id.toLowerCase()
}
const status = response?.status
await response?.body?.cancel()
if (attempt === 6 || (status && status !== 401 && status !== 429 && status < 500)) {
throw new RegistryError(`Cannot identify Runner ${name} (${status ?? 'connection failed'})`)
}
await sleep(attempt * 1000)
}
}
async function main() {
const api = new URL(process.env.API_URL)
if (api.protocol !== 'https:' || api.username || api.password || api.search || api.hash || api.pathname !== '/') {
throw new RegistryError('Expected an HTTPS API origin')
}
const endpoint = new URL('/api/runners/me', api)
const fleet = JSON.parse(await text(process.stdin))
if (!Array.isArray(fleet) || !fleet.length) throw new RegistryError('Runner fleet is empty')
const { GCP_PROJECT: project, GCP_ZONE: zone } = process.env
if (!project || !zone) throw new RegistryError('GCE project and zone are required')
await waitForApi(api)
const machines = new Map()
const ids = new Set()
for (const host of fleet) {
if (
typeof host?.vmName !== 'string' ||
!/^[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?$/.test(host.vmName) ||
typeof host.name !== 'string' ||
!host.name ||
/[\x00-\x1f\x7f]/.test(host.name) ||
typeof host.token !== 'string' ||
!host.token
) {
throw new RegistryError('Invalid GCE Runner assignment')
}
const instanceId = await currentInstanceId(project, zone, host.vmName)
if (machines.has(instanceId)) throw new RegistryError(`Duplicate GCE instance for Runner ${host.name}`)
const id = await registeredId(endpoint, host)
if (ids.has(id)) throw new RegistryError(`Duplicate registered UUID for Runner ${host.name}`)
ids.add(id)
machines.set(instanceId, id)
}
process.stdout.write(JSON.stringify({ gcp_iit: Object.fromEntries(machines) }))
}
main().catch((error) => {
// Parse errors can quote input containing credentials.
console.error(
error instanceof RegistryError ? error.message : 'Could not read the GCP Runner registry inputs or API reply',
)
process.exitCode = 1
})