#!/usr/bin/env python3
"""Bounded read-only GGUF storage inventory; no neural runtime or tensor decode.

The exact pinned editor Scanner admits the complete little-endian single-file
v3 directory. Code/runtime/lock hashes precede NumPy/GGUF imports. Reports contain
raw ranges/geometry/hashes, never tensor values or numerical control authority.
"""
import hashlib
import importlib.metadata
import importlib.util
import json
import math
import os
from pathlib import Path
import re
import resource
import signal
import stat
import struct
import sys
import time

SCHEMA = "omnius.nol.gguf-control-reader-request.v1"
EDITOR_SHA = "fecf1d3ccb4b4d3964d15ae98b1da231e3eabb0aa5d3395f334cc6df24f9ea78"
CODE = re.compile(r"(?:\.(?:py|pyi|pyd|dll|dylib|pth)|\.so(?:\.\d+)*)$|\.dist-info/(?:METADATA|WHEEL|RECORD|entry_points\.txt)$")
LIMITS = {"maxInputBytes", "maxWorkingBytes", "maxReadBytes", "maxTensors", "maxCoordinates",
          "maxRuntimeBytes", "maxRuntimeFiles", "maxDurationMs", "maxProtocolBytes",
          "maxStderrBytes", "chunkBytes", "maxReportBytes", "maxAddressSpaceBytes",
          "maxMetadataBytes", "maxKeyValues", "maxArrayElements", "maxDecodedCoordinates", "maxDecodeArithmeticUnits"}


def need(ok, message):
    if not ok:
        raise ValueError(message)


def fields(value, required, optional=()):
    need(type(value) is dict and set(required) <= value.keys() <= set(required) | set(optional), "Unknown/missing reader fields")


def pairs(items):
    result = {}
    for key, value in items:
        need(key not in result, "Duplicate JSON key")
        result[key] = value
    return result


def parse(raw):
    return json.loads(raw, object_pairs_hook=pairs, parse_constant=lambda _: (_ for _ in ()).throw(ValueError("Nonfinite JSON")),
                      parse_int=lambda text: -0.0 if text == "-0" else int(text))


def encoded(value):
    return json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()


def integer(value, minimum=0, maximum=9007199254740991):
    need(type(value) is int and minimum <= value <= maximum, "Invalid reader integer")
    return value


def regular(path):
    path = Path(path)
    info = path.lstat()
    need(stat.S_ISREG(info.st_mode) and not path.is_symlink() and path.resolve(strict=True) == path, "Reader source must be canonical nonsymlink regular file")
    return info


def identity(info):
    return (info.st_dev, info.st_ino, info.st_size, info.st_mtime_ns, info.st_ctime_ns)


class Budget:
    def __init__(self, limits):
        fields(limits, LIMITS)
        for value in limits.values():
            integer(value, 1)
        need(limits["maxProtocolBytes"] <= 16 * 1024 ** 2 and limits["maxStderrBytes"] <= 1024 ** 2 and
             4096 <= limits["chunkBytes"] <= 4 * 1024 ** 2 and limits["chunkBytes"] % 4 == 0 and
             limits["maxDurationMs"] <= 86400000, "Unsupported reader limits")
        self.limits, self.started = limits, time.monotonic()
        self.read_bytes = self.working = self.runtime_bytes = self.runtime_files = 0
        self.input = self.artifacts = self.arithmetic = 0
        self.stamps = {}
        self.directories = {}
        need(limits["maxMetadataBytes"] <= 256 * 1024 ** 2 and limits["maxKeyValues"] <= 100000 and limits["maxArrayElements"] <= 10000000, "GGUF metadata bounds exceeded")
        self.reserve(limits["chunkBytes"] * 3 + limits["maxProtocolBytes"] * 2)

    def check(self):
        need(time.monotonic() - self.started <= self.limits["maxDurationMs"] / 1000, "Reader wall-clock budget exceeded")

    def read(self, count):
        self.check()
        self.read_bytes += integer(count)
        need(self.read_bytes <= self.limits["maxReadBytes"], "Reader logical-read byte budget exceeded")

    def reserve(self, count):
        self.check()
        self.working += integer(count)
        need(self.working <= self.limits["maxWorkingBytes"], "Reader working-byte budget exceeded")

    def guard(self):
        self.check()
        for path, stamp in self.stamps.items():
            need(identity(regular(path)) == stamp, "Reader source/runtime/checkpoint changed")
        for path, stamp in self.directories.items():
            info = Path(path).lstat()
            need(stat.S_ISDIR(info.st_mode) and not Path(path).is_symlink() and identity(info) == stamp, "Reader checkpoint directory inventory changed")


