Files
aigc-home/docs/hel-892/history-attribution-correction/package_delivery.py
T

142 lines
5.3 KiB
Python

"""Package the narrow provenance correction, never the old transport closure."""
import hashlib
import json
import subprocess
import urllib.request
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
HERE = Path(__file__).resolve().parent
LOCAL = ROOT / ".local-evidence/history-attribution-correction"
BASE = "0e3e7b071f4c293642900849db8b1ead997417b8"
def git(*args):
return subprocess.check_output(["git", *args], cwd=ROOT).decode().strip()
def sha(raw):
return hashlib.sha256(raw).hexdigest()
def write(path, obj):
with path.open("x") as f:
json.dump(obj, f, ensure_ascii=False, indent=2)
f.write("\n")
def main():
out = LOCAL / "delivery"
out.mkdir()
assert not git("status", "--porcelain")
head, branch = git("rev-parse", "HEAD"), git("branch", "--show-current")
remote = git("ls-remote", "origin", "refs/heads/" + branch).split()[0]
assert head == remote and branch == "agent/agent/a64a969a6a44"
with urllib.request.urlopen(
"https://gitea.xbay.cc/api/v1/repos/leefer/aigc-home/pulls/25", timeout=45
) as response:
pr = json.load(response)
assert pr["state"] == "open" and pr["head"]["sha"] == head and pr["base"]["ref"] == "main"
index = json.loads((HERE / "file-purpose-sha256.json").read_bytes())
changed = git("diff", "--name-only", "-z", BASE, head).strip("\0").split("\0")
assert set(changed) == {r["path"] for r in index["files"]} | {
(HERE / "file-purpose-sha256.json").relative_to(ROOT).as_posix()
}
for r in index["files"]:
assert sha((ROOT / r["path"]).read_bytes()) == r["sha256"]
facts = json.loads((HERE / "git-facts.json").read_bytes())
for token, r in facts["resolutions"].items():
if r["status"] == "unique_commit":
assert git("rev-parse", "--disambiguate=" + token).splitlines() == [r["commit"]]
with (out / "complete.patch").open("xb") as f:
f.write(subprocess.check_output(["git", "diff", "--binary", BASE, head], cwd=ROOT))
subprocess.run(
["git", "bundle", "create", str(out / "correction.bundle"), branch, "^" + BASE],
cwd=ROOT,
check=True,
)
verified = subprocess.run(
["git", "bundle", "verify", str(out / "correction.bundle")],
cwd=ROOT,
capture_output=True,
check=True,
)
(out / "bundle-verify.stdout.txt").write_bytes(verified.stdout)
(out / "bundle-verify.stderr.txt").write_bytes(verified.stderr)
write(
out / "final-head.json",
dict(
base=BASE,
head=head,
remote_head=remote,
branch=branch,
pr="https://gitea.xbay.cc/leefer/aigc-home/pulls/25",
pr_head=pr["head"]["sha"],
pr_base="main",
pr_state=pr["state"],
clean=True,
product_changes=0,
independent_review="pending HEL-893 re-review",
),
)
files = {name: ROOT / name for name in changed}
for name in [
"complete.patch",
"correction.bundle",
"bundle-verify.stdout.txt",
"bundle-verify.stderr.txt",
"final-head.json",
]:
files[name] = out / name
# Retain the intermediate correction attempts and exact command records in this batch only.
for dirname in ["attempt-1", "attempt-2", "attempt-3", "records"]:
for path in sorted((LOCAL / dirname).iterdir()):
assert path.is_file()
files["run-evidence/" + dirname + "/" + path.name] = path
for name in ["prior-bundle-verify.stdout.txt", "prior-bundle-verify.stderr.txt"]:
files["run-evidence/" + name] = LOCAL / name
files["run-evidence/log-index-before-resource.json"] = LOCAL / "log-index-before-resource.json"
for key in ["commit", "push"]:
record = json.loads((LOCAL / "records" / (key + ".json")).read_bytes())
for r in record["logs"]:
files["delivery-logs/" + Path(r["path"]).name] = ROOT / r["path"]
purposes = [
dict(
path=n,
bytes=p.stat().st_size,
sha256=sha(p.read_bytes()),
purpose="本轮窄修代码/归因矩阵/原账快照/差集/冻结/独占日志或中间输出;不含旧ZIP闭包",
)
for n, p in sorted(files.items())
]
write(
out / "bundle-purpose-sha256.json",
dict(base=BASE, head=head, self_excluded=True, files=purposes),
)
files["bundle-purpose-sha256.json"] = out / "bundle-purpose-sha256.json"
archive = out / "HEL-892-history-attribution-correction-evidence.zip"
with zipfile.ZipFile(archive, "x", zipfile.ZIP_DEFLATED) as z:
for n, p in sorted(files.items()):
z.write(p, n)
with zipfile.ZipFile(archive) as z:
assert z.testzip() is None and set(z.namelist()) == set(files)
for n, p in files.items():
assert z.read(n) == p.read_bytes() and sha(z.read(n)) == sha(p.read_bytes())
write(
out / "archive-verification.json",
dict(
archive=archive.name,
bytes=archive.stat().st_size,
sha256=sha(archive.read_bytes()),
members=len(files),
all_bytes_sha_crc_equal=True,
),
)
print("PASS", head, len(files), "members byte/SHA/CRC; ZIP bytes", archive.stat().st_size)
if __name__ == "__main__":
main()