#!/usr/bin/env python3
"""Offline NOL parameter editor. No model construction, tokenization or CUDA.

Imports are stdlib-only until the explicit edit request has passed file/hash,
format and bounded ZIP/header admission. Numerical tensor work is CPU-only.
The separate --publish action is stdlib-only and uses atomic no-replace rename.
"""
import ctypes
import hashlib
import importlib.metadata
import io
import json
import math
import os
from pathlib import Path
import platform
import pickle
from collections import OrderedDict
import signal
import stat
import struct
import sys
import time
import zipfile

SCHEMA = "omnius.nol.checkpoint-edit.v1"
OPS = {"WEIGHT_ADD", "WEIGHT_GAIN", "WEIGHT_REPLACE", "WEIGHT_PROJECT", "LOW_RANK_UPDATE", "WEIGHT_MERGE"}
SIZES = {"BOOL": 1, "I8": 1, "U8": 1, "I16": 2, "U16": 2, "I32": 4, "U32": 4,
         "I64": 8, "U64": 8, "F16": 2, "BF16": 2, "F32": 4, "F64": 8}
ALIASES = {"F16": "f16", "BF16": "bf16", "F32": "f32", "F64": "f64"}


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() and not value.keys() - set(required) - set(optional), "Unknown/missing request fields")


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


def parse(raw):
    return json.loads(raw, object_pairs_hook=unique, 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 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[key], b[key]) for key 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


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


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


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


def shape_size(shape, allow_zero=False):
    need(type(shape) is list and len(shape) <= 8, "Unsupported tensor rank")
    result = 1
    for dimension in shape:
        result *= integer(dimension, 0 if allow_zero else 1)
        integer(result)
    return result


def safe(root, name, exists=True):
    need(type(name) is str and name and "\\" not in name and "\0" not in name and not name.startswith("/"), "Unsafe relative path")
    need(all(part not in ("", ".", "..") for part in name.split("/")), "Unsafe relative path component")
    current = root
    for part in name.split("/"):
        current /= part
        need(not current.is_symlink(), "Symlink input/output refused")
    if exists:
        need(current.is_file(), "Missing regular input file")
    return current


class Budget:
    def __init__(self, limits):
        self.limits = limits
        for value in limits.values():
            integer(value, 1)
        self.deadline = time.monotonic() + limits["maxDurationMs"] / 1000
        self.started = time.monotonic()
        self.working = self.arithmetic = self.input = self.artifacts = 0
        self.reserve(131072)  # two bounded 64 KiB comparison/copy/hash blocks

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

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

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


def hash_file(path, budget=None):
    before = path.lstat()
    need(stat.S_ISREG(before.st_mode) and not path.is_symlink(), "Hash source is not a nonsymlink regular file")
    value, size = hashlib.sha256(), 0
    with path.open("rb") as stream:
        opened = os.fstat(stream.fileno())
        need((opened.st_dev, opened.st_ino) == (before.st_dev, before.st_ino), "Input identity changed")
        while True:
            if budget:
                budget.check()
            block = stream.read(65536)
            if not block:
                break
            size += len(block)
            value.update(block)
        after = os.fstat(stream.fileno())
    need((before.st_size, before.st_mtime_ns, before.st_ctime_ns) == (size, after.st_mtime_ns, after.st_ctime_ns), "Input changed while hashing")
    return {"sha256": value.hexdigest(), "sizeBytes": size}


def verify_identity(raw, expected, sha):
    need(type(raw) is str and digest(raw.encode()) == sha and same(parse(raw), expected), "Canonical identity body/hash mismatch")


def header(path, budget):
    with path.open("rb") as stream:
        raw = stream.read(8)
        need(len(raw) == 8, "Truncated safetensors header")
        size = struct.unpack("<Q", raw)[0]
        need(1 <= size <= min(16 * 1024 * 1024, budget.limits["maxProtocolBytes"] * 16), "Safetensors header budget exceeded")
        budget.reserve(size * 8)
        raw = stream.read(size)
        need(len(raw) == size, "Truncated safetensors header")
    meta = parse(raw)
    need(type(meta) is dict, "Invalid safetensors inventory")
    metadata = meta.get("__metadata__", {})
    need(type(metadata) is dict and all(type(k) is str and type(v) is str for k, v in metadata.items()), "Invalid safetensors metadata")
    intervals = []
    for name, info in meta.items():
        if name == "__metadata__":
            continue
        fields(info, ("dtype", "shape", "data_offsets"))
        need(info["dtype"] in SIZES and type(name) is str and name, "Unsupported safetensors dtype/name")
        need(type(info["data_offsets"]) is list and len(info["data_offsets"]) == 2, "Invalid tensor offsets")
        first, last = [integer(value) for value in info["data_offsets"]]
        need(last >= first and last - first == shape_size(info["shape"], True) * SIZES[info["dtype"]], "Tensor shape/offset byte mismatch")
        intervals.append((first, last))
    cursor = 0
    for first, last in sorted(intervals):
        need(first == cursor, "Safetensors overlapping tensors or data gaps")
        cursor = last
    need(8 + size + cursor == path.stat().st_size, "Safetensors offsets exceed/trail file")
    return {"header": meta, "dataStart": 8 + size, "dataBytes": cursor}


