130 lines
4.9 KiB
Python
130 lines
4.9 KiB
Python
"""Initial implementation delivery workflow with independent transport readbacks."""
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import re
|
|
from pathlib import Path
|
|
from types import ModuleType
|
|
from typing import Any
|
|
|
|
|
|
_SHA40 = re.compile(r"^[0-9a-f]{40}$")
|
|
_REQUIRED = ("push", "read_remote_head", "create_or_reuse_pr", "read_pr")
|
|
|
|
|
|
def _load_delivery_adoption() -> ModuleType:
|
|
path = Path(__file__).resolve().parent / "delivery_adoption.py"
|
|
spec = importlib.util.spec_from_file_location("gitea_delivery_adoption", path)
|
|
if spec is None or spec.loader is None:
|
|
raise AdoptionError("delivery adoption unavailable")
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
_delivery_adoption = _load_delivery_adoption()
|
|
AdoptionError = _delivery_adoption.AdoptionError
|
|
|
|
|
|
def _reject(message: str) -> None:
|
|
raise AdoptionError(message)
|
|
|
|
|
|
def _validate_adapter(adapter: Any) -> None:
|
|
for name in _REQUIRED:
|
|
try:
|
|
value = getattr(adapter, name)
|
|
except Exception:
|
|
_reject("trusted delivery adapter required")
|
|
else:
|
|
if not callable(value):
|
|
_reject("trusted delivery adapter required")
|
|
|
|
|
|
def _validate_head(head: Any) -> str:
|
|
if type(head) is not str or not _SHA40.fullmatch(head):
|
|
_reject("invalid delivery head")
|
|
return head
|
|
|
|
|
|
def _validate_pr_number(value: Any) -> int:
|
|
if type(value) is not int or isinstance(value, bool) or value < 1:
|
|
_reject("invalid pull request number")
|
|
return value
|
|
|
|
|
|
def _validate_readback(value: Any, number: int, head: str) -> dict[str, Any]:
|
|
if type(value) is not dict:
|
|
_reject("invalid pull request readback")
|
|
if value.get("number") != number or type(value.get("number")) is not int:
|
|
_reject("pull request number mismatch")
|
|
if value.get("head") != head or type(value.get("head")) is not str:
|
|
_reject("pull request head mismatch")
|
|
if value.get("state") != "open":
|
|
_reject("pull request state mismatch")
|
|
# Bound the returned artifact even when the trusted adapter exposes richer data.
|
|
return {"number": number, "head": head, "state": "open"}
|
|
|
|
|
|
def deliver_implementation(adapter: Any, *, head: str, declaration: Any) -> dict[str, Any]:
|
|
"""Push an implementation and create/reuse a PR only after fresh gates."""
|
|
_validate_head(head)
|
|
_validate_adapter(adapter)
|
|
|
|
# The canonical helper performs the explicit declaration/adoption check
|
|
# immediately before the first external write.
|
|
_delivery_adoption.deliver(lambda: adapter.push(head), head=head, declaration=declaration)
|
|
|
|
remote_head = adapter.read_remote_head()
|
|
if remote_head != head or type(remote_head) is not str or not _SHA40.fullmatch(remote_head):
|
|
_reject("remote head mismatch")
|
|
|
|
# Re-check fresh evidence immediately before the PR write.
|
|
pr_number = _delivery_adoption.deliver(
|
|
lambda: adapter.create_or_reuse_pr(head), head=head, declaration=declaration
|
|
)
|
|
pr_number = _validate_pr_number(pr_number)
|
|
|
|
readback = adapter.read_pr(pr_number)
|
|
bounded_pr = _validate_readback(readback, pr_number, head)
|
|
return {"remote_head": remote_head, "pr": bounded_pr}
|
|
|
|
|
|
def reconcile_implementation(adapter: Any, *, head: str, number: int,
|
|
declaration: Any) -> dict[str, Any]:
|
|
"""Explicit read-only recovery after uncertain write acknowledgement.
|
|
|
|
The trusted main-owned adapter must freshly verify identity/scope, exact
|
|
intended body, protected Issue snapshot, and complete files/commits via
|
|
verify_reconciliation(head, number). Only literal True accepts that proof.
|
|
These callbacks MUST be read-only. This is not a transport sandbox, an
|
|
automatic exception handler, or permission to retry a write.
|
|
"""
|
|
_validate_head(head)
|
|
_validate_pr_number(number)
|
|
for name in ("read_remote_head", "read_pr", "verify_reconciliation"):
|
|
callback = None
|
|
try:
|
|
callback = getattr(adapter, name)
|
|
except Exception:
|
|
_reject("trusted reconciliation adapter required")
|
|
if not callable(callback):
|
|
_reject("trusted reconciliation adapter required")
|
|
|
|
remote_head = _delivery_adoption.deliver(
|
|
adapter.read_remote_head, head=head, declaration=declaration
|
|
)
|
|
if type(remote_head) is not str or remote_head != head:
|
|
_reject("remote head mismatch")
|
|
readback = _delivery_adoption.deliver(
|
|
lambda: adapter.read_pr(number), head=head, declaration=declaration
|
|
)
|
|
bounded_pr = _validate_readback(readback, number, head)
|
|
verified = _delivery_adoption.deliver(
|
|
lambda: adapter.verify_reconciliation(head, number),
|
|
head=head, declaration=declaration
|
|
)
|
|
if verified is not True:
|
|
_reject("reconciliation intent or protected state mismatch")
|
|
return {"remote_head": remote_head, "pr": bounded_pr}
|