diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index aaf016ef1c..cbfce08b0d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -2,6 +2,11 @@ name: Publish to npm permissions: {} +# Explicit bash enables pipefail, including digest-generation pipelines. +defaults: + run: + shell: bash + on: push: tags: @@ -21,6 +26,10 @@ env: @hyperframes/player @hyperframes/producer @hyperframes/shader-transitions @hyperframes/studio @hyperframes/aws-lambda @hyperframes/gcp-cloud-run hyperframes +concurrency: + group: publish-${{ github.event.pull_request.head.ref || github.ref }} + cancel-in-progress: false + jobs: lambda-arm64-ffmpeg: name: Lambda arm64 ffmpeg @@ -59,6 +68,7 @@ jobs: - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 with: name: hf-media-linux-aarch64 + overwrite: true path: out/hf-media-linux-aarch64.tar.gz if-no-files-found: error @@ -110,11 +120,14 @@ jobs: - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 with: name: hyperframes-lambda-handler-zip + overwrite: true path: packages/aws-lambda/dist/handler.zip if-no-files-found: error pack: name: Build and pack + outputs: + bundle_id: ${{ steps.previous_bundle.outputs.artifact || steps.bundle.outputs.artifact-id }} needs: [lambda-arm64-ffmpeg, lambda-handler-zip] runs-on: ubuntu-latest timeout-minutes: 30 @@ -122,6 +135,7 @@ jobs: # read-only token and no secrets. The publish job gets only what this job packs. permissions: contents: read + actions: read env: EXPECTED_RELEASE_SHA: >- ${{ github.event_name == 'pull_request' @@ -150,7 +164,27 @@ jobs: exit 1 fi + - name: Find previous release bundle + id: previous_bundle + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} + run: | + gh api --paginate "repos/$REPO/actions/runs/$RUN_ID/artifacts?per_page=100" > "$RUNNER_TEMP/previous_bundle-pages.json" + FOUND="$(jq -sce '[.[].artifacts[] | select(.name == "release-bundle")] | if length == 0 then "" elif length == 1 and .[0].expired == false then .[0].id | tostring else error("Missing valid unique release-bundle") end' "$RUNNER_TEMP/previous_bundle-pages.json")" + echo "artifact=${FOUND}" >> "$GITHUB_OUTPUT" + + - name: Restore previous release bundle + if: steps.previous_bundle.outputs.artifact != '' + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + artifact-ids: ${{ steps.previous_bundle.outputs.artifact }} + path: release-bundle + merge-multiple: true + - name: Resolve version + if: steps.previous_bundle.outputs.artifact == '' id: version env: EVENT_NAME: ${{ github.event_name }} @@ -176,6 +210,7 @@ jobs: echo "Resolved version=${VERSION} dist_tag=${DIST_TAG}" - name: Validate release channel + if: steps.previous_bundle.outputs.artifact == '' env: VERSION: ${{ steps.version.outputs.version }} DIST_TAG: ${{ steps.version.outputs.dist_tag }} @@ -185,25 +220,32 @@ jobs: # The release installs the bun it downloads, never one restored from the shared cache. - uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2 + if: steps.previous_bundle.outputs.artifact == '' with: no-cache: true - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + if: steps.previous_bundle.outputs.artifact == '' with: node-version: 24 - run: corepack enable + if: steps.previous_bundle.outputs.artifact == '' - run: corepack prepare pnpm@10.17.1 --activate + if: steps.previous_bundle.outputs.artifact == '' - run: bun install --frozen-lockfile + if: steps.previous_bundle.outputs.artifact == '' - run: bun run build + if: steps.previous_bundle.outputs.artifact == '' - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + if: steps.previous_bundle.outputs.artifact == '' with: name: hyperframes-lambda-handler-zip path: dist/lambda-handler-build - # Before packing, so @hyperframes/aws-lambda pins this exact zip. The pin - # is always this run's build; the publish job decides which asset the - # release keeps. + # Before packing, so @hyperframes/aws-lambda pins this exact zip. - name: Pin Lambda handler zip + if: steps.previous_bundle.outputs.artifact == '' + id: lambda_zip env: EXPECTED_SHA256: ${{ needs.lambda-handler-zip.outputs.sha256 }} run: | @@ -217,12 +259,15 @@ jobs: bun packages/aws-lambda/scripts/pin-handler-zip.ts - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + if: steps.previous_bundle.outputs.artifact == '' with: name: hf-media-linux-aarch64 path: dist/hf-media # Built from the zip pinned above. The video stage only copies files, so no emulation is needed. - name: Build Lambda video image + if: steps.previous_bundle.outputs.artifact == '' + id: lambda_video_image run: | bun packages/aws-lambda/scripts/stage-arm64-image.ts --zip=dist/hyperframes-lambda-handler.zip \ --ffmpeg=dist/hf-media/hf-media-linux-aarch64.tar.gz --out=dist/arm64-image @@ -234,10 +279,12 @@ jobs: bun packages/aws-lambda/scripts/pin-handler-zip.ts --video-image - run: bun run verify:packed-manifests + if: steps.previous_bundle.outputs.artifact == '' # pnpm pack writes the same tarball pnpm publish uploads: publishConfig # applied and workspace: ranges replaced with versions. - name: Pack packages + if: steps.previous_bundle.outputs.artifact == '' run: | OUT="$PWD/release-bundle/npm" mkdir -p "$OUT" @@ -256,9 +303,12 @@ jobs: (cd packages/cli && npm pack --pack-destination "$OUT") - name: Build portable agent plugin + if: steps.previous_bundle.outputs.artifact == '' run: node scripts/package-agent-plugin.mjs + # Keep the original bytes for all retries, including partially uploaded pairs. - name: Collect release bundle + if: steps.previous_bundle.outputs.artifact == '' env: VERSION: ${{ steps.version.outputs.version }} run: | @@ -271,12 +321,14 @@ jobs: fi - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + id: bundle + if: steps.previous_bundle.outputs.artifact == '' with: name: release-bundle path: release-bundle/ if-no-files-found: error compression-level: 1 - overwrite: true + overwrite: false retention-days: 7 publish: @@ -293,6 +345,7 @@ jobs: # pack job's tarballs and uploads them. permissions: contents: write + actions: read id-token: write env: EXPECTED_RELEASE_SHA: >- @@ -415,7 +468,8 @@ jobs: - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 with: - name: release-bundle + artifact-ids: ${{ needs.pack.outputs.bundle_id }} + merge-multiple: true path: ${{ runner.temp }}/release-bundle - name: Check packed tarballs @@ -465,97 +519,146 @@ jobs: fi cat "$ORDER" + - name: Check published Lambda pins + env: + VERSION: ${{ steps.version.outputs.version }} + run: | + META="$RUNNER_TEMP/aws-lambda-version.json" + PUBLISHED="$RUNNER_TEMP/aws-lambda-published.tgz" + STATUS="$(curl -sS -o "$META" -w '%{http_code}' "https://registry.npmjs.org/@hyperframes%2faws-lambda/${VERSION}")" + if [ "$STATUS" = "404" ]; then + exit 0 + fi + if [ "$STATUS" != "200" ]; then + echo "::error::The npm registry answered $STATUS for @hyperframes/aws-lambda@${VERSION}" + exit 1 + fi + curl -fsSL -o "$PUBLISHED" "$(jq -r '.dist.tarball' "$META")" + INTEGRITY="sha512-$(openssl dgst -sha512 -binary "$PUBLISHED" | base64 -w0)" + if [ "$INTEGRITY" != "$(jq -r '.dist.integrity' "$META")" ]; then + echo "::error::The @hyperframes/aws-lambda@${VERSION} tarball does not match its npm integrity" + exit 1 + fi + check_pin() { + local asset="$1" manifest="$2" pinned built + pinned="$(tar -xzOf "$PUBLISHED" "package/prebuilt/$manifest" | jq -r '.sha256')" + built="$(sha256sum "$RUNNER_TEMP/release-bundle/assets/$asset" | cut -d' ' -f1)" + if [ "$pinned" != "$built" ]; then + echo "::error::@hyperframes/aws-lambda@${VERSION} on npm pins $asset at $pinned, but this run's original bundle has $built" + exit 1 + fi + } + check_pin hyperframes-lambda-handler.zip handler-zip.json + check_pin hyperframes-lambda-video-arm64.oci.tar video-image.json + + - name: Find previous release receipt + id: previous_receipt + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} + run: | + gh api --paginate "repos/$REPO/actions/runs/$RUN_ID/artifacts?per_page=100" > "$RUNNER_TEMP/previous_receipt-pages.json" + FOUND="$(jq -sce '[.[].artifacts[] | select(.name == "lambda-release-receipt")] | if length == 0 then "" elif length == 1 and .[0].expired == false then .[0].id | tostring else error("Missing valid unique lambda-release-receipt") end' "$RUNNER_TEMP/previous_receipt-pages.json")" + echo "artifact=${FOUND}" >> "$GITHUB_OUTPUT" + + - name: Restore previous release receipt + if: steps.previous_receipt.outputs.artifact != '' + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 + with: + artifact-ids: ${{ steps.previous_receipt.outputs.artifact }} + path: ${{ runner.temp }}/release-receipt + merge-multiple: true + + # This receipt is stored in this run's immutable Actions artifact, never on the mutable release. - name: Create GitHub Release + id: release env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} VERSION: ${{ steps.version.outputs.version }} PRERELEASE: ${{ steps.version.outputs.prerelease }} REPO: ${{ github.repository }} + RUN_ID: ${{ github.run_id }} run: | - NOTES_FILE="$RUNNER_TEMP/release-bundle/notes.md" - - # Skip if release already exists (idempotent re-runs) - if gh release view "v${VERSION}" --repo "$REPO" >/dev/null 2>&1; then - echo "Release v${VERSION} already exists — skipping" + RECEIPT="$RUNNER_TEMP/release-receipt/receipt.json" + ASSETS="$RUNNER_TEMP/release-bundle/assets" + mkdir -p "$RUNNER_TEMP/release-receipt" + DIGESTS="$RUNNER_TEMP/lambda-digests.json" + for name in hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256; do + digest="$(sha256sum "$ASSETS/$name" | cut -d' ' -f1)" + jq -n --arg name "$name" --arg digest "$digest" '{key: $name, value: $digest}' + done | jq -s 'from_entries' > "$DIGESTS" + if [ -f "$RECEIPT" ]; then + jq -e --arg repo "$REPO" --arg run "$RUN_ID" --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --slurpfile assets "$DIGESTS" ' + .repo == $repo and .run == $run and .tag == $tag and .sha == $sha and + (.release_id | type == "number" and . > 0 and floor == .) and .assets == $assets[0] + ' "$RECEIPT" > /dev/null + RELEASE_ID="$(jq -r .release_id "$RECEIPT")" + gh api "repos/$REPO/releases/tags/v$VERSION" > "$RUNNER_TEMP/release.json" + jq -e --argjson id "$RELEASE_ID" '.id == $id' "$RUNNER_TEMP/release.json" > /dev/null else - FLAGS=(--repo "$REPO" --title "v${VERSION}") - if [ -f "$NOTES_FILE" ]; then - FLAGS+=(--notes-file "$NOTES_FILE") - else - echo "No reviewed release notes found at releases/v${VERSION}.md — using GitHub generated notes" - FLAGS+=(--generate-notes) + # Create-only: an existing release or a concurrent creator fails, never becomes ours. + NOTES_FILE="$RUNNER_TEMP/release-bundle/notes.md" + if [ ! -f "$NOTES_FILE" ]; then + gh api --method POST "repos/$REPO/releases/generate-notes" -f tag_name="v$VERSION" --jq .body > "$RUNNER_TEMP/generated-notes.md" + NOTES_FILE="$RUNNER_TEMP/generated-notes.md" fi - if [ "$PRERELEASE" = "true" ]; then - FLAGS+=(--prerelease) - fi - gh release create "v${VERSION}" "${FLAGS[@]}" + jq -n --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --argjson prerelease "$PRERELEASE" --rawfile body "$NOTES_FILE" \ + '{tag_name:$tag, target_commitish:$sha, name:$tag, prerelease:$prerelease, body:$body}' > "$RUNNER_TEMP/create-release.json" + gh api --method POST "repos/$REPO/releases" --input "$RUNNER_TEMP/create-release.json" > "$RUNNER_TEMP/release.json" + RELEASE_ID="$(jq -er '.id | select(type == "number" and . > 0 and floor == .)' "$RUNNER_TEMP/release.json")" + jq -n --arg repo "$REPO" --arg run "$RUN_ID" --arg tag "v$VERSION" --arg sha "$EXPECTED_RELEASE_SHA" --argjson id "$RELEASE_ID" --slurpfile assets "$DIGESTS" \ + '{repo:$repo,run:$run,tag:$tag,sha:$sha,release_id:$id,assets:$assets[0]}' > "$RECEIPT" fi + echo "id=$RELEASE_ID" >> "$GITHUB_OUTPUT" + + - name: Save release receipt + if: steps.previous_receipt.outputs.artifact == '' + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: lambda-release-receipt + path: ${{ runner.temp }}/release-receipt/receipt.json + if-no-files-found: error + overwrite: false + retention-days: 7 - # Users get these assets only through the sha256 pins in - # @hyperframes/aws-lambda. Once that version is on npm, the release must - # keep exactly the pinned files. Before then, an asset this bundle did not - # produce fails a first attempt, because something other than this run - # attached it, and a re-run of this workflow run replaces it. - name: Attach Lambda assets to release env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - VERSION: ${{ steps.version.outputs.version }} REPO: ${{ github.repository }} - RUN_ATTEMPT: ${{ github.run_attempt }} + VERSION: ${{ steps.version.outputs.version }} + RELEASE_ID: ${{ steps.release.outputs.id }} run: | ASSETS="$RUNNER_TEMP/release-bundle/assets" - META="$RUNNER_TEMP/aws-lambda-version.json" - PUBLISHED="$RUNNER_TEMP/aws-lambda-published.tgz" - rm -f "$PUBLISHED" - STATUS="$(curl -sS -o "$META" -w '%{http_code}' "https://registry.npmjs.org/@hyperframes%2faws-lambda/${VERSION}")" - if [ "$STATUS" = "200" ]; then - curl -fsSL -o "$PUBLISHED" "$(jq -r '.dist.tarball' "$META")" - INTEGRITY="sha512-$(openssl dgst -sha512 -binary "$PUBLISHED" | base64 -w0)" - if [ "$INTEGRITY" != "$(jq -r '.dist.integrity' "$META")" ]; then - echo "::error::The @hyperframes/aws-lambda@${VERSION} tarball does not match its npm integrity" - exit 1 - fi - elif [ "$STATUS" != "404" ]; then - echo "::error::The npm registry answered $STATUS for @hyperframes/aws-lambda@${VERSION}" - exit 1 - fi - ON_RELEASE="$RUNNER_TEMP/release-assets.tsv" - gh api "repos/$REPO/releases/tags/v${VERSION}" --jq '.assets[] | [.name, (.digest // "")] | @tsv' > "$ON_RELEASE" - - attach_asset() { - local asset="$1" sidecar="$2" manifest="$3" - local line attached pinned built - line="$(awk -F'\t' -v name="$asset" '$1 == name' "$ON_RELEASE")" - attached="${line#*$'\t'}" - if [ -n "$line" ] && [ -z "$attached" ]; then - echo "::error::Release v${VERSION} has $asset without a digest to check it by" - exit 1 - fi - if [ -f "$PUBLISHED" ]; then - pinned="sha256:$(tar -xzOf "$PUBLISHED" "package/prebuilt/$manifest" | jq -r '.sha256')" - if [ "$attached" != "$pinned" ]; then - echo "::error::@hyperframes/aws-lambda@${VERSION} on npm pins $asset at $pinned, but release v${VERSION} has ${attached:-no such asset}" - exit 1 - fi - echo "$asset on the release is the one npm pins — keeping it" - return 0 - fi - built="sha256:$(sha256sum "$ASSETS/$asset" | cut -d' ' -f1)" - if [ -z "$line" ]; then - gh release upload "v${VERSION}" "$ASSETS/$asset" "$ASSETS/$sidecar" --repo "$REPO" - elif [ "$attached" = "$built" ]; then - echo "$asset is already attached with these contents — skipping" - elif [ "$RUN_ATTEMPT" -gt 1 ]; then - echo "::warning::Replacing $asset on release v${VERSION}; nothing on npm pins it yet." - gh release upload "v${VERSION}" "$ASSETS/$asset" "$ASSETS/$sidecar" --repo "$REPO" --clobber + for name in hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256; do + digest="$(sha256sum "$ASSETS/$name" | cut -d' ' -f1)" + gh api --paginate "repos/$REPO/releases/$RELEASE_ID/assets?per_page=100" > "$RUNNER_TEMP/assets-pages.json" + existing="$(jq -sc --arg name "$name" '[.[][] | select(.name == $name)]' "$RUNNER_TEMP/assets-pages.json")" + if [ "$(jq length <<< "$existing")" != 0 ]; then + jq -e --arg digest "sha256:$digest" 'length == 1 and .[0].state == "uploaded" and .[0].digest == $digest' <<< "$existing" > /dev/null else - echo "::error::Release v${VERSION} already has $asset, and this run did not attach it. Find out who did before releasing this version." - exit 1 + # Upload by the recorded ID; a replacement release at the tag cannot receive these bytes. + gh api --method POST "https://uploads.github.com/repos/$REPO/releases/$RELEASE_ID/assets?name=$name" \ + -H 'Content-Type: application/octet-stream' --input "$ASSETS/$name" > "$RUNNER_TEMP/uploaded-asset.json" + jq -e --arg digest "sha256:$digest" '.state == "uploaded" and .digest == $digest' "$RUNNER_TEMP/uploaded-asset.json" > /dev/null fi - } + done - attach_asset hyperframes-lambda-handler.zip hyperframes-lambda-handler.sha256 handler-zip.json - attach_asset hyperframes-lambda-video-arm64.oci.tar hyperframes-lambda-video-arm64.sha256 video-image.json + - name: Verify Lambda release before npm publication + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + VERSION: ${{ steps.version.outputs.version }} + RELEASE_ID: ${{ steps.release.outputs.id }} + run: | + gh api "repos/$REPO/releases/tags/v$VERSION" > "$RUNNER_TEMP/release.json" + jq -e --argjson id "$RELEASE_ID" '.id == $id' "$RUNNER_TEMP/release.json" > /dev/null + gh api --paginate "repos/$REPO/releases/$RELEASE_ID/assets?per_page=100" > "$RUNNER_TEMP/assets-pages.json" + jq -se --slurpfile receipt "$RUNNER_TEMP/release-receipt/receipt.json" ' + [.[][]] as $assets | $receipt[0].assets | to_entries | all(.[]; + . as $expected | [$assets[] | select(.name == $expected.key)] | + length == 1 and .[0].state == "uploaded" and .[0].digest == ("sha256:" + $expected.value)) + ' "$RUNNER_TEMP/assets-pages.json" > /dev/null # Runs outside the checkout so no project .npmrc applies, and with # --ignore-scripts so no lifecycle script runs next to the npm token. @@ -565,7 +668,6 @@ jobs: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} VERSION: ${{ steps.version.outputs.version }} DIST_TAG: ${{ steps.version.outputs.dist_tag }} - RUN_ATTEMPT: ${{ github.run_attempt }} run: | FAILED=0 @@ -578,8 +680,6 @@ jobs: if published="$(npm view "${name}@${VERSION}" dist.integrity 2>/dev/null)" && [ -n "$published" ]; then if [ "$published" = "$integrity" ]; then echo "⏭️ ${name}@${VERSION} already published with these contents — skipping" - elif [ "$RUN_ATTEMPT" -gt 1 ]; then - echo "::warning::${name}@${VERSION} is already on npm with other contents; this re-run keeps the earlier attempt's upload." else echo "::error::${name}@${VERSION} is already on npm, and this run did not publish it. Find out who did before releasing this version." FAILED=1 diff --git a/docs/contributing/changelog-process.mdx b/docs/contributing/changelog-process.mdx index e372dd1698..8c84fc262a 100644 --- a/docs/contributing/changelog-process.mdx +++ b/docs/contributing/changelog-process.mdx @@ -61,6 +61,10 @@ The docs changelog lives in `docs/changelog.mdx` and uses Mintlify `` en To recover a failed publish, rerun the original merged-PR workflow. Do not push the stable tag or use a manual dispatch; those paths are intentionally disabled so recovery cannot publish a different commit. + Retries reuse the original run's immutable `release-bundle` artifact, including the npm tarballs and Lambda assets. The publish job stores the release ID, source commit, run ID, and Lambda digests in a separate `lambda-release-receipt` artifact before uploading assets. It accepts an existing release only when that receipt matches, and skips an existing asset only when its digest matches the original bundle. Partially uploaded asset pairs can resume from those original bytes. An existing npm version with different contents fails even on a retry. + + Both artifacts are retained for seven days. An expired or deleted receipt, a recreated release, mismatched bytes, or a failure between release creation and receipt persistence stops automatic recovery. Inspect the original run and any already published packages before choosing a new release version; do not recreate a receipt from release metadata or delete published assets just to make a retry pass. This workflow does not enable repository-wide immutable releases or add deploy-time attestation verification. + The generated compare link points to the future `v0.6.53` tag. It may not resolve until the release PR merges and the publish workflow creates the tag. diff --git a/docs/contributing/release-channels.mdx b/docs/contributing/release-channels.mdx index 8b5b3b88db..dd2cc3c061 100644 --- a/docs/contributing/release-channels.mdx +++ b/docs/contributing/release-channels.mdx @@ -72,7 +72,7 @@ The publish workflow validates release channel boundaries before publishing: - Every publish job verifies that its checkout matches the immutable event SHA. - Builds, lifecycle scripts and `npm pack` run in a job with a read-only token and no secrets. - The job that holds the npm token runs no code from the release commit. It checks that every packed tarball is a package this workflow publishes, at exactly the release version, then uploads it with lifecycle scripts off. -- A version already on npm is skipped only when its contents match the new tarball, or on a re-run of the same workflow run. Otherwise the publish fails, because something other than this run published that version. -- The Lambda handler zip and arm64 video image are always built by the run, and `@hyperframes/aws-lambda` pins those builds. Once that version is on npm, the release keeps exactly the assets it pins. Before then, if the release already holds a Lambda asset this run did not produce, a first attempt fails and a re-run of the same workflow run replaces the asset. +- A version already on npm is skipped only when its contents match the original run’s tarball, including on a retry. Different contents fail the publish. +- The Lambda handler zip and arm64 video image are built by the run, and `@hyperframes/aws-lambda` pins those builds. Retries reuse the original immutable run bundle. If the version is already on npm, its verified package must pin those same bytes before any release mutation. A separate immutable run receipt binds the release ID and asset digests; foreign releases and mismatched assets fail instead of being adopted or overwritten. See [publish recovery](/contributing/changelog-process) for retention and recovery limits. This prevents an alpha-only feature from being included in a stable hotfix by accident. diff --git a/package.json b/package.json index 1ae9d0e6f5..718c680065 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "player:perf": "bun run --filter @hyperframes/player perf", "format:check": "oxfmt --check .", "knip": "knip", - "test:scripts": "node --import tsx --test scripts/animejs-v4-guidance.test.mjs scripts/check-tracked-artifacts.test.mjs scripts/check-committed-keys.test.mjs scripts/check-symlinks.test.mjs scripts/upload-docs-images.test.mjs scripts/check-registry-set-delta.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-pr-captures.test.mjs scripts/check-comment-citations.test.mjs scripts/comments-workflow.test.mjs scripts/comment-ratchet.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-media-use-copy-parity.test.mjs scripts/check-svg-sanitize-parity.test.mjs scripts/check-media-use-svg-sanitize-generated.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/check-cli-lazy-deps.test.mjs scripts/file-url-paths.test.mjs scripts/check-studio-copy-dashes.test.mjs scripts/check-large-files.test.mjs scripts/check-registry-innerhtml.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/clawhub-sync.test.mjs scripts/clawhub-unlist-retired.test.mjs scripts/publish-workflow.test.mjs scripts/publish-main-only-workflows.test.mjs scripts/pr-edit-concurrency.test.mjs scripts/merge-queue-workflows.test.mjs scripts/ci-cache-workflows.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/check-edit-accuracy-filter.test.mjs scripts/edit-accuracy-retry.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/host-registry-assets.test.ts scripts/catalog-preview-temp.test.ts scripts/backfill-block-previews.test.ts scripts/catalog-hosted-files.test.ts scripts/player-cdn-pin.test.ts scripts/registry-cdn-pins.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/rewrite-relative-import-extensions.test.mjs scripts/lint-skills.test.mjs scripts/check-core-skills.test.mjs scripts/creator-editing-recipes.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs packages/core/scripts/writeGeneratedFile.test.ts scripts/catalog-publication.test.mjs scripts/ci/resolve-workflow-pr.test.mjs scripts/lefthook-order.test.mjs scripts/inline-gsap.test.mjs scripts/ci/flake-gate.test.mjs scripts/ci/motion-blur-nightly-report.test.mjs scripts/ci/base-file.test.mjs scripts/ci/release-only-diff.test.mjs scripts/check-catalog-source-pr.test.mjs scripts/generate-registry-items.test.ts scripts/catalog-drift.test.ts scripts/catalog-fetch-mirror.test.ts scripts/catalog-script-inlining.test.ts scripts/catalog-detail.test.ts scripts/generate-catalog-pages.test.ts scripts/verify-catalog-payloads.test.ts scripts/registry-theme-fallbacks.test.ts scripts/registry-skill-files.test.ts scripts/creator-editing-capabilities.test.mjs scripts/generate-catalog-previews.test.ts scripts/registry-primitive-payloads.test.ts registry/components/pan-stations/pan-stations.test.mjs registry/components/filmed-screen/filmed-screen.test.mjs registry/components/echo-smear/echo-smear.test.mjs registry/examples/path-traced-3d/source/path-traced-3d.test.mjs && vitest run scripts/catalog/ scripts/contrastRatchet.test.ts scripts/generate-catalog-payloads.test.ts", + "test:scripts": "node --import tsx --test scripts/animejs-v4-guidance.test.mjs scripts/check-tracked-artifacts.test.mjs scripts/check-committed-keys.test.mjs scripts/check-symlinks.test.mjs scripts/upload-docs-images.test.mjs scripts/check-registry-set-delta.test.mjs scripts/check-no-main-deletions.test.mjs scripts/check-pr-captures.test.mjs scripts/check-comment-citations.test.mjs scripts/comments-workflow.test.mjs scripts/comment-ratchet.test.mjs scripts/check-docs-snippet-motion.test.mjs scripts/registry-target-paths.test.mjs scripts/check-workspace-contracts.test.mjs scripts/check-media-use-copy-parity.test.mjs scripts/check-svg-sanitize-parity.test.mjs scripts/check-media-use-svg-sanitize-generated.test.mjs scripts/check-package-cycles.test.mjs scripts/check-cli-process-ownership.test.mjs scripts/check-cli-lazy-deps.test.mjs scripts/file-url-paths.test.mjs scripts/check-studio-copy-dashes.test.mjs scripts/check-large-files.test.mjs scripts/check-registry-innerhtml.test.mjs scripts/package-subpaths.test.mjs scripts/validate-release-channel.test.mjs scripts/clawhub-sync.test.mjs scripts/clawhub-unlist-retired.test.mjs scripts/publish-workflow.test.mjs scripts/release-lambda-assets.test.mjs scripts/publish-main-only-workflows.test.mjs scripts/pr-edit-concurrency.test.mjs scripts/merge-queue-workflows.test.mjs scripts/ci-cache-workflows.test.mjs scripts/install-workspace-dependencies.test.mjs scripts/check-edit-accuracy-filter.test.mjs scripts/edit-accuracy-retry.test.mjs scripts/draft-changelog.test.ts scripts/set-version.test.ts scripts/release-prepare.test.ts scripts/cli-options.test.ts scripts/changelog-weekly.test.ts scripts/claude-plugin-compression.test.ts scripts/catalog-payload-assets.test.ts scripts/host-registry-assets.test.ts scripts/catalog-preview-temp.test.ts scripts/backfill-block-previews.test.ts scripts/catalog-hosted-files.test.ts scripts/player-cdn-pin.test.ts scripts/registry-cdn-pins.test.ts scripts/studio-runtime-smoke.test.mjs scripts/verify-packed-manifests.test.mjs scripts/rewrite-relative-import-extensions.test.mjs scripts/lint-skills.test.mjs scripts/check-core-skills.test.mjs scripts/creator-editing-recipes.test.mjs packages/gcp-cloud-run/check-dockerfile-workspaces.test.mjs packages/core/scripts/writeGeneratedFile.test.ts scripts/catalog-publication.test.mjs scripts/ci/resolve-workflow-pr.test.mjs scripts/lefthook-order.test.mjs scripts/inline-gsap.test.mjs scripts/ci/flake-gate.test.mjs scripts/ci/motion-blur-nightly-report.test.mjs scripts/ci/base-file.test.mjs scripts/ci/release-only-diff.test.mjs scripts/check-catalog-source-pr.test.mjs scripts/generate-registry-items.test.ts scripts/catalog-drift.test.ts scripts/catalog-fetch-mirror.test.ts scripts/catalog-script-inlining.test.ts scripts/catalog-detail.test.ts scripts/generate-catalog-pages.test.ts scripts/verify-catalog-payloads.test.ts scripts/registry-theme-fallbacks.test.ts scripts/registry-skill-files.test.ts scripts/creator-editing-capabilities.test.mjs scripts/generate-catalog-previews.test.ts scripts/registry-primitive-payloads.test.ts registry/components/pan-stations/pan-stations.test.mjs registry/components/filmed-screen/filmed-screen.test.mjs registry/components/echo-smear/echo-smear.test.mjs registry/examples/path-traced-3d/source/path-traced-3d.test.mjs && vitest run scripts/catalog/ scripts/contrastRatchet.test.ts scripts/generate-catalog-payloads.test.ts", "typecheck:scripts": "tsc --noEmit -p scripts/tsconfig.json", "test:skills": "HYPERFRAMES_MEDIA_HOME_REQUIRED=1 node --import ./packages/cli/scripts/test-home.mjs --test 'skills/**/*.test.mjs' 'packages/cli/src/media-use/**/*.test.mjs'", "generate:previews": "tsx scripts/generate-template-previews.ts", diff --git a/scripts/publish-workflow.test.mjs b/scripts/publish-workflow.test.mjs index 84393acdcd..08c8bfb6c7 100644 --- a/scripts/publish-workflow.test.mjs +++ b/scripts/publish-workflow.test.mjs @@ -191,208 +191,16 @@ test("the Lambda handler zip is pinned before packing and attached before npm pu assert.ok( indexIn(publish, "Attach Lambda assets to release") < indexIn(publish, "Publish packages"), ); + const attach = publish.steps[indexIn(publish, "Attach Lambda assets to release")]; + assert.doesNotMatch(attach.run, /--clobber/); }); -test("the pack job pins only the Lambda assets this run built", () => { - for (const step of pack.steps) { - assert.doesNotMatch(step.run ?? "", /\bgh release\b/, step.name); - assert.ok(!JSON.stringify(step.env ?? {}).includes("secrets."), step.name); - } +test("packing pins only the freshly built zip and never downloads release assets", () => { const step = pack.steps.find((s) => s.name === "Pin Lambda handler zip"); - const builtZip = "built handler zip"; - const builtSha256 = createHash("sha256").update(builtZip).digest("hex"); - const run = (expectedSha256) => { - const root = mkdtempSync(join(tmpdir(), "hyperframes-lambda-zip-step-")); - try { - const bin = join(root, "bin"); - mkdirSync(bin); - mkdirSync(join(root, "dist", "lambda-handler-build"), { recursive: true }); - writeFileSync(join(root, "dist", "lambda-handler-build", "handler.zip"), builtZip); - const log = join(root, "calls.log"); - writeFileSync(log, ""); - writeFileSync(join(bin, "bun"), `#!/bin/sh\necho "bun $*" >> "${log}"\n`); - chmodSync(join(bin, "bun"), 0o755); - const result = spawnSync("bash", ["-euo", "pipefail", "-c", step.run], { - cwd: root, - encoding: "utf8", - env: { - ...process.env, - PATH: `${bin}:${process.env.PATH}`, - EXPECTED_SHA256: expectedSha256, - }, - timeout: 5_000, - }); - let pinned = null; - try { - pinned = readFileSync(join(root, "dist", "hyperframes-lambda-handler.zip"), "utf8"); - } catch {} - return { - status: result.status, - stdout: result.stdout, - calls: readFileSync(log, "utf8"), - pinned, - }; - } finally { - rmSync(root, { recursive: true, force: true }); - } - }; - - const built = run(builtSha256); - assert.equal(built.status, 0, built.stdout); - assert.match(built.calls, /^bun packages\/aws-lambda\/scripts\/pin-handler-zip\.ts$/m); - assert.equal(built.pinned, builtZip); - - for (const expectedSha256 of ["0".repeat(64), ""]) { - const rejected = run(expectedSha256); - assert.equal(rejected.status, 1, rejected.stdout); - assert.match(rejected.stdout, /::error::Lambda handler zip sha256/); - assert.doesNotMatch(rejected.calls, /pin-handler-zip/); - assert.equal(rejected.pinned, null); - } -}); - -test("the release keeps only Lambda assets that npm pins or this run produced", () => { - const step = publish.steps.find((s) => s.name === "Attach Lambda assets to release"); - assert.ok(step); - const sha = (text) => createHash("sha256").update(text).digest("hex"); - const assets = { - "hyperframes-lambda-handler.zip": ["hyperframes-lambda-handler.sha256", "handler-zip.json"], - "hyperframes-lambda-video-arm64.oci.tar": [ - "hyperframes-lambda-video-arm64.sha256", - "video-image.json", - ], - }; - const run = ({ npm = 404, pins, release = {}, attempt = 1, badIntegrity = false }) => { - const temp = mkdtempSync(join(tmpdir(), "hyperframes-attach-step-")); - try { - const bin = join(temp, "bin"); - const bundle = join(temp, "release-bundle", "assets"); - mkdirSync(bin); - mkdirSync(bundle, { recursive: true }); - for (const [asset, [sidecar]] of Object.entries(assets)) { - writeFileSync(join(bundle, asset), `bundle ${asset}`); - writeFileSync(join(bundle, sidecar), `${sha(`bundle ${asset}`)} ${asset}\n`); - } - const fixture = join(temp, "fixture"); - mkdirSync(join(fixture, "package", "prebuilt"), { recursive: true }); - for (const [asset, [, manifest]] of Object.entries(assets)) { - const pinned = pins?.[asset] ?? `pinned ${asset}`; - writeFileSync( - join(fixture, "package", "prebuilt", manifest), - JSON.stringify({ sha256: sha(pinned) }), - ); - } - const tarball = join(temp, "aws-lambda.tgz"); - execFileSync("tar", ["-czf", tarball, "-C", fixture, "package"], { stdio: "pipe" }); - const integrity = `sha512-${createHash("sha512").update(readFileSync(tarball)).digest("base64")}`; - writeFileSync( - join(temp, "meta.json"), - JSON.stringify({ - dist: { - tarball: "https://registry.invalid/aws-lambda.tgz", - integrity: badIntegrity ? "sha512-other" : integrity, - }, - }), - ); - writeFileSync( - join(temp, "release.tsv"), - Object.entries(release) - .map( - ([asset, contents]) => - `${asset}\t${contents === null ? "" : `sha256:${sha(contents)}`}\n`, - ) - .join(""), - ); - const log = join(temp, "calls.log"); - writeFileSync(log, ""); - writeFileSync( - join(bin, "curl"), - [ - "#!/bin/bash", - `echo "curl $*" >> "${log}"`, - 'while [ $# -gt 0 ]; do case "$1" in -o) out="$2"; shift ;; -w) status=1 ;; esac; shift; done', - `if [ -n "$status" ]; then cp "${join(temp, "meta.json")}" "$out"; printf '%s' "${npm}"; else cp "${tarball}" "$out"; fi`, - ].join("\n"), - ); - writeFileSync( - join(bin, "gh"), - `#!/bin/sh\necho "gh $*" >> "${log}"\nif [ "$1" = api ]; then cat "${join(temp, "release.tsv")}"; fi\n`, - ); - chmodSync(join(bin, "curl"), 0o755); - chmodSync(join(bin, "gh"), 0o755); - const result = runStep(step, { - cwd: temp, - path: `${bin}:${process.env.PATH}`, - env: { - RUNNER_TEMP: temp, - VERSION: "1.2.3", - REPO: "o/r", - GH_TOKEN: "test-token", - RUN_ATTEMPT: String(attempt), - }, - }); - const uploads = readFileSync(log, "utf8") - .split("\n") - .filter((line) => line.startsWith("gh release upload")); - return { ...result, uploads }; - } finally { - rmSync(temp, { recursive: true, force: true }); - } - }; - const names = Object.keys(assets); - const bundleRelease = Object.fromEntries(names.map((name) => [name, `bundle ${name}`])); - const foreignRelease = Object.fromEntries(names.map((name) => [name, `other ${name}`])); - const pinnedRelease = Object.fromEntries(names.map((name) => [name, `pinned ${name}`])); - - const fresh = run({}); - assert.equal(fresh.status, 0, fresh.stdout + fresh.stderr); - assert.equal(fresh.uploads.length, 2); - for (const line of fresh.uploads) { - assert.match(line, /^gh release upload v1\.2\.3 \S+ \S+\.sha256 --repo o\/r$/); - } - - const rerunPublish = run({ release: bundleRelease }); - assert.equal(rerunPublish.status, 0, rerunPublish.stdout + rerunPublish.stderr); - assert.deepEqual(rerunPublish.uploads, []); - - const foreign = run({ release: foreignRelease }); - assert.equal(foreign.status, 1); - assert.match( - foreign.stdout, - /::error::Release v1\.2\.3 already has .* this run did not attach it/, - ); - assert.deepEqual(foreign.uploads, []); - - const rerunReplaces = run({ release: foreignRelease, attempt: 2 }); - assert.equal(rerunReplaces.status, 0, rerunReplaces.stdout + rerunReplaces.stderr); - assert.equal(rerunReplaces.uploads.length, 2); - for (const line of rerunReplaces.uploads) assert.match(line, / --clobber$/); - assert.match(rerunReplaces.stdout, /::warning::Replacing/); - - const keepsPinned = run({ npm: 200, release: pinnedRelease, attempt: 2 }); - assert.equal(keepsPinned.status, 0, keepsPinned.stdout + keepsPinned.stderr); - assert.deepEqual(keepsPinned.uploads, []); - - for (const release of [foreignRelease, bundleRelease, {}]) { - const unpinned = run({ npm: 200, release, attempt: 2 }); - assert.equal(unpinned.status, 1, unpinned.stdout); - assert.match(unpinned.stdout, /::error::@hyperframes\/aws-lambda@1\.2\.3 on npm pins/); - assert.deepEqual(unpinned.uploads, []); - } - - const tamperedNpm = run({ npm: 200, release: pinnedRelease, badIntegrity: true }); - assert.equal(tamperedNpm.status, 1); - assert.match(tamperedNpm.stdout, /does not match its npm integrity/); - - const registryDown = run({ npm: 503 }); - assert.equal(registryDown.status, 1); - assert.match(registryDown.stdout, /npm registry answered 503/); - assert.deepEqual(registryDown.uploads, []); - - const noDigest = run({ release: { [names[0]]: null } }); - assert.equal(noDigest.status, 1); - assert.match(noDigest.stdout, /without a digest/); - assert.deepEqual(noDigest.uploads, []); + assert.match(step.run, /ACTUAL_SHA256.*sha256sum/); + assert.match(step.run, /ACTUAL_SHA256.*!=.*EXPECTED_SHA256/); + assert.doesNotMatch(workflow, /gh release download/); + assert.equal(step.env.EXPECTED_SHA256, "${{ needs.lambda-handler-zip.outputs.sha256 }}"); }); const stepOf = (job, name) => { @@ -436,7 +244,7 @@ test("the workflow publishes exactly the public workspace packages", () => { }); test("release-commit code runs only in a job without secrets", () => { - assert.deepEqual(pack.permissions, { contents: "read" }); + assert.deepEqual(pack.permissions, { contents: "read", actions: "read" }); assert.equal(pack.environment, undefined); const packCheckout = pack.steps.find((step) => step.uses?.startsWith("actions/checkout@")); assert.equal(packCheckout.with["persist-credentials"], false); @@ -602,8 +410,8 @@ test("publishing skips only identical uploads and fails on a version it did not assert.doesNotMatch(squatted.calls, /npm publish/); const rerun = run({ viewResult: "sha512-other", attempt: 2 }); - assert.equal(rerun.status, 0, rerun.stdout); - assert.match(rerun.stdout, /::warning::/); + assert.equal(rerun.status, 1, rerun.stdout); + assert.match(rerun.stdout, /this run did not publish it/); assert.doesNotMatch(rerun.calls, /npm publish/); }); diff --git a/scripts/release-lambda-assets.test.mjs b/scripts/release-lambda-assets.test.mjs new file mode 100644 index 0000000000..15df05a962 --- /dev/null +++ b/scripts/release-lambda-assets.test.mjs @@ -0,0 +1,342 @@ +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { mkdtempSync, mkdirSync, readFileSync, writeFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import { parse } from "yaml"; + +const workflow = parse( + readFileSync(new URL("../.github/workflows/publish.yml", import.meta.url), "utf8"), +); +const names = [ + "hyperframes-lambda-handler.zip", + "hyperframes-lambda-handler.sha256", + "hyperframes-lambda-video-arm64.oci.tar", + "hyperframes-lambda-video-arm64.sha256", +]; +const hash = (value) => createHash("sha256").update(value).digest("hex"); +const step = (name, job = "publish") => workflow.jobs[job].steps.find((s) => s.name === name); + +function fixture(t) { + const root = mkdtempSync(join(tmpdir(), "hf-release-receipt-")); + t.after(() => rmSync(root, { recursive: true, force: true })); + for (const dir of [ + "bin", + "release-bundle/assets", + "release-receipt", + "dist/lambda-handler-build", + ]) + mkdirSync(join(root, dir), { recursive: true }); + for (const name of names) + writeFileSync(join(root, "release-bundle/assets", name), `built ${name}`); + writeFileSync(join(root, "dist/lambda-handler-build/handler.zip"), "built zip"); + const statePath = join(root, "state.json"); + const initial = { release: null, assets: [], calls: [], artifacts: [], failUpload: null }; + writeFileSync(statePath, JSON.stringify(initial)); + // Only this offline stub can service gh calls. No credentials or real network calls. + writeFileSync( + join(root, "bin/gh"), + `#!${process.execPath} +const fs = require('node:fs'); const crypto = require('node:crypto'); +const state = JSON.parse(fs.readFileSync(process.env.STATE_FILE, 'utf8')); +const args = process.argv.slice(2); state.calls.push(args); +let response; let status = 0; +const route = args.find(a => a.startsWith('repos/') || a.startsWith('https://uploads.github.com/')); +if (route?.includes('/actions/runs/')) response = {artifacts:state.artifacts}; +else if (route?.endsWith('/generate-notes')) response = 'generated notes'; +else if (route === 'repos/test/repo/releases' && args.includes('POST')) { + if (state.release) status = 1; + else { state.release = {id:123, tag_name:'v9.8.7'}; response = state.release; } +} else if (route?.includes('/releases/tags/')) { + if (!state.release) status = 1; else response = state.release; +} else if (route?.startsWith('https://uploads.github.com/')) { + const id = Number(route.split('/releases/')[1].split('/')[0]); + const name = new URL(route).searchParams.get('name'); + if (state.release?.id !== id || state.failUpload === name) status = 1; + else { + const file = args[args.indexOf('--input') + 1]; + response = {id:state.assets.length+1,name,state:'uploaded',digest:'sha256:'+crypto.createHash('sha256').update(fs.readFileSync(file)).digest('hex')}; + state.assets.push(response); + } +} else if (route?.includes('/releases/123/assets')) response = state.assets; +else status = 1; +fs.writeFileSync(process.env.STATE_FILE, JSON.stringify(state)); +if (response !== undefined) process.stdout.write(typeof response === 'string' ? response : JSON.stringify(response)); +process.exit(status); +`, + { mode: 0o755 }, + ); + writeFileSync( + join(root, "bin/bun"), + "#!/bin/sh\ncp dist/hyperframes-lambda-handler.zip pinned.zip\n", + { mode: 0o755 }, + ); + // Registry responses and package bytes are local fixtures; curl never accesses the network. + writeFileSync( + join(root, "bin/curl"), + `#!${process.execPath} +const fs = require('node:fs'); const path = require('node:path'); +const args = process.argv.slice(2); const root = process.env.RUNNER_TEMP; +if (process.env.NPM_STATUS === 'network-error') process.exit(7); +const out = args[args.indexOf('-o') + 1]; +if (args.includes('-w')) { + fs.copyFileSync(path.join(root, 'npm-meta.json'), out); + process.stdout.write(process.env.NPM_STATUS || '404'); +} else fs.copyFileSync(path.join(root, 'npm-package.tgz'), out); +`, + { mode: 0o755 }, + ); + const published = (pins = {}) => { + const dir = join(root, "npm-fixture/package/prebuilt"); + mkdirSync(dir, { recursive: true }); + for (const [manifest, asset] of [ + ["handler-zip.json", names[0]], + ["video-image.json", names[2]], + ]) + writeFileSync( + join(dir, manifest), + JSON.stringify({ sha256: pins[manifest] ?? hash(`built ${asset}`) }), + ); + execFileSync("tar", [ + "-czf", + join(root, "npm-package.tgz"), + "-C", + join(root, "npm-fixture"), + "package", + ]); + const integrity = + "sha512-" + + createHash("sha512") + .update(readFileSync(join(root, "npm-package.tgz"))) + .digest("base64"); + writeFileSync( + join(root, "npm-meta.json"), + JSON.stringify({ dist: { tarball: "https://fixture.invalid/package.tgz", integrity } }), + ); + }; + published(); + const state = () => JSON.parse(readFileSync(statePath, "utf8")); + const update = (changes) => writeFileSync(statePath, JSON.stringify({ ...state(), ...changes })); + const receiptPath = join(root, "release-receipt/receipt.json"); + const receipt = () => JSON.parse(readFileSync(receiptPath, "utf8")); + const run = (name, env = {}, job = "publish") => { + const command = step(name, job); + assert.ok(command, `Missing workflow step: ${name}`); + return spawnSync( + "bash", + [ + "-e", + ...(workflow.defaults?.run?.shell === "bash" ? ["-o", "pipefail"] : []), + "-c", + command.run, + ], + { + cwd: root, + encoding: "utf8", + timeout: 5000, + env: { + PATH: `${join(root, "bin")}:${process.env.PATH}`, + STATE_FILE: statePath, + RUNNER_TEMP: root, + GITHUB_OUTPUT: join(root, "outputs"), + REPO: "test/repo", + VERSION: "9.8.7", + RUN_ID: "55", + RUN_NUMBER: "55", + EXPECTED_RELEASE_SHA: "a".repeat(40), + PRERELEASE: "false", + RELEASE_ID: "123", + ...env, + }, + }, + ); + }; + return { root, run, state, update, receipt, receiptPath, published }; +} +const succeeds = (result) => assert.equal(result.status, 0, result.stdout + result.stderr); +const fails = (result) => assert.notEqual(result.status, 0, result.stdout + result.stderr); + +test("a first publish creates a durable receipt before uploads; same-run retry preserves bytes", (t) => { + const f = fixture(t); + succeeds(f.run("Create GitHub Release")); + assert.equal(f.receipt().release_id, 123); + assert.equal(f.receipt().run, "55"); + assert.equal(f.receipt().assets[names[0]], hash(`built ${names[0]}`)); + succeeds(f.run("Attach Lambda assets to release")); + succeeds(f.run("Verify Lambda release before npm publication")); + succeeds(f.run("Create GitHub Release")); + succeeds(f.run("Attach Lambda assets to release")); + assert.equal(f.state().assets.length, 4); + assert.equal( + f.state().calls.filter((args) => args.includes("repos/test/repo/releases")).length, + 1, + ); +}); + +test("an existing foreign release is rejected, including a bot-authored creation race", (t) => { + const f = fixture(t); + f.update({ release: { id: 999, author: { login: "github-actions[bot]" } } }); + fails(f.run("Create GitHub Release")); + assert.equal(f.state().assets.length, 0); +}); + +test("receipt scope, release identity and original digests fail closed", (t) => { + const f = fixture(t); + succeeds(f.run("Create GitHub Release")); + const original = f.receipt(); + for (const changed of [ + { run: "other" }, + { repo: "other/repo" }, + { sha: "b".repeat(40) }, + { tag: "v0.0.0" }, + { release_id: null }, + { release_id: 1.5 }, + { release_id: 999 }, + { assets: {} }, + ]) { + writeFileSync(f.receiptPath, JSON.stringify({ ...original, ...changed })); + fails(f.run("Create GitHub Release")); + } + writeFileSync(f.receiptPath, JSON.stringify(original)); + f.update({ release: { id: 999 } }); + fails(f.run("Create GitHub Release")); + f.update({ release: { id: 123 } }); + writeFileSync(join(f.root, "release-bundle/assets", names[0]), "replacement bundle"); + fails(f.run("Create GitHub Release")); +}); + +test("partial pair upload is recoverable from the exact original bundle", (t) => { + const f = fixture(t); + succeeds(f.run("Create GitHub Release")); + f.update({ failUpload: names[1] }); + fails(f.run("Attach Lambda assets to release")); + assert.equal(f.state().assets.length, 1); + f.update({ failUpload: null }); + succeeds(f.run("Create GitHub Release")); + succeeds(f.run("Attach Lambda assets to release")); + succeeds(f.run("Verify Lambda release before npm publication")); + assert.equal(f.state().assets.length, 4); +}); + +test("missing, altered or ambiguous release assets never pass publication checks", (t) => { + const f = fixture(t); + succeeds(f.run("Create GitHub Release")); + succeeds(f.run("Attach Lambda assets to release")); + const original = f.state().assets; + for (const assets of [ + original.slice(1), + [...original, original[0]], + original.map((a, i) => (i ? a : { ...a, digest: undefined })), + original.map((a, i) => (i ? a : { ...a, digest: "sha256:" + "0".repeat(64) })), + ]) { + f.update({ assets }); + fails(f.run("Verify Lambda release before npm publication")); + } + f.update({ assets: original, release: { id: 999 } }); + fails(f.run("Verify Lambda release before npm publication")); +}); + +test("a substituted asset is rejected on attachment retry instead of being trusted or clobbered", (t) => { + const f = fixture(t); + succeeds(f.run("Create GitHub Release")); + f.update({ + assets: [{ id: 42, name: names[0], state: "uploaded", digest: "sha256:" + hash("attacker") }], + }); + fails(f.run("Attach Lambda assets to release")); + assert.equal(f.state().assets.length, 1); +}); + +test("artifact lookup is run-scoped and refuses expired or ambiguous receipts and bundles", (t) => { + const f = fixture(t); + for (const [name, job, artifactName] of [ + ["Find previous release bundle", "pack", "release-bundle"], + ["Find previous release receipt", "publish", "lambda-release-receipt"], + ]) { + for (const artifacts of [[], [{ id: 7, name: artifactName, expired: false }]]) { + f.update({ artifacts }); + succeeds(f.run(name, {}, job)); + } + for (const artifacts of [ + [{ id: 7, name: artifactName, expired: true }], + [ + { id: 7, name: artifactName, expired: false }, + { id: 8, name: artifactName, expired: false }, + ], + ]) { + f.update({ artifacts }); + fails(f.run(name, {}, job)); + } + } + assert.ok( + f.state().calls.every((args) => args.some((arg) => arg.includes("/actions/runs/55/artifacts"))), + ); +}); + +test("fresh pack pins only the independent build hash and rejects mismatches", (t) => { + const f = fixture(t); + for (const expected of ["", "0".repeat(64)]) + fails(f.run("Pin Lambda handler zip", { EXPECTED_SHA256: expected }, "pack")); + succeeds(f.run("Pin Lambda handler zip", { EXPECTED_SHA256: hash("built zip") }, "pack")); + assert.equal(readFileSync(join(f.root, "pinned.zip"), "utf8"), "built zip"); + assert.equal(f.state().calls.length, 0); +}); + +test("receipt and bundle persistence precede side effects and do not overwrite provenance", () => { + const pack = workflow.jobs.pack; + const upload = pack.steps.find((s) => s.id === "bundle"); + assert.equal(upload.with.overwrite, false); + const resolve = pack.steps.findIndex((s) => s.name === "Resolve version"); + for (const s of pack.steps.slice(resolve)) + assert.equal(s.if, "steps.previous_bundle.outputs.artifact == ''"); + const publish = workflow.jobs.publish.steps; + assert.ok( + publish.findIndex((s) => s.name === "Save release receipt") < + publish.findIndex((s) => s.name === "Attach Lambda assets to release"), + ); + assert.equal(step("Save release receipt").with.overwrite, false); + assert.ok( + publish.findIndex((s) => s.name === "Verify Lambda release before npm publication") < + publish.findIndex((s) => s.name === "Publish packages"), + ); + const download = publish.find( + (s) => + s.uses?.startsWith("actions/download-artifact@") && + s.with.path === "${{ runner.temp }}/release-bundle", + ); + assert.equal(download.with["artifact-ids"], "${{ needs.pack.outputs.bundle_id }}"); + assert.equal(workflow.concurrency["cancel-in-progress"], false); +}); + +// GitHub's implicit bash shell does not enable pipefail; explicit shell: bash does. +test("an incomplete bundle fails before creating a release or receipt", (t) => { + const f = fixture(t); + rmSync(join(f.root, "release-bundle/assets", names[0])); + fails(f.run("Create GitHub Release")); + assert.equal(f.state().release, null); +}); + +test("published npm pins accept only the original bundle and an integrity-verified package", (t) => { + const f = fixture(t); + succeeds(f.run("Check published Lambda pins")); + succeeds(f.run("Check published Lambda pins", { NPM_STATUS: "200" })); + for (const manifest of ["handler-zip.json", "video-image.json"]) { + f.published({ [manifest]: hash("different published bytes") }); + fails(f.run("Check published Lambda pins", { NPM_STATUS: "200" })); + } + f.published(); + writeFileSync(join(f.root, "npm-package.tgz"), "corrupt package"); + fails(f.run("Check published Lambda pins", { NPM_STATUS: "200" })); + assert.equal(f.state().calls.length, 0); +}); + +test("npm registry uncertainty fails before release mutation", (t) => { + const f = fixture(t); + for (const status of ["500", "403", "network-error"]) + fails(f.run("Check published Lambda pins", { NPM_STATUS: status })); + const steps = workflow.jobs.publish.steps; + const check = steps.findIndex((s) => s.name === "Check published Lambda pins"); + assert.ok(check >= 0 && check < steps.findIndex((s) => s.name === "Create GitHub Release")); + assert.equal(f.state().calls.length, 0); +});