Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 79 additions & 17 deletions .github/scripts/uk_replay_results_pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ assemble() {
}

build() {
# Every third-party module the builder and the checks import (tests check).
# Every third-party module the builder and the checks import, which tests
# check.
local CONTROL=(uv run --with rfc8785==0.1.4 --with jsonschema python)
builder() { PYTHONPATH="$HERE" "${CONTROL[@]}" -m pipeline.uk_replay_results_pr "$@"; }
git fetch -q --no-tags origin "+refs/heads/$BASE:$BASE_REF" \
Expand Down Expand Up @@ -155,18 +156,61 @@ build() {
echo "::error::scorecard_db.build_db is nondeterministic on the results branch" >&2
exit 1
fi
# What this job verified, read before CI's checks run in its tree.
local verified sealed hash branch
verified=$(git -C "$TREE" rev-parse 'HEAD^{tree}')
sealed=$(cat "$WORK/verify-sealed.txt")
hash=$(content_hash "$WORK/build1.json")
branch=$(plan branch)
preflight
# The verified commit, for the job that holds the token to reproduce.
output base "$base_commit"
output state "$state_commit"
output tree "$(git -C "$TREE" rev-parse 'HEAD^{tree}')"
output sealed "$(cat "$WORK/verify-sealed.txt")"
output content_hash "$(content_hash "$WORK/build1.json")"
output tree "$verified"
output sealed "$sealed"
output content_hash "$hash"
output preflight "$(cat "$WORK/preflight.json")"
output action build
echo "verified tree $(git -C "$TREE" rev-parse 'HEAD^{tree}') for $(plan branch)"
echo "verified tree $verified for $branch"
}

# CI's format check and suite on the commit, as ci.yml's python job runs them
# (tests check the lines are its lines), then its no-drift check. A failure
# is reported, not fatal: the pull request opens as a draft that names it
# (review of 24434729, 3): this runs in the build job, which holds no write
# credential. The variables that name the step's command files are unset for
# it, so a test or tool that writes $GITHUB_OUTPUT or $GITHUB_ENV by name
# writes nowhere. That is hygiene, not a sandbox: a process that finds the
# files on the runner's disk can still write them, which is why job 2 takes
# every output of this job as its word, checked for shape.
preflight() (
local RUFF=(uvx ruff format --check be nz scorecard_db tests pipeline)
local SUITE=(uv run --with pytest --with hypothesis --with pypdf --with pyyaml --with numpy --with psutil --with rfc8785==0.1.4 --with jsonschema pytest tests/ -q)
if [ -n "${GH_TOKEN:-}${GITHUB_TOKEN:-}" ]; then
echo "::error::CI's checks run with no GitHub token in the environment" >&2
exit 1
fi
unset GITHUB_ENV GITHUB_OUTPUT GITHUB_PATH GITHUB_STATE GITHUB_STEP_SUMMARY
# Their output goes to the log, where GitHub would act on a line that is a
# workflow command (::set-output, ::add-mask:: ...): a token only this
# shell holds pauses those until the checks are done. The runner reads a
# command only at the start of a line, so each of these starts its own: a
# suite that stops mid-line would otherwise leave commands paused.
local ruff=0 suite=0 resume
resume=$(python3 -I -c 'import os; print(os.urandom(16).hex())')
printf '\n::stop-commands::%s\n' "$resume"
(cd "$TREE" && "${RUFF[@]}") 2>&1 | tee "$WORK/ruff.log" || ruff=$?
(cd "$TREE" && PYTHONPATH=. "${SUITE[@]}") 2>&1 | tee "$WORK/suite.log" || suite=$?
printf '\n::%s::\n' "$resume"
git -C "$TREE" status --porcelain > "$WORK/porcelain.txt"
builder preflight --ruff-exit "$ruff" --ruff-log "$WORK/ruff.log" \
--suite-exit "$suite" --suite-log "$WORK/suite.log" \
--porcelain "$WORK/porcelain.txt" --output "$WORK/preflight.json"
)

rebuild() {
: "${BUILT_TREE:?}" "${BUILT_BASE:?}" "${BUILT_STATE:?}" "${BUILT_SEALED:?}" "${BUILT_CONTENT_HASH:?}"
: "${BUILT_TREE:?}" "${BUILT_BASE:?}" "${BUILT_STATE:?}" "${BUILT_SEALED:?}" "${BUILT_CONTENT_HASH:?}" \
"${BUILT_PREFLIGHT:?}"
# The standard library alone: no site-packages, nothing installed. check
# skips the schema and digest checks the build job made; the rest of the
# builder needs no package in either job.
Expand Down Expand Up @@ -218,41 +262,57 @@ rebuild() {
exit 0
fi
fi
python3 -I - "$WORK/validation.json" "$IDENTICAL" "$BUILT_SEALED" "$BUILT_CONTENT_HASH" <<'PY'
python3 -I - "$WORK/validation.json" "$IDENTICAL" "$BUILT_SEALED" "$BUILT_CONTENT_HASH" \
"$BUILT_PREFLIGHT" <<'PY'
import json
import sys

path, identical, sealed, content_hash = sys.argv[1:]
path, identical, sealed, content_hash, preflight = sys.argv[1:]
with open(path, "w") as handle:
json.dump(
{
"identical_files": int(identical),
"verify_sealed": sealed,
"content_hash": content_hash,
"preflight": preflight,
},
handle,
)
PY
python3 -I -S "$HERE/pipeline/uk_replay_results_pr.py" body --request-dir "$STAGE/$REQUEST" \
--plan "$WORK/plan.json" --validation "$WORK/validation.json" --output "$WORK/BODY.md"
# body refuses a malformed preflight report, and says whether the pull
# request is a draft: integrity is not complete, or one of CI's checks fails.
local draft
draft=$(python3 -I -S "$HERE/pipeline/uk_replay_results_pr.py" body --request-dir "$STAGE/$REQUEST" \
--plan "$WORK/plan.json" --validation "$WORK/validation.json" --output "$WORK/BODY.md")
output commit "$(git -C "$TREE" rev-parse HEAD)"
output branch "$(plan branch)"
output draft "$draft"
output action build
echo "rebuilt $(plan branch) at $(git -C "$TREE" rev-parse HEAD), tree $BUILT_TREE"
}

propose() {
: "${BUILT_COMMIT:?}" "${BUILT_BRANCH:?}" "${GH_TOKEN:?}"
: "${BUILT_COMMIT:?}" "${BUILT_BRANCH:?}" "${BUILT_DRAFT:?}" "${GH_TOKEN:?}"
local branch=$BUILT_BRANCH head
# Push exactly the commit this job assembled, on the main it assembled it on.
head=$(git -C "$TREE" rev-parse HEAD)
if [ "$head" != "$BUILT_COMMIT" ] || [ "$branch" != "$(plan branch)" ]; then
echo "::error::the results commit changed after it was assembled" >&2
exit 1
fi
if [ "$BUILT_DRAFT" != true ] && [ "$BUILT_DRAFT" != false ]; then
echo "::error::BUILT_DRAFT must be true or false" >&2
exit 1
fi
# The token reaches git only through gh, for these commands alone.
pushgit() { git -C "$TREE" -c credential.helper= -c 'credential.helper=!gh auth git-credential' "$@"; }

# The open pull request on this branch, if any. Nothing here merges it.
local title number
title=$(plan title)
number=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$branch" --base "$BASE" \
--state open --json number --jq '.[0].number // empty')

# Push: a new branch, or a rebuild over the one commit this builder wrote.
local remote pushed=false
remote=$(git ls-remote --heads origin "refs/heads/$branch" | cut -f1)
Expand All @@ -276,6 +336,13 @@ propose() {
echo "::error::$branch holds work this builder did not write; update the branch by hand" >&2
exit 1
fi
# A rebuild that should be a draft turns its open pull request back
# into one before the push, so it is never ready on a commit whose
# checks failed. Nothing here marks one ready: a person does, once CI
# passes.
if [ -n "$number" ] && [ "$BUILT_DRAFT" = true ]; then
gh pr ready "$number" --repo "$GITHUB_REPOSITORY" --undo
fi
# The token has no workflows permission. This commit changes no
# workflow, but the rebuilt branch also carries main's commits since
# $remote; if one changed .github/workflows (even one main later
Expand All @@ -293,15 +360,10 @@ propose() {
fi

# The pull request: opening it, or the push to its branch, starts CI on it.
# Nothing here merges it.
local title number
title=$(plan title)
number=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$branch" --base "$BASE" \
--state open --json number --jq '.[0].number // empty')
if [ -z "$number" ]; then
local create=(--repo "$GITHUB_REPOSITORY" --base "$BASE" --head "$branch"
--title "$title" --body-file "$WORK/BODY.md")
if [ "$(plan draft)" = True ]; then
if [ "$BUILT_DRAFT" = true ]; then
create+=(--draft)
fi
gh pr create "${create[@]}"
Expand Down
23 changes: 14 additions & 9 deletions .github/workflows/uk-replay-results.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ name: UK replay results PR
# code. It holds no dataset token.
#
# Two jobs, two runners. `build` assembles the commit and runs every check,
# with PyPI packages and no write credential. `propose` runs on a fresh
# runner, so nothing those packages did can reach it: with git and the
# standard library alone it assembles the same commit from the same main and
# replay-state history, refuses unless its tree is the one `build` verified
# (`build`'s other outputs are its word, checked for shape; CI on the pull
# request reruns the checks), decides again against main's tip from the
# request it guarded, and only then mints a short-lived token for the
# organisation's policyengine GitHub App, scoped to this repository, with
# contents and pull-requests to push its own uk-replay-results/<variant>
# with PyPI packages and no write credential: main's results checks, then
# CI's own format check, suite and no-drift check, whose failure makes the
# pull request a draft that names it rather than stopping the run. `propose`
# runs on a fresh runner, so nothing those packages did can reach it: with
# git and the standard library alone it assembles the same commit from the
# same main and replay-state history, refuses unless its tree is the one
# `build` verified (`build`'s other outputs are its word, checked for shape;
# CI on the pull request reruns the checks), decides again against main's
# tip from the request it guarded, and only then mints a short-lived token
# for the organisation's policyengine GitHub App, scoped to this repository,
# with contents and pull-requests to push its own uk-replay-results/<variant>
# branch and open the pull request. A pull request opened with GITHUB_TOKEN
# would start no pull_request run, so it would never carry checks; the
# App's does. The token has no workflows permission: the commit touches
Expand Down Expand Up @@ -64,6 +66,7 @@ jobs:
tree: ${{ steps.build.outputs.tree }}
sealed: ${{ steps.build.outputs.sealed }}
content_hash: ${{ steps.build.outputs.content_hash }}
preflight: ${{ steps.build.outputs.preflight }}
steps:
- uses: actions/checkout@v4
with:
Expand Down Expand Up @@ -104,6 +107,7 @@ jobs:
BUILT_STATE: ${{ needs.build.outputs.state }}
BUILT_SEALED: ${{ needs.build.outputs.sealed }}
BUILT_CONTENT_HASH: ${{ needs.build.outputs.content_hash }}
BUILT_PREFLIGHT: ${{ needs.build.outputs.preflight }}
run: bash .github/scripts/uk_replay_results_pr.sh rebuild
- name: Mint a repository-scoped token to push the branch and open the PR
id: token
Expand All @@ -122,5 +126,6 @@ jobs:
BASE_BRANCH: ${{ github.event.repository.default_branch }}
BUILT_COMMIT: ${{ steps.rebuild.outputs.commit }}
BUILT_BRANCH: ${{ steps.rebuild.outputs.branch }}
BUILT_DRAFT: ${{ steps.rebuild.outputs.draft }}
GH_TOKEN: ${{ steps.token.outputs.token }}
run: bash .github/scripts/uk_replay_results_pr.sh propose
56 changes: 50 additions & 6 deletions docs/uk_replay/RELEASE_REPLAY.md
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ runs only the default branch's code.

| Job | Authority and inputs |
|---|---|
| `build` | `GITHUB_TOKEN` `contents: read`, checkout credentials not persisted, no secret. Assembles the commit and runs every check, with the PyPI packages the checks need. Outputs the verified tree, the main and `replay-state` commits it used, and two check results. |
| `build` | `GITHUB_TOKEN` `contents: read`, checkout credentials not persisted, no secret. Assembles the commit and runs every check, with the PyPI packages the checks need, CI's own format check and suite among them. Outputs the verified tree, the main and `replay-state` commits it used, two check results and its report of CI's checks. |
| `propose` | On a fresh runner. `GITHUB_TOKEN` `contents: read`; actions pinned to commits; no uv and no installed package. Assembles the same commit again with git and the standard library (the builder under `python3 -I -S`, small helpers under `python3 -I`) on the commits `build` used: `replay-state` history, and main history no older than the workflow's own commit. It refuses unless its tree is `build`'s, then decides again against main's current tip, reading only the request it extracted and guarded. Only then is a short-lived token for the organisation's `policyengine` GitHub App minted, scoped to this repository with `contents: write` and `pull-requests: write`, to push that commit and open the pull request. |

Neither job holds a dataset token, and neither installs, imports or runs
Expand All @@ -992,9 +992,10 @@ runner: whatever the checks' packages did there (a `$GITHUB_PATH` or
the token. Job 2 receives only job 1's outputs, as strings it validates, and
pushes only what this code builds from main's and `replay-state`'s history.
The tree comparison catches an honest build job whose commit diverges; the
`verify-sealed` and content-hash lines in the pull request body are the build
job's word, checked only for shape. CI on the pull request runs the
regeneration, both database builds and the drift check again.
`verify-sealed` and content-hash lines and the report of CI's checks in the
pull request body are the build job's word, checked only for shape. CI on the
pull request runs the regeneration, both database builds, the format check,
the suite and the drift checks again.

The App token, not `GITHUB_TOKEN`, writes because a pull request opened with
`GITHUB_TOKEN` starts no `pull_request` workflow
Expand Down Expand Up @@ -1063,21 +1064,64 @@ which CI now reads from the index:
4. **Checks.** `uk_replay_qualify verify-sealed` (the new namespace, then
every one), CI's `.github/scripts/uk_replay_regenerate.sh`, and two
`scorecard_db.build_db` runs, which must agree on the content hash and
leave no tracked-file drift.
leave no tracked-file drift. A failure of any of these stops the run.
Then CI's own checks on the same tree, as `ci.yml`'s `python` job runs
them on every pull request: `uvx ruff format --check be nz scorecard_db
tests pipeline`, the suite (`uv run --with pytest … pytest tests/ -q`,
with `PYTHONPATH=.`), and its no-drift check, `git status --porcelain`
after the suite. A test checks that these are `ci.yml`'s lines word for
word. They run in the `build` job, which holds no write credential: the
script refuses to start them if `GH_TOKEN` or `GITHUB_TOKEN` is set, and
unsets the variables that name the step's command files
(`$GITHUB_OUTPUT`, `$GITHUB_ENV`, `$GITHUB_PATH`, `$GITHUB_STATE`,
`$GITHUB_STEP_SUMMARY`), so a test that writes one by name writes
nowhere. That is hygiene, not a sandbox: a process that finds the files
on the runner's disk can still write them, and `propose` takes every
output of `build` as its word, checked for shape. Their output goes to
the step log with workflow commands paused (`::stop-commands::`, by a
token only the script holds), so a line the suite prints is not acted
on as one, and what `build` outputs about the commit is read before they
run. Their failure does not stop the run. The job reports each check's
exit status, how many files, tests or paths failed (the node ids of
pytest's last short test summary, the files ruff names, the paths
`git status` lists) and the first 50 that are printable ASCII without a
backtick, with no space at either end and at most 200 characters long,
as one line of JSON (three full lists keep the body inside GitHub's
65,536-character limit). On CI pytest prints whole messages over lines:
the job reads a message line that itself starts with `FAILED` or `ERROR`
as another node id, and one that is itself a summary header drops the
ids before it. Only the list changes: a check's exit status, not its
list, says whether it failed. `propose` refuses a report of any other
shape, a count of a billion or more among them. Results pull requests
#195, #204 and #206 opened ready and failed CI's `pytest` job; with these
checks such a pull request opens as a draft that names the failures.
5. **Pull request.** In the `propose` job, steps 1 to 3 run again with the
standard library, skipping only the JSON Schema and RFC 8785 checks
`build` made. Only when the tree matches is the App token minted. The
commit, authored and committed as `github-actions[bot]` (the receiver's
bot identity) with a `Replay-Request:` trailer and pushed with the App's
token, goes to `uk-replay-results/<variant_id>`, and a pull request
against main follows; CI runs on it as on any pull request, without the
dataset token. It is a draft when integrity is not `complete`.
dataset token. It is a draft when integrity is not `complete`, or when
one of CI's checks in step 4 fails; its body then lists what failed,
under "Validation in the results run", in a fenced block, with the
count of any it could not list. Fix those on main and run the workflow
again for the same request: it rebuilds the branch on the new main.
The builder never marks a pull request ready; a person does, once CI
passes.

Running the workflow again for the same request is safe. An identical tree
pushes nothing. A branch built on an older main is rebuilt and force-pushed,
with a lease on the commit it read, only when it holds exactly one commit,
authored and committed by the bot with this request's trailer; an added or
amended commit is a person's, and the run fails and leaves the branch alone.
A rebuild that pushes edits the open pull request's title and body. One that
is a draft (integrity not `complete`, or one of CI's checks failing) first
turns a ready pull request back into a draft (`gh pr ready --undo`), before
the push, so it is never ready on a commit whose checks failed; one that is
not leaves it as it is. A rerun on
an unchanged main pushes nothing, so it leaves the body as it was, even when
the earlier run's suite failed only by chance.
A rebuilt branch also carries main's commits since the branch was built.
GitHub does not document whether it counts that as editing a workflow when
one of those commits changed `.github/workflows`, even a change main later
Expand Down
Loading
Loading