def hash_file(path, budget, maximum=None):
    path = Path(path)
    before = regular(path)
    need(maximum is None or before.st_size <= maximum, "Reader file exceeds declared bound")
    budget.read(before.st_size)
    result, size = hashlib.sha256(), 0
    with path.open("rb") as stream:
        need(identity(os.fstat(stream.fileno())) == identity(before), "Reader file changed while opening")
        while True:
            budget.check()
            block = stream.read(budget.limits["chunkBytes"])
            if not block:
                break
            size += len(block)
            need(size <= before.st_size, "Reader file grew")
            result.update(block)
        need(identity(os.fstat(stream.fileno())) == identity(before), "Reader file changed while hashing")
    need(size == before.st_size and identity(regular(path)) == identity(before), "Reader file extent/identity changed")
    budget.stamps[str(path)] = identity(before)
    return {"sha256": result.hexdigest(), "sizeBytes": size}


def read_range(path, offset, count, budget):
    info = regular(path)
    integer(offset)
    integer(count)
    need(offset + count <= info.st_size, "Reader range escaped actual source")
    budget.read(count)
    with Path(path).open("rb") as stream:
        need(identity(os.fstat(stream.fileno())) == identity(info), "Reader range source changed")
        stream.seek(offset)
        raw = stream.read(count)
        need(len(raw) == count and identity(os.fstat(stream.fileno())) == identity(info), "Reader range truncated/changed")
    return raw


def cpu_guard(allow_codecs=False):
    need(all(os.environ.get(key) == "-1" for key in ("CUDA_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES", "ROCR_VISIBLE_DEVICES")), "Explicit denied CUDA/HIP/ROCR visibility required")
    need("torch" not in sys.modules and not any(name.startswith(("transformers", "llama_cpp", "gne")) for name in sys.modules), "Framework/model imported before CPU admission")
    need(sys.byteorder == "little", "Only little-endian CPU storage is supported")
    if not allow_codecs:
        need("numpy" not in sys.modules and "gguf" not in sys.modules, "Codecs imported before full runtime admission")


def bootstrap(budget):
    # -I alone still processes site .pth files. -S keeps their arbitrary startup
    # code inert; append only this explicit venv's canonical package directory.
    need(sys.flags.isolated == 1 and sys.flags.no_site == 1, "Reader requires isolated no-site Python startup")
    executable = Path(os.path.abspath(sys.executable))
    root = executable.parent.parent
    config = root / "pyvenv.cfg"
    config_hash = hash_file(config, budget, 65536)
    site = root / "lib" / ("python%d.%d" % sys.version_info[:2]) / "site-packages"
    info = site.lstat()
    need(stat.S_ISDIR(info.st_mode) and not site.is_symlink() and site.resolve(strict=True) == site, "Explicit canonical venv site-packages required")
    need(str(site) not in sys.path, "Venv packages unexpectedly active before admission")
    sys.path.append(str(site))
    return {"policy": "isolated_no_site_processing", "venvConfig": config_hash,
            "sitePackagesPathDigest": hashlib.sha256(str(site).encode()).hexdigest()}


def load_editor(payload, budget):
    cpu_guard()
    own = hash_file(Path(__file__).resolve(strict=True), budget)
    need(own["sha256"] == payload["expectedReaderSha256"], "Reader source pin mismatch")
    path = Path(payload["checkpointEditorPath"])
    source = hash_file(path, budget)
    need(source["sha256"] == payload["expectedCheckpointEditorSha256"] == EDITOR_SHA, "Exact GGUF Scanner source pin mismatch")
    spec = importlib.util.spec_from_file_location("nol_pinned_gguf_admission", path)
    need(spec is not None and spec.loader is not None, "Pinned editor import unavailable")
    editor = importlib.util.module_from_spec(spec)
    # This pinned module is stdlib-only at import. Never register it as a
    # substitutable package or execute its __main__ edit/publish entry point.
    spec.loader.exec_module(editor)
    cpu_guard()
    original = editor.hash_file

    def tracked_hash(path, original_budget=None):
        budget.read(regular(path).st_size)
        return original(path, original_budget)

    editor.hash_file = tracked_hash
    return editor, own, source, bootstrap(budget)


