Skip to content

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

7.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 BoxLite AI
import assert from 'node:assert/strict'
import { execFileSync, spawnSync } from 'node:child_process'
import { createHash } from 'node:crypto'
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import test from 'node:test'

const root = new URL('../../../', import.meta.url)
const script = fileURLToPath(new URL('apps/infra/scripts/release-candidate.mjs', root))
const tag = 'v1.2.3'
const sha = 'a'.repeat(40)
const group = 'wheels-ubuntu-latest'
const primary = 'boxlite-1.2.3-cp314-cp314-manylinux_2_28_x86_64.whl'
const missing = 'boxlite-1.2.3-cp314-cp314t-manylinux_2_28_x86_64.whl'
const hash = text => 'sha256:' + createHash('sha256').update(text).digest('hex')

function fixture(t) {
  const dir = mkdtempSync(join(tmpdir(), 'release-repair-'))
  t.after(() => rmSync(dir, { recursive: true, force: true }))
  for (const name of ['release-source', 'release-source/.github', 'release', 'source']) mkdirSync(join(dir, name))
  cpSync(new URL('.github/ci-config.json', root), join(dir, 'release-source/.github/ci-config.json'))
  const releaseFile = name => join(dir, 'release', name)
  writeFileSync(releaseFile(primary), 'updated release wheel')
  writeFileSync(join(dir, 'ids'), group + '\tcp314-manylinux_x86_64\n' + group + '\tcp314t-manylinux_x86_64\n')
  writeFileSync(join(dir, 'source', missing), 'original producer wheel')
  execFileSync('zip', ['-q', join(dir, 'artifact.zip'), missing], { cwd: join(dir, 'source') })
  writeFileSync(join(dir, 'gh'), `#!/bin/sh
set -eu
case "$*" in
  *commits/refs%2Ftags%2F*) jq -n --arg sha "$SHA" '{sha:$sha}' ;;
  *releases/7) echo '{"tag_name":"v1.2.3","draft":false}' ;;
  *releases/7/assets*)
    starter=$(cat starter 2>/dev/null || true)
    ls release | jq -Rn --arg starter "$starter" '[[inputs | if .==$starter then {name:.,id:71,size:0,state:"starter"} else {name:.,size:8,state:"uploaded"} end]]' ;;
  'api --method DELETE repos/boxlite-ai/boxlite/releases/assets/71')
    rm "release/$(cat starter)" starter ;;
  'release download '*) cp "release/$7" "$9/$7" ;;
  *actions/artifacts/55/zip)
    if test -f finish-upload; then
      cp finish-upload "release/$(cat starter)"; rm starter
    fi
    cat artifact.zip ;;
  *actions/artifacts/55) jq -n --argjson expired "$(test -e expired && echo true || echo false)" '{id:55,name:"wheels-ubuntu-latest",expired:$expired,workflow_run:{id:10}}' ;;
  'release upload '*)
    name=$(basename "$4")
    test ! -f "release/$name" || { echo "Asset already exists: $name" >&2; exit 1; }
    if test -f interrupt-once; then
      rm interrupt-once; echo "$name" > starter; touch "release/$name"; echo 'Interrupted upload' >&2; exit 1
    fi
    cp "$4" release/ ;;
  *) echo "Unexpected gh command: $*" >&2; exit 2 ;;
esac
`, { mode: 0o755 })
  function manifest(digest) {
    writeFileSync(releaseFile('release-candidate.json'), JSON.stringify({
      tag, sha, run: 10, producers: { [group]: { id: 55 } },
      assets: { [primary]: { group, digest: hash('old wheel') }, [missing]: { group, digest } },
    }))
  }
  function run() {
    return spawnSync(process.execPath, [script, 'repair'], { cwd: dir, encoding: 'utf8', env: {
      ...process.env, PATH: dir + ':' + process.env.PATH, TAG: tag, SHA: sha, GITHUB_REPOSITORY: 'boxlite-ai/boxlite',
      RELEASE_ID: '7', COMPONENT_GROUP: 'wheels-', RELEASE_ASSET_DIR: join(dir, 'snapshot'), WHEEL_IDENTIFIERS: join(dir, 'ids'),
    } })
  }
  return { dir, run, releaseFile, manifest }
}

