Files
mtusi2026lab2/evidence/final-memory-validation-2026-11-07/audit-files.py
T

154 lines
4.9 KiB
Python

import ast
import hashlib
import json
import re
import subprocess
from pathlib import Path
ROOT = Path.cwd()
FOLDER = Path(__file__).resolve().parent.relative_to(ROOT)
MEASURED = "74c33160ac01618f70e9e1c6a121a5e45c3c7155"
REVIEWED = "6096aa7948b8b666aa3892397a96d044a55f02c6"
def git(*args):
return subprocess.check_output(["git", *args], text=True).strip()
assert git("rev-parse", "HEAD") == MEASURED
assert git("branch", "--show-current") == "solution/bpi2303-kostylev"
paths = git("diff", "--name-only", MEASURED).splitlines()
assert all(p in {"REPORT.md", "AGENTS.md"} or p.startswith("evidence/") for p in paths)
runtime_diff = git(
"diff",
"--name-only",
REVIEWED,
"--",
"src",
"infra",
"compose.yaml",
"Dockerfile",
"pyproject.toml",
"uv.lock",
"alembic",
"scripts",
"student-tests",
"openspec",
)
assert not runtime_diff
assert not git(
"diff", "--name-only", "upstream/main", "--", "contracts", "tests", "README.md", "COURSE.md"
)
subprocess.run(["git", "diff", "--check"], check=True)
whitespace = subprocess.run(["git", "diff", "--cached", "--check"], capture_output=True, text=True)
raw_whitespace = re.findall(r"^([^\n:]+):\d+: trailing whitespace\.$", whitespace.stdout, re.M)
assert whitespace.returncode in (0, 2)
assert set(raw_whitespace) <= {
str(FOLDER / "png-boundary.txt"),
str(FOLDER / "png-http.txt"),
str(FOLDER / "smoke.txt"),
}
subprocess.run(
["git", "diff", "--cached", "--check", "--", ".", f":(exclude){FOLDER}/*.txt"],
check=True,
)
tracked = set(git("ls-files").splitlines())
docs = [
Path("REPORT.md"),
Path("AGENTS.md"),
Path("evidence/README.md"),
Path("evidence/load-validation.md"),
FOLDER / "RESULTS.md",
]
links = 0
for doc in docs:
content = doc.read_text()
assert len(re.findall(r"^```", content, re.M)) % 2 == 0, doc
for target in re.findall(r"\]\(([^)]+)\)", content):
if re.match(r"(?:https?://|#)", target):
continue
destination = (doc.parent / target.split("#")[0]).resolve()
assert destination.exists(), (doc, target)
relative = str(destination.relative_to(ROOT))
assert relative in tracked or any(p.startswith(relative + "/") for p in tracked), relative
links += 1
json_files = 0
for name in tracked:
if name.endswith(".json"):
json.loads(Path(name).read_text())
json_files += 1
for path in FOLDER.glob("*.py"):
ast.parse(path.read_text(), filename=str(path))
hashes = 0
for manifest, key in [
("evidence/final-validation-2026-11-07/sha256.json", None),
("evidence/review-png-memory-2026-11-07/checks.json", "sha256"),
]:
entries = json.loads(Path(manifest).read_text())
if key:
entries = entries[key]
for name, expected in entries.items():
assert hashlib.sha256(Path(name).read_bytes()).hexdigest() == expected, name
hashes += 1
accepted = []
for name in [
"evidence/forward-final-2026-11-06/level4.json",
*[f"evidence/forward-final-2026-11-06/track-run{n}.json" for n in (1, 2, 3)],
"evidence/final-validation-2026-11-07/postgres-outage-retry.json",
"evidence/final-validation-2026-11-07/delivery-targeted.json",
]:
data = json.loads(Path(name).read_text())
scenarios = data.get("scenarios", data.get("cases"))
assert data["result"] == "PASS"
assert all(x["result"] == "PASS" for x in scenarios + data["invariants"])
accepted.append(
{
"file": name,
"source_sha": data["source_sha"],
"scenarios": len(scenarios),
"invariants": len(data["invariants"]),
}
)
own_containers = subprocess.check_output(
["docker", "ps", "-aq", "--filter", "label=com.docker.compose.project=mtusi2026lab5"], text=True
).strip()
assert not own_containers
volumes = subprocess.check_output(
[
"docker",
"volume",
"ls",
"--filter",
"label=com.docker.compose.project=mtusi2026lab5",
"--format",
"{{.Name}}",
],
text=True,
).splitlines()
assert len(volumes) == 4
report = {
"measured_sha": MEASURED,
"reviewed_runtime": REVIEWED,
"runtime_equal": True,
"normative_equal_to_upstream_main": True,
"changed_paths": paths,
"local_links_verified": links,
"tracked_json_parsed": json_files,
"previous_and_fix_hashes_verified": hashes,
"raw_diff_check_exit": whitespace.returncode,
"preserved_raw_trailing_whitespace_lines": len(raw_whitespace),
"docs_and_helpers_diff_check": "PASS",
"accepted_without_rerun": accepted,
"own_containers": [],
"retained_volumes": volumes,
"result": "PASS",
}
(FOLDER / "final-audit.json").write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(
json.dumps(
{k: v for k, v in report.items() if k not in {"changed_paths", "accepted_without_rerun"}},
ensure_ascii=False,
indent=2,
)
)