Skip to content

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

16.7 KB
Raw Blame
/*
 * Which runner binary a deploy installs, resolved from the checkout.
 *
 * The same question `artifacts/source.ts` and `artifacts/runner.ts` answer on
 * the incumbent path, answered here without reaching into that tree — mdeploy is
 * a package whose only dependencies are mstage and mbuild.
 *
 * Two sources, one shape:
 *
 *   release → the GHCR manifest and archive pinned by the release candidate
 *   build   → an object staged for one commit, read with the host's own identity
 *             rather than published; S3 on AWS, Cloud Storage on GCP, one key
 *
 * The version is the *checkout's*, not a stage's setting: the workspace
 * `Cargo.toml` is what the release workflow publishes under, and `VERSION`
 * overrides it for a run that means to install a different published release.
 * Nothing about it is stored — a store value would let a stage pin a fleet to
 * whatever was current the day someone seeded it, and would drift from the
 * commit the rest of the deploy is shipping.
 *
 * Resolution is synchronous. Hosts verify release manifest and archive digests,
 * or the staged checksum sidecar for a commit build, before installation.
 *
 * Every string here is interpolated into bash that runs as root on a host, so
 * each is validated where it enters rather than where it is spent.
 */

import { releaseRunner, runnerRegistry, registryDownload, type ReleaseRunner } from './runner-registry.ts'
import { quote } from 'shell-quote'
import { readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'

export class RunnerBinaryError extends Error {
  constructor(message: string) {
    super(message)
    this.name = 'RunnerBinaryError'
  }
}

/** Stable X.Y.Z only. A prerelease has no ordering the upgrade guard can use. */
const STABLE_VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/
/** Full git object names only: an abbreviation addresses a different object. */
const COMMIT_REF = /^[0-9a-f]{40}$/
/** The S3 rules that matter here: lowercase, no underscores, 3-63 characters. */
const BUCKET_NAME = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/

export type RunnerBinarySource = 'release' | 'build'

/** What a deploy selected, before it is turned into addresses. */
export type RunnerBinarySelector =
  | { kind: 'release'; version: string }
  | { kind: 'build'; version: string; ref: string }

/** The two addresses a source resolves to, and how each is read. */
export type RunnerArtifact = {
  release?: ReleaseRunner
  tarballName: string
  tarballUrl: string
  checksumUrl: string
  /** Public HTTPS, or an object only this stage's own identity may read. */
  transport: 'https' | 's3' | 'gcs'
}

/**
 * Where a build-mode object is staged, and which CLI reads it back.
 *
 * The cloud is carried rather than inferred from the bucket: `s3://` and
 * `gs://` are two transports on the host — one `aws s3 cp`, one `gcloud storage
 * cp` — and a name alone says nothing about which. Both halves come from the
 * composition root, which is the only place that knows the cloud it is
 * deploying into.
 */
export type ArtifactStaging = { cloud: 'aws' | 'gcp'; bucket: string }

/** What the stack installs, and what a live host must report once it has. */
export type ResolvedRunnerBinary = RunnerArtifact & {
  source: RunnerBinarySource
  /** The stable version this checkout publishes under. */
  version: string
  /**
   * The identity a host serving this binary reports on its health route.
   *
   * A build carries its commit as semver build metadata, because two builds of
   * one checkout are otherwise indistinguishable on the wire — and an upgrade
   * that could not tell them apart would skip every dev deploy after the first.
   */
  identity: string
}

const trimmed = (environment: NodeJS.ProcessEnv, key: string): string | null => environment[key]?.trim() || null

/**
 * `RUNNER_*` wins over `BOXLITE_*`, and the key that answered is reported.
 *
 * Two levels because the two artifacts are staged by different things: CI
 * publishes the images and the runner for one commit and sets the global key,
 * while staging only a runner has to say so — otherwise the API would resolve a
 * commit image that nothing pushed. Reporting the key is what lets a failure
 * name the variable an operator actually set rather than the global one they may
 * never have touched.
 */
const componentOrGlobal = (
  environment: NodeJS.ProcessEnv,
  componentKey: string,
  globalKey: string,
): { key: string; value: string | null } => {
  const own = trimmed(environment, componentKey)
  if (own) return { key: componentKey, value: own }
  return { key: globalKey, value: trimmed(environment, globalKey) }
}

/**
 * The version this checkout publishes under.
 *
 * Read from the workspace `Cargo.toml`, found by walking up from the config's
 * own directory rather than from a path relative to this file: `mdeploy` is
 * installed as a package, so its own location says nothing about the repository
 * it is deploying.
 *
 * One marker, not two. The incumbent's `findRepositoryRoot` also requires
 * `apps/infra/package.json`, which is this repository's own shape — a name
 * mdeploy has no business knowing.
 */
export const readWorkspaceVersion = ({ from }: { from: string }): string => {
  let directory = resolve(from)
  for (;;) {
    const candidate = join(directory, 'Cargo.toml')
    let contents: string
    try {
      contents = readFileSync(candidate, 'utf8')
    } catch {
      const parent = dirname(directory)
      if (parent === directory) {
        throw new RunnerBinaryError(
          `could not find a workspace Cargo.toml above ${resolve(from)} to read the runner version from. ` +
            'Set VERSION to name a published release explicitly',
        )
      }
      directory = parent
      continue
    }
    const version = contents.match(/^version\s*=\s*"(.+?)"/m)?.[1]
    if (!version) {
      throw new RunnerBinaryError(`${candidate} has no top-level \`version = "X.Y.Z"\` to read the runner version from`)
    }
    return version
  }
}

/**
 * Release or build, and the commit a build is addressed by.
 *
 * The default is `release`, which is what an unconfigured deploy has always
 * installed. A build is opt-in for the reason the incumbent resolver records: a
 * rule derived from the stage would silently change which binary a plain deploy
 * puts onto a state-holding host.
 */
export const selectRunnerBinary = ({
  environment,
  workspaceVersion,
}: {
  environment: NodeJS.ProcessEnv
  workspaceVersion: string
}): RunnerBinarySelector => {
  const configuredVersion = trimmed(environment, 'VERSION')
  const version = (configuredVersion ?? workspaceVersion).replace(/^v/, '')
  if (!STABLE_VERSION.test(version)) {
    throw new RunnerBinaryError(
      configuredVersion
        ? `VERSION must be a stable semantic version (X.Y.Z); got ${JSON.stringify(configuredVersion)}`
        : `the workspace version ${JSON.stringify(workspaceVersion)} is not a stable semantic version (X.Y.Z)`,
    )
  }

  const { key: sourceKey, value: configured } = componentOrGlobal(
    environment,
    'RUNNER_ARTIFACT_SOURCE',
    'BOXLITE_ARTIFACT_SOURCE',
  )
  const kind = configured ?? 'release'
  if (kind !== 'release' && kind !== 'build') {
    throw new RunnerBinaryError(`${sourceKey} must be "release" or "build"; got ${JSON.stringify(configured)}`)
  }
  if (kind === 'release') return { kind, version }

  const { key: refKey, value: ref } = componentOrGlobal(environment, 'RUNNER_ARTIFACT_REF', 'BOXLITE_ARTIFACT_REF')
  if (!ref || !COMMIT_REF.test(ref.toLowerCase())) {
    throw new RunnerBinaryError(
      `a build-mode runner binary is addressed by the commit it was produced from; set ${refKey} to a ` +
        `full git commit sha (40 hex characters), got ${JSON.stringify(ref ?? '')}`,
    )
  }
  return { kind, version, ref: ref.toLowerCase() }
}

/**
 * Where a build-mode binary is staged, composed rather than configured.
 *
 * The bootstrap owns this bucket for the same ordering reason it owns the image
 * repository: CI stages the object before the stack can consume one, so the
 * consumer cannot also create its own input. That makes the name a rule rather
 * than a setting — and one three things have to agree on: the read-only grant
 * the runner gets, the address this module resolves, and the destination
 * `src/runner-build.ts` uploads to. It is spelled once, here.
 *
 * The shape is the incumbent's (`deployment/environment.ts`'s
 * `awsResourceName`), because mdeploy adopts the bucket that path created rather
 * than making a second one beside it. That path joins `[app, 'app', stage, …]`
 * with `app` as a literal, and this repository's app already carries it — so the
 * segment is spelled once, here, rather than twice.
 */
export const runnerArtifactsBucket = ({
  app,
  stage,
  accountId,
}: {
  app: string
  stage: string
  /** S3's namespace is global, so the bucket needs a qualifier no other account can claim. */
  accountId: string
}): string => `${app}-${stage}-artifacts-${accountId}`

/**
 * The same rule on Google, qualified by the project instead of the account.
 *
 * Cloud Storage shares one global namespace with every other project, so the
 * name needs a qualifier nobody else can claim; the project id is that, and it
 * is the one the deploy already carries. Composed rather than recorded for the
 * same reason as the AWS name: the grant, the address and `runner:build`'s
 * destination all have to agree, and a name three places spell is a name that
 * drifts.
 */
export const gcpRunnerArtifactsBucket = ({
  app,
  stage,
  project,
}: {
  app: string
  stage: string
  project: string
}): string => `${app}-${stage}-artifacts-${project}`

/**
 * The two addresses one selector resolves to.
 *
 * The names are the publisher's, not this file's choice: `build-runner-binary
 * .yml` uploads exactly these, and a deploy that composed a different name would
 * resolve an address nothing published.
 */
export const runnerArtifactFor = ({
  selector,
  metadata,
  staging = null,
}: {
  selector: RunnerBinarySelector
  metadata?: string
  /** Where a build-mode object is staged. The root knows it; a release needs none. */
  staging?: ArtifactStaging | null
}): RunnerArtifact => {
  if (selector.kind === 'release') {
    const tarballName = `boxlite-runner-v${selector.version}-linux-amd64.tar.gz`
    const release = releaseRunner(selector.version, metadata)
    return {
      release,
      tarballName,
      tarballUrl: `${runnerRegistry}/blobs/${release.pins?.archive ?? '$RUNNER_ARCHIVE'}`,
      checksumUrl: `${runnerRegistry}/manifests/${release.pins?.manifest ?? '$RUNNER_MANIFEST'}`,
      transport: 'https',
    }
  }
  const bucket = staging?.bucket.trim()
  if (!staging || !bucket) {
    throw new RunnerBinaryError(
      'a build-mode runner binary is staged in this stage’s artifacts bucket, and this deploy was handed ' +
        'none. Run `npm run bootstrap -- --stage <stage>` to create it, or install a published release',
    )
  }
  if (!BUCKET_NAME.test(bucket)) {
    throw new RunnerBinaryError(`the artifacts bucket ${JSON.stringify(bucket)} is not a valid bucket name`)
  }
  const tarballName = `boxlite-runner-v${selector.version}-${selector.ref}-linux-amd64.tar.gz`
  const key = `runner/${selector.ref}/${tarballName}`
  // One key on both clouds, because `runner:build` composes it from the same
  // rule: a stage that changed clouds would otherwise stage under one name and
  // read under another.
  const scheme = staging.cloud === 'aws' ? 's3' : 'gs'
  return {
    tarballName,
    tarballUrl: `${scheme}://${bucket}/${key}`,
    checksumUrl: `${scheme}://${bucket}/${key}.sha256`,
    transport: staging.cloud === 'aws' ? 's3' : 'gcs',
  }
}

const AWS_REGION_NAME = /^[a-z0-9-]+$/

/**
 * How one of those addresses is read, on the host.
 *
 * Shared by both install paths — first boot and every later upgrade — so "how do
 * we get this URL" is answered once. Bounded on purpose: a transport that can
 * hang forever may continue after the deploy supervising it already failed, and
 * swap a binary nobody is watching for.
 */
export const artifactFetchCommand = ({
  artifact,
  url,
  destination,
  region = null,
}: {
  artifact: Pick<RunnerArtifact, 'transport'>
  url: string
  destination: string
  /** Required for an `s3://` address, meaningless for a public one. */
  region?: string | null
}): string => {
  if (artifact.transport === 'https') {
    return (
      `curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' ` +
      `--connect-timeout 10 --max-time 300 --retry 5 --retry-delay 2 --retry-connrefused ` +
      `--retry-max-time 300 "${url}" -o "${destination}"`
    )
  }
  if (artifact.transport === 'gcs') {
    // The same CLI the host already reads its secrets with, so nothing new is
    // installed for this. Its own retries cover a transient 5xx, and the bucket
    // is reached with the host's own service account rather than a credential
    // this script would have to carry.
    return `gcloud storage cp "${url}" "${destination}"`
  }
  if (!AWS_REGION_NAME.test(region ?? '')) {
    throw new RunnerBinaryError(`reading ${url} needs the region that bucket lives in, got ${JSON.stringify(region ?? '')}`)
  }
  return `aws --cli-connect-timeout 10 --cli-read-timeout 300 s3 cp --region ${region} "${url}" "${destination}"`
}

// Upgrades unpack under a private directory created by the host. Expand only that prefix.
const checksumPath = (path: string): string =>
  path.startsWith('$WORK/') ? '"$WORK"/' + quote([path.slice('$WORK/'.length)]) : quote([String(path)])

/** Match the literal archive name, then verify its lowercase SHA-256 before installation. */
export const verifyAgainstManifest = ({
  tarballName,
  tarball,
  manifest,
}: {
  tarballName: string
  /** Shell path of the downloaded tarball. */
  tarball: string
  /** Shell path of the downloaded `.sha256` sidecar. */
  manifest: string
}): string => {
  return `export RUNNER_TARBALL_NAME=${quote([String(tarballName)])}
EXPECTED=$(awk '{
  digest = $1
  sub(/^[^[:space:]]+[[:space:]]+/, "")
  if ($0 == ENVIRON["RUNNER_TARBALL_NAME"] || $0 == "*" ENVIRON["RUNNER_TARBALL_NAME"]) print digest
}' < ${checksumPath(manifest)})
[ -n "$EXPECTED" ] || { echo "FATAL: the checksum manifest does not name $RUNNER_TARBALL_NAME" >&2; exit 1; }
case "$EXPECTED" in
  *[!0-9a-f]* | "") echo "FATAL: the checksum manifest names no lowercase sha256 for $RUNNER_TARBALL_NAME" >&2; exit 1 ;;
esac
[ \${#EXPECTED} -eq 64 ] || { echo "FATAL: the checksum manifest digest is not 64 hex characters" >&2; exit 1; }
ACTUAL=$(sha256sum < ${checksumPath(tarball)} | awk '{print $1}')
[ "$EXPECTED" = "$ACTUAL" ] || {
  echo "FATAL: runner checksum mismatch (want $EXPECTED got $ACTUAL)" >&2
  exit 1
}
echo "runner tarball checksum verified ($ACTUAL)"`
}

/** Render both downloads and verification together so a host cannot install unverified bytes. */
export function fetchVerifiedRunner(binary: RunnerArtifact, phase: 'boot' | 'upgrade', region?: string | null): string {
  const [tarball, manifest] = phase === 'boot'
    ? ['/tmp/boxlite-runner.tar.gz', '/tmp/boxlite-runner.sha256'] as const
    : ['$WORK/runner.tar.gz', '$WORK/runner.sha256'] as const
  if (binary.release) return `RUNNER_ARCHIVE_FILE="${tarball}"\nRUNNER_MANIFEST_FILE="${manifest}"\n${registryDownload}`
  return [
    artifactFetchCommand({ artifact: binary, url: binary.tarballUrl, destination: tarball, region }),
    artifactFetchCommand({ artifact: binary, url: binary.checksumUrl, destination: manifest, region }),
    verifyAgainstManifest({ tarballName: binary.tarballName, tarball, manifest }),
  ].join('\n')
}

/**
 * One deploy's runner binary, from the checkout to the addresses a host reads.
 *
 * Called by both composition roots — `sst.config.ts` and `pulumi/program.ts` —
 * rather than by each provider, for the same reason `readStackEnvironment` is:
 * two copies that drifted would install a different binary on one cloud, found
 * months later with nothing reporting it. The roots are also where the
 * artifacts bucket is known, which is what a build-mode address needs.
 */
export const resolveRunnerBinary = ({
  environment,
  configRoot,
  staging = null,
  readVersion = readWorkspaceVersion,
}: {
  environment: NodeJS.ProcessEnv
  /** Where `mdeploy.config.json` was found; the workspace is at or above it. */
  configRoot: string
  staging?: ArtifactStaging | null
  /** Injected so the resolution is provable without a checkout. */
  readVersion?: typeof readWorkspaceVersion
}): ResolvedRunnerBinary => {
  const selector = selectRunnerBinary({ environment, workspaceVersion: readVersion({ from: configRoot }) })
  return {
    ...runnerArtifactFor({ selector, staging, metadata: environment.BOXLITE_RELEASE_DIGESTS }),
    source: selector.kind,
    version: selector.version,
    identity: selector.kind === 'build' ? `${selector.version}+${selector.ref}` : selector.version,
  }
}