Repository navigation
Report only policyengine-us's own commit as build metadata git_sha - #10014
Merged
Merged
Conversation
_get_git_sha walked from the package directory up through every parent and returned HEAD of the first directory with a .git entry. Installed into a virtualenv inside another checkout (a data repository such as microcosm), that was the outer repository's commit. An empty .git directory led to the same answer through git's own upward discovery. The sha now comes only from policyengine-us's own checkout (package parent holds .git, its pyproject names policyengine-us, and git's top level is that directory) or from the installer's PEP 610 direct_url.json git record for this copy; otherwise None. Git runs without the repository-redirecting environment variables, and the lookup never raises. Ports PolicyEngine/policyengine-uk#2192. Fixes #10013 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review finding: tomllib raises UnicodeDecodeError for non-UTF-8 bytes (e.g. a UTF-16 pyproject written by Windows PowerShell) and RecursionError for very deep nesting, and _declares_package caught neither, so get_runtime_metadata() could raise. Catch ValueError and RecursionError there, and guard _get_git_sha as a whole so provenance lookup can never stop the model loading. Tests: unparseable pyproject examples, a Hypothesis property over arbitrary pyproject bytes, the outer guard, and duplicate dist-info directories (which must give None, not either copy's commit). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Merge audit for head
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10013
Ports PolicyEngine/policyengine-uk#2192 (issue PolicyEngine/policyengine-uk#2191), which fixed the same bug in policyengine-uk.
What was wrong
build_metadata._get_git_sha()walked from the package directory up through every parent and returnedgit rev-parse HEADfor the first directory with a.gitentry. When policyengine-us is installed into a virtualenv inside another checkout, as in a data build (.venv/lib/python3.x/site-packages/policyengine_usunder a repository such as PolicyEngine/microcosm), that directory is the outer repository.get_runtime_metadata()["git_sha"], andget_data_build_metadata(), which returns the same dict, then name the outer repository's commit.An empty or unusable
.gitdirectory caused the same failure. It passes.exists(), and git's own discovery then continues to the enclosing repository.What it does now
_get_git_sha()returns a sha from one of two sources and otherwise returnsNone:.git, itspyproject.tomlmust namepolicyengine-us, andgit rev-parse --show-toplevelmust resolve to that same directory. HEAD of that repository is returned. This covers editable and development installs, including git worktrees, where.gitis a file. Behaviour there is unchanged.vcs_info.commit_idis read fromdirect_url.jsonin the dist-info directory next to the package. A record belonging to another install elsewhere onsys.pathis ignored. policyengine-core already gets its owngit_shafrom this file (policyengine_core/build_metadata.py::_get_direct_url_git_sha).Git runs with the repository-redirecting variables from
git rev-parse --local-env-varsremoved (GIT_DIR,GIT_WORK_TREE, ...). Without that, a caller's environment, such as a git hook, could substitute another repository's HEAD. The returned value must be a 40- or 64-character hex sha. Any failure givesNonerather than an exception. That covers a missinggitexecutable and apyproject.tomlthat tomllib cannot read: non-UTF-8 bytes raiseUnicodeDecodeErrorand very deep nesting raisesRecursionError. The whole lookup is also guarded, because policyengine.py callsget_data_build_metadata()unguarded while constructing the US model (tax_benefit_models/us/model.py:129-136).Wheel and sdist installs return
None, as before.Invariants
git_shais eitherNoneor the commit of policyengine-us itself. That means HEAD of a repository whose top level is the package's parent and whose pyproject namespolicyengine-us, or the installer-recorded git commit for that installed copy.commit_idonly whenvcs == "git"and the id is a hex sha, elseNone.Tests
policyengine_us/tests/test_build_metadata.pyadds:Example tests:
.venv/lib/python3.13/site-packagesinside a data repository) returnsNone;.venvinside a policyengine-us checkout returnsNone;pip install --target .) returnsNone;.gitdirectory and a malformed pyproject returnNone;Nonewithout raising, and so does an unexpected exception inside the lookup;GIT_DIR/GIT_WORK_TREEpointing elsewhere is ignored;gitreturnNone;None;Nonerather than either copy's commit;get_runtime_metadata()["git_sha"]for the running install isNoneor a full commit id.Hypothesis properties:
.venv,site-packages,dist-packagesor random names) combined with four repository identities;direct_url.jsonpayloads;pyproject.tomlbytes.Together they encode the invariants above.
Results
Run locally on macOS with git 2.55 and the worktree's
uv sync --extra devenvironment (Python 3.14).uv run ruff formatanduv run ruff checkon the changed files pass.uv run pytest policyengine_us/tests/test_build_metadata.py -rs: 38 passed, 1 skipped. The skip is thepolicyengine_bundlescontract test, because that package is not installed locally. CI runs it, and it passes.Mutation checks. Each mutant below is killed by the suite:
except (OSError, TOMLDecodeError)with no outer guard;End to end. Real
uv pip installs into virtualenvs inside a scratch git repository laid out like microcosm. The probe loads the installedbuild_metadata.py, withpolicyengine_corestubbed because the sha lookup does not use it:git_shamain(99b9b05), non-editablenulluv pip install "policyengine-us @ git+file://…@b96f342f"b96f342f…(from uv'sdirect_url.json)b96f342f…Independent review. The first round (Opus 5.5) found that a non-UTF-8 or deeply nested
pyproject.tomlmade the lookup raise, which broke the never-raises invariant. It also found that the duplicate dist-info rule was untested. I reproduced both errors on e84a725; b96f342 fixes them and adds the tests listed above. The second round approved b96f342 after executing the suite (38 passed). It also ran 10 mutants, all killed; 13 layout probes, including a submodule, a--separate-git-dirclone, a shallow clone, a sha256 repository and broken.gitsymlinks; and a fuzz of_declares_package, which never raised.Downstream compatibility
From reading the code at policyengine.py
main(07bae750):get_data_build_metadata()in one place (tax_benefit_models/us/model.py:129-136, used incommon/model_version.py:144-148), and it uses onlydata_build_fingerprint. The fingerprint hashes surface files and does not includegit_sha, so this PR cannot move it._validate_runtime_policyengine_us_matchnever reads this module'sgit_sha. It compares a long-term sidecar's commit with the runtime commit that it reads itself fromimportlib.metadata.distribution("policyengine-us")and that distribution'sdirect_url.json(tax_benefit_models/us/datasets.py:678-705,:741-774). For git installs, it and this module now read the same PEP 610commit_id.built_with_model_package.git_shais only passed through, intobuilt_with_model_git_shaand the TRO'spe:builtWithModelGitSha. Every schema field holding it isOptional[str], and the writers skipNone(provenance/certification.py:672).name,version,git_sha,data_build_fingerprint,core. That matters because the policyengine-bundles contract that CI pins usesextra="forbid"and typesgit_shaasstr | None.uv.lock). Its US release manifests record only{name, version}forbuilt_with_model_package(tools/build_us_fiscal_refresh_release.py:10649,tools/build_us_acs_local_release.py:3419), and nothing in it imports this module. Installed in its.venv, the current wheel's_get_git_sha()returns microcosm's HEAD, but no manifest records that value. Registry installs will reportgit_sha: nullafter this fix: honest, but empty.Published US manifests checked (read-only; nothing was changed)
One US manifest carries the wrong sha. It is on the Hugging Face
policyengine/policyengine-us-databranchmp-ecps-2024-2cdd45d-20260605, uploaded 2026-06-05 (HF commit a091769a):releases/mp-ecps-2024-2cdd45d-20260605/release_manifest.json(sha2564961f6ee…).built_with_model_package = {policyengine-us 1.715.3, git_sha f7458313c86fa580fb1e43a2f18252d67cf76e4a}.build.metadata.data_package_git_sha. It is a policyengine-us-data commit ("Update publication candidate", 2026-05-30), and GitHub returns 422 for it in this repository.data/release_manifests/us.jsonandus.trace.tro.jsonld; on PyPI since 2026-06-05, not yanked) and into the archived policyengine-bundles (bundles/4.14.0/countries/us.json:17).Every other US manifest checked has no wrong sha:
policyengine-us-datamainhavegit_shanull or absent.policyengine/populace-usrelease and parent manifests, the line bundled in current policyengine.py, have nogit_sha.Not checked: the private repos (
policyengine-us-data-private,populace-us-private), which were not readable with the available token.Same pattern elsewhere. The archived policyengine-us-data has its own copy of the parent walk in
utils/policyengine.py::_find_git_root, which feeds long-term sidecarcommit_idandgit_dirty. The repository is archived, so this is noted, not fixed. The policyengine-uk counterpart is PolicyEngine/policyengine-uk#2192. The never-raises defect above also applies there and is noted on that PR.axiom: n/a: build provenance metadata only, no policy change
🤖 Generated with Claude Code