Skip to content

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

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

/*
 * Rolling in-place upgrade of the boxlite-runner binary on live Runner EC2s.
 *
 * The Runner resources in stack/runners.ts carry `ignoreChanges: ['ami','userDataBase64']`,
 * so a Cargo.toml version bump is detected but never acted on — `sst deploy` will not
 * replace a runner that holds box state (/var/lib/boxlite + in-memory libkrun VMs).
 * This is how the new version actually lands: /usr/local/bin/boxlite-runner is replaced
 * over SSM and the systemd unit restarted. The instance is not touched.
 *
 * One host at a time. On the deploy path each `UpgradeRunnerBinary-*` command handles a
 * single instance and Pulumi's dependsOn chain sequences them; invoked by hand it walks
 * the discovered fleet in the same order. Either way a failure stops the roll, so the
 * hosts not yet visited keep serving the old binary.
 *
 * The runner is NOT drained first: boxes on the host being upgraded take the restart.
 * Cordoning through the admin API needs a control-plane runner id, an operator key, and
 * the organization-infrastructure flag, none of which the deploy path has.
 *
 * The binary comes from whichever source artifacts/source.ts selects: the published
 * release for a version, or a build of one commit staged in the stack's artifacts bucket. Only
 * the two URLs and the command that fetches them differ — the verify/swap/rollback below is the
 * same either way.
 *
 * Usage — the `:legacy` suffix is load-bearing. The unsuffixed names are
 * mdeploy's, they reach the fleet that path owns, and they take `--version`
 * rather than the positional argument below:
 *   npm run runner:update:legacy                 # source from the environment, every running runner
 *   npm run runner:update:legacy -- 0.9.5        # explicit version, always a release
 *   INSTANCE_IDS=i-abc npm run runner:update:legacy
 *
 * Env:
 *   RUNNER_ARTIFACT_SOURCE  release|build (see artifacts/source.ts; default release)
 *   BOXLITE_ARTIFACT_REF    commit a build-mode binary was produced from
 *   RUNNER_ARTIFACT_BUCKET  bucket a build-mode binary is staged in
 *   RUNNER_VERSION   target version (argv[2] wins; falls back to the workspace version)
 *   INSTANCE_IDS     comma-separated EC2 ids; unset = discover by tag:Name=boxlite-runner-*
 *   AWS_REGION       default ap-southeast-1
 *   RUNNER_PORT      port the runner's health route listens on (default 3003)
 *   ALLOW_DOWNGRADE  set to 1 to permit replacing a runner with an OLDER version
 */

import { spawnSync } from 'node:child_process'
import { kernelDeployment } from 'mdeploy/runner-kernel'
import { realpathSync } from 'node:fs'
import { pathToFileURL } from 'node:url'

import { resolveArtifactSource } from '../artifacts/source.js'
import { readWorkspaceVersion } from '../deployment/environment.js'
import { artifactFetchCommand, resolveRunnerArtifact } from '../artifacts/runner.js'

const REGION = process.env.AWS_REGION || 'ap-southeast-1'
const RUNNER_PORT = process.env.RUNNER_PORT || '3003'
const NAME_TAG_PATTERN = 'boxlite-runner-*'
// ~5 min at 10s apart — covers SSM agent registration on a freshly created instance.
const SSM_REGISTRATION_ATTEMPTS = 30
// ~40 min at 5s apart. This has to stay above the payload's own worst case: giving up first
// would be worse than slow — nothing cancels an accepted SSM command, so the deployer would
// report failure while the host went on to stop and swap the unit anyway. The bound the payload
// can actually reach is derived below rather than guessed, so raising a fetch timeout that
// crosses this line fails a test instead of stranding a fleet.
const SSM_POLL_INTERVAL_SECONDS = 5
const SSM_COMPLETION_ATTEMPTS = 480

// The payload's bounded steps, in seconds, taken from the timeouts it is actually emitted with.
// Exported so the supervision budget above is checked against them rather than assumed.
//
// This is a floor, not the true worst case: the payload also runs apt-get, unzip and the AWS CLI
// installer, none of which take a timeout, plus tar/sha256sum/systemctl. The window above keeps
// over twenty minutes of slack over this floor to cover them. Read it as "supervision must
// exceed everything we can bound, with room for what we cannot".
export const PAYLOAD_WORST_CASE_SECONDS = {
  awsCliInstall: 120,
  tarballFetch: 300,
  checksumFetch: 300,
  kernelFetch: 120,
  readinessGate: 150,
  kernelReadinessGate: 150,
}
export const ssmSupervisionSeconds = () => SSM_COMPLETION_ATTEMPTS * SSM_POLL_INTERVAL_SECONDS
export const payloadWorstCaseSeconds = () =>
  Object.values(PAYLOAD_WORST_CASE_SECONDS).reduce((total, seconds) => total + seconds, 0)

