"""Replay a pinned PACTree StringKey fragment, not the full index or recovery.""" import hashlib import json import subprocess import sys import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parent SHA = "f4401629ed78865fc91eadf45b64aad3d98cff214661511ff6b6a98eca947355" REV = "f173a0f30a3ea492cdc194e6e83a00f9807a8cc8" CASES = [ ("nul_suffix", b"a\0x", b"a\0y", True, 1, 1), ("leading_nul", b"\0ax", b"\0by", True, 0, 0), ("ordinary_difference", b"abc", b"abd", False, 3, 3), ("equal_control", b"abc", b"abc", True, 3, 3), ] def run(): raw = (ROOT / "common.h").read_bytes() if hashlib.sha256(raw).hexdigest() != SHA: raise ValueError("Pinned header digest mismatch") source = raw.decode() start, end = "template ", "\n#ifdef STRINGKEY" if source.count(start) != 1 or source.count(end) != 1: raise ValueError("Unexpected fragment boundaries") fragment = source[source.index(start):source.index(end)] body = r""" int main() { const char inputs[][2][4] = { {{'a',0,'x',0},{'a',0,'y',0}}, {{0,'a','x',0},{0,'b','y',0}}, {{'a','b','c',0},{'a','b','d',0}}, {{'a','b','c',0},{'a','b','c',0}} }; for (int i=0; i<4; ++i) { StringKey<32> a(inputs[i][0]), b(inputs[i][1]); StringKey<32> c, d; c.set(inputs[i][0], 3); d.set(inputs[i][1], 3); std::cout << i << " constructor " << (a==b) << " " << a.size() << " " << b.size() << "\n"; std::cout << i << " explicit_length " << (c==d) << " " << c.size() << " " << d.size() << "\n"; } } """ program = "#include \n#include \n#include \n#include \n" + fragment + body modes = [["-O0"], ["-O2", "-DNDEBUG"]] outputs = [] with tempfile.TemporaryDirectory(prefix="hesela-pactree-") as tmp: cpp, exe = Path(tmp)/"probe.cpp", Path(tmp)/"probe" cpp.write_text(program) for flags in modes: subprocess.run(["c++", "-std=c++17", "-Wall", "-Wextra", *flags, str(cpp), "-o", str(exe)], check=True, timeout=60) text = subprocess.check_output([str(exe)], text=True, timeout=10) rows = [] for line in text.splitlines(): i, api, equal, left, right = line.split() name, a, b, expected, la, lb = CASES[int(i)] actual = (bool(int(equal)), int(left), int(right)) if actual != (expected, la, lb): raise ValueError((name, api, actual)) rows.append({"case":name, "api":api, "left_hex":a.hex(), "right_hex":b.hex(), "bytes_equal":a == b, "string_key_equal":actual[0], "reported_left_size":actual[1], "reported_right_size":actual[2]}) if len(rows) != 8 or len({(r["case"], r["api"]) for r in rows}) != 8: raise ValueError("Incomplete native matrix") outputs.append({"flags":flags, "rows":rows}) if outputs[0]["rows"] != outputs[1]["rows"]: raise ValueError("Optimization changes observed behavior") return {"schema_version":1, "upstream_revision":REV, "header_sha256":SHA, "scope":"Exact StringKey class fragment; no full index, PMDK, persistence or performance test.", "compiler":subprocess.check_output(["c++","--version"], text=True).splitlines()[0], "runs":outputs} if __name__ == "__main__": result = run() if "--check" in sys.argv: recorded = json.loads((ROOT/"results.json").read_text()) # Compiler identification is provenance, not an assumed portable invariant. if {k:v for k,v in result.items() if k != "compiler"} != {k:v for k,v in recorded.items() if k != "compiler"}: raise SystemExit("Recorded observations differ") print("PASS: 16 native observations across two compiler configurations") else: (ROOT/"results.json").write_text(json.dumps(result, indent=2)+"\n")