Skip to content

Check one Microcosm build against one model version and certify on the result - #789

Draft
anth-volk wants to merge 8 commits into
mainfrom
claude/7c-1-compatibility-check
Draft

anth-volk wants to merge 8 commits into
mainfrom
claude/7c-1-compatibility-check

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #788

What this does

Adds a check of one Microcosm data build (UK or US) against one model version, and a certification basis that rests on a passed check.

python scripts/bundle.py check-compatibility runs from a checkout:

  1. It runs certify-data's metadata checks on the build's manifest. The build must record its model and core, must not have been built with the model version being checked, and must pin every microdata H5 file by revision and SHA-256.
  2. It downloads the model's PyPI wheel and each H5 file, and checks each against its SHA-256.
  3. It reads the columns each file stores. It refuses a file that stores a column the build's model defines and the new model does not. The build model is read in a separate uv run environment, as the regional input audit reads it.
  4. It runs a sample of each file through .py's own code (create_datasets, then Simulation with a household-ID row filter) under exactly the verified wheel and core. The core is the bundle's pin or --core-version. The run happens in a uv run --isolated --no-project --no-config environment that installs from PyPI only and is given no Hugging Face token. Inside that process, only the bundle-certificate lookup in the model-version constructor is replaced.
    • The sample is 2,000 households, ranked by the SHA-256 of policyengine-compatibility-sample-v1:<id>, lowest first.
    • Each headline variable must have a finite, positive weighted total:
      • both countries: household_net_income, income_tax;
      • UK: universal_credit, child_benefit, state_pension, pension_credit, housing_benefit;
      • US: snap, ssi, social_security.
  5. A pass writes a record (provenance/compatibility_record.py). It names the build by the SHA-256 of its raw release_manifest.json bytes and the model by its wheel's SHA-256. It also holds the sample method, the digest of the sampled IDs, the totals, and the never-modelled and defaulted columns.

Certification and load time:

  • certify-data --compatibility-record (and --regional-compatibility-record for the regional release) certifies on the new basis compatibility_checked_exact_pair.
    • It accepts a record only when the record matches the fetched manifest's SHA-256 and build, the model version, PyPI's wheel SHA-256 and the core.
    • --core-version defaults to the record's core.
    • It refuses a record given together with --qualified-pair-id, and a record for the build's own model.
    • It stores the records in src/policyengine/data/compatibility/<country>.json.
  • The three older bases are unchanged. A record replaces a publisher claim and a qualified pair; built_with_model_package still applies for the build's own model.
  • At load time (provenance/manifest.py), a certificate on the new basis is accepted only when the installed records file holds a record for the fetched manifest bytes, the build, the running model and core, and the bundle's wheel.
    • Without the manifest, it is accepted only with such a record for the certified build; otherwise the existing "unverified" result is returned.
    • A bundle whose regional release rests on a record refuses to load unless a record covers the running model and installed core.
  • The strict TRO check (scripts/generate_trace_tros.py) re-validates a certificate on the new basis with its stored record.
  • A test checks that every committed certificate on the new basis has a matching record.
  • Docs: docs/release-bundles.md (certification rules) and docs/engineering/skills/data-certification.md (how to run the check and certify with its record).

Tests and local results

The tests were written first and each set was committed before its code. Each set was run by node ID only: no full suite and no directory.

Commits Tests Before the change After the change
bd3a4ad then e081234 tests/test_compatibility_check.py, tests/test_compatibility_certification.py: 73 functions, 79 cases Both files fail at import, not on assertions: the modules they test are new (ModuleNotFoundError: policyengine.provenance.compatibility_record, ImportError: compatibility_check) 79 passed
ec9142a then d9e3457 (round 1 fixes) 3 functions, 6 cases (probe command, malformed probe result, regional record under another installed core) 6 failed: 1 assertion, 3 wrong exception types, 2 "DID NOT RAISE" 6 passed
19791dc then 37d9480 (round 2 fix) 1 function (check-compatibility skips the country model imports) failed on its assertion ([None] == ['1']) passed
ba498b5 (round 3 typing cleanup, no new test) the same 79 cases (no new test) 79 passed
  • ruff check and ruff format --check are clean on the changed files.
  • mypy reports nothing in the new and changed modules. The errors it reports are in code this PR does not touch: certification.release_manifest_dir, bundle.main, release_lock.py, generate_bundle_artifacts.py and an existing line of generate_trace_tros.py.
  • Not run: check-compatibility against a real build. It downloads H5 files and installs wheels, so the tests replace uv and the downloads with fakes.

Review

