Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions src/microplex_us/pipelines/ecps_replacement_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import argparse
import hashlib
import importlib.metadata
import json
import subprocess
from pathlib import Path
Expand Down Expand Up @@ -526,6 +527,7 @@ def build_sound_ecps_replacement_comparison(
baseline_dataset_path=baseline_path,
policyengine_targets_db_path=resolved_targets_db,
policyengine_us_data_repo=policyengine_us_data_repo,
policyengine_us_version=_installed_policyengine_us_version(),
period=period,
target_names=target_names,
target_scope=target_scope,
Expand Down Expand Up @@ -1728,6 +1730,7 @@ def _frozen_ecps_baseline_certificate(
baseline_dataset_path: Path,
policyengine_targets_db_path: Path | None,
policyengine_us_data_repo: str | Path | None,
policyengine_us_version: str,
period: int,
target_names: list[str],
target_scope: str,
Expand Down Expand Up @@ -1787,6 +1790,7 @@ def _frozen_ecps_baseline_certificate(
else None
),
"policyengine_us_data": _git_repo_descriptor(policyengine_us_data_repo),
"policyengine_us": {"version": str(policyengine_us_version)},
"target_surface": {
"target_profile": "pe_native_broad",
"target_scope": str(target_scope),
Expand All @@ -1802,6 +1806,13 @@ def _frozen_ecps_baseline_certificate(
}


def _installed_policyengine_us_version() -> str:
try:
return importlib.metadata.version("policyengine-us")
except importlib.metadata.PackageNotFoundError as exc:
raise ValueError("policyengine-us is not installed") from exc


def _git_repo_descriptor(repo_path: str | Path | None) -> dict[str, Any] | None:
if repo_path is None:
return None
Expand Down
111 changes: 109 additions & 2 deletions src/microplex_us/pipelines/mp300k_artifact_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
"_imputation_source",
)
_REQUIRED_BENCHMARK_MANIFEST_EVIDENCE = {
"certificate_type": (
("certificate_type",),
("frozen_ecps_baseline_certificate", "certificate_type"),
),
"period": (
("period",),
("target_period",),
),
"baseline_dataset.path": (
("baseline_dataset", "path"),
("baseline_dataset_path",),
Expand All @@ -136,16 +144,38 @@
("policyengine_us", "version"),
("policyengine_us_version",),
),
"target_surface.target_profile": (
("target_surface", "target_profile"),
("target_profile",),
),
"target_surface.target_scope": (
("target_surface", "target_scope"),
("target_scope",),
("target_scope_filter",),
),
"target_surface.target_count": (
("target_surface", "target_count"),
("target_count",),
),
"target_surface.target_names_sha256": (
("target_surface", "target_names_sha256"),
("target_names_sha256",),
),
"scoring_config.sha256": (
("scoring_config", "sha256"),
("scoring_config_sha256",),
),
"target_db.path": (
("target_db", "path"),
("target_db_path",),
("targets_db", "path"),
("policyengine_targets_db",),
("policyengine_targets_db", "path"),
),
"target_db.sha256": (
("target_db", "sha256"),
("target_db_sha256",),
("targets_db", "sha256"),
("policyengine_targets_db", "sha256"),
("policyengine_targets_db_sha256",),
),
}
Expand Down Expand Up @@ -238,6 +268,7 @@ def build_mp300k_artifact_gate_report(
ecps_comparison_gate = _ecps_comparison_gate(
resolved_ecps_comparison,
benchmark_evidence=benchmark_evidence,
expected_period=period,
)
arch_coverage_gate = _arch_target_coverage_gate(
arch_coverage_payload,
Expand Down Expand Up @@ -987,6 +1018,7 @@ def _ecps_comparison_gate(
ecps_comparison_payload: dict[str, Any] | None,
*,
benchmark_evidence: dict[str, Any] | None = None,
expected_period: int,
) -> dict[str, Any]:
if ecps_comparison_payload is None:
return _gate(
Expand Down Expand Up @@ -1022,6 +1054,7 @@ def _ecps_comparison_gate(
ecps_comparison_payload,
summary,
benchmark_evidence=benchmark_evidence,
expected_period=expected_period,
)
details.update(contract["details"])
missing_requirements = list(contract["missing_requirements"])
Expand Down Expand Up @@ -1101,6 +1134,7 @@ def _ecps_comparison_contract_summary(
summary: dict[str, Any],
*,
benchmark_evidence: dict[str, Any] | None = None,
expected_period: int,
) -> dict[str, Any]:
candidate_households = _first_nested_present(
payload,
Expand Down Expand Up @@ -1216,6 +1250,7 @@ def _ecps_comparison_contract_summary(
payload,
summary,
benchmark_evidence=benchmark_evidence,
expected_period=expected_period,
)

requirements = {
Expand Down Expand Up @@ -1255,6 +1290,7 @@ def _frozen_baseline_certificate_summary(
summary: dict[str, Any],
*,
benchmark_evidence: dict[str, Any] | None,
expected_period: int,
) -> dict[str, Any]:
certificate = _find_frozen_baseline_certificate(payload)
if not isinstance(certificate, dict):
Expand All @@ -1275,8 +1311,32 @@ def _frozen_baseline_certificate_summary(
"actual": schema_version,
}
)
certificate_type = certificate.get("certificate_type")
if certificate_type != "frozen_production_ecps_baseline":
mismatches.append(
{
"field": "certificate_type",
"expected": "frozen_production_ecps_baseline",
"actual": certificate_type,
}
)
certificate_period = certificate.get("period")
try:
certificate_period_int = int(certificate_period)
except (TypeError, ValueError):
certificate_period_int = None
if certificate_period_int != int(expected_period):
mismatches.append(
{
"field": "period",
"expected": int(expected_period),
"actual": certificate_period,
}
)

evidence_values = {
"certificate_type": certificate_type,
"period": certificate_period,
"baseline_dataset.sha256": _first_nested_path_value(
certificate,
(
Expand Down Expand Up @@ -1305,6 +1365,28 @@ def _frozen_baseline_certificate_summary(
("policyengine_us_data_commit_sha",),
),
),
"policyengine_us.version": _first_nested_path_value(
certificate,
(
("policyengine_us", "version"),
("policyengine_us_version",),
),
),
"target_surface.target_profile": _first_nested_path_value(
certificate,
(
("target_surface", "target_profile"),
("target_profile",),
),
),
"target_surface.target_scope": _first_nested_path_value(
certificate,
(
("target_surface", "target_scope"),
("target_scope",),
("target_scope_filter",),
),
),
"scoring_config.sha256": _first_nested_path_value(
certificate,
(
Expand Down Expand Up @@ -1356,9 +1438,17 @@ def _frozen_baseline_certificate_summary(
)

for evidence_name in (
"certificate_type",
"period",
"baseline_dataset.sha256",
"target_db.sha256",
"policyengine_us_data.commit",
"policyengine_us.version",
"target_surface.target_profile",
"target_surface.target_scope",
"target_surface.target_count",
"target_surface.target_names_sha256",
"scoring_config.sha256",
):
expected_value = (benchmark_evidence or {}).get(evidence_name)
certificate_value = evidence_values.get(evidence_name)
Expand All @@ -1384,7 +1474,10 @@ def _frozen_baseline_certificate_summary(
"policyengine_us_data_commit": evidence_values.get(
"policyengine_us_data.commit"
),
"policyengine_us_version": evidence_values.get("policyengine_us.version"),
"scoring_config_sha256": evidence_values.get("scoring_config.sha256"),
"target_profile": evidence_values.get("target_surface.target_profile"),
"target_scope": evidence_values.get("target_surface.target_scope"),
"target_names_sha256": evidence_values.get(
"target_surface.target_names_sha256"
),
Expand Down Expand Up @@ -1422,11 +1515,18 @@ def _certificate_metric(certificate: dict[str, Any], metric_name: str) -> Any:


def _valid_certificate_evidence_value(name: str, value: Any) -> bool:
if name == "target_surface.target_count":
if name in {"period", "target_surface.target_count"}:
try:
return int(value) > 0
except (TypeError, ValueError):
return False
if name == "certificate_type":
return value == "frozen_production_ecps_baseline"
if name.endswith(".version") or name in {
"target_surface.target_profile",
"target_surface.target_scope",
}:
return isinstance(value, str) and bool(value)
if name.endswith(".sha256"):
return (
isinstance(value, str)
Expand Down Expand Up @@ -2103,6 +2203,13 @@ def _first_nested_path_value(


def _valid_benchmark_evidence_value(name: str, value: Any) -> bool:
if name in {"period", "target_surface.target_count"}:
try:
return int(value) > 0
except (TypeError, ValueError):
return False
if name == "certificate_type":
return value == "frozen_production_ecps_baseline"
if not isinstance(value, str) or not value:
return False
if name.endswith(".sha256"):
Expand Down
27 changes: 27 additions & 0 deletions src/microplex_us/pipelines/mp_benchmark_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def build_mp_benchmark_manifest(
target_db_path: str | Path,
period: int = 2024,
target_profile: str = "pe_native_broad",
target_scope: str = "all",
target_count: int,
target_names_sha256: str,
scoring_config_sha256: str,
certificate_type: str = "frozen_production_ecps_baseline",
policyengine_us_data_repo: str | Path | None = None,
policyengine_us_data_commit: str | None = None,
policyengine_us_version: str | None = None,
Expand All @@ -42,9 +47,18 @@ def build_mp_benchmark_manifest(
version = policyengine_us_version or _installed_policyengine_us_version()
return {
"schema_version": 1,
"certificate_type": str(certificate_type),
"generated_at": datetime.now(UTC).isoformat(),
"period": int(period),
"target_profile": str(target_profile),
"target_scope": str(target_scope),
"target_surface": {
"target_profile": str(target_profile),
"target_scope": str(target_scope),
"target_count": int(target_count),
"target_names_sha256": str(target_names_sha256),
},
"scoring_config": {"sha256": str(scoring_config_sha256)},
"baseline_dataset": baseline_dataset,
"policyengine_us_data": repo_descriptor,
"policyengine_us": {"version": version},
Expand Down Expand Up @@ -142,6 +156,14 @@ def main(argv: list[str] | None = None) -> int:
parser.add_argument("--output-json", required=True)
parser.add_argument("--period", type=int, default=2024)
parser.add_argument("--target-profile", default="pe_native_broad")
parser.add_argument("--target-scope", default="all")
parser.add_argument("--target-count", type=int, required=True)
parser.add_argument("--target-names-sha256", required=True)
parser.add_argument("--scoring-config-sha256", required=True)
parser.add_argument(
"--certificate-type",
default="frozen_production_ecps_baseline",
)
parser.add_argument("--policyengine-us-data-repo")
parser.add_argument("--policyengine-us-data-commit")
parser.add_argument("--policyengine-us-version")
Expand All @@ -161,6 +183,11 @@ def main(argv: list[str] | None = None) -> int:
target_db_path=args.target_db,
period=args.period,
target_profile=args.target_profile,
target_scope=args.target_scope,
target_count=args.target_count,
target_names_sha256=args.target_names_sha256,
scoring_config_sha256=args.scoring_config_sha256,
certificate_type=args.certificate_type,
policyengine_us_data_repo=args.policyengine_us_data_repo,
policyengine_us_data_commit=args.policyengine_us_data_commit,
policyengine_us_version=args.policyengine_us_version,
Expand Down
Loading
Loading