Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bd3a4ad
Add failing tests for the build compatibility check
anth-volk Dec 3, 2026
e081234
Check one Microcosm build against one model version
anth-volk Dec 3, 2026
ec9142a
Add failing tests for round 1 review findings
anth-volk Dec 3, 2026
d9e3457
Fix round 1 review findings in the compatibility check
anth-volk Dec 3, 2026
19791dc
Add a failing test for the round 2 review finding
anth-volk Dec 3, 2026
37d9480
Fix the round 2 review finding and document the check
anth-volk Dec 3, 2026
ba498b5
Type the pinned microdata files and correct the core guidance
anth-volk Dec 3, 2026
0be343d
Add the changelog fragment for #788
anth-volk Dec 3, 2026
060288b
Add failing tests for certifying a Microcosm build by name
anth-volk Dec 3, 2026
c6f2992
Certify a Microcosm build of either country by release and build name
anth-volk Dec 3, 2026
79d5d6a
Fix the type errors mypy finds in the certification script
anth-volk Dec 3, 2026
ef4da45
Add failing tests for round 1 review findings
anth-volk Dec 3, 2026
ff8ee12
Fix round 1 review findings in the certification command
anth-volk Dec 3, 2026
c9ccf8b
Add failing tests for checking only builds made with another model
anth-volk Dec 3, 2026
60f62c0
Check only the builds made with another model version
anth-volk Dec 3, 2026
45a6962
Add failing tests for round 2 review findings
anth-volk Dec 3, 2026
42b0149
Fix round 2 review findings in the certification command
anth-volk Dec 3, 2026
db30f3c
Add a failing test for formatting rewritten constant files
anth-volk Dec 3, 2026
f317337
Format the constant files the certification rewrites
anth-volk Dec 3, 2026
d4bf6fb
Add failing tests for naming the local-area build in the branch
anth-volk Dec 3, 2026
f6d1e3f
Name the local-area build in the certification branch
anth-volk Dec 3, 2026
8e9c1ee
Add failing tests for round 3 review findings
anth-volk Dec 3, 2026
d4d36f5
Fix round 3 review findings in the certification command
anth-volk Dec 3, 2026
90525c6
Describe what the test stand-in for the shell runs
anth-volk Dec 3, 2026
648db8b
Add a changelog entry for certify-build
anth-volk Dec 3, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
1 change: 1 addition & 0 deletions changelog.d/793.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`python scripts/bundle.py certify-build --country uk|us --national-build <build> (--local-area-build <build> | --keep-regional-release | --drop-regional-release)` certifies a Microcosm build of either country by release and build name. It reads each build's folder at the Hugging Face commit that published it, runs `check-compatibility` on a build made with a model version other than the target and certifies the others on the version they were made with, then runs `certify-data`, regenerates the lock and provenance files, moves the test constants that name the certified build, and runs the strict bundle check (and, for the UK, the Universal Credit floor). It commits the result on `certify/<country>/<national build>/<local-area build>/<model package>-<version>` in a new worktree, with a pull request body listing anything left to a person; `--push` and `--open-pr` publish it, and `--in-place` makes the same changes in the current checkout without committing.
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
123 changes: 122 additions & 1 deletion scripts/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
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 certify-build --country uk --national-build <build> \
--local-area-build <build>
python scripts/bundle.py generate
python scripts/bundle.py generate --include-tros
python scripts/bundle.py check
Expand Down Expand Up @@ -66,9 +71,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 +336,67 @@ 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)
# Listed for --help only: main() hands certify-build's arguments to
# scripts/certify_build.py before this parser runs.
subparsers.add_parser(
"certify-build",
add_help=False,
help=(
"Certify a Microcosm build of either country, named by release and "
"build, on a new branch (see scripts/certify_build.py)."
),
)

packages = subparsers.add_parser(
"update-packages",
help="Update package pins in the bundle manifest.",
Expand Down Expand Up @@ -372,8 +486,15 @@ def _parser() -> argparse.ArgumentParser:


def main(argv: list[str] | None = None) -> int:
arguments = sys.argv[1:] if argv is None else list(argv)
if arguments[:1] == ["certify-build"]:
# Its own parser holds its options, and importing it loads .py, so it
# is imported only when it runs.
from certify_build import main as certify_build_main

return certify_build_main(arguments[1:])
parser = _parser()
args = parser.parse_args(argv)
args = parser.parse_args(arguments)
if getattr(args, "strict_tros", False) and not args.include_tros:
parser.error("--strict-tros requires --include-tros")
try:
Expand Down
Loading