#!/usr/bin/env python3 """Build the fresh terminal result for a mediator-published SCM retry.""" from __future__ import annotations import re from typing import Any from execution_pool_protocol import ProtocolError FIELDS = ("changed_files", "tests_run", "artifacts", "findings", "blockers") SHA = re.compile(r"[0-9a-f]{40,64}\Z") def result(receipt: Any, submission: Any, node: str) -> tuple[dict[str, Any], str, str]: """Reuse completed evidence only after the mediator confirms its exact head.""" if not isinstance(receipt, dict) or not isinstance(submission, dict): raise ProtocolError("publication retry response is malformed") structured = receipt.get("structured") title, body, head = receipt.get("title"), receipt.get("body"), receipt.get("head") if ( not isinstance(structured, dict) or structured.get("status") != "completed" or not isinstance(title, str) or not title or not isinstance(body, str) or not isinstance(head, str) or not SHA.fullmatch(head) or submission.get("head") != head ): raise ProtocolError("publication retry evidence changed") copied = {"status": "completed", "summary": str(structured.get("summary") or "")[:8000]} if not copied["summary"]: raise ProtocolError("publication retry summary is missing") for name in FIELDS: value = structured.get(name) if not isinstance(value, list) or any(not isinstance(item, str) for item in value): raise ProtocolError("publication retry result is malformed") copied[name] = list(value[:100]) for artifact in (submission.get("pull_request"), f"branch:{submission.get('branch', '')}", f"preserved-head:{head}"): if isinstance(artifact, str) and artifact and artifact not in copied["artifacts"]: copied["artifacts"].append(artifact) return ({"structured": copied, "returncode": 0, "capacity_failure": False, "node": node, "route": {"provider": "mediator", "model": "publication-resume"}, "provider_sessions": {}, "final_activity": "Mediator republished preserved SCM commit."}, title[:240], body[:12000]) def transient_failure(node: str) -> tuple[dict[str, Any], str, str]: """Return the sole mediator-classified retryable publication failure.""" structured = {"status": "blocked", "summary": "SCM publication is temporarily unavailable.", "changed_files": [], "tests_run": [], "artifacts": [], "findings": [], "blockers": ["Mediator classified the preserved SCM publication as transient."]} return ({"structured": structured, "returncode": 1, "capacity_failure": True, "publication_retry_transient": True, "node": node, "route": {}, "provider_sessions": {}, "final_activity": "Mediator deferred preserved SCM publication."}, "SCM publication deferred", "") def rejected_failure(node: str) -> tuple[dict[str, Any], str, str]: """Surface mediator policy rejection without treating it as capacity loss.""" structured = {"status": "blocked", "summary": "SCM publication retry was rejected.", "changed_files": [], "tests_run": [], "artifacts": [], "findings": [], "blockers": ["Mediator rejected the preserved SCM publication evidence or policy."]} return ({"structured": structured, "returncode": 1, "capacity_failure": False, "node": node, "route": {}, "provider_sessions": {}, "final_activity": "Mediator rejected publication retry."}, "SCM publication rejected", "")