// ── aws CLI ──────────────────────────────────────────────────────────────────
// Shelling out to the CLI (rather than adding an @aws-sdk dependency) matches
// deployment/sst.ts and keeps the remote payload dependency-free.

function runAws(args: any) {
  const result = spawnSync('aws', args, { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 })
  if (result.error) {
    if ((result.error as NodeJS.ErrnoException).code === 'ENOENT')
      throw new Error('the `aws` CLI is required but was not found on PATH')
    throw new Error(`could not launch aws: ${result.error.message}`)
  }
  return { ok: result.status === 0, stdout: (result.stdout || '').trim(), stderr: (result.stderr || '').trim() }
}

// Same call, but a non-zero exit is fatal — for output we cannot proceed without.
function aws(args: any) {
  const { ok, stdout, stderr } = runAws(args)
  if (!ok) throw new Error(`aws ${args[0]} ${args[1]} failed: ${stderr || '(no stderr)'}`)
  return stdout
}

// ── target + version resolution ──────────────────────────────────────────────

// Explicit ids (SST passes exactly one per command) or every running runner in the
// region. The caller walks whichever list it gets one entry at a time.
export function resolveTargets(environment = process.env, { describe = aws } = {}) {
  const explicit = (environment.INSTANCE_IDS || '')
    .split(',')
    .map((id) => id.trim())
    .filter(Boolean)
  if (explicit.length > 0) return explicit

  const discovered = describe([
    'ec2',
    'describe-instances',
    '--region',
    REGION,
    '--filters',
    `Name=tag:Name,Values=${NAME_TAG_PATTERN}`,
    'Name=instance-state-name,Values=running',
    '--query',
    'Reservations[].Instances[].InstanceId',
    '--output',
    'text',
  ])
    .split(/\s+/)
    .filter((id) => id && id !== 'None')
    // describe-instances does not promise an order; sort so a fleet-wide roll visits the
    // same hosts in the same sequence every run.
    .sort()

  if (discovered.length === 0) {
    throw new Error(`no running instances tagged Name=${NAME_TAG_PATTERN} in ${REGION}`)
  }
  return discovered
}

// The workspace version is the release version for every published asset — the same
// field stack/runners.ts bakes into the runner user-data.
export function resolveVersion(argv = process.argv, environment = process.env) {
  const explicit = argv[2] || environment.RUNNER_VERSION
  return explicit ? explicit.trim().replace(/^v/, '') : readWorkspaceVersion()
}

// What this run should land, and the identity the host must report once it has. Named for the
// artifact, not the hosts — resolveTargets above answers the other half (which instances).
export function resolveUpgrade(
  argv = process.argv,
  environment = process.env,
  { readVersion = readWorkspaceVersion } = {},
) {
  // An explicit version on the command line names a release whatever the environment selects:
  // `npm run runner:update:legacy -- 0.9.5` has only ever meant one thing.
  const explicitVersion = argv[2] ? resolveVersion(argv, environment) : undefined
  const source: any = explicitVersion
    ? { kind: 'release', version: explicitVersion }
    : resolveArtifactSource('runner', environment, { readVersion })
  // RUNNER_VERSION is the legacy release-only out-of-band override. Otherwise a release uses the
  // same VERSION selector as the API. A build always uses source.version: it came from the checkout
  // that determines the versioned S3 filename, so reconstructing it from a different deploy
  // checkout could fetch one valid binary while waiting forever for another identity.
  const version =
    explicitVersion ??
    (source.kind === 'build'
      ? source.version
      : environment.RUNNER_VERSION
        ? resolveVersion(['node', 'script'], environment)
        : source.version)
  const artifact = resolveRunnerArtifact(source.kind === 'release' ? { kind: 'release', version } : source, environment)
  // A build carries its commit as semver build metadata. Without it two builds of the same
  // checkout are indistinguishable on the wire, and the "already current" guard below would skip
  // every dev deploy after the first — the exact failure that makes a dev Runner untestable.
  const expectedVersion = source.kind === 'build' ? `${version}+${source.ref}` : version
  return { kind: source.kind, version, ref: source.ref, expectedVersion, artifact }
}

