Skip to content
Draft
1 change: 1 addition & 0 deletions changelog.d/788.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`python scripts/bundle.py check-compatibility` checks one UK or US Microcosm build against one model version, from a checkout. It runs `certify-data`'s metadata checks, downloads the model's PyPI wheel and each microdata H5 file and checks their SHA-256s, refuses a file that stores a column the build's model defines and the new model does not, and runs a deterministic 2,000-household sample of each file through .py under exactly that wheel and core, requiring finite, positive weighted totals for the headline variables. A pass writes a record naming the build by the SHA-256 of its `release_manifest.json` and the model by its wheel's SHA-256. `certify-data --compatibility-record` (and `--regional-compatibility-record`) certifies on it with the new basis `compatibility_checked_exact_pair` and stores the record in `src/policyengine/data/compatibility/<country>.json`; a model accepts such a certificate at load only when that file holds a matching record for the running model, wheel and core.
55 changes: 50 additions & 5 deletions docs/engineering/skills/data-certification.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,52 @@ Microcosm national release with one of:
`src/policyengine/data/schemas/execution_receipt.v1.schema.json`.

Either way, certification checks that the regional release serves the pinned
model by its own build or compatibility claims (a `--qualified-pair-id` names
the primary release only), checks its core claims as for the primary release
(below), and HEAD-checks every artifact it carries.
model by its own build, its compatibility claims or a
`--regional-compatibility-record` (a `--qualified-pair-id` names the primary
release only), checks its core claims as for the primary release
(below; a `--regional-compatibility-record` names the core it ran instead),
and HEAD-checks every artifact it carries.

### Certifying a build for a newer model with a compatibility check

A build can be certified for a model version it was not built with, and that
no publisher claim covers, by checking it against that version:

```bash
python scripts/bundle.py check-compatibility --country uk \
--manifest-uri "hf://dataset/<repo_id>@<revision>/releases/<release>/<build>/release_manifest.json" \
--model-version <model version> \
--work-dir <local folder> --output <record.json>
```

