|
| 1 | +"""Isolated GitHub Actions preflight for same-run weekly lock acquisition. |
| 2 | +
|
| 3 | +This is test-only harness code. It uses a fixed representative snapshot and |
| 4 | +never derives a period from the wall clock. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import argparse |
| 10 | +import hashlib |
| 11 | +import json |
| 12 | +import re |
| 13 | +from pathlib import Path |
| 14 | +from typing import Any |
| 15 | + |
| 16 | +from political_event_tracking_research.weekly_period_lock import ( |
| 17 | + LOCK_VERSION, |
| 18 | + PeriodLockError, |
| 19 | + parse_period_lock, |
| 20 | + parse_period_lock_bytes, |
| 21 | + serialize_period_lock, |
| 22 | +) |
| 23 | + |
| 24 | +BUNDLE_VERSION = "pert.weekly.period_lock_acquisition.v1" |
| 25 | +BUNDLE_FILES = ("bundle_manifest.json", "input_snapshot.json", "period_lock.json") |
| 26 | +SNAPSHOT_VERSION = "pert.weekly.input_snapshot.v1" |
| 27 | +_RUN_ID_RE = re.compile(r"^[1-9][0-9]*$") |
| 28 | + |
| 29 | + |
| 30 | +def expected_artifact_name(run_id: str) -> str: |
| 31 | + _validate_run_id(run_id) |
| 32 | + return f"pert-weekly-period-lock-{run_id}" |
| 33 | + |
| 34 | + |
| 35 | +def _validate_run_id(run_id: object) -> str: |
| 36 | + if type(run_id) is not str or not _RUN_ID_RE.fullmatch(run_id): |
| 37 | + raise ValueError("period_lock_run_invalid") |
| 38 | + return run_id |
| 39 | + |
| 40 | + |
| 41 | +def _canonical_json(value: object, error_code: str) -> bytes: |
| 42 | + try: |
| 43 | + return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode("utf-8") |
| 44 | + except (TypeError, ValueError, UnicodeError, OverflowError, RecursionError): |
| 45 | + raise ValueError(error_code) from None |
| 46 | + |
| 47 | + |
| 48 | +def _parse_canonical_json(raw: bytes, error_code: str) -> dict[str, Any]: |
| 49 | + if type(raw) is not bytes: |
| 50 | + raise ValueError(error_code) |
| 51 | + |
| 52 | + def pairs(items: list[tuple[str, object]]) -> dict[str, object]: |
| 53 | + result: dict[str, object] = {} |
| 54 | + for key, value in items: |
| 55 | + if key in result: |
| 56 | + raise ValueError(error_code) |
| 57 | + result[key] = value |
| 58 | + return result |
| 59 | + |
| 60 | + def reject_constant(_: str) -> None: |
| 61 | + raise ValueError(error_code) |
| 62 | + |
| 63 | + try: |
| 64 | + value = json.loads(raw.decode("utf-8"), object_pairs_hook=pairs, parse_constant=reject_constant) |
| 65 | + except ValueError: |
| 66 | + raise ValueError(error_code) from None |
| 67 | + if not isinstance(value, dict) or _canonical_json(value, error_code) != raw: |
| 68 | + raise ValueError(error_code) |
| 69 | + return value |
| 70 | + |
| 71 | + |
| 72 | +def _sha256(raw: bytes) -> str: |
| 73 | + return hashlib.sha256(raw).hexdigest() |
| 74 | + |
| 75 | + |
| 76 | +def _fixture_lock_payload(run_id: str) -> dict[str, object]: |
| 77 | + return { |
| 78 | + "lock_version": LOCK_VERSION, |
| 79 | + "calendar": "utc_iso_week_monday_sunday", |
| 80 | + "period_start": "2026-07-06", |
| 81 | + "period_end_exclusive": "2026-07-13", |
| 82 | + "as_of": "2026-07-12", |
| 83 | + "workflow_ref": "QuantStrategyLab/PoliticalEventTrackingResearch/.github/workflows/pert_weekly_period_lock_acquisition.yml@refs/heads/main", |
| 84 | + "source_run_id": run_id, |
| 85 | + "source_attempt": 1, |
| 86 | + "producer_ref": "a" * 40, |
| 87 | + "source_snapshot_id": "pert_weekly_input_snapshot_20260712", |
| 88 | + "source_snapshot_digest": "b" * 64, |
| 89 | + "source_provenance": "official_political_event_tracking_research_v1", |
| 90 | + "source_artifacts": [ |
| 91 | + {"path": "data/live/source_events.csv", "sha256": "c" * 64, "row_count": 11}, |
| 92 | + {"path": "data/live/source_manifest.json", "sha256": "d" * 64, "row_count": 1}, |
| 93 | + ], |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def _fixture_snapshot(lock_payload: dict[str, object]) -> dict[str, object]: |
| 98 | + return { |
| 99 | + "snapshot_version": SNAPSHOT_VERSION, |
| 100 | + "source_run_id": lock_payload["source_run_id"], |
| 101 | + "source_snapshot_id": lock_payload["source_snapshot_id"], |
| 102 | + "source_snapshot_digest": lock_payload["source_snapshot_digest"], |
| 103 | + "source_artifacts": lock_payload["source_artifacts"], |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +def _assert_empty_output_dir(output_dir: Path) -> None: |
| 108 | + if output_dir.is_symlink() or not output_dir.is_dir(): |
| 109 | + raise ValueError("period_lock_output_dir_invalid") |
| 110 | + try: |
| 111 | + if any(output_dir.iterdir()): |
| 112 | + raise ValueError("period_lock_output_not_empty") |
| 113 | + except OSError: |
| 114 | + raise ValueError("period_lock_output_dir_invalid") from None |
| 115 | + |
| 116 | + |
| 117 | +def build_bundle(output_dir: Path, run_id: str, artifact_name: str | None = None) -> dict[str, object]: |
| 118 | + run_id = _validate_run_id(run_id) |
| 119 | + expected_name = expected_artifact_name(run_id) |
| 120 | + if artifact_name is not None and artifact_name != expected_name: |
| 121 | + raise ValueError("period_lock_artifact_name_mismatch") |
| 122 | + _assert_empty_output_dir(output_dir) |
| 123 | + |
| 124 | + lock_payload = _fixture_lock_payload(run_id) |
| 125 | + lock = parse_period_lock(lock_payload) |
| 126 | + lock_bytes = serialize_period_lock(lock) |
| 127 | + snapshot = _fixture_snapshot(lock_payload) |
| 128 | + snapshot_bytes = _canonical_json(snapshot, "period_lock_snapshot_invalid") |
| 129 | + manifest = { |
| 130 | + "bundle_version": BUNDLE_VERSION, |
| 131 | + "artifact_name": expected_name, |
| 132 | + "source_run_id": run_id, |
| 133 | + "source_attempt": 1, |
| 134 | + "lock_version": LOCK_VERSION, |
| 135 | + "snapshot_version": SNAPSHOT_VERSION, |
| 136 | + "files": [ |
| 137 | + {"name": "input_snapshot.json", "sha256": _sha256(snapshot_bytes)}, |
| 138 | + {"name": "period_lock.json", "sha256": _sha256(lock_bytes)}, |
| 139 | + ], |
| 140 | + } |
| 141 | + manifest_bytes = _canonical_json(manifest, "period_lock_manifest_invalid") |
| 142 | + payloads = { |
| 143 | + "period_lock.json": lock_bytes, |
| 144 | + "input_snapshot.json": snapshot_bytes, |
| 145 | + "bundle_manifest.json": manifest_bytes, |
| 146 | + } |
| 147 | + try: |
| 148 | + for name, raw in payloads.items(): |
| 149 | + with (output_dir / name).open("xb") as handle: |
| 150 | + handle.write(raw) |
| 151 | + except (OSError, ValueError): |
| 152 | + raise ValueError("period_lock_bundle_write_invalid") from None |
| 153 | + return verify_bundle(output_dir, run_id, expected_name) |
| 154 | + |
| 155 | + |
| 156 | +def _read_exact_bundle(output_dir: Path) -> dict[str, bytes]: |
| 157 | + if output_dir.is_symlink() or not output_dir.is_dir(): |
| 158 | + raise ValueError("period_lock_bundle_shape_invalid") |
| 159 | + try: |
| 160 | + names = {path.name for path in output_dir.iterdir()} |
| 161 | + except OSError: |
| 162 | + raise ValueError("period_lock_bundle_shape_invalid") from None |
| 163 | + if names != set(BUNDLE_FILES): |
| 164 | + raise ValueError("period_lock_bundle_shape_invalid") |
| 165 | + result: dict[str, bytes] = {} |
| 166 | + for name in BUNDLE_FILES: |
| 167 | + path = output_dir / name |
| 168 | + try: |
| 169 | + if path.is_symlink() or not path.is_file(): |
| 170 | + raise ValueError("period_lock_bundle_member_invalid") |
| 171 | + result[name] = path.read_bytes() |
| 172 | + except (OSError, ValueError): |
| 173 | + raise ValueError("period_lock_bundle_member_invalid") from None |
| 174 | + return result |
| 175 | + |
| 176 | + |
| 177 | +def verify_bundle(output_dir: Path, expected_run_id: str, expected_artifact: str) -> dict[str, object]: |
| 178 | + expected_run_id = _validate_run_id(expected_run_id) |
| 179 | + if expected_artifact != expected_artifact_name(expected_run_id): |
| 180 | + raise ValueError("period_lock_artifact_name_mismatch") |
| 181 | + payloads = _read_exact_bundle(output_dir) |
| 182 | + try: |
| 183 | + lock = parse_period_lock_bytes(payloads["period_lock.json"]) |
| 184 | + except PeriodLockError: |
| 185 | + raise ValueError("period_lock_invalid") from None |
| 186 | + if serialize_period_lock(lock) != payloads["period_lock.json"] or lock.source_run_id != expected_run_id or lock.source_attempt != 1: |
| 187 | + raise ValueError("period_lock_run_mismatch") |
| 188 | + snapshot = _parse_canonical_json(payloads["input_snapshot.json"], "period_lock_snapshot_invalid") |
| 189 | + expected_snapshot = _fixture_snapshot(_fixture_lock_payload(expected_run_id)) |
| 190 | + if snapshot != expected_snapshot: |
| 191 | + raise ValueError("period_lock_snapshot_mismatch") |
| 192 | + manifest = _parse_canonical_json(payloads["bundle_manifest.json"], "period_lock_manifest_invalid") |
| 193 | + if set(manifest) != {"bundle_version", "artifact_name", "source_run_id", "source_attempt", "lock_version", "snapshot_version", "files"}: |
| 194 | + raise ValueError("period_lock_manifest_invalid") |
| 195 | + if ( |
| 196 | + manifest["bundle_version"] != BUNDLE_VERSION |
| 197 | + or manifest["artifact_name"] != expected_artifact |
| 198 | + or manifest["source_run_id"] != expected_run_id |
| 199 | + or type(manifest["source_attempt"]) is not int |
| 200 | + or manifest["source_attempt"] != 1 |
| 201 | + or manifest["lock_version"] != LOCK_VERSION |
| 202 | + or manifest["snapshot_version"] != SNAPSHOT_VERSION |
| 203 | + ): |
| 204 | + raise ValueError("period_lock_manifest_mismatch") |
| 205 | + files = manifest["files"] |
| 206 | + if not isinstance(files, list) or {item.get("name") for item in files if isinstance(item, dict)} != {"input_snapshot.json", "period_lock.json"}: |
| 207 | + raise ValueError("period_lock_manifest_files_invalid") |
| 208 | + for item in files: |
| 209 | + if not isinstance(item, dict) or set(item) != {"name", "sha256"} or item["name"] not in payloads: |
| 210 | + raise ValueError("period_lock_manifest_files_invalid") |
| 211 | + if item["sha256"] != _sha256(payloads[item["name"]]): |
| 212 | + raise ValueError("period_lock_artifact_digest_mismatch") |
| 213 | + return { |
| 214 | + "bundle_version": BUNDLE_VERSION, |
| 215 | + "artifact_name": expected_artifact, |
| 216 | + "source_run_id": expected_run_id, |
| 217 | + "source_attempt": 1, |
| 218 | + "files": {name: _sha256(raw) for name, raw in sorted(payloads.items())}, |
| 219 | + } |
| 220 | + |
| 221 | + |
| 222 | +def main() -> int: |
| 223 | + parser = argparse.ArgumentParser() |
| 224 | + subparsers = parser.add_subparsers(dest="command", required=True) |
| 225 | + build = subparsers.add_parser("build") |
| 226 | + build.add_argument("--output-dir", type=Path, required=True) |
| 227 | + build.add_argument("--run-id", required=True) |
| 228 | + build.add_argument("--artifact-name") |
| 229 | + verify = subparsers.add_parser("verify") |
| 230 | + verify.add_argument("--bundle-dir", type=Path, required=True) |
| 231 | + verify.add_argument("--run-id", required=True) |
| 232 | + verify.add_argument("--artifact-name", required=True) |
| 233 | + args = parser.parse_args() |
| 234 | + evidence = ( |
| 235 | + build_bundle(args.output_dir, args.run_id, args.artifact_name) |
| 236 | + if args.command == "build" |
| 237 | + else verify_bundle(args.bundle_dir, args.run_id, args.artifact_name) |
| 238 | + ) |
| 239 | + print(json.dumps(evidence, sort_keys=True, separators=(",", ":"))) |
| 240 | + return 0 |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == "__main__": |
| 244 | + raise SystemExit(main()) |
0 commit comments