#!/usr/bin/env python3 """Verify a KAB1 run directory (STRA-248). Standard library only, Python >= 3.9. verify_manifest.py D recompute everything; exit 0 and "OK files" when the directory is whole verify_manifest.py D --ids additionally print the set of ggml type ids found in the case headers Exit codes: 0 verified; 1 a file differs from the manifest or violates the format (the message names the file); 2 incomplete directory (no manifest.json: the writer publishes it LAST, so its absence means the run did not finish). Reader rule (KAB1): verify the manifest, recompute every sha256 and length, check the header-derived sizes, then use; any mismatch refuses the WHOLE directory. Nothing in a case file is trusted before it has been checked here. """ import hashlib import json import math import os import struct import sys MAGIC_IN = 0x4942414B # 'KABI' MAGIC_OUT = 0x4F42414B # 'KABO' VERSION = 1 H, FF = 2560, 640 GU_IDS = (16, 17, 18, 21, 22, 23, 42) # 16 / 17 only in the `ext` case set (STRA-329) DOWN_IDS = (20, 42) REQUIRED_IDS = {18, 20, 21, 22, 23, 42} EXT_IDS = REQUIRED_IDS | {16, 17} # ggml id -> (elements per block, bytes per block): crates/native-cpu-expert/src/lib.rs block() BLOCK = {18: (256, 98), 21: (256, 110), 22: (256, 82), 23: (256, 136), 42: (64, 18), 20: (32, 18), 16: (256, 66), 17: (256, 74)} TREE_ID = {"avx512": 0, "avx2": 1} IN_HEADER = 64 OUT_HEADER = 20 class Refused(Exception): def __init__(self, name, why): Exception.__init__(self, "%s: %s" % (name, why)) def sha256_of(path): h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def finite_f32_block(raw): """True when no 4-byte little-endian word of `raw` has the f32 exponent field all ones (inf or NaN).""" for (w,) in struct.iter_unpack("