Skip to content

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

6.3 KB
Raw Blame
import type { EnvRow } from './db'
import type { BoxUsage } from '@boxlite-ai/cloud-app-shared'
import { requestJson } from './http'

export interface ServiceState {
  name: string
  state: string
  pid?: number | null
  restarts?: number
  last_exit_code?: number | null
  health?: string
  port?: number | null
  /** Resident memory of the service and its children (supervisors from 2026-10-24 on). */
  memory_bytes?: number | null
}

/** What the supervisor reports on POST /sv/v1/status. */
export interface SupervisorStatus {
  deployment: number
  phase: 'building' | 'built' | 'starting' | 'live' | 'failed'
  step?: string
  error?: string
  log_tail?: string[]
  services?: ServiceState[]
}

export interface SupervisorHealth {
  /** Wire protocol (contracts §2.3); see SUPERVISOR_PROTOCOL. */
  version?: number
  build?: string
  /** The supervisor runs as root and app code as boxlite (§2.1). */
  root?: boolean
  /** boxlite's sudo is limited to the launcher (§2.1). */
  sudo_locked?: boolean
  ok: boolean
  deployment: number | null
  spec_version?: number | null
  services: ServiceState[]
  /** Memory and disk of the whole Box; absent from an older supervisor, null where unreadable. */
  box?: BoxUsage | null
}

/**
 * The two credentials of an app Box's gateway (security review F3). They never travel together:
 *
 * - forwarding: `X-BoxLite-Gateway: <forward_secret>` on every end-user request the proxy forwards.
 *   It only lets traffic through to the app; the gateway refuses it on `/.boxlite/sv/*`.
 * - control: `X-BoxLite-Control: <control_secret>` on the CP's own `/.boxlite/sv/*` calls. It never
 *   reaches the app.
 *
 * Every caller builds its headers here, so neither secret can end up on the wrong kind of request.
 */
const FORWARD_HEADER = 'x-boxlite-gateway'
const CONTROL_HEADER = 'x-boxlite-control'

export function forwardHeaders(env: Pick<EnvRow, 'forward_secret'>): Record<string, string> {
  return { [FORWARD_HEADER]: env.forward_secret }
}

export type TunnelHeaders = (boxUrl: string) => Promise<Record<string, string>>

/** The Proxy consumes the tunnel token before forwarding the Supervisor control secret. */
export async function supervisorControlHeaders(
  boxUrl: string,
  secret: string,
  tunnelHeaders?: TunnelHeaders,
  signal?: AbortSignal,
): Promise<Record<string, string>> {
  signal?.throwIfAborted()
  if (!signal || !tunnelHeaders) return { ...(await tunnelHeaders?.(boxUrl)), [CONTROL_HEADER]: secret }

  let onAbort!: () => void
  const aborted = new Promise<never>((_, reject) => {
    onAbort = () => reject(signal.reason)
    signal.addEventListener('abort', onAbort, { once: true })
  })
  try {
    const lookup = Promise.resolve().then(() => {
      signal.throwIfAborted()
      return tunnelHeaders(boxUrl)
    })
    const headers = await Promise.race([lookup, aborted])
    signal.throwIfAborted()
    return { ...headers, [CONTROL_HEADER]: secret }
  } finally {
    signal.removeEventListener('abort', onAbort)
  }
}

/**
 * CP → supervisor calls (contracts §2.3), made through the Box tunnel. `secret` is the
 * env's control secret (`env.control_secret`); the gateway refuses control calls without it.
 */
export interface SupervisorClient {
  health(boxUrl: string, secret: string, signal?: AbortSignal): Promise<SupervisorHealth>
  /** Fetch + extract + build release N, then report `built`; the old release keeps serving. */
  deploy(boxUrl: string, secret: string, release: number): Promise<void>
  /** Start release N's services, health-gate, flip traffic; reports `starting` then `live|failed`. */
  activate(boxUrl: string, secret: string, release: number): Promise<void>
  /** Re-start onto an existing release dir (no build); same health gate as activate. */
  rollback(boxUrl: string, secret: string, release: number): Promise<void>
  restart(boxUrl: string, secret: string, service: string | null): Promise<void>
  logs(boxUrl: string, secret: string, source: string | null, tail: number): Promise<unknown>
}

export class HttpSupervisorClient implements SupervisorClient {
  constructor(private readonly tunnelHeaders?: TunnelHeaders) {}
  private headers(boxUrl: string, secret: string, signal?: AbortSignal): Promise<Record<string, string>> {
    return supervisorControlHeaders(boxUrl, secret, this.tunnelHeaders, signal)
  }
  async health(boxUrl: string, secret: string, signal?: AbortSignal): Promise<SupervisorHealth> {
    const timeout = AbortSignal.timeout(10_000)
    const deadline = signal ? AbortSignal.any([signal, timeout]) : timeout
    const { body } = await requestJson<SupervisorHealth>('supervisor', {
      method: 'GET',
      url: `${boxUrl}/.boxlite/sv/health`,
      headers: await this.headers(boxUrl, secret, deadline),
      signal: deadline,
      redirect: 'manual',
    })
    return body
  }

  async deploy(boxUrl: string, secret: string, release: number): Promise<void> {
    await requestJson('supervisor', {
      method: 'POST',
      url: `${boxUrl}/.boxlite/sv/deploy`,
      headers: await this.headers(boxUrl, secret),
      body: { deployment: release },
      timeoutMs: 30_000,
    })
  }

  async activate(boxUrl: string, secret: string, release: number): Promise<void> {
    await requestJson('supervisor', {
      method: 'POST',
      url: `${boxUrl}/.boxlite/sv/activate`,
      headers: await this.headers(boxUrl, secret),
      body: { deployment: release },
      timeoutMs: 30_000,
    })
  }

  async rollback(boxUrl: string, secret: string, release: number): Promise<void> {
    await requestJson('supervisor', {
      method: 'POST',
      url: `${boxUrl}/.boxlite/sv/rollback`,
      headers: await this.headers(boxUrl, secret),
      body: { deployment: release },
      timeoutMs: 30_000,
    })
  }

  async restart(boxUrl: string, secret: string, service: string | null): Promise<void> {
    await requestJson('supervisor', {
      method: 'POST',
      url: `${boxUrl}/.boxlite/sv/restart`,
      headers: await this.headers(boxUrl, secret),
      body: { service },
      timeoutMs: 30_000,
    })
  }

  async logs(boxUrl: string, secret: string, source: string | null, tail: number): Promise<unknown> {
    const q = new URLSearchParams({ tail: String(tail) })
    if (source) q.set('source', source)
    const { body } = await requestJson('supervisor', {
      method: 'GET',
      url: `${boxUrl}/.boxlite/sv/logs?${q}`,
      headers: await this.headers(boxUrl, secret),
      timeoutMs: 15_000,
    })
    return body
  }
}