Files
aigc-home/docs/hel-892/animate2-loop-document-evidence/source_history.py
T

170 lines
7.4 KiB
Python

"""Resolve every historical citation by exact source identity; no network or upstream execution."""
import hashlib
import json
import re
from datetime import datetime
from pathlib import Path
from urllib.parse import urlsplit
ROOT = Path(__file__).resolve().parents[3]
OUT = Path(__file__).resolve().parent
CORRECTION = OUT / "history-correction"
def moment(value):
if value is None or value.startswith(("未知", "unknown")):
return None
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
if parsed.utcoffset() is None:
raise ValueError("source time requires timezone")
return parsed
def repository(value):
name = value or ""
if "://" in name:
parsed = urlsplit(name)
if parsed.hostname not in {"github.com", "www.github.com"}:
raise ValueError("unrecognized canonical repository")
name = parsed.path.strip("/")
return name.removesuffix(".git").lower()
def identity(source):
commit = source.get("commit", "")
if re.fullmatch("[0-9a-f]{40}", commit):
repo = repository(source.get("repo"))
if not repo:
raise ValueError("Git identity lacks repository")
provider = ("git", repo, commit)
elif source.get("tag") and source.get("url", "").startswith("https://cdn.comfy.org/"):
parsed = urlsplit(source["url"])
provider = ("release", parsed.hostname, parsed.path, source["tag"])
else:
raise ValueError("unknown source identity cannot prove historical acquisition")
return provider + (source["file"], source["sha256"])
def validate_prior(source, reference):
path = (ROOT / reference["record"]).resolve()
if not path.is_relative_to(ROOT) or path.name != "sources.json":
raise ValueError("historical manifest must be a repository sources.json")
if path == ROOT / "docs/hel-892/animate2-loop-reference/sources.json":
raise ValueError("self citation cannot prove prior acquisition")
data = path.read_bytes()
manifest = json.loads(data)
rows = manifest.get("sources", []) if isinstance(manifest, dict) else manifest
wanted = identity(source)
found = []
for index, row in enumerate(rows):
try:
matches = identity(row) == wanted
except ValueError:
matches = False
if matches and row.get("acquisition_time") == reference["acquisition_time"]:
if moment(row["acquisition_time"]) is None:
raise ValueError("unknown historical time is not an acquisition proof")
found.append(index)
if not found:
raise ValueError(
"no matching canonical identity/path/SHA/acquisition_time in " + reference["record"]
)
return dict(
file=source["file"],
record=reference["record"],
manifest_sha256=hashlib.sha256(data).hexdigest(),
matched_rows=found,
acquisition_time=reference["acquisition_time"],
identity=list(wanted),
)
def validate_fetch(source, origin):
evidence = ROOT / origin["evidence_path"]
if hashlib.sha256(evidence.read_bytes()).hexdigest() != origin["evidence_sha256"]:
raise ValueError("original fetch evidence bytes changed")
if (source["id"], source["file"], source["sha256"]) != (
origin["id"],
origin["file"],
origin["sha256"],
):
raise ValueError("fetch origin belongs to a different source")
kind = origin["evidence_kind"]
if kind in {"first-batch-history-record", "first-helper-history-record"}:
records = json.loads(evidence.read_bytes())
record = next(
row for row in records if row["id"] == source["id"] and row["file"] == source["file"]
)
if record["current_fetch_at"] != origin["expected_current_fetch_at"]:
raise ValueError("fetch ledger does not reproduce the original member history")
else:
original = json.loads(
(CORRECTION / "before/docs/hel-892/animate2-loop-reference/sources.json").read_bytes()
)["sources"]
baseline = next(
row for row in original if row["id"] == source["id"] and row["file"] == source["file"]
)
if baseline["sha256"] != source["sha256"]:
raise ValueError("original source identity changed")
if kind == "unproved-template-value":
exemplar = next(
row for row in original if row["id"] == "core0311" and row["file"] == "nodes.py"
)
tool = evidence.read_text()
if (
"e=dict(exemplar," not in tool
or "acquisition_time=now,reverified_at=utc()" not in tool
):
raise ValueError(
"original helper implementation does not substantiate template inheritance"
)
if baseline.get("current_fetch_at") != exemplar.get("current_fetch_at"):
raise ValueError("inherited value differs from its original template")
if moment(origin["expected_current_fetch_at"]) is not None:
raise ValueError("unproved member fetch must remain unknown")
elif baseline.get("current_fetch_at") != origin["expected_current_fetch_at"]:
raise ValueError("fetch ledger differs from original source record")
if source.get("current_fetch_at") != origin["expected_current_fetch_at"]:
raise ValueError("current_fetch_at does not match independently reviewed original record")
if source["reverified_at"] != origin["original_reverified_at"]:
raise ValueError("narrow correction cannot refresh original verification")
fetch, verified = moment(source.get("current_fetch_at")), moment(source["reverified_at"])
if fetch is not None and fetch > verified:
raise ValueError("fetch follows verification")
return dict(
file=source["file"],
current_fetch_at=source.get("current_fetch_at"),
evidence_kind=origin["evidence_kind"],
evidence_path=origin["evidence_path"],
original_record_matches=True,
record_has_timestamp=fetch is not None,
notice="核对原记录一致;复用/仓库fetch不认证本轮成员网络取得",
)
def validate_sources(rows):
origins = json.loads((CORRECTION / "fetch-origin-evidence.json").read_bytes())["records"]
if len(rows) != len(origins) or len(rows) != 86:
raise ValueError("source scope changed")
matrix, fetches = [], []
for source, origin in zip(rows, origins, strict=True):
valid = [validate_prior(source, ref) for ref in source.get("prior_acquisition_records", [])]
matrix.extend(valid)
fetches.append(validate_fetch(source, origin))
candidates = [moment(origin["original_acquisition_time"])]
candidates += [moment(ref["acquisition_time"]) for ref in valid]
if moment(source.get("current_fetch_at")) is not None:
candidates.append(moment(source["current_fetch_at"]))
if moment(source["acquisition_time"]) != min(x for x in candidates if x is not None):
raise ValueError("earliest acquisition is not selected from valid identity records")
if moment(source["acquisition_time"]) > moment(source["reverified_at"]):
raise ValueError("acquisition follows verification")
result = dict(
sources=86, valid_prior_references=len(matrix), matrix=matrix, fetch_origins=fetches
)
(CORRECTION / "source-reference-matrix.json").write_text(
json.dumps(result, ensure_ascii=False, indent=2) + "\n"
)
return result