Research-Stack/5-Applications/tools-scripts/graph/file_manifest_builder.py
Devin AI 0c9efac330 chore(consolidation): integrate E8Sidon stack (PRs #79 #80 #81 #89) into one PR
Squash the four overlapping feature branches into a single change set against
main, eliminating cross-PR merge conflicts and the duplicated CI-fix scripts.

What this brings in (merge order #79 -> #80 -> #81 -> #89):
- #79 refactor(infra): shared utilities (4-Infrastructure/lib/*: q16, hashing,
  jsonl, fraction_utils) + the scripts/math-first/* validators that the
  math-check CI requires.
- #80 feat(lean): Semantics.E8Sidon (1025 lines) -- Eisenstein coefficient
  identity E4^2 = E8 and the Sidon framework. E4_sq_eq_E8_coeff is fully proved
  (all Fourier-coefficient extraction machine-checked); the single residual gap
  is pinned to E4_sq_eq_E8_qExpansion (Mathlib lacks the valence formula /
  dim M8 = 1). 4 sorries + 1 axiom (e8_additive_completeness), all TODO(lean-port).
- #81 refactor(lean): Float-free FixedPoint core (integer-only sqrt/log2/expNeg).
  E8Sidon.lean kept at #80's final 1025-line version (the #81 intermediate
  438-line copy was overridden by merge order).
- #89 feat(lean): Semantics.RRC.PolyFactorIdentity -- short-sleeve polynomial
  detection at the zerocopy limb boundary; now imports Semantics.E8Sidon for
  sigma3/sigma7/convolutionLHS (single source of truth) instead of inlining them.

Conflict resolution:
- flake.nix -> canonical rs-surface removal (Garnix shutdown).
- scripts/math-first/* -> byte-identical across branches, clean.
- .cursorrules / AGENTS.md -> unified; baselines + sorry inventory refreshed.

Verification:
- lake build (default aggregator): 3573 jobs, 0 errors.
- lake build Semantics.RRC.PolyFactorIdentity (E8Sidon + FixedPoint + PolyFactor):
  3655 jobs, 0 errors. Witnesses verified (sigma7 4 = 16513, convolutionLHS 6 = 2350).
- Python tests: 68/68 pass.

Note: the "Workers Builds: researchstack" check is a preexisting external
Cloudflare build unrelated to this change (no branch touches 4-Infrastructure/cloudflare/).

Build: 3573 jobs (default), 3655 jobs (narrow), 0 errors
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-16 02:01:31 +00:00

202 lines
7.4 KiB
Python

#!/usr/bin/env python3
# ==============================================================================
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
# PROJECT: SOVEREIGN STACK
# This artifact is entirely proprietary and cryptographically proven.
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ==============================================================================
import argparse
import json
from pathlib import Path
from typing import Any, Dict, List, Tuple, cast
import sys
sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "4-Infrastructure"))
from lib.hashing import sha256_bytes, sha256_file
def chunk_file(path: Path, chunk_size: int) -> List[Tuple[int, int, bytes]]:
chunks: List[Tuple[int, int, bytes]] = []
with path.open("rb") as handle:
index = 0
offset = 0
while True:
data = handle.read(chunk_size)
if not data:
break
chunks.append((index, offset, data))
index += 1
offset += len(data)
return chunks
def build_manifest(input_path: Path, chunk_size: int) -> Dict[str, Any]:
file_bytes = input_path.read_bytes()
file_hash = sha256_bytes(file_bytes)
chunks = chunk_file(input_path, chunk_size=chunk_size)
chunk_rows: List[Dict[str, Any]] = []
leaf_hashes: List[str] = []
for index, offset, data in chunks:
c_hash = sha256_bytes(data)
leaf_hashes.append(c_hash)
chunk_rows.append(
{
"index": index,
"offset_bytes": offset,
"length_bytes": len(data),
"sha256_hex": c_hash,
"sha256_bytes": 32,
"sha256_nibbles": 64,
}
)
merkle_root = sha256_bytes("".join(leaf_hashes).encode("ascii")) if leaf_hashes else ""
return {
"version": "manifest.v1",
"path": str(input_path),
"file_size_bytes": len(file_bytes),
"file_size_nibbles": len(file_bytes) * 2,
"sha256_hex": file_hash,
"sha256_bytes": 32,
"sha256_nibbles": 64,
"chunk_size_bytes": chunk_size,
"chunk_count": len(chunk_rows),
"merkle_root_sha256": merkle_root,
"chunks": chunk_rows,
}
def write_chunk_store(input_path: Path, manifest: Dict[str, Any], store_dir: Path) -> None:
store_dir.mkdir(parents=True, exist_ok=True)
chunks = cast_list_dict(manifest.get("chunks", []))
with input_path.open("rb") as handle:
for chunk in chunks:
offset = int(chunk["offset_bytes"])
length = int(chunk["length_bytes"])
digest = str(chunk["sha256_hex"])
handle.seek(offset)
data = handle.read(length)
out = store_dir / f"{digest}.bin"
if not out.exists():
out.write_bytes(data)
def cast_list_dict(value: Any) -> List[Dict[str, Any]]:
if isinstance(value, list):
out: List[Dict[str, Any]] = []
for v in cast(List[Any], value):
if isinstance(v, dict):
out.append(cast(Dict[str, Any], v))
return out
return []
def verify_manifest(manifest: Dict[str, Any], file_path: Path) -> Dict[str, Any]:
expected_size = int(manifest.get("file_size_bytes", 0) or 0)
expected_hash = str(manifest.get("sha256_hex", ""))
actual_size = file_path.stat().st_size
actual_hash = sha256_file(file_path)
return {
"size_match": actual_size == expected_size,
"hash_match": actual_hash == expected_hash,
"actual_size_bytes": actual_size,
"actual_sha256_hex": actual_hash,
}
def rebuild_from_store(manifest: Dict[str, Any], chunk_store: Path, out_file: Path) -> Dict[str, Any]:
chunks = cast_list_dict(manifest.get("chunks", []))
out_file.parent.mkdir(parents=True, exist_ok=True)
with out_file.open("wb") as handle:
for chunk in sorted(chunks, key=lambda c: int(c.get("index", 0))):
digest = str(chunk.get("sha256_hex", ""))
source_path = chunk_store / f"{digest}.bin"
if not source_path.exists():
raise FileNotFoundError(f"missing chunk in store: {source_path}")
data = source_path.read_bytes()
expected_len = int(chunk.get("length_bytes", 0) or 0)
if len(data) != expected_len:
raise ValueError(f"chunk length mismatch for {digest}: got {len(data)} expected {expected_len}")
if sha256_bytes(data) != digest:
raise ValueError(f"chunk hash mismatch for {digest}")
handle.write(data)
return verify_manifest(manifest, out_file)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Build deterministic file manifests with nibble sizes and SHA-256 metadata, plus verify/rebuild operations.")
sub = parser.add_subparsers(dest="command", required=True)
b = sub.add_parser("build", help="Build manifest from input file")
b.add_argument("--input", required=True, help="Input file path")
b.add_argument("--manifest-out", required=True, help="Output manifest JSON path")
b.add_argument("--chunk-size-bytes", type=int, default=1024 * 1024, help="Chunk size in bytes")
b.add_argument("--chunk-store", default="", help="Optional directory to emit chunk blobs named by SHA-256")
v = sub.add_parser("verify", help="Verify file against manifest")
v.add_argument("--manifest", required=True, help="Manifest JSON path")
v.add_argument("--file", required=True, help="File to verify")
r = sub.add_parser("rebuild", help="Rebuild file from chunk store + manifest")
r.add_argument("--manifest", required=True, help="Manifest JSON path")
r.add_argument("--chunk-store", required=True, help="Chunk store directory")
r.add_argument("--out-file", required=True, help="Rebuilt output file")
return parser.parse_args()
def main() -> int:
args = parse_args()
if args.command == "build":
in_path = Path(args.input)
manifest = build_manifest(in_path, chunk_size=max(1, int(args.chunk_size_bytes)))
out_path = Path(args.manifest_out)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
if args.chunk_store:
write_chunk_store(in_path, manifest, Path(args.chunk_store))
print(
json.dumps(
{
"manifest": str(out_path),
"file_size_bytes": manifest["file_size_bytes"],
"file_size_nibbles": manifest["file_size_nibbles"],
"sha256_hex": manifest["sha256_hex"],
"chunk_count": manifest["chunk_count"],
},
indent=2,
)
)
return 0
if args.command == "verify":
manifest = json.loads(Path(args.manifest).read_text(encoding="utf-8"))
result = verify_manifest(manifest, Path(args.file))
print(json.dumps(result, indent=2))
return 0 if result["size_match"] and result["hash_match"] else 2
if args.command == "rebuild":
manifest = json.loads(Path(args.manifest).read_text(encoding="utf-8"))
result = rebuild_from_store(manifest, Path(args.chunk_store), Path(args.out_file))
print(json.dumps(result, indent=2))
return 0 if result["size_match"] and result["hash_match"] else 2
return 2
if __name__ == "__main__":
raise SystemExit(main())