Three rounds of adversarial review ran on the pushed branch before this PR.

  • Round 1 (head e081234):
    • Medium, fixed: at load, the regional release's record was matched against the national certificate's model and core, not the running model and installed core.
    • Medium, fixed: uv run --with <checkout> could reuse a wheel uv had cached from an older state of the checkout. The checkout is now installed with --with-editable.
    • Low, fixed: a malformed probe result raised pydantic or attribute errors instead of a refusal with a reason.
  • Round 2 (head d9e3457):
    • Low, fixed: check-compatibility imported .py without POLICYENGINE_SKIP_COUNTRY_IMPORTS, so building the installed country models could refuse before the check ran.
    • All round 1 fixes held.
  • Round 3 (head 37d9480):
    • No bugs. All earlier fixes held.
    • Two code-quality notes were acted on (ba498b5): the pinned revision and SHA-256 of each microdata file are now typed, and two places in the core guidance were corrected.

Bugs left: none.

Notes left, not fixed (low priority; all three reviewers called them notes, not bugs):

  • certify_data_release derives the records file's location from the bundle folder's parent. This is right for the only production caller.
  • The record does not name the .py commit or the spm-calculator version; load time reads neither.
  • A build with no built_at reads its build model without --exclude-newer. The build model and core are pinned exactly, so this can only refuse, never pass wrongly.
  • Network errors during the check escape CompatibilityCheckError and print a traceback.
  • Test gaps:
    • no test exercises reading the installed core version;
    • no test varies the running model for a regional record;
    • the committed-certificate test does not compare a record's manifest SHA-256 with the TRO's pinned value (no committed certificate uses the new basis yet).
  • The TRACE sidecar and receipt digests do not cover the records file, so a record's totals or column lists could change after certification without any digest changing.
  • Two record readers repeat the same parsing, and the check imports three private helpers from regional_input_surface.

Review files are kept outside the repository.

What the merge waits for

38 earlier tasks; all are complete:

  • The decision on what the check covers, how its result is recorded and when it runs: done. This task depends on it.
  • The 37 other tasks of substages 6a to 6d, which this substage depends on: all done.

None needs a deployment. This PR pins, installs and documents no package that those tasks publish.

The branch is from main at b1c8d2c and builds on no earlier task's branch. No deployment code changed.

🤖 Generated with Claude Code

anth-volk and others added 8 commits December 3, 2026 22:33
Tests for a scripts/bundle.py check-compatibility command that checks one
Microcosm build against one model version, the record it writes, certify-data
certifying on that record, and load-time acceptance of only the recorded
build, model and core. They fail before the change: the modules they import
do not exist yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
scripts/bundle.py check-compatibility checks one UK or US build against one
model version: certify-data's metadata checks, the input columns of every
microdata H5 file against the build's model and the new one, and a
2,000-household sample run through .py under the new model's PyPI wheel and
the chosen core. A pass writes a record bound to the SHA-256 of the build's
release_manifest.json and of the wheel.

certify-data --compatibility-record (and --regional-compatibility-record)
certifies on that record, on the basis compatibility_checked_exact_pair, and
stores it in src/policyengine/data/compatibility/<country>.json. Loading a
model accepts such a certificate only for the recorded build, wheel and
core, with or without network access to the manifest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The probe installs the checkout editable, so uv cannot run an earlier
  cached build of it.
- A malformed probe result refuses with a reason instead of raising a
  validation or attribute error.
- A regional release's compatibility record holds only for the installed
  core, whatever the national build's basis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Install the .py checkout editable in the probe environment, so the run
  imports the checkout's current source rather than a wheel uv cached.
- Refuse a probe result with no policyengine_version, a file entry that is
  not a mapping, or no SHA-256 of the sampled household IDs, with a
  CompatibilityCheckError naming the field.
- Match the regional release's compatibility record against the running
  model and the installed core, not the national certificate's.
- Docs: the fingerprint basis is a planned further basis; the module
  docstring names the tagged-layout check and the missing built_at case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
check-compatibility must import .py without building the installed
country models, whose bundle certificate need not cover the installed
model version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- check-compatibility sets POLICYENGINE_SKIP_COUNTRY_IMPORTS before it
  imports .py, as the TRO commands do, so importing .py does not build
  the installed country models and check their bundle certificate.
- The regional refusal says no installed record covers the running model
  and core, which also covers an installed record for another model or
  core.
- The data-certification guide describes check-compatibility and
  certify-data --compatibility-record, and that a record binds its core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From the round 3 review's code-quality notes:
- _microdata_artifacts returns each file with the revision and SHA-256
  it is pinned to, so mypy checks them where the record and the
  input-column evidence take them.
- The data-certification guide says a regional record names its core
  instead of the core claims check, and that a qualified pair or a
  compatibility check's record admits a core no release claims.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check one Microcosm build against one model version and certify on the result

1 participant