// ── remote upgrade script ────────────────────────────────────────────────────

export function buildRemoteScript(
  upgrade: any,
  { runnerPort = RUNNER_PORT, allowDowngrade = false, region = REGION } = {},
) {
  const { artifact, expectedVersion } = upgrade
  const { tarballUrl, checksumUrl, tarballName } = artifact
  // The port reaches the remote script body verbatim, so reject a malformed one here
  // rather than letting it fail obscurely inside a curl on the host.
  if (!/^[0-9]+$/.test(String(runnerPort))) {
    throw new Error(`RUNNER_PORT must be numeric (got '${runnerPort}')`)
  }
  // The manifest's filename column is matched as an awk ERE, so the dots in the tarball
  // name must be escaped — otherwise they are wildcards and a differently-named asset
  // could satisfy the check.
  const tarballPattern = tarballName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
  const fetchTarball = artifactFetchCommand(artifact, tarballUrl, '$WORK/runner.tar.gz', region)
  const fetchChecksum = artifactFetchCommand(artifact, checksumUrl, '$WORK/runner.sha256', region)

  // SSM is the only channel that reaches a long-lived Runner: its user-data carries
  // ignoreChanges, so a host created before the AWS CLI became an unconditional part of the
  // bootstrap never receives it. Before this, the CLI was installed only when a ghcr pull
  // credential was wired — a supported deployment can therefore have no `aws` at all, and an
  // S3-sourced upgrade would die on `command not found` on its very first build deploy.
  const ensureAwsCli =
    artifact.fetch === 's3'
      ? `# Install the AWS CLI if this host predates the unconditional bootstrap install.
if ! command -v aws >/dev/null 2>&1; then
  echo "aws CLI missing (host predates the unconditional install); installing it now"
  CLI_WORK=$(mktemp -d)
  # set -e aborts the whole payload on a failed curl/unzip/install, so the removal below would
  # never run and each failed upgrade would leave a directory on a long-lived host.
  trap 'rm -rf "$CLI_WORK"' EXIT
  curl --fail --silent --show-error --location --connect-timeout 10 --max-time 120 \\
    --retry 3 --retry-delay 2 --retry-connrefused \\
    "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "$CLI_WORK/awscliv2.zip"
  command -v unzip >/dev/null 2>&1 || { apt-get update && apt-get install -y unzip; }
  unzip -q "$CLI_WORK/awscliv2.zip" -d "$CLI_WORK"
  "$CLI_WORK/aws/install" --update
  rm -rf "$CLI_WORK"
  trap - EXIT
  command -v aws >/dev/null 2>&1 || { echo "FATAL: aws CLI still unavailable" >&2; exit 1; }
fi
`
      : ''

  // Ordering only means something between releases. Two commits of the same checkout version are
  // neither older nor newer than each other, so in build mode the only sane reading of "install
  // this commit" is to install it — and a build is never a hand-install worth protecting.
  const downgradeGuard =
    upgrade.kind === 'release'
      ? `# Is the live version newer than the target? \`sort -V\` orders release cores correctly
# (0.9.10 above 0.9.9, 1.0.0 above 0.9.99) but gets prereleases backwards — it calls
# 0.9.8-alpha newer than 0.9.8 — so cores are compared with sort -V and semver's
# "a prerelease precedes its release" is applied by hand. TARGET is always a stable
# X.Y.Z (the shared resolver rejects anything else), so only the live side can carry a
# prerelease suffix or the +commit a build-mode install leaves behind. Semver ignores
# build metadata for precedence, so it is stripped too — otherwise a release deploy would
# read 0.9.7+abc as newer than 0.9.7 and refuse to replace a dev build. A blank CURRENT
# (not serving) is never newer, so an unhealthy runner still gets repaired below.
live_is_newer() {
  cur_core=\${CURRENT%%-*}
  cur_core=\${cur_core%%+*}
  tgt_core=\${TARGET%%-*}
  if [ "$cur_core" != "$tgt_core" ]; then
    [ "$(printf '%s\\n%s\\n' "$cur_core" "$tgt_core" | sort -V | tail -1)" = "$cur_core" ]
    return $?
  fi
  # Same core, so live is either the identical release (handled above), a prerelease of it,
  # or a build of it — all of which semver puts first, meaning they are due this upgrade.
  return 1
}

# Never move a runner backwards by accident. One serving something newer than the declared
# version is usually a deliberate hand-install, and silently reverting it during an
# unrelated deploy is a nasty surprise; a real rollback sets ALLOW_DOWNGRADE=1.
if [ "\${ALLOW_DOWNGRADE:-}" != "1" ] && live_is_newer; then
  echo "WARNING: live $CURRENT is newer than target $TARGET; refusing to downgrade (set ALLOW_DOWNGRADE=1 to force)"
  exit 0
fi
`
      : `# Build mode: no version ordering to guard, the requested commit is the requested commit.
`

  const binary = `set -euo pipefail

TARGET="${expectedVersion}"
HEALTH="http://127.0.0.1:${runnerPort}/"
ALLOW_DOWNGRADE="${allowDowngrade ? '1' : ''}"

# The binary parses no CLI args, so there is no --version to ask. Of the places that do
# report it, this health route is the only one reachable here without a token (/info is
# auth-gated and the healthcheck service only pushes to the control plane). Empty output
# means the runner is not serving.
probe_version() {
  curl -fsS --max-time 3 "$HEALTH" | sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\\([^"]*\\)".*/\\1/p'
}

# A just-created EC2 reports "running" — all Pulumi waits for — long before cloud-init has
# installed the binary and written the unit. Nothing to do on such a host: its user-data
# installs this very version. Bail out BEFORE the probe, because "not serving" here means
# "not built yet", and stopping/installing/starting against a missing unit would fail and
# roll back a runner that was coming up fine.
# A unit that is neither enabled nor active is still waiting for cloud-init. An enabled
# runner that died still needs repair, and an active runner remains managed even if an
# operator disabled its automatic start at boot.
if [ ! -x /usr/local/bin/boxlite-runner ] ||
  [ ! -f /etc/systemd/system/boxlite-runner.service ] ||
  { ! systemctl is-enabled --quiet boxlite-runner 2>/dev/null &&
    ! systemctl is-active --quiet boxlite-runner; }; then
  echo "still bootstrapping (binary/unit not in place, or neither enabled nor active); user-data installs v$TARGET itself — nothing to do"
  exit 0
fi

# Converge, don't reinstall: a runner already on the target version is left completely
# alone, which is what makes running this on every deploy free of gratuitous restarts.
# Past the guard above, an unreachable probe means installed-but-unhealthy — do NOT skip
# that, a binary swap is exactly what might repair it.
CURRENT=$(probe_version || true)
echo "current version: \${CURRENT:-<not serving>}"
if [ "$CURRENT" = "$TARGET" ]; then
  echo "already at $TARGET; leaving the unit untouched"
  exit 0
fi

${downgradeGuard}
# Download and verify BEFORE stopping the unit, so a failed or corrupt fetch never takes
# the runner down. Fail closed: the checksum sidecar is required, and must name exactly
# the tarball we fetched — same contract the deploy-time preflight enforces.
${ensureAwsCli}WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT
${fetchTarball}
${fetchChecksum}
EXPECTED=$(awk '$2 ~ /^\\*?${tarballPattern}$/ {print $1}' "$WORK/runner.sha256")
[ -n "$EXPECTED" ] || { echo "FATAL: checksum manifest does not name ${tarballName}" >&2; exit 1; }
ACTUAL=$(sha256sum "$WORK/runner.tar.gz" | awk '{print $1}')
[ "$EXPECTED" = "$ACTUAL" ] || { echo "FATAL: checksum mismatch (want $EXPECTED got $ACTUAL)" >&2; exit 1; }
echo "checksum verified ($ACTUAL)"
tar -xzf "$WORK/runner.tar.gz" -C "$WORK"
test -x "$WORK/boxlite-runner" || { echo "FATAL: tarball has no boxlite-runner binary" >&2; exit 1; }

# Back up the live binary so a failed swap or start can roll back.
HAD_PREVIOUS=false
if [ -x /usr/local/bin/boxlite-runner ]; then
  cp -a /usr/local/bin/boxlite-runner /usr/local/bin/boxlite-runner.bak
  HAD_PREVIOUS=true
fi
systemctl stop boxlite-runner || true

# The rolling-step boundary: the caller must not move to the next host until this one is
# actually serving the new version, so process-alive is not a sufficient signal.
wait_for_target() {
  for _ in $(seq 1 30); do
    [ "$(probe_version || true)" = "$TARGET" ] && return 0
    sleep 2
  done
  return 1
}

# Swap + start + readiness as one guarded condition: any failing step routes to the
# rollback branch instead of aborting under set -e (if-conditions are exempt).
# daemon-reload closes the narrow window where user-data has written the unit file but not
# yet loaded it: without this, the start below would fail on a unit systemd cannot see.
if install -m 0755 "$WORK/boxlite-runner" /usr/local/bin/boxlite-runner \\
  && systemctl daemon-reload \\
  && systemctl start boxlite-runner \\
  && systemctl is-active --quiet boxlite-runner \\
  && wait_for_target; then
  if [ "$HAD_PREVIOUS" = true ]; then rm -f /usr/local/bin/boxlite-runner.bak; fi
  echo "systemd unit: active"
  echo "new version: $(probe_version)"
else
  echo "upgrade failed; rolling back" >&2
  if [ "$HAD_PREVIOUS" = true ]; then
    mv -f /usr/local/bin/boxlite-runner.bak /usr/local/bin/boxlite-runner
    systemctl restart boxlite-runner || true
  fi
  journalctl -u boxlite-runner --no-pager -n 50 || true
  exit 1
fi
`
  return `set -euo pipefail\n(\n${binary}\n)\n${kernelDeployment(undefined, Number(runnerPort)).enforce}`
}

