|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build two frozen-clock representative daily preview workspaces and evidence.""" |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import argparse |
| 6 | +import hashlib |
| 7 | +import json |
| 8 | +import os |
| 9 | +import re |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +import tempfile |
| 13 | +from pathlib import Path |
| 14 | +from unittest.mock import patch |
| 15 | + |
| 16 | +from quant_advisor_research.advisory_report import build_advisory_report |
| 17 | +from quant_advisor_research.preview_bundle import read_preview_bundle |
| 18 | +from quant_advisor_research.preview_workspace import build_preview_workspace |
| 19 | + |
| 20 | +EVIDENCE_VERSION = "qar.d3.build_evidence.v1" |
| 21 | +FIXED_FILES = ("manifest.json", "report.html", "report.json") |
| 22 | +DEPENDENCY_INVENTORY = [ |
| 23 | + ".github/workflows/qar_d3_daily_preview_artifact.yml", |
| 24 | + "scripts/d3_build_daily_preview.py", |
| 25 | + "scripts/d3_verify_daily_preview.py", |
| 26 | + "src/quant_advisor_research/advisory_report.py", |
| 27 | + "src/quant_advisor_research/artifact_integrity.py", |
| 28 | + "src/quant_advisor_research/artifacts.py", |
| 29 | + "src/quant_advisor_research/contracts.py", |
| 30 | + "src/quant_advisor_research/csv_utils.py", |
| 31 | + "src/quant_advisor_research/period_contract.py", |
| 32 | + "src/quant_advisor_research/preview_bundle.py", |
| 33 | + "src/quant_advisor_research/preview_workspace.py", |
| 34 | + "src/quant_advisor_research/time_contract.py", |
| 35 | + "tests/test_d3_preview_artifact.py", |
| 36 | + "examples/political_events.example.csv", |
| 37 | + "examples/political_watchlist.example.csv", |
| 38 | + "pyproject.toml", |
| 39 | +] |
| 40 | + |
| 41 | + |
| 42 | +def _base_sha() -> str: |
| 43 | + return subprocess.check_output(["git", "rev-parse", "HEAD"], text=True).strip() |
| 44 | + |
| 45 | + |
| 46 | +def _file_hashes(workspace: Path) -> dict[str, dict[str, object]]: |
| 47 | + return { |
| 48 | + name: {"sha256": hashlib.sha256((workspace / name).read_bytes()).hexdigest(), "size": (workspace / name).stat().st_size} |
| 49 | + for name in FIXED_FILES |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +def _build_report(*, as_of: str, events: Path, watchlist: Path, frozen_generated_at: str) -> dict[str, object]: |
| 54 | + with patch("quant_advisor_research.advisory_report.utc_now_iso", return_value=frozen_generated_at): |
| 55 | + return build_advisory_report( |
| 56 | + as_of=as_of, |
| 57 | + cadence="daily", |
| 58 | + political_events_path=events, |
| 59 | + political_watchlist_path=watchlist, |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def build_evidence(args: argparse.Namespace) -> tuple[Path, dict[str, object]]: |
| 64 | + if any(not Path(path).is_file() for path in DEPENDENCY_INVENTORY): |
| 65 | + raise RuntimeError("dependency_inventory_invalid") |
| 66 | + temp_root = Path(args.temp_root) |
| 67 | + temp_root.mkdir(parents=True, exist_ok=True) |
| 68 | + parent = Path(tempfile.mkdtemp(prefix="qar-d3-parent-", dir=temp_root)) |
| 69 | + succeeded = False |
| 70 | + try: |
| 71 | + first_report = _build_report( |
| 72 | + as_of=args.as_of, |
| 73 | + events=Path(args.political_events), |
| 74 | + watchlist=Path(args.political_watchlist), |
| 75 | + frozen_generated_at=args.frozen_generated_at, |
| 76 | + ) |
| 77 | + second_report = _build_report( |
| 78 | + as_of=args.as_of, |
| 79 | + events=Path(args.political_events), |
| 80 | + watchlist=Path(args.political_watchlist), |
| 81 | + frozen_generated_at=args.frozen_generated_at, |
| 82 | + ) |
| 83 | + first = build_preview_workspace(first_report, parent) |
| 84 | + second = build_preview_workspace(second_report, parent) |
| 85 | + read_preview_bundle(first) |
| 86 | + read_preview_bundle(second) |
| 87 | + first_files = _file_hashes(first) |
| 88 | + second_files = _file_hashes(second) |
| 89 | + if first_files != second_files or any((first / name).read_bytes() != (second / name).read_bytes() for name in FIXED_FILES): |
| 90 | + raise RuntimeError("repeat_build_not_equal") |
| 91 | + manifest = json.loads((first / "manifest.json").read_text(encoding="utf-8")) |
| 92 | + source = manifest["source"] |
| 93 | + base_sha = args.base_sha or _base_sha() |
| 94 | + if not re.fullmatch(r"[0-9a-f]{40}", base_sha): |
| 95 | + raise RuntimeError("base_sha_invalid") |
| 96 | + if source != { |
| 97 | + "schema_version": "5", |
| 98 | + "contract_version": "model_recommendations.v5", |
| 99 | + "cadence": "daily", |
| 100 | + "as_of": args.as_of, |
| 101 | + "generated_at": args.frozen_generated_at, |
| 102 | + }: |
| 103 | + raise RuntimeError("source_contract_mismatch") |
| 104 | + evidence = { |
| 105 | + "evidence_version": EVIDENCE_VERSION, |
| 106 | + "base_sha": base_sha, |
| 107 | + "source": { |
| 108 | + "fixture_paths": [Path(args.political_events).as_posix(), Path(args.political_watchlist).as_posix()], |
| 109 | + "provenance": "repository_representative_fixture", |
| 110 | + }, |
| 111 | + "deterministic_clock": {"frozen_generated_at": args.frozen_generated_at, "producer_invocations": 2}, |
| 112 | + "workflow_dependency_inventory": DEPENDENCY_INVENTORY, |
| 113 | + "bundle": { |
| 114 | + "contract": manifest["bundle_contract"], |
| 115 | + "source": source, |
| 116 | + "files": first_files, |
| 117 | + }, |
| 118 | + "repeat_build": {"independent_invocations": 2, "bytes_equal": True, "files": second_files}, |
| 119 | + } |
| 120 | + evidence_path = Path(args.evidence_path) |
| 121 | + evidence_path.parent.mkdir(parents=True, exist_ok=True) |
| 122 | + evidence_path.write_text(json.dumps(evidence, ensure_ascii=False, sort_keys=True, separators=(",", ":")) + "\n", encoding="utf-8") |
| 123 | + Path(args.workspace_path_file).write_text(str(first) + "\n", encoding="utf-8") |
| 124 | + print(json.dumps({"workspace": str(first), "evidence": str(evidence_path), "base_sha": evidence["base_sha"]}, sort_keys=True)) |
| 125 | + succeeded = True |
| 126 | + return first, evidence |
| 127 | + finally: |
| 128 | + if not succeeded: |
| 129 | + try: |
| 130 | + parent.rmdir() |
| 131 | + except OSError: |
| 132 | + shutil.rmtree(parent, ignore_errors=True) |
| 133 | + |
| 134 | + |
| 135 | +def parse_args() -> argparse.Namespace: |
| 136 | + parser = argparse.ArgumentParser() |
| 137 | + parser.add_argument("--as-of", required=True) |
| 138 | + parser.add_argument("--political-events", required=True) |
| 139 | + parser.add_argument("--political-watchlist", required=True) |
| 140 | + parser.add_argument("--frozen-generated-at", required=True) |
| 141 | + parser.add_argument("--temp-root", default=os.environ.get("RUNNER_TEMP", tempfile.gettempdir())) |
| 142 | + parser.add_argument("--evidence-path", required=True) |
| 143 | + parser.add_argument("--workspace-path-file", required=True) |
| 144 | + parser.add_argument("--base-sha") |
| 145 | + return parser.parse_args() |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + build_evidence(parse_args()) |
0 commit comments