diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1ec0441..69384e6 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -129,6 +129,8 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + pull-requests: write + statuses: write outputs: sha: ${{ steps.commit.outputs.sha }} steps: @@ -146,7 +148,7 @@ jobs: - env: VOCABULARY_PLAN: ${{ needs.discover.outputs.plan }} run: node scripts/vocabulary-maintenance.mjs apply --artifact "$RUNNER_TEMP/vocabulary" - - id: commit + - id: candidate env: GH_TOKEN: ${{ github.token }} run: | @@ -156,12 +158,93 @@ jobs: test -n "$changed" test -z "$(printf '%s\n' "$changed" | grep -Ev '^(data/public-vocabulary.json|src/public-vocabulary.ts)$')" git diff --check - git add -- data/public-vocabulary.json src/public-vocabulary.ts - git -c user.name='github-actions[bot]' -c user.email='41898282+github-actions[bot]@users.noreply.github.com' \ - -c core.hooksPath=/dev/null commit -m 'chore(telemetry): refresh released public vocabulary' + data_blob=$(git hash-object data/public-vocabulary.json) + source_blob=$(git hash-object src/public-vocabulary.ts) + pending_head=$(git ls-remote origin refs/heads/automation/public-vocabulary | cut -f1) + if [[ -n "$pending_head" ]]; then + merged_head=$(gh pr list --repo "$GITHUB_REPOSITORY" --base main --head automation/public-vocabulary \ + --state merged --limit 1 --json headRefOid --jq '.[0].headRefOid // empty') + # A pushed candidate can survive failed PR creation or a closed PR. + # Only a proven merged head may be replaced with different snapshots. + if [[ "$pending_head" != "$merged_head" ]]; then + git fetch --no-tags origin "$pending_head" + pending_data=$(git rev-parse "$pending_head:data/public-vocabulary.json") + pending_source=$(git rev-parse "$pending_head:src/public-vocabulary.ts") + other_paths=$(git diff --name-only "$GITHUB_SHA" "$pending_head" | grep -Ev '^(data/public-vocabulary.json|src/public-vocabulary.ts)$' || true) + if [[ "$pending_data" != "$data_blob" || "$pending_source" != "$source_blob" || -n "$other_paths" ]]; then + echo "Unmerged vocabulary branch has a different candidate; recover or merge it before publishing another payload." >&2 + exit 1 + fi + fi + fi + printf 'data_blob=%s\nsource_blob=%s\n' "$data_blob" "$source_blob" >> "$GITHUB_OUTPUT" + - id: pr + uses: peter-evans/create-pull-request@3ffccacd46053766916f087ce41d627c0b8cf2d1 # v8.0.0 + with: + token: ${{ github.token }} + branch-token: ${{ github.token }} + path: . + base: main + branch: automation/public-vocabulary + add-paths: | + data/public-vocabulary.json + src/public-vocabulary.ts + commit-message: 'chore(telemetry): refresh released public vocabulary' + title: 'chore(telemetry): refresh released public vocabulary' + body: 'Publishes the released public vocabulary validated by this workflow before deployment.' + draft: false + - id: commit + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ steps.pr.outputs.pull-request-number }} + PR_HEAD: ${{ steps.pr.outputs.pull-request-head-sha }} + DATA_BLOB: ${{ steps.candidate.outputs.data_blob }} + SOURCE_BLOB: ${{ steps.candidate.outputs.source_blob }} + run: | + set -euo pipefail + test -n "$PR_NUMBER" + trap 'echo "Publication incomplete; inspect https://github.com/$GITHUB_REPOSITORY/pull/$PR_NUMBER" >&2' ERR + # The action must preserve the exact checked artifact, even if its + # branch preparation observed a newer base. A moved main aborts here. + test "$(git rev-parse "$PR_HEAD:data/public-vocabulary.json")" = "$DATA_BLOB" + test "$(git rev-parse "$PR_HEAD:src/public-vocabulary.ts")" = "$SOURCE_BLOB" + # Keep candidate validation with this publisher; the neutral status + # supplies GitHub's strict up-to-date merge check, including base races. + node scripts/vocabulary-maintenance.mjs admit --target "$GITHUB_SHA" --head "$PR_HEAD" + ready=false + for ((attempt=0; attempt<15; attempt++)); do + state=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '[.head.sha,(.mergeable|tostring)] | @tsv') + IFS=$'\t' read -r head mergeable <<< "$state" + test "$head" = "$PR_HEAD" + if [[ "$mergeable" == true ]]; then ready=true; break; fi + test "$mergeable" = null + sleep 2 + done + if [[ "$ready" != true ]]; then + echo "GitHub has not resolved mergeability; retained the candidate for a later run." >&2 + exit 1 + fi + node scripts/vocabulary-maintenance.mjs guard-main --target "$GITHUB_SHA" + if ! gh pr merge "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --squash --match-head-commit "$PR_HEAD"; then + echo "Merge did not confirm success; checking the PR before continuing." >&2 + fi + receipt=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" --jq '[.merged,.head.sha,.merge_commit_sha,.base.ref,.base.repo.full_name,.head.repo.full_name] | @tsv') + IFS=$'\t' read -r merged head sha base base_repo head_repo <<< "$receipt" + test "$merged" = true + test "$head" = "$PR_HEAD" + test "$base" = main + test "$base_repo" = "$GITHUB_REPOSITORY" + test "$head_repo" = "$GITHUB_REPOSITORY" + test -n "$sha" gh auth setup-git - git -c gc.auto=0 -c maintenance.auto=false push origin HEAD:refs/heads/main - sha="$(git rev-parse HEAD)" + git fetch --no-tags origin main:refs/remotes/origin/main "$sha" + git merge-base --is-ancestor "$sha" origin/main + if [[ "$(git rev-parse "$sha^")" != "$GITHUB_SHA" ]]; then + echo "Main advanced during merge; a fresh run must validate before deployment." >&2 + exit 1 + fi + test "$(git rev-parse "$sha:data/public-vocabulary.json")" = "$DATA_BLOB" + test "$(git rev-parse "$sha:src/public-vocabulary.ts")" = "$SOURCE_BLOB" node scripts/vocabulary-maintenance.mjs guard-main --target "$sha" printf 'sha=%s\n' "$sha" >> "$GITHUB_OUTPUT" diff --git a/.github/workflows/publication-admission.yml b/.github/workflows/publication-admission.yml new file mode 100644 index 0000000..1742b78 --- /dev/null +++ b/.github/workflows/publication-admission.yml @@ -0,0 +1,28 @@ +name: Publication admission gate + +on: + pull_request_target: + branches: [main] + types: [opened, synchronize, reopened, edited, ready_for_review] + +permissions: + contents: read + pull-requests: read + statuses: write + +jobs: + inspect-publication-metadata: + runs-on: ubuntu-latest + steps: + - name: Checkout trusted base code + uses: actions/checkout@v7 + with: + ref: ${{ github.workflow_sha }} + persist-credentials: false + - uses: actions/setup-node@v7 + with: + node-version: 24.22.1 + - name: Admit current pull request metadata + env: + GH_TOKEN: ${{ github.token }} + run: node scripts/vocabulary-maintenance.mjs gate diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c09983..48f9012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Publish checked public vocabulary through pull requests with strict GitHub Actions merge admission, and verify the exact source parent and merged payload before the existing deployment step. - Batch main CodeQL analysis weekly while preserving PR and manual scans. - Accept identifier-free reporting-installation counts into an optional aggregate-only D1 `(day, gap, reports)` histogram with 90-day pruning; successful update answers advertise the server UTC day only when the binding exists, and production stays unbound. Thanks @roboclaw-bot. diff --git a/README.md b/README.md index af17757..d3141c4 100644 --- a/README.md +++ b/README.md @@ -296,24 +296,37 @@ The existing Deploy workflow owns the whole update: then runs the normal checks, complete-vocabulary byte-budget tests, and Wrangler dry-run. 4. A separate job can publish only `data/public-vocabulary.json` and `src/public-vocabulary.ts`. It checks the artifact again without installing dependencies or executing producer-supplied code. - Existing snapshots and aliases cannot change. Publication is a fast-forward from the exact - discovered main commit; concurrent changes stop it instead of overwriting source. + Existing snapshots and aliases cannot change. Publication uses a pull request and a normal + expected-head squash merge. The candidate must have exactly the validated source parent and + change only those two files. A strict GitHub Actions `Publication merge admission` status rule + rejects a merge if main advances after validation. The merged payload, parent, and current main + are verified again before deployment. Unmerged candidates are retained instead of replaced. 5. The existing deployment job checks out the exact checked or published commit. All production runs share one concurrency group, and stale runs fail the current-main guard. The job tags the Worker with its commit and verifies that the newest deployment sends 100% of traffic to that exact tagged version before recording success. It retains the existing trusted npm/Wrangler toolchain; the artifact cannot replace package files, scripts, or workflow code. -Generation never learns names from client requests. The publisher alone receives `contents: write`; -the deployment job alone receives `statuses: write` and the existing `CLOUDFLARE_API_TOKEN`. +Generation never learns names from client requests. The publisher alone receives `contents: write` +and `pull-requests: write`. It and a trusted metadata-only `pull_request_target` gate can write the +neutral admission status; the gate never executes candidate code. The deployment job retains its +deployment status permission and alone receives the existing `CLOUDFLARE_API_TOKEN`. No new credential or cross-repository write is required. A commit made with `GITHUB_TOKEN` does not start the normal push workflow, so its checked deployment happens in the same run. +Before enabling automatic publication, an administrator must activate and verify a default-branch +rule requiring `Publication merge admission` from the GitHub Actions App (ID `15368`), with strict +up-to-date checks and no bypass actors, plus non-fast-forward protection. The publisher verifies +the effective rules before posting its own admission status; it does not depend on a new PR event. +GitHub can redact bypass metadata from its token, so no-bypass configuration is an administrator +admission check. Exposed bypasses or missing protection hold publication and retain the PR. +Existing open PRs without this status need a normal handled PR event after activation. + Merging this workflow into main enables daily generation, data-only publication, and deployment. Review that operational authority before activation. Coverage can lag until the next daily run plus GitHub queue delays. GitHub can disable schedules in inactive public repositories. Changed upstream metadata contracts, moved or deleted tags, incomplete pagination, byte-budget growth, and blocked -fast-forward publication fail visibly and still need maintainer review; automation does not remove +pull-request publication fail visibly and still need maintainer review; automation does not remove those maintenance boundaries. A failed rollout leaves a retryable status even if its source commit was already published. Inspect the failed Deploy run, repair the named contract or permission, then run Deploy manually from current main. Do not weaken the allowlist or force-push past a guard. diff --git a/scripts/vocabulary-maintenance.mjs b/scripts/vocabulary-maintenance.mjs index 361999d..d189aa3 100644 --- a/scripts/vocabulary-maintenance.mjs +++ b/scripts/vocabulary-maintenance.mjs @@ -11,6 +11,8 @@ const FILES = ["data/public-vocabulary.json", "src/public-vocabulary.ts"]; const SHA = /^[a-f0-9]{40}$/u; const NAME = /^[a-z0-9][a-z0-9._/-]{0,63}$/u; const CONTEXT = "telemetry/deploy"; +const ADMISSION = "Publication merge admission"; +const ACTIONS_APP = 15368; const json = (value) => `${JSON.stringify(value, null, 2)}\n`; const equal = (actual, expected, message) => assert.deepEqual(actual, expected, message); const sorted = (values) => [...new Set(values)].sort(); @@ -119,6 +121,56 @@ export function assertCurrentMain(target, current) { equal(immutable(target), immutable(current), "Main advanced; retry from current main instead of rolling back"); } +function currentPullRequest(repository, number, head) { + const pr = gh(`repos/${repository}/pulls/${number}`); + return pr.state === "open" && pr.base.ref === "main" && + pr.base.repo.full_name === repository && pr.head.sha === head ? pr : null; +} + +function admissionStatus(repository, head) { + gh(`repos/${repository}/statuses/${immutable(head)}`, "-f", "state=success", + "-f", `context=${ADMISSION}`, "-f", "description=Pull request metadata admitted"); +} + +function requirePublicationRules(repository) { + const rules = gh(`repos/${repository}/rules/branches/main`); + const configs = new Map(); + const active = (rule) => { + if (!configs.has(rule.ruleset_id)) { + configs.set(rule.ruleset_id, gh(`repos/${repository}/rulesets/${rule.ruleset_id}`)); + } + const config = configs.get(rule.ruleset_id); + // Metadata readers can receive redacted bypasses. An administrator must + // verify no bypass at activation; runtime rejects any exposed exception. + return config.enforcement === "active" && + (!Object.hasOwn(config, "bypass_actors") || + (Array.isArray(config.bypass_actors) && config.bypass_actors.length === 0)) && + (!Object.hasOwn(config, "current_user_can_bypass") || config.current_user_can_bypass === "never"); + }; + assert(rules.some((rule) => rule.type === "non_fast_forward" && active(rule)), + "Publication held: missing active non-fast-forward protection"); + assert(rules.some((rule) => rule.type === "required_status_checks" && + rule.parameters?.strict_required_status_checks_policy === true && + rule.parameters.required_status_checks.some((check) => + check.context === ADMISSION && check.integration_id === ACTIONS_APP) && active(rule)), + "Publication held: missing strict GitHub Actions publication admission rule"); +} + +function admitPublication(repository, number, head, base) { + const pr = currentPullRequest(repository, number, head); + assert(pr?.head.repo?.full_name === repository, "Publication held: candidate PR identity changed"); + equal(command("git", ["rev-list", "--parents", "-n", "1", immutable(head)]).trim().split(" "), + [head, immutable(base)], "Publication held: candidate must have only its validated source parent"); + const changes = command("git", ["diff", "--no-renames", "--name-status", base, head]).trim().split("\n"); + assert(changes.length > 0 && changes.every((line) => FILES.some((path) => line === `M\t${path}`)), + "Publication held: candidate must change only the two owned files without renames"); + requirePublicationRules(repository); + assertCurrentMain(base, gh(`repos/${repository}/git/ref/heads/main`).object.sha); + assert(currentPullRequest(repository, number, head)?.head.repo?.full_name === repository, + "Publication held: candidate PR identity changed"); + admissionStatus(repository, head); +} + /** Runs from trusted base code in check/publish jobs; candidate TypeScript is never imported. */ export function validateCandidate(previous, candidate, generated, releases) { keys(candidate, ["schemaVersion", "legacyAliases", "snapshots", "releases"]); @@ -191,8 +243,26 @@ async function main() { const { values, positionals } = parseArgs({ allowPositionals: true, options: { base: { type: "string" }, event: { type: "string" }, source: { type: "string" }, artifact: { type: "string" }, target: { type: "string" }, + head: { type: "string" }, } }); const [mode] = positionals; + if (mode === "gate") { + const event = JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH, "utf8")); + const repository = process.env.GITHUB_REPOSITORY; + const pr = event.pull_request; + if (event.repository.default_branch === "main" && currentPullRequest(repository, pr.number, pr.head.sha)) { + // This status is commit-wide and neutral. Payload and provenance checks + // belong exclusively to the publisher, not other PRs sharing this SHA. + admissionStatus(repository, pr.head.sha); + } else { + console.log("Obsolete pull request event; no status written"); + } + return; + } + if (mode === "admit") { + admitPublication(process.env.GITHUB_REPOSITORY, process.env.PR_NUMBER, values.head, values.target); + return; + } if (mode === "guard-main") { assertCurrentMain(values.target, gh("repos/openclaw/telemetry/git/ref/heads/main").object.sha); return; @@ -243,7 +313,7 @@ async function main() { await writeFile(join(ROOT, FILES[0]), candidate.metadata); await writeFile(join(ROOT, FILES[1]), candidate.source); } else { - throw new Error("Use plan, generate, apply, guard-main, or verify-deployment"); + throw new Error("Use plan, generate, apply, admit, gate, guard-main, or verify-deployment"); } } diff --git a/test/helpers/vocabulary-publication.mjs b/test/helpers/vocabulary-publication.mjs new file mode 100644 index 0000000..24ace7c --- /dev/null +++ b/test/helpers/vocabulary-publication.mjs @@ -0,0 +1,231 @@ +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; + +export const publicationCases = [ + "success", "pending-same", "pending-different", "orphan-same", "orphan-different", "merged-branch", "stale-before", "stale-after", + "changed-candidate", "mergeable-pending", "mergeable-timeout", "mergeable-conflict", "changed-pr-head", "blocked-merge", "ambiguous-merge", + "concurrent-base", "wrong-head", "wrong-tree", "unpublished-merge", "retargeted-receipt", "foreign-base-receipt", "foreign-head-receipt", + "multi-parent", "unowned-path", "missing-rule", "wrong-app", "non-strict", "missing-nonff", + "inactive-rule", "exposed-bypass", "malformed-bypass", "current-bypass", "redacted-bypass", + "gate-success", "gate-moved", "gate-closed", "gate-retargeted", "gate-wrong-repo", "gate-default", +]; + +// The fixture executes the workflow bodies; only GitHub responses are simulated. +export async function checkPublication(mode) { + const workflow = await readFile(new URL("../../.github/workflows/deploy.yml", import.meta.url), "utf8"); + const step = (id) => { + const match = workflow.match(new RegExp(` - id: ${id}\\n[\\s\\S]*? run: \\|\\n((?: .*\\n|\\n)+)`)); + assert(match, `Missing ${id} workflow step`); + return match[1].replace(/^ /gm, ""); + }; + const root = await mkdtemp(join(tmpdir(), "vocabulary-publication-")); + try { + const checkout = join(root, "checkout"); + const remote = join(root, "remote.git"); + const bin = join(root, "bin"); + for (const path of [checkout, bin, join(checkout, "data"), join(checkout, "src"), join(checkout, "scripts")]) { + await mkdir(path, { recursive: true }); + } + const env = { + ...process.env, PATH: `${bin}:${dirname(process.execPath)}:${process.env.PATH}`, MODE: mode, + GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_NOSYSTEM: "1", GIT_TERMINAL_PROMPT: "0", + GITHUB_REPOSITORY: "openclaw/telemetry", GITHUB_OUTPUT: join(root, "output"), + CALL_LOG: join(root, "calls"), GUARD_COUNT: join(root, "guards"), + MERGE_COUNT: join(root, "merge-count"), MERGED_MARKER: join(root, "merged"), + }; + const git = (...args) => execFileSync("git", args, { + cwd: checkout, env, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], + }).trim(); + const payload = async (value) => { + await writeFile(join(checkout, "data/public-vocabulary.json"), `${value}\n`); + await writeFile(join(checkout, "src/public-vocabulary.ts"), `${value}\n`); + }; + const commit = (message) => { + git("add", "data", "src"); + git("commit", "-qm", message); + return git("rev-parse", "HEAD"); + }; + git("init", "--bare", "--quiet", remote); + git("init", "--quiet", "--initial-branch=main"); + git("config", "user.name", "Fixture"); + git("config", "user.email", "fixture@example.invalid"); + git("config", "commit.gpgsign", "false"); + git("config", "core.hooksPath", "/dev/null"); + await payload("base"); + env.GITHUB_SHA = commit("base"); + git("remote", "add", "origin", remote); + git("push", "origin", "main"); + for (const name of ["vocabulary-maintenance.mjs", "public-vocabulary.mjs"]) { + await writeFile(join(checkout, "scripts", name), await readFile(new URL(`../../scripts/${name}`, import.meta.url))); + } + if (mode.startsWith("pending-") || mode.startsWith("orphan-") || mode === "merged-branch") { + git("checkout", "-b", "automation/public-vocabulary"); + await payload(mode.endsWith("-same") ? "candidate" : "earlier-candidate"); + env.PENDING_HEAD = commit("retained candidate"); + git("push", "origin", "automation/public-vocabulary"); + git("checkout", "main"); + if (mode === "merged-branch") env.MERGED_HEAD = env.PENDING_HEAD; + } + await payload("candidate"); + await writeFile(join(bin, "sleep"), "#!/bin/sh\nexit 0\n", { mode: 0o755 }); + await writeFile(join(bin, "gh"), `#!/usr/bin/env bash +set -euo pipefail +printf '%s\\n' "$*" >> "$CALL_LOG" +case "$1 $2" in + 'pr list') + [[ "$*" == *'--state merged'* ]] + printf '%s' "\u0024{MERGED_HEAD:-}" + ;; + 'api repos/openclaw/telemetry/git/ref/heads/main') + count=0 + [[ ! -f "$GUARD_COUNT" ]] || count=$(cat "$GUARD_COUNT") + echo $((count+1)) > "$GUARD_COUNT" + sha=$(git ls-remote origin refs/heads/main | cut -f1) + if [[ $MODE == stale-before || ( $MODE == stale-after && $count -gt 0 ) ]]; then sha=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; fi + printf '{"object":{"sha":"%s"}}' "$sha" + ;; + 'pr merge') + [[ $MODE != blocked-merge ]] + [[ "$*" == *"--squash --match-head-commit $PR_HEAD" ]] + if [[ $MODE == concurrent-base ]]; then + git commit --allow-empty -m 'concurrent base' >&2 + git push origin main >&2 + echo 'Required status check "Publication merge admission" is expected.' >&2 + exit 1 + fi + git merge --squash "$PR_HEAD" >&2 + [[ $MODE != wrong-tree ]] || echo wrong > data/public-vocabulary.json + git add data src + git commit -m 'squash publication' >&2 + [[ $MODE == unpublished-merge ]] || git push origin main >&2 + touch "$MERGED_MARKER" + [[ $MODE != ambiguous-merge ]] + ;; + 'api repos/openclaw/telemetry/pulls/7') + if [[ "$*" != *'--jq'* ]]; then + state=open; base=main; repository=openclaw/telemetry; head=$PR_HEAD + [[ $MODE != gate-closed ]] || state=closed + [[ $MODE != gate-retargeted ]] || base=other + [[ $MODE != gate-wrong-repo ]] || repository=other/repo + [[ $MODE != gate-moved && $MODE != changed-pr-head ]] || head=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + printf '{"state":"%s","base":{"ref":"%s","repo":{"full_name":"%s"}},"head":{"sha":"%s","repo":{"full_name":"openclaw/telemetry"}}}' "$state" "$base" "$repository" "$head" + exit 0 + fi + if [[ "$*" == *mergeable* ]]; then + count=0 + [[ ! -f "$MERGE_COUNT" ]] || count=$(cat "$MERGE_COUNT") + echo $((count+1)) > "$MERGE_COUNT" + mergeable=true + if [[ $MODE == mergeable-timeout || ( $MODE == mergeable-pending && $count -lt 2 ) ]]; then mergeable=null; fi + [[ $MODE != mergeable-conflict ]] || mergeable=false + head=$PR_HEAD + [[ $MODE != changed-pr-head ]] || head=changed + printf '%s\\t%s\\n' "$head" "$mergeable" + exit 0 + fi + head=$PR_HEAD + [[ $MODE != wrong-head ]] || head=wrong + merged=false + [[ ! -f "$MERGED_MARKER" ]] || merged=true + base=main; base_repo=openclaw/telemetry; head_repo=openclaw/telemetry + [[ $MODE != retargeted-receipt ]] || base=other + [[ $MODE != foreign-base-receipt ]] || base_repo=other/repo + [[ $MODE != foreign-head-receipt ]] || head_repo=other/repo + printf '%s\\t%s\\t%s\\t%s\\t%s\\t%s\\n' "$merged" "$head" "$(git rev-parse HEAD)" "$base" "$base_repo" "$head_repo" + ;; + 'api repos/openclaw/telemetry/rules/branches/main') + if [[ $MODE == missing-rule ]]; then echo '[]'; exit 0; fi + strict=true; app=15368 + [[ $MODE != non-strict ]] || strict=false + [[ $MODE != wrong-app ]] || app=1 + printf '[{"type":"required_status_checks","ruleset_id":2,"parameters":{"strict_required_status_checks_policy":%s,"required_status_checks":[{"context":"Publication merge admission","integration_id":%s}]}}' "$strict" "$app" + [[ $MODE == missing-nonff ]] || printf ',{"type":"non_fast_forward","ruleset_id":1}' + echo ']' + ;; + 'api repos/openclaw/telemetry/rulesets/1'|'api repos/openclaw/telemetry/rulesets/2') + enforcement=active; bypass='[]'; current=never + [[ $MODE != inactive-rule ]] || enforcement=disabled + [[ $MODE != exposed-bypass ]] || bypass='[{"actor_type":"Integration","actor_id":15368}]' + [[ $MODE != malformed-bypass ]] || bypass=null + [[ $MODE != current-bypass ]] || current=always + if [[ $MODE == redacted-bypass ]]; then echo '{"enforcement":"active"}' + else printf '{"enforcement":"%s","bypass_actors":%s,"current_user_can_bypass":"%s"}' "$enforcement" "$bypass" "$current"; fi + ;; + "api repos/openclaw/telemetry/statuses/$PR_HEAD") + [[ "$*" == *'state=success'* && "$*" == *'context=Publication merge admission'* ]] + echo '{}' + ;; + 'auth setup-git') ;; + *) echo "Unexpected gh request: $*" >&2; exit 2 ;; +esac +`, { mode: 0o755 }); + const run = (id) => spawnSync("/bin/bash", ["-e", "-c", step(id)], { + cwd: checkout, env, encoding: "utf8", timeout: 30_000, + }); + if (mode.startsWith("gate-")) { + env.PR_HEAD = env.GITHUB_SHA; + env.GITHUB_EVENT_PATH = join(root, "event.json"); + await writeFile(env.GITHUB_EVENT_PATH, JSON.stringify({ repository: { + default_branch: mode === "gate-default" ? "other" : "main", + }, pull_request: { number: 7, head: { sha: env.PR_HEAD } } })); + const result = spawnSync(process.execPath, ["scripts/vocabulary-maintenance.mjs", "gate"], { + cwd: checkout, env, encoding: "utf8", timeout: 30_000, + }); + assert.equal(result.status, 0, result.stdout + result.stderr); + const calls = await readFile(env.CALL_LOG, "utf8").catch(() => ""); + assert.equal(calls.includes(`/statuses/${env.PR_HEAD}`), mode === "gate-success"); + return; + } + const admitted = run("candidate"); + if (mode === "pending-different" || mode === "orphan-different" || mode === "stale-before") { + assert.notEqual(admitted.status, 0, admitted.stdout + admitted.stderr); + assert.equal(git("ls-remote", "origin", "refs/heads/main").split("\t")[0], env.GITHUB_SHA); + if (env.PENDING_HEAD) assert.equal(git("ls-remote", "origin", "refs/heads/automation/public-vocabulary").split("\t")[0], env.PENDING_HEAD); + return; + } + assert.equal(admitted.status, 0, admitted.stdout + admitted.stderr); + const values = Object.fromEntries((await readFile(env.GITHUB_OUTPUT, "utf8")).trim().split("\n").map((line) => line.split("="))); + env.DATA_BLOB = values.data_blob; + env.SOURCE_BLOB = values.source_blob; + git("checkout", "-B", "automation/candidate"); + if (mode === "changed-candidate") await payload("unvalidated"); + if (mode === "unowned-path") { + await writeFile(join(checkout, "other.txt"), "unowned\n"); + git("add", "other.txt"); + } + env.PR_HEAD = commit("candidate"); + if (mode === "multi-parent") { + env.PR_HEAD = git("commit-tree", "HEAD^{tree}", "-p", env.GITHUB_SHA, "-p", env.PR_HEAD, "-m", "merge candidate"); + } + env.PR_NUMBER = "7"; + git("checkout", "main"); + const published = run("commit"); + const success = mode === "success" || mode.endsWith("-same") || mode === "merged-branch" || mode === "mergeable-pending" || mode === "ambiguous-merge" || mode === "redacted-bypass"; + assert.equal(published.status === 0, success, published.stdout + published.stderr); + if (success) { + const output = await readFile(env.GITHUB_OUTPUT, "utf8"); + const main = git("ls-remote", "origin", "refs/heads/main").split("\t")[0]; + assert(output.includes(`sha=${main}\n`)); + assert.notEqual(main, env.PR_HEAD, "Fixture must exercise a squash commit"); + } else { + assert(!(await readFile(env.GITHUB_OUTPUT, "utf8")).includes("sha=")); + } + const calls = await readFile(env.CALL_LOG, "utf8"); + if (success || mode === "concurrent-base") { + const statusIndex = calls.indexOf(`/statuses/${env.PR_HEAD}`); + const mergeIndex = calls.indexOf("pr merge"); + assert(statusIndex >= 0 && mergeIndex >= 0 && statusIndex < mergeIndex, "Admission must precede merge"); + } + if (mode === "concurrent-base") { + const main = git("ls-remote", "origin", "refs/heads/main").split("\t")[0]; + assert.equal(git("rev-parse", `${main}^`), env.GITHUB_SHA); + assert.equal(git("rev-parse", `${main}^{tree}`), git("rev-parse", `${env.GITHUB_SHA}^{tree}`)); + assert(!calls.includes("auth setup-git"), "Refused publication must never produce a deployment receipt"); + } + } finally { + await rm(root, { recursive: true, force: true }); + } +} diff --git a/test/vocabulary-maintenance.test.mjs b/test/vocabulary-maintenance.test.mjs index 2e0e063..49f4246 100644 --- a/test/vocabulary-maintenance.test.mjs +++ b/test/vocabulary-maintenance.test.mjs @@ -1,9 +1,9 @@ import { describe, expect, it } from "vitest"; -import { execFileSync, spawnSync } from "node:child_process"; -import { mkdtemp, mkdir, rm, symlink, writeFile } from "node:fs/promises"; +import { mkdtemp, mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { renderVocabulary } from "../scripts/public-vocabulary.mjs"; +import { checkPublication, publicationCases } from "./helpers/vocabulary-publication.mjs"; import { assertCurrentMain, assertDeployedCommit, collectReleases, deployedVersion, needsDeployment, planReleases, readCandidate, validateCandidate, @@ -119,38 +119,21 @@ describe("constrained publication", () => { expect(() => assertCurrentMain(oldRevision, oldRevision)).not.toThrow(); }); - it("rejects a native push when main advances after the publication guard", async () => { - const directory = await mkdtemp(join(tmpdir(), "vocabulary-publish-race-")); - const remote = join(directory, "remote.git"); - const checkout = join(directory, "publisher"); - const git = (...args) => execFileSync("git", ["-C", checkout, ...args], { - encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], - }); - const commit = async (value) => { - await writeFile(join(checkout, "data.json"), value); - git("add", "data.json"); - git("-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", - "-c", "commit.gpgsign=false", "-c", "core.hooksPath=/dev/null", "commit", "-qm", "fixture"); - return git("rev-parse", "HEAD").trim(); - }; - try { - await mkdir(checkout); - execFileSync("git", ["init", "--bare", "--quiet", remote]); - git("init", "--quiet"); - git("remote", "add", "origin", remote); - const base = await commit("base"); - git("push", "origin", "HEAD:refs/heads/main"); - assertCurrentMain(base, git("ls-remote", "origin", "refs/heads/main").split("\t")[0]); - const concurrent = await commit("concurrent maintainer change"); - git("push", "origin", "HEAD:refs/heads/main"); - git("checkout", "--detach", base); - await commit("generated vocabulary"); - const push = spawnSync("git", ["-C", checkout, "push", "origin", "HEAD:refs/heads/main"], { encoding: "utf8" }); - expect(push.status).not.toBe(0); - expect(git("ls-remote", "origin", "refs/heads/main").split("\t")[0]).toBe(concurrent); - } finally { - await rm(directory, { recursive: true, force: true }); - } + it.each(publicationCases)("verifies the PR publication boundary: %s", async (mode) => { + await checkPublication(mode); + }, 30_000); + + it("runs admission only from trusted code and retains the unchanged deployment recovery path", async () => { + const gate = await readFile(new URL("../.github/workflows/publication-admission.yml", import.meta.url), "utf8"); + expect(gate).toContain("branches: [main]"); + expect(gate).toContain("types: [opened, synchronize, reopened, edited, ready_for_review]"); + expect(gate).toContain("ref: ${{ github.workflow_sha }}"); + expect(gate).toContain("persist-credentials: false"); + expect(gate).toContain("run: node scripts/vocabulary-maintenance.mjs gate"); + expect(gate).not.toMatch(/pull_request\.head|npm (?:ci|install)|download-artifact/u); + const workflow = await readFile(new URL("../.github/workflows/deploy.yml", import.meta.url), "utf8"); + expect(workflow).toContain("(needs.publish.result == 'skipped' && needs.discover.outputs.refresh == 'false')"); + expect(workflow).toContain("TARGET_SHA: ${{ needs.publish.outputs.sha || github.sha }}"); }); it("reads only bounded regular artifact files and never executes generated source", async () => {