def zip_admission(path, budget):
    need(zipfile.is_zipfile(path), "Legacy/non-ZIP PyTorch checkpoints are unsupported")
    with zipfile.ZipFile(path) as archive:
        entries = archive.infolist()
        need(len(entries) <= 100000 and len({entry.filename for entry in entries}) == len(entries), "Duplicate/oversized PyTorch ZIP inventory")
        total = 0
        for entry in entries:
            need(entry.compress_type == zipfile.ZIP_STORED and not entry.flag_bits & 1, "Compressed/encrypted PyTorch ZIP rejected before allocation")
            need(not entry.filename.startswith("/") and all(part not in ("", ".", "..") for part in entry.filename.split("/")), "Unsafe PyTorch ZIP entry")
            total += integer(entry.file_size)
            need(total <= budget.limits["maxWorkingBytes"], "PyTorch declared storage exceeds working byte budget")
            if entry.filename.endswith("/data.pkl"):
                need(entry.file_size <= budget.limits["maxProtocolBytes"], "PyTorch pickle metadata exceeds protocol cap")
        need(any(entry.filename.endswith("/data.pkl") for entry in entries), "Missing PyTorch tensor-state metadata")
        pickle_entries = [entry for entry in entries if entry.filename.endswith("/data.pkl")]
        need(len(pickle_entries) == 1, "Ambiguous PyTorch pickle root")
        pickle_entry = pickle_entries[0]
        prefix = pickle_entry.filename[:-len("data.pkl")]
        storage_files = {entry.filename[len(prefix + "data/"):]: entry.file_size for entry in entries if entry.filename.startswith(prefix + "data/")}
        budget.reserve(pickle_entry.file_size * 128)
        declared = {}

        class TensorMetadataReader(pickle.Unpickler):
            # No Torch import, builtin eval, model class or arbitrary reducer.
            # This tiny symbolic allowlist validates actual storage extents
            # BEFORE weights_only/mmap can honor attacker-declared sizes.
            def find_class(self, module, name):
                widths = {"ByteStorage": 1, "CharStorage": 1, "BoolStorage": 1, "ShortStorage": 2,
                          "HalfStorage": 2, "BFloat16Storage": 2, "IntStorage": 4, "FloatStorage": 4,
                          "LongStorage": 8, "DoubleStorage": 8}
                if module == "torch" and name in widths:
                    return ("storage_type", widths[name])
                if module == "collections" and name == "OrderedDict":
                    return OrderedDict
                if module == "torch._utils" and name in ("_rebuild_tensor_v2", "_rebuild_tensor"):
                    return self.tensor
                raise ValueError("Unsupported PyTorch metadata reducer before load")

            def persistent_load(self, value):
                need(type(value) is tuple and len(value) == 5 and value[0] == "storage" and
                     type(value[1]) is tuple and len(value[1]) == 2 and value[1][0] == "storage_type" and
                     type(value[2]) is str and type(value[3]) is str, "Unsupported PyTorch storage descriptor")
                width, key, count = value[1][1], value[2], integer(value[4])
                need(key in storage_files and count * width == storage_files[key] and count * width <= budget.limits["maxWorkingBytes"],
                     "PyTorch declared storage extent differs from bounded ZIP payload")
                descriptor = ("storage", key, count, width)
                need(key not in declared or declared[key] == descriptor, "Conflicting PyTorch storage descriptors")
                declared[key] = descriptor
                return descriptor

            @staticmethod
            def tensor(storage, offset, size, stride, requires_grad=False, hooks=None, metadata=None):
                need(type(storage) is tuple and len(storage) == 4 and storage[0] == "storage" and
                     type(size) is tuple and type(stride) is tuple and len(size) == len(stride) <= 8,
                     "Invalid symbolic tensor geometry")
                offset = integer(offset)
                count = shape_size(list(size), True)
                extent = offset + sum(max(0, integer(dimension) - 1) * integer(step) for dimension, step in zip(size, stride))
                need((count == 0 and offset <= storage[2]) or (count > 0 and extent < storage[2]), "PyTorch tensor view exceeds admitted storage")
                need(count * storage[3] <= budget.limits["maxWorkingBytes"] and metadata in (None, {}) and not hooks,
                     "Tensor metadata/logical payload exceeds admission")
                return {"__nol_tensor_descriptor__": True, "elements": count}

        metadata = TensorMetadataReader(io.BytesIO(archive.read(pickle_entry))).load()
        need(isinstance(metadata, dict) and metadata and all(type(key) is str and type(value) is dict and value.get("__nol_tensor_descriptor__") is True for key, value in metadata.items()),
             "PyTorch requires flat tensor-only metadata before load")
        need(set(declared) == set(storage_files), "PyTorch unreferenced/missing storage payload")
    # ZIP storage maps plus a conservative decode/copy allowance, admitted
    # before torch.load; arbitrary optimizer/nested objects are never accepted.
    budget.reserve(total * 3)


