From 7cf3bd7b7b7f948b5701c086663451c2d1806296 Mon Sep 17 00:00:00 2001 From: Maintenance Date: Tue, 24 Nov 2026 13:15:48 -0800 Subject: [PATCH 1/2] Normalize the report clock before reading project evidence --- .../portfolio_truth_reconcile.py | 3 ++ tests/test_snapshot_clock_normalization.py | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/test_snapshot_clock_normalization.py diff --git a/src/github_repo_auditor/portfolio_truth_reconcile.py b/src/github_repo_auditor/portfolio_truth_reconcile.py index 9c7cd61..1ba699c 100644 --- a/src/github_repo_auditor/portfolio_truth_reconcile.py +++ b/src/github_repo_auditor/portfolio_truth_reconcile.py @@ -910,6 +910,9 @@ def build_materialization_context( ) -> MaterializationContext: """Load catalog, registry, Notion context, and workspace discovery once.""" now = now or datetime.now(timezone.utc) + if now.utcoffset() is None: + raise ValueError("The report clock must be timezone-aware.") + now = now.astimezone(timezone.utc) catalog_data = load_portfolio_catalog(catalog_path) legacy_rows = load_legacy_registry_rows(legacy_registry_path) notion_context = load_safe_notion_project_context() if include_notion else {} diff --git a/tests/test_snapshot_clock_normalization.py b/tests/test_snapshot_clock_normalization.py new file mode 100644 index 0000000..3b1e739 --- /dev/null +++ b/tests/test_snapshot_clock_normalization.py @@ -0,0 +1,28 @@ +from datetime import datetime, timedelta, timezone + +import pytest + +from github_repo_auditor.portfolio_truth_reconcile import build_portfolio_truth_snapshot + + +def test_snapshot_clock_is_normalized_before_publication(tmp_path): + clock = datetime(2025, 2, 1, 4, 15, tzinfo=timezone(timedelta(hours=4))) + snapshot = build_portfolio_truth_snapshot( + workspace_root=tmp_path, + catalog_path=tmp_path / "missing.yaml", + legacy_registry_path=tmp_path / "missing.md", + include_notion=False, + now=clock, + ).snapshot + assert snapshot.to_dict()["generated_at"] == "2025-02-01T00:15:00+00:00" + + +def test_snapshot_clock_requires_an_offset_before_loading_inputs(tmp_path, monkeypatch): + monkeypatch.setattr( + "github_repo_auditor.portfolio_truth_reconcile.load_portfolio_catalog", + lambda _: pytest.fail("loaded catalog before validating report clock"), + ) + with pytest.raises(ValueError, match="timezone-aware"): + build_portfolio_truth_snapshot( + workspace_root=tmp_path, now=datetime(2025, 2, 1) + ) From b28efeb5cb031022a007498f4ca3a9e34cbb9e22 Mon Sep 17 00:00:00 2001 From: Maintenance Date: Tue, 24 Nov 2026 13:17:58 -0800 Subject: [PATCH 2/2] Validate explicit report clocks at the publication entry point --- .../portfolio_truth_publish.py | 14 ++++++++------ .../portfolio_truth_reconcile.py | 11 +++++++---- tests/test_snapshot_clock_normalization.py | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/github_repo_auditor/portfolio_truth_publish.py b/src/github_repo_auditor/portfolio_truth_publish.py index bdebd19..146ffd1 100644 --- a/src/github_repo_auditor/portfolio_truth_publish.py +++ b/src/github_repo_auditor/portfolio_truth_publish.py @@ -24,26 +24,23 @@ from github_repo_auditor.portfolio_cohort_plan_contract import ( COHORT_TRANSITION_PROTOCOL, ) +from github_repo_auditor.portfolio_truth_lineage import resolve_notion_origin from github_repo_auditor.portfolio_truth_reconcile import ( CohortR3Error, CohortTransitionOutcome, build_portfolio_truth_snapshot, load_prior_notion_context, + normalize_report_clock, ) from github_repo_auditor.portfolio_truth_render import ( render_portfolio_report_markdown, render_registry_markdown, ) -from github_repo_auditor.portfolio_truth_lineage import resolve_notion_origin from github_repo_auditor.portfolio_truth_sources import ( checkout_collision_paths, workspace_exclusion_reason, ) from github_repo_auditor.portfolio_truth_types import truth_latest_path -from github_repo_auditor.producer_preflight import ( - ProducerEvidence, - verify_evidence_still_current, -) from github_repo_auditor.portfolio_truth_validate import ( canonicalize_prior_security_truth_payload, validate_portfolio_report_markdown, @@ -51,12 +48,15 @@ validate_registry_markdown, validate_truth_snapshot, ) +from github_repo_auditor.producer_preflight import ( + ProducerEvidence, + verify_evidence_still_current, +) from github_repo_auditor.project_registry import ( build_project_registry, load_source_paths, ) - _PORTFOLIO_TRUTH_IN_PROCESS_LOCK = threading.Lock() @@ -548,6 +548,8 @@ def publish_portfolio_truth( require_producer_evidence: bool = False, now: datetime | None = None, ) -> PortfolioTruthPublishResult: + if now is not None: + now = normalize_report_clock(now) if ( security_coverage_metadata is not None or security_receipt_binding is not None ) and now is None: diff --git a/src/github_repo_auditor/portfolio_truth_reconcile.py b/src/github_repo_auditor/portfolio_truth_reconcile.py index 1ba699c..81673ee 100644 --- a/src/github_repo_auditor/portfolio_truth_reconcile.py +++ b/src/github_repo_auditor/portfolio_truth_reconcile.py @@ -896,6 +896,12 @@ def _projects_at_clock( return result +def normalize_report_clock(now: datetime) -> datetime: + if now.utcoffset() is None: + raise ValueError("The report clock must be timezone-aware.") + return now.astimezone(timezone.utc) + + def build_materialization_context( *, workspace_root: Path, @@ -909,10 +915,7 @@ def build_materialization_context( prior_notion_generated_at: str | None = None, ) -> MaterializationContext: """Load catalog, registry, Notion context, and workspace discovery once.""" - now = now or datetime.now(timezone.utc) - if now.utcoffset() is None: - raise ValueError("The report clock must be timezone-aware.") - now = now.astimezone(timezone.utc) + now = normalize_report_clock(now or datetime.now(timezone.utc)) catalog_data = load_portfolio_catalog(catalog_path) legacy_rows = load_legacy_registry_rows(legacy_registry_path) notion_context = load_safe_notion_project_context() if include_notion else {} diff --git a/tests/test_snapshot_clock_normalization.py b/tests/test_snapshot_clock_normalization.py index 3b1e739..78e8721 100644 --- a/tests/test_snapshot_clock_normalization.py +++ b/tests/test_snapshot_clock_normalization.py @@ -2,6 +2,7 @@ import pytest +from github_repo_auditor import portfolio_truth_publish as publish from github_repo_auditor.portfolio_truth_reconcile import build_portfolio_truth_snapshot @@ -26,3 +27,18 @@ def test_snapshot_clock_requires_an_offset_before_loading_inputs(tmp_path, monke build_portfolio_truth_snapshot( workspace_root=tmp_path, now=datetime(2025, 2, 1) ) + + +def test_publication_rejects_naive_clock_before_touching_sources(tmp_path, monkeypatch): + def unexpected_target_read(**kwargs): + pytest.fail("publication reached target validation before clock validation") + + monkeypatch.setattr(publish, "validate_publish_targets", unexpected_target_read) + with pytest.raises(ValueError, match="timezone-aware"): + publish.publish_portfolio_truth( + workspace_root=tmp_path / "workspace", + output_dir=tmp_path / "output", + registry_output=tmp_path / "workspace" / "registry.md", + portfolio_report_output=tmp_path / "workspace" / "report.md", + now=datetime(2025, 2, 1), + )