File size: 3,492 Bytes
7956103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Retain the original presentation and refresh labels without rerunning science."""
import argparse
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys


def sha(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def main(run, source):
    run = run.resolve(strict=True)
    source = source.resolve(strict=True)
    status_path = run / "status.json"
    status = json.loads(status_path.read_text())
    assert status["status"] == "COMPLETE"
    backup = run / "presentation_original"
    if backup.exists():
        raise ValueError("Presentation snapshot already exists; will not overwrite")
    protected = ("config.json", "witnesses.json", "certificates.json", "search_results.json")
    before = {name: sha(run / name) for name in protected}
    backup.mkdir()
    (backup / "source").mkdir()
    for name in ("status.json", "plot_data.json", "RESULTS_PHASE2.md"):
        shutil.copy2(run / name, backup / name)
    shutil.copytree(run / "figures", backup / "figures")
    updated = ("normalize_results.py", "plot_phase2.py")
    for name in updated:
        shutil.copy2(run / "source" / name, backup / "source" / name)
        shutil.copy2(source / name, run / "source" / name)
    shutil.copy2(Path(__file__), run / "source" / "revise_presentation.py")
    report = (run / "RESULTS_PHASE2.md").read_text()
    marker = "## 求解器超时与独立恢复记录"
    recovery = "\n\n" + marker + report.split(marker, 1)[1] if marker in report else ""
    env = os.environ.copy()
    env.update(MPLBACKEND="Agg", OPENBLAS_NUM_THREADS="1", OMP_NUM_THREADS="1",
               MKL_NUM_THREADS="1", NUMEXPR_NUM_THREADS="1")
    for name in updated:
        with (run / "logs" / ("presentation_" + name + ".log")).open("w") as log:
            subprocess.run([sys.executable, str(run / "source" / name),
                            "--run-dir", str(run)], check=True, env=env,
                           stdout=log, stderr=subprocess.STDOUT, timeout=120)
    assert before == {name: sha(run / name) for name in protected}
    target = run / "RESULTS_PHASE2.md"
    target.write_text(target.read_text() + recovery +
        "\n\n## 展示与交叉核验修订\n\n"
        "图中 KL 明确标注为认证区间上端点;候选图明确区分浮点筛选与入选见证认证。"
        "汇总程序额外核对 n、tau 与认证 ID 集合。模型、搜索结果和数学证书没有改动;"
        "修订前展示和代码保存在 presentation_original/。\n")
    for name in updated + ("revise_presentation.py",):
        status["source_sha256"][name] = sha(run / "source" / name)
    status["presentation_revision"] = {
        "time_utc": dt.datetime.now(dt.timezone.utc).isoformat(),
        "scope": "Labels and additional cross-artifact assertions only; no scientific rerun",
        "protected_artifact_sha256": before,
        "prior_presentation": "presentation_original/",
        "updated_sources": list(updated),
        "status": "COMPLETE",
    }
    status_path.write_text(json.dumps(status, indent=2, allow_nan=False) + "\n")
    print("Presentation updated; scientific artifacts unchanged.", flush=True)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--run-dir", type=Path, required=True)
    parser.add_argument("--source-dir", type=Path, required=True)
    args = parser.parse_args()
    main(args.run_dir, args.source_dir)