class Checkpoint:
    def __init__(self, value, root, identity, budget):
        self.model, self.root, self.budget = value, Path(root).resolve(strict=True), budget
        need(value["schema"] == "omnius.nol.model.v1" and value["format"] in ("hf", "safetensors", "pytorch") and value["quantization"]["method"] == "none", "Unsupported checkpoint model")
        verify_identity(identity["modelJson"], {key: v for key, v in value.items() if key not in ("id", "modelDigest")}, value["modelDigest"])
        verify_identity(identity["tensorSitesJson"], value["tensorSites"], value["architectureMapDigest"])
        self.files = {file["path"]: file for file in value["files"]}
        need(len(self.files) == len(value["files"]), "Duplicate checkpoint path")
        actual = []
        for path in self.root.rglob("*"):
            budget.check()
            need(not path.is_symlink(), "Checkpoint symlink refused")
            if path.is_file():
                actual.append(path.relative_to(self.root).as_posix())
        need(set(actual) == self.files.keys(), "Complete checkpoint file inventory mismatch")
        self.before = {}
        for name, file in self.files.items():
            budget.input += integer(file["artifact"]["sizeBytes"])
            need(budget.input <= budget.limits["maxInputBytes"], "Aggregate checkpoint input bytes exceeded")
            fact = hash_file(safe(self.root, name), budget)
            need(fact == {"sha256": file["artifact"]["sha256"], "sizeBytes": file["artifact"]["sizeBytes"]}, "Complete checkpoint file hash mismatch")
            self.before[name] = fact
        self.weights = [name for name, file in self.files.items() if file["role"] == "weights"]
        need(self.weights, "Missing weight shards")
        kinds = {"safe" if name.endswith(".safetensors") else "torch" if name.endswith((".bin", ".pt", ".pth")) else "unknown" for name in self.weights}
        need(len(kinds) == 1 and "unknown" not in kinds, "Unsupported/mixed native weight formats")
        self.kind = kinds.pop()
        need(value["format"] == "hf" or (value["format"] == "safetensors" and self.kind == "safe") or
             (value["format"] == "pytorch" and self.kind == "torch"), "Declared checkpoint format differs from actual shard codec")
        self.shards, self.locations, self.states = {}, {}, {}
        for name in self.weights:
            if self.kind == "safe":
                self.shards[name] = header(safe(self.root, name), budget)
                for parameter in self.shards[name]["header"]:
                    if parameter == "__metadata__":
                        continue
                    need(parameter not in self.locations, "Duplicate tensor across weight shards")
                    self.locations[parameter] = name
            else:
                zip_admission(safe(self.root, name), budget)
        self.sites = {site["id"]: site for site in value["tensorSites"] if site["kind"] == "parameter"}
        need(len(self.sites) == len([site for site in value["tensorSites"] if site["kind"] == "parameter"]), "Duplicate parameter site ID")

    def load_torch(self, torch):
        self.torch = torch
        if self.kind == "torch":
            for name in self.weights:
                state = torch.load(str(safe(self.root, name)), weights_only=True, map_location="cpu", mmap=True)
                need(isinstance(state, dict) and state and all(type(key) is str and type(value) is torch.Tensor for key, value in state.items()), "PyTorch requires a flat tensor-only state_dict")
                payload = 0
                for parameter, tensor in state.items():
                    need(tensor.device.type == "cpu" and tensor.layout == torch.strided and tensor.is_contiguous() and not tensor.is_quantized, "Unsupported PyTorch tensor storage/layout")
                    need(parameter not in self.locations, "Duplicate tensor across PyTorch shards")
                    self.locations[parameter] = name
                    payload += tensor.numel() * tensor.element_size()
                self.budget.reserve(payload)
                self.states[name] = state
        for name, file in self.files.items():
            if name.endswith(".index.json"):
                need(file["artifact"]["sizeBytes"] <= self.budget.limits["maxProtocolBytes"], "Shard index exceeds metadata cap")
                data = parse(safe(self.root, name).read_bytes())
                need(type(data) is dict and type(data.get("weight_map")) is dict, "Invalid checkpoint shard index")
                need(data["weight_map"] == self.locations, "Shard index misses/misbinds actual tensors")
        for site in self.sites.values():
            need(site["name"] in self.locations, "Parameter site absent from actual checkpoint")
            shape, dtype = self.geometry(site["name"])
            need(shape == site["shape"] and dtype == site["dtype"], "Actual parameter shape/dtype differs from frozen site")

    def geometry(self, name):
        shard = self.locations[name]
        if self.kind == "safe":
            info = self.shards[shard]["header"][name]
            return info["shape"], ALIASES.get(info["dtype"], info["dtype"].lower())
        tensor = self.states[shard][name]
        return list(tensor.shape), {self.torch.float16: "f16", self.torch.bfloat16: "bf16", self.torch.float32: "f32", self.torch.float64: "f64"}.get(tensor.dtype, str(tensor.dtype))

    def tensor(self, site):
        name, torch = site["name"], self.torch
        shape, dtype = self.geometry(name)
        need(shape == site["shape"] and dtype == site["dtype"] and dtype in DTYPES, "Target tensor geometry/dtype mismatch")
        n = shape_size(shape)
        self.budget.reserve(n * 32)
        shard = self.locations[name]
        if self.kind == "safe":
            info, data_start = self.shards[shard]["header"][name], self.shards[shard]["dataStart"]
            first, last = info["data_offsets"]
            with safe(self.root, shard).open("rb") as stream:
                stream.seek(data_start + first)
                raw = stream.read(last - first)
            need(len(raw) == last - first, "Truncated source tensor")
            return torch.frombuffer(bytearray(raw), dtype=DTYPES[dtype]).reshape(shape).clone()
        tensor = self.states[shard][name]
        for other_name, other in self.states[shard].items():
            if other_name != name:
                need(other.untyped_storage().data_ptr() != tensor.untyped_storage().data_ptr(), "Aliased PyTorch parameter target requires explicit tied-weight semantics")
        return tensor.detach().clone()