def runtime_inventory(payload, budget, own, editor_source, python_environment):
    packages, names = [], set()
    for distribution in sorted(importlib.metadata.distributions(), key=lambda d: d.metadata.get("Name", "").lower()):
        budget.check()
        name = distribution.metadata.get("Name", "")
        if not name:
            continue
        need(name.lower() not in names, "Ambiguous duplicate installed distribution")
        names.add(name.lower())
        paths = {}
        metadata = Path(distribution._path)
        record = metadata / "RECORD"
        record_size = regular(record).st_size
        need(record_size <= min(budget.limits["maxProtocolBytes"] * 4, 16 * 1024 ** 2), "Runtime RECORD allocation cap exceeded")
        budget.reserve(record_size * 8)
        hash_file(record, budget)
        budget.read(record_size)  # importlib.metadata's bounded CSV projection.
        for item in distribution.files or ():
            text = str(item)
            if CODE.search(text) and "__pycache__" not in text:
                paths[text] = Path(os.path.abspath(distribution.locate_file(item)))
                need(len(paths) <= budget.limits["maxRuntimeFiles"], "Runtime file enumeration cap exceeded")
        # Codecs are checked against the complete actual package trees, not
        # merely a potentially incomplete RECORD file. Bundled libraries and
        # all distribution metadata are included before any codec import.
        if name.lower() in ("numpy", "gguf"):
            base = Path(distribution.locate_file(""))
            package = base / name.lower()
            spec = importlib.util.find_spec(name.lower())
            need(spec is not None and spec.origin == str(package / "__init__.py") and list(spec.submodule_search_locations or ()) == [str(package)], "Codec import origin differs from pinned package")
            candidates = [package, base / (name.lower() + ".libs"), Path(distribution._path)]
            for top in candidates:
                if not top.exists():
                    continue
                pending = [top]
                while pending:
                    directory = pending.pop(); budget.check()
                    info = directory.lstat()
                    need(stat.S_ISDIR(info.st_mode) and not directory.is_symlink(), "Runtime package directory symlink refused")
                    budget.directories[str(directory)] = identity(info)
                    with os.scandir(directory) as children:
                        for child in children:
                            budget.check()
                            need(not child.is_symlink(), "Runtime package symlink refused")
                            if child.name == "__pycache__" or child.name.endswith((".pyc", ".pyo")):
                                continue
                            path = Path(child.path)
                            if child.is_dir(follow_symlinks=False):
                                pending.append(path)
                            else:
                                need(child.is_file(follow_symlinks=False), "Runtime special file refused")
                                paths[path.relative_to(base).as_posix()] = path
                            need(len(paths) + len(pending) <= budget.limits["maxRuntimeFiles"], "Complete runtime tree enumeration cap exceeded")
        entries, total = [], 0
        for text, path in sorted(paths.items()):
            budget.check()
            info = regular(path)
            budget.runtime_bytes += info.st_size; budget.runtime_files += 1
            need(budget.runtime_bytes <= budget.limits["maxRuntimeBytes"] and budget.runtime_files <= budget.limits["maxRuntimeFiles"], "Reader runtime inventory budget exceeded")
            budget.reserve(512 + len(text.encode()))
            actual = hash_file(path, budget); total += actual["sizeBytes"]
            entries.append({"path": text, **actual})
        packages.append({"name": name, "version": distribution.version, "files": len(entries), "bytes": total, "digest": hashlib.sha256(encoded(entries)).hexdigest()})
    need(any(p["name"].lower() == "numpy" for p in packages) and any(p["name"].lower() == "gguf" for p in packages), "Explicit NumPy/GGUF codec environment unavailable")
    need(importlib.metadata.version("gguf") == "0.19.0", "Unreviewed GGUF codec version")
    body = {"schema": "omnius.nol.gguf-control-reader-runtime.v1", "pythonVersion": sys.version.split()[0],
            "pythonExecutable": hash_file(Path(sys.executable).resolve(strict=True), budget), "packages": packages,
            "reader": own, "checkpointEditor": editor_source, "dependencyLock": hash_file(Path(payload["dependencyLockPath"]), budget, 1024 ** 2), "pythonEnvironment": python_environment}
    cpu_guard()
    return {**body, "runtimeDigest": hashlib.sha256(encoded(body)).hexdigest()}