// ── one host ─────────────────────────────────────────────────────────────────

const indent = (text: any) =>
  text
    .split('\n')
    .map((line: any) => `    ${line}`)
    .join('\n')

// Blocking on purpose: the whole script is sequential (one host at a time), so there is
// nothing to interleave and this keeps the call chain synchronous.
const sleepSeconds = (seconds: any) => spawnSync('sleep', [String(seconds)])

// Two transient conditions are worth retrying, and nothing else. SendCommand is rejected
// with InvalidInstanceId until the instance's SSM agent has registered, and an instance
// reports `running` — all the Pulumi resource waits for — well before that; this runs on
// the deployer, before any payload exists, so the still-bootstrapping guard inside the
// payload cannot cover it. Throttling is the other: losing a whole roll to a rate limit
// would strand the fleet half-upgraded. A bad id, a denied permission, anything else,
// fails immediately.
const RETRYABLE_SEND_ERRORS = /InvalidInstanceId|ThrottlingException|TooManyUpdates|RequestLimitExceeded/
export function sendCommand(
  instanceId: any,
  version: any,
  payload: any,
  { run = runAws, sleep = sleepSeconds }: any = {},
) {
  const args = [
    'ssm',
    'send-command',
    '--region',
    REGION,
    '--document-name',
    'AWS-RunShellScript',
    '--instance-ids',
    instanceId,
    '--comment',
    `boxlite-runner upgrade to v${version}`,
    '--parameters',
    `commands=["echo ${payload} | base64 -d | bash"]`,
    '--query',
    'Command.CommandId',
    '--output',
    'text',
  ]
  for (let attempt = 1; ; attempt++) {
    const { ok, stdout, stderr } = run(args)
    if (ok) return stdout
    if (!RETRYABLE_SEND_ERRORS.test(stderr) || attempt >= SSM_REGISTRATION_ATTEMPTS) {
      throw new Error(`aws ssm send-command failed for ${instanceId}: ${stderr || '(no stderr)'}`)
    }
    console.log(
      `    ${instanceId} SSM send-command not accepted yet (${attempt}/${SSM_REGISTRATION_ATTEMPTS}); retrying`,
    )
    sleep(10)
  }
}