def tensor_bytes(tensor):
    return tensor.detach().contiguous().view(torch.uint8).numpy().tobytes()


class Editor:
    def __init__(self, payload, budget, base, sources):
        self.payload, self.budget, self.base, self.sources = payload, budget, base, sources
        self.artifacts = set()

    def operand(self, ref):
        fields(ref, ("artifact", "shape", "dtype"))
        spec, shape, dtype = ref["artifact"], ref["shape"], ref["dtype"]
        need(dtype in DTYPES and len(shape) >= 1, "Unsupported operand dtype/shape")
        n = shape_size(shape)
        need(spec["uri"] == "omnius-artifact://sha256/" + spec["sha256"], "Artifact URI identity mismatch")
        if spec["sha256"] not in self.artifacts:
            self.budget.artifacts += integer(spec["sizeBytes"])
            need(self.budget.artifacts <= self.budget.limits["maxArtifactBytes"], "Aggregate operand artifact cap exceeded")
            self.artifacts.add(spec["sha256"])
        self.budget.reserve(n * 64 + spec["sizeBytes"] * 4)
        root = Path(self.payload["artifactRoot"]).resolve(strict=True)
        path = safe(root, spec["sha256"][:2] + "/" + spec["sha256"])
        need(hash_file(path, self.budget) == {"sha256": spec["sha256"], "sizeBytes": spec["sizeBytes"]}, "Operand full hash mismatch")
        raw = path.read_bytes()
        if spec["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")
            values = [finite(value) for value in value["data"]]
            tensor = torch.tensor(values, dtype=DTYPES[dtype]).reshape(shape)
        elif spec["mediaType"] == "application/octet-stream":
            need(len(raw) == n * torch.empty((), dtype=DTYPES[dtype]).element_size(), "Operand binary length mismatch")
            tensor = torch.frombuffer(bytearray(raw), dtype=DTYPES[dtype]).reshape(shape).clone()
        else:
            raise ValueError("Unsupported operand codec")
        need(bool(torch.isfinite(tensor).all()), "Operand overflows actual declared dtype")
        return tensor

    def apply(self, op, current, site):
        n, shape, kind = current.numel(), list(current.shape), op["operation"]
        need(op["tokenScope"] == {"kind": "all"} and kind in OPS, "Unsupported static operation/scope")
        self.budget.reserve(n * 160)  # explicit float64 products/copies/casts/masks/serialization; not full process RSS
        old_raw = tensor_bytes(current)
        ideal, replacement = None, None
        if kind == "WEIGHT_ADD":
            delta = op["delta"]
            value = finite(delta["value"]) if delta["kind"] == "scalar" else self.operand(delta["tensor"]).to(torch.float64)
            need(isinstance(value, (int, float)) or list(value.shape) == shape, "Addition operand geometry mismatch")
            self.budget.work(n)
            if isinstance(value, (int, float)) and value == 0:
                return current, 0, 0.0
            ideal = current.to(torch.float64) + value
            if not isinstance(value, (int, float)):
                ideal = torch.where(value == 0, current.to(torch.float64), ideal)
        elif kind == "WEIGHT_GAIN":
            gain = op["gain"]
            if gain["kind"] == "scalar":
                value = finite(gain["factor"])
                if value == 1:
                    return current, 0, 0.0
            else:
                axis = integer(gain["axis"])
                need(axis < len(shape), "Gain axis outside tensor")
                value = self.operand(gain["factors"]).to(torch.float64)
                need(list(value.shape) == [shape[axis]], "Axis gain factor shape mismatch")
                broadcast = [1] * len(shape)
                broadcast[axis] = shape[axis]
                value = value.reshape(broadcast)
            self.budget.work(n)
            ideal = current.to(torch.float64) * value
        elif kind == "WEIGHT_REPLACE":
            replacement = self.operand(op["replacement"])
            need(list(replacement.shape) == shape and replacement.dtype == current.dtype, "Replacement shape/dtype mismatch")
            self.budget.work(n)
        elif kind == "WEIGHT_PROJECT":
            need(len(shape) == 2 and op["side"] in ("left", "right") and op["normalization"] == "orthonormal", "Invalid projection semantics")
            basis = self.operand(op["basis"]).to(torch.float64)
            if basis.ndim == 1:
                basis = basis.unsqueeze(0)
            dimension = shape[0 if op["side"] == "left" else 1]
            need(basis.ndim == 2 and basis.shape[1] == dimension and 0 < basis.shape[0] <= dimension, "Canonical projection row-basis shape mismatch")
            rank = basis.shape[0]
            self.budget.reserve(rank * rank * 32)
            self.budget.work((2 * dimension + 1) * rank * rank)
            need(float((basis @ basis.T - torch.eye(rank, dtype=torch.float64)).abs().max()) <= self.payload["orthogonalityTolerance"], "Projection basis is not orthonormal")
            strength = finite(op["strength"])
            if strength == 0:
                return current, 0, 0.0
            self.budget.work(5 * n * rank)
            value = current.to(torch.float64)
            projected = basis.T @ (basis @ value) if op["side"] == "left" else (value @ basis.T) @ basis
            ideal = value - strength * projected
        elif kind == "LOW_RANK_UPDATE":
            need(len(shape) == 2 and op["scaling"] == "alpha_over_rank", "Invalid low-rank semantics")
            rank = integer(op["rank"], 1)
            a, b = self.operand(op["a"]).to(torch.float64), self.operand(op["b"]).to(torch.float64)
            need(list(a.shape) == [rank, shape[1]] and list(b.shape) == [shape[0], rank], "Low-rank A/B geometry mismatch")
            factor = finite(finite(op["scale"]) * (finite(op["alpha"]) / rank))
            self.budget.work(2)
            if factor == 0:
                return current, 0, 0.0
            self.budget.work(2 * n * rank + 2 * n)
            delta = b @ a
            ideal = torch.where(delta == 0, current.to(torch.float64), current.to(torch.float64) + factor * delta)
        elif kind == "WEIGHT_MERGE":
            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 parent")
                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["shape"] == site["shape"] and source_site["dtype"] == site["dtype"], "Merge source exact site geometry/dtype mismatch")
                weight = finite(source["weight"])
                value = parent.tensor(source_site)
                need(bool(torch.isfinite(value).all()), "Merge source contains nonfinite values")
                if weight != 0:
                    terms.append((value, weight))
            self.budget.work(2 * n * len(terms))
            if len(terms) == 1 and terms[0][1] == 1:
                replacement = terms[0][0]
            else:
                ideal = torch.zeros(shape, dtype=torch.float64)
                for value, weight in terms:
                    ideal = ideal + weight * value.to(torch.float64)
        if replacement is not None:
            result, rounding = replacement.clone(), 0.0
        else:
            need(ideal is not None and bool(torch.isfinite(ideal).all()), "Nonfinite float64 edit result")
            result = ideal.to(current.dtype)
            need(bool(torch.isfinite(result).all()), "Edited value overflows checkpoint storage dtype")
            rounding = float((ideal - result.to(torch.float64)).abs().max())
        new_raw = tensor_bytes(result)
        width = current.element_size()
        changed = sum(old_raw[i:i + width] != new_raw[i:i + width] for i in range(0, len(old_raw), width))
        return result, changed, rounding


