Files
aigc-home/docs/hel-892/animate-time-correction/correct_sources.py
T

118 lines
4.3 KiB
Python

"""Reverify three local exact texts, then sample real UTC; no network or upstream imports."""
import hashlib
import json
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
ROOT = Path(__file__).resolve().parents[3]
OUT = Path(__file__).resolve().parent
REF = OUT.parent / "animate-reference"
BASE = "e420c312e8f7ad226661db6a20608af816c719ab"
INDICES = [116, 117, 127]
sys.path.insert(0, str(OUT.parent / "animate-document-evidence"))
def digest(raw, kind):
return hashlib.sha1(kind + b" " + str(len(raw)).encode() + b"\0" + raw).hexdigest()
def main():
from verify_sources import validate_source_times
path = REF / "sources.json"
before = json.loads(
subprocess.check_output(["git", "show", BASE + ":" + str(path.relative_to(ROOT))])
)
original = before["sources"]
rejected = []
for i in INDICES:
try:
validate_source_times([original[i]])
except ValueError as error:
rejected.append(dict(index=i, error=str(error), original=original[i]))
else:
raise AssertionError("Original reversed timestamp was not rejected")
assert len(rejected) == 3
current = json.loads(path.read_bytes())
audit = OUT / "source-time-correction.json"
if audit.exists():
recorded = json.loads(audit.read_bytes())
assert current == recorded["corrected_manifest"]
validate_source_times(current["sources"])
print("PASS existing correction unchanged; original three inversions rejected")
return
assert current == before, "Refuse to overwrite unrelated source changes"
checks = []
for i in INDICES:
row = current["sources"][i]
raw = (REF / row["local_file"]).read_bytes()
assert hashlib.sha256(raw).hexdigest() == row["sha256"]
assert digest(raw, b"blob") == row["git_blob_sha1"]
identity = (REF / row["identity_root"]).resolve()
key = row["identity_key"]
tree_path = identity / (key + "-tree.json")
tree = json.loads(tree_path.read_bytes())
assert tree["sha"] == row["tree"]
assert (
next(e["sha"] for e in tree["tree"] if e["path"] == row["file"]) == row["git_blob_sha1"]
)
commit_path = identity / (key + "-commit.txt")
commit = commit_path.read_bytes()
assert digest(commit, b"commit") == row["commit"]
assert commit.splitlines()[0] == ("tree " + row["tree"]).encode()
license_path = REF / row["license_file"]
assert license_path.read_bytes() == subprocess.check_output(
["git", "show", BASE + ":" + str(license_path.relative_to(ROOT))]
)
# This instant is sampled only after the exact bytes and identities above pass.
now = datetime.now(timezone.utc).isoformat()
row["reverified_at"] = now
validate_source_times([row])
checks.append(
dict(
index=i,
reverified_at=now,
sha256=row["sha256"],
git_blob_sha1=row["git_blob_sha1"],
commit=row["commit"],
tree=row["tree"],
identity_files=[str(p.relative_to(ROOT)) for p in [tree_path, commit_path]],
license_sha256=hashlib.sha256(license_path.read_bytes()).hexdigest(),
upstream_fetched=False,
upstream_executed=False,
)
)
validate_source_times(current["sources"])
for i, (old, new) in enumerate(zip(original, current["sources"], strict=True)):
expected = dict(old)
if i in INDICES:
expected["reverified_at"] = new["reverified_at"]
assert expected == new
audit.write_text(
json.dumps(
dict(
base=BASE,
error="Batch start times were incorrectly called post-acquisition rechecks",
original_manifest=before,
original_rejections=rejected,
checks=checks,
corrected_manifest=current,
),
ensure_ascii=False,
indent=2,
)
+ "\n"
)
path.write_text(json.dumps(current, ensure_ascii=False, indent=2) + "\n")
print(
"PASS three exact local texts/identities; actual UTC sampled after checks; "
"other125 records frozen"
)
if __name__ == "__main__":
main()