// `aws ssm wait command-executed` gives up after 100s (delay 5 × 20 attempts) and treats
// InProgress as retryable, so on a host whose upgrade legitimately runs longer — the
// readiness gates allow up to 300s on top of the downloads — it returns with the command
// still running. Reading that as a verdict would abort the roll on a host that is
// upgrading fine, so poll to a terminal status instead of using the waiter at all.
export const TERMINAL_SSM_STATUSES = new Set(['Success', 'Failed', 'Cancelled', 'TimedOut'])

export function waitForTerminalStatus(
  invocation: any,
  instanceId: any,
  { run = runAws, sleep = sleepSeconds }: any = {},
) {
  // A poll can legitimately fail for a moment — InvocationDoesNotExist is normal right
  // after send-command — so a single failure is not fatal. But the cause must survive:
  // a persistent AccessDeniedException otherwise looks identical to a slow upgrade and
  // would be reported as a timeout naming nothing, which is the very fault this script
  // was written to stop making.
  let lastFailure = ''
  let lastStatus = ''
  for (let attempt = 1; attempt <= SSM_COMPLETION_ATTEMPTS; attempt++) {
    const { ok, stdout, stderr } = run([...invocation, '--query', 'Status', '--output', 'text'])
    if (ok) {
      if (TERMINAL_SSM_STATUSES.has(stdout)) return stdout
      lastStatus = stdout
      // Cleared on recovery: InvocationDoesNotExist right after send-command is expected,
      // and a stale one would otherwise outrank the whole supervision window of later
      // InProgress polls and blame a failure that had already resolved.
      lastFailure = ''
    } else {
      lastFailure = stderr || '(no stderr)'
    }
    if (attempt < SSM_COMPLETION_ATTEMPTS) sleep(SSM_POLL_INTERVAL_SECONDS)
  }
  const why = lastFailure ? `last polling error: ${lastFailure}` : `last status: ${lastStatus || 'unknown'}`
  throw new Error(
    `${instanceId}: SSM command still not terminal after ${ssmSupervisionSeconds() / 60} minutes (${why})`,
  )
}

