Coverage for scripts/freeze_run_config.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Freeze a Lean run's config (V6.4.0): a base config plus the rule-adopted changes, written where the DinD runner and the 

2run-folder tools read it (`ml/evaluation/run_configs/<name>.json`), with its sha256 printed for the Changelog's Freeze bullet. 

3 

4 python scripts/freeze_run_config.py --base ml/evaluation/run_configs/v640_bt5.json --adopted ml/evaluation/v640/adopted_changes.json \\ 

5 --name v640_bt6 [--lean-window 2023-07-01 2026-10-16] 

6 

7`--adopted` is `scripts/v640_research.py`'s `adopted_changes.json`: its `config` (dotted keys under `phase_v2`) and `patch` (dotted keys 

8from the config root) are applied; items listed in `needs_live_path` must already be built in `main.py` (the script refuses 

9otherwise, so a run never freezes a setting live code does not read). 

10""" 

11 

12from __future__ import annotations 

13 

14import argparse 

15import hashlib 

16import json 

17import sys 

18from pathlib import Path 

19 

20ROOT_DIR = Path(__file__).resolve().parents[1] 

21sys.path.insert(0, str(ROOT_DIR)) 

22sys.path.insert(0, str(Path(__file__).resolve().parent)) 

23 

24from v610_research import apply_dotted # noqa: E402 

25from v640_research import patch_config # noqa: E402 

26 

27BUILT_LIVE_PATHS: set[str] = {"graduates_off"} # extend as an adopted item's live path lands (direction_overlay, tranches, ...) 

28 

29 

30def freeze(base: dict, adopted: dict, lean_window: tuple[str, str] | None = None) -> dict: 

31 """The base config with the adopted changes applied; raises when an adopted item has no live path yet.""" 

32 unbuilt = sorted(set(adopted.get("needs_live_path") or []) - BUILT_LIVE_PATHS) 

33 if unbuilt: 

34 raise ValueError(f"adopted items without a live path in main.py: {unbuilt}") 

35 config = patch_config(apply_dotted(base, adopted.get("config") or {}), adopted.get("patch") or {}) 

36 if adopted.get("graduates_off"): # L9: the crypto graduates are no book candidates (`class_weight_caps.crypto.core_only`) 

37 config["phase_v2"]["class_weight_caps"]["crypto"]["core_only"] = True 

38 if lean_window: 

39 config["runtime"]["lean_window_override"] = {"start": lean_window[0], "end": lean_window[1]} 

40 return config 

41 

42 

43def main() -> int: 

44 parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 

45 parser.add_argument("--base", required=True) 

46 parser.add_argument("--adopted", required=True) 

47 parser.add_argument("--name", required=True) 

48 parser.add_argument("--lean-window", nargs=2, default=None, metavar=("START", "END")) 

49 parser.add_argument("--allow-unbuilt", action="store_true", help="freeze even when an adopted item has no live path (research only)") 

50 args = parser.parse_args() 

51 base = json.loads(Path(args.base).read_text(encoding="utf-8")) 

52 adopted = json.loads(Path(args.adopted).read_text(encoding="utf-8")) 

53 if args.allow_unbuilt: 

54 adopted = {**adopted, "needs_live_path": []} 

55 config = freeze(base, adopted, tuple(args.lean_window) if args.lean_window else None) 

56 text = json.dumps(config, indent=4) 

57 out = ROOT_DIR / "ml" / "evaluation" / "run_configs" / f"{args.name}.json" 

58 out.write_text(text, encoding="utf-8", newline="\n") 

59 print(f"{out}\nsha256 {hashlib.sha256(text.encode('utf-8')).hexdigest()}") 

60 return 0 

61 

62 

63if __name__ == "__main__": 

64 raise SystemExit(main())