66"""
77import hashlib
88import importlib .util
9+ import json
910import subprocess
1011import sys
12+ import tempfile
1113from pathlib import Path
1214
1315ROOT = Path (__file__ ).resolve ().parents [2 ]
@@ -61,31 +63,47 @@ def require(ok, message):
6163 raise SystemExit (message )
6264
6365
64- def load_checker ():
65- path = ROOT / "scripts/replay_historical_check.py"
66- spec = importlib .util .spec_from_file_location ("historical_selected_replay_gap_checker" , path )
66+ def load_module (path ):
67+ spec = importlib .util .spec_from_file_location (path .stem , path )
6768 module = importlib .util .module_from_spec (spec )
6869 spec .loader .exec_module (module )
6970 return module
7071
7172
73+ def checkout_snapshot (snapshot , sources , runner , env ):
74+ paths = [runner .canonical_relative_path (row ["path" ]) for row in sources ["files" ]]
75+ git = ["git" , "-c" , "maintenance.auto=false" , "-c" , "gc.auto=0" ,
76+ "-c" , "core.autocrlf=false" ]
77+ subprocess .run ([* git , "clone" , "--quiet" , "--shared" , "--no-tags" , "--no-checkout" ,
78+ "--" , str (ROOT ), str (snapshot )], cwd = ROOT , env = env , check = True )
79+ subprocess .run ([* git , "update-ref" , "--no-deref" , "HEAD" , INSPECTED ],
80+ cwd = snapshot , env = env , check = True )
81+ # The pinned planner reads omitted manifests from the sparse HEAD/index.
82+ patterns = "" .join (f"/{ path .as_posix ()} \n " for path in paths )
83+ subprocess .run ([* git , "sparse-checkout" , "set" , "--no-cone" , "--stdin" ],
84+ cwd = snapshot , env = env , input = patterns .encode ("utf-8" ), check = True )
85+ subprocess .run ([* git , "read-tree" , "-mu" , "HEAD" ], cwd = snapshot , env = env , check = True )
86+ for path , row in zip (paths , sources ["files" ], strict = True ):
87+ require (hashlib .sha256 ((snapshot / path ).read_bytes ()).hexdigest () == row ["sha256" ],
88+ "historical checkout source digest changed: " + path .as_posix ())
89+
90+
7291def entries (checker ):
7392 for name , config in checker .CONFIG .items ():
7493 for entry in [config , * config .get ("additional_inputs" , [])]:
7594 yield name , entry
7695
7796
78- def reject (checker , entry ):
97+ def reject (root , checker , entry ):
7998 try :
80- checker .authenticated_input (ROOT , entry )
99+ checker .authenticated_input (root , entry )
81100 except ValueError as exc :
82101 return str (exc )
83102 raise SystemExit ("historical input was admitted: " + entry ["path" ])
84103
85104
86- def main ():
87- require (not sys .flags .optimize , "assertions must stay enabled" )
88- checker = load_checker ()
105+ def check_snapshot (root , env ):
106+ checker = load_module (root / "scripts/replay_historical_check.py" )
89107 selected = [entry for name , entry in entries (checker )
90108 if name == "tooling-ledger" and entry ["path" ] == SELECTED_PATH ]
91109 require (len (selected ) == 1 , "selected-result checker overlay is not unique" )
@@ -94,44 +112,55 @@ def main():
94112 require (entry ["accepted_live_sha256" ] == [SELECTED_ACCEPTED ],
95113 "accepted selected-checker successors changed" )
96114 frozen = (checker .INPUTS / entry ["input" ]).read_bytes ()
97- live = (ROOT / SELECTED_PATH ).read_bytes ()
115+ live = (root / SELECTED_PATH ).read_bytes ()
98116 require (hashlib .sha256 (frozen ).hexdigest () == SELECTED_PIN , "frozen selected checker bytes changed" )
99117 require (hashlib .sha256 (live ).hexdigest () == SELECTED_LIVE , "live selected checker digest changed" )
100118 require (SELECTED_LIVE not in (SELECTED_PIN , SELECTED_ACCEPTED ), "live checker was already admitted" )
101119 # The recorded pin would pass the same membership test. The failure is the
102120 # live successor, not a broken comparison.
103121 require (SELECTED_PIN in (SELECTED_PIN , * entry ["accepted_live_sha256" ]),
104122 "pin membership control failed" )
105- require (reject (checker , entry ) == REJECTIONS [SELECTED_PATH ][1 ],
123+ require (reject (root , checker , entry ) == REJECTIONS [SELECTED_PATH ][1 ],
106124 "selected checker rejection text changed" )
107125
108126 counts = {path : 0 for path in REJECTIONS }
109127 messages = {}
110128 for _name , item in entries (checker ):
111129 if item ["path" ] not in REJECTIONS :
112130 continue
113- message = reject (checker , item )
131+ message = reject (root , checker , item )
114132 counts [item ["path" ]] += 1
115133 messages .setdefault (item ["path" ], message )
116134 for path , (count , message ) in REJECTIONS .items ():
117135 require (counts [path ] == count , "rejection count changed for " + path )
118136 require (messages [path ] == message , "rejection text changed for " + path )
119137 require (sum (counts .values ()) == 19 , "rejection total changed" )
120138 ci = [item for _name , item in entries (checker ) if item ["path" ] == CI_PATH ]
121- require (len (ci ) == 1 and hashlib .sha256 ((ROOT / CI_PATH ).read_bytes ()).hexdigest () == CI_LIVE ,
139+ require (len (ci ) == 1 and hashlib .sha256 ((root / CI_PATH ).read_bytes ()).hexdigest () == CI_LIVE ,
122140 "selected CI note digest changed" )
123141
124142 diff = subprocess .check_output (
125- ["git" , "diff" , "--name-only" , PARENT , INSPECTED ], cwd = ROOT , text = True )
143+ ["git" , "diff" , "--name-only" , PARENT , INSPECTED ], cwd = root , env = env , text = True )
126144 changed = sorted (name for name in diff .splitlines () if name )
127145 require (changed == CHANGED , "inspected commit diff changed" )
128- sys .path .insert (0 , str (ROOT / "scripts" ))
129- from research_package import plan_details
130- details = plan_details (ROOT , set (changed ))
146+ planner = load_module (root / "scripts/research_package.py" )
147+ details = planner .plan_details (root , set (changed ))
131148 require ([row ["package" ] for row in details ] == PLAN_PACKAGES , "helper plan selection changed" )
132149 matched = {name for row in details for names in row ["causes" ].values () for name in names }
133150 require (sorted (set (changed ) - matched ) == UNMATCHED , "unmatched plan paths changed" )
134- require (plan_details (ROOT , set ()) == [], "empty change set selected a package" )
151+ require (planner .plan_details (root , set ()) == [], "empty change set selected a package" )
152+
153+
154+ def main ():
155+ require (not sys .flags .optimize , "assertions must stay enabled" )
156+ sources = json .loads (Path (__file__ ).with_name ("SOURCES.json" ).read_bytes ())
157+ require (sources ["base_commit" ] == INSPECTED , "historical checkout base changed" )
158+ runner = load_module (ROOT / "scripts/research_package.py" )
159+ env = runner .git_source_env ()
160+ with tempfile .TemporaryDirectory (prefix = "historical-selected-replay-" ) as temporary :
161+ snapshot = Path (temporary ) / "snapshot"
162+ checkout_snapshot (snapshot , sources , runner , env )
163+ check_snapshot (snapshot , env )
135164 print ("PASS_HISTORICAL_SELECTED_REPLAY_GAP" )
136165 print ("selected_checker_live=" + SELECTED_LIVE )
137166 print ("rejections=19 distinct_paths=7 plan_packages=9" )
0 commit comments