scripts/tests/reproduction_cli/test_skip_passed.py
scripts/tests/reproduction_cli/test_skip_passed.pyBrowse 41 files
1,685 tokens
6,155 bytes
Token encoding: o200k_base
Snapshot a9fb1c3
← Back to SKILL.md
1import json2import sys3from pathlib import Path4 5sys.path.insert(0, str(Path(__file__).resolve().parents[2]))6 7from cli_testlib import _chain, _write_stub_proof8from mechanical_refactor_reproduction_cli import (9 VERDICT_FAIL,10 VERDICT_PASS,11 main,12 render_report,13 verify_chain,14)15 16_MSG = "mechanical_provable: move foo"17 18 19def _counting_proof(script: Path, counter: Path, *, passing: bool = True) -> None:20 """Make the stub proof bump a run counter so re-execution is observable."""21 verdict = (22 'print("PASS: reproduces the commit byte-for-byte.")\nsys.exit(0)\n'23 if passing24 else 'print("RESIDUAL (1 lines):\\n+x")\nsys.exit(1)\n'25 )26 script.write_text(27 "import sys\n"28 f"counter = __import__('pathlib').Path({str(counter)!r})\n"29 "runs = int(counter.read_text()) if counter.exists() else 0\n"30 "counter.write_text(str(runs + 1))\n" + verdict31 )32 33 34def _cache_file(repo: Path) -> Path:35 return repo / ".git" / "mechanical_refactor_passed_proofs.json"36 37 38def test_skip_passed_reuses_an_unchanged_pass_without_rerunning(39 repo: Path, tmp_path: Path40) -> None:41 """A PASS recorded on the first run is reused: the proof does not execute again."""42 proof = tmp_path / "proof"43 counter = tmp_path / "runs"44 base, shas = _chain(repo, [_MSG])45 _counting_proof(_write_stub_proof(proof, shas[0]), counter)46 args = dict(base=base, branch="chain", proof=proof, repo_root=str(repo))47 48 first = verify_chain(**args)49 assert first.verdicts[0].verdict == VERDICT_PASS50 assert counter.read_text() == "1"51 52 second = verify_chain(**args, skip_passed=True)53 assert second.verdicts[0].verdict == VERDICT_PASS54 assert second.verdicts[0].cached55 assert counter.read_text() == "1"56 assert second.passed57 58 59def test_without_the_flag_the_proof_always_reruns(repo: Path, tmp_path: Path) -> None:60 """The cache is recorded on every run but consulted only under skip_passed."""61 proof = tmp_path / "proof"62 counter = tmp_path / "runs"63 base, shas = _chain(repo, [_MSG])64 _counting_proof(_write_stub_proof(proof, shas[0]), counter)65 args = dict(base=base, branch="chain", proof=proof, repo_root=str(repo))66 67 verify_chain(**args)68 result = verify_chain(**args)69 70 assert counter.read_text() == "2"71 assert not result.verdicts[0].cached72 73 74def test_editing_the_proof_script_invalidates_the_cache(75 repo: Path, tmp_path: Path76) -> None:77 """A changed script hash misses the cache, so the edited (failing) proof reruns."""78 proof = tmp_path / "proof"79 base, shas = _chain(repo, [_MSG])80 script = _write_stub_proof(proof, shas[0])81 82 verify_chain(base=base, branch="chain", proof=proof, repo_root=str(repo))83 script.write_text('print("RESIDUAL (1 lines):\\n+x")\nraise SystemExit(1)\n')84 result = verify_chain(85 base=base, branch="chain", proof=proof, repo_root=str(repo), skip_passed=True86 )87 88 assert result.verdicts[0].verdict == VERDICT_FAIL89 90 91def test_editing_the_utils_copy_invalidates_the_cache(92 repo: Path, tmp_path: Path93) -> None:94 """The utils module next to the scripts is part of the key: editing it forces a rerun."""95 proof = tmp_path / "proof"96 counter = tmp_path / "runs"97 utils = proof / "mechanical_refactor_reproduction_utils.py"98 base, shas = _chain(repo, [_MSG])99 _counting_proof(_write_stub_proof(proof, shas[0]), counter)100 utils.parent.mkdir(parents=True, exist_ok=True)101 utils.write_text("ENGINE = 1\n")102 args = dict(base=base, branch="chain", proof=proof, repo_root=str(repo))103 104 verify_chain(**args)105 utils.write_text("ENGINE = 2\n")106 result = verify_chain(**args, skip_passed=True)107 108 assert counter.read_text() == "2"109 assert not result.verdicts[0].cached110 111 112def test_a_fail_is_never_recorded_in_the_cache(repo: Path, tmp_path: Path) -> None:113 """Only PASS verdicts enter the cache; a failing proof leaves no entry for its sha."""114 proof = tmp_path / "proof"115 base, shas = _chain(repo, [_MSG])116 _write_stub_proof(proof, shas[0], passing=False)117 118 verify_chain(base=base, branch="chain", proof=proof, repo_root=str(repo))119 120 cache = _cache_file(repo)121 assert not cache.exists() or shas[0] not in json.loads(cache.read_text())["passed"]122 123 124def test_corrupt_cache_file_is_treated_as_empty(repo: Path, tmp_path: Path) -> None:125 """A garbage cache file never crashes the walk; the proof simply runs."""126 proof = tmp_path / "proof"127 base, shas = _chain(repo, [_MSG])128 _write_stub_proof(proof, shas[0])129 _cache_file(repo).write_text("{not json")130 131 result = verify_chain(132 base=base, branch="chain", proof=proof, repo_root=str(repo), skip_passed=True133 )134 135 assert result.verdicts[0].verdict == VERDICT_PASS136 assert not result.verdicts[0].cached137 138 139def test_cache_lives_in_the_git_common_dir_and_records_the_pass(140 repo: Path, tmp_path: Path141) -> None:142 """A PASS writes the (sha, script hash, utils hash) entry under .git/."""143 proof = tmp_path / "proof"144 base, shas = _chain(repo, [_MSG])145 _write_stub_proof(proof, shas[0])146 147 verify_chain(base=base, branch="chain", proof=proof, repo_root=str(repo))148 149 entry = json.loads(_cache_file(repo).read_text())["passed"][shas[0]]150 assert set(entry) == {"script_sha256", "utils_sha256"}151 assert len(entry["script_sha256"]) == 64152 153 154def test_report_counts_reused_proofs_and_main_accepts_the_flag(155 repo: Path, tmp_path: Path156) -> None:157 """The report carries the reused count and --skip-passed works through main."""158 proof = tmp_path / "proof"159 base, shas = _chain(repo, [_MSG])160 _write_stub_proof(proof, shas[0])161 cli_args = [162 "--base",163 base,164 "--branch",165 "chain",166 "--proof",167 str(proof),168 "--repo-root",169 str(repo),170 ]171 172 assert main(cli_args) == 0173 result = verify_chain(174 base=base, branch="chain", proof=proof, repo_root=str(repo), skip_passed=True175 )176 report = render_report(result)177 178 assert "reused from the passed-proof cache (--skip-passed): 1" in report179 assert main([*cli_args, "--skip-passed"]) == 0180