#!/usr/bin/env python3
"""Bounded CPU-only GGUF parameter export. No model/native runtime is loaded.

GGUF codecs are supplied by an explicitly pinned gguf/NumPy installation; this
file contains original bounded parsing/publication logic, not vendored GNE code.
Operations run in declared order at IEEE float64 reference precision, followed
by one storage encode per changed tensor. This is never native equivalence.
"""
from __future__ import annotations
import ctypes
import hashlib
import importlib.metadata
import json
import math
import os
from pathlib import Path
import platform
import re
import signal
import stat
import struct
import sys
import time

OPS = {"WEIGHT_ADD", "WEIGHT_GAIN", "WEIGHT_REPLACE", "WEIGHT_PROJECT", "LOW_RANK_UPDATE", "WEIGHT_MERGE"}
SUPPORTED = {"F32", "F16", "BF16", "Q4_0", "Q8_0", "Q4_K", "Q6_K"}
FLOATS = {"f16": "<f2", "f32": "<f4", "f64": "<f8"}
MAX_FRAME = 16 * 1024 * 1024


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


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


def finite(value):
    need(type(value) in (int, float) and math.isfinite(value), "Nonfinite scalar")
    return float(value)


def fields(value, required, optional=()):
    need(type(value) is dict and set(required) <= value.keys() <= set(required) | set(optional), "Unexpected/missing protocol 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")))


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


def digest(raw):
    return hashlib.sha256(raw).hexdigest()


def same(a, b):
    if type(a) in (int, float) and type(b) in (int, float):
        return a == b and (a != 0 or math.copysign(1, a) == math.copysign(1, b))
    if type(a) is not type(b):
        return False
    if type(a) is dict:
        return a.keys() == b.keys() and all(same(a[k], b[k]) for k in a)
    if type(a) is list:
        return len(a) == len(b) and all(same(x, y) for x, y in zip(a, b))
    return a == b


class Budget:
    def __init__(self, limits):
        names = ("maxInputBytes", "maxArtifactBytes", "maxOutputBytes", "maxWorkingBytes", "maxArithmeticUnits", "maxOperations", "maxDurationMs", "maxProtocolBytes", "maxMetadataBytes", "maxTensors", "maxKeyValues", "maxArrayElements")
        fields(limits, names)
        for value in limits.values():
            integer(value, 1)
        need(limits["maxProtocolBytes"] <= MAX_FRAME and limits["maxMetadataBytes"] <= 256 * 1024 ** 2 and limits["maxDurationMs"] <= 86400000 and limits["maxTensors"] <= 100000 and limits["maxKeyValues"] <= 100000 and limits["maxArrayElements"] <= 10000000, "Unsupported absolute admission bound")
        self.limits = limits
        self.started = time.monotonic()
        self.deadline = self.started + limits["maxDurationMs"] / 1000
        self.input = self.artifacts = self.working = self.arithmetic = 0
        self.verified = {}
        self.inputHashes, self.artifactHashes = set(), set()
        self.reserve(262144)

    def check(self):
        need(time.monotonic() <= self.deadline, "GGUF wall budget exceeded")

    def reserve(self, count):
        self.check()
        self.working += integer(count)
        need(self.working <= self.limits["maxWorkingBytes"], "GGUF cumulative tensor/workspace reservation exceeded")

    def work(self, count):
        self.check()
        self.arithmetic += integer(count)
        need(self.arithmetic <= self.limits["maxArithmeticUnits"], "GGUF arithmetic budget exceeded")


def regular(path):
    need(path.is_absolute(), "Explicit absolute file required")
    current = Path(path.anchor)
    for part in path.parts[1:]:
        current /= part
        need(not current.is_symlink(), "Symlink component refused")
    info = path.lstat()
    need(stat.S_ISREG(info.st_mode), "Nonsymlink regular file required")
    return info


def safe(root, name):
    need(type(name) is str and name and "\\" not in name and "\0" not in name and not name.startswith("/") and all(x not in ("", ".", "..") for x in name.split("/")), "Unsafe relative file path")
    path = root / name
    regular(path)
    return path


def hash_file(path, budget=None):
    before = regular(path)
    h, size = hashlib.sha256(), 0
    fd = os.open(path, os.O_RDONLY | os.O_NOFOLLOW)
    try:
        opened = os.fstat(fd)
        need((opened.st_dev, opened.st_ino) == (before.st_dev, before.st_ino), "Source inode changed before open")
        with os.fdopen(fd, "rb", closefd=False) as f:
            while True:
                if budget:
                    budget.check()
                block = f.read(65536)
                if not block:
                    break
                h.update(block)
                size += len(block)
        after = os.fstat(fd)
        need((opened.st_size, opened.st_mtime_ns, opened.st_ctime_ns) == (after.st_size, after.st_mtime_ns, after.st_ctime_ns) and size == opened.st_size, "File changed while hashing")
    finally:
        os.close(fd)
    return {"sha256": h.hexdigest(), "sizeBytes": size}


def range_hash(path, start, size, budget):
    h = hashlib.sha256()
    with path.open("rb") as stream:
        stream.seek(start)
        while size:
            budget.check()
            block = stream.read(min(size, 65536))
            need(block, "Truncated tensor extent")
            h.update(block)
            size -= len(block)
    return h.hexdigest()


