diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4071633..89aecbc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -221,7 +221,8 @@ jobs: cargo build --release --locked -p marqdo-cli -p marqdo-provider-worker version="$(cargo metadata --no-deps --format-version 1 --offline \ | python3 -c 'import json,sys; print(next(p["version"] for p in json.load(sys.stdin)["packages"] if p["name"]=="marqdo-cli"))')" - python3 tools/release/package.py --source-sha "$GITHUB_SHA" --version "$version" + python3 tools/release/package.py --source-sha "$GITHUB_SHA" --version "$version" \ + --stdlib-dir stdlib --bundle-pin tools/stdlib/bundle-pin.json python3 tools/release/smoke.py --archive dist/marqdo-*.tar.gz tools/ci/run_frozen_extensions.sh dist/marqdo-*.tar.gz "$GITHUB_SHA" dist/frozen-extension-evidence.json - uses: actions/upload-artifact@v4 diff --git a/.woodpecker/main.yml b/.woodpecker/main.yml index d07b483..d385bdb 100644 --- a/.woodpecker/main.yml +++ b/.woodpecker/main.yml @@ -77,7 +77,8 @@ steps: set -eu version="$(cargo metadata --no-deps --format-version 1 --offline \ | python3 -c 'import json,sys; print(next(p["version"] for p in json.load(sys.stdin)["packages"] if p["name"]=="marqdo-cli"))')" - python3 tools/release/package.py --source-sha "$CI_COMMIT_SHA" --version "$version" + python3 tools/release/package.py --source-sha "$CI_COMMIT_SHA" --version "$version" \ + --stdlib-dir stdlib --bundle-pin tools/stdlib/bundle-pin.json python3 tools/release/smoke.py --archive dist/marqdo-*.tar.gz tools/ci/run_frozen_extensions.sh dist/marqdo-*.tar.gz "$CI_COMMIT_SHA" dist/frozen-extension-evidence.json cat dist/frozen-extension-evidence.json diff --git a/.woodpecker/performance.yml b/.woodpecker/performance.yml index 2c868f3..57a3e43 100644 --- a/.woodpecker/performance.yml +++ b/.woodpecker/performance.yml @@ -22,7 +22,8 @@ steps: (cd "$perf_dir/beta" && cargo build --release --locked -p marqdo-cli) version="$(target/release/marqdo --version | cut -d ' ' -f 2)" - python3 tools/release/package.py --version "$version" --source-sha "$CI_COMMIT_SHA" --out-dir "$perf_dir" + python3 tools/release/package.py --version "$version" --source-sha "$CI_COMMIT_SHA" \ + --stdlib-dir stdlib --bundle-pin tools/stdlib/bundle-pin.json --out-dir "$perf_dir" archive="$perf_dir/marqdo-$version-x86_64-unknown-linux-gnu.tar.gz" (cd "$perf_dir" && sha256sum -c "$(basename "$archive.sha256")") tar -xzf "$archive" -C "$perf_dir" diff --git a/docs/releasing.md b/docs/releasing.md index 60e8d6a..e77b079 100644 --- a/docs/releasing.md +++ b/docs/releasing.md @@ -116,7 +116,8 @@ The archive is built deterministically from a clean tree at `SOURCE_SHA`: version="$(cargo metadata --no-deps --format-version 1 --offline \ | python3 -c 'import json,sys; print(next(p["version"] for p in json.load(sys.stdin)["packages"] if p["name"]=="marqdo-cli"))')" sha="$(git rev-parse HEAD)" -python3 tools/release/package.py --version "$version" --source-sha "$sha" +python3 tools/release/package.py --version "$version" --source-sha "$sha" \ + --stdlib-dir stdlib --bundle-pin tools/stdlib/bundle-pin.json # dist/marqdo--x86_64-unknown-linux-gnu.tar.gz(+.sha256) ``` @@ -128,9 +129,15 @@ and self-test), `LICENSE-APACHE`, `README.md`, `CHANGELOG.md`, `COMPATIBILITY.md artifacts so the examples run without a compiler) and `tests/consumers/`. `RELEASE_INFO.json` records the stdlib `name`, `version`, `tree_digest`, -`aliases_digest`, `file_count` and `files`. `package.py` recomputes both digests -with the same canonical algorithm as the runtime and refuses to package a mismatch -with `stdlib/stdlib.json` (RFC-EXT-0003 H-08 / T-PKG-04). +`aliases_digest`, `file_count` and `files`, plus `stdlib.source` (kind, +repository, commit, path) when `--bundle-pin` is given. `--stdlib-dir` is a +required build input and names the unpacked Bundle root; `package.py` verifies +its manifest and recomputes both digests with the same canonical algorithm as +the runtime, and fails on a missing directory, a missing `stdlib.json`, a +tree/alias digest mismatch or a pin mismatch before it stages anything +(ADR-0070, RFC-EXT-0003 H-08 / T-PKG-04). The pin itself is resolved and +verified by `python3 tools/stdlib/bundle.py`; tests, the performance runner and +release CI use the same resolver instead of an implicit checkout path. The `protocols.analysis` field identifies the default Analysis major. `protocols.analysis_versions` lists all majors the archive can emit. The archive smoke exercises a published example and a consumer for each major. The bundled stdlib is a diff --git a/tools/ci/test_release_archive.py b/tools/ci/test_release_archive.py index a76f640..ae8c430 100644 --- a/tools/ci/test_release_archive.py +++ b/tools/ci/test_release_archive.py @@ -2,8 +2,8 @@ These tests are stdlib-only and do not build an archive; they pin the digest helpers that package.py/smoke.py share with the runtime and check that the -release pipeline references a verified stdlib bundle instead of relying on the -compile-time source path. +release pipeline takes the stdlib Bundle as one explicit, verified input +instead of relying on the compile-time source path (ADR-0070). """ from __future__ import annotations @@ -12,6 +12,8 @@ import contextlib import importlib.util import io import json +import shutil +import tempfile import unittest from pathlib import Path @@ -31,10 +33,11 @@ class ReleaseArchiveContractTest(unittest.TestCase): self.package = load_module("release_package", ROOT / "tools/release/package.py") self.smoke = load_module("release_smoke", ROOT / "tools/release/smoke.py") self.manifest = json.loads((ROOT / "stdlib/stdlib.json").read_text(encoding="utf-8")) + self.pin = json.loads((ROOT / "tools/stdlib/bundle-pin.json").read_text(encoding="utf-8")) def test_tree_digest_matches_manifest(self) -> None: self.assertEqual( - self.package.stdlib_tree_digest(), self.manifest["digest"] + self.package.stdlib_tree_digest(ROOT / "stdlib"), self.manifest["digest"] ) self.assertEqual( self.smoke.stdlib_tree_digest(ROOT / "stdlib"), self.manifest["digest"] @@ -51,7 +54,9 @@ class ReleaseArchiveContractTest(unittest.TestCase): ) def test_verify_record_names_required_files_and_count(self) -> None: - record = self.package.verify_stdlib() + record = self.package.verify_stdlib( + ROOT / "stdlib", ROOT / "tools/stdlib/bundle-pin.json" + ) self.assertEqual(record["name"], self.manifest["name"]) self.assertEqual(record["version"], self.manifest["version"]) self.assertEqual(record["tree_digest"], self.manifest["digest"]) @@ -60,13 +65,53 @@ class ReleaseArchiveContractTest(unittest.TestCase): self.assertIn("io.mq.md", record["files"]) self.assertIn("io/console.mq.md", record["files"]) self.assertEqual(record["file_count"], len(record["files"])) + self.assertEqual(record["source"]["commit"], self.pin["source"]["commit"]) + self.assertEqual(record["source"]["kind"], "in-repo") - def test_package_stages_stdlib_next_to_the_binary_and_math_backend(self) -> None: + def test_verify_record_has_no_source_without_a_pin(self) -> None: + record = self.package.verify_stdlib(ROOT / "stdlib") + self.assertNotIn("source", record) + + def test_missing_explicit_input_fails_closed(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + with self.assertRaises(SystemExit) as raised: + self.package.verify_stdlib(Path(tmp) / "missing") + self.assertEqual(raised.exception.code, 1) + self.assertIn("does not exist", stderr.getvalue()) + + def test_tampered_explicit_input_and_wrong_pin_fail_closed(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + copy = Path(tmp) / "stdlib" + shutil.copytree(ROOT / "stdlib", copy) + (copy / "io.mq.md").write_text("## @knowledge tampered\n", encoding="utf-8") + with contextlib.redirect_stderr(io.StringIO()): + with self.assertRaises(SystemExit) as raised: + self.package.verify_stdlib(copy) + self.assertEqual(raised.exception.code, 1) + + shutil.rmtree(copy) + shutil.copytree(ROOT / "stdlib", copy) + restamped = json.loads((copy / "stdlib.json").read_text(encoding="utf-8")) + restamped["version"] = "9.9.9" + (copy / "stdlib.json").write_text(json.dumps(restamped), encoding="utf-8") + stderr = io.StringIO() + with contextlib.redirect_stderr(stderr): + with self.assertRaises(SystemExit) as raised: + self.package.verify_stdlib(copy, ROOT / "tools/stdlib/bundle-pin.json") + self.assertEqual(raised.exception.code, 1) + self.assertIn("does not match the pin", stderr.getvalue()) + + def test_package_takes_one_explicit_stdlib_dir(self) -> None: source = (ROOT / "tools/release/package.py").read_text(encoding="utf-8") - self.assertIn('copy_tree(STDLIB, stage_dir / "stdlib")', source) - self.assertIn('"math-backend"', source.replace("math_backend", "math-backend")) + self.assertIn('add_argument("--stdlib-dir", required=True)', source) + self.assertIn('add_argument("--bundle-pin"', source) + self.assertIn('copy_tree(stdlib_dir, stage_dir / "stdlib")', source) self.assertIn('"stdlib": stdlib_info', source) - self.assertIn("verify_stdlib()", source) + self.assertIn("verify_stdlib(", source) + self.assertNotIn('STDLIB = ROOT / "stdlib"', source) + self.assertIn('"math-backend"', source.replace("math_backend", "math-backend")) def test_archive_declares_only_current_analysis_revision(self) -> None: self.smoke.verify_analysis_metadata({"analysis": 3}) diff --git a/tools/release/package.py b/tools/release/package.py index b52c159..f1d645b 100644 --- a/tools/release/package.py +++ b/tools/release/package.py @@ -4,7 +4,13 @@ The archive is deterministic (sorted entries, commit timestamp, uid/gid 0) and contains the CLI, the provider worker helper (same directory, same target), the licenses/changelog/compatibility matrix, the public specs, the developer docs, -the runnable examples and the public protocol consumers. +the runnable examples, the public protocol consumers and the stdlib Bundle that +`--stdlib-dir` names. + +The stdlib Bundle is one required, explicit build input (ADR-0070): a missing +directory, missing manifest, tree/alias digest mismatch or pin mismatch fails +before anything is staged. An optional `--bundle-pin` also records the pinned +source commit in `RELEASE_INFO.json.stdlib.source`. It refuses to package a dirty tree, a version that does not match the Cargo workspace, or a HEAD that differs from `--source-sha`, and it fails if the @@ -16,7 +22,9 @@ Usage (from the repository root, after python3 tools/release/package.py \ --source-sha "$(git rev-parse HEAD)" \ - --version 3.0.0-rc.4 + --version 3.0.0-rc.4 \ + --stdlib-dir stdlib \ + --bundle-pin tools/stdlib/bundle-pin.json """ from __future__ import annotations @@ -34,7 +42,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parents[2] BINARIES = ("marqdo", "marqdo-provider-worker") -STDLIB = ROOT / "stdlib" +PRODUCT_STDLIB_FILES = ("stdlib.json", "io.mq.md", "io/console.mq.md") EXAMPLES_WITH_BUILD = ( "plugin-demo", "plugin-multi-export", @@ -42,6 +50,9 @@ EXAMPLES_WITH_BUILD = ( "math-backend", ) +sys.path.insert(0, str(ROOT / "tools" / "stdlib")) +import bundle as stdlib_bundle # noqa: E402 + def fail(message: str) -> "NoReturn": print(f"package: error: {message}", file=sys.stderr) @@ -64,81 +75,51 @@ def sha256_file(path: Path) -> str: return digest.hexdigest() -def sha256_bytes(*chunks: bytes) -> str: - digest = hashlib.sha256() - for chunk in chunks: - digest.update(chunk) - return "sha256:" + digest.hexdigest() +def stdlib_tree_digest(root: Path) -> str: + """Canonical stdlib tree digest, byte-identical to the CLI loader. - -def stdlib_tree_digest(root: Path = STDLIB) -> str: - """Canonical stdlib tree digest, byte-identical to - `marqdo_cli::stdlib::tree_digest`: sorted `*.mq.md` relative POSIX paths, - `path + NUL + bytes + NUL`.""" - paths = sorted( - (path.relative_to(root).as_posix(), path) - for path in root.rglob("*.mq.md") - if path.is_file() - ) - return sha256_bytes( - *( - chunk - for relative, path in paths - for chunk in (relative.encode(), b"\0", path.read_bytes(), b"\0") - ) - ) + The algorithm lives in `tools/stdlib/bundle.py` (ADR-0070); `root` is the + explicit `--stdlib-dir` input, never an implicit checkout path. + """ + return stdlib_bundle.tree_digest(root) def stdlib_aliases_digest(manifest: dict) -> str: """Canonical public-alias digest, byte-identical to the CLI loader.""" - chunks: list[bytes] = [] - aliases = sorted( - manifest.get("public_aliases", []), - key=lambda alias: (alias["target_module"], alias["target_function"]), - ) - for alias in aliases: - chunks.append(alias["target_module"].encode()) - chunks.append(b"\0") - chunks.append(alias["target_function"].encode()) - chunks.append(b"\0") - for name in sorted(alias["public_names"]): - chunks.append(name.encode()) - chunks.append(b"\0") - return sha256_bytes(*chunks) + return stdlib_bundle.aliases_digest(manifest) -def verify_stdlib() -> dict: - """Validate the shipped stdlib tree and return the RELEASE_INFO record.""" - manifest_path = STDLIB / "stdlib.json" - if not manifest_path.is_file(): - fail(f"missing bundled stdlib manifest {manifest_path}") - manifest = json.loads(manifest_path.read_text(encoding="utf-8")) - tree = stdlib_tree_digest() - aliases = stdlib_aliases_digest(manifest) - if tree != manifest.get("digest"): - fail(f"stdlib tree digest {tree} does not match stdlib.json digest {manifest.get('digest')}") - if aliases != manifest.get("aliases_digest"): - fail( - f"stdlib alias digest {aliases} does not match stdlib.json " - f"aliases_digest {manifest.get('aliases_digest')}" - ) - required = ["stdlib.json", "io.mq.md", "io/console.mq.md"] - files = sorted( - path.relative_to(STDLIB).as_posix() - for path in STDLIB.rglob("*") - if path.is_file() - ) - for name in required: - if name not in files: - fail(f"bundled stdlib is missing required file {name}") - return { - "name": manifest.get("name"), - "version": manifest.get("version"), - "tree_digest": tree, - "aliases_digest": aliases, - "file_count": len(files), - "files": files, - } +def verify_stdlib(root: Path, pin_path: Path | None = None) -> dict: + """Validate the explicit Bundle root and return the RELEASE_INFO record.""" + root = Path(root) + try: + record = stdlib_bundle.verify(root) + stdlib_bundle.require_files(record, PRODUCT_STDLIB_FILES) + if record["aliases_digest"] is None: + raise stdlib_bundle.BundleError( + f"{root / 'stdlib.json'} declares no aliases_digest" + ) + info = { + "name": record["name"], + "version": record["version"], + "tree_digest": record["tree_digest"], + "aliases_digest": record["aliases_digest"], + "file_count": record["file_count"], + "files": record["files"], + } + if pin_path is not None: + pin = stdlib_bundle.load_pin(pin_path) + stdlib_bundle.verify_pin(record, pin) + source = pin["source"] + info["source"] = { + "kind": source.get("kind"), + "repository": source.get("repository"), + "commit": source.get("commit"), + "path": source.get("path"), + } + return info + except stdlib_bundle.BundleError as error: + fail(str(error)) def host_target() -> str: @@ -206,7 +187,14 @@ def copy_tree(source: Path, destination: Path) -> None: shutil.copytree(source, destination, dirs_exist_ok=True) -def stage(stage_dir: Path, version: str, source_sha: str, target: str) -> dict: +def stage( + stage_dir: Path, + version: str, + source_sha: str, + target: str, + stdlib_dir: Path, + stdlib_info: dict, +) -> dict: stage_dir.mkdir(parents=True) release_dir = ROOT / "target" / "release" for name in BINARIES: @@ -226,7 +214,7 @@ def stage(stage_dir: Path, version: str, source_sha: str, target: str) -> dict: # The bundled stdlib is a required runtime artifact, not a build-machine # side effect: copy the verified tree next to the binary (ADR/SPEC H-08). - copy_tree(STDLIB, stage_dir / "stdlib") + copy_tree(stdlib_dir, stage_dir / "stdlib") # Examples without local build output, then the freshly built extension # artifacts so a clean install can run every example without a compiler. @@ -242,7 +230,6 @@ def stage(stage_dir: Path, version: str, source_sha: str, target: str) -> dict: shutil.copy2(limitations, stage_dir / "KNOWN_LIMITATIONS.md") commit_time = int(git("show", "-s", "--format=%ct", "HEAD")) - stdlib_info = verify_stdlib() binaries = { name: { "sha256": sha256_file(stage_dir / name), @@ -326,6 +313,8 @@ def main(argv: list[str]) -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--source-sha", required=True) parser.add_argument("--version", required=True) + parser.add_argument("--stdlib-dir", required=True) + parser.add_argument("--bundle-pin", default=None) parser.add_argument("--target", default=None) parser.add_argument("--out-dir", default=str(ROOT / "dist")) parser.add_argument("--skip-example-build", action="store_true") @@ -335,6 +324,10 @@ def main(argv: list[str]) -> int: ch not in "0123456789abcdef" for ch in args.source_sha ): fail("--source-sha must be a full lowercase 40-character commit SHA") + stdlib_dir = Path(args.stdlib_dir) + stdlib_info = verify_stdlib( + stdlib_dir, Path(args.bundle_pin) if args.bundle_pin else None + ) check_clean(args.source_sha) version = workspace_version() if version != args.version: @@ -349,7 +342,7 @@ def main(argv: list[str]) -> int: out_dir.mkdir(parents=True, exist_ok=True) stage_dir = out_dir / f"marqdo-{version}-{target}" shutil.rmtree(stage_dir, ignore_errors=True) - staged = stage(stage_dir, version, args.source_sha, target) + staged = stage(stage_dir, version, args.source_sha, target, stdlib_dir, stdlib_info) archive = out_dir / f"marqdo-{version}-{target}.tar.gz" archive.unlink(missing_ok=True) make_archive(stage_dir, archive, staged["commit_time"])