Run it from a checkout of this repository, with a Hugging Face token that can
read the build in `HUGGING_FACE_TOKEN` or `HF_TOKEN`. One run checks one
build, UK or US. It refuses a build whose model is the version given (certify
that on `built_with_model_package`), downloads the model's PyPI wheel and each
microdata H5 file and checks their SHA-256s, refuses a file that stores a
column the build's model defines and the given version does not, and runs a
2,000-household sample of each file through .py under exactly that wheel and
core (the bundle's pin, or `--core-version`). Every headline variable must
have a finite, positive weighted total. A pass writes a record to `--output`
that names the build by the SHA-256 of its `release_manifest.json` and the
model by its wheel's SHA-256.

Certify with the record (`--regional-compatibility-record` for the regional
release):

```bash
python scripts/bundle.py certify-data --country uk --data-producer populace \
--manifest-uri "<the same URI>" --model-version <model version> \
--compatibility-record <record.json>
```

The basis is `compatibility_checked_exact_pair`. The certificate names the
core, and certification stores the record in
`src/policyengine/data/compatibility/<country>.json`; commit that file with
the bundle. Certification refuses a record for other manifest bytes, another
build, model, wheel or core, and a record given with `--qualified-pair-id`. At
load, a model accepts the certificate only when that file holds a matching
record for the running model and core.

### The core

Expand All @@ -285,9 +328,11 @@ country. A certificate says how it covers it:
the release's claim; the warning, the certificate and its TRO say so.
- **`core_binding: certified` (`--core-version`).** The certificate names the
core (`certified_for_core_version`). Certification refuses a core that a
release neither was built with nor claims, and the strict check refuses a
release neither was built with nor claims, unless a qualified exact pair or
a compatibility check's record ran it, and the strict check refuses a
pin that differs from the named core, so moving the core needs a
re-certification first. A qualified exact pair always binds its core.
re-certification first. A qualified exact pair and a compatibility check's
record always bind their core.

Use the default unless a release must not run on any other core.

Expand Down
25 changes: 20 additions & 5 deletions docs/release-bundles.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,11 +640,26 @@ Things that should usually not affect the fingerprint:
(`compatibility_basis: legacy_compatible_model_package` — the publisher's claim,
recorded with a warning and made good by the test suite passing on the
pinned pair)

If neither is true, certification fails hard and a new data build or a
published compatibility claim is required. A `data_build_fingerprint`
basis (same fingerprint as the build-time model) is a planned third
basis; it requires fingerprint metadata from the installed model package.
3. a compatibility check of exactly this build, model wheel and core passed
(`compatibility_basis: compatibility_checked_exact_pair`). `python
scripts/bundle.py check-compatibility` runs it from a checkout, for one
UK or US Microcosm build and one model version: `certify-data`'s metadata
checks, the input columns of every microdata H5 file (a column the build's
model defines and the new model does not refuses), and a 2,000-household
sample run through .py under the new model's PyPI wheel. A pass writes a
record naming the build by the SHA-256 of its `release_manifest.json` and
the model by its wheel's SHA-256. `certify-data --compatibility-record`
(or `--regional-compatibility-record` for the regional release) certifies
on it and stores it in `src/policyengine/data/compatibility/<country>.json`;
a model loads the certificate only for that build, wheel and core. The
record replaces a publisher claim, but never applies to the build's own
model or with a qualified pair.

If none is true, certification fails hard and a new data build, a published
compatibility claim or a passed compatibility check is required. A
`data_build_fingerprint` basis (same fingerprint as the build-time model)
is planned as a further basis; it requires fingerprint metadata from the
installed model package.

## Artifact states

Expand Down
102 changes: 102 additions & 0 deletions scripts/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
Examples:

python scripts/bundle.py update-packages --us 1.730.0 --uk 2.91.0
python scripts/bundle.py check-compatibility --country uk \
--manifest-uri hf://... --model-version 2.180.0 \
--work-dir /tmp/check --output record.json
python scripts/bundle.py certify-data --country uk --manifest-uri hf://...
python scripts/bundle.py generate
python scripts/bundle.py generate --include-tros
Expand Down Expand Up @@ -66,9 +69,59 @@ def _certify_data(args: argparse.Namespace) -> int:
argv.extend(["--core-version", args.core_version])
if args.qualified_pair_id:
argv.extend(["--qualified-pair-id", args.qualified_pair_id])
if args.compatibility_record:
argv.extend(["--compatibility-record", args.compatibility_record])
if args.regional_compatibility_record:
argv.extend(
["--regional-compatibility-record", args.regional_compatibility_record]
)
return certify_data_release_main(argv)


def _check_compatibility(args: argparse.Namespace) -> int:
"""Check one build against one model version; write the record of a pass."""
# Building the installed country models at import would check the
# bundle's certificate against the installed model version, which the
# check does not use and the bundle need not cover.
os.environ.setdefault("POLICYENGINE_SKIP_COUNTRY_IMPORTS", "1")
from policyengine.provenance import compatibility_check

if not (REPO_ROOT / "pyproject.toml").is_file():
raise ValueError(
"check-compatibility runs from a checkout of policyengine.py: the "
"sample run installs this checkout."
)
token = os.environ.get("HUGGING_FACE_TOKEN") or os.environ.get("HF_TOKEN")
try:
record = compatibility_check.check_build_compatibility(
country=args.country,
manifest_uri=args.manifest_uri,
model_version=args.model_version,
core_version=args.core_version,
token=token,
work_dir=Path(args.work_dir),
policyengine_source=REPO_ROOT,
bundle_path=REPO_ROOT
/ "src"
/ "policyengine"
/ "data"
/ "bundle"
/ "manifest.json",
)
except compatibility_check.CompatibilityCheckError as exc:
print(f"The build does not pass: {exc}", file=sys.stderr)
return 1
output = Path(args.output)
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text(record.model_dump_json(indent=2) + "\n")
print(
f"{args.country.upper()} build {record.build} passes with "
f"{record.model_package.name} {record.model_package.version} and "
f"policyengine-core {record.core_version}; record: {output}"
)
return 0


def _update_packages(args: argparse.Namespace) -> int:
from prepare_package_bundle_update import main as prepare_package_bundle_update_main

Expand Down Expand Up @@ -281,8 +334,57 @@ def _parser() -> argparse.ArgumentParser:
"--qualified-pair-id",
help="Admit the exact pair on automated qualification (needs --core-version).",
)
certify.add_argument(
"--compatibility-record",
help=(
"Record from check-compatibility: certify exactly its model, core "
"and build on the basis compatibility_checked_exact_pair."
),
)
certify.add_argument(
"--regional-compatibility-record",
help=(
"Record from check-compatibility for the regional release: route "
"regions to it on that record."
),
)
certify.set_defaults(func=_certify_data)

