autonomy-unit-tests Mission Stage-2 gate migration checks passed at exact head
autonomy-smoke-tests Mission Stage-2 gate migration checks passed at exact head
git-diff-check Mission Stage-2 gate migration checks passed at exact head
py-compile Mission Stage-2 gate migration checks passed at exact head
reconciliation-contract Mission Stage-2 gate migration checks passed at exact head
release-state-machine Mission Stage-2 gate migration checks passed at exact head
secret-scan Mission Stage-2 gate migration checks passed at exact head
signer-validator-contract Mission Stage-2 gate migration checks passed at exact head
owner-approval-gate Mission Stage-2 gate migration checks passed at exact head
campaign-approval-gate Mission Stage-2 gate migration checks passed at exact head
109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
"""Atomic, predicate-bound activation of the durable release campaign."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import tempfile
|
|
from typing import Any, Mapping
|
|
|
|
from release_campaign_authorization import CampaignError, validate_policy
|
|
|
|
OID_RE = re.compile(r"^[0-9a-f]{40}$")
|
|
CAMPAIGN_ID = "AIOS-AUTONOMOUS-RELEASE-CAMPAIGN-20260807"
|
|
|
|
|
|
class CampaignActivationError(CampaignError):
|
|
pass
|
|
|
|
|
|
def canonical_json(value: Any) -> bytes:
|
|
return (json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False) + "\n").encode("utf-8")
|
|
|
|
|
|
def digest_bytes(value: bytes) -> str:
|
|
return hashlib.sha256(value).hexdigest()
|
|
|
|
|
|
def build_active_policy(current: Mapping[str, Any], facts: Mapping[str, Any]) -> dict[str, Any]:
|
|
policy = validate_policy(dict(current))
|
|
required = {
|
|
"campaign_id",
|
|
"repository",
|
|
"source_main_oid",
|
|
"gitea_main_oid",
|
|
"authority_main_oid",
|
|
"runtime_source_oid",
|
|
"durable_job_state",
|
|
"writer_ready",
|
|
"gitea_machine_authenticated",
|
|
"governance_executor_ready",
|
|
"preapproval_publisher_ready",
|
|
"runtime_exact_main",
|
|
"main_lineage_verified",
|
|
"campaign_revoked",
|
|
}
|
|
if set(facts) != required:
|
|
raise CampaignActivationError("ACTIVATION_FACTS_SCHEMA_INVALID")
|
|
if policy["activation_state"] != "PENDING_CONTROL_PLANE_MERGE" or policy["active"] is not False:
|
|
raise CampaignActivationError("CAMPAIGN_NOT_PENDING")
|
|
if policy["campaign_id"] != CAMPAIGN_ID or facts["campaign_id"] != CAMPAIGN_ID:
|
|
raise CampaignActivationError("CAMPAIGN_ID_MISMATCH")
|
|
if facts["repository"] != "aios/aios" or policy["repository"] != facts["repository"]:
|
|
raise CampaignActivationError("REPOSITORY_MISMATCH")
|
|
for name in ("source_main_oid", "gitea_main_oid", "authority_main_oid", "runtime_source_oid"):
|
|
if not isinstance(facts[name], str) or not OID_RE.fullmatch(facts[name]):
|
|
raise CampaignActivationError("ACTIVATION_OID_INVALID")
|
|
if not (
|
|
facts["source_main_oid"]
|
|
== facts["gitea_main_oid"]
|
|
== facts["authority_main_oid"]
|
|
== facts["runtime_source_oid"]
|
|
):
|
|
raise CampaignActivationError("ACTIVATION_MAIN_DIVERGENCE")
|
|
if facts["durable_job_state"] != "COMPLETED":
|
|
raise CampaignActivationError("DURABLE_JOB_NOT_COMPLETED")
|
|
boolean_fields = (
|
|
"writer_ready",
|
|
"gitea_machine_authenticated",
|
|
"governance_executor_ready",
|
|
"preapproval_publisher_ready",
|
|
"runtime_exact_main",
|
|
"main_lineage_verified",
|
|
)
|
|
if any(facts[name] is not True for name in boolean_fields) or facts["campaign_revoked"] is not False:
|
|
raise CampaignActivationError("ACTIVATION_PREDICATE_FAILED")
|
|
activated = dict(policy)
|
|
activated["activation_state"] = "ACTIVE"
|
|
activated["active"] = True
|
|
return validate_policy(activated)
|
|
|
|
|
|
def atomic_write_policy(path: Path, sidecar: Path, receipt_path: Path, policy: Mapping[str, Any], receipt: Mapping[str, Any]) -> str:
|
|
if path.is_symlink() or sidecar.is_symlink() or receipt_path.exists() or receipt_path.is_symlink():
|
|
raise CampaignActivationError("ACTIVATION_PATH_INVALID")
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
raw = canonical_json(dict(policy))
|
|
digest = digest_bytes(raw)
|
|
for target, data, mode in (
|
|
(path, raw, "wb"),
|
|
(sidecar, f"{digest} {path.name}\n".encode("ascii"), "wb"),
|
|
(receipt_path, canonical_json(dict(receipt)), "wb"),
|
|
):
|
|
fd, temporary = tempfile.mkstemp(prefix=f".{target.name}.", dir=target.parent)
|
|
try:
|
|
os.fchmod(fd, 0o600)
|
|
with os.fdopen(fd, mode) as stream:
|
|
stream.write(data)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
os.replace(temporary, target)
|
|
os.chmod(target, 0o600)
|
|
finally:
|
|
if os.path.exists(temporary):
|
|
os.unlink(temporary)
|
|
return digest
|