# Independent host uses the same wire specification, not NumPy/gguf dequantize.
BLOCKS = {"F32": (0, 1, 4), "F16": (1, 1, 2), "BF16": (30, 1, 2), "Q8_0": (8, 32, 34),
          "Q4_0": (2, 32, 18), "Q4_K": (12, 256, 144), "Q6_K": (14, 256, 210)}


def storage_class(name, shape):
    need(type(name) is str and len(shape) in (1, 2), "Unknown/packed GGUF tensor rank")
    if name in ("token_embd.weight", "position_embd.weight", "output.weight"):
        need(len(shape) == 2, "Embedding/output storage must be a matrix")
        return "ordinary_matrix_storage_only"
    if name in ("output_norm.weight", "token_embd_norm.weight", "rope_freqs.weight", "output.bias") or re.fullmatch(r"blk\.\d+\.(attn_norm|ffn_norm|post_attention_norm|attn_q_norm|attn_k_norm|ssm_norm)\.weight", name):
        need(len(shape) == 1, "Norm/vector storage must be a vector")
        return "ordinary_vector_storage_only"
    if name in ("output_scale", "logit_scale"):
        need(shape == [1], "Scalar storage must have one element")
        return "ordinary_scalar_storage_only"
    if re.fullmatch(r"blk\.\d+\.(ffn_(up|gate|down)|attn_(q|k|v|qkv|output|gate)|ssm_(alpha|beta|out|conv1d))\.weight", name):
        need(len(shape) == 2, "Known dense/recurrent storage must be a matrix")
        return "ordinary_matrix_storage_only"
    if re.fullmatch(r"blk\.\d+\.(ffn_(up|gate|down)|attn_(q|k|v|qkv|output|gate)|ssm_dt)\.bias", name) or re.fullmatch(r"blk\.\d+\.ssm_a", name):
        need(len(shape) == 1, "Known bias/recurrent storage must be a vector")
        return "ordinary_vector_storage_only"
    raise ValueError("Unknown/expert/router/packed GGUF storage name; semantics are not inferred")


