Co-authored-by: multica <multica@users.noreply.github.com> Co-authored-by: multica-agent <github@multica.ai>
120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
"""Preserve the rejected ledger and verify its original transport before correcting."""
|
|
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
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"
|
|
PRIOR = ROOT / "docs/hel-892/batch175-closure-evidence"
|
|
|
|
|
|
def sha(raw):
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def write(path, obj):
|
|
with path.open("x", encoding="utf-8") as f:
|
|
json.dump(obj, f, ensure_ascii=False, indent=2)
|
|
f.write("\n")
|
|
|
|
|
|
def main():
|
|
assert subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=ROOT).decode().strip() == BASE
|
|
assert not subprocess.check_output(["git", "diff", "--name-only"], cwd=ROOT)
|
|
old = ROOT / ".local-evidence/batch175-closure/delivery"
|
|
metadata = json.loads((old / "archive-verification.json").read_bytes())
|
|
archive = old / metadata["archive"]
|
|
assert sha(archive.read_bytes()) == metadata["sha256"]
|
|
assert archive.stat().st_size == metadata["bytes"]
|
|
purposes = json.loads((old / "bundle-purpose-sha256.json").read_bytes())
|
|
with zipfile.ZipFile(archive) as z:
|
|
assert len(z.namelist()) == metadata["members"] == 551 and z.testzip() is None
|
|
for r in purposes["files"]:
|
|
raw = z.read(r["path"])
|
|
assert len(raw) == r["bytes"] and sha(raw) == r["sha256"]
|
|
verified = subprocess.run(
|
|
["git", "bundle", "verify", str(old / "batch175.bundle")],
|
|
cwd=ROOT,
|
|
capture_output=True,
|
|
check=True,
|
|
)
|
|
(LOCAL / "prior-bundle-verify.stdout.txt").write_bytes(verified.stdout)
|
|
(LOCAL / "prior-bundle-verify.stderr.txt").write_bytes(verified.stderr)
|
|
prior = HERE / "prior175"
|
|
prior.mkdir()
|
|
names = [
|
|
"ledger.json",
|
|
"ledger.csv",
|
|
"summary.json",
|
|
"remaining-work.json",
|
|
"逐文件总账与剩余条件.md",
|
|
"进度说明与下一阶段建议.md",
|
|
"README.md",
|
|
"audit.py",
|
|
"historical-comment-texts.json",
|
|
"current-instruction.json",
|
|
]
|
|
snapshots = []
|
|
for name in names:
|
|
raw = (PRIOR / name).read_bytes()
|
|
with (prior / name).open("xb") as f:
|
|
f.write(raw)
|
|
snapshots.append(
|
|
dict(
|
|
original_path=(PRIOR / name).relative_to(ROOT).as_posix(),
|
|
archive_path=(prior / name).relative_to(ROOT).as_posix(),
|
|
sha256=sha(raw),
|
|
bytes=len(raw),
|
|
)
|
|
)
|
|
baseline = []
|
|
for item in (
|
|
subprocess.check_output(["git", "ls-tree", "-rz", BASE], cwd=ROOT)
|
|
.rstrip(b"\0")
|
|
.split(b"\0")
|
|
):
|
|
meta, encoded = item.split(b"\t", 1)
|
|
path = encoded.decode()
|
|
raw = (ROOT / path).read_bytes()
|
|
blob = meta.decode().split()[2]
|
|
assert hashlib.sha1(b"blob " + str(len(raw)).encode() + b"\0" + raw).hexdigest() == blob
|
|
baseline.append(dict(path=path, git_blob_sha1=blob, sha256=sha(raw)))
|
|
write(HERE / "baseline-files.json", dict(base=BASE, files=baseline))
|
|
write(
|
|
HERE / "prior175-sha256.json",
|
|
dict(base=BASE, files=snapshots, original_transport=metadata, originals_remain_in_git=True),
|
|
)
|
|
write(
|
|
HERE / "preflight.json",
|
|
dict(
|
|
base=BASE,
|
|
directory_existed_before=False,
|
|
original_zip_members_verified=551,
|
|
original_purpose_rows=len(purposes["files"]),
|
|
old_blobs=len(baseline),
|
|
source_of_review="HEL-888 current review attachment",
|
|
product_change_allowlist=[],
|
|
existing_tool_allowlist=[
|
|
"docs/hel-892/batch175-closure-evidence/audit.py",
|
|
"docs/hel-892/batch175-closure-evidence/README.md",
|
|
],
|
|
),
|
|
)
|
|
review = LOCAL / "review/hel893-batch175-review.md"
|
|
with (HERE / review.name).open("xb") as f:
|
|
f.write(review.read_bytes())
|
|
print(
|
|
"PASS rejected175 transport/551 members/550 purposes/bundle;",
|
|
len(baseline),
|
|
"baseline blobs; 10 prior snapshots preserved",
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|