|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Render a deterministic pull-request body from an agent artifact bundle.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from typing import Any |
| 11 | + |
| 12 | +REPO_ROOT = Path(__file__).resolve().parents[1] |
| 13 | +if str(REPO_ROOT) not in sys.path: |
| 14 | + sys.path.insert(0, str(REPO_ROOT)) |
| 15 | + |
| 16 | +from scripts.validate_agent_artifact_bundle import DEFAULT_BUNDLE_PATH, _bundle_from_payload, _load_json_object, validate_bundle_payload |
| 17 | + |
| 18 | + |
| 19 | +def _bullet_list(values: list[str], empty: str) -> list[str]: |
| 20 | + if not values: |
| 21 | + return [f"- {empty}"] |
| 22 | + return [f"- `{value}`" for value in values] |
| 23 | + |
| 24 | + |
| 25 | +def _validation_lines(validation_evidence: object) -> list[str]: |
| 26 | + if not isinstance(validation_evidence, list) or not validation_evidence: |
| 27 | + return ["- No validation evidence provided in bundle."] |
| 28 | + |
| 29 | + lines: list[str] = [] |
| 30 | + for entry in validation_evidence: |
| 31 | + if not isinstance(entry, dict): |
| 32 | + continue |
| 33 | + command = entry.get("command") |
| 34 | + result = entry.get("result") |
| 35 | + if isinstance(command, str) and isinstance(result, str): |
| 36 | + lines.append(f"- `{command}`: `{result}`") |
| 37 | + return lines or ["- No validation evidence provided in bundle."] |
| 38 | + |
| 39 | + |
| 40 | +def _safe_gate_lines(safe_pr_gate: object) -> list[str]: |
| 41 | + if not isinstance(safe_pr_gate, dict): |
| 42 | + return ["- safe_pr_gate: `unavailable`"] |
| 43 | + |
| 44 | + lines = [ |
| 45 | + f"- result: `{safe_pr_gate.get('result')}`", |
| 46 | + f"- ok: `{str(safe_pr_gate.get('ok')).lower()}`", |
| 47 | + f"- allow_dirty: `{str(safe_pr_gate.get('allow_dirty')).lower()}`", |
| 48 | + ] |
| 49 | + problems = safe_pr_gate.get("problems") |
| 50 | + if isinstance(problems, list) and problems: |
| 51 | + lines.append("- problems:") |
| 52 | + lines.extend(f" - `{problem}`" for problem in problems if isinstance(problem, str)) |
| 53 | + else: |
| 54 | + lines.append("- problems: `none`") |
| 55 | + return lines |
| 56 | + |
| 57 | + |
| 58 | +def render_pr_body_from_bundle(bundle: dict[str, Any]) -> str: |
| 59 | + changed_files = bundle.get("changed_files") |
| 60 | + changed_file_lines = _bullet_list(changed_files if isinstance(changed_files, list) else [], "No changed files provided in bundle.") |
| 61 | + validation_lines = _validation_lines(bundle.get("validation_evidence")) |
| 62 | + safe_gate_lines = _safe_gate_lines(bundle.get("safe_pr_gate")) |
| 63 | + |
| 64 | + evidence_lines = [ |
| 65 | + f"- branch: `{bundle.get('branch')}`", |
| 66 | + f"- bundle_result: `{bundle.get('result')}`", |
| 67 | + ] |
| 68 | + mcp_ref = bundle.get("mcp_context_output_ref") |
| 69 | + if isinstance(mcp_ref, str): |
| 70 | + evidence_lines.append(f"- mcp_context_output_ref: `{mcp_ref}`") |
| 71 | + |
| 72 | + lines = [ |
| 73 | + "## Summary", |
| 74 | + "", |
| 75 | + "Deterministic agent artifact bundle evidence for this change.", |
| 76 | + "", |
| 77 | + "## Scope", |
| 78 | + "", |
| 79 | + *changed_file_lines, |
| 80 | + "", |
| 81 | + "## Validation", |
| 82 | + "", |
| 83 | + *validation_lines, |
| 84 | + "", |
| 85 | + "## Safety Gate", |
| 86 | + "", |
| 87 | + *safe_gate_lines, |
| 88 | + "", |
| 89 | + "## Evidence", |
| 90 | + "", |
| 91 | + *evidence_lines, |
| 92 | + "", |
| 93 | + ] |
| 94 | + return "\n".join(lines) |
| 95 | + |
| 96 | + |
| 97 | +def render_pr_body_from_payload(payload: dict[str, Any]) -> str: |
| 98 | + validation = validate_bundle_payload(payload) |
| 99 | + if not validation["ok"]: |
| 100 | + issues = "\n".join(f"- {issue}" for issue in validation["issues"]) |
| 101 | + raise RuntimeError(f"agent artifact bundle failed validation:\n{issues}") |
| 102 | + |
| 103 | + bundle, bundle_issues = _bundle_from_payload(payload) |
| 104 | + if bundle is None: |
| 105 | + raise RuntimeError("; ".join(bundle_issues)) |
| 106 | + return render_pr_body_from_bundle(bundle) |
| 107 | + |
| 108 | + |
| 109 | +def render_pr_body_from_file(path: Path) -> str: |
| 110 | + return render_pr_body_from_payload(_load_json_object(path)) |
| 111 | + |
| 112 | + |
| 113 | +def _parse_args(argv: list[str]) -> argparse.Namespace: |
| 114 | + parser = argparse.ArgumentParser(description="Render deterministic PR body Markdown from an agent artifact bundle.") |
| 115 | + parser.add_argument("--bundle", type=Path, default=DEFAULT_BUNDLE_PATH, help="Bundle JSON path.") |
| 116 | + return parser.parse_args(argv) |
| 117 | + |
| 118 | + |
| 119 | +def main(argv: list[str] | None = None) -> int: |
| 120 | + args = _parse_args(sys.argv[1:] if argv is None else argv) |
| 121 | + try: |
| 122 | + sys.stdout.write(render_pr_body_from_file(args.bundle)) |
| 123 | + return 0 |
| 124 | + except RuntimeError as exc: |
| 125 | + sys.stderr.write(f"{exc}\n") |
| 126 | + return 1 |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + raise SystemExit(main()) |
0 commit comments