function upgradeOne(instanceId: any, upgrade: any) {
  // Hand the payload to SSM base64-encoded rather than quote-escaped: it becomes a
  // single token with no shell metacharacters, sidestepping the brittle escaping of a
  // multi-line script inside the commands=[...] shorthand.
  const script = buildRemoteScript(upgrade, { allowDowngrade: process.env.ALLOW_DOWNGRADE === '1' })
  const commandId = sendCommand(instanceId, upgrade.expectedVersion, Buffer.from(script).toString('base64'))
  console.log(`    command:  ${commandId}`)

  const invocation = [
    'ssm',
    'get-command-invocation',
    '--region',
    REGION,
    '--command-id',
    commandId,
    '--instance-id',
    instanceId,
  ]
  const status = waitForTerminalStatus(invocation, instanceId)
  const stdout = aws([...invocation, '--query', 'StandardOutputContent', '--output', 'text'])
  const stderr = aws([...invocation, '--query', 'StandardErrorContent', '--output', 'text'])

  if (stdout) console.log(indent(stdout))
  if (stderr) console.error(indent(stderr))
  if (status !== 'Success') throw new Error(`${instanceId}: SSM command ${commandId} finished ${status}`)
}

// ── roll ─────────────────────────────────────────────────────────────────────

function main() {
  const upgrade = resolveUpgrade()
  const targets = resolveTargets()

  console.log(
    `==> Rolling boxlite-runner to ${upgrade.expectedVersion} (${upgrade.kind}) ` +
      `across ${targets.length} instance(s) in ${REGION}`,
  )
  console.log(`==> artifact: ${upgrade.artifact.tarballUrl}`)
  targets.forEach((instanceId, i) => {
    console.log(`==> [${i + 1}/${targets.length}] ${instanceId}`)
    upgradeOne(instanceId, upgrade)
  })
  // Deliberately does not assert the fleet is at the target: a host can be skipped as
  // already-current, left alone as still-bootstrapping, or refused as a downgrade. The
  // per-host lines above say which; a blanket "all at vX" would be false for those.
  console.log(`==> done (${targets.length} instance(s))`)
}

export function runRunnerUpdateCli() {
  try {
    main()
  } catch (err: any) {
    console.error(`runner-update-binary: ${err.message}`)
    process.exitCode = 1
  }
}

// Importable for tests; only the direct invocation performs the roll. Both normalizations
// are load-bearing, and both failure modes are silent — the roll is skipped while the
// Pulumi command still reports success. `import.meta.url` is percent-encoded and
// symlink-resolved; a raw `file://${argv[1]}` matches neither a path containing a space
// nor a checkout reached through a symlink.
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
  runRunnerUpdateCli()
}