def copy_file(source, destination, budget):
    destination.parent.mkdir(parents=True, exist_ok=True)
    with source.open("rb") as incoming, destination.open("xb") as outgoing:
        while True:
            budget.check()
            block = incoming.read(65536)
            if not block:
                break
            outgoing.write(block)
        outgoing.flush()
        os.fsync(outgoing.fileno())


def save_safetensors(base, name, destination, edits, payload, budget, remaining_output_bytes):
    shard = base.shards[name]
    metadata = dict(shard["header"])
    metadata["__metadata__"] = {**metadata.get("__metadata__", {}), "omnius.nol.parent_model_digest": base.model["modelDigest"],
                                "omnius.nol.recipe_digest": payload["recipe"]["recipeDigest"], "omnius.nol.edit_schema": SCHEMA}
    raw_header = encoded(metadata)
    raw_header += b" " * ((-len(raw_header)) % 8)
    expected_size = 8 + len(raw_header) + shard["dataBytes"]
    need(expected_size <= remaining_output_bytes, "Changed shard exceeds remaining output cap")
    destination.parent.mkdir(parents=True, exist_ok=True)
    with safe(base.root, name).open("rb") as incoming, destination.open("xb") as outgoing:
        outgoing.write(struct.pack("<Q", len(raw_header)))
        outgoing.write(raw_header)
        incoming.seek(shard["dataStart"])
        while True:
            budget.check()
            block = incoming.read(65536)
            if not block:
                break
            outgoing.write(block)
        for parameter, value in edits.items():
            first, last = metadata[parameter]["data_offsets"]
            raw = tensor_bytes(value)
            need(len(raw) == last - first, "Edited tensor changes native byte geometry")
            outgoing.seek(8 + len(raw_header) + first)
            outgoing.write(raw)
        outgoing.flush()
        os.fsync(outgoing.fileno())
    # Verify exact copied payload of every untouched tensor independently.
    with safe(base.root, name).open("rb") as incoming, destination.open("rb") as outgoing:
        for parameter, info in metadata.items():
            if parameter == "__metadata__" or parameter in edits:
                continue
            first, last = info["data_offsets"]
            incoming.seek(shard["dataStart"] + first)
            outgoing.seek(8 + len(raw_header) + first)
            remaining = last - first
            while remaining:
                budget.check()
                count = min(65536, remaining)
                need(incoming.read(count) == outgoing.read(count), "Untouched safetensors payload changed")
                remaining -= count


