#!/usr/bin/env python3 """Fix return #1555's `lean-compile.log`: remove the hard-coded home prefix, change nothing else. The file is a CAPTURED LOG, not a program: it records one successful Lean check of `Route27.lean` together with the path the check was run against. A re-run cannot fix it -- a re-run produces a new observation (this machine measured 33-55 s, the log records the author's 29 s) and would discard the thing the log exists to record. So the repair is a substitution, and the whole job is to prove the substitution is the ONLY change. The prefix to remove is DERIVED from the served bytes, never written into this file. Hard-coding one author's home directory here would be the same defect the job repairs, and a literal would itself be a machine path; deriving it also makes the assertion stronger, since it must hold for whatever prefix the served artifact actually carries. Refuses to write unless every postcondition holds: * the input hashes to the served sha, * the derived machine prefix is the same on both lines, * that prefix occurs exactly twice and nowhere else, * the result carries no home path by the platform's own rule, * the line count, each line's `[lean-check]` tag and each line's verdict are unchanged, * canonicalising the two removed spans to one marker makes the input and the output byte-identical. Usage: python fix-lean-compile-log.py --in --out [--subs ] Exit 0 only if the corrected file was written. """ import argparse import hashlib import json import os import re import sys SERVED_SHA = "33d349026dab5269d24435badcd605b0d9f2b42e2c0b09e284443132c0b1df86" SERVED_BYTES = 253 MARK = b".solveathome/" # the platform's rule (src/lib/files.ts findHomePath), copied as a pattern, not as a module HOME_RX = re.compile(r"""(?:^|[\s"'(=:])((?:/Users|/home|C:\\Users)[/\\][A-Za-z0-9._-]+[/\\][^\s"')]{0,80})""") def derive_prefix(orig: bytes) -> bytes: """The machine prefix a line carries: the path token that immediately precedes `.solveathome/`.""" head = orig.split(b"\n", 1)[0] before, sep, _ = head.partition(MARK) assert sep, "the served log names no path under .solveathome/" prefix = before.rsplit(b" ", 1)[-1] assert prefix.startswith((b"/", b"~")) and b":" not in prefix, f"implausible prefix: {prefix!r}" return prefix def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--in", dest="src", required=True) ap.add_argument("--out", dest="dst", required=True) ap.add_argument("--subs") a = ap.parse_args() sys.stdout.reconfigure(newline="\n") orig = open(a.src, "rb").read() sha = hashlib.sha256(orig).hexdigest() assert sha == SERVED_SHA, f"input is not the served file: {sha}" assert len(orig) == SERVED_BYTES, f"input is {len(orig)} bytes, expected {SERVED_BYTES}" PREFIX = derive_prefix(orig) assert derive_prefix(orig.split(b"\n")[1]) == PREFIX, "line 2 does not carry the same prefix as line 1" n = orig.count(PREFIX) assert n == 2, f"the prefix occurs {n} times, expected 2 (one per line)" fixed = orig.replace(PREFIX, b"") # postconditions assert HOME_RX.search(fixed.decode("utf-8", "replace")) is None, "a home path survives" o_lines, f_lines = orig.split(b"\n"), fixed.split(b"\n") assert len(o_lines) == len(f_lines) == 3, "the line count moved" for i, (o, f) in enumerate(zip(o_lines, f_lines), 1): if not o.strip(): # the tail after the final newline assert f == o, f"line {i}: the trailing bytes moved" continue assert o.startswith(b"[lean-check] ") and f.startswith(b"[lean-check] "), f"line {i}: tag lost" if o.strip(): assert b"Route27.lean" in f, f"line {i}: subject lost" assert (b"OK" in o) == (b"OK" in f), f"line {i}: verdict changed" # the parenthesised observation (`(29s)`) is the run's own figure: it must not move, # even though the path beside it must. assert re.findall(rb"\(([^)]*)\)", o) == re.findall(rb"\(([^)]*)\)", f), \ f"line {i}: the observed figure changed" # the only differing spans are the two prefixes: walk the bytes rather than re-applying # the substitution, so a changed byte anywhere else is caught here and not just assumed. i = j = spans = 0 while i < len(orig) or j < len(fixed): if i < len(orig) and j < len(fixed) and orig[i] == fixed[j]: i += 1 j += 1 continue assert orig[i:i + len(PREFIX)] == PREFIX, f"a byte outside the removed spans moved at {i}" i += len(PREFIX) spans += 1 assert spans == 2, f"{spans} spans removed, expected 2" changed = [i for i, (o, f) in enumerate(zip(o_lines, f_lines), 1) if o != f] with open(a.dst, "wb") as fh: fh.write(fixed) subs = { "file": "lean-compile.log", "return": 1555, "job": 2960, "served_sha256": SERVED_SHA, "served_bytes": len(orig), "corrected_sha256": hashlib.sha256(fixed).hexdigest(), "corrected_bytes": len(fixed), "table": [{ "to": "", "occurrences": n, "removed_bytes": len(PREFIX), # The 46 removed bytes are pinned by digest, not quoted: a substitution table does not have # to spell out a machine path to be exact, and quoting one makes this artifact trip the # same detector it documents. Given the served original (content-addressed) the digest # names the bytes uniquely, and `removed_derivation` says how to recover them. "removed_sha256": hashlib.sha256(PREFIX).hexdigest(), "removed_derivation": ("the path token of line 1 immediately preceding the first " "`.solveathome/` -- the repository root as that machine saw it; " "the remainder is already repository-relative"), "remaining_path": ".solveathome/private/research/rescue-622/Route27.lean", "rule": "the repository root, left implicit: the remainder is already relative to it", }], "bytes_removed": n * len(PREFIX), "lines": len(f_lines) - 1, "lines_touched": changed, "newline_convention": "LF (%d)" % orig.count(b"\n"), "deliberately_not_compensated": [ "the observation itself: `(29s)`, the author's measured compile time, kept verbatim " "because a log records a run and this repair must not restate it", "the directory structure `.solveathome/private/research/rescue-622/`, kept verbatim " "because it is what the original path actually said; only the machine prefix is removed", ], "not_claimed": [ "that `.solveathome/private/...` exists on any other machine -- it is the author's private " "working subtree and is not in the public repository; the reproducible source of the " "compiled artifact is the served `Route27.lean` (sha256 " "1d710248e0e04f1fe9c2e322037bd741e2901b36ef644e456c8077eff54baa20)", "that a re-run reproduces the 29 s", ], } if a.subs: with open(a.subs, "w", encoding="utf-8", newline="\n") as fh: json.dump(subs, fh, indent=1, sort_keys=True) fh.write("\n") print(json.dumps({"ok": True, "served_sha256": SERVED_SHA, "corrected_sha256": subs["corrected_sha256"], "served_bytes": len(orig), "corrected_bytes": len(fixed), "removed_bytes": len(PREFIX), "removed_sha256": hashlib.sha256(PREFIX).hexdigest(), "occurrences": n, "lines_touched": changed}, sort_keys=True)) return 0 if __name__ == "__main__": sys.exit(main())