diff --git a/.github/bdd-parts.json b/.github/bdd-parts.json index 75b55f8a9b..f385106a69 100644 --- a/.github/bdd-parts.json +++ b/.github/bdd-parts.json @@ -261,21 +261,6 @@ "VR-22", "VR-23" ], - "preservation-lists": [ - "SP-15" - ], - "preservation-runtime": [ - "SP-22" - ], - "preservation-support": [ - "SP-25" - ], - "bootstrap-runtime": [ - "BS-09" - ], - "preservation-prims": [ - "SP-28" - ], "preservation-std": [ "SP-29", "SP-32", diff --git a/.github/scripts/host_lanes.py b/.github/scripts/host_lanes.py new file mode 100755 index 0000000000..bbe3c9b8a1 --- /dev/null +++ b/.github/scripts/host_lanes.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +"""Run one supported host's tests as lanes of one run, and join them (SPEC §8.3/§9.4). + +A host whose one `cargo test --workspace` was the longest job of the gate runs +it as lanes instead, each a job of its own clean checkout and build: the +conformance harness's cases, partitioned exactly as the Intel lanes partition +them (`.github/intel-host-lanes.json`, whose completeness honesty checks), and +the `producer` lane, every other test of the workspace and every doctest. Each +lane records what the host listed and what it executed; the join requires a +receipt from every lane of the same run and commit, lanes that are disjoint, +and an execution whose union is the host's whole listing, test for test, +before the host's context passes. Nothing crosses runs. Python 3.9+. +""" + +import argparse +from collections import Counter +import json +import os +from pathlib import Path +import re +import subprocess +import sys + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import intel_host_tests as factory # noqa: E402 (the shared partition) + +SCHEMA = 1 +PRODUCER = "producer" +CARGO = ["cargo", "test", "--locked", "--all-features", "--no-fail-fast"] +HARNESS = [*CARGO, "-p", "repo-conformance", "--test", "conformance"] +RESULT = re.compile(r"test (.+?) \.\.\. (ok|ignored|FAILED)(?:, .*)?") +SUFFIXES = (" - should panic", " - compile fail", " - no_run") + +require = factory.require +Invalid = factory.Invalid + + +def lanes(root): + """The lanes of a host: the producer, then the conformance lanes.""" + bdd, plan = factory.plan(root) + return bdd, plan, [PRODUCER, *plan["lanes"]] + + +def listing(output): + """Every `name: test` line of a (multi-binary) `--list` transcript.""" + return [line[: -len(": test")] for line in output.splitlines() if line.endswith(": test")] + + +def executed(output): + names = [] + for line in output.splitlines(): + match = RESULT.fullmatch(line.strip()) + if match: + name = match.group(1) + for suffix in SUFFIXES: + name = name.removesuffix(suffix) + names.append(name) + return names + + +def run(command, root, capture=True): + print("+ " + " ".join(command), flush=True) + process = subprocess.Popen(command, cwd=root, stdout=subprocess.PIPE, text=True, + encoding="utf-8", errors="replace") + lines = [] + for line in process.stdout: + sys.stdout.write(line) + lines.append(line) + sys.stdout.flush() + return process.wait(), "".join(lines) + + +def metadata_targets(root): + """The test-carrying targets of repo-conformance other than the harness.""" + output = subprocess.run(["cargo", "metadata", "--locked", "--format-version", "1", "--no-deps"], + cwd=root, check=True, stdout=subprocess.PIPE, text=True).stdout + package = next(p for p in json.loads(output)["packages"] if p["name"] == "repo-conformance") + selectors = [] + for target in package["targets"]: + kinds = set(target["kind"]) + if "test" in kinds and target["name"] != "conformance": + selectors += ["--test", target["name"]] + elif kinds & {"lib", "rlib"}: + selectors.append("--lib") + elif "bin" in kinds: + selectors += ["--bin", target["name"]] + return selectors + + +def identity(root): + git = lambda *args: subprocess.run(["git", *args], cwd=root, check=True, # noqa: E731 + stdout=subprocess.PIPE, text=True).stdout.strip() + return {"commit": git("rev-parse", "HEAD"), "tree": git("rev-parse", "HEAD^{tree}"), + "run": os.environ.get("GITHUB_RUN_ID", ""), "attempt": os.environ.get("GITHUB_RUN_ATTEMPT", "")} + + +def execute(root, target, lane, output): + host = subprocess.run(["rustc", "-vV"], cwd=root, check=True, stdout=subprocess.PIPE, + text=True).stdout + require(f"host: {target}\n" in host, f"this runner is not {target}") + bdd, plan, names = lanes(root) + require(lane in names, f"`{lane}` is no lane of {factory.LANES}") + status, out = run([*HARNESS, "--", "--list"], root) + require(status == 0, "the conformance harness does not list") + conformance = factory.parse_list(out) + receipt = {"schema": SCHEMA, "target": target, "lane": lane, **identity(root), + "conformance": conformance, "whole": None, "executed": []} + failed = False + if lane == PRODUCER: + status, out = run(["cargo", "test", "--locked", "--workspace", "--all-features", "--", "--list"], root) + require(status == 0, "the workspace does not list") + receipt["whole"] = sorted(listing(out)) + commands = [ + [*CARGO, "--workspace", "--exclude", "repo-conformance"], + [*CARGO, "-p", "repo-conformance", *metadata_targets(root)], + [*CARGO, "-p", "repo-conformance", "--doc"], + ] + else: + commands = [[*HARNESS, "--", *factory.lane_filters(bdd, plan)[lane]]] + for command in commands: + status, out = run(command, root) + failed |= status != 0 + receipt["executed"] += executed(out) + receipt["executed"].sort() + receipt["passed"] = not failed + output.mkdir(parents=True, exist_ok=True) + (output / "receipt.json").write_text(json.dumps(receipt, indent=1, sort_keys=True) + "\n") + require(not failed, f"lane `{lane}` has a failing test") + print(f"host-lanes: {target} lane {lane}: {len(receipt['executed'])} tests passed") + + +def join(root, target, artifacts): + bdd, plan, names = lanes(root) + receipts = {} + for path in sorted(Path(artifacts).glob("*/receipt.json")): + receipt = json.loads(path.read_text()) + require(receipt.get("schema") == SCHEMA and receipt.get("target") == target, + f"{path}: a receipt of another host or format") + require(receipt["lane"] not in receipts, f"two receipts of lane `{receipt['lane']}`") + receipts[receipt["lane"]] = receipt + return check_join(receipts, names, plan, bdd, identity(root)) + + +def check_join(receipts, names, plan, bdd, here): + """The lanes are this run's and commit's, complete, disjoint, and passed.""" + require(sorted(receipts) == sorted(names), f"lanes {sorted(names)}, receipts {sorted(receipts)}") + for lane, receipt in receipts.items(): + for field in ("commit", "tree", "run", "attempt"): + if field == "attempt": + require(str(receipt[field]).isdigit() and int(receipt[field]) <= int(here[field] or 0), + f"lane `{lane}` ran in another attempt") + else: + require(receipt[field] == here[field], f"lane `{lane}` ran on another {field}") + require(receipt.get("passed") is True, f"lane `{lane}` did not pass") + conformance = receipts[PRODUCER]["conformance"] + require(conformance and all(r["conformance"] == conformance for r in receipts.values()), + "the lanes listed different conformance harnesses") + named = {factory.test_name(i) for ids in bdd["parts"].values() for i in ids} + require(named <= set(conformance), f"named tests absent: {sorted(named - set(conformance))}") + whole = Counter(receipts[PRODUCER]["whole"] or []) + ran = Counter(name for r in receipts.values() for name in r["executed"]) + require(whole, "the producer listed no workspace") + missing, extra = whole - ran, ran - whole + require(not missing and not extra, + f"executed is not the host's listing: missing {sorted(missing)[:20]}, extra {sorted(extra)[:20]}") + for lane in names: + if lane == PRODUCER: + continue + require(set(receipts[lane]["executed"]) <= set(conformance), + f"lane `{lane}` ran a test outside the conformance harness") + print(f"host-lanes: {len(ran)} tests of {len(names)} lanes equal the host's listing, each once") + return sum(ran.values()) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + sub = parser.add_subparsers(dest="command", required=True) + one = sub.add_parser("execute") + one.add_argument("--target", required=True) + one.add_argument("--lane", required=True) + one.add_argument("--output", required=True, type=Path) + both = sub.add_parser("join") + both.add_argument("--target", required=True) + both.add_argument("--artifacts", required=True) + arguments = parser.parse_args() + root = Path(__file__).resolve().parents[2] + try: + if arguments.command == "execute": + execute(root, arguments.target, arguments.lane, arguments.output) + else: + join(root, arguments.target, arguments.artifacts) + except Invalid as error: + print(f"::error::{error}", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/take-bootstrap-export.sh b/.github/scripts/take-bootstrap-export.sh new file mode 100755 index 0000000000..79990117a0 --- /dev/null +++ b/.github/scripts/take-bootstrap-export.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# The fetcher of the run's bootstrap export (SPEC.md §9.4), started beside a +# leg's gate command in a run that is the whole gate. It waits until a case +# of the leg asks for the export (the harness writes `/wanted`), then +# waits for the artifact `bootstrap-export` of THIS run, unpacks it into +# `/artifact`, and writes `/ready`; the harness checks the custody +# record against its own run and checkout before any case reads the bundle. +# When the `bootstrap export` job ends without the artifact it writes the +# reason to `/failed`, and the case fails with it. It reads the run's +# artifacts and jobs through `gh` with the job's token (`actions: read`), +# and nothing of another run. +set -uo pipefail +dir=${1:?the directory the harness waits in} +repo=${GITHUB_REPOSITORY:?} +run=${GITHUB_RUN_ID:?} +mkdir -p "$dir" +fail() { + printf '%s\n' "$1" > "$dir/failed.tmp" && mv "$dir/failed.tmp" "$dir/failed" + echo "take-bootstrap-export: $1" >&2 + exit 1 +} +# No API call until a case needs the export: most legs never ask. +until [ -e "$dir/wanted" ]; do sleep 5; done +echo "take-bootstrap-export: wanted at $(date -u +%H:%M:%SZ)" +polls=0 +while :; do + id=$(gh api "repos/$repo/actions/runs/$run/artifacts?name=bootstrap-export" \ + --jq '[.artifacts[] | select(.expired | not)] | max_by(.created_at) | .id // empty' 2>/dev/null) || id= + if [ -n "$id" ]; then + rm -rf "$dir/artifact" "$dir/artifact.zip" + gh api "repos/$repo/actions/artifacts/$id/zip" > "$dir/artifact.zip" \ + || fail "the artifact $id of run $run did not download" + mkdir -p "$dir/artifact" && unzip -q "$dir/artifact.zip" -d "$dir/artifact" \ + || fail "the artifact $id of run $run did not unpack" + rm -f "$dir/artifact.zip" + echo "take-bootstrap-export: artifact $id of run $run at $(date -u +%H:%M:%SZ)" + touch "$dir/ready" + exit 0 + fi + # Every fourth poll, whether the export job ended without its artifact. + if [ $((polls % 4)) -eq 0 ]; then + state=$(gh api "repos/$repo/actions/runs/$run/jobs?filter=latest&per_page=100" --paginate \ + --jq '.jobs[] | select(.name == "bootstrap export") | "\(.status) \(.conclusion)"' 2>/dev/null) || state= + case "$state" in + "completed success") ;; # uploaded: the next listing shows it + completed*) fail "the bootstrap export job of run $run ended: $state" ;; + esac + fi + polls=$((polls + 1)) + sleep 30 +done diff --git a/.github/scripts/test_host_lanes.py b/.github/scripts/test_host_lanes.py new file mode 100755 index 0000000000..32318b3109 --- /dev/null +++ b/.github/scripts/test_host_lanes.py @@ -0,0 +1,88 @@ +"""Behavioral tests of the host lanes' join (SPEC §8.3), run in honesty.""" + +import copy +from pathlib import Path +import sys +import unittest + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import host_lanes # noqa: E402 + +BDD = {"parts": {"one": ["AA-01"], "two": ["AA-02"]}, "targets": {"honesty": ["bdd"]}, "remainder": "rest"} +PLAN = {"producer": ["honesty"], "lanes": {"a": ["one"], "b": ["two", "rest"]}} +NAMES = ["producer", "a", "b"] +HERE = {"commit": "c" * 40, "tree": "t" * 40, "run": "7", "attempt": "1"} +HARNESS = ["conformance_aa_01", "conformance_aa_02", "conformance_aa_03"] + + +def receipts(): + base = {"schema": 1, "target": "x86_64-pc-windows-msvc", "passed": True, "conformance": HARNESS, + "whole": None, **HERE} + return { + "producer": {**base, "lane": "producer", "executed": ["meta", "unit"], + "whole": sorted(["meta", "unit", *HARNESS])}, + "a": {**base, "lane": "a", "executed": ["conformance_aa_01"]}, + "b": {**base, "lane": "b", "executed": ["conformance_aa_02", "conformance_aa_03"]}, + } + + +class Join(unittest.TestCase): + def check(self, value): + return host_lanes.check_join(value, NAMES, PLAN, BDD, HERE) + + def refuse(self, value): + with self.assertRaises(host_lanes.Invalid): + self.check(value) + + def test_complete_lanes_pass(self): + self.assertEqual(self.check(receipts()), 5) + + def test_missing_lane(self): + value = receipts() + del value["b"] + self.refuse(value) + + def test_test_run_twice(self): + value = receipts() + value["a"]["executed"].append("conformance_aa_02") + self.refuse(value) + + def test_test_lost(self): + value = receipts() + value["b"]["executed"] = ["conformance_aa_02"] + self.refuse(value) + + def test_failed_lane(self): + value = receipts() + value["a"]["passed"] = False + self.refuse(value) + + def test_other_commit_or_run(self): + for field in ("commit", "tree", "run"): + value = receipts() + value["a"][field] = "x" + self.refuse(value) + + def test_different_harness(self): + value = receipts() + value["b"] = copy.deepcopy(value["b"]) + value["b"]["conformance"] = HARNESS[:2] + self.refuse(value) + + def test_conformance_lane_outside_harness(self): + value = receipts() + value["a"]["executed"] = ["meta"] + value["producer"]["executed"] = ["conformance_aa_01", "unit"] + self.refuse(value) + + def test_named_test_absent(self): + value = receipts() + for receipt in value.values(): + receipt["conformance"] = ["conformance_aa_02", "conformance_aa_03"] + value["producer"]["whole"] = sorted(["meta", "unit", "conformance_aa_02", "conformance_aa_03"]) + value["a"]["executed"] = [] + self.refuse(value) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 812c99c315..d569943a50 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -150,7 +150,14 @@ jobs: - name: Test the Intel construction and inventory checks env: PYTHONDONTWRITEBYTECODE: "1" - run: python3 -m unittest discover -s .github/scripts -p 'test_intel_host_tests.py' + run: | + python3 -m unittest discover -s .github/scripts -p 'test_intel_host_tests.py' + python3 -m unittest discover -s .github/scripts -p 'test_host_lanes.py' + # Stale generated files fail here in minutes, before the expensive + # roots: every input a committed build oracle records, every lock, and + # every committed export package must be current (§9.4). No Lean. + - name: Check that the committed generated files are current + run: cargo xtask check-freshness - name: Run the honesty part of the acceptance gate shell: bash run: | @@ -169,6 +176,87 @@ jobs: if-no-files-found: error retention-days: 14 + # The bootstrap production export, once for the run (SPEC.md §9.4). Every + # bootstrap case compares the packages of that export with the committed + # packages, and exporting verifies the whole bootstrap project, which took + # 20 to 37 minutes of every part that ran it. This job runs it once, at the + # profile of the `examples` leg, requires its packages to equal the + # committed ones, and uploads the bundle with a custody record that binds it + # to the repository, the run and its attempt, the commit and its tree, the + # toolchain pins and the toolchains that ran, and the bundle's digest. A leg + # takes it through `.github/scripts/take-bootstrap-export.sh` when one of + # its cases first needs it, and the harness checks the record against the + # leg's own run and checkout and replays the bundle before the case reads + # it. The legs start when honesty passes, as before, and wait only at that + # point; the artifact is read by this run alone. + bootstrap-export: + name: bootstrap export + needs: [plan, honesty] + if: ${{ needs.plan.outputs.full == 'true' }} + runs-on: ubuntu-24.04 + timeout-minutes: 360 + env: + LEXLEAN_CI_TELEMETRY: ${{ github.workspace }}/ci-evidence + # The resource profile of the examples leg (§22.11); operational only. + LEXLEAN_VERIFY_WIDTH: "2" + LEXLEAN_VERIFY_MEMORY_KIB: "12582912" + LEXLEAN_VERIFY_HINTS: ${{ github.workspace }}/.github/verify-hints.json + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Install the pinned Rust toolchain + run: rustup toolchain install + - name: Install elan and the pinned Lean toolchain + run: | + set -euo pipefail + curl https://elan.lean-lang.org/elan-init.sh -sSf | sh -s -- -y --default-toolchain none + echo "$HOME/.elan/bin" >> "$GITHUB_PATH" + "$HOME/.elan/bin/elan" toolchain install "$(cat lean-toolchain)" + - name: Reserve bounded overflow memory for the export + run: | + set -euo pipefail + sudo fallocate -l 4G "$RUNNER_TEMP/lexlean-atlas.swap" + sudo chmod 600 "$RUNNER_TEMP/lexlean-atlas.swap" + sudo mkswap "$RUNNER_TEMP/lexlean-atlas.swap" + sudo swapon "$RUNNER_TEMP/lexlean-atlas.swap" + sudo sysctl vm.swappiness=10 + swapon --show + - name: Export the bootstrap project once for the run + shell: bash + run: | + set -euo pipefail + mkdir -p ci-evidence + sample_resources() { + set +e + while true; do + avail=$(awk '/^MemAvailable:/ {print $2}' /proc/meminfo) + swap=$(awk '/^SwapTotal:/ {t=$2} /^SwapFree:/ {f=$2} END {print t-f}' /proc/meminfo) + top=$( (ps -eo rss= --sort=-rss || true) | awk 'NR==1 {print $1}') + printf '%s\t%s\t%s\t%s\n' "$(date +%s.%N | cut -c1-14)" "$avail" "$swap" "${top:-0}" + sleep 5 + done + } + sample_resources > ci-evidence/resources.tsv & + sample_pid=$! + trap 'kill "$sample_pid" 2>/dev/null || true; wait "$sample_pid" 2>/dev/null || true' EXIT + cargo xtask export-bootstrap --output "$RUNNER_TEMP/bootstrap-export" 2>&1 | tee ci-evidence/vv.txt + - name: Summarize the run's timing and memory + if: ${{ !cancelled() }} + run: cargo xtask ci-summary --evidence ci-evidence + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: bootstrap-export + path: ${{ runner.temp }}/bootstrap-export + if-no-files-found: error + include-hidden-files: true + retention-days: 1 + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + if: ${{ !cancelled() }} + with: + name: acceptance-gate-evidence-bootstrap-export + path: ci-evidence + if-no-files-found: error + retention-days: 14 + vv: name: just vv (${{ matrix.part }}) needs: [plan, honesty] @@ -181,6 +269,11 @@ jobs: matrix: include: ${{ fromJSON(needs.plan.outputs.fanout) }} runs-on: ubuntu-24.04 + # `actions: read` lets a leg list and download this run's artifacts: the + # bootstrap export, which its fetcher takes when a case needs it. + permissions: + contents: read + actions: read # Complete proof coverage is intentionally the long pole. GitHub-hosted # jobs permit six hours; a shorter local-style timeout would turn complete # proof coverage into a CI failure mode. @@ -227,9 +320,23 @@ jobs: swapon --show - name: Run the normative acceptance gate shell: bash + env: + # In a run that is the whole gate, the `bootstrap export` job exports + # the bootstrap project for every leg (§9.4): the fetcher takes the + # run's artifact when a case first asks for it, and the harness + # checks its custody record before any case reads it. Otherwise no + # job exports it once, and a case exports it itself. + FULL_GATE: ${{ needs.plan.outputs.full }} + GH_TOKEN: ${{ github.token }} run: | set -euo pipefail mkdir -p ci-evidence + take_pid= + if [ "$FULL_GATE" = true ]; then + export LEXLEAN_BOOTSTRAP_EXPORT="$RUNNER_TEMP/bootstrap-export" + .github/scripts/take-bootstrap-export.sh "$LEXLEAN_BOOTSTRAP_EXPORT" > ci-evidence/take-bootstrap-export.txt 2>&1 & + take_pid=$! + fi monitor_resources() { while true; do date -u +'%Y-%m-%dT%H:%M:%SZ' @@ -255,7 +362,7 @@ jobs: monitor_pid=$! sample_resources > ci-evidence/resources.tsv & sample_pid=$! - trap 'kill "$monitor_pid" "$sample_pid" 2>/dev/null || true; wait "$monitor_pid" "$sample_pid" 2>/dev/null || true' EXIT + trap 'kill "$monitor_pid" "$sample_pid" $take_pid 2>/dev/null || true; wait "$monitor_pid" "$sample_pid" $take_pid 2>/dev/null || true' EXIT just --timestamp --timestamp-format '%s%.3f' ${{ matrix.recipes }} 2>&1 | tee ci-evidence/vv.txt # The end of the leg's last recipe, as `vv` prints its own at the end # of the whole gate (`repo_conformance::evidence::PART_PASSED_LINE`). @@ -1021,9 +1128,10 @@ jobs: # reports which ones it skipped; on Linux x86-64 the same gate makes the # toolchain mandatory, so nothing passes vacuously where it matters. # - # The three hosts below keep their original single invocation. Intel's - # conformance execution is partitioned below after one construction; its - # final aggregate retains the fourth supported-host status (issue #449). + # The two hosts below keep their original single invocation. Windows runs + # as lanes below, each building for itself, and Intel's conformance + # execution is partitioned below after one construction; each aggregate + # retains its supported-host status (issue #449). # There is no separate build step here: `cargo test` with the same `--locked # --workspace --all-features` builds, in the same profile, the library and the # binary of `lexlean` (its integration tests run the binary) before it runs a @@ -1040,7 +1148,6 @@ jobs: include: - { os: ubuntu-24.04-arm, target: aarch64-unknown-linux-gnu } - { os: macos-15, target: aarch64-apple-darwin } - - { os: windows-2022, target: x86_64-pc-windows-msvc } runs-on: ${{ matrix.os }} # Keep the existing bound; scheduling changes do not extend it. timeout-minutes: 240 @@ -1161,3 +1268,72 @@ jobs: path: ${{ runner.temp }}/intel-evidence if-no-files-found: error retention-days: 14 + + # Windows x86-64 as lanes of one run (§8.3): its one `cargo test` took 106 + # to 126 minutes, the longest job of the gate, almost all of it the + # conformance harness. Each lane is its own clean checkout and build; the + # conformance lanes run the cases of the Intel lanes' partition + # (`.github/intel-host-lanes.json`), and `producer` runs every other test + # and doctest of the workspace. Each lane records what the host listed and + # what ran, and the join requires every lane of this run and commit, lanes + # that are disjoint, and an execution equal to the host's whole listing, + # before the host's context passes. + windows-part: + name: Windows tests (${{ matrix.lane }}) + needs: [plan, honesty] + if: ${{ needs.plan.outputs.full == 'true' }} + strategy: + fail-fast: false + matrix: + lane: [producer, lower, print, validate, moves] + runs-on: windows-2022 + timeout-minutes: 240 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - name: Install the pinned Rust toolchain + run: rustup toolchain install + - name: The runner is the host this leg claims + shell: bash + run: | + set -euo pipefail + host="$(rustc -vV | sed -n 's/^host: //p')" + if [ "$host" != x86_64-pc-windows-msvc ]; then + echo "::error::windows-2022 is $host, not x86_64-pc-windows-msvc (SPEC.md §8.3)" + exit 1 + fi + echo "$host" + - name: Execute this lane and record its inventory + shell: bash + run: python .github/scripts/host_lanes.py execute --target x86_64-pc-windows-msvc --lane ${{ matrix.lane }} --output "$RUNNER_TEMP/host-lane" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + if: ${{ always() }} + with: + name: host-x86_64-pc-windows-msvc-${{ matrix.lane }} + path: ${{ runner.temp }}/host-lane + if-no-files-found: error + retention-days: 14 + + # The Windows host status: every lane's receipt, joined. It runs even after + # a failure, so a missing lane reads as a failure and never as success. + windows-join: + name: build and non-Lean tests (x86_64-pc-windows-msvc) + needs: [plan, windows-part] + if: ${{ always() && needs.plan.outputs.full == 'true' }} + runs-on: ubuntu-24.04 + timeout-minutes: 15 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: host-x86_64-pc-windows-msvc-* + path: ${{ runner.temp }}/host-lanes + - name: Verify every lane and the complete Windows inventory + run: python3 .github/scripts/host_lanes.py join --target x86_64-pc-windows-msvc --artifacts "$RUNNER_TEMP/host-lanes" + - name: All Windows lanes succeeded + if: ${{ always() }} + shell: bash + env: + PART_RESULT: ${{ needs.windows-part.result }} + run: | + set -euo pipefail + test "$PART_RESULT" = success diff --git a/.github/workflows/regenerate.yml b/.github/workflows/regenerate.yml new file mode 100755 index 0000000000..e639c89868 --- /dev/null +++ b/.github/workflows/regenerate.yml @@ -0,0 +1,299 @@ +# The write recipes of SPEC.md §9.2 on a runner (§9.4): every file the gate +# compares with a committed oracle, regenerated from a clean checkout of the +# dispatched ref and returned as one binary patch. Not part of the gate. It +# reads the repository and writes nothing to it; the patch is reviewed and +# committed like any other written output (§28.3). +# +# git push origin :refs/heads/regenerate/ # or regenerate/atlas/ +# gh workflow run regenerate.yml --ref [-f atlas=true] # once on the default branch +# gh run download --name regenerated- +# git apply --index regenerated.patch +# +# A push to `regenerate/**` starts it from any commit, because GitHub +# dispatches only a workflow its default branch carries. +name: regenerate + +on: + push: + branches: ['regenerate/**'] + workflow_dispatch: + inputs: + atlas: + description: Rewrite UOR Atlas's verification records too (its whole proof, in the records job) + type: boolean + default: false + +permissions: + contents: read + +concurrency: + group: regenerate-${{ github.ref }} + cancel-in-progress: true + +jobs: + # Each writer runs from its own clean checkout, so the three run at once; + # the paths they write are disjoint, which `combine` checks. + writer: + name: write (${{ matrix.writer }}) + if: github.repository == 'UOR-Foundation/LexLean' + strategy: + fail-fast: false + matrix: + writer: [production-export, records, oracles] + runs-on: ubuntu-24.04 + timeout-minutes: 360 + env: + # The profile of the `examples` leg (§22.11): a schedule, never a byte + # of what is written, which is that of width 1. + LEXLEAN_VERIFY_WIDTH: "2" + LEXLEAN_VERIFY_MEMORY_KIB: "12582912" + LEXLEAN_VERIFY_HINTS: ${{ github.workspace }}/.github/verify-hints.json + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install the pinned Rust toolchain + run: rustup toolchain install + - uses: taiki-e/install-action@b6b84cf49ebfe0176417bdce007c624f0db37f20 # v2.86.2 + with: + tool: just + - name: Install elan and the pinned Lean toolchain + run: | + set -euo pipefail + curl https://elan.lean-lang.org/elan-init.sh -sSf | sh -s -- -y --default-toolchain none + echo "$HOME/.elan/bin" >> "$GITHUB_PATH" + "$HOME/.elan/bin/elan" toolchain install "$(cat lean-toolchain)" + - name: Reserve bounded overflow memory for the proofs + run: | + set -euo pipefail + sudo fallocate -l 4G "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo chmod 600 "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo mkswap "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo swapon "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo sysctl vm.swappiness=10 + - name: Run the write recipes + shell: bash + env: + ATLAS: ${{ inputs.atlas || startsWith(github.ref_name, 'regenerate/atlas/') }} + run: | + set -euo pipefail + case "${{ matrix.writer }}" in + production-export) recipes="production-export-write" ;; + records) + recipes="verify-write" + if [ "$ATLAS" != "true" ]; then + export LEXLEAN_EXAMPLES_PART=without:uor-atlas + fi + ;; + oracles) recipes="golden-write fixtures-write calculus-write model-write" ;; + esac + just --timestamp --timestamp-format '%s%.3f' $recipes + - name: Capture what the recipes wrote + shell: bash + run: | + set -euo pipefail + mkdir -p "$RUNNER_TEMP/part" + git add --all + git diff --cached --binary > "$RUNNER_TEMP/part/${{ matrix.writer }}.patch" + git diff --cached --name-only | tee "$RUNNER_TEMP/part/${{ matrix.writer }}.files" + echo "${{ matrix.writer }}: $(wc -l < "$RUNNER_TEMP/part/${{ matrix.writer }}.files") paths" >> "$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: regenerate-part-${{ matrix.writer }} + path: ${{ runner.temp }}/part + if-no-files-found: error + retention-days: 7 + + # The parts applied in turn to a clean checkout of the same commit: two parts + # that write one path fail here, rather than one silently replacing the other. + combine: + name: combine + needs: [writer] + runs-on: ubuntu-24.04 + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: regenerate-part-* + path: ${{ runner.temp }}/parts + merge-multiple: true + - name: Apply every part + shell: bash + run: | + set -euo pipefail + for part in production-export records oracles; do + # A writer that changed nothing returns an empty patch. + if [ -s "$RUNNER_TEMP/parts/$part.patch" ]; then + git apply --index "$RUNNER_TEMP/parts/$part.patch" + fi + done + mkdir -p "$RUNNER_TEMP/combined" + git diff --cached --binary > "$RUNNER_TEMP/combined/combined.patch" + echo "combined: $(git diff --cached --name-only | wc -l) paths" >> "$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: regenerate-combined + path: ${{ runner.temp }}/combined + if-no-files-found: error + retention-days: 7 + + # What follows from the regenerated tree: the counts the bootstrap count + # cases assert (`cargo xtask counts`, from runs of their parts under + # `LEXLEAN_COUNTS=fast`), the model documents, and the stack bound of each + # recursive package the record lacks (PD-08), measured by the cases that run + # it. A measuring run is a whole run of its case, so it also checks the + # counts just written. + finish: + name: counts and stack bounds + needs: [combine] + runs-on: ubuntu-24.04 + timeout-minutes: 360 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install the pinned Rust toolchain + run: rustup toolchain install + - name: Install elan and the pinned Lean toolchain + run: | + set -euo pipefail + curl https://elan.lean-lang.org/elan-init.sh -sSf | sh -s -- -y --default-toolchain none + echo "$HOME/.elan/bin" >> "$GITHUB_PATH" + "$HOME/.elan/bin/elan" toolchain install "$(cat lean-toolchain)" + - name: Reserve bounded overflow memory for the proofs + run: | + set -euo pipefail + sudo fallocate -l 4G "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo chmod 600 "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo mkswap "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo swapon "$RUNNER_TEMP/lexlean-regenerate.swap" + sudo sysctl vm.swappiness=10 + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: regenerate-combined + path: ${{ runner.temp }}/combined + - name: Apply the regenerated tree + shell: bash + run: | + set -euo pipefail + if [ -s "$RUNNER_TEMP/combined/combined.patch" ]; then + git apply --index "$RUNNER_TEMP/combined/combined.patch" + fi + - name: Write the moved counts + shell: bash + run: | + set -uo pipefail + mkdir -p "$RUNNER_TEMP/regenerated" + # The parts of `.github/bdd-parts.json` that hold a case with a + # `crate::counts!` check; each runs only up to its counts. + parts=$(python3 - <<'PY' + import json, pathlib, re + ids = set() + for path in pathlib.Path("crates/conformance/src").rglob("*.rs"): + ids |= set(re.findall(r'counts!\(\s*"([A-Z]+-[0-9]+)"', path.read_text())) + plan = json.loads(pathlib.Path(".github/bdd-parts.json").read_text()) + parts = plan["parts"] if "parts" in plan else plan + print(" ".join(sorted(name for name, members in parts.items() + if isinstance(members, list) and ids & set(members)))) + PY + ) + if [ -z "$parts" ]; then + echo "no case of this tree has a \`crate::counts!\` check: no counts written" | tee -a "$GITHUB_STEP_SUMMARY" + exit 0 + fi + echo "parts with counts: $parts" + for part in $parts; do + LEXLEAN_COUNTS=fast LEXLEAN_BDD_PART="$part" cargo xtask bdd > "$RUNNER_TEMP/fast-$part.log" 2>&1 + echo "$part: exit $?" + done + cargo xtask counts --write 2>&1 | grep -v -E '^\s*(Finished|Running|Compiling)' | tee "$RUNNER_TEMP/regenerated/counts.txt" + test "${PIPESTATUS[0]}" -eq 0 + - name: Write the model documents + run: cargo xtask validate-model --write + - name: Measure the stack bound of each recursive package the record lacks + shell: bash + env: + LEXLEAN_VERIFY_WIDTH: "2" + LEXLEAN_VERIFY_MEMORY_KIB: "12582912" + run: | + set -euo pipefail + # Rows for packages no longer in the exports are dropped; for each + # recursive package without a row, the case whose source names its + # root runs whole and measures it (`LEXLEAN_MEASURE_STACK=all`). + cases=$(python3 - <<'PY' + import json, pathlib, re + record_path = pathlib.Path("tests/stack-bounds.json") + record = json.loads(record_path.read_text()) + recursive = set() + for corpus in ("bootstrap", "stdlib"): + for provenance in pathlib.Path("tests/production-export", corpus, "packages").rglob("provenance.json"): + if "recursion" in json.loads(provenance.read_text()).get("effects", []): + recursive.add(f"{corpus}/{provenance.parent.name}") + rows = {key for key in record["rows"] if key != "bootstrap/evaluator"} + for stale in sorted(rows - recursive): + print(f"dropping the row of {stale}", file=__import__("sys").stderr) + del record["rows"][stale] + if rows - recursive: + record_path.write_text(json.dumps(record, indent=2) + "\n") + sources = list(pathlib.Path("crates/conformance/src/cases").rglob("*.rs")) + cases = set() + for package in sorted(recursive - rows): + name = package.split("/", 1)[1] + root = name.split("_")[0] + "".join(part.title() for part in name.split("_")[1:]) + owners = set() + for source in sources: + text = source.read_text() + # The file's first line names its own case. + own = re.match(r'//! `([A-Z]+-[0-9]+)`', text) + if f'"{root}"' in text and own: + owners.add(own.group(1)) + if not owners: + raise SystemExit(f"{package}: no case names the root `{root}`; measure it by hand") + print(f"{package}: measured by {', '.join(sorted(owners))}", file=__import__("sys").stderr) + cases |= owners + print(" ".join("conformance_" + case.lower().replace("-", "_") for case in sorted(cases))) + PY + ) + if [ -z "$cases" ]; then + echo "every recursive package has its row" | tee -a "$GITHUB_STEP_SUMMARY" + exit 0 + fi + echo "measuring: $cases" | tee -a "$GITHUB_STEP_SUMMARY" + LEXLEAN_MEASURE_STACK=all cargo test -p repo-conformance --all-features --test conformance -- $cases --nocapture 2>&1 \ + | grep -E --line-buffered 'debug [0-9]+ bytes|test result|panicked|^test ' + - name: Return the regenerated tree + if: ${{ !cancelled() }} + shell: bash + run: | + set -euo pipefail + out="$RUNNER_TEMP/regenerated" + mkdir -p "$out" + git add --all + git diff --cached --binary > "$out/regenerated.patch" + git diff --cached --name-only > "$out/regenerated-files.txt" + echo "$GITHUB_SHA" > "$out/base-commit.txt" + (cd "$out" && sha256sum regenerated.patch regenerated-files.txt base-commit.txt > SHA256SUMS) + { + echo "### Regenerated $(wc -l < "$out/regenerated-files.txt") paths on $GITHUB_SHA" + echo + if [ -s "$out/counts.txt" ]; then + echo '#### Counts (the writer lists here anything it could not place)' + echo '```text' + cat "$out/counts.txt" + echo '```' + fi + echo '```bash' + echo "gh run download $GITHUB_RUN_ID --repo $GITHUB_REPOSITORY --name regenerated-$GITHUB_SHA" + echo "git apply --index regenerated.patch" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + if: ${{ !cancelled() }} + with: + name: regenerated-${{ github.sha }} + path: ${{ runner.temp }}/regenerated + if-no-files-found: error + retention-days: 7 diff --git a/CHANGELOG.md b/CHANGELOG.md index 08322fa32a..20399e3a34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,20 @@ workspace carried at those versions without a tag. `SurfaceTooling` return what `surface::tooling::print_module`, `parse_module`, and `declaration_spans` return; the printer admits `recursion` and the other two do not. The register is 484 IDs. +- `cargo xtask check-freshness` (SPEC.md §9.4), run in the honesty job before the expensive roots: + every input a committed build oracle records still hashes to its recorded digest, every lock + passes `lexlean lock --check`, and every committed export package names this compiler's + semantics and its project's committed semantic ID. Stale generated files fail in minutes. +- CI exports the bootstrap project once per run (SPEC.md §9.4): the `bootstrap export` job + runs `cargo xtask export-bootstrap` at the `examples` leg's profile, requires the committed + packages, and uploads the bundle with a custody record (`lexlean/ci-bootstrap-export/1`); a + leg takes it when a case first needs it, and the harness checks the record against its own + run and checkout and replays the bundle. No bootstrap part verifies the bootstrap project. +- CI runs the Windows x86-64 host's tests as five lanes of one run (SPEC.md §8.3): the four + conformance lanes of `.github/intel-host-lanes.json` and a `producer` lane with every other test + and doctest. A join on `ubuntu-24.04` keeps the context `build and non-Lean tests + (x86_64-pc-windows-msvc)` and passes only when the lanes' executed tests equal the host's + listing, each once. The single Windows job took 106-126 minutes. - Declaration IDs (SPEC.md §21.9, `SM-38`; no new diagnostic code, no change to any compiler-semantics ID). A language-1.2 build publishes `declarations/.json` (`lexlean/declarations/1`, schema @@ -46,6 +60,16 @@ workspace carried at those versions without a tag. declarations that reference one another hashed as one, and a statement ID for every theorem that omits its proof. Builds of languages 1.0 and 1.1 are unchanged. The register is 483 IDs. +- The evaluator differential of the `BS` cases runs its corpus on every processor of the host: + the calling thread and helpers from one budget the process shares, each taking the next run. + The verdict and the disagreements it reports are the sequential ones; each `bdd-bootstrap-*` + part of the gate is a single case, so three of a runner's four processors were idle. +- `regenerate.yml` (SPEC.md §9.4): a push to `regenerate/`, or a dispatch, runs the write recipes on runners, the production + export, the verification records and the other oracles as three parallel jobs, and returns one + reviewed patch, so that a change which moves committed output is no longer regenerated on a + workstation. + A fourth job then writes the moved counts of the bootstrap count cases, the model documents, + and the stack bound of each recursive package that lacks one, so the patch is the whole landing. - `rustix` 1.1.6 (RUSTSEC-2026-0486: the extended-attribute functions marked unwritten bytes as initialized for a zero-length buffer), so `cargo deny` passes; `errno` and `tempfile` resolve `windows-sys` 0.59, the version `fs4` 0.13 and `rustix` 1.1.6 both accept. diff --git a/SPEC.md b/SPEC.md index fb94dc902f..b7e6bd3a82 100644 --- a/SPEC.md +++ b/SPEC.md @@ -557,7 +557,7 @@ The Rust crate MUST build and its non-Lean tests MUST pass on: - macOS AArch64; - Windows x86-64. -`ci.yml` checks these hosts in every run that is the whole gate (§9.4). The normative full verification and reproducibility gate runs on Linux x86-64. The Linux AArch64, macOS AArch64 and Windows x86-64 legs each run one `cargo test --locked --workspace --all-features --no-fail-fast`, which builds the library and binary in the same locked, all-features test profile and reports every failing test binary rather than stopping at the first. These three legs are unchanged by the Intel macOS partition below. Each execution job MUST check the actual Rust host triple against the host it claims. Other hosts produce platform-bound attestations and MUST obey the same source, manifest, and policy formats. +`ci.yml` checks these hosts in every run that is the whole gate (§9.4). The normative full verification and reproducibility gate runs on Linux x86-64. The Linux AArch64 and macOS AArch64 legs each run one `cargo test --locked --workspace --all-features --no-fail-fast`, which builds the library and binary in the same locked, all-features test profile and reports every failing test binary rather than stopping at the first. These two legs are unchanged by the partitions below. Each execution job MUST check the actual Rust host triple against the host it claims. Other hosts produce platform-bound attestations and MUST obey the same source, manifest, and policy formats. On macOS x86-64, one construction job and one conformance execution job per committed lane partition that same complete workspace test workload. Construction MUST compile all workspace test targets once from a clean checkout with the pinned Rust toolchain, locked dependencies, all features and the default test profile, and discover the executable test targets from Cargo's machine-readable output. It MUST execute every non-conformance test target, including the `repo-conformance` library, complete `bdd` meta target and other integration targets, and all workspace doctests. It MUST record each execution's outcome and continue independent executions after a test failure. A local test or doctest failure MUST NOT prevent the conformance jobs from running after successful construction; it remains a failure of the host aggregate. @@ -565,6 +565,8 @@ All conformance execution jobs MUST run the same constructed `repo-conformance` The aggregate MUST require the construction result and exactly one successful receipt for each committed lane, reject missing, duplicate, foreign, changed, failed or incomplete evidence, and run even when a prerequisite failed or was cancelled so absence cannot become success. Its check context MUST remain exactly `build and non-Lean tests (x86_64-apple-darwin)`. The other auxiliary contexts remain exactly `build and non-Lean tests (aarch64-unknown-linux-gnu)`, `build and non-Lean tests (aarch64-apple-darwin)` and `build and non-Lean tests (x86_64-pc-windows-msvc)`. Previous-run and release evidence MUST require each of these four exact contexts once, not infer complete coverage from the number of matrix rows or accept a partition context in place of a host. The partition changes scheduling and custody only: it reduces no test, changes no assertion or corpus, and raises no host timeout. +On Windows x86-64 that same complete workspace test workload runs as lanes of one run (`.github/scripts/host_lanes.py`): the conformance lanes of `.github/intel-host-lanes.json`, each running the conformance harness with that lane's filters, and a `producer` lane running every other test target of the workspace, the `repo-conformance` library, `bdd` meta target and other integration targets among them, and every doctest. Each lane is a job of its own clean checkout that builds with the pinned toolchain, locked dependencies, all features and the default test profile, checks its host triple, and records the commit, tree, run and attempt, the harness's listing, the producer's listing of the whole workspace, and every test it executed. The aggregate, whose check context remains exactly `build and non-Lean tests (x86_64-pc-windows-msvc)`, runs even when a lane failed or was cancelled, and passes only with exactly one passing receipt for each lane of the run and commit, lanes that listed the same harness, and executed tests whose union is the whole listing with each test once. It reduces no test, changes no assertion, and raises no host timeout. + Non-UTF-8 project paths are outside language 1.0 and produce a registered environment diagnostic. ### 8.4 Cargo dependency rules @@ -693,20 +695,24 @@ Its image MUST install the pinned Rust and Lean toolchains at build, so that not **The release's evidence.** `release.yml` runs no recipe of the gate. The CI evidence a release attaches (§30.3) is a run of `ci.yml` that was the whole gate and passed every job, on the tagged commit or, when the tagged commit is a merge whose tree is the tree of its second parent, on that parent; `.github/release-gate.sh` finds it under the rule `plan` applies to a push, and refuses, naming the commits it looked at, when no such run exists. The evidence names the run, every job of it with its conclusion, and the transcript of every leg the run still retains. The gate run whole on one hosted runner exceeds the six-hour job limit (the `bdd` recipe alone takes over four hours there, and Atlas follows it), so a release gate that re-ran it would never conclude, and the run of the line has already verified the tree. -In a run that is the whole gate, the jobs run the recipes as the rows of `.github/vv-rows.json`, each from its own clean checkout: `examples`, which verifies every example but UOR Atlas; `honesty`, which runs `fmt-check model spec-links bdd` before any expensive root; `rest`, the other recipes in the order of `vv`; and one `bdd-` row for each named part of the `bdd` plan below, which runs `bdd` alone. Each leg runs `just` with its recipes, which stops at the first that fails, as `vv` does. The part of `examples` a job runs is named by its matrix row through `LEXLEAN_EXAMPLES_PART` and by nothing else: `without:uor-atlas` on the leg, `shard:uor-atlas:` and `join:uor-atlas` on the Atlas jobs below; unset or empty, which is what `just vv` runs, the recipe verifies every example whole. After honesty succeeds, the remaining rows run as one matrix and the supported-host matrix, Intel construction and Atlas prefix may start; a failed or skipped honesty part starts none of these expensive roots. Together the legs and the Atlas jobs run every recipe of `vv`, every example, and every test of `bdd` exactly once, and the run passes only when every job passes, so it proves what one run of `just vv` proves; no recipe depends on another having run, and every recipe owns the files it builds. Its wall time is the longest path through the jobs rather than the sum. `just vv` remains the acceptance recipe of §9.2 for every other run, and release acceptance requires the complete same-tree run. +In a run that is the whole gate, the jobs run the recipes as the rows of `.github/vv-rows.json`, each from its own clean checkout: `examples`, which verifies every example but UOR Atlas; `honesty`, which runs `fmt-check model spec-links bdd` before any expensive root; `rest`, the other recipes in the order of `vv`; and one `bdd-` row for each named part of the `bdd` plan below, which runs `bdd` alone. Each leg runs `just` with its recipes, which stops at the first that fails, as `vv` does. The part of `examples` a job runs is named by its matrix row through `LEXLEAN_EXAMPLES_PART` and by nothing else: `without:uor-atlas` on the leg, `shard:uor-atlas:` and `join:uor-atlas` on the Atlas jobs below; unset or empty, which is what `just vv` runs, the recipe verifies every example whole. After honesty succeeds, the remaining rows run as one matrix and the supported-host matrix, Intel construction, the bootstrap export and Atlas prefix may start; a failed or skipped honesty part starts none of these expensive roots. Together the legs and the Atlas jobs run every recipe of `vv`, every example, and every test of `bdd` exactly once, and the run passes only when every job passes, so it proves what one run of `just vv` proves; no recipe depends on another having run, and every recipe owns the files it builds. Its wall time is the longest path through the jobs rather than the sum. `just vv` remains the acceptance recipe of §9.2 for every other run, and release acceptance requires the complete same-tree run. UOR Atlas is verified as the parts of one run. `.github/verify-shards.json` partitions its verification into shards, each naming the shards it needs, the modules it elaborates, and the modules it replays and audits, one of them named `join`; `cargo xtask ci-shards` checks, as the verifier would when a part ran, that every module is elaborated by exactly one shard and replayed by exactly one, that every import of a module a shard elaborates or replays is elaborated by that shard or by one it needs, directly or through another, and that `join` needs every other shard. Each shard is a job of its own clean checkout that runs the verifier's prelude whole (certificate generation, the preflights, staging, the generated-source audit, the probe), then elaborates, replays, and audits only the modules of its plan, reading the `.olean` files of the modules it imports from the output the jobs of the shards it needs uploaded in the same run, and uploads its own `.olean` files and process records as its output. The join runs the modules of its plan, reads every shard's records, requires each to name the executable the join itself hashed for the tool that produced it (`lean` for an elaboration or an audit member, `leanchecker` for a replay), judges each as the stage that produced it judges its own, and continues through policy, the attestation over every record in the canonical order of §22.11, publication, and the comparison of every normalized record with `expected/verify` (§29.5): what it publishes is what the whole verification publishes, record for record and attestation identity included (`VR-23`). A module without a record from any part, a record of a process no module owns, a record whose `executable_sha256` is not the join's digest of its tool (`LLI9001`, naming the record and both digests), or a shard whose plan leaves a module without the `.olean` it imports fails the run. Every process of the verification runs exactly once per run. The executable the Atlas jobs run is the one the `prefix` job built from its clean checkout, carried to the other jobs of the run as an artifact beside the shards' output; it, the output, and the records cross no run, and a job reads the artifacts of its own run only. The Atlas jobs run after the jobs of the shards they need, at the profile of the `examples` leg, and only in a run that is the whole gate. The `bdd` recipe runs as the parts of one run as well. `.github/bdd-parts.json` names parts by conformance ID, complete integration-test targets in `targets`, and a remainder, `rest`. Cases that share a fixture built once per test process belong to one part. The `honesty` part owns the complete `bdd` meta-gate target, including any tests added to that target; no copied test-name list selects it. `cargo xtask bdd` runs `cargo test -p repo-conformance --all-features` with the Cargo target selectors and harness filters of `LEXLEAN_BDD_PART`. A target part selects its targets through `--test`; an ID part uses `--exact` and the test of each ID. The remainder discovers all integration-test targets from Cargo metadata, selects every target no target part owns, and keeps the library and binary tests and doctests; Cargo requires doctests in a separate invocation when target selectors are present. Its `--exact` and `--skip` filters omit every conformance test an ID part owns. A new test in a target belongs to that target's part; a new integration target and a new conformance ID belong to the remainder until assigned. No case decides whether it runs. `inventory` lists the tests using the same selectors and filters, proves disjointness and equality with the whole `bdd` suite, and requires a target part to own every test of its target (§9.2). Exactly one row runs every part, the remainder included. The initial honesty row names its part; each later `bdd-` row and the `rest` row name theirs. Unset or empty, as in local `just vv`, the recipe runs the whole suite unchanged. Each row builds from its own clean checkout; no build or evidence crosses runs. -Every normative gate job runs on the standard GitHub-hosted runner, `ubuntu-24.04`; no larger runner is part of the gate. The supported-host execution jobs run on their named hosts (§8.3); the Intel aggregate runs on `ubuntu-24.04` and executes no host test. The verification resource profile (§22.11) of a job is named by its matrix row, or on an Atlas job by the job itself, and by nothing else, no repository or workflow variable and no input: the `examples` leg and every Atlas job verify at width 2, with the scheduling hints committed in `.github/verify-hints.json` (derived from a run's telemetry by `cargo xtask ci-hints`) and a memory budget of 12 GiB (12582912 KiB) for the summed expected peaks of the proof processes running at once; the other legs name no profile and run at the default. A profile is a schedule and never a verdict: every elaboration, replay, and audit member still runs on the pinned toolchain, the directory each project publishes is byte for byte the one of width 1 without hints or a budget (§22.11), and the leg and the join still require every example's normalized verification records to equal `expected/verify` (§29.5), so a profile that changed a byte would fail the gate rather than pass a different one. A stale hint reorders processes and never skips one. Local `just vv` verifies UOR Atlas whole at width 1 with no profile. The budget is below the runner's 16 GB, with the 4 GiB swap file of the job as overflow, so two processes run together only when their expected peaks fit, and a process whose expected peak alone passes the budget runs alone. The gate has no path filter, no cache of a build directory, object, verification tree, attestation, or proof result, and no condition that skips a recipe of `just vv` but the plan's, which decides only what a run is, as above; the artifacts the jobs of one run exchange are read by that run alone, and every job starts from a clean checkout with the exact pinned toolchains. Each proof or fan-out job writes its performance telemetry (§22.12) to `ci-evidence/` and uploads that directory as the artifact named after it, `acceptance-gate-evidence-` or `acceptance-gate-evidence-atlas-`: the gate log (`just --timestamp` stamps each recipe's first command on a leg, which is how a recipe is timed without changing it; an Atlas job's log is the recipe's own output), the host's memory and swap sampled every five seconds, the verifier's telemetry, and a summary of the wall time of each recipe and verification stage, the slowest modules and audit members, and the peak resident set and swap of the proof processes. The summary step also prints that summary to the job log and appends it to the step summary, and runs when the job failed, because an uploaded artifact cannot be read from the log; each table lists only its slowest rows and the part appended to the step summary is cut at 512 KiB, half of what GitHub accepts. The initial honesty job retains its recipe timing and summary in the same evidence format, without Lean processes or proof-resource samples. None of it is an input of any recipe, and a timing is never a claim about a semantic property. +Every normative gate job runs on the standard GitHub-hosted runner, `ubuntu-24.04`; no larger runner is part of the gate. The supported-host execution jobs run on their named hosts (§8.3); the Intel and Windows aggregates run on `ubuntu-24.04` and execute no host test. The verification resource profile (§22.11) of a job is named by its matrix row, or on an Atlas job by the job itself, and by nothing else, no repository or workflow variable and no input: the `examples` leg and every Atlas job verify at width 2, with the scheduling hints committed in `.github/verify-hints.json` (derived from a run's telemetry by `cargo xtask ci-hints`) and a memory budget of 12 GiB (12582912 KiB) for the summed expected peaks of the proof processes running at once; the other legs name no profile and run at the default. A profile is a schedule and never a verdict: every elaboration, replay, and audit member still runs on the pinned toolchain, the directory each project publishes is byte for byte the one of width 1 without hints or a budget (§22.11), and the leg and the join still require every example's normalized verification records to equal `expected/verify` (§29.5), so a profile that changed a byte would fail the gate rather than pass a different one. A stale hint reorders processes and never skips one. Local `just vv` verifies UOR Atlas whole at width 1 with no profile. The budget is below the runner's 16 GB, with the 4 GiB swap file of the job as overflow, so two processes run together only when their expected peaks fit, and a process whose expected peak alone passes the budget runs alone. The gate has no path filter, no cache of a build directory, object, verification tree, attestation, or proof result, and no condition that skips a recipe of `just vv` but the plan's, which decides only what a run is, as above; the artifacts the jobs of one run exchange are read by that run alone, and every job starts from a clean checkout with the exact pinned toolchains. Each proof or fan-out job writes its performance telemetry (§22.12) to `ci-evidence/` and uploads that directory as the artifact named after it, `acceptance-gate-evidence-` or `acceptance-gate-evidence-atlas-`: the gate log (`just --timestamp` stamps each recipe's first command on a leg, which is how a recipe is timed without changing it; an Atlas job's log is the recipe's own output), the host's memory and swap sampled every five seconds, the verifier's telemetry, and a summary of the wall time of each recipe and verification stage, the slowest modules and audit members, and the peak resident set and swap of the proof processes. The summary step also prints that summary to the job log and appends it to the step summary, and runs when the job failed, because an uploaded artifact cannot be read from the log; each table lists only its slowest rows and the part appended to the step summary is cut at 512 KiB, half of what GitHub accepts. The initial honesty job retains its recipe timing and summary in the same evidence format, without Lean processes or proof-resource samples. None of it is an input of any recipe, and a timing is never a claim about a semantic property. + +The bootstrap production export (§24.7) is made once per run. Every bootstrap case compares the packages of the bootstrap project's export with the committed packages, and a test process that needs the export exports the project, which verifies it whole; each part that ran a bootstrap case ran that verification. In a run that is the whole gate, the job `bootstrap-export` runs `cargo xtask export-bootstrap` from its clean checkout after honesty, at the profile of the `examples` leg, requires every exported package to equal the committed package, and uploads the bundle as the artifact `bootstrap-export` with a custody record (`lexlean/ci-bootstrap-export/1`) that names the repository, the run and its attempt, the commit and its tree, the SHA-256 of `rust-toolchain.toml`, `lean-toolchain` and `Cargo.lock`, what `rustc -vV` and `lean --version` report, the bundle's project-relative path, and the digest of its files. The legs do not wait for that job to start: each leg of such a run starts `.github/scripts/take-bootstrap-export.sh`, which asks nothing until a case of the leg needs the export, then downloads that artifact of its own run, or records that the export job ended without it, and the case fails with that reason. The harness names the directory it waits in through `LEXLEAN_BOOTSTRAP_EXPORT`; before any case reads the bundle it requires the record to name the leg's own repository, run, commit, tree, pins and toolchains, an attempt at most the leg's (a rerun of failed jobs keeps the export that passed, on the same commit), the digest of the files it received, and a path below the project, and it replays the bundle (§24.7). A record that differs in any of these fails the case. Unset or empty, as in `just vv` and in a run that is not the whole gate, each process that needs the export runs it itself. The export crosses no run, as the Atlas parts do not; `RP-14` checks the job, the legs' fetcher, and the record against planted changes. The Intel host factory is separate from the normative BDD and Atlas partitions above: it transfers only the same-run host test construction and dependency data of §8.3. Construction requires successful honesty; consumers require construction; the aggregate requires all of them and cannot skip an incomplete result. The factory's behavioral tests run in the initial honesty job, before the expensive roots, and exercise inventory completeness, custody and fail-closed result joins without needing an Intel runner. These tests do not substitute for execution of the complete workload on Intel macOS. -`ci.yml` MUST expose the initial `honesty` job (retaining the previous check context) separately so claim drift is distinguishable from code failure. It runs the existing format, model and specification-link recipes and the complete BDD meta-gate target, without Lean. The remaining rows, supported hosts and Atlas prefix MUST require its success; these recipes and meta tests run once in the normative gate. Both the plan's previous-run evidence and release evidence MUST require the honesty row, including its successful conclusion; missing, failed or skipped honesty never establishes full acceptance. The separate `honesty.yml` workflow is subsumed by this same-run job. +`ci.yml` MUST expose the initial `honesty` job (retaining the previous check context) separately so claim drift is distinguishable from code failure. It runs the existing format, model and specification-link recipes and the complete BDD meta-gate target, without Lean. Before them it runs `cargo xtask check-freshness`, also without Lean: every input a committed build oracle (`expected/build/manifest.json`) records still has its recorded digest and length, every such project's lock passes `lexlean lock --check`, every oracle and every committed package under `tests/production-export/` names this compiler's semantics, and every such package names its project's committed semantic ID. A tree committed without regenerating what its change moves fails there in minutes, before the expensive roots; the check adds a precondition and replaces no check of the gate, which still compares every generated byte. The remaining rows, supported hosts, the bootstrap export and Atlas prefix MUST require its success; these recipes and meta tests run once in the normative gate. Both the plan's previous-run evidence and release evidence MUST require the honesty row, including its successful conclusion; missing, failed or skipped honesty never establishes full acceptance. The separate `honesty.yml` workflow is subsumed by this same-run job. `reproducibility.yml` MUST perform two builds in distinct absolute directories and compare every platform-independent artifact byte-for-byte after path normalization. +`regenerate.yml` is not part of the gate: it runs the write recipes of §9.2 on runners, so that a change which moves committed output is regenerated from a clean checkout rather than on a workstation. A push to a branch under `regenerate/`, or a `workflow_dispatch` on a ref, starts three jobs at once, each from its own clean checkout of that commit: `production-export-write`; `verify-write` over every example but UOR Atlas, or over every example when the branch is under `regenerate/atlas/` or the `atlas` input is set; and `golden-write fixtures-write calculus-write model-write`. They verify with the profile of the `examples` leg, which changes no byte of what is written (§22.11). Each returns what its recipes wrote as a binary patch, and the job `combine` applies the patches in turn to a clean checkout of the same commit, failing when two write one path. The job `finish` then applies that tree and writes what follows from it: the counts the bootstrap count cases assert, by `cargo xtask counts --write` over runs of the parts of `.github/bdd-parts.json` that hold a `crate::counts!` check under `LEXLEAN_COUNTS=fast`; the model documents, by `validate-model --write`; and the row of `tests/stack-bounds.json` of each recursive package of the bootstrap and stdlib exports that has none (`PD-08`), measured by a whole run under `LEXLEAN_MEASURE_STACK=all` of the case whose source names the package's root, which also checks the counts just written, while a row of a package no longer exported is dropped. It returns the result as one patch with the list of its paths, the counts the writer reported, and the commit it applies to. The workflow reads the repository and writes nothing to it (`contents: read`, no persisted credentials); its patch is reviewed and committed like any other written output (§28.3), and the gate then verifies the tree that carries it. + --- ## 10. Project files diff --git a/crates/conformance/src/cases/bootstrap.rs b/crates/conformance/src/cases/bootstrap.rs index 2f7102a7a8..ac797da29e 100644 --- a/crates/conformance/src/cases/bootstrap.rs +++ b/crates/conformance/src/cases/bootstrap.rs @@ -980,30 +980,119 @@ fn readable(value: &Json) -> String { } /// Every run of `corpus` on which the evaluator and the oracle disagree, up -/// to `limit`, each as the input and the two outcomes. +/// to `limit`, each as the input and the two outcomes: the first `limit` in +/// the corpus's order, whichever thread ran them. +/// +/// The runs are independent, so they run on the calling thread and on as +/// many helpers as the shared budget of [`helpers`] allows, each taking the +/// next run in order. When one has found `limit` disagreements no run is +/// taken after it; every run before the last one taken still finishes, so +/// the first `limit` disagreements are among those run and the answer is the +/// sequential one. When the cases measure the evaluator's stack (`PD-08`) +/// every run is on the calling thread, whose stack is the one measured. fn evaluate(lowered: &Lowered, corpus: &[Run], limit: usize) -> Vec { - let mut out = Vec::new(); - for (run, (arguments, expected)) in corpus.iter().enumerate() { + use std::sync::atomic::{AtomicUsize, Ordering}; + + let disagreement = |run: usize| -> Option { + let (arguments, expected) = &corpus[run]; let outcome = interp::run(&lowered.program, FUEL, lowered.entry(), arguments); let observed = rust::observable(&outcome) .unwrap_or_else(|| panic!("run {run} on {arguments:?}: {outcome:?}")); - if &observed != expected { + (&observed != expected).then(|| { let input: Vec = arguments .iter() .map(|argument| readable(&serde_json::to_value(argument).expect("JSON"))) .collect(); - out.push(format!( + format!( "run {run} on {}: the program gives {}, the oracle {}", input.join(" "), readable(&observed), readable(expected) - )); - if out.len() >= limit { + ) + }) + }; + let next = AtomicUsize::new(0); + let found = AtomicUsize::new(0); + let work = || { + let mut out = Vec::new(); + while found.load(Ordering::SeqCst) < limit { + let run = next.fetch_add(1, Ordering::SeqCst); + if run >= corpus.len() { break; } + if let Some(text) = disagreement(run) { + found.fetch_add(1, Ordering::SeqCst); + out.push((run, text)); + } + } + out + }; + let wanted = if crate::stack_bounds::measuring().is_some() { + 0 + } else { + corpus.len().saturating_sub(1) + }; + let permits = helpers::take(wanted); + let mut out = std::thread::scope(|scope| { + let spawned: Vec<_> = (0..permits.0) + .map(|_| { + std::thread::Builder::new() + .stack_size(EVALUATOR_STACK) + .spawn_scoped(scope, work) + .expect("an evaluator thread") + }) + .collect(); + let mut out = work(); + for helper in spawned { + match helper.join() { + Ok(more) => out.extend(more), + Err(failure) => std::panic::resume_unwind(failure), + } } + out + }); + drop(permits); + out.sort_by_key(|(run, _)| *run); + out.truncate(limit); + out.into_iter().map(|(_, text)| text).collect() +} + +/// The helper threads every [`evaluate`] of this process shares: one fewer +/// than the host's processors, because the threads that call it, the test +/// harness's, already run, and a part of the `bdd` recipe may run several +/// cases at once. Taking never waits: a call takes what is free, and the +/// calling thread alone runs every run when nothing is. +mod helpers { + use std::sync::Mutex; + + static FREE: Mutex> = Mutex::new(None); + + fn budget() -> usize { + std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get) - 1 + } + + /// Helpers taken from the budget, returned when dropped, on a panic too. + pub(super) struct Taken(pub(super) usize); + + impl Drop for Taken { + fn drop(&mut self) { + let mut free = FREE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + *free.get_or_insert_with(budget) += self.0; + } + } + + /// Up to `wanted` helpers, as many as are free. + pub(super) fn take(wanted: usize) -> Taken { + let mut free = FREE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let available = free.get_or_insert_with(budget); + let taken = wanted.min(*available); + *available -= taken; + Taken(taken) } - out } /// What the lowered root returns for `arguments`, as the oracle's values are diff --git a/crates/conformance/src/cases/repository.rs b/crates/conformance/src/cases/repository.rs index df7f0f0dee..9c227c41dd 100644 --- a/crates/conformance/src/cases/repository.rs +++ b/crates/conformance/src/cases/repository.rs @@ -1552,7 +1552,7 @@ pub(crate) fn run(id: &str) { for (key, body) in &jobs { if matches!( key.as_str(), - "host-matrix" | "intel-construction" | "intel-part" + "host-matrix" | "intel-construction" | "intel-part" | "windows-part" ) { continue; } @@ -1562,7 +1562,7 @@ pub(crate) fn run(id: &str) { ); // The plan decides what the run is and runs no recipe: it has // ten minutes, not the six hours of the proof jobs. - if key == "intel-join" { + if key == "intel-join" || key == "windows-join" { let limit = body .lines() .find_map(|line| line.trim().strip_prefix("timeout-minutes: ")) @@ -2074,7 +2074,7 @@ pub(crate) fn run(id: &str) { let mut honesty_conditions = 0; for (index, line) in jobs .iter() - .filter(|(key, _)| !key.starts_with("intel-")) + .filter(|(key, _)| !key.starts_with("intel-") && !key.starts_with("windows-")) .flat_map(|(_, body)| body.lines()) .enumerate() { @@ -2093,17 +2093,42 @@ pub(crate) fn run(id: &str) { } assert_eq!( step_conditions, - 4 + 2 * atlas_jobs.len(), + 6 + 2 * atlas_jobs.len(), "the summary step and the evidence upload of every gate job carry it" ); assert_eq!( full_conditions, - atlas_jobs.len() + 1, - "every Atlas job and the host matrix, and no other job, wait for the plan to name the whole gate" + atlas_jobs.len() + 2, + "every Atlas job, the bootstrap export, and the host matrix, and no other job, wait for the plan to name the whole gate" ); assert_eq!(rows_conditions, 1, "the legs run when the plan names a leg"); assert_eq!(honesty_conditions, 1, "honesty runs when the plan names it"); intel_host_workflow(&ci).expect("§8.3: the host inventory and Intel custody graph"); + windows_host_workflow(&ci).expect("§8.3: the Windows lanes and their join"); + for (from, to) in [ + ("lane: [producer, lower, print, validate, moves]", "lane: [producer, lower, print, validate]"), + (" needs: [plan, windows-part]", " needs: [plan]"), + ( + " if: ${{ always() && needs.plan.outputs.full == 'true' }}\n runs-on: ubuntu-24.04\n timeout-minutes: 15\n steps:\n - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1\n - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2\n with:\n pattern: host-x86_64", + " if: ${{ needs.plan.outputs.full == 'true' }}\n runs-on: ubuntu-24.04\n timeout-minutes: 15\n steps:\n - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1\n - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2\n with:\n pattern: host-x86_64", + ), + ("host_lanes.py join --target", "host_lanes.py execute --target"), + ( + "windows-part.result }}\n run: |\n set -euo pipefail\n test \"$PART_RESULT\" = success", + "windows-part.result }}\n run: |\n set -euo pipefail\n test \"$PART_RESULT\" != cancelled", + ), + (" runs-on: windows-2022\n", " runs-on: windows-2025\n"), + ( + "python3 -m unittest discover -s .github/scripts -p 'test_host_lanes.py'", + "echo lane checks", + ), + ] { + assert!(ci.contains(from), "the plant must change the workflow: {from}"); + assert!( + windows_host_workflow(&ci.replacen(from, to, 1)).is_err(), + "the Windows lanes must refuse `{from}` replaced by `{to}`" + ); + } for (from, to) in [ ( "name: build and non-Lean tests (x86_64-apple-darwin)", @@ -2145,13 +2170,92 @@ pub(crate) fn run(id: &str) { "the Intel graph must refuse `{from}` replaced by `{to}`" ); } + let take = root_file(TAKE_EXPORT); + bootstrap_export_workflow(&ci, &take) + .expect("§9.4: the run's one bootstrap export and its same-run custody"); + let export_job = workflow_job(&ci, "bootstrap-export").expect("the export job"); + for (from, to) in [ + (" needs: [plan, honesty]", " needs: [plan]"), + ( + "cargo xtask export-bootstrap --output \"$RUNNER_TEMP/bootstrap-export\"", + "echo exported", + ), + ( + " LEXLEAN_VERIFY_WIDTH: \"2\"", + " LEXLEAN_VERIFY_WIDTH: \"4\"", + ), + ( + " name: bootstrap-export\n", + " name: bootstrap-bundle\n", + ), + (" name: bootstrap export\n", " name: export\n"), + ] { + assert!( + export_job.contains(from), + "the plant must change the export job: {from}" + ); + let planted = ci.replacen(&export_job, &export_job.replacen(from, to, 1), 1); + assert!( + bootstrap_export_workflow(&planted, &take).is_err(), + "the bootstrap export's custody must refuse `{from}` replaced by `{to}`" + ); + } + for (from, to) in [ + ( + " FULL_GATE: ${{ needs.plan.outputs.full }}\n", + " FULL_GATE: true\n", + ), + ( + " permissions:\n contents: read\n actions: read\n # Complete proof", + " permissions:\n contents: read\n # Complete proof", + ), + ( + " export LEXLEAN_BOOTSTRAP_EXPORT=\"$RUNNER_TEMP/bootstrap-export\"\n", + " export LEXLEAN_BOOTSTRAP_EXPORT=\"$RUNNER_TEMP/elsewhere\"\n", + ), + ] { + assert!(ci.contains(from), "the plant must change the workflow: {from}"); + assert!( + bootstrap_export_workflow(&ci.replacen(from, to, 1), &take).is_err(), + "the bootstrap export's custody must refuse `{from}` replaced by `{to}`" + ); + } + for (from, to) in [ + ("run=${GITHUB_RUN_ID:?}", "run=${OTHER_RUN_ID:?}"), + ( + "artifacts?name=bootstrap-export", + "artifacts?name=bootstrap-bundle", + ), + ( + "select(.name == \"bootstrap export\")", + "select(.name == \"export\")", + ), + ("until [ -e \"$dir/wanted\" ]", "until true"), + ] { + assert!( + take.contains(from), + "the plant must change the fetcher: {from}" + ); + assert!( + bootstrap_export_workflow(&ci, &take.replacen(from, to, 1)).is_err(), + "the fetcher's custody must refuse `{from}` replaced by `{to}`" + ); + } + bootstrap_custody_decisions(); honesty_dependencies(&ci).expect("expensive roots require successful same-run honesty"); assert!( honesty_dependencies(&ci.replacen("name: ${{ matrix.part }}", "name: honesty", 1)) .is_err(), "a static matrix name acquires a suffix instead of the exact honesty context" ); - for root in ["vv", "atlas-prefix", "host-matrix", "intel-construction"] { + for root in [ + "bootstrap-export", + "vv", + "atlas-prefix", + "host-matrix", + "intel-construction", + "windows-part", + ] { let original = format!(" {root}:\n"); let pos = ci.find(&original).expect("the root"); let end = ci[pos + original.len()..] @@ -2244,6 +2348,15 @@ pub(crate) fn run(id: &str) { ); // The honesty and reproducibility workflows stay separate. let honesty = job("honesty"); + // §9.4: stale generated files fail in honesty, before the gate + // command and every expensive root. + let fresh = honesty + .find(" run: cargo xtask check-freshness\n") + .expect("§9.4: honesty checks that the committed generated files are current"); + assert!( + fresh < honesty.find("just --timestamp").expect("the gate command"), + "§9.4: the freshness check runs before the honesty recipes" + ); assert!( honesty.contains("LEXLEAN_BDD_PART: ${{ matrix.bdd_part }}") && honesty.contains("fromJSON(needs.plan.outputs.honesty)") @@ -3115,6 +3228,14 @@ fn intel_host_workflow(ci: &str) -> Result<(), String> { .find_map(|line| line.strip_prefix(" name: ")) .ok_or("RP-14: the Intel aggregate has no context")?; contexts.push(aggregate_name.to_owned()); + let windows = workflow_job(ci, "windows-join")?; + contexts.push( + windows + .lines() + .find_map(|line| line.strip_prefix(" name: ")) + .ok_or("RP-14: the Windows aggregate has no context")? + .to_owned(), + ); contexts.sort(); let mut expected = supported_host_contexts(); expected.sort(); @@ -3256,6 +3377,336 @@ fn intel_host_workflow(ci: &str) -> Result<(), String> { Ok(()) } +/// The fetcher a leg runs beside its gate command to take the run's +/// bootstrap export (§9.4). +const TAKE_EXPORT: &str = ".github/scripts/take-bootstrap-export.sh"; + +/// §9.4: one bootstrap export per run, carried to the legs of the same run. +/// The export job follows honesty and the plan's whole-gate decision, runs +/// the export at the profile of the `examples` leg, and uploads it under the +/// one name the fetcher reads; a leg reads the artifacts of its own run only, +/// takes the export only in a run that is the whole gate, and only into the +/// directory its harness waits in; the fetcher names this run and the export +/// job, and asks nothing before a case wants the export. +fn bootstrap_export_workflow(ci: &str, take: &str) -> Result<(), String> { + let job = workflow_job(ci, "bootstrap-export")?; + let legs = workflow_job(ci, "vv")?; + let require = |body: &str, text: &str, rule: &str| -> Result<(), String> { + if body.contains(text) { + Ok(()) + } else { + Err(format!("RP-14: {rule}: missing `{text}`")) + } + }; + for (text, rule) in [ + (" name: bootstrap export\n", "the job the fetcher watches"), + (" needs: [plan, honesty]\n", "the export follows honesty"), + ( + " if: ${{ needs.plan.outputs.full == 'true' }}\n", + "the export runs in a run that is the whole gate", + ), + (" LEXLEAN_VERIFY_WIDTH: \"2\"\n", "the examples leg's width"), + ( + " LEXLEAN_VERIFY_MEMORY_KIB: \"12582912\"\n", + "the examples leg's budget", + ), + ( + " LEXLEAN_VERIFY_HINTS: ${{ github.workspace }}/.github/verify-hints.json\n", + "the committed hints", + ), + ( + "cargo xtask export-bootstrap --output \"$RUNNER_TEMP/bootstrap-export\" 2>&1 | tee ci-evidence/vv.txt\n", + "the export, with the comparison and the custody record", + ), + (" name: bootstrap-export\n", "the artifact the fetcher reads"), + ( + " path: ${{ runner.temp }}/bootstrap-export\n", + "the directory the export wrote", + ), + ( + "name: acceptance-gate-evidence-bootstrap-export\n", + "the job's evidence", + ), + ] { + require(&job, text, rule)?; + } + for (text, rule) in [ + (" actions: read\n", "a leg lists the run's artifacts"), + ( + " FULL_GATE: ${{ needs.plan.outputs.full }}\n", + "a leg takes the export only in a run that is the whole gate", + ), + ( + " if [ \"$FULL_GATE\" = true ]; then\n export LEXLEAN_BOOTSTRAP_EXPORT=\"$RUNNER_TEMP/bootstrap-export\"\n .github/scripts/take-bootstrap-export.sh \"$LEXLEAN_BOOTSTRAP_EXPORT\"", + "the fetcher fills the directory the harness waits in", + ), + ] { + require(&legs, text, rule)?; + } + for body in [&job, &legs] { + for forbidden in [ + "run-id:", + "github-token:", + "actions/cache", + "continue-on-error", + ] { + if body.contains(forbidden) { + return Err(format!("RP-14: the export's custody admits `{forbidden}`")); + } + } + } + for (text, rule) in [ + ("run=${GITHUB_RUN_ID:?}", "the fetcher reads this run"), + ( + "repos/$repo/actions/runs/$run/artifacts?name=bootstrap-export", + "the fetcher reads the export job's artifact", + ), + ( + "select(.name == \"bootstrap export\")", + "the fetcher watches the export job", + ), + ( + "until [ -e \"$dir/wanted\" ]", + "the fetcher asks nothing before a case wants the export", + ), + ] { + require(take, text, rule)?; + } + if take.matches("actions/runs/").count() != 2 || take.contains("GITHUB_RUN_ID=") { + return Err("RP-14: the fetcher reads the run it is in and no other".to_owned()); + } + Ok(()) +} + +/// §9.4: a custody record names the run and checkout that take the export, +/// and every way it can differ is refused. +fn bootstrap_custody_decisions() { + use crate::export_custody::{check, Custody, Identity, SPEC}; + use std::collections::BTreeMap; + let pins: BTreeMap = [ + ("Cargo.lock", "a".repeat(64)), + ("lean-toolchain", "b".repeat(64)), + ("rust-toolchain.toml", "c".repeat(64)), + ] + .into_iter() + .map(|(name, digest)| (name.to_owned(), digest)) + .collect(); + let toolchains: BTreeMap = + [("lean", "Lean (version 4.32.1)"), ("rustc", "rustc 1.97.1")] + .into_iter() + .map(|(name, text)| (name.to_owned(), text.to_owned())) + .collect(); + let here = Identity { + repository: "UOR-Foundation/LexLean".to_owned(), + run_id: "100".to_owned(), + run_attempt: "2".to_owned(), + commit: "1".repeat(40), + tree: "2".repeat(40), + pins: pins.clone(), + toolchains: toolchains.clone(), + }; + let bundle = ("d".repeat(64), 162); + let record = Custody { + spec: SPEC.to_owned(), + repository: here.repository.clone(), + run_id: here.run_id.clone(), + run_attempt: "1".to_owned(), + commit: here.commit.clone(), + tree: here.tree.clone(), + pins, + toolchains, + bundle_path: ".lexlean/production/bundle".to_owned(), + bundle_sha256: bundle.0.clone(), + bundle_files: bundle.1, + }; + check(&record, &here, &bundle).expect("§9.4: the run's own export, from an earlier attempt"); + type Plant = Box; + let plants: Vec<(&str, Plant)> = vec![ + ("spec", Box::new(|record| record.spec.push('2'))), + ( + "repository", + Box::new(|record| record.repository = "fork/LexLean".to_owned()), + ), + ("run", Box::new(|record| record.run_id = "99".to_owned())), + ( + "attempt", + Box::new(|record| record.run_attempt = "3".to_owned()), + ), + ("commit", Box::new(|record| record.commit = "3".repeat(40))), + ("tree", Box::new(|record| record.tree = "4".repeat(40))), + ( + "pins", + Box::new(|record| { + record + .pins + .insert("lean-toolchain".to_owned(), "e".repeat(64)); + }), + ), + ( + "toolchains", + Box::new(|record| { + record + .toolchains + .insert("rustc".to_owned(), "rustc 1.97.0".to_owned()); + }), + ), + ( + "bundle", + Box::new(|record| record.bundle_sha256 = "f".repeat(64)), + ), + ("bundle", Box::new(|record| record.bundle_files += 1)), + ( + "bundle path", + Box::new(|record| record.bundle_path = "../outside".to_owned()), + ), + ( + "bundle path", + Box::new(|record| record.bundle_path = "/abs/bundle".to_owned()), + ), + ]; + for (field, plant) in plants { + let mut planted = record.clone(); + plant(&mut planted); + let refusal = check(&planted, &here, &bundle) + .expect_err("§9.4: a custody record that names another run, tree, pin, or bundle"); + assert!( + refusal.starts_with(field) && refusal.lines().count() == 1, + "the refusal names the field `{field}` alone: {refusal}" + ); + } +} + +/// §8.3: Windows runs as the lanes of one run, the conformance lanes of the +/// Intel partition and the producer, after honesty and only in a run that is +/// the whole gate; its aggregate keeps the host's context, runs after any +/// lane's failure, joins every lane's receipt, and fails on a missing one. +fn windows_host_workflow(ci: &str) -> Result<(), String> { + let part = workflow_job(ci, "windows-part")?; + let join = workflow_job(ci, "windows-join")?; + let honesty = workflow_job(ci, "honesty")?; + let require = |body: &str, text: &str, rule: &str| -> Result<(), String> { + if body.contains(text) { + Ok(()) + } else { + Err(format!("RP-14: {rule}: missing `{text}`")) + } + }; + let plan: serde_json::Value = serde_json::from_str(&root_file(".github/intel-host-lanes.json")) + .map_err(|error| format!("RP-14: the lanes file: {error}"))?; + let mut lanes = vec!["producer".to_owned()]; + lanes.extend( + plan["lanes"] + .as_object() + .ok_or("RP-14: the lanes file names its lanes")? + .keys() + .cloned(), + ); + lanes.sort(); + let mut matrix: Vec = part + .lines() + .find_map(|line| line.strip_prefix(" lane: [")) + .and_then(|rest| rest.strip_suffix(']')) + .ok_or("RP-14: the Windows matrix names its lanes")? + .split(',') + .map(|lane| lane.trim().to_owned()) + .collect(); + matrix.sort(); + if matrix != lanes { + return Err(format!( + "RP-14: the Windows matrix runs {matrix:?}, the producer and the lanes of the partition are {lanes:?}" + )); + } + for (body, text, rule) in [ + (&part, " needs: [plan, honesty]\n", "the lanes follow honesty"), + ( + &part, + " if: ${{ needs.plan.outputs.full == 'true' }}\n", + "the lanes run in a run that is the whole gate", + ), + (&part, " fail-fast: false\n", "a failing lane stops no other"), + (&part, " runs-on: windows-2022\n", "the lanes run on the host"), + (&part, " run: rustup toolchain install\n", "the Rust pin"), + ( + &part, + "if [ \"$host\" != x86_64-pc-windows-msvc ]; then", + "a lane checks its host triple", + ), + ( + &part, + "python .github/scripts/host_lanes.py execute --target x86_64-pc-windows-msvc --lane ${{ matrix.lane }} --output \"$RUNNER_TEMP/host-lane\"", + "a lane executes and records its part", + ), + ( + &part, + " name: host-x86_64-pc-windows-msvc-${{ matrix.lane }}\n", + "a lane uploads its receipt", + ), + ( + &join, + " name: build and non-Lean tests (x86_64-pc-windows-msvc)\n", + "the aggregate keeps the host's context", + ), + (&join, " needs: [plan, windows-part]\n", "the aggregate follows the lanes"), + ( + &join, + " if: ${{ always() && needs.plan.outputs.full == 'true' }}\n", + "the aggregate runs after a failed or cancelled lane", + ), + ( + &join, + " pattern: host-x86_64-pc-windows-msvc-*\n", + "the aggregate reads every lane's receipt", + ), + ( + &join, + "python3 .github/scripts/host_lanes.py join --target x86_64-pc-windows-msvc --artifacts \"$RUNNER_TEMP/host-lanes\"", + "the aggregate joins the receipts", + ), + ( + &join, + "PART_RESULT: ${{ needs.windows-part.result }}", + "the aggregate reads the lanes' result", + ), + ( + &honesty, + "python3 -m unittest discover -s .github/scripts -p 'test_host_lanes.py'", + "the join's behavioral tests run before expensive roots", + ), + ] { + require(body, text, rule)?; + } + let result_step = join + .split("\n - ") + .find(|step| step.contains("test \"$PART_RESULT\" = success")) + .ok_or("RP-14: the Windows aggregate does not check its lanes' result")?; + require( + result_step, + "if: ${{ always() }}", + "the aggregate cannot hide a failed or cancelled lane", + )?; + let timeout = part + .lines() + .find_map(|line| line.trim().strip_prefix("timeout-minutes: ")) + .and_then(|value| value.parse::().ok()) + .ok_or("RP-14: a Windows lane has no timeout")?; + if timeout == 0 || timeout > 240 { + return Err("RP-14: a Windows lane cannot raise the 240-minute bound".to_owned()); + } + for body in [&part, &join] { + for forbidden in [ + "run-id:", + "github-token:", + "continue-on-error", + "actions/cache", + ] { + if body.contains(forbidden) { + return Err(format!("RP-14: the Windows lanes admit `{forbidden}`")); + } + } + } + Ok(()) +} + /// Claim drift must finish successfully before a costly root can start. fn honesty_dependencies(ci: &str) -> Result<(), String> { let initial = workflow_job(ci, "honesty")?; @@ -3267,7 +3718,14 @@ fn honesty_dependencies(ci: &str) -> Result<(), String> { "RP-14: honesty must expose its matrix-resolved context and follow the plan".to_owned(), ); } - for root in ["vv", "atlas-prefix", "host-matrix", "intel-construction"] { + for root in [ + "bootstrap-export", + "vv", + "atlas-prefix", + "host-matrix", + "intel-construction", + "windows-part", + ] { let body = workflow_job(ci, root)?; if !body.contains("needs: [plan, honesty]\n") || body diff --git a/crates/conformance/src/export_custody.rs b/crates/conformance/src/export_custody.rs new file mode 100644 index 0000000000..0f6c5fff54 --- /dev/null +++ b/crates/conformance/src/export_custody.rs @@ -0,0 +1,418 @@ +//! The bootstrap production export carried between the jobs of one CI run +//! (SPEC.md §9.4). The `bootstrap-export` job exports the bootstrap project +//! once, requires its packages to equal the committed packages, and uploads +//! the bundle with a custody record that binds it to the repository, the +//! run and its attempt, the commit and its tree, the toolchain pins and the +//! toolchains that ran, and the bundle's bytes. A conformance case of the same +//! run that needs the export takes it from there instead of exporting the +//! project again: the harness names the directory it waits in through +//! [`VARIABLE`], checks the record against its own run and checkout, replays +//! the bundle, and only then hands it to the case. Unset, as in `just vv`, +//! every process that needs the export runs it as before. + +use std::collections::BTreeMap; +use std::process::Command; +use std::time::{Duration, Instant}; + +use camino::{Utf8Path, Utf8PathBuf}; +use lexlean::api::production::{self, ProductionOutput, ProductionResult}; +use serde::{Deserialize, Serialize}; +use sha2::{Digest, Sha256}; + +use crate::production_export::{self as harness, Exported, BOOTSTRAP}; +use crate::support::{self, P}; + +/// The variable naming the directory a job takes the run's export in. +pub const VARIABLE: &str = "LEXLEAN_BOOTSTRAP_EXPORT"; + +/// The custody record's format. +pub const SPEC: &str = "lexlean/ci-bootstrap-export/1"; + +/// The custody record, beside the bundle in the uploaded directory. +pub const CUSTODY_FILE: &str = "custody.json"; + +/// The exported bundle, beside the custody record. +pub const BUNDLE_DIR: &str = "bundle"; + +/// Written by the harness when a case first needs the export. +pub const WANTED: &str = "wanted"; + +/// Written by the job's fetcher once the artifact is in [`ARTIFACT_DIR`]. +pub const READY: &str = "ready"; + +/// Written by the job's fetcher, with the reason, when the run has no export. +pub const FAILED: &str = "failed"; + +/// Where the fetcher unpacks the uploaded directory. +pub const ARTIFACT_DIR: &str = "artifact"; + +/// The repository files whose bytes pin the toolchains and the dependencies. +pub const PINS: [&str; 3] = ["rust-toolchain.toml", "lean-toolchain", "Cargo.lock"]; + +/// The longest a case waits for the run's export: the export job's own +/// limit, after which the job has failed and the fetcher says so. +const WAIT: Duration = Duration::from_secs(6 * 60 * 60); + +/// What binds an uploaded export to the run and the tree that made it. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Custody { + /// Exactly [`SPEC`]. + pub spec: String, + /// `owner/name` of the repository whose run exported it. + pub repository: String, + /// The run that exported it. + pub run_id: String, + /// The attempt of that run. + pub run_attempt: String, + /// The commit checked out. + pub commit: String, + /// The tree of that commit. + pub tree: String, + /// The SHA-256 of each file of [`PINS`]. + pub pins: BTreeMap, + /// What the toolchains report: `rustc -vV` and `lean --version`. + pub toolchains: BTreeMap, + /// The project-relative bundle directory of the export. + pub bundle_path: String, + /// [`bundle_digest`] of the bundle. + pub bundle_sha256: String, + /// The number of files in the bundle. + pub bundle_files: u64, +} + +/// The run and checkout a job is in: what a custody record must name. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Identity { + /// `GITHUB_REPOSITORY`. + pub repository: String, + /// `GITHUB_RUN_ID`. + pub run_id: String, + /// `GITHUB_RUN_ATTEMPT`. + pub run_attempt: String, + /// `HEAD`. + pub commit: String, + /// `HEAD^{tree}`. + pub tree: String, + /// The SHA-256 of each file of [`PINS`]. + pub pins: BTreeMap, + /// What the toolchains report. + pub toolchains: BTreeMap, +} + +fn sha256_hex(bytes: &[u8]) -> String { + Sha256::digest(bytes) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +fn run(root: &Utf8Path, program: &str, arguments: &[&str]) -> Result { + let output = Command::new(program) + .args(arguments) + .current_dir(root.as_std_path()) + .output() + .map_err(|error| format!("`{program} {}`: {error}", arguments.join(" ")))?; + if !output.status.success() { + return Err(format!( + "`{program} {}` failed: {}", + arguments.join(" "), + String::from_utf8_lossy(&output.stderr).trim() + )); + } + String::from_utf8(output.stdout) + .map(|text| text.trim().to_owned()) + .map_err(|_| format!("`{program} {}` printed no UTF-8", arguments.join(" "))) +} + +/// The run and checkout this process is in. +/// +/// # Errors +/// +/// When the run is not named by GitHub's variables, the checkout's tracked +/// files differ from its commit, or a toolchain cannot be asked its version. +pub fn identity(root: &Utf8Path) -> Result { + let variable = |name: &str| -> Result { + std::env::var(name) + .ok() + .filter(|value| !value.is_empty()) + .ok_or_else(|| format!("`{name}` is unset: the export crosses jobs of a CI run only")) + }; + let repository = variable("GITHUB_REPOSITORY")?; + let run_id = variable("GITHUB_RUN_ID")?; + let run_attempt = variable("GITHUB_RUN_ATTEMPT")?; + for (name, value) in [ + ("GITHUB_RUN_ID", &run_id), + ("GITHUB_RUN_ATTEMPT", &run_attempt), + ] { + if !value.bytes().all(|byte| byte.is_ascii_digit()) { + return Err(format!("`{name}` is not a number: {value}")); + } + } + let changed = run( + root, + "git", + &["status", "--porcelain", "--untracked-files=no"], + )?; + if !changed.is_empty() { + return Err(format!( + "the checkout's tracked files differ from its commit:\n{changed}" + )); + } + let mut pins = BTreeMap::new(); + for name in PINS { + let bytes = std::fs::read(root.join(name).as_std_path()) + .map_err(|error| format!("{name}: {error}"))?; + pins.insert(name.to_owned(), sha256_hex(&bytes)); + } + let toolchains = BTreeMap::from([ + ("lean".to_owned(), run(root, "lean", &["--version"])?), + ("rustc".to_owned(), run(root, "rustc", &["-vV"])?), + ]); + Ok(Identity { + repository, + run_id, + run_attempt, + commit: run(root, "git", &["rev-parse", "HEAD"])?, + tree: run(root, "git", &["rev-parse", "HEAD^{tree}"])?, + pins, + toolchains, + }) +} + +/// The digest of a bundle directory: the SHA-256 of one line +/// ` ` per file, in path order, and the +/// number of files. +#[must_use] +pub fn bundle_digest(bundle: &Utf8Path) -> (String, u64) { + let files = harness::tree(bundle); + let mut listing = String::new(); + for (relative, bytes) in &files { + listing.push_str(&sha256_hex(bytes)); + listing.push_str(" "); + listing.push_str(relative); + listing.push('\n'); + } + (sha256_hex(listing.as_bytes()), files.len() as u64) +} + +/// Whether `custody` names the run and checkout `current` is in and the +/// bundle whose digest is `bundle`. Any attempt of the same run may take +/// an export an attempt before it made: a rerun of failed jobs reruns the +/// consumers and keeps the export that passed, on the same commit. +/// +/// # Errors +/// +/// Every field that does not match, one per line. +pub fn check(custody: &Custody, current: &Identity, bundle: &(String, u64)) -> Result<(), String> { + let mut unmet = Vec::new(); + let mut same = + |what: &str, recorded: &dyn std::fmt::Debug, here: &dyn std::fmt::Debug, equal: bool| { + if !equal { + unmet.push(format!( + "{what}: the record names {recorded:?}, this job has {here:?}" + )); + } + }; + same("spec", &custody.spec, &SPEC, custody.spec == SPEC); + same( + "repository", + &custody.repository, + ¤t.repository, + custody.repository == current.repository, + ); + same( + "run", + &custody.run_id, + ¤t.run_id, + custody.run_id == current.run_id, + ); + let attempt = |value: &str| value.parse::().ok(); + same( + "attempt (at most this job's)", + &custody.run_attempt, + ¤t.run_attempt, + matches!( + (attempt(&custody.run_attempt), attempt(¤t.run_attempt)), + (Some(recorded), Some(here)) if recorded <= here + ), + ); + same( + "commit", + &custody.commit, + ¤t.commit, + custody.commit == current.commit, + ); + same( + "tree", + &custody.tree, + ¤t.tree, + custody.tree == current.tree, + ); + same( + "pins", + &custody.pins, + ¤t.pins, + custody.pins == current.pins, + ); + same( + "toolchains", + &custody.toolchains, + ¤t.toolchains, + custody.toolchains == current.toolchains, + ); + same( + "bundle", + &(&custody.bundle_sha256, custody.bundle_files), + &(&bundle.0, bundle.1), + custody.bundle_sha256 == bundle.0 && custody.bundle_files == bundle.1, + ); + let path = Utf8Path::new(&custody.bundle_path); + same( + "bundle path (relative, below the project)", + &custody.bundle_path, + &"a relative path", + !custody.bundle_path.is_empty() + && path + .components() + .all(|part| matches!(part, camino::Utf8Component::Normal(_))), + ); + if unmet.is_empty() { + Ok(()) + } else { + Err(unmet.join("\n")) + } +} + +fn copy_files(from: &Utf8Path, to: &Utf8Path) -> Result<(), String> { + for (relative, bytes) in harness::tree(from) { + let path = to.join(&relative); + std::fs::create_dir_all(path.parent().expect("a file has a parent").as_std_path()) + .map_err(|error| format!("{path}: {error}"))?; + std::fs::write(path.as_std_path(), bytes).map_err(|error| format!("{path}: {error}"))?; + } + Ok(()) +} + +/// Export the bootstrap project once for the run, require its packages to +/// equal the committed packages, and write the bundle and its custody record +/// to `output`. Returns the record. +/// +/// # Errors +/// +/// When the run or checkout cannot be named, the export fails, a package +/// differs from the committed one, or `output` cannot be written. +pub fn publish(output: &Utf8Path) -> Result { + let root = support::repo_root(); + let before = identity(&root)?; + let exported = harness::export(BOOTSTRAP)?; + let unmet = harness::differences(BOOTSTRAP, &exported); + if !unmet.is_empty() { + return Err(unmet.join("\n")); + } + // The export ran from this checkout: it changed no tracked file. + let after = identity(&root)?; + if after != before { + return Err("the checkout changed while the bootstrap project exported".to_owned()); + } + if output.as_std_path().exists() { + return Err(format!("{output} exists: the export is written once")); + } + let bundle = output.join(BUNDLE_DIR); + copy_files(&exported.bundle(), &bundle)?; + let (bundle_sha256, bundle_files) = bundle_digest(&bundle); + let custody = Custody { + spec: SPEC.to_owned(), + repository: before.repository, + run_id: before.run_id, + run_attempt: before.run_attempt, + commit: before.commit, + tree: before.tree, + pins: before.pins, + toolchains: before.toolchains, + bundle_path: exported.output.bundle.to_string(), + bundle_sha256, + bundle_files, + }; + std::fs::write( + output.join(CUSTODY_FILE).as_std_path(), + harness::canonical(&custody), + ) + .map_err(|error| format!("{output}/{CUSTODY_FILE}: {error}"))?; + Ok(custody) +} + +/// Wait in `directory` for the run's export, check its custody record +/// against this job's run and checkout, place the bundle in a fresh copy of +/// the bootstrap project, and replay it. +/// +/// # Errors +/// +/// When the run has no export, the record names another run, commit, tree, +/// pin, toolchain, or bundle, or the bundle does not replay. +pub fn take(directory: &Utf8Path) -> Result { + std::fs::create_dir_all(directory.as_std_path()) + .and_then(|()| std::fs::write(directory.join(WANTED).as_std_path(), b"")) + .map_err(|error| format!("{directory}: {error}"))?; + let started = Instant::now(); + eprintln!("bootstrap export: waiting in {directory} for the run's export"); + loop { + if directory.join(READY).as_std_path().exists() { + break; + } + if let Ok(reason) = std::fs::read_to_string(directory.join(FAILED).as_std_path()) { + return Err(format!( + "the run has no bootstrap export: {}", + reason.trim() + )); + } + if started.elapsed() > WAIT { + return Err(format!("no export reached {directory} in {WAIT:?}")); + } + std::thread::sleep(Duration::from_secs(5)); + } + let artifact = directory.join(ARTIFACT_DIR); + let custody: Custody = serde_json::from_slice( + &std::fs::read(artifact.join(CUSTODY_FILE).as_std_path()) + .map_err(|error| format!("{CUSTODY_FILE}: {error}"))?, + ) + .map_err(|error| format!("{CUSTODY_FILE}: {error}"))?; + let source = artifact.join(BUNDLE_DIR); + let digest = bundle_digest(&source); + let root = support::repo_root(); + check(&custody, &identity(&root)?, &digest)?; + let project = P::bootstrap(); + let bundle = project.root.join(&custody.bundle_path); + copy_files(&source, &bundle)?; + let result: ProductionResult = serde_json::from_slice( + &std::fs::read(bundle.join("result.json").as_std_path()) + .map_err(|error| format!("result.json: {error}"))?, + ) + .map_err(|error| format!("result.json: {error}"))?; + let report = production::verify_artifact(&bundle) + .map_err(|error| format!("the bundle does not replay: {:?}", error.diagnostics))?; + if report.result_id != result.result_id { + return Err(format!( + "the replay names result {}, result.json {}", + report.result_id, result.result_id + )); + } + eprintln!( + "bootstrap export: taken from run {} attempt {} after {:?} (commit {}, bundle sha256 {}, {} files), replayed", + custody.run_id, + custody.run_attempt, + started.elapsed(), + custody.commit, + custody.bundle_sha256, + custody.bundle_files + ); + Ok(Exported { + project, + output: ProductionOutput { + result, + bundle: Utf8PathBuf::from(&custody.bundle_path), + }, + report, + gnaf: None, + }) +} diff --git a/crates/conformance/src/freshness.rs b/crates/conformance/src/freshness.rs new file mode 100644 index 0000000000..828e6e4f5f --- /dev/null +++ b/crates/conformance/src/freshness.rs @@ -0,0 +1,318 @@ +//! Whether the committed generated files are current (SPEC.md §9.4), checked +//! by `cargo xtask check-freshness` in the initial honesty job, without Lean +//! and in seconds. A change to a source, a project file, a lock, or the +//! compiler that was committed without regenerating what depends on it fails +//! here, before the expensive roots of the gate start, instead of hours into +//! them. It adds a precondition to the gate and replaces none of its checks: +//! `golden`, `examples`, and the bootstrap and production-export cases still +//! compare every generated byte. +//! +//! What it reads: each committed build oracle `expected/build/manifest.json` +//! records the digest and length of every input its build read and the +//! compiler semantics that built it; each committed package of +//! `tests/production-export/` records, in `provenance.json`, the compiler +//! semantics and the semantic ID of the build it was exported from; each +//! project's `lexlean.lock` is what `lexlean lock --check` accepts. + +use std::collections::BTreeMap; + +use camino::{Utf8Path, Utf8PathBuf}; +use lexlean::{Engine, LockRequest}; +use serde_json::Value; +use sha2::{Digest, Sha256}; + +/// The projects of `tests/production-export/`, each with its directory. +pub const EXPORTED: [(&str, &str); 5] = [ + ("bootstrap", "bootstrap"), + ("helpdesk", "examples/helpdesk"), + ("production", "examples/production"), + ("production-coverage", "examples/production-coverage"), + ("stdlib", "stdlib"), +]; + +/// What the check read. +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct Summary { + /// Projects with a committed build oracle. + pub projects: usize, + /// Recorded inputs hashed. + pub inputs: usize, + /// Committed export packages read. + pub packages: usize, +} + +fn sha256_hex(bytes: &[u8]) -> String { + Sha256::digest(bytes) + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} + +fn read_json(path: &Utf8Path) -> Result { + let bytes = std::fs::read(path.as_std_path()).map_err(|error| format!("{path}: {error}"))?; + serde_json::from_slice(&bytes).map_err(|error| format!("{path}: {error}")) +} + +/// Every project directory under `root` with a committed build oracle. +#[must_use] +pub fn oracle_projects(root: &Utf8Path) -> Vec { + let mut found = Vec::new(); + for parent in [root.to_owned(), root.join("examples")] { + let Ok(entries) = std::fs::read_dir(parent.as_std_path()) else { + continue; + }; + for entry in entries.flatten() { + let Ok(path) = Utf8PathBuf::from_path_buf(entry.path()) else { + continue; + }; + if path.join("expected/build/manifest.json").is_file() { + found.push(path); + } + } + } + found.sort(); + found +} + +/// The source packages a project names, by package, as directories. +fn source_packages( + root: &Utf8Path, + project: &Utf8Path, +) -> Result, String> { + let file = project.join("lexlean.toml"); + let text = + std::fs::read_to_string(file.as_std_path()).map_err(|error| format!("{file}: {error}"))?; + let config: toml::Value = toml::from_str(&text).map_err(|error| format!("{file}: {error}"))?; + let mut packages = BTreeMap::new(); + for row in config + .get("source_package") + .and_then(toml::Value::as_array) + .into_iter() + .flatten() + { + if let (Some(name), Some(path)) = ( + row.get("package").and_then(toml::Value::as_str), + row.get("path").and_then(toml::Value::as_str), + ) { + packages.insert(name.to_owned(), root.join(path)); + } + } + Ok(packages) +} + +/// Whether one project's committed build oracle and lock are current. Returns +/// the oracle's semantic ID and the number of inputs hashed. +/// +/// # Errors +/// +/// Every stale or unreadable input, one per line. +pub fn check_project(root: &Utf8Path, project: &Utf8Path) -> Result<(String, usize), String> { + let mut unmet = Vec::new(); + let manifest = read_json(&project.join("expected/build/manifest.json"))?; + let name = project.strip_prefix(root).unwrap_or(project); + let language = manifest["language"].as_str().unwrap_or_default(); + let compiler = lexlean::compiler_semantics_id_for(language).to_hex(); + if manifest["compiler"]["semantics_id"].as_str() != Some(compiler.as_str()) { + unmet.push(format!( + "{name}: expected/build was built by compiler semantics {}, this compiler is {compiler}", + manifest["compiler"]["semantics_id"] + )); + } + let packages = source_packages(root, project)?; + let mut hashed = 0; + for input in manifest["inputs"].as_array().into_iter().flatten() { + let path = input["path"].as_str().unwrap_or_default(); + if input["kind"] == "lexicon" { + // A lexicon's digest is its package tree's: the lock records a + // local package's (`lock --check` below), and the compiler's + // semantics ID, compared above, covers an embedded one. + continue; + } + let file = match path.split_once(':') { + Some((package, relative)) => match packages.get(package) { + Some(directory) => directory.join(relative), + None => { + unmet.push(format!( + "{name}: input `{path}` names no source package of lexlean.toml" + )); + continue; + } + }, + None => project.join(path), + }; + hashed += 1; + match std::fs::read(file.as_std_path()) { + Ok(bytes) => { + if input["sha256"].as_str() != Some(sha256_hex(&bytes).as_str()) + || input["byte_length"].as_u64() != Some(bytes.len() as u64) + { + unmet.push(format!( + "{name}: `{path}` changed since expected/build was generated" + )); + } + } + Err(error) => unmet.push(format!("{name}: `{path}`: {error}")), + } + } + let lock = Engine::load(&project.join("lexlean.toml")).and_then(|engine| { + engine.lock(LockRequest { + check_only: true, + allow_network: false, + }) + }); + if let Err(error) = lock { + unmet.push(format!( + "{name}: `lexlean lock --check` fails: {:?}", + error + .diagnostics + .iter() + .map(|diagnostic| diagnostic.message.clone()) + .collect::>() + )); + } + if unmet.is_empty() { + Ok(( + manifest["semantic_id"] + .as_str() + .unwrap_or_default() + .to_owned(), + hashed, + )) + } else { + Err(unmet.join("\n")) + } +} + +/// Whether every committed generated file under `root` is current. +/// +/// # Errors +/// +/// Every stale file, one per line, with the regeneration that renews it. +pub fn check(root: &Utf8Path) -> Result { + let mut unmet = Vec::new(); + let mut summary = Summary::default(); + let mut semantic = BTreeMap::new(); + for project in oracle_projects(root) { + summary.projects += 1; + match check_project(root, &project) { + Ok((id, hashed)) => { + summary.inputs += hashed; + semantic.insert(project, id); + } + Err(reason) => unmet.push(reason), + } + } + let compiler = lexlean::compiler_semantics_id_for(lexlean::LANGUAGE_1_2).to_hex(); + for (exported, directory) in EXPORTED { + let packages = root + .join("tests/production-export") + .join(exported) + .join("packages"); + let expected = semantic.get(&root.join(directory)); + for entry in walkdir::WalkDir::new(packages.as_std_path()) + .into_iter() + .flatten() + { + if entry.file_name() != "provenance.json" { + continue; + } + let Ok(path) = Utf8PathBuf::from_path_buf(entry.into_path()) else { + continue; + }; + summary.packages += 1; + let relative = path.strip_prefix(root).unwrap_or(&path).to_owned(); + let provenance = match read_json(&path) { + Ok(value) => value, + Err(reason) => { + unmet.push(reason); + continue; + } + }; + if provenance["compiler_semantics"].as_str() != Some(compiler.as_str()) { + unmet.push(format!( + "{relative}: exported by compiler semantics {}, this compiler is {compiler}", + provenance["compiler_semantics"] + )); + } + let sources: Vec<&str> = provenance["sources"] + .as_array() + .into_iter() + .flatten() + .filter_map(Value::as_str) + .collect(); + if expected.is_none_or(|id| sources != [id.as_str()]) { + unmet.push(format!( + "{relative}: exported from {sources:?}, {directory}/expected/build is {}", + expected.map_or("missing", String::as_str) + )); + } + } + } + if unmet.is_empty() { + Ok(summary) + } else { + unmet.sort(); + Err(format!( + "{} committed generated file(s) are stale; regenerate them (`regenerate.yml`, or the `-write` recipes):\n{}", + unmet.len(), + unmet.join("\n") + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn copy(from: &Utf8Path, to: &Utf8Path) { + for entry in walkdir::WalkDir::new(from.as_std_path()) + .into_iter() + .flatten() + { + let relative = entry + .path() + .strip_prefix(from.as_std_path()) + .expect("below"); + if relative.starts_with(".lexlean") { + continue; + } + let target = to.as_std_path().join(relative); + if entry.file_type().is_dir() { + std::fs::create_dir_all(&target).expect("dir"); + } else if entry.file_type().is_file() { + std::fs::copy(entry.path(), &target).expect("copy"); + } + } + } + + #[test] + fn a_committed_oracle_is_current_and_a_changed_input_is_stale() { + let root = crate::support::repo_root(); + let temp = tempfile::tempdir().expect("tempdir"); + let copy_root = Utf8PathBuf::from_path_buf(temp.path().to_owned()).expect("utf8"); + let project = copy_root.join("examples/nat-add-zero"); + copy(&root.join("examples/nat-add-zero"), &project); + let (semantic, hashed) = + check_project(©_root, &project).expect("the committed oracle is current"); + assert!(!semantic.is_empty() && hashed >= 2, "{semantic} {hashed}"); + let source = walkdir::WalkDir::new(project.join("src").as_std_path()) + .into_iter() + .flatten() + .find(|entry| entry.path().extension().is_some_and(|ext| ext == "tex")) + .expect("a source") + .into_path(); + let mut text = std::fs::read_to_string(&source).expect("source"); + text.push('\n'); + std::fs::write(&source, text).expect("write"); + let stale = check_project(©_root, &project).expect_err("a changed source is stale"); + assert!( + stale.contains("changed since expected/build was generated"), + "{stale}" + ); + std::fs::remove_file(project.join("expected/build/manifest.json")).expect("remove"); + assert!( + check_project(©_root, &project).is_err(), + "a missing oracle fails" + ); + } +} diff --git a/crates/conformance/src/lib.rs b/crates/conformance/src/lib.rs index eed1e3dee7..ba6f1450ac 100644 --- a/crates/conformance/src/lib.rs +++ b/crates/conformance/src/lib.rs @@ -16,7 +16,9 @@ pub mod certification_library; pub mod counts; pub mod differential; pub mod evidence; +pub mod export_custody; pub mod fixtures; +pub mod freshness; pub mod gnaf; pub mod inventory; pub mod lcnf_adapter; diff --git a/crates/conformance/src/production_export.rs b/crates/conformance/src/production_export.rs index 06d90d00d8..3daaa00d16 100644 --- a/crates/conformance/src/production_export.rs +++ b/crates/conformance/src/production_export.rs @@ -321,7 +321,20 @@ pub fn exported(example: &str) -> &'static Exported { HELPDESK => &DESK, other => panic!("`{other}` is not an exported example"), }; - cell.get_or_init(|| export(example).unwrap_or_else(|reason| panic!("{reason}"))) + cell.get_or_init(|| { + // In a CI run that exports the bootstrap project once (SPEC.md §9.4), + // the job's harness takes that export instead of running it again. + if example == BOOTSTRAP { + if let Some(directory) = std::env::var(crate::export_custody::VARIABLE) + .ok() + .filter(|value| !value.is_empty()) + { + return crate::export_custody::take(Utf8Path::new(&directory)) + .unwrap_or_else(|reason| panic!("§9.4: the run's bootstrap export: {reason}")); + } + } + export(example).unwrap_or_else(|reason| panic!("{reason}")) + }) } /// Every file under `root`, by `/`-separated relative path. @@ -345,6 +358,28 @@ pub fn tree(root: &Utf8Path) -> BTreeMap> { out } +/// Every difference between one export's packages and the committed expected +/// packages of `example`, one line each. +#[must_use] +pub fn differences(example: &str, exported: &Exported) -> Vec { + let packages = tree(&exported.bundle().join("packages")); + let expected = tree(&fixture_dir(example).join("packages")); + let mut unmet = Vec::new(); + for (relative, bytes) in &packages { + match expected.get(relative) { + Some(committed) if committed == bytes => {} + Some(_) => unmet.push(format!("{example}: packages/{relative} differs")), + None => unmet.push(format!("{example}: packages/{relative} is not committed")), + } + } + for relative in expected.keys() { + if !packages.contains_key(relative) { + unmet.push(format!("{example}: packages/{relative} was not exported")); + } + } + unmet +} + /// Compare every exported package with the committed expected packages, or /// with `write`, replace the expected packages with the exported ones. /// @@ -367,19 +402,7 @@ pub fn check(write: bool) -> Result<(), String> { } continue; } - let expected = tree(&expected_root); - for (relative, bytes) in &packages { - match expected.get(relative) { - Some(committed) if committed == bytes => {} - Some(_) => unmet.push(format!("{example}: packages/{relative} differs")), - None => unmet.push(format!("{example}: packages/{relative} is not committed")), - } - } - for relative in expected.keys() { - if !packages.contains_key(relative) { - unmet.push(format!("{example}: packages/{relative} was not exported")); - } - } + unmet.extend(differences(example, &exported)); } if unmet.is_empty() { Ok(()) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 84ec1de64d..8d5b3a9b3d 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xtask" -description = "Repository gates: validate-model, validate-spec-links, verify-examples, check-golden, check-reproducibility, check-fixtures, check-calculus, check-production-export, check-package, counts, check-test-inventory, bdd, ci-summary, ci-hints, ci-shards, release-artifacts, release-check, validate." +description = "Repository gates: validate-model, validate-spec-links, verify-examples, check-golden, check-reproducibility, check-fixtures, check-calculus, check-production-export, export-bootstrap, check-freshness, check-package, counts, check-test-inventory, bdd, ci-summary, ci-hints, ci-shards, release-artifacts, release-check, validate." version.workspace = true edition.workspace = true rust-version.workspace = true diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 93bee814d7..0d62ccd961 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -36,6 +36,8 @@ fn main() -> ExitCode { "check-fixtures" => check_fixtures(&root, write), "check-calculus" => check_calculus(&root, write), "check-production-export" => check_production_export(write), + "check-freshness" => check_freshness(&root), + "export-bootstrap" => export_bootstrap(&std::env::args().skip(2).collect::>()), "check-package" => check_package(&root), "counts" => counts::counts(&root, write), "check-test-inventory" => ci::check_test_inventory(&root), @@ -64,6 +66,11 @@ fn main() -> ExitCode { LEXLEAN_COUNTS=fast|report into its constants and statements\n\ check-production-export §24.7: the consumer's exported packages equal the committed\n\ packages under tests/production-export\n\ + check-freshness §9.4: every committed build oracle's recorded inputs, every lock, + and every committed export package are current (no Lean) + export-bootstrap §9.4: export the bootstrap project once for a CI run, require + its committed packages, and write the bundle with its custody + record (--output DIR) check-package §30.3, RP-12: package lexlean crate, verify offline build & identity\n\ check-test-inventory RP-13: the `test` and `bdd` recipes partition the workspace tests,\n\ and the parts of .github/bdd-parts.json partition `bdd`\n\ @@ -128,6 +135,35 @@ fn check_production_export(write: bool) -> Result<(), Fail> { Ok(()) } +/// §9.4: the committed generated files are current, checked in honesty +/// before the expensive roots (`repo_conformance::freshness`). +fn check_freshness(root: &Path) -> Result<(), Fail> { + let root = camino::Utf8Path::from_path(root).ok_or("the repository path is UTF-8")?; + let summary = repo_conformance::freshness::check(root)?; + println!( + "check-freshness: {} build oracles ({} recorded inputs), their locks, and {} committed export packages are current (§9.4)", + summary.projects, summary.inputs, summary.packages + ); + Ok(()) +} + +/// §9.4: the bootstrap production export, run once for a CI run by the +/// `bootstrap-export` job, which uploads what this writes for the run's other +/// jobs (`repo_conformance::export_custody`). +fn export_bootstrap(arguments: &[String]) -> Result<(), Fail> { + let output = arguments + .iter() + .position(|argument| argument == "--output") + .and_then(|index| arguments.get(index + 1)) + .ok_or("export-bootstrap --output DIR")?; + let custody = repo_conformance::export_custody::publish(camino::Utf8Path::new(output))?; + println!( + "export-bootstrap: the bootstrap project exported once for run {} attempt {} (commit {}); its packages equal the committed packages (§24.7); bundle sha256 {} ({} files) in {output}", + custody.run_id, custody.run_attempt, custody.commit, custody.bundle_sha256, custody.bundle_files + ); + Ok(()) +} + fn example_dirs(root: &Path) -> Result, Fail> { let mut dirs = Vec::new(); for entry in std::fs::read_dir(root.join("examples"))? {