name: OpenClaw NPM Release

on:
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag to publish, or a full 40-character workflow-branch commit SHA for validation-only preflight (for example v2026.3.22 or 0123456789abcdef0123456789abcdef01234567)
        required: true
        type: string
      preflight_only:
        description: Run validation/build only and skip the gated publish job
        required: true
        default: false
        type: boolean
      preflight_run_id:
        description: Existing successful preflight workflow run id to promote without rebuilding
        required: false
        type: string
      full_release_validation_run_id:
        description: Successful Full Release Validation run id for this tag/SHA, required for real publish
        required: false
        type: string
      release_publish_run_id:
        description: Approved OpenClaw Release Publish workflow run id
        required: false
        type: string
      npm_dist_tag:
        description: npm dist-tag to publish to
        required: true
        default: beta
        type: choice
        options:
          - alpha
          - beta
          - latest

concurrency:
  group: ${{ github.event_name == 'workflow_dispatch' && inputs.preflight_only && format('openclaw-npm-release-{0}-{1}-preflight', inputs.tag, inputs.npm_dist_tag) || github.event_name == 'workflow_dispatch' && format('openclaw-npm-release-{0}-{1}-publish-{2}', inputs.tag, inputs.npm_dist_tag, github.run_id) || format('openclaw-npm-release-{0}', github.ref) }}
  cancel-in-progress: ${{ github.event_name == 'workflow_dispatch' && inputs.preflight_only && inputs.npm_dist_tag == 'alpha' }}

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
  NODE_VERSION: "24.15.0"