def runtime_inventory(lock):
    # Import only pure GGUF codecs plus NumPy. Never import GNE, Torch, llama.cpp.
    import numpy as np
    import gguf
    packages = []
    total = 0
    for name in ("numpy", "gguf"):
        distribution = importlib.metadata.distribution(name)
        files = []
        for item in sorted(distribution.files or (), key=str):
            text = str(item)
            if "__pycache__" in text or text.endswith((".pyc", ".pyo")):
                continue
            # Complete package, bundled numeric libraries and distribution pins.
            if not (text.startswith(name + "/") or text.startswith(name + ".libs/") or ".dist-info/" in text):
                continue
            path = Path(distribution.locate_file(item)).resolve(strict=True)
            info = regular(path)
            total += info.st_size
            need(total <= 512 * 1024 ** 2 and len(files) < 50000, "Runtime dependency inventory bound exceeded")
            files.append({"path": text, **hash_file(path)})
        need(files, "Missing package runtime inventory")
        packages.append({"name": name, "version": distribution.version, "files": files})
    need(importlib.metadata.version("gguf") == "0.19.0", "Unsupported GGUF codec version")
    need(not any(n.startswith(("torch", "llama_cpp", "gne.runtime")) for n in sys.modules), "Native/model runtime unexpectedly imported")
    body = {"schema": "omnius.nol.gguf-editor-runtime.v1", "pythonVersion": platform.python_version(), "pythonExecutable": hash_file(Path(sys.executable).resolve(strict=True)), "packages": packages, "dependencyLock": hash_file(Path(lock))}
    return {**body, "runtimeDigest": digest(encoded(body))}


class Scanner:
    def __init__(self, path, budget):
        from gguf.constants import GGML_QUANT_SIZES, GGMLQuantizationType
        self.path, self.budget = path, budget
        self.size = regular(path).st_size
        self.stream = path.open("rb")
        self.off, self.arrays = 0, 0
        self.keys, self.tensors = {}, []
        try:
            need(self.read(4) == b"GGUF", "Bad GGUF magic")
            self.version = self.u("I")
            need(self.version == 3, "Only little-endian GGUF v3 is supported")
            tensors, kv = self.u("Q"), self.u("Q")
            need(0 < tensors <= budget.limits["maxTensors"] and kv <= budget.limits["maxKeyValues"], "GGUF directory count bound exceeded")
            for _ in range(kv):
                key = self.string()
                need(key and key not in self.keys and not key.startswith("split."), "Duplicate/empty key or split GGUF unsupported")
                kind = self.u("I")
                value = self.value(kind)
                self.keys[key] = (kind, value)
            self.metadataEnd = self.off
            alignment = self.keys.get("general.alignment", (4, 32))
            need(alignment[0] == 4 and type(alignment[1]) is int and 0 < alignment[1] <= 1048576 and alignment[1] & (alignment[1] - 1) == 0, "Invalid GGUF alignment")
            self.alignment = alignment[1]
            need(self.keys.get("general.architecture", (None, None))[0] == 8, "Missing architecture metadata")
            names = set()
            for _ in range(tensors):
                name = self.string()
                need(name and len(name.encode()) <= 4096 and name not in names, "Duplicate/unsafe tensor name")
                names.add(name)
                ndim = self.u("I")
                need(1 <= ndim <= 4, "Unsupported GGUF tensor rank")
                ne = [integer(self.u("Q"), 1) for _ in range(ndim)]
                elements = math.prod(ne)
                integer(elements, 1)
                typeOffset = self.off
                rawType = self.u("I")
                try:
                    qtype = GGMLQuantizationType(rawType)
                    block, width = GGML_QUANT_SIZES[qtype]
                except (ValueError, KeyError):
                    raise ValueError("Unknown GGUF storage block type")
                need(ne[0] % block == 0, "GGUF row is not aligned to its storage block")
                offsetField = self.off
                offset = self.u("Q")
                size = elements // block * width
                need(offset % self.alignment == 0 and size > 0, "Unaligned/empty GGUF tensor")
                self.tensors.append({"name": name, "ne": ne, "shape": list(reversed(ne)), "qtype": qtype.name, "typeId": rawType, "typeOffset": typeOffset, "offsetField": offsetField, "relativeOffset": offset, "sizeBytes": size, "elements": elements})
            self.directoryEnd = self.off
            self.dataStart = (self.off + self.alignment - 1) // self.alignment * self.alignment
            need(self.dataStart <= budget.limits["maxMetadataBytes"] and self.dataStart <= self.size, "GGUF header byte bound exceeded")
            previous = self.dataStart
            for tensor in sorted(self.tensors, key=lambda t: t["relativeOffset"]):
                start = self.dataStart + tensor["relativeOffset"]
                need(start >= previous and start + tensor["sizeBytes"] <= self.size, "Overlapping/aliased/truncated tensor extent")
                tensor["dataOffset"] = start
                previous = start + tensor["sizeBytes"]
            self.byName = {t["name"]: t for t in self.tensors}
            # Account independent official reader's bounded metadata object graph.
            budget.reserve(self.dataStart * 4 + self.arrays * 1024 + len(self.tensors) * 2048)
        finally:
            self.stream.close()

    def read(self, size):
        self.budget.check()
        need(type(size) is int and size >= 0 and self.off + size <= min(self.size, self.budget.limits["maxMetadataBytes"]), "GGUF header extent exceeds bounds")
        raw = self.stream.read(size)
        need(len(raw) == size, "Truncated GGUF directory")
        self.off += size
        return raw

    def u(self, fmt):
        return struct.unpack("<" + fmt, self.read(struct.calcsize("<" + fmt)))[0]

    def string(self):
        size = self.u("Q")
        need(size <= 1048576, "GGUF string bound exceeded")
        return self.read(size).decode("utf-8", "strict")

    def value(self, kind):
        scalar = {0: "B", 1: "b", 2: "H", 3: "h", 4: "I", 5: "i", 6: "f", 7: "B", 10: "Q", 11: "q", 12: "d"}
        if kind == 8:
            return self.string()
        if kind in scalar:
            value = self.u(scalar[kind])
            need(kind != 7 or value in (0, 1), "Invalid GGUF boolean")
            return value
        need(kind == 9, "Unknown metadata type")
        subtype, count = self.u("I"), self.u("Q")
        self.arrays += count
        need(self.arrays <= self.budget.limits["maxArrayElements"] and subtype in set(scalar) | {8}, "Nested/oversized metadata array unsupported")
        # Array values are preserved as raw bytes; do not build vocabulary views.
        if subtype == 8:
            for _ in range(count):
                self.string()
        else:
            remaining = count * struct.calcsize("<" + scalar[subtype])
            while remaining:
                size = min(remaining, 65536)
                self.read(size)
                remaining -= size
        return None


