checkpoint_seal.py
verification/tools/checkpoint_seal.py
1"""Owner-controlled end-of-run artifact digests, not proof or host attestation."""2from pathlib import Path3import hashlib4import json56import run_receipt78NAME = 'checkpoint-seal.json'91011def sha(path):12 return hashlib.sha256(path.read_bytes()).hexdigest()131415def files(output):16 output = Path(output)17 run_receipt.require(output.is_dir() and not output.is_symlink(), 'Invalid checkpoint directory')18 result = {}19 for path in sorted(output.rglob('*')):20 run_receipt.require(not path.is_symlink(), 'Symlinked checkpoint artifact')21 if path.is_file() and path != output / NAME:22 result[path.relative_to(output).as_posix()] = sha(path)23 run_receipt.require('inputs.json' in result, 'Checkpoint lacks source receipt')24 return result252627def bridge_artifacts(ticket, *, compiler_digest='rustc_sha256'):28 """Bind the current, already-validated equality/numeric producer ticket files."""29 result = {}30 for record in ticket['producers'].values():31 run_receipt.require(set(record) == {'output', 'exit_code', 'result_sha256', 'log_sha256',32 'inputs_sha256', 'staged_sha256'}, 'Unexpected producer ticket fields')33 output = Path(record['output'])34 for name, key in (('result.json', 'result_sha256'), ('run.log', 'log_sha256'), ('inputs.json', 'inputs_sha256')):35 result[str(output / name)] = record[key]36 result.update({str(output / name): digest for name, digest in record['staged_sha256'].items()})37 for compiler in ticket['compilers'].values():38 result[compiler['path']] = compiler[compiler_digest]39 return result404142def capture(output, *, kind, qualification, external=None, seal_path=None):43 """Call only after the owner's complete qualification/reconsumption gates."""44 output = Path(output)45 metadata = json.loads((output / 'inputs.json').read_text())46 run_receipt.verify_staged(output, metadata['staged'])47 external = {} if external is None else external48 run_receipt.require(all(Path(path).is_file() and sha(Path(path)) == digest49 for path, digest in external.items()), 'External checkpoint input changed')50 record = {'format': 1, 'kind': kind, 'factory': metadata['factory'], 'sdk': metadata['sdk'],51 'tools': metadata['binaries'], 'qualification': qualification, 'files': files(output), 'external': external}52 path = output / NAME if seal_path is None else Path(seal_path)53 with path.open('x') as handle:54 handle.write(json.dumps(record, indent=2) + '\n')55 digest = sha(path)56 verify(output, digest, factory=metadata['factory'], sdk=metadata['sdk'], tools=metadata['binaries'], seal_path=path)57 return digest585960def verify(output, expected_sha256, *, factory, sdk, tools, seal_path=None):61 """The caller supplies a reviewed digest and source identity, never learns them here."""62 output = Path(output)63 path = output / NAME if seal_path is None else Path(seal_path)64 run_receipt.require(run_receipt.regular(path) and sha(path) == expected_sha256,65 'Missing, changed or unreviewed checkpoint seal')66 record = json.loads(path.read_text())67 run_receipt.require(set(record) == {'format', 'kind', 'factory', 'sdk', 'tools', 'qualification', 'files', 'external'}68 and record['format'] == 1 and record['factory'] == factory and record['sdk'] == sdk and record['tools'] == tools,69 'Checkpoint source/tool identity differs')70 run_receipt.require(files(output) == record['files'], 'Checkpoint artifact set/bytes changed')71 run_receipt.require(all(Path(name).is_file() and sha(Path(name)) == digest72 for name, digest in record['external'].items()), 'External checkpoint input changed')73 metadata = json.loads((output / 'inputs.json').read_text())74 run_receipt.require(metadata['factory'] == factory and metadata['sdk'] == sdk and metadata['binaries'] == tools,75 'Checkpoint receipt identity differs')76 run_receipt.verify_staged(output, metadata['staged'])77 return record