diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e962d79..8dba784 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,6 +41,9 @@ jobs: browser="$(command -v google-chrome || command -v chromium || command -v chromium-browser)" test -n "$browser" echo "RETENTION_BROWSER=$browser" >> "$GITHUB_ENV" + - name: Test release preflight with synthetic inputs + if: matrix.os == 'ubuntu-latest' + run: pnpm check:release-preflight - run: pnpm check env: RETENTION_CANDIDATE_SHA: ${{ github.event.pull_request.head.sha || github.sha }} diff --git a/.github/workflows/release-audit.yml b/.github/workflows/release-audit.yml new file mode 100644 index 0000000..74f7ac2 --- /dev/null +++ b/.github/workflows/release-audit.yml @@ -0,0 +1,75 @@ +name: Release candidate audit + +on: + pull_request: + paths: + - CHANGELOG.md + - package.json + - pnpm-lock.yaml + - .github/workflows/release-audit.yml + - .github/workflows/release.yml + - scripts/release-preflight.sh + - scripts/stage-release.sh + - tests/release-preflight.sh + push: + branches: [main] + paths: + - CHANGELOG.md + - package.json + - pnpm-lock.yaml + - .github/workflows/release-audit.yml + - .github/workflows/release.yml + - scripts/release-preflight.sh + - scripts/stage-release.sh + - tests/release-preflight.sh + workflow_dispatch: + +permissions: + contents: read + +jobs: + audit: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + persist-credentials: false + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - uses: pnpm/action-setup@v6 + - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 + with: + node-version: 24.22.3 + package-manager-cache: false + - name: Check public registry version + run: | + set -euo pipefail + git rev-parse HEAD + version="$(node -p 'require("./package.json").version')" + status="$(curl --silent --show-error --max-time 30 --retry 2 \ + --output "$RUNNER_TEMP/retention-packument.json" --write-out '%{http_code}' \ + --header 'Accept: application/vnd.npm.install-v1+json' \ + 'https://registry.npmjs.org/@0xbuff%2fretention')" + echo "Public registry HTTP status: $status" + case "$status" in + 404) + echo "No publicly visible package metadata. Scope ownership and staged/private versions need authenticated owner verification." + ;; + 200) + jq -e '.name == "@0xbuff/retention" and (.versions | type == "object")' "$RUNNER_TEMP/retention-packument.json" >/dev/null + if jq -e --arg v "$version" '.versions | has($v)' "$RUNNER_TEMP/retention-packument.json" >/dev/null; then + echo "::error::Candidate version $version is already public" + exit 1 + fi + echo "Candidate version $version is not present in the public version list." + ;; + *) + echo "::error::Registry status is inconclusive" + exit 1 + ;; + esac + - name: Audit locked development dependencies + if: always() + run: | + set -euo pipefail + pnpm audit --json --registry=https://registry.npmjs.org/ diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..844931e --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,48 @@ +name: Stage npm package + +on: + workflow_dispatch: + inputs: + quality_run_id: + description: Successful main quality run ID + required: true + type: string + +permissions: {} + +concurrency: + group: retention-npm-stage + cancel-in-progress: false + +jobs: + stage: + if: github.repository == '0xbuffdev/retention' && github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + timeout-minutes: 10 + environment: npm-stage + permissions: + contents: read + actions: read + id-token: write + steps: + - name: Check out exact staging commit + uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 + with: + ref: ${{ github.sha }} + persist-credentials: false + + - name: Set up Node.js + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 + with: + node-version: 24.22.3 + package-manager-cache: false + + - name: Verify and stage exact approved archive + env: + GH_TOKEN: ${{ github.token }} + RUN_ID: ${{ inputs.quality_run_id }} + APPROVED_RUN_ID: ${{ vars.RETENTION_STAGE_RUN_ID }} + APPROVED_SHA: ${{ vars.RETENTION_STAGE_COMMIT }} + APPROVED_DIGEST: ${{ vars.RETENTION_STAGE_SHA256 }} + NPM_CONFIG_REGISTRY: https://registry.npmjs.org/ + run: bash scripts/stage-release.sh diff --git a/AGENTS.md b/AGENTS.md index e5021bd..c0923f9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -62,5 +62,5 @@ Use `pnpm exec vitest run ` for focused tests. Run `pnpm check:archit - Prioritize nondeterminism, incorrect time arithmetic, invalid lifecycle acceptance, deleted-history resurrection, practice-to-credit leakage, policy/prefix mismatch, resource-bound bypasses, unsafe diagnostics, dependency violations and weakened gates. Include file/line, triggering condition, impact and a testable correction. - PRs must identify the ticket where applicable, requirement coverage, changes, tests and outstanding limitations. Do not mark tickets complete without required review, merge and acceptance evidence. -- Obtain maintainer approval for each publication. Preserve Apache-2.0 and copyright notices, and verify source and contributor rights. Follow [release policy](docs/releasing.md): check the exact ESM/types archive, explicit archive/export allowlists and independent offline Node/TypeScript and browser consumers. Host integration requires separate acceptance. +- Obtain maintainer approval for each publication. Preserve Apache-2.0 and copyright notices, and verify source and contributor rights. Follow [release policy](docs/releasing.md): check the exact ESM/types archive, explicit archive/export allowlists and independent offline Node/TypeScript and browser consumers. Host integration requires separate acceptance. Release staging uses the protected manual OIDC workflow; keep it stage-only, validate the approved main commit and exact CI archive, and require interactive npm approval. Never store npm publishing tokens in CI. - No merge, tag, publication, deployment, maintainer signing, relicensing or repository ownership/visibility change without explicit maintainer authorization. diff --git a/CHANGELOG.md b/CHANGELOG.md index 83dec50..cf74227 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,19 @@ Notable changes to Retention are recorded here. The format follows [Keep a Chang ## [Unreleased] +## [0.0.2] - 2026-11-29 + +### Changed + +- Updated public documentation, installation guidance, event reference and architecture diagrams. +- Made packed-package verification use the version in the package manifest. +- Added protected, stage-only npm trusted publishing with exact CI artifact checks and synthetic preflight tests. +- Added a read-only release audit for registry version availability and locked development dependencies. + +### Fixed + +- Retry temporary browser profile removal on transient filesystem errors during package verification. + ## [0.0.1] - 2026-11-29 ### Added diff --git a/docs/development.md b/docs/development.md index 9488eda..9f41705 100644 --- a/docs/development.md +++ b/docs/development.md @@ -29,4 +29,4 @@ The source build targets ES2022 without Node ambient types or DOM APIs. Test and The verifier checks the archive inventory and digest, installs the package offline in a temporary project, compiles a TypeScript consumer and the README quick start, and runs both against the installed package. The Ubuntu CI job also exercises the installed library in Chromium. Consult the release evidence for the tested browser version; older Chromium versions, Firefox and Safari have not been verified by these checks. -Run `pnpm verify:package` after building to verify the exact tarball and independent offline Node/TypeScript consumer. Set `RETENTION_BROWSER` to a Chromium executable on Linux to exercise the browser consumer too. See the [release policy](releasing.md) for release verification and [contributing](../CONTRIBUTING.md) for proposed changes. +Run `pnpm verify:package` after building to verify the exact tarball and independent offline Node/TypeScript consumer. Set `RETENTION_BROWSER` to a Chromium executable on Linux to exercise the browser consumer too. On Ubuntu, run `pnpm check:release-preflight` to exercise release approval and staging checks with synthetic archives and stub commands. The Ubuntu CI job runs these tests. See the [release policy](releasing.md) for release verification and [contributing](../CONTRIBUTING.md) for proposed changes. diff --git a/docs/releasing.md b/docs/releasing.md index 950b552..cf2b3ac 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -1,6 +1,6 @@ # Releasing Retention -Retention is published as [`@0xbuff/retention`](https://www.npmjs.com/package/@0xbuff/retention). Releases use Semantic Versioning. Pin tested versions before 1.0.0. Maintainers publish approved archives through npm; CI does not publish automatically. +Retention is published as [`@0xbuff/retention`](https://www.npmjs.com/package/@0xbuff/retention). Releases use Semantic Versioning. Pin tested versions before 1.0.0. A protected manual workflow stages the approved archive using npm trusted publishing. A maintainer approves publication separately. ## Prepare a release @@ -12,6 +12,8 @@ pnpm install --frozen-lockfile pnpm check ``` +On Ubuntu, run `pnpm check:release-preflight` with Bash, jq, OpenSSL and GNU tar/coreutils. The tests use synthetic archives and stubbed GitHub and npm commands; they do not contact npm or stage a package. + ## Verify the candidate The package check packs the built files with npm, checks the exact archive allowlist and integrity, and installs the tarball offline outside the checkout. It compiles and executes a TypeScript consumer and the README quick start against the installed root export. It rejects unexpected files, runtime dependencies, install hooks and external module imports. @@ -28,13 +30,23 @@ Compare the source SHA, archive SHA-256, npm SHA-512 integrity, byte count, file ## Release approval -Before accepting a candidate, review the changed behavior, applicable acceptance tests, exact source, package contents and security. Record verified runtime versions and limitations. Confirm ownership and contributor rights, license and notices, registry scope access and version availability. Obtain explicit maintainer approval for the source commit, successful CI run and verified archive digest before publishing. CI never publishes, tags or signs a release. +Before accepting a candidate, review the changed behavior, applicable acceptance tests, exact source, package contents and security. Record verified runtime versions and limitations. Confirm ownership and contributor rights, license and notices, registry scope access and version availability. Run the read-only Release candidate audit workflow and review the registry version check and locked dependency audit. Confirm the version is not already staged or published using the authenticated npm account. Obtain explicit maintainer approval for the current main commit, successful main quality run and verified archive digest. CI does not directly publish, tag or sign a release. Applications are responsible for testing admission, authorization, grade invariance, deletion, delivery and stale-generation rejection in their own integrations. -## Publish and verify +## Stage with trusted publishing + +The Stage npm package workflow runs manually on main. It uses the protected `npm-stage` environment and GitHub Actions OIDC. It verifies the successful main quality run, downloads the exact Ubuntu archive, checks the evidence, browser result, package metadata and archive digest, then rechecks main and the digest immediately before staging. It does not build or directly publish. + +1. Configure the GitHub environment `npm-stage` for main only, require reviewers and disable administrator bypass. Prevent self-review when a separate reviewer is available. +2. Configure [npm trusted publishing](https://docs.npmjs.com/trusted-publishers/) for `0xbuffdev/retention`, workflow `release.yml`, environment `npm-stage`. Allow staging only; disable direct publishing and dist-tag management. Do not store an npm token in CI. +3. Set environment variables `RETENTION_STAGE_COMMIT`, `RETENTION_STAGE_RUN_ID` and `RETENTION_STAGE_SHA256` to the approved main commit, quality run ID and archive SHA-256. Both CI archives must match. If an attempt lacks either archive after a partial rerun, rerun all quality jobs before approving. +4. Dispatch the workflow with the approved run ID and approve the environment deployment. Inspect the staged version and compare its tarball digest to the approved archive. +5. With explicit publication approval, approve the staged version through npm using the required interactive authentication. Clear the approval variables when finished. + +## Verify publication -Publish the approved tarball through an authenticated npm account with public access and scripts disabled. If npm stages the package, inspect the staged version and approve it with the required authentication. Do not rebuild or republish while approval is pending. After publication, compare `npm view @0xbuff/retention@ dist.integrity` with the candidate evidence, then install the exact version in a temporary consumer outside the checkout and import the root API. Keep the verified source commit, artifact digest and registry integrity in the release record. Tag the source of the published artifact, not a subsequent documentation-only commit. +Download the public registry tarball and compare its SHA-256 with the approved archive. Check the registry version, dist-tag, integrity, signatures and available build provenance. Install the exact version with scripts disabled in a temporary ESM project outside the checkout and import the root API. Run the quick start against the installed package. Record the verified source commit, artifact digest, registry integrity and consumer results. Create the authorized tag and GitHub release for that source commit, not a later documentation change. ## Correct a published release diff --git a/package.json b/package.json index b3b90b2..8cca80e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@0xbuff/retention", - "version": "0.0.1", + "version": "0.0.2", "description": "Deterministic review scheduling for learning and long-term retention", "private": false, "license": "Apache-2.0", @@ -40,7 +40,8 @@ "check:architecture": "node scripts/check-architecture.mjs", "verify:dist": "node scripts/verify-dist.mjs", "check": "pnpm check:architecture && pnpm typecheck && pnpm check:style && pnpm test && pnpm build && pnpm verify:package", - "verify:package": "node scripts/verify-package.mjs" + "verify:package": "node scripts/verify-package.mjs", + "check:release-preflight": "bash tests/release-preflight.sh" }, "devDependencies": { "@biomejs/biome": "2.6.8", diff --git a/scripts/release-preflight.sh b/scripts/release-preflight.sh new file mode 100644 index 0000000..7d1d64a --- /dev/null +++ b/scripts/release-preflight.sh @@ -0,0 +1,102 @@ +#!/usr/bin/env bash +# Read-only candidate validation; no credentials, network, or npm operations. +set -euo pipefail + +fail() { + printf 'Release preflight: %s\n' "$1" >&2 + exit 1 +} + +approval() { + [[ "${GITHUB_REPOSITORY:-}" == "0xbuffdev/retention" ]] || fail repository + [[ "${GITHUB_REF:-}" == "refs/heads/main" ]] || fail branch + [[ "${RUN_ID:-}" =~ ^[1-9][0-9]*$ ]] || fail run-id + [[ "${APPROVED_RUN_ID:-}" == "$RUN_ID" ]] || fail approved-run-id + [[ "${APPROVED_SHA:-}" =~ ^[a-f0-9]{40}$ && "$APPROVED_SHA" == "$GITHUB_SHA" ]] || fail approved-sha + [[ "${APPROVED_DIGEST:-}" =~ ^[a-f0-9]{64}$ ]] || fail approved-digest +} + +archive_files() { + printf '%s\n' CHANGELOG.md COPYRIGHT.md LICENSE README.md \ + dist/index.d.ts dist/index.js package.json +} + +root="$(cd "$(dirname "$0")/.." && pwd)" +version="$(jq -er '.version | strings' "$root/package.json")" || fail package-version +[[ "$version" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] || fail package-version +archive="0xbuff-retention-$version.tgz" + +approval +case "$1" in + filename) + [[ "$#" == 1 ]] || fail arguments + printf '%s\n' "$archive" + ;; + approval) + [[ "$#" == 1 ]] || fail arguments + ;; + run) + [[ "$#" == 3 ]] || fail arguments + [[ "$2" == "$GITHUB_SHA" ]] || fail stale-main + jq -e --arg sha "$GITHUB_SHA" --arg repo "$GITHUB_REPOSITORY" --arg id "$RUN_ID" ' + (.id | tostring) == $id and + .head_sha == $sha and .head_branch == "main" and + .head_repository.full_name == $repo and .repository.full_name == $repo and + .path == ".github/workflows/ci.yml" and .event == "push" and + .status == "completed" and .conclusion == "success" and + (.run_attempt | type == "number" and . >= 1 and floor == .) + ' "$3" >/dev/null || fail quality-run + attempt="$(jq -er '.run_attempt' "$3")" + [[ "$attempt" =~ ^[1-9][0-9]*$ ]] || fail run-attempt + printf '%s\n' "$attempt" + ;; + archive) + [[ "$#" == 4 ]] || fail arguments + [[ "$3" =~ ^[1-9][0-9]*$ ]] || fail run-attempt + [[ "$4" == "retention-candidate-ubuntu-latest-$GITHUB_SHA-$3" ]] || fail artifact-name + cd "$2" + [[ -f "$archive" && ! -L "$archive" && -f evidence.json && ! -L evidence.json ]] || fail artifact-files + [[ "$(find . -mindepth 1 -maxdepth 1 | wc -l)" -eq 2 ]] || fail artifact-files + [[ "$(sha256sum "$archive" | cut -d ' ' -f 1)" == "$APPROVED_DIGEST" ]] || fail archive-digest + integrity="sha512-$(openssl dgst -sha512 -binary "$archive" | openssl base64 -A)" + jq -e --arg digest "$APPROVED_DIGEST" --argjson size "$(wc -c < "$archive")" \ + --arg commit "$GITHUB_SHA" --arg integrity "$integrity" --arg version "$version" --arg archive "$archive" ' + .name == "@0xbuff/retention" and .version == $version and + .commit == $commit and .platform == "linux" and + (.node | type == "string" and test("^v24\\.")) and + (.browser | type == "string" and length > 0) and + (.checks | sort) == ["archive","browser","node","offline-install","typescript"] and + .filename == $archive and .integrity == $integrity and + .sha256 == $digest and .bytes == $size and + (.files | sort) == [ + "CHANGELOG.md", "COPYRIGHT.md", "LICENSE", "README.md", + "dist/index.d.ts", "dist/index.js", "package.json" + ] + ' evidence.json >/dev/null || fail archive-manifest + expected="$(archive_files | sed 's|^|package/|')" + actual="$(tar -tzf "$archive" | LC_ALL=C sort)" + [[ "$actual" == "$expected" ]] || fail archive-contents + # Require regular files only; no symlink, hardlink or directory entries. + tar -tvzf "$archive" | awk 'substr($0,1,1) != "-" { bad=1 } END { exit bad }' || fail archive-types + tar -xOzf "$archive" package/package.json | jq -e --arg version "$version" ' + .name == "@0xbuff/retention" and .version == $version and + .private == false and .license == "Apache-2.0" and .type == "module" and + .publishConfig.access == "public" and + .publishConfig.registry == "https://registry.npmjs.org/" and + .engines.node == ">=24" and + .exports == {".":{types:"./dist/index.d.ts",import:"./dist/index.js"}} and + ((.scripts // {}) | keys | all(. as $s | ["preinstall","install","postinstall","prepare","prepack","postpack","prepublish","prepublishOnly","publish","postpublish"] | index($s) | not)) and + ([.dependencies, .optionalDependencies, .peerDependencies, + .bundledDependencies, .bundleDependencies] | all(. == null or length == 0)) + ' >/dev/null || fail package-metadata + notes="$(tar -xOzf "$archive" package/CHANGELOG.md)" + heading="$(awk -v version="$version" 'index($0, "## [" version "]") == 1' <<< "$notes")" + prefix="## [$version] - " + [[ "$heading" == "$prefix"* ]] || fail dated-notes + release_date="${heading#"$prefix"}" + [[ "$release_date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] || fail dated-notes + [[ "$(date -u -d "$release_date" +%F)" == "$release_date" ]] || fail dated-notes + printf '%s\n' "Release preflight passed" + ;; + *) fail mode ;; +esac diff --git a/scripts/stage-release.sh b/scripts/stage-release.sh new file mode 100644 index 0000000..d8447d1 --- /dev/null +++ b/scripts/stage-release.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# Invoked only by the separately authorized protected manual workflow. +set -euo pipefail +root="$(cd "$(dirname "$0")/.." && pwd)" +preflight="$root/scripts/release-preflight.sh" + +archive="$(bash "$preflight" filename)" +workspace="$(mktemp -d "$RUNNER_TEMP/retention-release.XXXXXXXX")" +trap 'rm -rf "$workspace"' EXIT +archive_dir="$workspace/artifact" +main_sha="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/main" --jq '.object.sha')" +gh api "repos/$GITHUB_REPOSITORY/actions/runs/$RUN_ID" > "$workspace/run.json" +attempt="$(bash "$preflight" run "$main_sha" "$workspace/run.json")" +artifact_name="retention-candidate-ubuntu-latest-$GITHUB_SHA-$attempt" +gh run download "$RUN_ID" --repo "$GITHUB_REPOSITORY" \ + --name "$artifact_name" --dir "$archive_dir" +bash "$preflight" archive "$archive_dir" "$attempt" "$artifact_name" + +cd "$archive_dir" +unset NODE_AUTH_TOKEN NPM_TOKEN +node --version +npm --version +# Keep both rechecks immediately before the single staging invocation. +[[ "$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/main" --jq '.object.sha')" == "$GITHUB_SHA" ]] +[[ -f "$archive" && ! -L "$archive" ]] +[[ "$(sha256sum "$archive" | cut -d ' ' -f 1)" == "$APPROVED_DIGEST" ]] +npm stage publish "./$archive" --access public --tag latest --ignore-scripts --registry https://registry.npmjs.org/ diff --git a/scripts/verify-package.mjs b/scripts/verify-package.mjs index 3c7a9a4..f17e121 100644 --- a/scripts/verify-package.mjs +++ b/scripts/verify-package.mjs @@ -45,7 +45,7 @@ function run(args, cwd) { const manifest = JSON.parse(await readFile(path.join(root, "package.json"), "utf8")); assert.equal(manifest.name, "@0xbuff/retention"); assert.equal(manifest.private, false); -assert.equal(manifest.version, "0.0.1"); +assert.match(manifest.version, /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/); assert.equal(manifest.license, "Apache-2.0"); assert.equal(manifest.type, "module"); assert.deepEqual(manifest.publishConfig, { @@ -393,5 +393,5 @@ try { ); } } finally { - await rm(workspace, { recursive: true, force: true }); + await rm(workspace, { recursive: true, force: true, maxRetries: 5, retryDelay: 200 }); } diff --git a/tests/release-preflight.sh b/tests/release-preflight.sh new file mode 100644 index 0000000..e083e6b --- /dev/null +++ b/tests/release-preflight.sh @@ -0,0 +1,258 @@ +#!/usr/bin/env bash +# Synthetic candidate only. Run with Bash, jq and GNU tar/coreutils on Ubuntu. +set -euo pipefail +if [[ "$(uname -s)" != Linux ]]; then + echo "Release preflight regression requires Ubuntu/Linux with GNU utilities" >&2 + exit 1 +fi +root="$(cd "$(dirname "$0")/.." && pwd)" +preflight="$root/scripts/release-preflight.sh" +version="$(jq -er '.version | strings' "$root/package.json")" +archive="0xbuff-retention-$version.tgz" +workspace="$(mktemp -d)" +trap 'rm -rf "$workspace"' EXIT + +export GITHUB_REPOSITORY=0xbuffdev/retention GITHUB_REF=refs/heads/main +export GITHUB_SHA=0123456789abcdef0123456789abcdef01234567 +export APPROVED_SHA="$GITHUB_SHA" RUN_ID=42 APPROVED_RUN_ID=42 +artifact_name="retention-candidate-ubuntu-latest-$GITHUB_SHA-2" +mkdir -p "$workspace/package/dist" "$workspace/artifact" +cd "$workspace" +for file in CHANGELOG.md COPYRIGHT.md LICENSE README.md dist/index.d.ts dist/index.js package.json; do + printf 'synthetic\n' > "package/$file" +done +printf '# Changelog\n\n## [%s] - 2026-11-29\n' "$version" > package/CHANGELOG.md +jq -n --arg version "$version" '{name:"@0xbuff/retention",version:$version,private:false,license:"Apache-2.0",type:"module",engines:{node:">=24"},exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.js"}},publishConfig:{access:"public",registry:"https://registry.npmjs.org/"}}' > package/package.json +cp package/package.json metadata-good.json +cp package/CHANGELOG.md notes-good.md + +jq -n --arg sha "$GITHUB_SHA" --arg repo "$GITHUB_REPOSITORY" '{ + id:42, head_sha:$sha, head_branch:"main", + head_repository:{full_name:$repo}, repository:{full_name:$repo}, + path:".github/workflows/ci.yml", event:"push", + status:"completed", conclusion:"success", run_attempt:2 +}' > run-good.json + +pack() { + tar -czf "artifact/$archive" package/CHANGELOG.md package/COPYRIGHT.md \ + package/LICENSE package/README.md package/dist/index.d.ts package/dist/index.js \ + package/package.json "$@" + export APPROVED_DIGEST + APPROVED_DIGEST="$(sha256sum "artifact/$archive" | cut -d ' ' -f 1)" + local integrity="sha512-$(openssl dgst -sha512 -binary "artifact/$archive" | openssl base64 -A)" + jq -n --arg digest "$APPROVED_DIGEST" --argjson bytes "$(wc -c < "artifact/$archive")" \ + --arg commit "$GITHUB_SHA" --arg integrity "$integrity" --arg version "$version" --arg archive "$archive" '{ + name:"@0xbuff/retention", version:$version, sha256:$digest, bytes:$bytes, + commit:$commit, integrity:$integrity, platform:"linux", filename:$archive, + node:"v24.22.3", browser:"Google Chrome synthetic", + checks:["archive","offline-install","typescript","node","browser"], + files: ["CHANGELOG.md","COPYRIGHT.md","LICENSE","README.md","dist/index.d.ts", + "dist/index.js","package.json"] + }' > artifact/evidence.json +} + +reject() { + local reason="$1" output + shift + if output="$("$@" 2>&1)"; then + printf 'Unexpected preflight acceptance: %s\n' "$reason" >&2 + exit 1 + fi + if ! grep -Fq "Release preflight: $reason" <<< "$output"; then + printf 'Wrong preflight rejection for %s: %s\n' "$reason" "$output" >&2 + exit 1 + fi +} + +pack +[[ "$(bash "$preflight" filename)" == "$archive" ]] +[[ "$(bash "$preflight" run "$GITHUB_SHA" run-good.json)" == 2 ]] +bash "$preflight" archive artifact 2 "$artifact_name" + +reject repository env GITHUB_REPOSITORY=other/retention bash "$preflight" run "$GITHUB_SHA" run-good.json +reject branch env GITHUB_REF=refs/heads/other bash "$preflight" run "$GITHUB_SHA" run-good.json +reject run-id env RUN_ID=0 bash "$preflight" run "$GITHUB_SHA" run-good.json +reject approved-run-id env APPROVED_RUN_ID= bash "$preflight" run "$GITHUB_SHA" run-good.json +reject approved-sha env APPROVED_SHA= bash "$preflight" run "$GITHUB_SHA" run-good.json +reject approved-digest env APPROVED_DIGEST= bash "$preflight" run "$GITHUB_SHA" run-good.json +reject stale-main bash "$preflight" run 1111111111111111111111111111111111111111 run-good.json +for mutation in \ + '.id=43' '.head_sha="wrong"' '.head_branch="other"' \ + '.head_repository.full_name="other/retention"' '.repository.full_name="other/retention"' \ + '.path=".github/workflows/other.yml"' '.event="pull_request"' \ + '.status="in_progress"' '.conclusion="failure"' '.conclusion=null' \ + '.run_attempt=0' '.run_attempt=1.5' '.run_attempt="2"'; do + jq "$mutation" run-good.json > run-bad.json + reject quality-run bash "$preflight" run "$GITHUB_SHA" run-bad.json +done +reject artifact-name bash "$preflight" archive artifact 3 "$artifact_name" +reject run-attempt bash "$preflight" archive artifact 0 "$artifact_name" +reject archive-digest env APPROVED_DIGEST=1111111111111111111111111111111111111111111111111111111111111111 \ + bash "$preflight" archive artifact 2 "$artifact_name" + +cp artifact/evidence.json manifest-good.json +for mutation in \ + '.bytes+=1' '.sha256="wrong"' '.name="other"' '.version="0.0.0-invalid"' \ + '.commit="wrong"' '.filename="wrong.tgz"' '.platform="win32"' '.integrity="wrong"' \ + '.browser=null' '.browser=""' '.node="v20.0.0"' '.checks|=map(select(. != "browser"))' \ + '.files|=.[1:]' '.files += ["extra"]'; do + jq "$mutation" manifest-good.json > artifact/evidence.json + reject archive-manifest bash "$preflight" archive artifact 2 "$artifact_name" +done +cp manifest-good.json artifact/evidence.json +mv "artifact/$archive" artifact/wrong.tgz +reject artifact-files bash "$preflight" archive artifact 2 "$artifact_name" +mv artifact/wrong.tgz "artifact/$archive" +touch artifact/extra +reject artifact-files bash "$preflight" archive artifact 2 "$artifact_name" +rm artifact/extra +mv artifact/evidence.json manifest-moved.json +reject artifact-files bash "$preflight" archive artifact 2 "$artifact_name" +ln -s ../manifest-moved.json artifact/evidence.json +reject artifact-files bash "$preflight" archive artifact 2 "$artifact_name" +rm artifact/evidence.json +mv manifest-moved.json artifact/evidence.json + +# The manifest may claim the right files even when the tarball differs. +touch package/extra +pack package/extra +reject archive-contents bash "$preflight" archive artifact 2 "$artifact_name" +rm package/extra +pack package/README.md +reject archive-contents bash "$preflight" archive artifact 2 "$artifact_name" +rm package/README.md +ln -s LICENSE package/README.md +pack +reject archive-types bash "$preflight" archive artifact 2 "$artifact_name" +rm package/README.md +printf 'synthetic\n' > package/README.md + +for mutation in \ + '.name="other"' '.version="0.0.0-invalid"' '.private=true' \ + '.license="MIT"' '.type="commonjs"' '.dependencies={foo:"1.0.0"}' \ + '.engines.node=">=20"' '.exports={}' '.scripts.postinstall="echo bad"' \ + '.publishConfig.access="restricted"' '.publishConfig.registry="https://example.invalid/"'; do + jq "$mutation" metadata-good.json > package/package.json + pack + reject package-metadata bash "$preflight" archive artifact 2 "$artifact_name" +done +cp metadata-good.json package/package.json + +for heading in '## [Unreleased]' '## [0.0.0-invalid] - 2026-11-29' "## [$version] - 2026-02-30" "## [$version] - 2026-11-99" "$(printf '## [%s] - 2026-11-29\n## [%s] - 2026-11-30' "$version" "$version")"; do + printf '%s\n' "$heading" > package/CHANGELOG.md + pack + reject dated-notes bash "$preflight" archive artifact 2 "$artifact_name" +done +cp notes-good.md package/CHANGELOG.md +pack +[[ "$(bash "$preflight" run "$GITHUB_SHA" run-good.json)" == 2 ]] +bash "$preflight" archive artifact 2 "$artifact_name" +printf '%s\n' 'Release preflight positive and negative matrix passed' + +# Exercise the exact staging entry point with an isolated environment and fake +# GitHub/npm commands. Only the stubs can see these synthetic candidate files. +mkdir "$workspace/bin" "$workspace/runner" +cat > "$workspace/bin/gh" <<'STUB' +#!/usr/bin/env bash +set -euo pipefail +if [[ "$*" == "api repos/0xbuffdev/retention/git/ref/heads/main --jq .object.sha" ]]; then + if grep -qx main "$TRACE"; then + echo final-main >> "$TRACE" + if [[ "$CASE" == stale-final ]]; then + echo 1111111111111111111111111111111111111111 + exit 0 + fi + if [[ "$CASE" == changed-final ]]; then + printf 'changed after validation\n' >> "$(cat "$TRACE.archive")/$archive" + fi + else + echo main >> "$TRACE" + if [[ "$CASE" == stale-initial ]]; then + echo 1111111111111111111111111111111111111111 + exit 0 + fi + fi + echo "$GITHUB_SHA" +elif [[ "$*" == "api repos/0xbuffdev/retention/actions/runs/42" ]]; then + echo run >> "$TRACE" + if [[ "$CASE" == rejected-run ]]; then + jq '.conclusion="failure"' "$FIXTURE/run-good.json" + else + cat "$FIXTURE/run-good.json" + fi +elif [[ "$#" == 9 && "$1 $2 $3 $4 $5 $6 $7 $8" == \ + "run download 42 --repo 0xbuffdev/retention --name retention-candidate-ubuntu-latest-$GITHUB_SHA-2 --dir" ]]; then + echo download >> "$TRACE" + [[ "$CASE" != failed-download ]] || exit 7 + [[ "$9" == "$RUNNER_TEMP"/retention-release.*/artifact ]] + mkdir "$9" + cp "$FIXTURE/artifact/$archive" "$FIXTURE/artifact/evidence.json" "$9/" + printf '%s\n' "$9" > "$TRACE.archive" + if [[ "$CASE" == rejected-archive ]]; then + jq '.bytes+=1' "$FIXTURE/artifact/evidence.json" > "$9/evidence.json" + fi +else + echo unexpected-gh >> "$TRACE" + exit 99 +fi +STUB +cat > "$workspace/bin/npm" <<'STUB' +#!/usr/bin/env bash +set -euo pipefail +if [[ "$#" == 1 && "$1" == --version ]]; then + echo npm-version >> "$TRACE" + echo synthetic-npm + exit 0 +fi +# Record any attempt before checking its arguments. +echo stage >> "$TRACE" +[[ "$*" == "stage publish ./$archive --access public --tag latest --ignore-scripts --registry https://registry.npmjs.org/" ]] +[[ "$PWD" == "$(cat "$TRACE.archive")" ]] +[[ "$(sha256sum ./$archive | cut -d ' ' -f 1)" == "$APPROVED_DIGEST" ]] +[[ -z "${NODE_AUTH_TOKEN+x}" && -z "${NPM_TOKEN+x}" ]] +STUB +cat > "$workspace/bin/node" <<'STUB' +#!/usr/bin/env bash +set -euo pipefail +[[ "$#" == 1 && "$1" == --version ]] +echo synthetic-node +STUB +chmod +x "$workspace/bin/gh" "$workspace/bin/npm" "$workspace/bin/node" + +orchestrate() { + local scenario="$1" expected="$2" trace="$workspace/trace-$1" status=0 + : > "$trace" + # Start with no inherited credentials (including OIDC). Restrict command + # lookup to the stub directory and standard Ubuntu system binaries. + env -i PATH="$workspace/bin:/usr/bin:/bin" \ + GITHUB_REPOSITORY="$GITHUB_REPOSITORY" GITHUB_REF="$GITHUB_REF" \ + GITHUB_SHA="$GITHUB_SHA" RUN_ID="$RUN_ID" APPROVED_RUN_ID="$APPROVED_RUN_ID" \ + APPROVED_SHA="$APPROVED_SHA" APPROVED_DIGEST="$APPROVED_DIGEST" \ + RUNNER_TEMP="$workspace/runner" FIXTURE="$workspace" TRACE="$trace" CASE="$scenario" archive="$archive" \ + NODE_AUTH_TOKEN=synthetic NPM_TOKEN=synthetic \ + bash "$root/scripts/stage-release.sh" > "$trace.output" 2>&1 || status=$? + if [[ "$scenario" == approved ]]; then + [[ "$status" == 0 ]] || { cat "$trace.output" >&2; exit 1; } + else + [[ "$status" != 0 ]] || { echo "Unexpected staging success: $scenario" >&2; exit 1; } + if grep -qx stage "$trace"; then + echo "Rejected candidate reached staging: $scenario" >&2 + exit 1 + fi + fi + [[ "$(cat "$trace")" == "$expected" ]] || { + printf 'Unexpected orchestration trace for %s:\n' "$scenario" >&2 + cat "$trace" "$trace.output" >&2 + exit 1 + } + [[ -z "$(find "$workspace/runner" -mindepth 1 -maxdepth 1 -print -quit)" ]] +} + +orchestrate approved $'main\nrun\ndownload\nnpm-version\nfinal-main\nstage' +orchestrate stale-initial $'main\nrun' +orchestrate rejected-run $'main\nrun' +orchestrate failed-download $'main\nrun\ndownload' +orchestrate rejected-archive $'main\nrun\ndownload' +orchestrate stale-final $'main\nrun\ndownload\nnpm-version\nfinal-main' +orchestrate changed-final $'main\nrun\ndownload\nnpm-version\nfinal-main' +printf '%s\n' 'Protected staging orchestration passed with synthetic GitHub and npm stubs'