def verify_official(path, scanned, budget):
    from gguf import GGUFReader
    budget.check()
    reader = GGUFReader(path, mode="r")
    need(len(reader.tensors) == len(scanned.tensors) and reader.data_offset == scanned.dataStart, "Official GGUF reader directory differs")
    for actual, expected in zip(reader.tensors, scanned.tensors):
        need(actual.name == expected["name"] and list(map(int, actual.shape)) == expected["ne"] and actual.tensor_type.name == expected["qtype"] and actual.n_bytes == expected["sizeBytes"] and actual.data_offset == expected["dataOffset"], "Official GGUF geometry/type/payload extent differs")
    del reader
    budget.check()


def verified_file(path, ref, budget, operand=False):
    fields(ref, ("uri", "sha256", "sizeBytes"), ("mediaType",))
    need(re.fullmatch("[a-f0-9]{64}", ref["sha256"]) and ref["uri"] == "omnius-artifact://sha256/" + ref["sha256"], "Bad full artifact identity")
    integer(ref["sizeBytes"])
    need(regular(path).st_size == ref["sizeBytes"], "Declared input file size changed")
    seen = budget.artifactHashes if operand else budget.inputHashes
    if ref["sha256"] not in seen:
        need(ref["sizeBytes"] + (budget.artifacts if operand else budget.input) <= budget.limits["maxArtifactBytes" if operand else "maxInputBytes"], "Input/operand byte admission exceeded")
    actual = hash_file(path, budget)
    need(actual == {"sha256": ref["sha256"], "sizeBytes": ref["sizeBytes"]}, "Full source/operand artifact changed")
    if ref["sha256"] not in seen:
        if operand:
            budget.artifacts += actual["sizeBytes"]
            need(budget.artifacts <= budget.limits["maxArtifactBytes"], "Aggregate operand byte budget exceeded")
        else:
            budget.input += actual["sizeBytes"]
            need(budget.input <= budget.limits["maxInputBytes"], "Aggregate parent checkpoint byte budget exceeded")
        seen.add(ref["sha256"])
    budget.verified[str(path)] = ref["sha256"]
    return actual


def logical_dtype(qtype):
    return {"F32": "f32", "F16": "f16", "BF16": "bf16", "F64": "f64", "I8": "i8", "I16": "i16", "I32": "i32", "I64": "i64"}.get(qtype, "quantized")


def dense_site(site, tensor):
    need(site["kind"] == "parameter" and site["shape"] == tensor["shape"] and site["dtype"] == logical_dtype(tensor["qtype"]) and tensor["qtype"] in SUPPORTED, "Frozen parameter shape/storage type mismatch")
    name, role, rank = site["name"], site["role"], len(site["shape"])
    if name in ("token_embd.weight", "output.weight"):
        need(rank == 2 and role in ("embedding", "other"), "Unverified dense output/embedding role")
    elif re.fullmatch(r"blk\.\d+\.ffn_(up|gate|down)\.weight", name):
        need(rank == 2 and role == "ffn", "Unverified dense FFN geometry")
    elif re.fullmatch(r"blk\.\d+\.attn_(q|k|v|qkv|output)\.weight", name):
        need(rank == 2 and role == "attention", "Unverified dense attention geometry")
    elif name == "output_norm.weight" or re.fullmatch(r"blk\.\d+\.(attn_norm|ffn_norm|attn_q_norm|attn_k_norm)\.weight", name):
        need(rank == 1 and role == "norm", "Unverified normalization parameter")
    elif re.fullmatch(r"blk\.\d+\.(ffn_(up|gate|down)|attn_(q|k|v|qkv|output))\.bias", name):
        need(rank == 1 and role == "bias", "Unverified dense bias geometry")
    else:
        raise ValueError("Unsupported/unverified dense GGUF parameter name; MoE/router semantics are not inferred")


class Checkpoint:
    def __init__(self, value, root, identity, budget):
        fields(identity, ("modelJson", "tensorSitesJson"))
        body = {k: v for k, v in value.items() if k not in ("id", "modelDigest")}
        need(same(parse(identity["modelJson"]), body) and digest(identity["modelJson"].encode()) == value["modelDigest"], "Canonical full model identity mismatch")
        need(same(parse(identity["tensorSitesJson"]), value["tensorSites"]) and digest(identity["tensorSitesJson"].encode()) == value["architectureMapDigest"], "Canonical tensor inventory identity mismatch")
        need(value["format"] == "gguf", "Expected one single-file GGUF parent")
        self.model, self.root, self.budget = value, root, budget
        self.files = {}
        for f in value["files"]:
            path = safe(root, f["path"])
            verified_file(path, f["artifact"], budget)
            self.files[f["path"]] = path
        weights = [f for f in value["files"] if f["role"] == "weights"]
        need(len(weights) == 1, "Split/multiple GGUF weight files unsupported")
        self.name = weights[0]["path"]
        self.path = self.files[self.name]
        self.scan = Scanner(self.path, budget)
        self.sites = {s["id"]: s for s in value["tensorSites"]}
        for site in value["tensorSites"]:
            if site["kind"] == "parameter":
                tensor = self.scan.byName.get(site["name"])
                need(tensor is not None and tensor["shape"] == site["shape"] and logical_dtype(tensor["qtype"]) == site["dtype"], "Declared parameter does not match actual GGUF inventory")

    def tensor(self, site):
        import numpy as np
        from gguf import quants, GGMLQuantizationType
        tensor = self.scan.byName[site["name"]]
        dense_site(site, tensor)
        self.budget.reserve(tensor["elements"] * 128 + tensor["sizeBytes"] * 3)
        with self.path.open("rb") as stream:
            stream.seek(tensor["dataOffset"])
            raw = stream.read(tensor["sizeBytes"])
        need(len(raw) == tensor["sizeBytes"], "Truncated GGUF source tensor")
        if tensor["qtype"] in ("F32", "F16"):
            data = np.frombuffer(raw, dtype="<f4" if tensor["qtype"] == "F32" else "<f2").reshape(tensor["shape"]).astype(np.float64)
        else:
            qt = GGMLQuantizationType[tensor["qtype"]]
            from gguf.quants import quant_shape_to_byte_shape
            data = quants.dequantize(np.frombuffer(raw, dtype=np.uint8).reshape(quant_shape_to_byte_shape(tensor["shape"], qt)), qt).astype(np.float64)
        need(bool(np.isfinite(data).all()), "Source tensor contains nonfinite decoded values")
        return data