jobs:
  # PLEASE DON'T ADD LONG-RUNNING OR FLAKY CHECKS TO THE npm RELEASE PATH.
  # KEEP THIS WORKFLOW SHORT AND DETERMINISTIC OR IT CAN GET STUCK AND JEOPARDIZE THE RELEASE.
  # RELEASE-TIME LIVE OR END-TO-END VALIDATION BELONGS IN openclaw-release-checks.yml.
  # SECURITY NOTE: TOKEN-BASED npm dist-tag mutation moved to
  # openclaw/releases/.github/workflows/openclaw-npm-dist-tags.yml
  # so this source workflow can stay focused on OIDC publish only.
  preflight_openclaw_npm:
    if: ${{ inputs.preflight_only }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Validate release ref input format
        env:
          RELEASE_REF: ${{ inputs.tag }}
          PREFLIGHT_ONLY: ${{ inputs.preflight_only }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
        run: |
          set -euo pipefail
          if [[ ! "${RELEASE_REF}" =~ ^v[0-9]{4}\.[1-9][0-9]*\.[1-9][0-9]*((-(alpha|beta)\.[1-9][0-9]*)|(-[1-9][0-9]*))?$ ]] && [[ ! "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            echo "Invalid release ref format: ${RELEASE_REF}"
            exit 1
          fi
          if [[ "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]] && [[ "${PREFLIGHT_ONLY}" != "true" ]]; then
            echo "Full commit SHA input is only supported for validation-only preflight runs."
            exit 1
          fi
          if [[ "${RELEASE_REF}" == *"-alpha."* && "${RELEASE_NPM_DIST_TAG}" != "alpha" ]]; then
            echo "Alpha prerelease tags must publish to npm dist-tag alpha."
            exit 1
          fi
          if [[ "${RELEASE_REF}" == *"-beta."* && "${RELEASE_NPM_DIST_TAG}" != "beta" ]]; then
            echo "Beta prerelease tags must publish to npm dist-tag beta."
            exit 1
          fi

      - name: Forbid preflight artifact promotion on validation-only runs
        if: ${{ inputs.preflight_only && inputs.preflight_run_id != '' }}
        run: |
          echo "preflight_run_id is only valid for real publish runs."
          exit 1

      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: ${{ inputs.tag }}
          fetch-depth: 0

      - name: Validate Tideclaw alpha preflight target
        if: startsWith(github.ref, 'refs/heads/tideclaw/alpha/')
        env:
          RELEASE_REF: ${{ inputs.tag }}
          WORKFLOW_REF: ${{ github.ref }}
        run: |
          set -euo pipefail
          if [[ ! "${RELEASE_REF}" == *"-alpha."* && ! "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            echo "Tideclaw alpha preflight runs must target an alpha prerelease tag or SHA." >&2
            exit 1
          fi
          if [[ ! "${WORKFLOW_REF}" =~ ^refs/heads/tideclaw/alpha/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{4}Z$ ]]; then
            echo "Tideclaw alpha preflight runs must run from tideclaw/alpha/YYYY-MM-DD-HHMMZ." >&2
            exit 1
          fi
          alpha_branch="${WORKFLOW_REF#refs/heads/}"
          git fetch --no-tags origin "+refs/heads/${alpha_branch}:refs/remotes/origin/${alpha_branch}"
          if ! git merge-base --is-ancestor HEAD "refs/remotes/origin/${alpha_branch}"; then
            echo "Alpha preflight target must be reachable from ${alpha_branch}." >&2
            exit 1
          fi

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          node-version: ${{ env.NODE_VERSION }}
          install-bun: "true"

      - name: Ensure version is not already published
        env:
          PREFLIGHT_ONLY: ${{ inputs.preflight_only }}
        run: |
          set -euo pipefail
          PACKAGE_VERSION=$(node -p "require('./package.json').version")

          if npm view "openclaw@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
            if [[ "${PREFLIGHT_ONLY}" == "true" ]]; then
              echo "openclaw@${PACKAGE_VERSION} is already published on npm; continuing because preflight_only=true."
              exit 0
            fi
            echo "openclaw@${PACKAGE_VERSION} is already published on npm."
            exit 1
          fi

          echo "Publishing openclaw@${PACKAGE_VERSION}"

      - name: Check
        env:
          OPENCLAW_LOCAL_CHECK: "0"
        run: pnpm check

      - name: Check test types
        env:
          OPENCLAW_LOCAL_CHECK: "0"
        run: pnpm check:test-types

      - name: Check architecture
        env:
          OPENCLAW_LOCAL_CHECK: "0"
        run: pnpm check:architecture

      - name: Build
        run: pnpm build

      - name: Build Control UI
        run: pnpm ui:build

      - name: Validate release metadata
        if: ${{ inputs.preflight_run_id == '' }}
        env:
          OPENCLAW_NPM_RELEASE_SKIP_PACK_CHECK: "1"
          RELEASE_REF: ${{ inputs.tag }}
          PREFLIGHT_ONLY: ${{ inputs.preflight_only }}
          WORKFLOW_REF_NAME: ${{ github.ref_name }}
          OPENCLAW_NPM_PUBLISH_TAG: ${{ inputs.npm_dist_tag }}
        run: |
          set -euo pipefail
          RELEASE_SHA=$(git rev-parse HEAD)
          RELEASE_BRANCH_REF="refs/remotes/origin/${WORKFLOW_REF_NAME}"
          export RELEASE_SHA RELEASE_BRANCH_REF
          # Fetch the workflow branch so merge-base ancestry checks keep working
          # for older tagged commits contained in a release branch.
          git fetch --no-tags origin "+refs/heads/${WORKFLOW_REF_NAME}:refs/remotes/origin/${WORKFLOW_REF_NAME}"
          if [[ "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            BRANCH_SHA="$(git rev-parse "${RELEASE_BRANCH_REF}")"
            if [[ "${RELEASE_SHA}" != "${BRANCH_SHA}" ]]; then
              echo "Validation-only SHA mode only supports the current ${WORKFLOW_REF_NAME} HEAD." >&2
              exit 1
            fi
            RELEASE_TAG="v$(node -p "require('./package.json').version")"
            export RELEASE_TAG
            echo "Validation-only SHA mode: using synthetic release tag ${RELEASE_TAG} for package metadata checks."
          else
            RELEASE_TAG="${RELEASE_REF}"
            export RELEASE_TAG
          fi
          RELEASE_MAIN_REF="${RELEASE_BRANCH_REF}"
          export RELEASE_MAIN_REF
          pnpm release:openclaw:npm:check

      # KEEP THIS LANE LIMITED TO FAST, REPEATABLE RELEASE READINESS CHECKS.
      # IF A CHECK CAN TAKE A LONG TIME, NEEDS LIVE CREDENTIALS, OR IS KNOWN TO BE FLAKY,
      # IT BELONGS IN openclaw-release-checks.yml INSTEAD OF BLOCKING npm PUBLISH.
      - name: Verify release contents
        run: pnpm release:check

      - name: Generate dependency release evidence
        id: dependency_evidence
        env:
          RELEASE_REF: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
        run: |
          set -euo pipefail
          node scripts/generate-dependency-release-evidence.mjs \
            --release-ref "$RELEASE_REF" \
            --npm-dist-tag "$RELEASE_NPM_DIST_TAG" \
            --output-dir "$RUNNER_TEMP/openclaw-release-dependency-evidence" \
            --github-output "$GITHUB_OUTPUT" \
            --github-step-summary "$GITHUB_STEP_SUMMARY"

      - name: Pack prepared npm tarball
        id: packed_tarball
        env:
          OPENCLAW_PREPACK_PREPARED: "1"
          RELEASE_REF: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
          DEPENDENCY_EVIDENCE_DIR: ${{ steps.dependency_evidence.outputs.dir }}
        run: |
          set -euo pipefail
          PACK_OUTPUT="$RUNNER_TEMP/npm-pack-output.txt"
          npm pack --json 2>&1 | tee "$PACK_OUTPUT"
          PACK_PATH="$(node - "$PACK_OUTPUT" <<'NODE'
          const fs = require("node:fs");
          const input = fs.readFileSync(process.argv[2], "utf8");

          function arrayEndFrom(start) {
            let depth = 0;
            let inString = false;
            let escape = false;
            for (let i = start; i < input.length; i += 1) {
              const char = input[i];
              if (inString) {
                if (escape) {
                  escape = false;
                } else if (char === "\\") {
                  escape = true;
                } else if (char === "\"") {
                  inString = false;
                }
                continue;
              }
              if (char === "\"") {
                inString = true;
              } else if (char === "[") {
                depth += 1;
              } else if (char === "]") {
                depth -= 1;
                if (depth === 0) {
                  return i + 1;
                }
              }
            }
            return -1;
          }

          for (let start = input.indexOf("["); start !== -1; start = input.indexOf("[", start + 1)) {
            const end = arrayEndFrom(start);
            if (end === -1) {
              continue;
            }
            try {
              const parsed = JSON.parse(input.slice(start, end));
              const first = Array.isArray(parsed) ? parsed[0] : null;
              if (first && typeof first.filename === "string" && first.filename) {
                process.stdout.write(first.filename);
                process.exit(0);
              }
            } catch {
              // Keep scanning; npm lifecycle output can legally precede the JSON.
            }
          }

          console.error("Could not find npm pack --json output with a filename.");
          process.exit(1);
          NODE
          )"
          if [[ -z "$PACK_PATH" || ! -f "$PACK_PATH" ]]; then
            echo "npm pack did not produce a tarball file." >&2
            exit 1
          fi
          RELEASE_SHA="$(git rev-parse HEAD)"
          PACKAGE_VERSION="$(node -p "require('./package.json').version")"
          if [[ "${RELEASE_REF}" =~ ^[0-9a-fA-F]{40}$ ]]; then
            RELEASE_TAG="v${PACKAGE_VERSION}"
          else
            RELEASE_TAG="${RELEASE_REF}"
          fi
          TARBALL_NAME="$(basename "$PACK_PATH")"
          TARBALL_SHA256="$(sha256sum "$PACK_PATH" | awk '{print $1}')"
          ARTIFACT_DIR="$RUNNER_TEMP/openclaw-npm-preflight"
          rm -rf "$ARTIFACT_DIR"
          mkdir -p "$ARTIFACT_DIR"
          cp "$PACK_PATH" "$ARTIFACT_DIR/"
          cp -R "$DEPENDENCY_EVIDENCE_DIR" "$ARTIFACT_DIR/dependency-evidence"
          printf '%s\n' "$RELEASE_TAG" > "$ARTIFACT_DIR/release-tag.txt"
          printf '%s\n' "$RELEASE_SHA" > "$ARTIFACT_DIR/release-sha.txt"
          printf '%s\n' "$RELEASE_NPM_DIST_TAG" > "$ARTIFACT_DIR/release-npm-dist-tag.txt"
          ARTIFACT_DIR="$ARTIFACT_DIR" RELEASE_TAG="$RELEASE_TAG" RELEASE_SHA="$RELEASE_SHA" RELEASE_NPM_DIST_TAG="$RELEASE_NPM_DIST_TAG" PACKAGE_VERSION="$PACKAGE_VERSION" TARBALL_NAME="$TARBALL_NAME" TARBALL_SHA256="$TARBALL_SHA256" node <<'NODE'
          const fs = require("node:fs");
          const path = require("node:path");
          const manifest = {
            version: 1,
            releaseTag: process.env.RELEASE_TAG,
            releaseSha: process.env.RELEASE_SHA,
            npmDistTag: process.env.RELEASE_NPM_DIST_TAG,
            packageName: "openclaw",
            packageVersion: process.env.PACKAGE_VERSION,
            tarballName: process.env.TARBALL_NAME,
            tarballSha256: process.env.TARBALL_SHA256,
            dependencyEvidenceDir: "dependency-evidence",
            dependencyEvidenceManifest: "dependency-evidence/dependency-evidence-manifest.json",
          };
          fs.writeFileSync(
            path.join(process.env.ARTIFACT_DIR, "preflight-manifest.json"),
            `${JSON.stringify(manifest, null, 2)}\n`,
          );
          NODE
          echo "dir=$ARTIFACT_DIR" >> "$GITHUB_OUTPUT"
          echo "release_tag=$RELEASE_TAG" >> "$GITHUB_OUTPUT"

      - name: Verify prepared npm tarball install
        env:
          PREFLIGHT_ARTIFACT_DIR: ${{ steps.packed_tarball.outputs.dir }}
        run: |
          set -euo pipefail
          TARBALL_PATH="$(find "$PREFLIGHT_ARTIFACT_DIR" -maxdepth 1 -type f -name '*.tgz' -print | sort | tail -n 1)"
          if [[ -z "$TARBALL_PATH" ]]; then
            echo "Prepared preflight tarball not found." >&2
            ls -la "$PREFLIGHT_ARTIFACT_DIR" >&2 || true
            exit 1
          fi
          PACKAGE_VERSION="$(node -p "require('./package.json').version")"
          node --import tsx scripts/openclaw-npm-prepublish-verify.ts "$TARBALL_PATH" "$PACKAGE_VERSION"

      - name: Upload dependency release evidence
        uses: actions/upload-artifact@v7
        with:
          name: openclaw-release-dependency-evidence-${{ inputs.tag }}
          path: ${{ steps.dependency_evidence.outputs.dir }}
          if-no-files-found: error

      - name: Upload dependency release evidence tag alias
        if: ${{ steps.packed_tarball.outputs.release_tag != inputs.tag }}
        uses: actions/upload-artifact@v7
        with:
          name: openclaw-release-dependency-evidence-${{ steps.packed_tarball.outputs.release_tag }}
          path: ${{ steps.dependency_evidence.outputs.dir }}
          if-no-files-found: error

      - name: Upload prepared npm publish bundle
        uses: actions/upload-artifact@v7
        with:
          name: openclaw-npm-preflight-${{ inputs.tag }}
          path: ${{ steps.packed_tarball.outputs.dir }}
          if-no-files-found: error

      - name: Upload prepared npm publish bundle tag alias
        if: ${{ steps.packed_tarball.outputs.release_tag != inputs.tag }}
        uses: actions/upload-artifact@v7
        with:
          name: openclaw-npm-preflight-${{ steps.packed_tarball.outputs.release_tag }}
          path: ${{ steps.packed_tarball.outputs.dir }}
          if-no-files-found: error

  validate_publish_request:
    if: ${{ !inputs.preflight_only }}
    runs-on: ubuntu-latest
    permissions:
      actions: read
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          persist-credentials: false

      - name: Require trusted workflow ref for publish
        env:
          RELEASE_TAG: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
          WORKFLOW_REF: ${{ github.ref }}
        run: |
          set -euo pipefail
          tideclaw_alpha_publish=false
          if [[ "${RELEASE_TAG}" == *"-alpha."* && "${RELEASE_NPM_DIST_TAG}" == "alpha" && "${WORKFLOW_REF}" =~ ^refs/heads/tideclaw/alpha/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{4}Z$ ]]; then
            tideclaw_alpha_publish=true
          fi
          if [[ "${WORKFLOW_REF}" != "refs/heads/main" ]] && [[ ! "${WORKFLOW_REF}" =~ ^refs/heads/release/[0-9]{4}\.[1-9][0-9]*\.[1-9][0-9]*$ ]] && [[ "${tideclaw_alpha_publish}" != "true" ]]; then
            echo "Real publish runs must be dispatched from main, release/YYYY.M.D, or a Tideclaw alpha branch for alpha prereleases. Use preflight_only=true for other branch validation."
            exit 1
          fi

      - name: Require preflight artifact promotion on real publish
        env:
          RELEASE_TAG: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
          PREFLIGHT_RUN_ID: ${{ inputs.preflight_run_id }}
          FULL_RELEASE_VALIDATION_RUN_ID: ${{ inputs.full_release_validation_run_id }}
          RELEASE_PUBLISH_RUN_ID: ${{ inputs.release_publish_run_id }}
        run: |
          set -euo pipefail
          if [[ -z "${PREFLIGHT_RUN_ID}" ]]; then
            echo "Real publish requires preflight_run_id from a successful npm preflight run." >&2
            exit 1
          fi
          if [[ -z "${FULL_RELEASE_VALIDATION_RUN_ID}" ]]; then
            if [[ "${RELEASE_TAG}" == *"-beta."* && "${RELEASE_NPM_DIST_TAG}" == "beta" ]]; then
              echo "::warning::Beta publish is proceeding from npm preflight only; full release validation remains required before stable/latest promotion."
            else
              echo "Real publish requires full_release_validation_run_id from a successful Full Release Validation run." >&2
              exit 1
            fi
          fi
          if [[ -z "${RELEASE_PUBLISH_RUN_ID// }" && "${GITHUB_ACTOR}" == "github-actions[bot]" ]]; then
            echo "Workflow-dispatched real publish requires release_publish_run_id from the approved OpenClaw Release Publish workflow." >&2
            exit 1
          fi

      - name: Validate release publish approval run
        env:
          GH_TOKEN: ${{ github.token }}
          RELEASE_PUBLISH_RUN_ID: ${{ inputs.release_publish_run_id }}
          EXPECTED_WORKFLOW_BRANCH: ${{ github.ref_name }}
        run: |
          set -euo pipefail
          if [[ -z "${RELEASE_PUBLISH_RUN_ID// }" ]]; then
            if [[ "${GITHUB_ACTOR}" == "github-actions[bot]" ]]; then
              echo "OpenClaw npm publish dispatched by another workflow must include release_publish_run_id." >&2
              exit 1
            fi
            echo "Direct OpenClaw npm publish; relying on this workflow's npm-release environment approval."
            exit 0
          fi
          direct_recovery=false
          if [[ "${GITHUB_ACTOR}" != "github-actions[bot]" ]]; then
            direct_recovery=true
            echo "Direct OpenClaw npm recovery with release_publish_run_id; relying on this workflow's npm-release environment approval."
          fi
          RUN_JSON="$(gh run view "$RELEASE_PUBLISH_RUN_ID" --repo "$GITHUB_REPOSITORY" --json workflowName,headBranch,event,status,conclusion,url)"
          printf '%s' "$RUN_JSON" | DIRECT_RELEASE_RECOVERY="${direct_recovery}" node scripts/validate-release-publish-approval.mjs

  publish_openclaw_npm:
    # KEEP THE REAL RELEASE/PUBLISH PATH ON A GITHUB-HOSTED RUNNER.
    # npm trusted publishing + provenance requires this to stay on ubuntu-latest.
    needs: [validate_publish_request]
    if: ${{ !inputs.preflight_only }}
    runs-on: ubuntu-latest
    environment: npm-release
    permissions:
      actions: read
      contents: read
      id-token: write
    steps:
      - name: Validate tag input format
        env:
          RELEASE_TAG: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
        run: |
          set -euo pipefail
          if [[ ! "${RELEASE_TAG}" =~ ^v[0-9]{4}\.[1-9][0-9]*\.[1-9][0-9]*((-(alpha|beta)\.[1-9][0-9]*)|(-[1-9][0-9]*))?$ ]]; then
            echo "Invalid release tag format: ${RELEASE_TAG}"
            exit 1
          fi
          if [[ "${RELEASE_TAG}" == *"-alpha."* && "${RELEASE_NPM_DIST_TAG}" != "alpha" ]]; then
            echo "Alpha prerelease tags must publish to npm dist-tag alpha."
            exit 1
          fi
          if [[ "${RELEASE_TAG}" == *"-beta."* && "${RELEASE_NPM_DIST_TAG}" != "beta" ]]; then
            echo "Beta prerelease tags must publish to npm dist-tag beta."
            exit 1
          fi

      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: refs/tags/${{ inputs.tag }}
          fetch-depth: 0

      - name: Validate Tideclaw alpha publish target
        if: startsWith(github.ref, 'refs/heads/tideclaw/alpha/')
        env:
          RELEASE_TAG: ${{ inputs.tag }}
          WORKFLOW_REF: ${{ github.ref }}
        run: |
          set -euo pipefail
          if [[ ! "${RELEASE_TAG}" == *"-alpha."* ]]; then
            echo "Tideclaw alpha publish runs must target an alpha prerelease tag." >&2
            exit 1
          fi
          if [[ ! "${WORKFLOW_REF}" =~ ^refs/heads/tideclaw/alpha/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{4}Z$ ]]; then
            echo "Tideclaw alpha publish runs must run from tideclaw/alpha/YYYY-MM-DD-HHMMZ." >&2
            exit 1
          fi
          alpha_branch="${WORKFLOW_REF#refs/heads/}"
          git fetch --no-tags origin "+refs/heads/${alpha_branch}:refs/remotes/origin/${alpha_branch}"
          if ! git merge-base --is-ancestor HEAD "refs/remotes/origin/${alpha_branch}"; then
            echo "Alpha publish tag must be reachable from ${alpha_branch}." >&2
            exit 1
          fi

      - name: Setup Node environment
        uses: ./.github/actions/setup-node-env
        with:
          node-version: ${{ env.NODE_VERSION }}
          install-bun: "false"

      - name: Ensure version is not already published
        run: |
          set -euo pipefail
          PACKAGE_VERSION=$(node -p "require('./package.json').version")

          if npm view "openclaw@${PACKAGE_VERSION}" version >/dev/null 2>&1; then
            echo "openclaw@${PACKAGE_VERSION} is already published on npm."
            exit 1
          fi

          echo "Publishing openclaw@${PACKAGE_VERSION}"

      - name: Verify preflight run metadata
        env:
          GH_TOKEN: ${{ github.token }}
          PREFLIGHT_RUN_ID: ${{ inputs.preflight_run_id }}
        run: |
          set -euo pipefail
          RUN_JSON="$(gh run view "$PREFLIGHT_RUN_ID" --repo "$GITHUB_REPOSITORY" --json workflowName,headBranch,event,conclusion,url)"
          printf '%s' "$RUN_JSON" | node -e 'const fs = require("node:fs"); const run = JSON.parse(fs.readFileSync(0, "utf8")); const checks = [["workflowName", "OpenClaw NPM Release"], ["event", "workflow_dispatch"], ["conclusion", "success"]]; for (const [key, expected] of checks) { if (run[key] !== expected) { console.error(`Referenced npm preflight run ${process.env.PREFLIGHT_RUN_ID} must have ${key}=${expected}, got ${run[key] ?? "<missing>"}.`); process.exit(1); } } console.log(`Using npm preflight run ${process.env.PREFLIGHT_RUN_ID} from ${run.headBranch}: ${run.url}`);'

      - name: Verify full release validation run metadata
        if: ${{ inputs.full_release_validation_run_id != '' }}
        env:
          GH_TOKEN: ${{ github.token }}
          FULL_RELEASE_VALIDATION_RUN_ID: ${{ inputs.full_release_validation_run_id }}
        run: |
          set -euo pipefail
          RUN_JSON="$(gh run view "$FULL_RELEASE_VALIDATION_RUN_ID" --repo "$GITHUB_REPOSITORY" --json workflowName,headBranch,event,status,conclusion,url)"
          printf '%s' "$RUN_JSON" | node -e 'const fs = require("node:fs"); const run = JSON.parse(fs.readFileSync(0, "utf8")); const checks = [["workflowName", "Full Release Validation"], ["event", "workflow_dispatch"], ["status", "completed"], ["conclusion", "success"]]; for (const [key, expected] of checks) { if (run[key] !== expected) { console.error(`Referenced full release validation run ${process.env.FULL_RELEASE_VALIDATION_RUN_ID} must have ${key}=${expected}, got ${run[key] ?? "<missing>"}.`); process.exit(1); } } console.log(`Using full release validation run ${process.env.FULL_RELEASE_VALIDATION_RUN_ID} from ${run.headBranch}: ${run.url}`);'

      - name: Download prepared npm tarball
        env:
          GH_TOKEN: ${{ github.token }}
          PREFLIGHT_RUN_ID: ${{ inputs.preflight_run_id }}
          RELEASE_TAG: ${{ inputs.tag }}
        run: |
          set -euo pipefail

          download_preflight_artifact() {
            local preferred_name fallback_name
            preferred_name="openclaw-npm-preflight-${RELEASE_TAG}"
            rm -rf preflight-tarball
            mkdir -p preflight-tarball

            download_named_artifact() {
              local artifact_name="$1"
              for attempt in 1 2 3; do
                if gh run download "${PREFLIGHT_RUN_ID}" \
                  --repo "${GITHUB_REPOSITORY}" \
                  --name "${artifact_name}" \
                  --dir preflight-tarball; then
                  return 0
                fi
                if [[ "$attempt" != "3" ]]; then
                  echo "::warning::Artifact download for ${artifact_name} failed on attempt ${attempt}; retrying."
                  sleep $((attempt * 10))
                fi
              done
              return 1
            }

            if download_named_artifact "${preferred_name}"; then
              echo "Downloaded ${preferred_name}."
              return 0
            fi

            echo "::warning::${preferred_name} not found; checking run artifacts for a single compatible preflight artifact."
            mapfile -t matches < <(gh api -X GET "repos/${GITHUB_REPOSITORY}/actions/runs/${PREFLIGHT_RUN_ID}/artifacts" \
              --jq '.artifacts[] | select(.expired != true) | .name' |
              grep '^openclaw-npm-preflight-' || true)
            if [[ "${#matches[@]}" != "1" ]]; then
              echo "Expected ${preferred_name}, or exactly one openclaw-npm-preflight-* fallback artifact in run ${PREFLIGHT_RUN_ID}." >&2
              printf 'Available preflight candidates:\n' >&2
              printf -- '- %s\n' "${matches[@]:-<none>}" >&2
              exit 1
            fi
            fallback_name="${matches[0]}"
            download_named_artifact "${fallback_name}"
            echo "Downloaded fallback preflight artifact ${fallback_name}."
          }

          download_preflight_artifact

      - name: Download full release validation manifest
        if: ${{ inputs.full_release_validation_run_id != '' }}
        uses: actions/download-artifact@v8
        with:
          name: full-release-validation-${{ inputs.full_release_validation_run_id }}
          path: full-release-validation
          repository: ${{ github.repository }}
          run-id: ${{ inputs.full_release_validation_run_id }}
          github-token: ${{ github.token }}

      - name: Validate release tag and package metadata
        if: ${{ inputs.preflight_run_id == '' }}
        env:
          OPENCLAW_NPM_RELEASE_SKIP_PACK_CHECK: "1"
          RELEASE_TAG: ${{ inputs.tag }}
          WORKFLOW_REF_NAME: ${{ github.ref_name }}
        run: |
          set -euo pipefail
          RELEASE_SHA=$(git rev-parse HEAD)
          RELEASE_MAIN_REF="refs/remotes/origin/${WORKFLOW_REF_NAME}"
          export RELEASE_SHA RELEASE_TAG RELEASE_MAIN_REF
          # Fetch the workflow branch so merge-base ancestry checks keep working
          # for older tagged commits contained in a release branch.
          git fetch --no-tags origin "+refs/heads/${WORKFLOW_REF_NAME}:refs/remotes/origin/${WORKFLOW_REF_NAME}"
          pnpm release:openclaw:npm:check

      - name: Verify prepared tarball provenance
        env:
          RELEASE_TAG: ${{ inputs.tag }}
          RELEASE_NPM_DIST_TAG: ${{ inputs.npm_dist_tag }}
        run: |
          set -euo pipefail
          EXPECTED_RELEASE_SHA="$(git rev-parse HEAD)"
          MANIFEST_FILE="preflight-tarball/preflight-manifest.json"
          if [[ ! -f "$MANIFEST_FILE" ]]; then
            echo "Prepared preflight metadata is missing." >&2
            ls -la preflight-tarball >&2 || true
            exit 1
          fi
          ARTIFACT_RELEASE_TAG="$(jq -r '.releaseTag // ""' "$MANIFEST_FILE")"
          ARTIFACT_RELEASE_SHA="$(jq -r '.releaseSha // ""' "$MANIFEST_FILE")"
          ARTIFACT_RELEASE_NPM_DIST_TAG="$(jq -r '.npmDistTag // ""' "$MANIFEST_FILE")"
          ARTIFACT_TARBALL_NAME="$(jq -r '.tarballName // ""' "$MANIFEST_FILE")"
          ARTIFACT_TARBALL_SHA256="$(jq -r '.tarballSha256 // ""' "$MANIFEST_FILE")"
          if [[ "$ARTIFACT_RELEASE_TAG" != "$RELEASE_TAG" ]]; then
            echo "Prepared preflight tag mismatch: expected $RELEASE_TAG, got $ARTIFACT_RELEASE_TAG" >&2
            exit 1
          fi
          if [[ "$ARTIFACT_RELEASE_SHA" != "$EXPECTED_RELEASE_SHA" ]]; then
            echo "Prepared preflight SHA mismatch: expected $EXPECTED_RELEASE_SHA, got $ARTIFACT_RELEASE_SHA" >&2
            exit 1
          fi
          if [[ "$ARTIFACT_RELEASE_NPM_DIST_TAG" != "$RELEASE_NPM_DIST_TAG" ]]; then
            echo "Prepared preflight npm dist-tag mismatch: expected $RELEASE_NPM_DIST_TAG, got $ARTIFACT_RELEASE_NPM_DIST_TAG" >&2
            exit 1
          fi
          if [[ -z "$ARTIFACT_TARBALL_NAME" || ! -f "preflight-tarball/$ARTIFACT_TARBALL_NAME" ]]; then
            echo "Prepared preflight tarball named in manifest is missing: $ARTIFACT_TARBALL_NAME" >&2
            exit 1
          fi
          actual_tarball_sha256="$(sha256sum "preflight-tarball/$ARTIFACT_TARBALL_NAME" | awk '{print $1}')"
          if [[ "$actual_tarball_sha256" != "$ARTIFACT_TARBALL_SHA256" ]]; then
            echo "Prepared preflight tarball digest mismatch." >&2
            exit 1
          fi

      - name: Verify full release validation target
        if: ${{ inputs.full_release_validation_run_id != '' }}
        run: |
          set -euo pipefail
          EXPECTED_RELEASE_SHA="$(git rev-parse HEAD)"
          MANIFEST_FILE="full-release-validation/full-release-validation-manifest.json"
          if [[ ! -f "$MANIFEST_FILE" ]]; then
            echo "Full release validation manifest is missing." >&2
            ls -la full-release-validation >&2 || true
            exit 1
          fi
          WORKFLOW_NAME="$(jq -r '.workflowName // ""' "$MANIFEST_FILE")"
          TARGET_SHA="$(jq -r '.targetSha // ""' "$MANIFEST_FILE")"
          RERUN_GROUP="$(jq -r '.rerunGroup // ""' "$MANIFEST_FILE")"
          if [[ "$WORKFLOW_NAME" != "Full Release Validation" ]]; then
            echo "Full release validation manifest workflow mismatch: $WORKFLOW_NAME" >&2
            exit 1
          fi
          if [[ "$TARGET_SHA" != "$EXPECTED_RELEASE_SHA" ]]; then
            echo "Full release validation target SHA mismatch: expected $EXPECTED_RELEASE_SHA, got $TARGET_SHA" >&2
            exit 1
          fi
          if [[ "$RERUN_GROUP" != "all" ]]; then
            echo "Full release validation must run rerun_group=all before npm publish; got $RERUN_GROUP" >&2
            exit 1
          fi

      - name: Resolve publish tarball
        id: publish_tarball
        run: |
          set -euo pipefail
          TARBALL_PATH="$(find preflight-tarball -type f -name '*.tgz' -print | sort | tail -n 1)"
          if [[ -z "$TARBALL_PATH" ]]; then
            echo "Prepared preflight tarball not found." >&2
            ls -la preflight-tarball >&2 || true
            exit 1
          fi
          echo "path=$TARBALL_PATH" >> "$GITHUB_OUTPUT"

      - name: Publish
        env:
          OPENCLAW_PREPACK_PREPARED: "1"
          OPENCLAW_NPM_PUBLISH_TAG: ${{ inputs.npm_dist_tag }}
          PUBLISH_TARBALL_PATH: ${{ steps.publish_tarball.outputs.path }}
        run: |
          set -euo pipefail
          publish_target="${PUBLISH_TARBALL_PATH}"
          if [[ -n "${publish_target}" ]]; then
            publish_target="./${publish_target}"
          fi
          bash scripts/openclaw-npm-publish.sh --publish "${publish_target}"
