|
| 1 | +"""End a diagnostic corpus on real screened counts, then independently replay it.""" |
| 2 | + |
| 3 | +import argparse, json, os, subprocess, sys, time |
| 4 | +from pathlib import Path |
| 5 | +from verification.dataset import verify_dataset |
| 6 | + |
| 7 | + |
| 8 | +def main(): |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument("--engine", default="build/caissa") |
| 11 | + parser.add_argument("--output", type=Path, required=True) |
| 12 | + parser.add_argument("--interrupt", action="store_true") |
| 13 | + args = parser.parse_args() |
| 14 | + args.output.mkdir(parents=True, exist_ok=False) |
| 15 | + corpus = args.output / "corpus" |
| 16 | + dataset = args.output / "prepared" |
| 17 | + producer = [ |
| 18 | + sys.executable, |
| 19 | + "-m", |
| 20 | + "training.corpus", |
| 21 | + "--engine", |
| 22 | + args.engine, |
| 23 | + "--pairs", |
| 24 | + "5000", |
| 25 | + "--nodes", |
| 26 | + "500", |
| 27 | + "--workers", |
| 28 | + "2", |
| 29 | + "--policy", |
| 30 | + "searched-v2", |
| 31 | + "--max-seconds", |
| 32 | + "300", |
| 33 | + "--stop-prepared", |
| 34 | + str(dataset / "PREPARE_PROGRESS.json"), |
| 35 | + "--target-training-inputs", |
| 36 | + "5000", |
| 37 | + "--minimum-training-inputs", |
| 38 | + "4000", |
| 39 | + "--minimum-completed-games", |
| 40 | + "1", |
| 41 | + "--output", |
| 42 | + str(corpus), |
| 43 | + ] |
| 44 | + preparation = [ |
| 45 | + sys.executable, |
| 46 | + "-m", |
| 47 | + "training.prepare", |
| 48 | + str(corpus), |
| 49 | + str(dataset), |
| 50 | + "--follow", |
| 51 | + ] |
| 52 | + processes = [] |
| 53 | + preserved = {} |
| 54 | + with ( |
| 55 | + (args.output / "producer.log").open("w") as one, |
| 56 | + (args.output / "preparation.log").open("w") as two, |
| 57 | + ): |
| 58 | + try: |
| 59 | + source = subprocess.Popen(producer, stdout=one, stderr=subprocess.STDOUT) |
| 60 | + processes.append(source) |
| 61 | + deadline = time.monotonic() + 30 |
| 62 | + while not (corpus / "CONFIG.json").exists(): |
| 63 | + if source.poll() is not None: |
| 64 | + raise ValueError("producer exited before initialization") |
| 65 | + if time.monotonic() > deadline: |
| 66 | + raise TimeoutError("producer configuration missing") |
| 67 | + time.sleep(0.02) |
| 68 | + prepare = subprocess.Popen( |
| 69 | + preparation, stdout=two, stderr=subprocess.STDOUT |
| 70 | + ) |
| 71 | + processes.append(prepare) |
| 72 | + if args.interrupt: |
| 73 | + deadline = time.monotonic() + 60 |
| 74 | + while ( |
| 75 | + len(list(corpus.glob("*/*.json"))) < 8 |
| 76 | + or not (dataset / "PREPARE_PROGRESS.json").exists() |
| 77 | + ): |
| 78 | + if source.poll() is not None or prepare.poll() is not None: |
| 79 | + raise ValueError("acquisition exited before interruption") |
| 80 | + if time.monotonic() > deadline: |
| 81 | + raise TimeoutError( |
| 82 | + "no acquisition checkpoint before interruption" |
| 83 | + ) |
| 84 | + time.sleep(0.02) |
| 85 | + # Preparation rolls back its uncommitted tail. The producer drains |
| 86 | + # bounded children and records CPU before its replacement starts. |
| 87 | + prepare.terminate() |
| 88 | + prepare.wait(timeout=10) |
| 89 | + source.terminate() |
| 90 | + if source.wait(timeout=120) != 2: |
| 91 | + raise ValueError( |
| 92 | + "graceful acquisition interruption was not recorded" |
| 93 | + ) |
| 94 | + import hashlib |
| 95 | + |
| 96 | + preserved = { |
| 97 | + str(path.relative_to(corpus)): hashlib.sha256( |
| 98 | + path.read_bytes() |
| 99 | + ).hexdigest() |
| 100 | + for path in corpus.glob("*/*.json") |
| 101 | + } |
| 102 | + source = subprocess.Popen( |
| 103 | + producer, stdout=one, stderr=subprocess.STDOUT |
| 104 | + ) |
| 105 | + processes.append(source) |
| 106 | + deadline = time.monotonic() + 30 |
| 107 | + while len((corpus / "SESSIONS.jsonl").read_text().splitlines()) < 3: |
| 108 | + if source.poll() is not None or time.monotonic() > deadline: |
| 109 | + raise ValueError("resumed producer did not start") |
| 110 | + time.sleep(0.02) |
| 111 | + prepare = subprocess.Popen( |
| 112 | + preparation + ["--resume"], stdout=two, stderr=subprocess.STDOUT |
| 113 | + ) |
| 114 | + processes.append(prepare) |
| 115 | + if source.wait(timeout=300) != 0: |
| 116 | + raise ValueError("target producer did not complete") |
| 117 | + if prepare.wait(timeout=180) != 0: |
| 118 | + raise ValueError("target preparation did not export") |
| 119 | + finally: |
| 120 | + for process in processes: |
| 121 | + if process.poll() is None: |
| 122 | + process.terminate() |
| 123 | + for process in processes: |
| 124 | + try: |
| 125 | + process.wait(timeout=10) |
| 126 | + except subprocess.TimeoutExpired: |
| 127 | + process.kill() |
| 128 | + process.wait() |
| 129 | + summary = json.loads((corpus / "SUMMARY.json").read_text()) |
| 130 | + manifest = json.loads((dataset / "DATASET.json").read_text()) |
| 131 | + assert summary["completion_mode"] == "screened-target" and summary["pairs"] < 5000 |
| 132 | + assert manifest["splits"]["train"] >= 4000 |
| 133 | + assert ( |
| 134 | + summary["completion_proof"]["counts"]["distinct_train_inputs"] |
| 135 | + == manifest["splits"]["train"] |
| 136 | + ) |
| 137 | + assert ( |
| 138 | + len(json.loads((dataset / "SOURCE_PAIRS.json").read_text())) == summary["pairs"] |
| 139 | + ) |
| 140 | + if args.interrupt: |
| 141 | + import hashlib |
| 142 | + |
| 143 | + for relative, expected in preserved.items(): |
| 144 | + assert ( |
| 145 | + hashlib.sha256((corpus / relative).read_bytes()).hexdigest() == expected |
| 146 | + ) |
| 147 | + sessions = [ |
| 148 | + json.loads(line) |
| 149 | + for line in (corpus / "SESSIONS.jsonl").read_text().splitlines() |
| 150 | + ] |
| 151 | + finished = [event for event in sessions if event["event"] == "finished"] |
| 152 | + assert len(finished) == 2 |
| 153 | + assert ( |
| 154 | + sessions[2]["previous_child_cpu_seconds"] |
| 155 | + == finished[0]["child_cpu_seconds_this_session"] |
| 156 | + ) |
| 157 | + assert ( |
| 158 | + sessions[2]["campaign_wall_origin_unix"] |
| 159 | + == sessions[0]["campaign_wall_origin_unix"] |
| 160 | + ) |
| 161 | + assert summary["campaign_child_cpu_seconds"] == sum( |
| 162 | + event["child_cpu_seconds_this_session"] for event in finished |
| 163 | + ) |
| 164 | + verified = verify_dataset( |
| 165 | + dataset, |
| 166 | + corpus, |
| 167 | + Path("config/studies.json"), |
| 168 | + Path("config/strength-openings.json"), |
| 169 | + ) |
| 170 | + result = { |
| 171 | + "status": "passed", |
| 172 | + "profile": "diagnostic", |
| 173 | + "pairs": summary["pairs"], |
| 174 | + "maximum_pairs": 5000, |
| 175 | + "actual_train_inputs": manifest["splits"]["train"], |
| 176 | + "source_and_preparation_confirm_same_boundary": True, |
| 177 | + "independent_replay": verified["status"], |
| 178 | + "resumed_completed_pair_bytes_preserved": len(preserved), |
| 179 | + "cumulative_budget_recovery": args.interrupt, |
| 180 | + "full_acceptance": False, |
| 181 | + } |
| 182 | + (args.output / "TARGET.json").write_text(json.dumps(result, indent=2) + "\n") |
| 183 | + print(json.dumps(result)) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() |
0 commit comments