class Editor:
    def __init__(self, payload, budget, sources):
        self.payload, self.budget, self.sources = payload, budget, sources
        self.operands = {}
        # Validate every declared operand before any semantic-neutral fast path.
        for op in payload["recipe"]["operations"]:
            refs = []
            if op["operation"] == "WEIGHT_ADD" and op["delta"]["kind"] == "tensor": refs.append(op["delta"]["tensor"])
            if op["operation"] == "WEIGHT_GAIN" and op["gain"]["kind"] == "axis": refs.append(op["gain"]["factors"])
            if op["operation"] == "WEIGHT_REPLACE": refs.append(op["replacement"])
            if op["operation"] == "WEIGHT_PROJECT": refs.append(op["basis"])
            if op["operation"] == "LOW_RANK_UPDATE": refs += [op["a"], op["b"]]
            for ref in refs:
                self.operand(ref)

    def operand(self, ref):
        import numpy as np
        fields(ref, ("artifact", "shape", "dtype"))
        shape, dtype, artifact = ref["shape"], ref["dtype"], ref["artifact"]
        need(type(shape) is list and 1 <= len(shape) <= 4 and dtype in set(FLOATS) | {"bf16"}, "Unsupported floating operand shape/type")
        n = math.prod(integer(x, 1) for x in shape)
        integer(n, 1)
        key = digest(encoded(ref))
        if key in self.operands:
            return self.operands[key]
        path = safe(Path(self.payload["artifactRoot"]), artifact["sha256"][:2] + "/" + artifact["sha256"])
        verified_file(path, artifact, self.budget, True)
        self.budget.reserve(artifact["sizeBytes"] * 3 + n * 32)
        raw = path.read_bytes()
        if artifact.get("mediaType") == "application/json":
            value = parse(raw)
            fields(value, ("schema", "shape", "dtype", "data"))
            need(value["schema"] == "omnius.nol.tensor.v1" and value["shape"] == shape and value["dtype"] == dtype and type(value["data"]) is list and len(value["data"]) == n, "Operand JSON descriptor mismatch")
            data = np.array([finite(x) for x in value["data"]], dtype="<f4" if dtype == "bf16" else FLOATS[dtype])
            if dtype == "bf16":
                bits = data.view(np.uint32)
                need(bool((bits & 65535 == 0).all()), "JSON BF16 operand is not exactly representable")
        else:
            width = 2 if dtype == "bf16" else np.dtype(FLOATS[dtype]).itemsize
            need(len(raw) == n * width, "Binary operand byte length differs from descriptor")
            data = ((np.frombuffer(raw, dtype="<u2").astype(np.uint32) << 16).view(np.float32) if dtype == "bf16" else np.frombuffer(raw, dtype=FLOATS[dtype]))
        need(bool(np.isfinite(data).all()), "Nonfinite/overflowed operand")
        result = data.reshape(shape).astype(np.float64)
        self.operands[key] = result
        return result

    def apply(self, op, current, site, baseline_tensor):
        import numpy as np
        kind, shape, n = op["operation"], list(current.shape), current.size
        self.lastIdentity = False
        need(kind in OPS and op["tokenScope"] == {"kind": "all"}, "Only all-token static parameter edits are supported")
        self.budget.reserve(n * 96)
        if kind == "WEIGHT_ADD":
            delta = op["delta"]
            value = finite(delta["value"]) if delta["kind"] == "scalar" else self.operand(delta["tensor"])
            need(isinstance(value, float) or list(value.shape) == shape, "Addition operand geometry mismatch")
            self.budget.work(n)
            if (isinstance(value, float) and value == 0) or (not isinstance(value, float) and bool((value == 0).all())):
                self.lastIdentity = True; return current
            result = np.where(value == 0, current, current + value)
        elif kind == "WEIGHT_GAIN":
            gain = op["gain"]
            if gain["kind"] == "scalar":
                value = finite(gain["factor"])
                if value == 1: self.lastIdentity = True; return current
            else:
                axis = integer(gain["axis"])
                need(axis < len(shape), "Axis gain outside parameter")
                value = self.operand(gain["factors"])
                need(list(value.shape) == [shape[axis]], "Axis gain vector geometry mismatch")
                view = [1] * len(shape); view[axis] = shape[axis]; value = value.reshape(view)
                if bool((value == 1).all()): self.lastIdentity = True; return current
            self.budget.work(n)
            result = np.where(value == 1, current, current * value)
        elif kind == "WEIGHT_REPLACE":
            expected = "f32" if site["dtype"] == "quantized" else site["dtype"]
            need(op["replacement"]["dtype"] == expected, "Replacement must use the source parameter logical dtype")
            result = self.operand(op["replacement"])
            need(list(result.shape) == shape, "Replacement geometry mismatch")
            self.lastIdentity = bool(np.array_equal(result.view(np.uint64), current.view(np.uint64)))
            self.budget.work(n)
            result = result.copy()
        elif kind == "WEIGHT_PROJECT":
            need(len(shape) == 2 and op["normalization"] == "orthonormal" and op["side"] in ("left", "right"), "Projection requires an explicit orthonormal row basis")
            basis = self.operand(op["basis"])
            if basis.ndim == 1: basis = basis.reshape(1, -1)
            dimension = shape[0 if op["side"] == "left" else 1]
            need(basis.ndim == 2 and basis.shape[1] == dimension and 0 < basis.shape[0] <= dimension, "Projection basis geometry mismatch")
            rank = basis.shape[0]
            self.budget.reserve(rank * rank * 32)
            self.budget.work((2 * dimension + 1) * rank * rank)
            need(float(np.max(np.abs(basis @ basis.T - np.eye(rank)))) <= self.payload["orthogonalityTolerance"], "Projection basis is not orthonormal")
            strength = finite(op["strength"])
            if strength == 0: self.lastIdentity = True; return current
            self.budget.work(5 * n * rank)
            projected = basis.T @ (basis @ current) if op["side"] == "left" else (current @ basis.T) @ basis
            result = np.where(projected == 0, current, current - strength * projected)
        elif kind == "LOW_RANK_UPDATE":
            rank = integer(op["rank"], 1)
            a, b = self.operand(op["a"]), self.operand(op["b"])
            need(len(shape) == 2 and op["scaling"] == "alpha_over_rank" and list(a.shape) == [rank, shape[1]] and list(b.shape) == [shape[0], rank], "Low-rank A/B geometry/scaling mismatch")
            factor = finite(finite(op["scale"]) * finite(op["alpha"]) / rank)
            if finite(op["scale"]) == 0 or finite(op["alpha"]) == 0:
                self.lastIdentity = True; return current
            if factor == 0: return current
            self.budget.work(2 * n * rank + 2 * n)
            delta = b @ a
            result = np.where(delta == 0, current, current + factor * delta)
        else:
            terms, seen = [], set()
            for source in op["sources"]:
                key = (source["modelDigest"], source["siteId"])
                need(key not in seen and source["modelDigest"] in self.payload["recipe"]["parentModelDigests"], "Duplicate/undeclared merge source")
                seen.add(key)
                parent = self.sources.get(source["modelDigest"])
                source_site = parent.sites.get(source["siteId"]) if parent else None
                need(source_site and source_site["kind"] == "parameter" and source_site["name"] == site["name"] and source_site["shape"] == shape and source_site["dtype"] == site["dtype"], "Full merge parameter name/shape/dtype mismatch")
                source_tensor = parent.scan.byName[source_site["name"]]
                need(source_tensor["qtype"] == baseline_tensor["qtype"], "Merge source physical GGUF type differs")
                weight = finite(source["weight"])
                # Even zero-weight parents have already been full-hash verified.
                value = parent.tensor(source_site)
                if weight != 0: terms.append((value, weight))
            self.budget.work(2 * n * len(terms))
            if len(terms) == 1 and terms[0][1] == 1:
                result = terms[0][0].copy()
                self.lastIdentity = bool(np.array_equal(result.view(np.uint64), current.view(np.uint64)))
            else:
                result = np.zeros(shape, dtype=np.float64)
                for value, weight in terms: result = result + weight * value
        need(bool(np.isfinite(result).all()), "Nonfinite float64 operation result")
        return result