def edit(payload):
    fields(payload, ("schema", "exportId", "editorSha256", "checkpointRoot", "model", "identity", "recipe", "recipeIdentityJson", "sources", "artifactRoot", "stageDirectory", "limits", "orthogonalityTolerance"))
    need(payload["schema"] == "omnius.nol.checkpoint-edit-request.v1", "Unsupported edit request schema")
    need(sys.byteorder == "little", "Native little-endian tensor editor required")
    need(hash_file(Path(__file__))["sha256"] == payload["editorSha256"], "Editor source identity mismatch")
    limits = payload["limits"]
    fields(limits, ("maxInputBytes", "maxArtifactBytes", "maxOutputBytes", "maxWorkingBytes", "maxArithmeticUnits", "maxOperations", "maxDurationMs", "maxProtocolBytes"))
    budget = Budget(limits)
    need(len(encoded(payload)) <= limits["maxProtocolBytes"], "Request exceeds protocol cap")
    need(0 < finite(payload["orthogonalityTolerance"]) <= 0.01, "Invalid orthonormality tolerance")
    recipe, model = payload["recipe"], payload["model"]
    receipt_name = ".omnius-nol-edit." + payload["exportId"] + ".json"
    need(type(payload["exportId"]) is str and len(payload["exportId"]) == 36 and all(c in "0123456789abcdef-" for c in payload["exportId"]) and
         receipt_name not in {file["path"] for file in model["files"]}, "Invalid/colliding export receipt identity")
    verify_identity(payload["recipeIdentityJson"], {key: value for key, value in recipe.items() if key != "recipeDigest"}, recipe["recipeDigest"])
    need(recipe["schema"] == "omnius.nol.recipe.v1" and recipe["modelDigest"] == model["modelDigest"] and recipe["architectureMapDigest"] == model["architectureMapDigest"] and not recipe["calibrations"], "Recipe model/calibration mismatch")
    operations = recipe["operations"]
    need(0 < len(operations) <= limits["maxOperations"] and len({op["id"] for op in operations}) == len(operations), "Operation budget/ID mismatch")
    need(all(op["operation"] in OPS and op["tokenScope"] == {"kind": "all"} for op in operations), "Activation/token-masked edits cannot be serialized as weights")
    stage = Path(payload["stageDirectory"])
    need(stage.is_dir() and not stage.is_symlink() and not any(stage.iterdir()), "Editor requires a new empty private stage")
    stage = stage.resolve(strict=True)
    roots = [Path(payload["checkpointRoot"]).resolve(strict=True), Path(payload["artifactRoot"]).resolve(strict=True)] + [Path(source["root"]).resolve(strict=True) for source in payload["sources"]]
    need(all(not stage.is_relative_to(root) and not root.is_relative_to(stage) for root in roots), "Stage overlaps an immutable source")
    base = Checkpoint(model, payload["checkpointRoot"], payload["identity"], budget)
    sources = {model["modelDigest"]: base}
    for source in payload["sources"]:
        value = source["model"]
        need(value["modelDigest"] not in sources and value["modelDigest"] in recipe["parentModelDigests"], "Duplicate/undeclared source model")
        sources[value["modelDigest"]] = Checkpoint(value, source["root"], source["identity"], budget)
    # All explicit hashes, safe header/ZIP storage admission precede this import.
    global torch, DTYPES
    import torch
    torch.set_num_threads(1)
    torch.use_deterministic_algorithms(True)
    DTYPES = {"f16": torch.float16, "bf16": torch.bfloat16, "f32": torch.float32, "f64": torch.float64}
    for source in sources.values():
        source.load_torch(torch)
    editor, changed, logs = Editor(payload, budget, base, sources), {}, []
    for op in operations:
        budget.check()
        site = base.sites.get(op["siteId"])
        need(site is not None and site["dtype"] in DTYPES, "Unknown/nonfloating parameter target")
        name = site["name"]
        current = changed.get(name)
        if current is None:
            current = base.tensor(site)
        need(bool(torch.isfinite(current).all()), "Parent target contains nonfinite values")
        before = digest(tensor_bytes(current))
        result, count, rounding = editor.apply(op, current, site)
        changed[name] = result
        logs.append({"operationId": op["id"], "operation": op["operation"], "siteId": op["siteId"], "name": name,
                     "beforeTensorSha256": before, "afterTensorSha256": digest(tensor_bytes(result)),
                     "changedElements": count, "maxAbsRoundingError": rounding})
    # Do not rewrite a shard when every admitted edit was byte-neutral.
    effective = {}
    for name, tensor in changed.items():
        site = next(site for site in base.sites.values() if site["name"] == name)
        if tensor_bytes(tensor) != tensor_bytes(base.tensor(site)):
            effective.setdefault(base.locations[name], {})[name] = tensor
    files, output_bytes = [], 0
    for name in base.files:
        destination = safe(stage, name, False)
        if name not in effective:
            need(output_bytes + base.before[name]["sizeBytes"] <= limits["maxOutputBytes"], "Output byte budget exceeded before copy")
            copy_file(safe(base.root, name), destination, budget)
            representation = "copied_exact_file"
        elif base.kind == "safe":
            save_safetensors(base, name, destination, effective[name], payload, budget, limits["maxOutputBytes"] - output_bytes)
            representation = "safetensors_preserved_untouched_tensor_bytes"
        else:
            state = dict(base.states[name])
            state.update(effective[name])
            need(base.before[name]["sizeBytes"] * 2 + output_bytes + 65536 <= limits["maxOutputBytes"], "Conservative PyTorch serialization storage budget exceeded")
            destination.parent.mkdir(parents=True, exist_ok=True)
            torch.save(state, str(destination))
            zip_admission(destination, budget)
            check = torch.load(str(destination), weights_only=True, map_location="cpu", mmap=True)
            need(check.keys() == state.keys(), "PyTorch roundtrip tensor inventory mismatch")
            for parameter, value in state.items():
                need(check[parameter].dtype == value.dtype and list(check[parameter].shape) == list(value.shape) and tensor_bytes(check[parameter]) == tensor_bytes(value), "PyTorch roundtrip changed tensor bytes")
            representation = "pytorch_reencoded_verified_tensor_bytes"
        after = hash_file(destination, budget)
        output_bytes += after["sizeBytes"]
        need(output_bytes <= limits["maxOutputBytes"], "Aggregate output byte budget exceeded")
        files.append({"path": name, "before": base.before[name], "after": after,
                      "changed": after != base.before[name], "representation": representation})
    for source in sources.values():
        for name, before in source.before.items():
            need(hash_file(safe(source.root, name), budget) == before, "Parent/source changed during edit")
    receipt = {"schema": SCHEMA, "exportId": payload["exportId"], "parentModelDigest": model["modelDigest"], "recipeDigest": recipe["recipeDigest"],
               "sourceModelDigests": sorted(key for key in sources if key != model["modelDigest"]), "editorSha256": payload["editorSha256"],
               "runtime": {"python": platform.python_version(), "torch": str(torch.__version__), "safetensors": importlib.metadata.version("safetensors")},
               "numericalPolicy": "cpu_float64_operation_then_cast_original_dtype_each_operation; canonical_row_basis; scale_times_alpha_over_rank; explicit_linear_merge; native_blas_reassociation_possible",
               "inferencePerformed": False, "cudaUsed": False, "files": files, "operations": logs,
               "usage": {"inputBytes": budget.input, "artifactBytes": budget.artifacts, "outputBytes": output_bytes,
                         "reservedWorkingBytes": budget.working, "arithmeticUnits": budget.arithmetic, "durationMs": int((time.monotonic() - budget.started) * 1000)}}
    # Fixed-point includes receipt bytes in the declared storage total.
    for _ in range(8):
        raw = encoded(receipt)
        total = output_bytes + len(raw)
        if receipt["usage"]["outputBytes"] == total:
            break
        receipt["usage"]["outputBytes"] = total
    raw = encoded(receipt)
    need(len(raw) <= limits["maxProtocolBytes"] and output_bytes + len(raw) <= limits["maxOutputBytes"], "Receipt exceeds protocol/storage byte budget")
    with (stage / receipt_name).open("xb") as stream:
        stream.write(raw)
        stream.flush()
        os.fsync(stream.fileno())
    budget.check()
    return {"schema": "omnius.nol.checkpoint-edit-ready.v1", "exportId": payload["exportId"]}