test('repair publishes the live wheel and restores a missing free-threaded wheel from its producer', t => {
  const { dir, run, releaseFile, manifest } = fixture(t)
  manifest(hash('original producer wheel'))
  const result = run()
  assert.equal(result.status, 0, result.stderr)
  assert.equal(readFileSync(releaseFile(primary), 'utf8'), 'updated release wheel')
  assert.equal(readFileSync(releaseFile(missing), 'utf8'), 'original producer wheel')
  const snapshot = JSON.parse(readFileSync(join(dir, 'snapshot/release-candidate.json')))
  assert.equal(snapshot.assets[primary].group, group)
  assert.equal(snapshot.assets[missing].group, group)
  assert.equal(snapshot.assets[primary].digest, hash('updated release wheel'))
  assert.equal(snapshot.assets[missing].digest, hash('original producer wheel'))
})

test('a complete Release works without a manifest or Actions artifacts', t => {
  const { dir, run, releaseFile } = fixture(t)
  writeFileSync(releaseFile(missing), 'released wheel')
  const mac = 'boxlite-1.2.3-cp314-cp314-macosx_14_0_arm64.whl'
  writeFileSync(releaseFile(mac), 'released Mac wheel')
  const ids = join(dir, 'ids')
  writeFileSync(ids, readFileSync(ids, 'utf8') + 'wheels-macos-15\tcp314-macosx_arm64\n')
  rmSync(join(dir, 'artifact.zip'))
  const result = run()
  assert.equal(result.status, 0, result.stderr)
  const snapshot = JSON.parse(readFileSync(join(dir, 'snapshot/release-candidate.json')))
  assert.equal(snapshot.assets[mac].group, 'wheels-macos-15')
  assert.equal(snapshot.assets[primary].group, group)
})

test('a partial release reports the missing wheel when no fallback exists', t => {
  const { run, releaseFile } = fixture(t)
  assert.match(run().stderr, /cp314t-manylinux_x86_64 and no fallback manifest/)
  assert.equal(existsSync(releaseFile(missing)), false)
})

test('repair rejects damaged fallback bytes before restoring attachments', t => {
  const { run, releaseFile, manifest } = fixture(t)
  manifest(hash('different bytes'))
  assert.match(run().stderr, /Recovered file differs from manifest/)
  assert.equal(existsSync(releaseFile(missing)), false)
})

test('expired source artifacts cannot backfill a missing attachment', t => {
  const { dir, run, releaseFile, manifest } = fixture(t)
  manifest(hash('original producer wheel'))
  writeFileSync(join(dir, 'expired'), '')
  assert.match(run().stderr, /Unavailable source artifact/)
  assert.equal(existsSync(releaseFile(missing)), false)
})

test('a retried repair replaces the empty placeholder left by an interrupted upload', t => {
  const { dir, run, releaseFile, manifest } = fixture(t)
  manifest(hash('original producer wheel'))
  writeFileSync(join(dir, 'interrupt-once'), '')
  assert.match(run().stderr, /Interrupted upload/)
  assert.equal(readFileSync(releaseFile(missing), 'utf8'), '')
  rmSync(join(dir, 'snapshot'), { recursive: true })
  const retry = run()
  assert.equal(retry.status, 0, retry.stderr)
  assert.equal(readFileSync(releaseFile(missing), 'utf8'), 'original producer wheel')
  assert.equal(readFileSync(releaseFile(primary), 'utf8'), 'updated release wheel')
})

test('a completed attachment appearing during recovery is preserved for the next attempt', t => {
  const { dir, run, releaseFile, manifest } = fixture(t)
  manifest(hash('original producer wheel'))
  writeFileSync(releaseFile(missing), '')
  writeFileSync(join(dir, 'starter'), missing)
  writeFileSync(join(dir, 'finish-upload'), 'new public wheel')
  assert.match(run().stderr, /Release attachment changed/)
  assert.equal(readFileSync(releaseFile(missing), 'utf8'), 'new public wheel')
  rmSync(join(dir, 'snapshot'), { recursive: true })
  const retry = run()
  assert.equal(retry.status, 0, retry.stderr)
  const snapshot = JSON.parse(readFileSync(join(dir, 'snapshot/release-candidate.json')))
  assert.equal(snapshot.assets[missing].digest, hash('new public wheel'))
})