def l2(value):
    import numpy as np
    maximum = float(np.max(np.abs(value)))
    if maximum == 0: return 0.0
    result = maximum * math.sqrt(float(np.sum((value / maximum) ** 2, dtype=np.float64)))
    need(math.isfinite(result), "Norm overflow beyond supported numeric range")
    return result


def independent_decode(raw, shape, target):
    import numpy as np
    if target == "F16":
        return np.frombuffer(raw, dtype="<f2").astype(np.float64).reshape(shape)
    # Independent Q8_0 wire decoder: one IEEE-f16 scale + 32 signed bytes.
    need(shape[-1] % 32 == 0 and len(raw) == math.prod(shape) // 32 * 34, "Q8_0 encoded extent mismatch")
    blocks = np.frombuffer(raw, dtype=np.dtype([("scale", "<f2"), ("values", "i1", (32,))]))
    return (blocks["scale"].astype(np.float64)[:, None] * blocks["values"].astype(np.float64)).reshape(shape)


def encode_tensor(value, policy, budget):
    import numpy as np
    from gguf import quants, GGMLQuantizationType
    target = "F16" if policy["target"] == "f16" else "Q8_0"
    budget.work(value.size * 8)
    if target == "F16":
        with np.errstate(over="ignore", invalid="ignore"):
            raw = np.ascontiguousarray(value, dtype="<f2").tobytes()
    else:
        need(value.ndim == 2 and value.shape[-1] % 32 == 0, "Q8_0 output requires a 2D dense matrix with block-aligned logical last axis")
        with np.errstate(over="ignore", invalid="ignore"):
            f32 = value.astype(np.float32)
        need(bool(np.isfinite(f32).all()), "Q8_0 float32 staging overflow")
        raw = quants.quantize(f32, GGMLQuantizationType.Q8_0).tobytes()
    decoded = independent_decode(raw, value.shape, target)
    need(bool(np.isfinite(decoded).all()), "Storage dtype overflow/nonfinite round trip")
    error = decoded - value
    absolute = float(np.max(np.abs(error)))
    relative = l2(error) / max(l2(value), sys.float_info.min)
    need(math.isfinite(relative) and absolute <= policy["maxAbsError"] and relative <= policy["maxRelativeL2Error"], "Actual storage round-trip error exceeds declared bounds")
    return raw, target, {"maxAbsError": absolute, "relativeL2Error": relative, "changedReferenceElements": int(np.count_nonzero(decoded.view(np.uint64) != value.view(np.uint64)))}


def copy_range(source, destination, start, size, budget):
    source.seek(start)
    while size:
        budget.check()
        block = source.read(min(size, 65536))
        need(block, "Source truncated during stream copy")
        destination.write(block)
        size -= len(block)


def admit_output(stage, additional, budget):
    # Includes private tensor temporaries, not just the final exported files.
    used = sum(p.stat().st_size for p in stage.rglob("*") if p.is_file())
    need(used + additional <= budget.limits["maxOutputBytes"], "Staged output high-water byte budget exceeded")


def write_gguf(base, changes, output, budget):
    scan = base.scan
    admit_output(output.parent, scan.size + sum(c["sizeBytes"] for c in changes.values()) + (scan.alignment * len(scan.tensors) if changes else 0), budget)
    with base.path.open("rb") as source, output.open("xb") as dest:
        if not changes:
            copy_range(source, dest, 0, scan.size, budget)
        else:
            # Preserve the complete raw metadata/header/directory representation;
            # only the exact qtype and relative-offset fields may be rewritten.
            prefix = bytearray(source.read(scan.dataStart))
            dest.write(prefix)
            oldEnd = scan.dataStart
            for tensor in sorted(scan.tensors, key=lambda t: t["dataOffset"]):
                copy_range(source, dest, oldEnd, tensor["dataOffset"] - oldEnd, budget)
                padding = (-(dest.tell() - scan.dataStart)) % scan.alignment
                dest.write(b"\0" * padding)
                relative = dest.tell() - scan.dataStart
                replacement = changes.get(tensor["name"])
                if replacement:
                    with Path(replacement["path"]).open("rb") as rewritten:
                        copy_range(rewritten, dest, 0, replacement["sizeBytes"], budget)
                    struct.pack_into("<I", prefix, tensor["typeOffset"], replacement["typeId"])
                else:
                    copy_range(source, dest, tensor["dataOffset"], tensor["sizeBytes"], budget)
                struct.pack_into("<Q", prefix, tensor["offsetField"], relative)
                oldEnd = tensor["dataOffset"] + tensor["sizeBytes"]
            copy_range(source, dest, oldEnd, scan.size - oldEnd, budget)
            need(dest.tell() <= budget.limits["maxOutputBytes"], "Actual output byte budget exceeded")
            dest.seek(0); dest.write(prefix)
        dest.flush(); os.fsync(dest.fileno())


def edit(payload):
    import numpy as np
    from gguf import GGMLQuantizationType
    fields(payload, ("model", "recipe", "recipeJson", "identity", "sourceCheckpoints", "checkpointRoot", "artifactRoot", "stageDirectory", "outputName", "exportId", "limits", "orthogonalityTolerance", "encoding", "runtime", "dependencyLockPath"))
    budget = Budget(payload["limits"])
    actualRuntime = runtime_inventory(payload["dependencyLockPath"])
    need(same(actualRuntime, payload["runtime"]), "Pinned full GGUF/NumPy/runtime dependency inventory changed")
    policy = payload["encoding"]
    fields(policy, ("target", "maxAbsError", "maxRelativeL2Error"))
    need(policy["target"] in ("f16", "q8_0") and finite(policy["maxAbsError"]) >= 0 and finite(policy["maxRelativeL2Error"]) >= 0, "Explicit finite output quantization/error policy required")
    need(0 < finite(payload["orthogonalityTolerance"]) <= 0.01, "Invalid orthogonality tolerance")
    recipe = payload["recipe"]
    recipeBody = {k: v for k, v in recipe.items() if k != "recipeDigest"}
    need(same(parse(payload["recipeJson"]), recipeBody) and digest(payload["recipeJson"].encode()) == recipe["recipeDigest"], "Full canonical recipe identity mismatch")
    need(recipe["calibrations"] == [] and 0 < len(recipe["operations"]) <= budget.limits["maxOperations"] and all(o["operation"] in OPS and o["tokenScope"] == {"kind": "all"} for o in recipe["operations"]), "Unsupported recipe operations/calibrations/scope")
    stage = Path(payload["stageDirectory"])
    need(stage.is_dir() and not stage.is_symlink() and not list(stage.iterdir()), "Private empty stage required")
    base = Checkpoint(payload["model"], Path(payload["checkpointRoot"]), payload["identity"], budget)
    need(recipe["modelDigest"] == base.model["modelDigest"] and recipe["architectureMapDigest"] == base.model["architectureMapDigest"], "Recipe/base parameter identity mismatch")
    sources = {base.model["modelDigest"]: base}
    for source in payload["sourceCheckpoints"]:
        fields(source, ("model", "root", "identity"))
        parent = Checkpoint(source["model"], Path(source["root"]), source["identity"], budget)
        key = parent.model["modelDigest"]
        need(key in recipe["parentModelDigests"] and key not in sources, "Duplicate/undeclared full merge parent")
        sources[key] = parent
    need(set(sources) == set(recipe["parentModelDigests"]), "Every declared parent must be completely supplied and verified")
    editor = Editor(payload, budget, sources)
    grouped = {}
    for op in recipe["operations"]:
        site = base.sites.get(op["siteId"])
        need(site and site["kind"] == "parameter", "Unknown static parameter target")
        tensor = base.scan.byName.get(site["name"])
        need(tensor is not None, "Missing actual target tensor")
        dense_site(site, tensor)
        grouped.setdefault(site["name"], (site, []))[1].append(op)
    changes, operations, final = {}, [], []
    for name, (site, ops) in grouped.items():
        tensor = base.scan.byName[name]
        original = base.tensor(site)
        value = original
        explicitIdentity = True
        for op in ops:
            before = value
            value = editor.apply(op, value, site, tensor)
            explicitIdentity = explicitIdentity and editor.lastIdentity
            changed = int(np.count_nonzero(value.view(np.uint64) != before.view(np.uint64)))
            operations.append({"operationId": op["id"], "operation": op["operation"], "siteId": op["siteId"], "name": name, "changedReferenceElements": changed, "operatorOrder": next(i for i, declared in enumerate(recipe["operations"]) if declared["id"] == op["id"]), "evaluationOrder": len(operations), "beforeReferenceSha256": digest(before.astype("<f8", copy=False).tobytes()), "afterReferenceSha256": digest(value.astype("<f8", copy=False).tobytes()), "referencePrecision": "float64", "storageEncodePerformed": False})
        neutral = explicitIdentity and bool(np.array_equal(value.view(np.uint64), original.view(np.uint64)))
        beforeHash = range_hash(base.path, tensor["dataOffset"], tensor["sizeBytes"], budget)
        if neutral:
            final.append({"name": name, "from": tensor["qtype"], "to": tensor["qtype"], "neutral": True, "beforeTensorSha256": beforeHash, "afterTensorSha256": beforeHash, "maxAbsError": 0.0, "relativeL2Error": 0.0})
        else:
            raw, target, error = encode_tensor(value, policy, budget)
            path = stage / (".tensor-" + str(len(changes)))
            admit_output(stage, len(raw), budget)
            with path.open("xb") as stream:
                stream.write(raw); stream.flush(); os.fsync(stream.fileno())
            changes[name] = {"path": str(path), "typeId": int(GGMLQuantizationType[target]), "sizeBytes": len(raw)}
            final.append({"name": name, "from": tensor["qtype"], "to": target, "neutral": False, "beforeTensorSha256": beforeHash, "afterTensorSha256": digest(raw), **error})
    output = stage / payload["outputName"]
    need(output.parent == stage and output.name not in ("", ".", "..") and not output.name.startswith("."), "Unsafe output GGUF filename")
    write_gguf(base, changes, output, budget)
    check = Scanner(output, budget)
    need(range_hash(base.path, 24, base.scan.metadataEnd - 24, budget) == range_hash(output, 24, check.metadataEnd - 24, budget), "Raw metadata representation changed")
    need([t["name"] for t in check.tensors] == [t["name"] for t in base.scan.tensors], "Tensor inventory order changed")
    unchanged = []
    for tensor in base.scan.tensors:
        actual = check.byName[tensor["name"]]
        need(actual["shape"] == tensor["shape"], "Exported logical geometry changed")
        expected = next((item for item in final if item["name"] == tensor["name"]), None)
        if tensor["name"] not in changes:
            need(actual["qtype"] == tensor["qtype"] and range_hash(base.path, tensor["dataOffset"], tensor["sizeBytes"], budget) == range_hash(output, actual["dataOffset"], actual["sizeBytes"], budget), "Untouched tensor bytes/storage type changed")
            unchanged.append({"name": tensor["name"], "sha256": range_hash(output, actual["dataOffset"], actual["sizeBytes"], budget)})
        else:
            need(actual["qtype"] == expected["to"] and range_hash(output, actual["dataOffset"], actual["sizeBytes"], budget) == expected["afterTensorSha256"], "Stored edited tensor differs from independently checked encoding")
    verify_official(output, check, budget)
    for item in changes.values(): Path(item["path"]).unlink()
    files = []
    for f in base.model["files"]:
        source = base.files[f["path"]]
        if f["role"] == "weights": destination = output
        else:
            destination = stage / f["path"]
            destination.parent.mkdir(parents=True, exist_ok=True)
            admit_output(stage, regular(source).st_size, budget)
            with source.open("rb") as s, destination.open("xb") as d:
                copy_range(s, d, 0, regular(source).st_size, budget); d.flush(); os.fsync(d.fileno())
        files.append({"path": payload["outputName"] if f["role"] == "weights" else f["path"], "role": f["role"], "before": hash_file(source, budget), "after": hash_file(destination, budget)})
    # Recheck all declared parents and operands after math/copy and before commit.
    for parent in sources.values():
        for f in parent.model["files"]: verified_file(parent.files[f["path"]], f["artifact"], budget)
    for path, ref in list(budget.verified.items()):
        need(hash_file(Path(path), budget)["sha256"] == ref, "Declared input changed before publication")
    runtimePath = ".omnius-nol-gguf-runtime." + payload["exportId"] + ".json"
    runtimeFile = stage / runtimePath
    runtimeRaw = encoded(actualRuntime)
    admit_output(stage, len(runtimeRaw), budget)
    runtimeFile.write_bytes(runtimeRaw)
    with runtimeFile.open("rb") as stream: os.fsync(stream.fileno())
    runtimeInfo = hash_file(runtimeFile, budget)
    runtimeArtifact = {**runtimeInfo, "uri": "omnius-artifact://sha256/" + runtimeInfo["sha256"], "mediaType": "application/json"}
    operations.sort(key=lambda op: op["operatorOrder"])
    result = {"schema": "omnius.nol.gguf-checkpoint-edit.v1", "exportId": payload["exportId"], "editorSha256": hash_file(Path(__file__).resolve(strict=True), budget)["sha256"], "runtimePath": runtimePath, "runtimeArtifact": runtimeArtifact, "parentModelDigest": base.model["modelDigest"], "recipeDigest": recipe["recipeDigest"], "sourceModelDigests": sorted(sources), "runtimeDigest": actualRuntime["runtimeDigest"], "numericalPolicy": "declared_operator_order_float64_reference_then_one_final_storage_encode_per_changed_tensor; no_per_operation_storage_rounding; native_blas_reassociation_possible", "equivalence": "approximate_transfer_needs_native_validation", "neutralPolicy": "explicit_zero_add_unit_gain_zero_projection_or_scale_bitwise_identity_replace_merge_only_preserves_original_bytes", "allNeutral": not changes, "encoding": policy, "operations": operations, "tensors": final, "unchangedTensors": unchanged, "metadataSha256": range_hash(output, 24, check.metadataEnd - 24, budget), "officialReaderVerified": True, "files": files, "inferencePerformed": False, "cudaUsed": False, "nativeRuntimeImported": False, "usage": {"inputBytes": budget.input, "artifactBytes": budget.artifacts, "reservedWorkingBytes": budget.working, "arithmeticUnits": budget.arithmetic, "durationMs": int((time.monotonic() - budget.started) * 1000)}}
    receipt = stage / (".omnius-nol-gguf-edit." + payload["exportId"] + ".json")
    result["usage"]["outputBytes"] = 0
    otherBytes = sum(p.stat().st_size for p in stage.rglob("*") if p.is_file())
    for _ in range(8):
        size = otherBytes + len(encoded(result))
        if result["usage"]["outputBytes"] == size: break
        result["usage"]["outputBytes"] = size
    admit_output(stage, len(encoded(result)), budget)
    receipt.write_bytes(encoded(result))
    with receipt.open("rb") as stream: os.fsync(stream.fileno())
    size = sum(p.stat().st_size for p in stage.rglob("*") if p.is_file())
    need(size <= budget.limits["maxOutputBytes"], "Complete export/receipt output budget exceeded")
    need(result["usage"]["outputBytes"] == size, "Receipt/output byte accounting did not converge")
    need(not any(n.startswith(("torch", "llama_cpp", "gne.runtime")) for n in sys.modules), "Native runtime unexpectedly imported")
    return {"receipt": result, "receiptPath": receipt.name, "receiptArtifact": hash_file(receipt, budget)}


def publish(payload):
    fields(payload, ("stageDirectory", "outputDirectory", "exportId", "receiptSha256", "receiptPath"))
    need(sys.platform.startswith("linux"), "Atomic no-replace publication requires Linux")
    stage, target = Path(payload["stageDirectory"]), Path(payload["outputDirectory"])
    need(stage.is_dir() and not stage.is_symlink() and stage.parent.resolve(strict=True) == target.parent.resolve(strict=True), "Publish stage/parent mismatch")
    need(payload["receiptPath"] == ".omnius-nol-gguf-edit." + payload["exportId"] + ".json", "Publish receipt identity mismatch")
    receipt = safe(stage, payload["receiptPath"])
    need(hash_file(receipt)["sha256"] == payload["receiptSha256"] and parse(receipt.read_bytes())["exportId"] == payload["exportId"], "Publish receipt content changed")
    libc = ctypes.CDLL(None, use_errno=True)
    rename = getattr(libc, "renameat2", None)
    need(rename is not None, "No atomic no-replace rename support")
    rename.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint]; rename.restype = ctypes.c_int
    if rename(-100, os.fsencode(stage), -100, os.fsencode(target), 1) != 0:
        code = ctypes.get_errno(); raise OSError(code, os.strerror(code))
    fd = os.open(target.parent, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0))
    try: os.fsync(fd)
    finally: os.close(fd)
    return {"published": True, "exportId": payload["exportId"]}