def publish(payload):
    fields(payload, ("stageDirectory", "outputDirectory", "exportId", "receiptSha256", "receiptPath"))
    need(sys.platform.startswith("linux"), "Atomic no-replace directory publish unsupported on this platform")
    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-edit." + payload["exportId"] + ".json", "Publish receipt path binding mismatch")
    receipt = safe(stage, payload["receiptPath"])
    need(hash_file(receipt)["sha256"] == payload["receiptSha256"] and parse(receipt.read_bytes())["exportId"] == payload["exportId"], "Publish receipt custody mismatch")
    libc = ctypes.CDLL(None, use_errno=True)
    rename = getattr(libc, "renameat2", None)
    need(rename is not None, "Atomic no-replace rename unavailable; no unsafe fallback")
    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))
    directory = os.open(target.parent, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0))
    try:
        os.fsync(directory)
    finally:
        os.close(directory)
    return {"published": True, "exportId": payload["exportId"]}


if __name__ == "__main__":
    try:
        signal.signal(signal.SIGTERM, lambda *_: (_ for _ in ()).throw(InterruptedError("Checkpoint edit cancelled")))
        raw = sys.stdin.buffer.read(16 * 1024 * 1024 + 1)
        need(len(raw) <= 16 * 1024 * 1024, "Absolute request frame cap exceeded")
        payload = parse(raw)
        result = publish(payload) if sys.argv[1:] == ["--publish"] else edit(payload) if not sys.argv[1:] else (_ for _ in ()).throw(ValueError("Unknown editor command"))
        sys.stdout.buffer.write(encoded(result))
        sys.stdout.flush()
    except BaseException as error:
        # Diagnostics stay on the bounded private subprocess channel.
        sys.stderr.write((type(error).__name__ + ": " + str(error))[-4000:] +
                         "; torchImported=" + str("torch" in sys.modules).lower() + "\n")
        sys.exit(1)
