diff --git a/README.md b/README.md index 8c9f73afdd..082141e995 100644 --- a/README.md +++ b/README.md @@ -518,8 +518,9 @@ each build into its own folder, and no release publishes with tags. The US side of this holds from the US repository's switch commit: Microcosm's US change (microcosm#1570) merges straight after that commit. -**Each build is published into its own folder.** The UK national, dense and -local-area releases and the US releases below +**Each build is published into its own folder.** The UK national and dense +releases, each UK local-area release that the registry names, and the US +releases below (`microcosm.data.contract.publishes_to_build_folder`) hand each build to the separately installed emitter, whose release writer puts it in `releases///` and records it in the repository's diff --git a/changelog.d/1586-uk-local-k-registered.changed.md b/changelog.d/1586-uk-local-k-registered.changed.md new file mode 100644 index 0000000000..485cac5758 --- /dev/null +++ b/changelog.d/1586-uk-local-k-registered.changed.md @@ -0,0 +1 @@ +`publishes_to_build_folder` admits a UK local-area release (`microcosm-uk-2024-25-local-k`) only when a dataset registry entry names it as its `release`. The registry names none today, so no local-area release publishes yet: the release validators refuse every layout of an unregistered name, and neither publisher takes one. The pull request that authorizes a `k`'s first publication adds its entry. diff --git a/docs/agent-guide.md b/docs/agent-guide.md index b9ba37ec42..181ae3f60e 100644 --- a/docs/agent-guide.md +++ b/docs/agent-guide.md @@ -349,14 +349,17 @@ validator, the publication commands and the loaders enforce today, and what version 1 (ARCH-1, cos d1160) asks of every country's releases. Change it, its golden fixtures (`tools/generate_release_contract_fixtures.py`) and the code that enforces it in the same pull request. The US national golden freezes the -manifests that the US fiscal-refresh tool's `_build_manifests` writes when it -gets only its required arguments and a staging block: an empty `timing`, a null -`ledger_artifact` and only the four gate entries that the function always -writes. A real build adds its own timing, its ledger provenance and the -optional gate blocks its run produces, and the golden freezes none of them. A -pull request that changes what `_build_manifests` always writes, such as -`US_RELEASE_GATE_EVIDENCE_FILES` or `US_DOCUMENTED_ABSENT_INPUTS`, or the -layout that `rewrite_release_dir` gives it, regenerates the fixtures too. +manifests that the US fiscal-refresh tool's `_build_manifests` writes when the +generator replaces four of the builder's globals and passes only the required +arguments and a staging block: an empty `timing`, a null `ledger_artifact` and +only the four gate entries that the function always writes. A real build adds +its own timing, its ledger provenance, the optional gate blocks and the other +optional blocks its run passes (`base_pool` and `post_export_scoring`, for +example), and the blocks that its gate-evidence files bind, and the golden +freezes none of them. A pull request that changes what +`_build_manifests` always writes, such as `US_RELEASE_GATE_EVIDENCE_FILES` or +`US_DOCUMENTED_ABSENT_INPUTS`, or the layout that `rewrite_release_dir` gives +it, regenerates the fixtures too. PR CI never receives external writer credentials or touches restricted microdata. Same-repository runs may receive the optional read-only @@ -365,8 +368,9 @@ no secret. Green PR checks mean the code contracts hold — they do **not** certify data artifacts. Builds, calibrations, and releases run outside PR CI, need gated Hugging Face data and credentials, and cannot run from forks. Release publication is a -deliberate human step. The UK national, dense and local-area releases and the -US releases (`microcosm-us-2024-national`, the US default, `-local`, +deliberate human step. The UK national and dense releases, each UK local-area +release that the dataset registry names, and the US releases +(`microcosm-us-2024-national`, the US default, `-local`, `-receipt-donor`, `-evidence` and `-annual`) publish each build into its own folder through the emitter (`microcosm-publish-release-build`, from `microcosm.build.release_publication`), which records it in the repository's diff --git a/docs/release-contract.md b/docs/release-contract.md index 88c698ce29..9417640922 100644 --- a/docs/release-contract.md +++ b/docs/release-contract.md @@ -845,10 +845,12 @@ repository that has no `releases.json`. - **The releases.** `publishes_to_build_folder` names the releases that publish into build folders: the UK national release `microcosm-uk-2024-25-national`, the dense release - `microcosm-uk-2024-25-dense`, the local-area releases - `microcosm-uk-2024-25-local-k`, and the five US releases in - `US_RELEASES`. The function matches a local-area name by pattern - (`_UK_LOCAL_LINE_RELEASE_ID_RE`), so it admits every positive `N`. + `microcosm-uk-2024-25-dense`, each local-area release + `microcosm-uk-2024-25-local-k` that a UK registry entry names as its + `release`, and the five US releases in `US_RELEASES`. The function reads + the registry on each call. `validate_release_dir` and + `validate_evidence_release_dir` refuse every layout of a local-area name + that no entry names, and neither publisher takes one (microcosm#1586). `microcosm-us-2024-national` holds every national build: calibrated builds, SPM source enrichments, compatibility recertifications and exact-k builds. `microcosm-us-2024-local` holds the ACS local-area @@ -1022,7 +1024,8 @@ work. `evidence` is a registered line on the retire list. A UK local-area release takes the name `microcosm-uk-2024-25-local-k` for each `N` that the registry names, and the pull request that authorizes a k's first - publication adds its registry entry. On 2026-11-12 the registry names no UK + publication adds its registry entry. `publishes_to_build_folder` admits a + local-area name from that entry on. On 2026-11-12 the registry names no UK local-area release, so the UK list holds no `local-k` line yet. Legacy ids, `populace-uk-2023` among them, stay on C1's frozen list. Version 1 fixes no grammar for build names. A build's name must pass the emitter @@ -1035,23 +1038,23 @@ work. release joins `publishes_to_build_folder` and the registry in the pull request that authorizes its first publication. The gaps on 2026-11-12: - *A new country's tagged publish.* Nothing refuses it: the tagged - publisher accepts any release outside `publishes_to_build_folder`, into a - repository without `releases.json`. M3's refusal of unknown countries - closes the gap, under M3's owners. + publisher accepts any release outside `publishes_to_build_folder`, except + a UK local-area name that the registry does not name, into a repository + without `releases.json`. M3's refusal of unknown countries closes the gap, + under M3's owners. - *The NZ, AM and BE declarations.* Each country's `release_contract.json` (under `packages/microcosm-build/src/microcosm/build/`) names `latest.json` in its boundary. Each country's owner changes its file before that country's first publication. - - *The UK local-area pattern.* `publishes_to_build_folder` admits - `microcosm-uk-2024-25-local-k` for every positive `N`, through - `_UK_LOCAL_LINE_RELEASE_ID_RE`, while the registry names no UK local-area - release. The function therefore admits a `k` that no pull request - registered. `test_release_contract_keying.py` pins today's admission with - `microcosm-uk-2024-25-local-k55000`, which the registry does not name. - microcosm#1586 plans to narrow the function to the k's that the registry - names, which flips that case; Max's session owns that change as a code - pull request. Under that plan `line_for_release_id` keeps the pattern, - because the pointer reader still calls it. + - *The first build of a new UK `k`* (2026-11-13). `validate_release_dir` + refuses every layout of a local-area release until a registry entry names + it, and `build_folder_identity` and `microcosm-publish-release-build` + refuse its name. A new `k`'s registry entry therefore lands before its + first build validates. Until that build publishes, the registry names a + release with no published build: `available_variants()` lists the entry, + and `load` raises because `releases.json` does not list the release. + `line_for_release_id` keeps its pattern, because the pointer reader still + calls it. - *The tagged publisher.* `microcosm-publish-release` and `tools/publish_release.sh` stay installed and sit on the [retire list](#retire-list-as-of-2026-10-15). Their removal waits on the diff --git a/packages/microcosm-build/src/microcosm/build/release_publication.py b/packages/microcosm-build/src/microcosm/build/release_publication.py index 5cc3f5023a..c33e2acaac 100644 --- a/packages/microcosm-build/src/microcosm/build/release_publication.py +++ b/packages/microcosm-build/src/microcosm/build/release_publication.py @@ -1,7 +1,8 @@ """Publish a release build into its own folder through the emitter. -A release whose builds can publish into their own folders (the UK -national, dense and local-area releases and the US releases, which +A release whose builds can publish into their own folders (the UK national +and dense releases, each UK local-area release that the dataset registry +names, and the US releases, which ``microcosm.data.contract.publishes_to_build_folder`` names) is handed to the separately installed emitter with the interface's ``publish_release`` call. Microcosm decides what the build contains and checks it with the release @@ -91,6 +92,7 @@ TAGGED_RELEASE_ADVICE, BuildFolderRelease, build_folder_identity, + not_a_build_folder_release_reason, prepare_build_folder_release, ) from microcosm.data.releases_record import RELEASES_RECORD_PATH @@ -200,12 +202,14 @@ def collector_provenance(release: str, build: str) -> CollectorProvenance | None It names the publication's own release and build, as the interface requires, since the collector refuses a build folder whose provenance - names another. A UK build (the national, dense and local-area releases) - used licensed microdata, so the collector keeps its Orrery outputs - private; a US build is published in the public US repository, so it used - none whose licence keeps its outputs private. None for a release whose - builds do not publish into their own folders. The interface raises - ``ValueError`` for a build ID it does not accept. + names another. A UK build (the national and dense releases, and each + local-area release that the dataset registry names) used licensed + microdata, so the collector keeps its Orrery outputs private; a US build + is published in the public US repository, so it used none whose licence + keeps its outputs private. None for a release whose builds do not publish + into their own folders, a UK local-area release that the registry does + not name among them. The interface raises ``ValueError`` for a build ID + it does not accept. """ if not publishes_to_build_folder(release): return None @@ -610,9 +614,9 @@ def _parser() -> argparse.ArgumentParser: prog="microcosm-publish-release-build", description=( "Publish a build of a release whose builds publish into their own " - "folders (the UK national, dense and local-area releases and the US " - "releases) through the emitter, or make a published build current. " - + TAGGED_RELEASE_ADVICE + "folders (the UK national and dense releases, each UK local-area " + "release the dataset registry names, and the US releases) through " + "the emitter, or make a published build current. " + TAGGED_RELEASE_ADVICE ), ) parser.add_argument("release_dir", type=Path, help="The local release directory.") @@ -707,10 +711,7 @@ def main(argv: list[str] | None = None) -> int: args = parser.parse_args(argv) release_dir: Path = args.release_dir if not publishes_to_build_folder(release_dir.name): - parser.error( - f"release {release_dir.name!r} does not publish into build folders. " - + TAGGED_RELEASE_ADVICE - ) + parser.error(not_a_build_folder_release_reason(release_dir.name)) try: return _run(args) except (ValueError, FileNotFoundError) as error: diff --git a/packages/microcosm-build/tests/engine_free/uk/test_build_folder_switch.py b/packages/microcosm-build/tests/engine_free/uk/test_build_folder_switch.py index 47fc0346c0..2439543abd 100644 --- a/packages/microcosm-build/tests/engine_free/uk/test_build_folder_switch.py +++ b/packages/microcosm-build/tests/engine_free/uk/test_build_folder_switch.py @@ -37,6 +37,7 @@ sha256, uk_repository, ) +from test_support.microcosm_data.build_folders import register_uk_local_release #: The stable release the June 2023 build moves into, when it moves. JUNE_2023_RELEASE = "populace-uk-2023" @@ -1098,6 +1099,31 @@ def test_delete_old_layout_reports_a_deleted_folder_no_tag_names(hub, capsys): assert report.get("untagged_folders") == [untagged] +def test_delete_old_layout_reports_a_local_area_folder_the_registry_does_not_name( + hub, capsys, monkeypatch +): + """A folder named by a UK local-area release held copies of tagged builds + only if the registry names the release. Until it does, no build of that + name has published, so the report lists a folder no tag names.""" + switched(hub, capsys) + release = "microcosm-uk-2024-25-local-k55000" + folder = f"releases/{release}" + hub.seed({f"{folder}/release_manifest.json": b"{}"}, date=LATER) + + exit_code, report, error_output = run_step(hub, capsys, DELETE_STEP) + + assert exit_code == 0, error_output + assert f"{folder}/release_manifest.json" in report["delete"] + assert folder in report["untagged_folders"] + + register_uk_local_release(monkeypatch, release) + exit_code, report, error_output = run_step(hub, capsys, DELETE_STEP) + + assert exit_code == 0, error_output + assert f"{folder}/release_manifest.json" in report["delete"] + assert folder not in report["untagged_folders"] + + def test_delete_old_layout_refuses_an_invalid_releases_json(hub, capsys): switched(hub, capsys) hub.seed({"releases.json": b"{"}, date=LATER) diff --git a/packages/microcosm-build/tests/engine_free/uk/test_uk_release_publication.py b/packages/microcosm-build/tests/engine_free/uk/test_uk_release_publication.py index 1f10a9ba36..7418d1c7da 100644 --- a/packages/microcosm-build/tests/engine_free/uk/test_uk_release_publication.py +++ b/packages/microcosm-build/tests/engine_free/uk/test_uk_release_publication.py @@ -23,7 +23,10 @@ from microcosm.data import release as release_module from microcosm.data.releases_record import parse_releases_record from test_support.microcosm_build.emitter import FakeEmitterClient -from test_support.microcosm_data.build_folders import UK_NATIONAL_BUILD +from test_support.microcosm_data.build_folders import ( + UK_NATIONAL_BUILD, + register_uk_local_release, +) from test_support.microcosm_data.release import ( _as_uk_national_build_folder_release, _as_uk_national_line_release, @@ -195,7 +198,12 @@ def test_a_publication_names_its_build_for_the_collector( "microcosm-uk-2024-25-local-k12", ], ) -def test_every_uk_release_names_its_builds_as_licensed_uk_builds(release) -> None: +def test_every_uk_release_names_its_builds_as_licensed_uk_builds( + release, monkeypatch +) -> None: + if "-local-k" in release: + register_uk_local_release(monkeypatch, release) + assert release_publication.collector_provenance( release, UK_NATIONAL_BUILD ) == CollectorProvenance( @@ -206,6 +214,17 @@ def test_every_uk_release_names_its_builds_as_licensed_uk_builds(release) -> Non ) +def test_a_uk_local_area_release_the_registry_does_not_name_names_no_build() -> None: + """It does not publish into build folders, and ``prepare_build_folder_release`` + refuses its name before a publication asks for its provenance.""" + assert ( + release_publication.collector_provenance( + "microcosm-uk-2024-25-local-k12", UK_NATIONAL_BUILD + ) + is None + ) + + def test_a_release_that_publishes_with_tags_names_no_build() -> None: assert ( release_publication.collector_provenance( @@ -1047,6 +1066,22 @@ def test_the_command_says_the_other_uk_releases_take_no_new_builds( assert "take no new builds" in capsys.readouterr().err +def test_the_command_refuses_a_uk_local_area_release_the_registry_does_not_name( + tmp_path: Path, capsys +) -> None: + """The registry names no UK local-area release, so the command refuses the + name and points to the registry instead of to the tagged publisher.""" + unregistered = tmp_path / "microcosm-uk-2024-25-local-k55000" + unregistered.mkdir() + + with pytest.raises(SystemExit): + release_publication.main([str(unregistered)]) + + err = capsys.readouterr().err + assert "names no UK local-area release with this k" in err + assert "take no new builds" not in err + + def test_the_command_refuses_a_build_whose_staging_never_arrived( national: Path, artifact_root: Path, # noqa: F811 diff --git a/packages/microcosm-data/README.md b/packages/microcosm-data/README.md index 30662e9b5a..0ac94b7da2 100644 --- a/packages/microcosm-data/README.md +++ b/packages/microcosm-data/README.md @@ -41,7 +41,7 @@ it as its only and current build. An entry whose repository has no `latest-.json`) and reads the manifest at the build's tag; no registered entry's repository is still in that state. `available()` lists the default-variant `(country, year)` pairs (the ones a bare ``load(country, year)`` -resolves); `available_variants()` lists every published +resolves); `available_variants()` lists every registered `(country, year, variant)`, non-default lines included. The old mutable-root behavior is an unsafe escape hatch, diff --git a/packages/microcosm-data/src/microcosm/data/build_folder_manifests.py b/packages/microcosm-data/src/microcosm/data/build_folder_manifests.py index 3c967ccf2a..dbfb93b5c8 100644 --- a/packages/microcosm-data/src/microcosm/data/build_folder_manifests.py +++ b/packages/microcosm-data/src/microcosm/data/build_folder_manifests.py @@ -36,6 +36,7 @@ ReleaseContractError, publishes_to_build_folder, release_artifact_role, + unregistered_uk_local_release, ) __all__ = [ @@ -162,6 +163,14 @@ def rewrite_release_dir(release_dir: Path | str, *, build: str) -> Path: """ release_dir = Path(release_dir) release = release_dir.name + if unregistered_uk_local_release(release): + raise _refuse( + release, + f"{release_dir} is named {release!r}, a UK local-area release that " + "the dataset registry (microcosm.data.registry.REGISTRY) does not " + "name, so it does not publish into build folders; the pull request " + "that authorizes a k's first publication adds its registry entry", + ) if not publishes_to_build_folder(release): raise _refuse( release, diff --git a/packages/microcosm-data/src/microcosm/data/contract.py b/packages/microcosm-data/src/microcosm/data/contract.py index 1ab255142a..18a4aa50fd 100644 --- a/packages/microcosm-data/src/microcosm/data/contract.py +++ b/packages/microcosm-data/src/microcosm/data/contract.py @@ -51,6 +51,7 @@ from microcosm.data.denied_pools import denied_pool_publication_for from microcosm.data.gate_signing_keys import unregistered_gate_signing_key_problem +from microcosm.data.registry import REGISTRY from microcosm.data.stored_inputs import is_model_named from microcosm.data.us_critical_targets import ( US_CRITICAL_TARGET_FIT_REQUIREMENTS as _US_CRITICAL_TARGET_FIT_REQUIREMENTS, @@ -119,6 +120,7 @@ "release_dataset_role", "required_release_files", "stored_input_evidence_manifest_block", + "unregistered_uk_local_release", "us_engine_scopes_spm_universe", "validate_evidence_release_dir", "validate_release_dir", @@ -282,18 +284,66 @@ def publishes_to_build_folder(release_id: str) -> bool: """Whether builds of ``release_id`` publish into their own build folders. This is the one place that says which releases use the build-folder - layout: the UK national, dense and local-area releases, and the US - releases (:data:`US_RELEASES`). A US build's own release in the tagged - layout (``populace-us-…``) keeps tags, for the builds that have only - their tag. + layout: the UK national and dense releases, each UK local-area release + that the dataset registry names, and the US releases + (:data:`US_RELEASES`). A US build's own release in the tagged layout + (``populace-us-…``) keeps tags, for the builds that have only their tag. + + A UK local-area release is ``microcosm-uk-2024-25-local-k``, and it + publishes once a UK entry of :data:`microcosm.data.registry.REGISTRY` + names it as its ``release``. The registry is read on each call. """ return ( release_id in {_UK_NATIONAL_RELEASE_ID, _UK_DENSE_RELEASE_ID} - or _UK_LOCAL_LINE_RELEASE_ID_RE.fullmatch(release_id) is not None + or ( + _is_uk_local_release_name(release_id) + and _uk_registry_entry_names(release_id) + ) or release_id in US_RELEASES ) +def unregistered_uk_local_release(release_id: str) -> bool: + """Whether ``release_id`` is a UK local-area name that no registry entry names. + + No layout of such a release validates and neither publisher takes it: the + pull request that authorizes a ``k``'s first publication adds its registry + entry, and its builds then publish into build folders. + """ + return _is_uk_local_release_name(release_id) and not _uk_registry_entry_names( + release_id + ) + + +def _is_uk_local_release_name(release_id: str) -> bool: + return _UK_LOCAL_LINE_RELEASE_ID_RE.fullmatch(release_id) is not None + + +def _uk_registry_entry_names(release_id: str) -> bool: + return any( + spec.country == "uk" and spec.release == release_id + for spec in REGISTRY.values() + ) + + +def _refuse_unregistered_uk_local_release(release_dir: Path, release_id: str) -> None: + """Refuse a UK local-area release that the registry does not name. + + Its name decides its layout only once an entry names it, so until then + neither the build-folder layout nor the tagged one is its contract. + """ + if unregistered_uk_local_release(release_id): + raise ReleaseContractError( + release_dir, + [ + f"{release_id!r} is a UK local-area release that the dataset " + "registry (microcosm.data.registry.REGISTRY) does not name, so " + "no layout of it validates; the pull request that authorizes a " + "k's first publication adds its registry entry." + ], + ) + + def release_artifact_role(key: str, kind: object) -> str: """The role a build folder's manifest records for one artifact. @@ -7404,6 +7454,7 @@ def validate_release_dir( if not release_dir.is_dir(): raise ReleaseContractError(release_dir, [f"{release_dir} is not a directory."]) + _refuse_unregistered_uk_local_release(release_dir, release_id) # Role dispatch is strict about a PRESENT dataset_role: only an absent # field gets the legacy national semantics; an explicit null, boolean, @@ -7958,6 +8009,7 @@ def validate_evidence_release_dir(release_dir: Path | str) -> None: if not release_dir.is_dir(): raise ReleaseContractError(release_dir, [f"{release_dir} is not a directory."]) + _refuse_unregistered_uk_local_release(release_dir, release_id) build_folder = publishes_to_build_folder(release_id) diff --git a/packages/microcosm-data/src/microcosm/data/registry.py b/packages/microcosm-data/src/microcosm/data/registry.py index e8b721975e..bd043fc601 100644 --- a/packages/microcosm-data/src/microcosm/data/registry.py +++ b/packages/microcosm-data/src/microcosm/data/registry.py @@ -192,8 +192,10 @@ def register(spec: DatasetSpec) -> DatasetSpec: # microcosm#898 reserves the local-area line as variant "local", with pointer # latest-local-k.json and artifact microcosm_uk_2024_25_local_k.h5, and # the release microcosm-uk-2024-25-local-k once its repository records -# builds in releases.json. Add its registry entry only once a certified -# artifact exists. +# builds in releases.json. The pull request that authorizes a k's first +# publication adds its registry entry (docs/release-contract.md, C5). +# microcosm.data.contract.publishes_to_build_folder admits the release only +# once an entry here names it, and both publishers refuse it before then. register( DatasetSpec( diff --git a/packages/microcosm-data/src/microcosm/data/release.py b/packages/microcosm-data/src/microcosm/data/release.py index f486d5577d..264de8c8d2 100644 --- a/packages/microcosm-data/src/microcosm/data/release.py +++ b/packages/microcosm-data/src/microcosm/data/release.py @@ -1,8 +1,8 @@ """Publish a release and move its approved repository pointer. -**No UK or US release publishes here.** The UK national, dense and -local-area releases and the US releases -(:data:`~microcosm.data.contract.US_RELEASES`) publish each build into its +**No UK or US release publishes here.** The UK national and dense releases, +each UK local-area release that the dataset registry names, and the US +releases (:data:`~microcosm.data.contract.US_RELEASES`) publish each build into its own folder, ``releases///``, through the emitter's ``publish_release`` call (``microcosm-publish-release-build``, in ``microcosm-build``), and the repository's ``releases.json`` records which @@ -84,6 +84,7 @@ publishes_to_build_folder, release_dataset_role, required_release_files, + unregistered_uk_local_release, validate_evidence_release_dir, validate_release_dir, ) @@ -105,6 +106,7 @@ "RELEASE_TIER_CERTIFIED", "RELEASE_TIER_EVIDENCE", "TAGGED_RELEASE_ADVICE", + "UNREGISTERED_UK_LOCAL_RELEASE_ADVICE", "LatestPointer", "PreparedRelease", "build_folder_identity", @@ -115,6 +117,7 @@ "latest_pointer_payload", "line_pointer_path", "missing_on_hub", + "not_a_build_folder_release_reason", "line_pointer_payload", "publish_release", "latest_release", @@ -339,7 +342,32 @@ class PreparedRelease: ) +#: Why neither publisher takes a UK local-area release that the dataset +#: registry does not name. +UNREGISTERED_UK_LOCAL_RELEASE_ADVICE = ( + "The dataset registry (microcosm.data.registry.REGISTRY) names no UK " + "local-area release with this k. The pull request that authorizes a k's " + "first publication adds its registry entry, and its builds then publish " + f"with `{BUILD_FOLDER_PUBLICATION_COMMAND}`." +) + + +def not_a_build_folder_release_reason(release_id: str) -> str: + """Why ``release_id`` cannot publish into a build folder, for a refusal.""" + advice = ( + UNREGISTERED_UK_LOCAL_RELEASE_ADVICE + if unregistered_uk_local_release(release_id) + else TAGGED_RELEASE_ADVICE + ) + return f"release {release_id!r} does not publish into build folders. {advice}" + + def _refuse_build_folder_release(release_id: str) -> None: + if unregistered_uk_local_release(release_id): + raise ValueError( + f"release {release_id!r} publishes through no publisher yet. " + + UNREGISTERED_UK_LOCAL_RELEASE_ADVICE + ) if publishes_to_build_folder(release_id): raise ValueError( f"release {release_id!r} publishes each build into its own folder " @@ -392,10 +420,7 @@ def build_folder_identity(release_dir: Path | str) -> tuple[str, str, str]: """ release_dir = Path(release_dir) if not publishes_to_build_folder(release_dir.name): - raise ValueError( - f"release {release_dir.name!r} does not publish into build folders. " - + TAGGED_RELEASE_ADVICE - ) + raise ValueError(not_a_build_folder_release_reason(release_dir.name)) manifest = json.loads((release_dir / "release_manifest.json").read_text()) if not isinstance(manifest, Mapping): raise ValueError(f"{release_dir}/release_manifest.json is not an object.") @@ -828,9 +853,10 @@ def publish_release( ) -> dict: """Publish a release directory and optionally point ``latest.json`` at it. - A release that publishes into build folders (the UK national, dense - and local-area releases) is refused before any check or Hub call; see - the module docstring. + A release that publishes into build folders (the UK national and dense + releases and each registered UK local-area release) is refused before + any check or Hub call, and so is a UK local-area release that the + registry does not name; see the module docstring. The order is the guarantee: the release contract is validated first (an invalid release never reaches the Hub), then an immutable branch commit is diff --git a/packages/microcosm-data/tests/engine_free/shared/test_build_folder_manifests.py b/packages/microcosm-data/tests/engine_free/shared/test_build_folder_manifests.py index e8a72bdefa..0bc8ae5af3 100644 --- a/packages/microcosm-data/tests/engine_free/shared/test_build_folder_manifests.py +++ b/packages/microcosm-data/tests/engine_free/shared/test_build_folder_manifests.py @@ -180,6 +180,21 @@ def test_rewriting_a_directory_named_for_no_release_is_refused(tmp_path) -> None build_folder_manifests.rewrite_release_dir(renamed, build=BUILD) +def test_rewriting_an_unregistered_uk_local_area_release_is_refused(tmp_path) -> None: + """The registry names no UK local-area release, so the directory takes no + build-folder layout, and the refusal says who registers one.""" + directory = write_tagged_release(tmp_path) + renamed = directory.with_name("microcosm-uk-2024-25-local-k55000") + directory.rename(renamed) + + with pytest.raises( + ReleaseContractError, match="the dataset registry .* does not name" + ) as refusal: + build_folder_manifests.rewrite_release_dir(renamed, build=BUILD) + + assert "adds its registry entry" in str(refusal.value) + + def test_the_publication_commands_publish_then_make_current(tmp_path) -> None: directory = write_tagged_release(tmp_path) artifact_root = tmp_path / "artifacts" diff --git a/packages/microcosm-data/tests/engine_free/shared/test_contract.py b/packages/microcosm-data/tests/engine_free/shared/test_contract.py index 07070a4904..dcccc599d7 100644 --- a/packages/microcosm-data/tests/engine_free/shared/test_contract.py +++ b/packages/microcosm-data/tests/engine_free/shared/test_contract.py @@ -2504,7 +2504,8 @@ def test_uk_national_release_requires_policyengine_uk_model_pin( [ (UK_NATIONAL_RELEASE_ID, True), ("microcosm-uk-2024-25-dense", True), - ("microcosm-uk-2024-25-local-k55000", True), + # No registry entry names a UK local-area release (microcosm#1586). + ("microcosm-uk-2024-25-local-k55000", False), (US_NATIONAL_RELEASE, True), (US_LOCAL_RELEASE, True), (US_RECEIPT_DONOR_RELEASE, True), diff --git a/packages/microcosm-data/tests/engine_free/shared/test_local_area_contract.py b/packages/microcosm-data/tests/engine_free/shared/test_local_area_contract.py index fc2f476fa5..c37bc05710 100644 --- a/packages/microcosm-data/tests/engine_free/shared/test_local_area_contract.py +++ b/packages/microcosm-data/tests/engine_free/shared/test_local_area_contract.py @@ -17,6 +17,7 @@ import hashlib import json import re +import sys from pathlib import Path import pytest @@ -33,6 +34,7 @@ US_STORED_INPUTS_EVIDENCE_KEY, ReleaseContractError, release_dataset_role, + validate_evidence_release_dir, validate_release_dir, ) from microcosm.data.release import prepare_release, publish_release @@ -41,6 +43,7 @@ UK_LOCAL_BUILD, build_folder_build_manifest, build_folder_release_manifest, + register_uk_local_release, ) from test_support.paths import REPOSITORY_ROOT @@ -372,6 +375,18 @@ def _as_uk_build_folder(manifest: dict, *, release_id: str) -> dict: ) +@pytest.fixture +def uk_local_k_registered(monkeypatch) -> None: + """Name ``UK_LOCAL_RELEASE_ID`` in the registry for one test. + + The contract gives a UK local-area release the build-folder layout only + once a registry entry names it (microcosm#1586), and the shipped registry + names none, so each test of that layout registers its release first. + """ + register_uk_local_release(monkeypatch, UK_LOCAL_RELEASE_ID) + + +@pytest.mark.usefixtures("uk_local_k_registered") def test_uk_local_area_bundle_publishes_in_the_build_folder_layout( tmp_path: Path, ) -> None: @@ -390,6 +405,7 @@ def test_uk_local_area_bundle_publishes_in_the_build_folder_layout( validate_release_dir(release_dir) +@pytest.mark.usefixtures("uk_local_k_registered") def test_uk_local_area_bundle_refuses_the_tagged_layout(tmp_path: Path) -> None: release_dir = _write_local_bundle(tmp_path, release_id=UK_LOCAL_RELEASE_ID) build_manifest_path = release_dir / "build_manifest.json" @@ -403,6 +419,63 @@ def test_uk_local_area_bundle_refuses_the_tagged_layout(tmp_path: Path) -> None: ) +def _write_tagged_uk_local_bundle(root: Path, monkeypatch) -> Path: + """A UK local-area bundle in the tagged layout: the build is the release + id, and each artifact is pinned to that id as its revision.""" + + def keep_tagged(manifest: dict, *, release_id: str) -> dict: + for artifact in manifest["artifacts"].values(): + artifact["repo_id"] = "policyengine/populace-uk-private" + return manifest + + module = sys.modules[__name__] + monkeypatch.setattr(module, "_as_uk_build_folder", keep_tagged) + monkeypatch.setattr( + module, + "build_folder_build_manifest", + lambda manifest, *, release, build: manifest, + ) + return _write_local_bundle(root, release_id=UK_LOCAL_RELEASE_ID) + + +@pytest.mark.parametrize("layout", ["build-folder", "tagged"]) +def test_no_layout_of_an_unregistered_uk_local_area_release_validates( + tmp_path: Path, monkeypatch, layout: str +) -> None: + """The registry names no UK local-area release, so the contract refuses + the name itself, in either layout and at either tier. Without that + refusal the tagged layout would validate (microcosm#1586).""" + if layout == "tagged": + release_dir = _write_tagged_uk_local_bundle(tmp_path, monkeypatch) + else: + release_dir = _write_local_bundle(tmp_path, release_id=UK_LOCAL_RELEASE_ID) + + failures = _failures(release_dir) + + assert len(failures) == 1, failures + assert "the dataset registry" in failures[0] + assert "no layout of it validates" in failures[0] + with pytest.raises(ReleaseContractError, match="no layout of it validates"): + validate_evidence_release_dir(release_dir) + + +def test_a_registered_uk_local_area_release_refuses_the_tagged_bundle( + tmp_path: Path, monkeypatch +) -> None: + """The tagged bundle above is a real tagged layout: once the registry + names the release, the contract refuses it for its layout.""" + release_dir = _write_tagged_uk_local_bundle(tmp_path, monkeypatch) + register_uk_local_release(monkeypatch, UK_LOCAL_RELEASE_ID) + + failures = _failures(release_dir) + + assert any( + "build_manifest.json carries 'build_id'" in failure for failure in failures + ) + assert not any("no layout of it validates" in failure for failure in failures) + + +@pytest.mark.usefixtures("uk_local_k_registered") def test_uk_local_area_bundle_refuses_a_tagged_release_manifest( tmp_path: Path, ) -> None: @@ -887,6 +960,7 @@ def test_the_gate_is_graded_against_a_recorded_built_with_engine( assert "build.built_with_model_package" in failures[0] +@pytest.mark.usefixtures("uk_local_k_registered") def test_uk_local_k_bundle_needs_no_stored_input_gate(tmp_path: Path) -> None: """The requirement is scoped to US ids: UK local-k (and UK dense, which shares _check_local_area_gates) releases have no stored-input seam.""" @@ -978,6 +1052,7 @@ def test_the_build_manifest_copy_of_the_spm_gate_must_match(tmp_path: Path) -> N ), failures +@pytest.mark.usefixtures("uk_local_k_registered") def test_uk_local_k_bundle_needs_no_spm_measurement_gate(tmp_path: Path) -> None: release_dir = _write_local_bundle(tmp_path, release_id=UK_LOCAL_RELEASE_ID) gate_summary = json.loads((release_dir / "gate_summary.json").read_text()) @@ -1177,6 +1252,7 @@ def check(data): check() +@pytest.mark.usefixtures("uk_local_k_registered") def test_property_uk_local_k_verdicts_ignore_the_stored_input_gate( tmp_path_factory, ) -> None: diff --git a/packages/microcosm-data/tests/engine_free/shared/test_release.py b/packages/microcosm-data/tests/engine_free/shared/test_release.py index aef4c6a97e..b5555c2068 100644 --- a/packages/microcosm-data/tests/engine_free/shared/test_release.py +++ b/packages/microcosm-data/tests/engine_free/shared/test_release.py @@ -193,15 +193,23 @@ def _write_uk_build_folder_release( [UK_NATIONAL_RELEASE_ID, "microcosm-uk-2024-25-dense", UK_LOCAL_RELEASE_ID], ) def test_the_tagged_publisher_refuses_every_build_folder_release( - release_dir: Path, artifact_root: Path, tmp_path: Path, release_id: str + release_dir: Path, + artifact_root: Path, + tmp_path: Path, + monkeypatch, + release_id: str, ) -> None: """A UK build publishes into its own folder through the emitter; this publisher, which commits, tags and writes pointer files, refuses it - before any check or Hub call, on every path.""" + before any check or Hub call, on every path. The local-area release is + one the registry names.""" + from test_support.microcosm_data.build_folders import register_uk_local_release + + register_uk_local_release(monkeypatch, UK_LOCAL_RELEASE_ID) uk_dir = _write_uk_build_folder_release(release_id, release_dir, tmp_path) uk_hub = FakeHub("policyengine/populace-uk-private") uk_hub._download_dir = tmp_path / "uk-hub-cache" - message = "microcosm-publish-release-build" + message = "publishes each build into its own folder" with pytest.raises(ValueError, match=message): release_module.prepare_release(uk_dir, artifact_root=artifact_root) @@ -378,10 +386,13 @@ def test_prepare_build_folder_release_lists_each_file_with_its_role( def test_prepare_build_folder_release_publishes_the_checksum_ledger( - tmp_path: Path, + tmp_path: Path, monkeypatch ) -> None: + from test_support.microcosm_data.build_folders import register_uk_local_release + from .test_local_area_contract import _write_local_bundle + register_uk_local_release(monkeypatch, UK_LOCAL_RELEASE_ID) release_dir = _write_local_bundle(tmp_path, release_id=UK_LOCAL_RELEASE_ID) prepared = release_module.prepare_build_folder_release( @@ -410,6 +421,55 @@ def test_build_folder_identity_says_where_a_tagged_release_publishes( assert "take no new builds" in str(refusal.value) +def test_no_publisher_takes_a_uk_local_area_release_the_registry_does_not_name( + artifact_root: Path, tmp_path: Path +) -> None: + """Neither publisher takes a complete bundle of a UK local-area release + that the registry does not name. Each refusal points to the registry, and + the tagged publisher makes no Hub call (microcosm#1586).""" + from .test_local_area_contract import _write_local_bundle + + unregistered = _write_local_bundle(tmp_path, release_id=UK_LOCAL_RELEASE_ID) + uk_hub = FakeHub("policyengine/populace-uk-private") + uk_hub._download_dir = tmp_path / "uk-hub-cache" + refusals = [] + + with pytest.raises( + ValueError, match="does not publish into build folders" + ) as refusal: + release_module.build_folder_identity(unregistered) + refusals.append(refusal) + with pytest.raises( + ValueError, match="does not publish into build folders" + ) as refusal: + release_module.prepare_build_folder_release( + unregistered, artifact_root=artifact_root + ) + refusals.append(refusal) + tagged = "publishes through no publisher" + with pytest.raises(ValueError, match=tagged) as refusal: + release_module.prepare_release(unregistered, artifact_root=artifact_root) + refusals.append(refusal) + with pytest.raises(ValueError, match=tagged) as refusal: + publish_release( + unregistered, + "policyengine/populace-uk-private", + api=uk_hub, + artifact_root=artifact_root, + update_latest=False, + ) + refusals.append(refusal) + with pytest.raises(ValueError, match=tagged) as refusal: + publish_cli.main([str(unregistered), "--preflight-only", "--no-latest"]) + refusals.append(refusal) + + for refusal in refusals: + assert release_module.UNREGISTERED_UK_LOCAL_RELEASE_ADVICE in str(refusal.value) + assert "take no new builds" not in str(refusal.value) + assert uk_hub.events == [] + assert uk_hub.uploads == [] + + def test_prepare_build_folder_release_refuses_a_tagged_release( release_dir: Path, artifact_root: Path ) -> None: diff --git a/packages/microcosm-data/tests/engine_free/shared/test_release_contract_keying.py b/packages/microcosm-data/tests/engine_free/shared/test_release_contract_keying.py index 305982a557..09ce95ed68 100644 --- a/packages/microcosm-data/tests/engine_free/shared/test_release_contract_keying.py +++ b/packages/microcosm-data/tests/engine_free/shared/test_release_contract_keying.py @@ -9,12 +9,17 @@ from __future__ import annotations +import dataclasses import json from pathlib import Path +from unittest import mock import pytest +from hypothesis import example, given, settings +from hypothesis import strategies as st from microcosm.data import contract +from microcosm.data import release as release_module from microcosm.data.contract import ( REQUIRED_RELEASE_FILES, ReleaseContractError, @@ -23,7 +28,8 @@ required_release_files, validate_release_dir, ) -from microcosm.data.registry import DEFAULT_VARIANT, REGISTRY +from microcosm.data.registry import DEFAULT_VARIANT, REGISTRY, DatasetSpec +from test_support.microcosm_data.build_folders import uk_local_release_spec from test_support.microcosm_data.release import release_dir # noqa: F401 #: Names in the release grammar, ``microcosm---``, that @@ -84,7 +90,6 @@ def test_an_unlisted_id_does_not_publish_into_a_build_folder(release_id): ( contract._UK_NATIONAL_RELEASE_ID, contract._UK_DENSE_RELEASE_ID, - "microcosm-uk-2024-25-local-k55000", *contract.US_RELEASES, ), ) @@ -92,6 +97,148 @@ def test_the_uk_and_us_releases_publish_into_build_folders(release_id): assert publishes_to_build_folder(release_id) +def _uk_local_release_id(k: int) -> str: + return f"microcosm-uk-2024-25-local-k{k}" + + +@st.composite +def _registered_ks_and_a_k(draw): + """Some registered ``k`` values and one ``k``, registered about half the time.""" + + registered = draw(st.frozensets(st.integers(1, 10**9), max_size=4)) + any_k = st.integers(1, 10**9) + k = draw( + st.one_of(st.sampled_from(sorted(registered)), any_k) if registered else any_k + ) + return registered, k + + +def test_the_registry_names_no_uk_local_area_release(): + """docs/release-contract.md (C5) says so; a pull request that registers a + ``k`` updates the document and this test together.""" + + assert not [ + spec.release + for spec in REGISTRY.values() + if spec.release is not None + and contract._UK_LOCAL_LINE_RELEASE_ID_RE.fullmatch(spec.release) + ] + assert not publishes_to_build_folder("microcosm-uk-2024-25-local-k55000") + assert contract.unregistered_uk_local_release("microcosm-uk-2024-25-local-k55000") + + +@settings(max_examples=200, deadline=None, derandomize=True) +@given(_registered_ks_and_a_k()) +@example((frozenset(), 1)) +@example((frozenset({12}), 12)) +# A registered id that contains, or sits inside, the id asked about. +@example((frozenset({12}), 1)) +@example((frozenset({12}), 2)) +@example((frozenset({12}), 123)) +@example((frozenset({1, 123}), 12)) +def test_a_uk_local_area_release_publishes_only_once_the_registry_names_it(drawn): + """For any positive ``k``, ``microcosm-uk-2024-25-local-k`` publishes + into build folders exactly when a registry entry names it as its release, + the tagged publisher refuses it either way, and its pointer line is the + same whatever the registry holds (microcosm#1586).""" + + registered, k = drawn + release_id = _uk_local_release_id(k) + entries = { + spec.key: spec + for spec in map( + uk_local_release_spec, map(_uk_local_release_id, sorted(registered)) + ) + } + with mock.patch.dict(REGISTRY, entries): + assert publishes_to_build_folder(release_id) is (k in registered) + assert contract.unregistered_uk_local_release(release_id) is ( + k not in registered + ) + with pytest.raises(ValueError) as refusal: + release_module._refuse_build_folder_release(release_id) + reason = release_module.not_a_build_folder_release_reason(release_id) + assert line_for_release_id(release_id) == f"local-k{k}" + assert line_for_release_id(release_id) == f"local-k{k}" + if k in registered: + assert "publishes each build into its own folder" in str(refusal.value) + assert release_module.TAGGED_RELEASE_ADVICE in reason + else: + assert release_module.UNREGISTERED_UK_LOCAL_RELEASE_ADVICE in str(refusal.value) + assert release_module.UNREGISTERED_UK_LOCAL_RELEASE_ADVICE in reason + # Outside the patch the shipped registry names no local-area release. + assert not publishes_to_build_folder(release_id) + + +def test_a_registered_k_admits_its_own_name_and_no_other(): + """An entry admits the release it names, whole: registering ``local-k12`` + admits neither a ``k`` whose id sits inside that name nor one whose id + contains it.""" + + spec = uk_local_release_spec(_uk_local_release_id(12)) + with mock.patch.dict(REGISTRY, {spec.key: spec}): + assert publishes_to_build_folder(_uk_local_release_id(12)) + assert not contract.unregistered_uk_local_release(_uk_local_release_id(12)) + for other in (1, 2, 120, 123, 112, 1212): + assert not publishes_to_build_folder(_uk_local_release_id(other)) + assert contract.unregistered_uk_local_release(_uk_local_release_id(other)) + + +@pytest.mark.parametrize( + "changes", + ( + {"release": None}, + {"release": contract._UK_DENSE_RELEASE_ID}, + {"release": "microcosm-uk-2024-25-local-k70"}, + {"country": "us"}, + ), + ids=("no-release", "another-release", "a-longer-k", "a-us-entry"), +) +def test_only_a_uk_entrys_release_admits_a_k(changes): + """A ``k`` is registered by a UK entry's ``release`` and by nothing else. + An entry whose variant and file name carry ``local-k7`` admits nothing + when its release is missing or names another release, and a US entry + that names the UK release admits nothing either.""" + + release_id = _uk_local_release_id(7) + spec = dataclasses.replace(uk_local_release_spec(release_id), **changes) + assert spec.variant == "local-k7" + with mock.patch.dict(REGISTRY, {spec.key: spec}): + assert not publishes_to_build_folder(release_id) + assert contract.unregistered_uk_local_release(release_id) + + +@pytest.mark.parametrize( + "release_id", + ( + "microcosm-uk-2024-25-local-k0", + "microcosm-uk-2024-25-local-k01", + "microcosm-uk-2024-25-local-k", + "microcosm-uk-2024-25-local-k7-extra", + *UNLISTED_IDS, + ), +) +def test_registering_a_name_outside_the_local_area_grammar_admits_nothing(release_id): + """The registry admits a UK local-area name only. An entry that names any + other release leaves ``publishes_to_build_folder`` unchanged, and such a + name never reads as an unregistered local-area release.""" + + spec = DatasetSpec( + country="uk", + year=2025, + variant="outside-the-grammar", + hf_repo="policyengine/populace-uk-private", + filename="outside.h5", + engine_module="policyengine_uk.data", + engine_class="UKSingleYearDataset", + engine_package="policyengine-uk", + release=release_id, + ) + with mock.patch.dict(REGISTRY, {spec.key: spec}): + assert not publishes_to_build_folder(release_id) + assert not contract.unregistered_uk_local_release(release_id) + + @pytest.mark.parametrize( "release_id", (*UNLISTED_IDS, TAGGED_US_ID, *contract.US_RELEASES) ) diff --git a/test_support/microcosm_data/build_folders.py b/test_support/microcosm_data/build_folders.py index d6f1ee5b74..030b6491a4 100644 --- a/test_support/microcosm_data/build_folders.py +++ b/test_support/microcosm_data/build_folders.py @@ -15,6 +15,8 @@ from pathlib import Path from typing import Any +from microcosm.data.registry import REGISTRY, DatasetSpec + #: A calibration attempt ID, the build every UK fixture names by default. UK_NATIONAL_BUILD = "uk-frs-calibration-attempt-20260920T120000Z-deadbeef" UK_DENSE_BUILD = "uk-local-candidate-f100-s42-20260920T120000Z-deadbeef" @@ -24,6 +26,33 @@ _RETIRED_BUILD_FIELDS = ("build_id", "attempt_id", "cut_tag") +def uk_local_release_spec(release: str) -> DatasetSpec: + """A registry entry that names the UK local-area release ``release``. + + The pull request that authorizes a ``k``'s first publication adds such an + entry, and ``publishes_to_build_folder`` admits the release from then on. + """ + line = release.removeprefix("microcosm-uk-2024-25-") + return DatasetSpec( + country="uk", + year=2025, + variant=line, + hf_repo="policyengine/populace-uk-private", + filename=f"microcosm_uk_2024_25_{line.replace('-', '_')}.h5", + engine_module="policyengine_uk.data", + engine_class="UKSingleYearDataset", + engine_package="policyengine-uk", + release=release, + ) + + +def register_uk_local_release(monkeypatch: Any, release: str) -> DatasetSpec: + """Name ``release`` in the registry for one test, and return its entry.""" + spec = uk_local_release_spec(release) + monkeypatch.setitem(REGISTRY, spec.key, spec) + return spec + + def artifact_role(key: str, kind: object) -> str: """The file role a build-folder manifest records for one artifact.""" if kind == "microdata": diff --git a/tools/generate_release_contract_fixtures.py b/tools/generate_release_contract_fixtures.py index f972111c4a..da26ae8f5e 100644 --- a/tools/generate_release_contract_fixtures.py +++ b/tools/generate_release_contract_fixtures.py @@ -79,8 +79,10 @@ (``CALIBRATION_DIAGNOSTICS_SCHEMA_VERSION``), the fiscal-target exclusion receipt's key, the dataset and calibration keys and file names, and the repository id. A pull request that changes one of those regenerates the -fixtures. A real build also writes its timing, its ledger provenance and its -optional gate blocks, and the fixtures freeze none of those. +fixtures. A real build also writes its timing, its ledger provenance, its +optional gate blocks, the other optional blocks its run passes (``base_pool`` +and ``post_export_scoring``, for example), and the blocks that its +gate-evidence files bind, and the fixtures freeze none of those. What the fixtures omit ---------------------- diff --git a/tools/publish_release.sh b/tools/publish_release.sh index 687b24186d..204dccd987 100755 --- a/tools/publish_release.sh +++ b/tools/publish_release.sh @@ -6,7 +6,8 @@ # releases.json, which the UK and US repositories both do since their # switches, so it publishes no UK or US release. # -# The UK national, dense and local-area releases and the US releases +# The UK national and dense releases, each UK local-area release that the +# dataset registry names, and the US releases # (microcosm-us-2024-national, -local, -receipt-donor, -evidence and -annual) publish # each build into its own folder through the emitter with # `microcosm-publish-release-build` instead, and the calibration diagnostics