compatibility = subparsers.add_parser(
"check-compatibility",
help=(
"Check one Microcosm build against one model version and write the "
"record certify-data --compatibility-record reads."
),
)
compatibility.add_argument("--country", required=True, choices=["us", "uk"])
compatibility.add_argument(
"--manifest-uri",
required=True,
help="hf://dataset/<repo_id>@<revision>/<path-to-release_manifest.json>",
)
compatibility.add_argument(
"--model-version",
required=True,
help="The model package version to check, installed from its PyPI wheel.",
)
compatibility.add_argument(
"--core-version",
help="policyengine-core version to run (default: the bundle's pin).",
)
compatibility.add_argument(
"--work-dir",
required=True,
help=(
"Folder for the downloaded wheel and H5 files and the sample run; "
"files already there with the expected SHA-256 are reused."
),
)
compatibility.add_argument(
"--output", required=True, help="Where to write the record of a pass."
)
compatibility.set_defaults(func=_check_compatibility)

packages = subparsers.add_parser(
"update-packages",
help="Update package pins in the bundle manifest.",
Expand Down
28 changes: 28 additions & 0 deletions scripts/certify_data_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
REGIONAL_RELEASE_KINDS,
certify_data_release,
)
from policyengine.provenance.compatibility_record import ( # noqa: E402
CompatibilityRecord,
)


def _read_record(path: Path | None) -> CompatibilityRecord | None:
if path is None:
return None
return CompatibilityRecord.model_validate_json(path.read_bytes())


def main(argv=None) -> int:
Expand Down Expand Up @@ -136,6 +145,23 @@ def main(argv=None) -> int:
"the regional bytes, its build model and the exact target model."
),
)
parser.add_argument(
"--compatibility-record",
type=Path,
help=(
"Record from bundle.py check-compatibility: certify exactly its "
"model, core and build on the basis compatibility_checked_exact_pair. "
"The core defaults to the record's."
),
)
parser.add_argument(
"--regional-compatibility-record",
type=Path,
help=(
"Record from bundle.py check-compatibility for the regional release "
"named by --regional-manifest-uri."
),
)
parser.add_argument("--no-changelog", action="store_true")
parser.add_argument(
"--skip-artifact-check",
Expand Down Expand Up @@ -175,6 +201,8 @@ def main(argv=None) -> int:
if args.regional_input_surface_evidence is not None
else None
),
compatibility_record=_read_record(args.compatibility_record),
regional_compatibility_record=_read_record(args.regional_compatibility_record),
)
print(result.summary())

Expand Down
33 changes: 33 additions & 0 deletions scripts/generate_trace_tros.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
core_claim_warning,
validate_release_manifest,
)
from policyengine.provenance.compatibility_record import (
BASIS_COMPATIBILITY_CHECKED,
bundled_compatibility_records,
matching_compatibility_record,
)
from policyengine.provenance.manifest import (
CountryReleaseManifest,
DataReleaseManifest,
Expand Down Expand Up @@ -78,6 +83,25 @@ def _validate_reviewed_inputs(
!= country.model_package.version
):
raise ValueError(f"{prefix} requires certification for the pinned model")
compatibility_record = None
if country.certification.compatibility_basis == BASIS_COMPATIBILITY_CHECKED:
# The certificate stands only with the record of the check that ran
# exactly these manifest bytes, model wheel and core.
compatibility_record = matching_compatibility_record(
bundled_compatibility_records(country.country_id),
country=country.country_id,
manifest_sha256=data.source_sha256,
build=country.certification.data_build_id,
model_package=country.model_package.name,
model_version=country.model_package.version,
wheel_sha256=country.model_package.sha256,
core_version=country.certification.certified_for_core_version,
)
if compatibility_record is None:
raise ValueError(
f"{prefix} requires the stored compatibility record of the "
"certified build, model wheel and core"
)
validate_release_manifest(
data,
country.model_package.name,
Expand All @@ -90,6 +114,7 @@ def _validate_reviewed_inputs(
if country.certification.compatibility_basis == "qualified_exact_pair"
else None
),
compatibility_record=compatibility_record,
)
certified = country.certified_data_artifact
artifact = data.artifacts.get(certified.dataset) if certified else None
Expand Down Expand Up @@ -152,6 +177,14 @@ def _validate_runtime_model(bundle: dict, country: CountryReleaseManifest) -> No
raise ValueError(
f"{prefix}: a qualified exact pair must name its pair and core"
)
if (
certification is not None
and certification.compatibility_basis == BASIS_COMPATIBILITY_CHECKED
and not certification.certified_for_core_version
):
raise ValueError(
f"{prefix}: a compatibility check's certificate must name its core"
)
if (
certification is not None
and certification.core_binding is not None
Expand Down
Loading