if __name__ == "__main__":
    try:
        signal.signal(signal.SIGTERM, lambda *_: (_ for _ in ()).throw(InterruptedError("GGUF editor cancelled")))
        raw = sys.stdin.buffer.read(MAX_FRAME + 1)
        need(len(raw) <= MAX_FRAME, "Absolute protocol bound exceeded")
        payload = parse(raw)
        if sys.argv[1:] == ["--inspect-runtime"]:
            fields(payload, ("dependencyLockPath",)); result = runtime_inventory(payload["dependencyLockPath"])
        elif sys.argv[1:] == ["--inspect"]:
            fields(payload, ("checkpointPath", "limits", "runtime", "dependencyLockPath")); budget = Budget(payload["limits"])
            actualRuntime = runtime_inventory(payload["dependencyLockPath"])
            need(same(actualRuntime, payload["runtime"]), "Pinned GGUF inventory runtime changed")
            checkpoint = Path(payload["checkpointPath"])
            need(regular(checkpoint).st_size <= budget.limits["maxInputBytes"], "Checkpoint byte admission exceeded")
            info = hash_file(checkpoint, budget); need(info["sizeBytes"] <= budget.limits["maxInputBytes"], "Checkpoint input bound exceeded")
            scanned = Scanner(checkpoint, budget); verify_official(checkpoint, scanned, budget)
            result = {"schema": "omnius.nol.gguf-inventory.v1", "runtimeDigest": actualRuntime["runtimeDigest"], "checkpoint": info, "version": scanned.version, "alignment": scanned.alignment, "metadataSha256": range_hash(checkpoint, 24, scanned.metadataEnd - 24, budget), "tensors": [{k: t[k] for k in ("name", "shape", "qtype", "sizeBytes", "dataOffset")} for t in scanned.tensors], "officialReaderVerified": True, "modelLoaded": False, "cudaUsed": False}
        elif sys.argv[1:] == ["--publish"]: result = publish(payload)
        else:
            need(not sys.argv[1:], "Unknown GGUF editor action"); result = edit(payload)
        output = encoded(result); need(len(output) <= MAX_FRAME, "Output protocol frame bound exceeded")
        sys.stdout.buffer.write(output); sys.stdout.flush()
    except BaseException as error:
        sys.stderr.write((type(error).__name__ + ": " + str(error))[-4000:] + "; nativeRuntimeImported=" + str(any(n.startswith(("torch", "llama_cpp", "gne.runtime")) for n in sys.modules)).lower() + "\n")
        sys.exit(1)
