From fbd252eba340260ea9075ac6c800be02e6a96783 Mon Sep 17 00:00:00 2001 From: Maintenance Date: Tue, 24 Nov 2026 13:31:02 -0800 Subject: [PATCH] Serialize snapshot objects in stable key order --- .../portfolio_truth_publish.py | 4 +- tests/test_snapshot_json_order.py | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/test_snapshot_json_order.py diff --git a/src/github_repo_auditor/portfolio_truth_publish.py b/src/github_repo_auditor/portfolio_truth_publish.py index 146ffd1..4dc6221 100644 --- a/src/github_repo_auditor/portfolio_truth_publish.py +++ b/src/github_repo_auditor/portfolio_truth_publish.py @@ -803,7 +803,9 @@ def _publish_portfolio_truth_locked( allow_empty_notion=allow_empty_notion, ) latest_name = latest_path.name - snapshot_json = json.dumps(build_result.snapshot.to_dict(), indent=2) + "\n" + snapshot_json = ( + json.dumps(build_result.snapshot.to_dict(), indent=2, sort_keys=True) + "\n" + ) project_registry_json = _build_project_registry_json( build_result.snapshot, include_notion=include_notion ) diff --git a/tests/test_snapshot_json_order.py b/tests/test_snapshot_json_order.py new file mode 100644 index 0000000..05d98d4 --- /dev/null +++ b/tests/test_snapshot_json_order.py @@ -0,0 +1,44 @@ +import json +import subprocess +from datetime import datetime, timezone + +from github_repo_auditor.portfolio_truth_publish import publish_portfolio_truth + + +def test_snapshot_bytes_ignore_security_metadata_key_order(tmp_path, monkeypatch): + monkeypatch.setattr( + "github_repo_auditor.portfolio_truth_publish.load_source_paths", + lambda _: dict(bridge_db=None, notion_snapshot=None, memory_dir=None), + ) + workspace = tmp_path / "workspace" + repo = workspace / "sample" + repo.mkdir(parents=True) + subprocess.run(["git", "init", "-q", str(repo)], check=True) + (repo / "README.md").write_text("Sample") + clock = datetime(2025, 1, 3, tzinfo=timezone.utc) + metadata = { + "source_id": "github-security-coverage-receipt", + "schema_version": "GitHubSecurityCoverageReceiptV1", + "produced_at": clock.isoformat(), + "state": "fresh", + "age_hours": 0.0, + "producer_commit": "a" * 40, + "cohort_policy": "portfolio-default-attention-v1", + "cohort_repository_count": 0, + "path": "fixture-receipt.json", + } + publications = [] + for index, evidence in enumerate((metadata, dict(reversed(metadata.items())))): + generation = publish_portfolio_truth( + workspace_root=workspace, + output_dir=tmp_path / f"generation-{index}", + registry_output=workspace / "registry.md", + portfolio_report_output=workspace / "report.md", + catalog_path=tmp_path / "missing.yaml", + include_notion=False, + now=clock, + security_coverage_metadata=evidence, + ) + publications.append(generation.latest_path.read_bytes()) + assert json.loads(publications[0]) == json.loads(publications[1]) + assert publications[0] == publications[1]