Skip to content

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

18.5 KB
Raw Blame
/*
 * `npm run runner:update -- --stage <stage> [--version X.Y.Z] [--allow-downgrade]`
 *
 * Rolling the fleet's binary by hand, outside a deploy — and the only way to
 * move it *backwards*.
 *
 * A deploy already rolls the fleet forward: `stack/runner-upgrade.ts` renders
 * the payload, AWS chains hosts and GCP observes a canary before a parallel batch.
 * A deploy deliberately cannot downgrade. The payload refuses to replace a host
 * serving something newer than the target, because a host ahead of the declared
 * version is usually a deliberate hand-install and silently reverting it during
 * an unrelated deploy is a nasty surprise. That refusal exits 0, so a rollback
 * attempted by editing the version would report success and change nothing.
 *
 * So the force lives here instead of in the deploy, and that is the whole design
 * decision: a rollback is a decision someone makes, at a moment, watching the
 * output — not a state a stage's configuration can be left in. A stored flag
 * would be a stage that quietly permits downgrades on every future deploy, which
 * is exactly the surprise the guard exists to prevent.
 *
 * Everything else is shared with the deploy rather than reimplemented: the same
 * `renderUpgradePayload` converge/verify/swap/rollback script, the same
 * transports in `upgrade-runners.ts`, and the cloud's rollout sequencing that
 * stops on a failed batch. This file only answers the two questions a deploy
 * answers structurally — which hosts, and in what order.
 *
 * Release targets only. A build-mode binary is addressed by a commit and staged
 * per stage; installing one is what a deploy of that commit does. A rollback
 * names a published version.
 */

import { mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { parseInvocation, type Options } from 'mstage/cli'
import { loadConfig } from 'mstage/config'
import { resolveHome } from 'mstage/home'
import { run as mstage } from 'mstage/run'
import { resolveScope } from 'mstage/scope'
import { deployRoot } from './config.ts'
import {
  sleepSeconds,
  spawnWith,
  upgradeFleetOverPolicy,
  upgradeOne,
  type RunCommand,
  type UpgradeOneRequest,
} from './upgrade-runners.ts'
import { encodeUpgradePayload, renderPolicyScripts } from '../stack/runner-upgrade.ts'
import { ENROLLED_RUNNER_POLICY } from '../stack/runner-enrollment.ts'
import { RUNNER_PORT, runnerNamePrefix, runnerPolicyBatches } from '../stack/runners.ts'
import { resolveRunnerBinary } from '../stack/runner-binary.ts'
import { zoneIn } from '../stack/providers/gcp/index.ts'

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

const USAGE = [
  'usage: npm run runner:update -- --stage <stage> [--version <X.Y.Z>] [--host <name>[,<name>…]]',
  '                                [--allow-downgrade] [--confirm]',
  '',
  '  --version          a published release. Defaults to the checkout’s own version.',
  '  --host             only these hosts, by the name the console shows. Default: every one.',
  '  --allow-downgrade  replace a host serving something NEWER. This is the rollback.',
  '  --confirm          required for a stage mstage.config.json marks protected.',
].join('\n')

/**
 * This tool's own switches. mstage parses them but never advertises them.
 *
 * `--version` and `--confirm` are not here because mstage already knows both —
 * which is worth having rather than shadowing: its own guard catches the
 * `npm run … --version 0.9.5` that npm swallows before this ever runs.
 */
const OWN_OPTIONS = { flags: ['allow-downgrade'], values: ['host'] }

/**
 * How each cloud names a runner, which is also how each is found.
 *
 * The providers set both: AWS tags the instance `Name=boxlite-runner-*`, GCP
 * names it the same thing. Discovery matches that pattern rather than reading
 * the engine's state — a state file is one deploy's record, and this tool has to
 * work on a fleet whose last deploy failed halfway.
 */
// The prefix is `<app>-<stage>-runner`, built by the same `runnerNamePrefix`
// the stack names a host with — one definition, so discovery cannot drift from
// creation, and a roll in one stage never sees another stage's hosts.

export type Host = { target: string; label: string; policyLabel?: string }

/**
 * The order the fleet was created in, which is the order to visit it in.
 *
 * Not the API's order — neither cloud promises one, and a roll that took what
 * it was given would visit the fleet differently every run, which defeats the
 * point of going one at a time: after a failure, *which* hosts are still
 * serving has to be knowable.
 *
 * Not lexicographic either, and that is the part worth stating. `stack-env.ts`
 * names the first host `boxlite-runner-default` and every later one
 * `boxlite-runner-<n>`, so sorting by string puts `-2` before `-default` and
 * `-10` before `-2`. The fleet's own order is: the first host, then the rest by
 * number — the same order the deploy's `dependsOn` chain walks.
 */
const numbered = (label: string, prefix: string): number | null => {
  if (label === prefix) return 0
  if (!label.startsWith(`${prefix}-`)) return null
  const suffix = label.slice(prefix.length + 1)
  return /^[0-9]+$/.test(suffix) ? Number(suffix) : null
}

/** The comparator for one stage's fleet, which is the only fleet it explains. */
export const compareHostsIn = (prefix: string) => (left: Host, right: Host): number => {
  const [a, b] = [numbered(left.label, prefix), numbered(right.label, prefix)]
  // A host neither pattern explains — renamed by hand, or from another fleet —
  // goes last, in its own stable order, rather than jumping the queue.
  if (a === null || b === null) {
    if (a === null && b === null) return left.label.localeCompare(right.label)
    return a === null ? 1 : -1
  }
  return a - b
}

/** Every running runner in the stage's region, in a stable order. */
const awsHosts = (region: string, run: RunCommand, prefix: string): Host[] => {
  const listed = run('aws', [
    'ec2',
    'describe-instances',
    '--region',
    region,
    '--filters',
    `Name=tag:Name,Values=${prefix}*`,
    'Name=instance-state-name,Values=running',
    '--query',
    'Reservations[].Instances[].[InstanceId,Tags[?Key==`Name`].Value|[0]]',
    '--output',
    'text',
  ])
  if (!listed.ok) throw new RunnerUpdateError(`could not list the fleet: ${listed.stderr || '(no stderr)'}`)
  return listed.stdout
    .split('\n')
    .map((line) => line.trim().split(/\s+/))
    .filter(([id]) => id && id !== 'None')
    .map(([target, label]) => ({ target: target as string, label: label ?? (target as string) }))
    .sort(compareHostsIn(prefix))
}

const gcpHosts = ({
  project,
  zone,
  run,
  prefix,
  stage,
}: {
  project: string
  zone: string
  run: RunCommand
  prefix: string
  stage: string
}): Host[] => {
  const listed = run('gcloud', [
    'compute',
    'instances',
    'list',
    `--project=${project}`,
    `--zones=${zone}`,
    `--filter=name~^${prefix} AND status=RUNNING`,
    '--format=json(name,labels)',
  ])
  if (!listed.ok) throw new RunnerUpdateError(`could not list the fleet: ${listed.stderr || '(no stderr)'}`)
  return (JSON.parse(listed.stdout) as { name: string; labels?: Record<string, string> }[])
    .map(({ name, labels }) => {
      if (labels?.['boxlite-stage'] !== stage) {
        throw new RunnerUpdateError(`${name} has no matching stage label; reconcile the fleet through mdeploy`)
      }
      return { target: name, label: name, policyLabel: labels['boxlite-runner'] }
    })
    .sort(compareHostsIn(prefix))
}

/** The subset an operator named, or all of them. Naming one that is not there is a mistake, not a filter. */
/**
 * Where the rewritten policy is handed to gcloud.
 *
 * A file rather than stdin: `os-policy-assignments update` takes `--file`, and
 * the scripts inside carry newlines and shell metacharacters that no command
 * line survives. Written under the OS temp directory, which is this process's
 * own and goes away with it.
 */
const writePolicyFile = (contents: string): string => {
  const path = join(mkdtempSync(join(tmpdir(), 'boxlite-runner-policy-')), 'policy.json')
  writeFileSync(path, contents, { mode: 0o600 })
  return path
}

const selected = (hosts: Host[], named: string[]): Host[] => {
  if (named.length === 0) return hosts
  const missing = named.filter((name) => !hosts.some((host) => host.label === name || host.target === name))
  if (missing.length > 0) {
    throw new RunnerUpdateError(
      `${missing.join(', ')} is not a running runner in this stage. Found: ${hosts.map((host) => host.label).join(', ') || 'none'}`,
    )
  }
  return hosts.filter((host) => named.includes(host.label) || named.includes(host.target))
}

export type UpdateInput = {
  argv: string[]
  environment?: NodeJS.ProcessEnv
  cwd?: string
  log?: (line: string) => void
  checkLogin?: typeof mstage
  resolveHomeWith?: typeof resolveHome
  /** Injected so a roll is provable without an account, a project or a host. */
  run?: RunCommand
  sleep?: (seconds: number) => void
}

export const updateRunners = async ({
  argv,
  environment = process.env,
  cwd = process.cwd(),
  log = console.log,
  checkLogin = mstage,
  resolveHomeWith = resolveHome,
  run: injectedRun,
  sleep = sleepSeconds,
}: UpdateInput): Promise<number> => {
  if (argv[0] === 'help' || argv[0] === '--help') {
    log(USAGE)
    return 0
  }

  const { options, inner } = parseInvocation(['roll', ...argv], environment, OWN_OPTIONS)
  if (inner) throw new RunnerUpdateError(`runner:update takes no inner command.\n${USAGE}`)

  const config = loadConfig({ cwd, environment })
  // `resolveScope` refuses a missing --stage itself, and names the stages this
  // repository declares while doing it.
  const scope = resolveScope({ options: options as Options, config, environment })

  // Same gate a deploy applies, for the same reason: this restarts every runner
  // in the fleet, and boxes on a host take the restart.
  if (scope.protect && options.confirm !== true) {
    throw new RunnerUpdateError(`Stage "${scope.stage}" is protected in ${config.path}. Add --confirm to roll its fleet.`)
  }

  // Named with the stage, because which sign-ins this needs is the stage's
  // question: a repository with stages in both clouds declares both, and
  // without the stage an expired session in the cloud this fleet does not live
  // in would refuse the roll.
  const signedIn = await checkLogin({ argv: ['login', '--stage', scope.stage as string], environment, cwd, log })
  if (signedIn !== 0) throw new RunnerUpdateError('Required sign-ins are missing; run `npm run mstage login -- -f` first')

  const version = (options.version as string | undefined)?.trim()
  const allowDowngrade = options['allow-downgrade'] === true
  const named = (options.host as string | undefined)
    ?.split(',')
    .map((name) => name.trim())
    .filter(Boolean) ?? []

  /*
   * The same resolution the stack does, forced to release.
   *
   * `VERSION` is the selector `stack/runner-binary.ts` already honours, so a
   * named version reaches it the way a deploy's would — one resolver, one set of
   * asset names. The artifact source is pinned to `release` rather than
   * inherited: a build is addressed by a commit and staged per stage, and
   * installing one is what deploying that commit does.
   */
  const binary = resolveRunnerBinary({
    environment: {
      ...environment,
      ...(version ? { VERSION: version } : {}),
      RUNNER_ARTIFACT_SOURCE: 'release',
      BOXLITE_ARTIFACT_SOURCE: 'release',
    },
    configRoot: deployRoot({ cwd, environment }),
  })

  const home = await resolveHomeWith({ scope })
  const { env: credentials } = await home.identity.childEnvironment()
  const run = injectedRun ?? spawnWith({ ...environment, ...credentials })

  const prefix = runnerNamePrefix({ app: config.app, stage: scope.stage as string })
  const hosts = selected(
    home.identity.home === 'aws'
      ? awsHosts(scope.region as string, run, prefix)
      : gcpHosts({
          project: scope.project as string,
          zone: zoneIn(scope.region as string, scope.zone ?? null),
          run,
          prefix,
          stage: scope.stage as string,
        }),
    named,
  )
  if (hosts.length === 0) throw new RunnerUpdateError(`no running runner in ${config.app}/${scope.stage}`)

  const payload = encodeUpgradePayload({
    identity: binary.identity,
    binary,
    port: RUNNER_PORT,
    region: home.identity.home === 'aws' ? (scope.region as string) : null,
    allowDowngrade,
  })

  log(`==> rolling ${hosts.length} host(s) in ${config.app}/${scope.stage} to ${binary.identity}`)
  log(`==> artifact: ${binary.tarballUrl}`)
  if (allowDowngrade) log('==> --allow-downgrade: a host serving something newer WILL be replaced')

  /*
   * On GCP the fleet is declared, not commanded.
   *
   * A deploy owns disjoint canary and fleet assignments, so a hand roll
   * rewrites those assignments in order rather than reaching into each machine.
   * There is no ssh here that an account outside the instance's organization could open
   * anyway. The consequences are the two lines printed below: the roll covers
   * the whole fleet, and it lasts until the next deploy re-asserts the identity
   * the checkout declares.
   */
  if (home.identity.home === 'gcp') {
    if (named.length > 0) {
      throw new RunnerUpdateError(
        '--host is not available on GCP: the fleet is one declared state, and narrowing it would leave ' +
          'the assignment pointing at a single machine after the roll',
      )
    }
    const zone = zoneIn(scope.region as string, scope.zone ?? null)
    if (hosts[0]!.target !== prefix)
      throw new RunnerUpdateError(`the canary ${prefix} must be running before rolling the fleet`)
    const labels = hosts.map((host) => host.policyLabel)
    if (labels.some((label) => !label) || new Set(labels).size !== labels.length) {
      throw new RunnerUpdateError('GCP runner labels must be present and unique; reconcile the fleet through mdeploy')
    }
    const batches = runnerPolicyBatches({ app: config.app, stage: scope.stage as string }, hosts)
    // Preflight every batch before the first write, including migration from
    // the original assignment that selected the entire fleet.
    const deployed = batches.map((batch) => {
      const result = run('gcloud', [
        'compute',
        'os-config',
        'os-policy-assignments',
        'describe',
        batch.name,
        `--project=${scope.project}`,
        `--location=${zone}`,
        '--format=json',
      ])
      if (!result.ok) throw new RunnerUpdateError(`could not read ${batch.name}; run mdeploy before runner:update`)
      const policy = JSON.parse(result.stdout)
      const expected = batch.hosts.map((host) => host.policyLabel).sort()
      const actual = policy.instanceFilter?.inclusionLabels
        ?.map((entry: any) => entry.labels?.['boxlite-runner'])
        .sort()
      if (
        JSON.stringify(actual) !== JSON.stringify(expected) ||
        policy.instanceFilter?.all ||
        policy.instanceFilter?.inclusionLabels?.some(
          (entry: any) =>
            Object.keys(entry.labels ?? {}).length !== 2 || entry.labels?.['boxlite-stage'] !== scope.stage,
        ) ||
        policy.instanceFilter?.exclusionLabels?.length ||
        policy.instanceFilter?.inventories?.length ||
        policy.rollout?.minWaitDuration !== batch.rollout.minWaitDuration ||
        policy.rollout?.disruptionBudget?.fixed !== batch.rollout.disruptionBudget.fixed ||
        policy.rollout?.disruptionBudget?.percent !== batch.rollout.disruptionBudget.percent
      ) {
        throw new RunnerUpdateError(`${batch.name} does not match the canary rollout; run mdeploy before runner:update`)
      }
      const binaryPolicies = policy.osPolicies?.filter(
        (item: { id: string }) => item.id === ENROLLED_RUNNER_POLICY || item.id === 'runner-binary',
      )
      const binaryPolicy = binaryPolicies?.length === 1 ? binaryPolicies[0] : undefined
      const resources = binaryPolicy?.resourceGroups?.flatMap(
        (group: { resources: { id: string }[] }) => group.resources,
      )
      const swaps = resources?.filter((resource: { id: string }) => resource.id === 'swap-binary')
      if (swaps?.length !== 1) {
        throw new RunnerUpdateError(`${batch.name} needs exactly one swap-binary resource; run mdeploy before runner:update`)
      }
      // The deployed group may also enroll the host before starting the binary.
      const scripts = renderPolicyScripts({
        identity: binary.identity,
        binary,
        port: RUNNER_PORT,
        allowDowngrade,
        requireServingVersion: resources.some(
          (resource: { id: string }) => resource.id === 'converge-unit-environment',
        ),
      })
      swaps[0].exec = {
        validate: { interpreter: 'NONE', script: scripts.validate },
        enforce: { interpreter: 'NONE', script: scripts.enforce },
      }
      return policy.osPolicies
    })
    log('==> this rewrites the deployed policy; the next mdeploy restores the checkout’s identity')
    for (const [index, batch] of batches.entries()) {
      upgradeFleetOverPolicy({
        targets: batch.hosts.map((host) => host.target),
        identity: binary.identity,
        project: scope.project as string,
        zone,
        assignment: batch.name,
        osPolicies: deployed[index],
        run,
        sleep,
        log,
        writeFile: writePolicyFile,
      })
    }
    log(`==> done (${hosts.length} host(s))`)
    return 0
  }

  for (const [index, host] of hosts.entries()) {
    log(`==> [${index + 1}/${hosts.length}]`)
    const request = {
      identity: binary.identity,
      payload,
      label: host.label,
      ...(home.identity.home === 'aws'
        ? { cloud: 'aws' as const, target: host.target, region: scope.region as string }
        : {
            cloud: 'gcp' as const,
            target: host.target,
            project: scope.project as string,
            zone: zoneIn(scope.region as string, scope.zone ?? null),
          }),
    } satisfies UpgradeOneRequest
    // Sequential, and a failure stops the roll: the hosts not yet visited keep
    // serving what they were serving. The same property the deploy gets from
    // its dependency chain.
    upgradeOne(request, { run, sleep, log })
  }

  /*
   * Deliberately does not assert the fleet is at the target.
   *
   * A host can be skipped as already-serving-it, left alone as still
   * bootstrapping, or refused as a downgrade — the per-host lines above say
   * which, and a blanket "all at vX" would be false for every one of those.
   */
  log(`==> done (${hosts.length} host(s))`)
  return 0
}