def inventory(payload, editor, runtime, budget):
    model, root = payload["model"], Path(payload["checkpointRoot"])
    need(root.is_absolute() and root.resolve(strict=True) == root and not root.is_symlink(), "Canonical checkpoint root required")
    need(model.get("format") == "gguf", "Only explicit single-file GGUF supported")
    files = model["files"]
    need(type(files) is list and 0 < len(files) <= 100000, "Bounded model source inventory required")
    weights = [f for f in files if f["role"] == "weights"]
    need(len(weights) == 1 and weights[0]["path"].endswith(".gguf") and sum(f["path"].endswith(".gguf") for f in files) == 1, "Split/mixed/compressed GGUF checkpoints unsupported")
    total = sum(integer(f["artifact"]["sizeBytes"]) for f in files)
    need(total <= budget.limits["maxInputBytes"], "Reader aggregate model input cap exceeded")
    identity_body = payload["identity"]
    fields(identity_body, ("modelJson", "tensorSitesJson"))
    body = {key: value for key, value in model.items() if key not in ("id", "modelDigest")}
    need(hashlib.sha256(identity_body["modelJson"].encode()).hexdigest() == model["modelDigest"] and editor.same(parse(identity_body["modelJson"]), body), "Frozen complete model identity mismatch")
    need(hashlib.sha256(identity_body["tensorSitesJson"].encode()).hexdigest() == model["architectureMapDigest"] and editor.same(parse(identity_body["tensorSitesJson"]), model["tensorSites"]), "Frozen tensor-map identity mismatch")
    expected = {file["path"] for file in files}
    need(len(expected) == len(files), "Duplicate source path")
    directories, actual = [root], set()
    while directories:
        directory = directories.pop(); budget.check()
        info = directory.lstat()
        need(stat.S_ISDIR(info.st_mode) and not directory.is_symlink(), "Checkpoint directory symlink unsupported")
        budget.directories[str(directory)] = identity(info)
        need(len(budget.directories) <= len(expected) * 2 + 32, "Directory scan bound exceeded")
        with os.scandir(directory) as entries:
            for entry in entries:
                budget.check(); need(not entry.is_symlink(), "Checkpoint symlink unsupported")
                path = Path(entry.path)
                if entry.is_dir(follow_symlinks=False): directories.append(path)
                else:
                    need(entry.is_file(follow_symlinks=False), "Special checkpoint file refused")
                    actual.add(path.relative_to(root).as_posix())
                    need(len(actual) <= len(expected), "Complete source directory inventory exceeded")
    need(actual == expected, "Complete source directory differs from frozen model")
    facts = []
    for file in files:
        value = hash_file(editor.safe(root, file["path"]), budget)
        need(value == {"sha256": file["artifact"]["sha256"], "sizeBytes": file["artifact"]["sizeBytes"]}, "Complete source file hash mismatch")
        facts.append({"path": file["path"], "role": file["role"], **value})
    # Recompute the complete code/runtime/lock inventory before the first
    # Scanner import of NumPy/GGUF. No codecs or neural framework ran above.
    actual_runtime = runtime()
    need(editor.same(actual_runtime, payload["runtime"]), "Pinned reader runtime changed before codecs")
    cpu_guard()
    class TrackedScanner(editor.Scanner):
        def read(self, size):
            budget.read(size)
            return super().read(size)
    path = editor.safe(root, weights[0]["path"])
    allocation_bound = min(regular(path).st_size, budget.limits["maxMetadataBytes"]) * 4 + budget.limits["maxArrayElements"] * 1024 + budget.limits["maxTensors"] * 2048
    need(budget.working + allocation_bound <= budget.limits["maxWorkingBytes"], "GGUF metadata allocation bound exceeded before codec/Scanner import")
    scanned = TrackedScanner(path, budget)
    need(len(scanned.tensors) <= budget.limits["maxTensors"], "Tensor inventory bound exceeded")
    for key, (_, value) in scanned.keys.items():
        if "expert" in key.lower() or "router" in key.lower():
            need(value == 0 and type(value) in (int, bool), "Expert/router metadata requires a separate packed-storage protocol")
    architecture = scanned.keys["general.architecture"][1]
    need("moe" not in architecture.lower(), "MoE packed architecture is not admitted by this reader")
    # Scanner is complete and rejects split metadata, duplicates, malformed
    # arrays, unknown quantization IDs, aliases, overlaps and truncated ranges.
    editor.verify_official(path, scanned, budget)
    cpu_guard(True)
    coordinates, tensors = 0, []
    mapped = {s["name"]: s for s in model["tensorSites"] if s["kind"] == "parameter"}
    for tensor in scanned.tensors:
        budget.check()
        kind = tensor["qtype"]
        need(kind in BLOCKS, "Unsupported/unknown physical storage codec")
        type_id, block, width = BLOCKS[kind]
        need(tensor["typeId"] == type_id and tensor["ne"][0] % block == 0, "Actual codec block identity changed")
        classification = storage_class(tensor["name"], tensor["shape"])
        if tensor["name"] in mapped:
            site = mapped[tensor["name"]]
            need(site["shape"] == tensor["shape"] and site["dtype"] == editor.logical_dtype(kind), "Declared parameter map differs from storage")
        coordinates += tensor["elements"]
        need(coordinates <= budget.limits["maxCoordinates"], "Tensor coordinate inventory cap exceeded")
        size, start = tensor["sizeBytes"], tensor["dataOffset"]
        sha = hashlib.sha256()
        for offset in range(0, size, budget.limits["chunkBytes"]):
            sha.update(read_range(path, start + offset, min(budget.limits["chunkBytes"], size - offset), budget))
        tensors.append({"name": tensor["name"], "shape": tensor["shape"], "ggmlDimensions": tensor["ne"], "qtype": kind,
                        "typeId": type_id, "sourceFile": weights[0]["path"], "dataOffsetBytes": start, "sizeBytes": size,
                        "sha256": sha.hexdigest(), "elements": tensor["elements"], "storageClass": classification,
                        "block": {"elements": block, "bytes": width, "rowElements": tensor["ne"][0],
                                  "rowBytes": tensor["ne"][0] // block * width, "blocks": tensor["elements"] // block},
                        "decoding": "independent_host_supported" if kind in ("F32", "F16", "BF16", "Q8_0") else "opaque_only"})
    need(set(mapped) <= set(scanned.byName), "Declared parameter is missing from actual GGUF")
    for file in files:
        need(hash_file(editor.safe(root, file["path"]), budget) == {"sha256": file["artifact"]["sha256"], "sizeBytes": file["artifact"]["sizeBytes"]}, "Source changed after inventory")
    metadata_hash = hashlib.sha256()
    for offset in range(0, scanned.dataStart, budget.limits["chunkBytes"]):
        metadata_hash.update(read_range(path, offset, min(budget.limits["chunkBytes"], scanned.dataStart - offset), budget))
    metadata = metadata_hash.hexdigest()
    budget.guard()
    return {"schema": "omnius.nol.gguf-control-inventory.v1", "modelDigest": model["modelDigest"],
            "runtimeDigest": actual_runtime["runtimeDigest"], "readerSha256": actual_runtime["reader"]["sha256"],
            "checkpointEditorSha256": EDITOR_SHA, "files": facts, "tensors": sorted(tensors, key=lambda t: t["name"]),
            "version": 3, "alignment": scanned.alignment, "dataStartBytes": scanned.dataStart, "metadataSha256": metadata,
            "usage": {"inputBytes": total, "logicalReadBytes": budget.read_bytes, "reservedWorkingBytes": budget.working},
            "officialReaderVerified": True, "codecsImportedAfterFullRuntimeAdmission": True, "device": "cpu",
            "cudaInitialized": False, "modelConstructed": False, "numericalControlVerified": False, "authority": "none"}


def main():
    raw = sys.stdin.buffer.read(16 * 1024 ** 2 + 1)
    need(len(raw) <= 16 * 1024 ** 2, "Absolute reader request cap exceeded")
    payload = parse(raw)
    fields(payload, ("schema", "action", "expectedReaderSha256", "checkpointEditorPath", "expectedCheckpointEditorSha256", "dependencyLockPath", "limits"),
           ("runtime", "model", "checkpointRoot", "identity"))
    need(payload["schema"] == SCHEMA and payload["action"] in ("inspect", "inventory"), "Unsupported reader action/schema")
    budget = Budget(payload["limits"])
    need(len(raw) <= budget.limits["maxProtocolBytes"], "Reader request protocol cap exceeded")
    resource.setrlimit(resource.RLIMIT_AS, (budget.limits["maxAddressSpaceBytes"], budget.limits["maxAddressSpaceBytes"]))
    resource.setrlimit(resource.RLIMIT_CPU, (max(1, math.ceil(budget.limits["maxDurationMs"] / 1000)), max(2, math.ceil(budget.limits["maxDurationMs"] / 1000) + 1)))
    editor, own, source, python_environment = load_editor(payload, budget)
    runtime = lambda: runtime_inventory(payload, budget, own, source, python_environment)
    if payload["action"] == "inspect":
        need(not any(key in payload for key in ("runtime", "model", "checkpointRoot", "identity")), "Inspect cannot accept checkpoint/model input")
        result = {"runtime": runtime(), "codecsImported": False, "torchImported": False, "cudaInitialized": False, "modelConstructed": False}
        budget.guard()
    else:
        need(all(key in payload for key in ("runtime", "model", "checkpointRoot", "identity")), "Frozen runtime/model input required")
        result = inventory(payload, editor, runtime, budget)
    raw = encoded(result)
    need(len(raw) <= budget.limits["maxProtocolBytes"], "Reader response protocol cap exceeded")
    sys.stdout.buffer.write(raw); sys.stdout.flush()


if __name__ == "__main__":
    try:
        signal.signal(signal.SIGTERM, lambda *_: (_ for _ in ()).throw(InterruptedError("CPU reader Stop")))
        main()
    except BaseException as error:
        sys.stderr.write((type(error).__name__ + ": " + str(error))[-4000:] + "; codecsImported=" + str("numpy" in sys.modules or "gguf" in sys.modules).lower() + "; torchImported=" + str("torch" in sys.modules).lower() + "\n")
        sys.exit(1)
