legacy_upgrade_fixture_producer.py (12421B)
1 """Produce exact sanitized historical owner bytes from explicit public Git roots.""" 2 3 from pathlib import Path 4 import argparse 5 import hashlib 6 import json 7 import os 8 import subprocess 9 import tarfile 10 from legacy_upgrade_fixture_admission import admit_output, derive_refusal_inputs 11 12 MAX_FROZEN_FIXTURE_BYTES = 64 * 1024 * 1024 13 14 SOURCES = { 15 "tera": ("e25819267b51f659e5dfdf7b318239a8969bf45c", "radrootslabs/tera.git"), 16 "lib": ("ad17b7d3455a7147cfa303d976fc5c70c3a4c0cb", "radrootslabs/lib.git"), 17 "apple_kit": ( 18 "35aedb6b54ff645b663fecff26082b3e91fcb232", 19 "radrootslabs/apple_kit.git", 20 ), 21 } 22 23 24 def digest(data): 25 return hashlib.sha256(data).hexdigest() 26 27 28 def git(root, *args): 29 return subprocess.check_output( 30 ["git", "--no-replace-objects", "-C", str(root), *args] 31 ) 32 33 34 def validate_root(root, owner): 35 root = root.resolve(strict=True) 36 if ( 37 Path(git(root, "rev-parse", "--show-toplevel").decode().strip()).resolve() 38 != root 39 ): 40 raise ValueError("Explicit source root is not its Git root") 41 remote = git(root, "remote", "get-url", "origin").decode().strip() 42 suffix = SOURCES[owner][1] 43 if remote not in [ 44 "ssh://git@github.com/" + suffix, 45 "git@github.com:" + suffix, 46 "https://github.com/" + suffix, 47 ]: 48 raise ValueError("Explicit source identity differs") 49 revision = SOURCES[owner][0] 50 if ( 51 git(root, "rev-parse", "--verify", revision + "^{commit}").decode().strip() 52 != revision 53 ): 54 raise ValueError("Historical exact source is unavailable") 55 subprocess.run( 56 [ 57 "git", 58 "--no-replace-objects", 59 "-C", 60 str(root), 61 "merge-base", 62 "--is-ancestor", 63 revision, 64 "origin/master", 65 ], 66 check=True, 67 ) 68 return root 69 70 71 def archive(root, owner, work): 72 revision = SOURCES[owner][0] 73 path = work / (owner + ".tar") 74 directory = work / owner 75 raw = git(root, "archive", "--format=tar", revision) 76 path.write_bytes(raw) 77 directory.mkdir() 78 with tarfile.open(path) as tar: 79 members = [] 80 for member in tar.getmembers(): 81 name = Path(member.name) 82 if ( 83 name.is_absolute() 84 or ".." in name.parts 85 or not (member.isdir() or member.isfile()) 86 ): 87 raise ValueError("Historical archive contains unsupported paths") 88 if name.parts[0] not in [".github", ".act"]: 89 members.append(member) 90 tar.extractall(directory, members=members, filter="data") 91 return directory, { 92 "owner": owner, 93 "repository": "https://github.com/" + SOURCES[owner][1], 94 "commit": revision, 95 "tree": git(root, "rev-parse", revision + "^{tree}").decode().strip(), 96 "archive_sha256": digest(raw), 97 } 98 99 100 def copied_source(root, revision, path, destination, marker=None): 101 raw = git(root, "show", revision + ":" + path) 102 selected = raw if marker is None else raw[: raw.index(marker)] 103 destination.write_bytes(selected) 104 return { 105 "path": path, 106 "whole_blob_sha256": digest(raw), 107 "compiled_sha256": digest(selected), 108 "compiled_byte_count": len(selected), 109 "selection": "whole_file" 110 if marker is None 111 else "exact_prefix_before:" + marker.decode().strip(), 112 } 113 114 115 def run(argv, work, name, environment): 116 log = work / (name + ".txt") 117 with log.open("xb") as stream: 118 result = subprocess.run( 119 argv, stdout=stream, stderr=subprocess.STDOUT, env=environment 120 ) 121 if result.returncode: 122 raise RuntimeError( 123 "Historical producer lane failed; inspect its retained external log: " 124 + name 125 ) 126 return { 127 "name": name, 128 "exit_code": result.returncode, 129 "log_sha256": digest(log.read_bytes()), 130 } 131 132 133 def prepare_host(root, apple, work, swift_writer): 134 target = apple / "Tests/TeraHistoricalFixtureWriterTests" 135 target.mkdir() 136 manifest = apple / "Package.swift" 137 original = manifest.read_bytes() 138 anchor = b" targets: [\n" 139 if original.count(anchor) != 1: 140 raise ValueError("Historical test target insertion differs") 141 compiled = original.replace( 142 anchor, 143 anchor 144 + b' .testTarget(name: "TeraHistoricalFixtureWriterTests", dependencies: ["RadrootsKit"]),\n', 145 ) 146 manifest.write_bytes(compiled) 147 revision = SOURCES["tera"][0] 148 test_target = { 149 "path": "apple_kit/Package.swift", 150 "whole_blob_sha256": digest(original), 151 "compiled_sha256": digest(compiled), 152 "compiled_byte_count": len(compiled), 153 "selection": "explicit_isolated_historical_writer_test_target_only", 154 } 155 selections = [] 156 for path, name, marker in [ 157 ( 158 "Radroots/State/RadrootsConfigurationStore.swift", 159 "TeraLegacyConfigurationSource.swift", 160 None, 161 ), 162 ( 163 "Radroots/Runtime/RadrootsRuntimeModels.swift", 164 "TeraLegacyRuntimeModelsSource.swift", 165 b"enum RadrootsRuntimeSignerAvailability", 166 ), 167 ( 168 "Radroots/Runtime/RadrootsCheckedTime.swift", 169 "TeraLegacyCheckedTimeSource.swift", 170 None, 171 ), 172 ]: 173 selections.append(copied_source(root, revision, path, target / name, marker)) 174 (target / "TeraHistoricalFixtureWriterTests.swift").write_bytes( 175 swift_writer.read_bytes() 176 ) 177 return selections, test_target 178 179 180 def produce(roots, work, swift_writer, rust_writer): 181 # The caller must run through extbuild; never override Cargo routing. 182 managed = Path(os.environ["CARGO_TARGET_DIR"]).resolve().parent 183 work = work.resolve() 184 if work == managed or not work.is_relative_to(managed): 185 raise ValueError("Work must be a fresh child of the managed output root") 186 work.mkdir(parents=True, exist_ok=False) 187 archives = {} 188 provenance = [] 189 for owner, root in roots.items(): 190 archives[owner], record = archive(validate_root(root, owner), owner, work) 191 provenance.append(record) 192 selections, test_target = prepare_host( 193 roots["tera"], archives["apple_kit"], work, swift_writer 194 ) 195 test = archives["lib"] / "crates/mobile_ffi/tests/tera_historical_fixture_writer.rs" 196 test.parent.mkdir(exist_ok=True) 197 test.write_bytes(rust_writer.read_bytes()) 198 output = work / "output" 199 output.mkdir() 200 environment = os.environ.copy() 201 environment["TERA_LEGACY_FIXTURE_OUTPUT"] = str(output) 202 environment["RADROOTS_LIB_REVISION"] = SOURCES["lib"][0] 203 environment["RADROOTS_CONSUMER_REVISION"] = SOURCES["tera"][0] 204 environment["SOURCE_DATE_EPOCH"] = ( 205 git(roots["lib"], "show", "-s", "--format=%ct", SOURCES["lib"][0]) 206 .decode() 207 .strip() 208 ) 209 swift = [ 210 "swift", 211 "test", 212 "--package-path", 213 str(archives["apple_kit"]), 214 "--scratch-path", 215 str(work / "swift_scratch"), 216 "--cache-path", 217 os.environ["SWIFTPM_CACHE"], 218 "--disable-automatic-resolution", 219 "--filter", 220 "TeraHistoricalFixtureWriterTests/testProduceHistoricalOwnerBytes", 221 ] 222 receipts = [] 223 environment["TERA_LEGACY_FIXTURE_PHASE"] = "host" 224 receipts.append(run(swift, work, "historical-host", environment)) 225 receipts.append( 226 run( 227 [ 228 "cargo", 229 "test", 230 "--manifest-path", 231 str(archives["lib"] / "Cargo.toml"), 232 "--locked", 233 "-p", 234 "radroots_mobile_ffi", 235 "--test", 236 "tera_historical_fixture_writer", 237 "--", 238 "--test-threads=1", 239 ], 240 work, 241 "historical-rust", 242 environment, 243 ) 244 ) 245 environment["TERA_LEGACY_FIXTURE_PHASE"] = "transfer" 246 receipts.append(run(swift, work, "historical-transfer", environment)) 247 admission = admit_output(output, work) 248 admission["negative_derivatives"] = derive_refusal_inputs( 249 output, json.loads((output / "rust-metadata.json").read_text())["public_key"] 250 ) 251 (output / "owner-expectations.json").write_text( 252 json.dumps(admission, indent=2) + "\n" 253 ) 254 files = { 255 str(path.relative_to(output)): digest(path.read_bytes()) 256 for path in sorted(output.rglob("*")) 257 if path.is_file() 258 } 259 record = { 260 "state": "HISTORICAL_WRITER_ADMITTED_CURRENT_READERS_PENDING", 261 "sources": provenance, 262 "selected_unchanged_old_host_sources": selections, 263 "historical_writer_test_target": test_target, 264 "writer_source_sha256": { 265 "swift": digest(swift_writer.read_bytes()), 266 "rust": digest(rust_writer.read_bytes()), 267 }, 268 "actual_command_receipts": receipts, 269 "actual_case_admission": admission, 270 "files": files, 271 "limits": "Synthetic old actual owner writes, media preparation and typed native persistence. No old complete app binary, OS transfer, physical device, Keychain secret, network publication or byte-identical regeneration claim.", 272 } 273 (work / "producer.json").write_text(json.dumps(record, indent=2) + "\n") 274 return record 275 276 277 def frozen_source(work, relative): 278 path = Path(relative) 279 if path.is_absolute() or not path.parts or ".." in path.parts: 280 raise ValueError("Frozen fixture path escaped its owner") 281 source = work / "output" 282 if source.is_symlink(): 283 raise ValueError("Admitted owner bytes changed before freeze") 284 for part in path.parts: 285 source /= part 286 if source.is_symlink(): 287 raise ValueError("Admitted owner bytes changed before freeze") 288 if not source.is_file(): 289 raise ValueError("Admitted owner bytes changed before freeze") 290 return source 291 292 293 def frozen_inputs(work, record): 294 if record["state"] != "HISTORICAL_WRITER_ADMITTED_CURRENT_READERS_PENDING": 295 raise ValueError("Historical writer admission is missing") 296 contents = {} 297 total = 0 298 for relative, expected_hash in record["files"].items(): 299 path = Path(relative) 300 source = frozen_source(work, relative) 301 total += source.stat().st_size 302 if total > MAX_FROZEN_FIXTURE_BYTES: 303 raise ValueError("Historical fixture tooling input exceeds its byte bound") 304 content = source.read_bytes() 305 if digest(content) != expected_hash: 306 raise ValueError("Admitted owner bytes changed before freeze") 307 contents[path] = content 308 return contents 309 310 311 def freeze_output(work, record, destination): 312 """Freeze admitted owner bytes once; never rewrite an existing fixture.""" 313 expected = ( 314 Path(__file__).resolve().parent.parent / "test-fixtures/legacy_upgrade_v1" 315 ) 316 if ( 317 destination.resolve() != expected.resolve() 318 or expected.exists() 319 or expected.is_symlink() 320 ): 321 raise ValueError("Frozen fixture must be a fresh canonical owner path") 322 contents = frozen_inputs(work, record) 323 expected.mkdir() 324 for path, content in contents.items(): 325 output = expected / path 326 output.parent.mkdir(parents=True, exist_ok=True) 327 output.write_bytes(content) 328 (expected / "manifest.json").write_text(json.dumps(record, indent=2) + "\n") 329 330 331 def main(): 332 parser = argparse.ArgumentParser() 333 for owner in SOURCES: 334 parser.add_argument( 335 "--" + owner.replace("_", "-") + "-git-root", type=Path, required=True 336 ) 337 parser.add_argument("--work", type=Path, required=True) 338 parser.add_argument("--swift-writer", type=Path, required=True) 339 parser.add_argument("--rust-writer", type=Path, required=True) 340 parser.add_argument("--fixture-destination", type=Path) 341 args = parser.parse_args() 342 roots = { 343 owner: validate_root(getattr(args, owner + "_git_root"), owner) 344 for owner in SOURCES 345 } 346 result = produce(roots, args.work, args.swift_writer, args.rust_writer) 347 if args.fixture_destination is not None: 348 freeze_output(args.work, result, args.fixture_destination) 349 print(json.dumps({"state": result["state"], "file_count": len(result["files"])})) 350 351 352 if __name__ == "__main__": 353 main()