#!/usr/bin/env python3
"""Explicit CPU-only PEFT serialization. Importing this file is inert.

This exports an ideal ordered parameter delta, not a native serving equivalence
receipt. It never instantiates a model or imports PEFT. The separately pinned
checkpoint reader performs full-file and safe storage admission before Torch.
Format references: https://huggingface.co/docs/peft/en/developer_guides/checkpoint
and https://huggingface.co/docs/peft/en/package_reference/lora .
"""
import ctypes
import hashlib
import importlib.metadata
import json
import math
import os
from pathlib import Path
import platform
import re
import signal
import sys
import time
import types


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


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


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


def published_file_identity(path):
    before = path.lstat()
    need(path.is_file() and not path.is_symlink(), "Publish file must be nonsymlink regular")
    sha, size = hashlib.sha256(), 0
    with path.open("rb") as stream:
        opened = os.fstat(stream.fileno())
        need((before.st_dev, before.st_ino) == (opened.st_dev, opened.st_ino), "Publish file identity changed")
        while True:
            block = stream.read(65536)
            if not block:
                break
            sha.update(block); size += len(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), "Publish file changed while hashing")
    return {"sha256": sha.hexdigest(), "sizeBytes": size}


def load_reader(payload):
    path = Path(payload["checkpointEditorPath"])
    need(path.is_absolute() and path.is_file() and not path.is_symlink(), "Explicit pinned checkpoint reader required")
    raw = path.read_bytes()
    need(len(raw) <= 1024 * 1024 and digest(raw) == payload["checkpointEditorSha256"], "Checkpoint reader source identity mismatch")
    # Execute the verified bytes rather than reopening a path after its hash.
    reader = types.ModuleType("nol_pinned_checkpoint_reader")
    reader.__file__ = str(path)
    exec(compile(raw, str(path), "exec"), reader.__dict__)
    return reader


def export(payload):
    fields(payload, ("schema", "exportId", "exporterSha256", "checkpointEditorPath", "checkpointEditorSha256", "checkpointRoot", "artifactRoot",
                     "stageDirectory", "model", "identity", "recipe", "recipeIdentityJson", "binding", "bindingDigest", "moduleBindings", "baseModel", "limits"))
    need(payload["schema"] == "omnius.nol.lora-export-request.v1" and sys.byteorder == "little", "Unsupported export request/platform")
    need(digest(Path(__file__).read_bytes()) == payload["exporterSha256"], "LoRA exporter source identity mismatch")
    need(re.fullmatch(r"[a-f0-9-]{36}", payload["exportId"]) is not None, "Invalid export identity")
    reader = load_reader(payload)
    limits = payload["limits"]
    fields(limits, ("maxInputBytes", "maxArtifactBytes", "maxOutputBytes", "maxWorkingBytes", "maxArithmeticUnits", "maxOperations", "maxDurationMs", "maxProtocolBytes", "maxRank", "maxModules"))
    budget = reader.Budget(limits)
    need(len(encoded(payload)) <= limits["maxProtocolBytes"], "Request exceeds protocol budget")
    model, recipe, binding = payload["model"], payload["recipe"], payload["binding"]
    reader.verify_identity(payload["recipeIdentityJson"], {k: v for k, v in recipe.items() if k != "recipeDigest"}, recipe["recipeDigest"])
    need(recipe["modelDigest"] == model["modelDigest"] and recipe["architectureMapDigest"] == model["architectureMapDigest"] and
         recipe.get("approximateLoweringAcknowledged") is True and not recipe["calibrations"], "Recipe/model/lowering acknowledgement mismatch")
    need(binding["backend"] == "transformers" and all(binding[key] == model[key] for key in ("modelDigest", "architectureMapDigest", "tokenizerDigest", "chatTemplateDigest")), "Runtime/model identity mismatch")
    operations = recipe["operations"]
    need(0 < len(operations) <= limits["maxOperations"] and len({op["id"] for op in operations}) == len(operations), "Operation ID/count budget mismatch")
    need(all(op["operation"] in ("WEIGHT_GAIN", "LOW_RANK_UPDATE") and op["tokenScope"] == {"kind": "all"} for op in operations), "Unsupported LoRA operation or token scope")
    modules = payload["moduleBindings"]
    need(0 < len(modules) <= limits["maxModules"], "Module count exceeds budget")
    need(len({value["siteId"] for value in modules}) == len(modules) and len({value["modulePath"] for value in modules}) == len(modules), "Duplicate module binding")
    need({value["siteId"] for value in modules} == {op["siteId"] for op in operations}, "Exact module/site coverage required")
    stage = Path(payload["stageDirectory"])
    need(stage.is_dir() and not stage.is_symlink() and not any(stage.iterdir()), "New empty private stage required")
    roots = [Path(payload[key]).resolve(strict=True) for key in ("checkpointRoot", "artifactRoot")]
    need(all(not stage.resolve().is_relative_to(root) and not root.is_relative_to(stage.resolve()) for root in roots), "Stage overlaps immutable input")
    base = reader.Checkpoint(model, payload["checkpointRoot"], payload["identity"], budget)
    # Full hashes and bounded ZIP/header admission have completed. CPU tensors
    # only: no transformers/PEFT import, CUDA calls or model construction.
    import torch
    torch.set_num_threads(1)
    torch.use_deterministic_algorithms(True)
    reader.torch = torch
    reader.DTYPES = {"f16": torch.float16, "bf16": torch.bfloat16, "f32": torch.float32, "f64": torch.float64}
    base.load_torch(torch)
    editor = reader.Editor(payload, budget, base, {})
    states = {}
    for declaration in modules:
        fields(declaration, ("siteId", "modulePath", "moduleType", "layout"))
        site = base.sites.get(declaration["siteId"])
        need(site is not None and len(site["shape"]) == 2 and site["dtype"] in reader.DTYPES and
             site["name"] == declaration["modulePath"] + ".weight" and declaration["moduleType"] == "torch.nn.Linear" and declaration["layout"] == "out_in" and
             re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z0-9_]+)*", declaration["modulePath"]) is not None,
             "LoRA requires an explicitly declared linear [out,in] weight binding")
        tensor = base.tensor(site)
        need(bool(torch.isfinite(tensor).all()), "Nonfinite parent parameter")
        budget.reserve(tensor.numel() * 16)
        states[site["id"]] = {"site": site, "declaration": declaration, "base": tensor.to(torch.float64), "terms": [],
                              "sourceTensorSha256": digest(reader.tensor_bytes(tensor)), "operationIds": [], "operations": []}
    operands = {}
    def operand(ref):
        value = editor.operand(ref)
        operands[ref["artifact"]["sha256"]] = ref["artifact"]
        budget.reserve(value.numel() * 16)
        return value.to(torch.float64)
    def append(state, a, b):
        need(bool(torch.isfinite(a).all()) and bool(torch.isfinite(b).all()), "Nonfinite composed LoRA factor")
        rank = a.shape[0]
        need(sum(term[0].shape[0] for term in state["terms"]) + rank <= limits["maxRank"], "Exact LoRA rank exceeds budget; no approximation performed")
        if bool(torch.count_nonzero(a)) and bool(torch.count_nonzero(b)):
            state["terms"].append((a, b))
    for op in operations:
        budget.check()
        state = states[op["siteId"]]
        rows, cols = state["site"]["shape"]
        state["operationIds"].append(op["id"])
        state["operations"].append(op)
        if op["operation"] == "WEIGHT_GAIN":
            gain = op["gain"]
            if gain["kind"] == "scalar":
                need(gain["factor"] == 1, "Nonneutral scalar/dense gain is unsupported; use bounded sparse axis1 factors")
                continue
            need(gain["axis"] == 1 and gain["factors"]["shape"] == [cols], "Sparse column gain requires axis1 factors matching input width")
            factors = operand(gain["factors"])
            indices = torch.nonzero(factors != 1).flatten()
            rank = indices.numel()
            need(rank <= limits["maxRank"], "Sparse column gain exceeds exact rank budget")
            # (W0 + sum B_i A_i) D = W0 + W0(D-I) + sum B_i(A_i D).
            # Rewriting EACH previous A preserves order without cross terms.
            transformed = []
            for a, b in state["terms"]:
                budget.reserve(a.numel() * 16)
                budget.work(a.numel())
                scaled = a * factors.unsqueeze(0)
                need(bool(torch.isfinite(scaled).all()), "Ordered gain composition overflow")
                transformed.append((scaled, b))
            state["terms"] = transformed
            if rank:
                need(sum(term[0].shape[0] for term in transformed) + rank <= limits["maxRank"], "Composed exact LoRA rank exceeds budget")
                budget.reserve(rank * (rows + cols) * 32)
                budget.work(rank * (rows + cols))
                a = torch.zeros((rank, cols), dtype=torch.float64)
                a[torch.arange(rank), indices] = 1
                b = state["base"][:, indices] * (factors[indices] - 1).unsqueeze(0)
                append(state, a, b)
        else:
            rank = reader.integer(op["rank"], 1)
            need(rank <= limits["maxRank"] and op["a"]["shape"] == [rank, cols] and op["b"]["shape"] == [rows, rank], "Low-rank factor geometry/rank mismatch")
            need(op["scaling"] == "alpha_over_rank", "Unsupported low-rank scaling")
            coefficient = reader.finite(op["scale"]) * (reader.finite(op["alpha"]) / rank)
            need(math.isfinite(coefficient), "Canonical scale*(alpha/rank) overflows")
            a, b = operand(op["a"]), operand(op["b"])
            budget.reserve(b.numel() * 16)
            budget.work(b.numel())
            append(state, a, b * coefficient)
    common_rank = max(1, *(sum(a.shape[0] for a, _ in state["terms"]) for state in states.values()))
    need(common_rank <= limits["maxRank"], "Common adapter rank exceeds budget")
    tensors, module_receipts = {}, []
    tensor_payload_bytes = 0
    for site_id, state in states.items():
        budget.check()
        rows, cols = state["site"]["shape"]
        n = common_rank * (rows + cols)
        # Explicit concat/padding/cast/comparison/serializer buffers; cumulative
        # payload budget, not a claim to cap Torch process RSS/allocator overhead.
        budget.reserve(n * 96)
        budget.work(n)
        a, b = torch.zeros((common_rank, cols), dtype=torch.float64), torch.zeros((rows, common_rank), dtype=torch.float64)
        cursor = 0
        for term_a, term_b in state["terms"]:
            r = term_a.shape[0]
            a[cursor:cursor+r], b[:, cursor:cursor+r] = term_a, term_b
            cursor += r
        af, bf = a.to(torch.float32).contiguous(), b.to(torch.float32).contiguous()
        need(bool(torch.isfinite(af).all()) and bool(torch.isfinite(bf).all()), "Adapter F32 storage overflow")
        error_a = float(torch.max(torch.abs(af.to(torch.float64) - a)))
        error_b = float(torch.max(torch.abs(bf.to(torch.float64) - b)))
        need(math.isfinite(error_a) and math.isfinite(error_b), "Factor cast error overflows report")
        # Independently reconstruct both ideal and stored-factor deltas in
        # bounded row blocks. This catches finite-factor products that overflow
        # the parameter dtype, without allocating a full dense delta at once.
        # It still cannot prove the serving kernel's accumulation semantics.
        budget.reserve(n * 16 + rows * cols * 128)
        budget.work(rows * cols * (4 * common_rank + 8))
        af64, bf64 = af.to(torch.float64), bf.to(torch.float64)
        delta_cast_error = target_cast_error = 0.0
        for first_row in range(0, rows, 64):
            budget.check()
            last_row = min(rows, first_row + 64)
            ideal_delta = b[first_row:last_row] @ a
            stored_delta = bf64[first_row:last_row] @ af64
            target = state["base"][first_row:last_row] + stored_delta
            cast_target = target.to(reader.DTYPES[state["site"]["dtype"]])
            need(bool(torch.isfinite(ideal_delta).all()) and bool(torch.isfinite(stored_delta).all()) and
                 bool(torch.isfinite(target).all()) and bool(torch.isfinite(cast_target).all()), "LoRA dense delta/target dtype overflow")
            delta_cast_error = max(delta_cast_error, float(torch.max(torch.abs(ideal_delta - stored_delta))))
            target_cast_error = max(target_cast_error, float(torch.max(torch.abs(cast_target.to(torch.float64) - target))))
        need(math.isfinite(delta_cast_error) and math.isfinite(target_cast_error), "Delta/dtype cast error report overflow")
        prefix = "base_model.model." + state["declaration"]["modulePath"]
        tensors[prefix + ".lora_A.weight"], tensors[prefix + ".lora_B.weight"] = af, bf
        tensor_payload_bytes += n * 4
        need(tensor_payload_bytes <= limits["maxOutputBytes"], "Adapter tensor storage budget exceeded")
        module_receipts.append({**state["declaration"], "parameterName": state["site"]["name"], "shape": state["site"]["shape"], "sourceDtype": state["site"]["dtype"],
                                "sourceTensorSha256": state["sourceTensorSha256"], "operationIds": state["operationIds"], "effectiveRank": cursor, "serializedRank": common_rank,
                                "alpha": common_rank, "scaling": 1, "storageDtype": "f32", "maxFactorAbsCastError": {"a": error_a, "b": error_b},
                                "maxAbsDeltaFactorCastError": delta_cast_error, "maxAbsTargetDtypeCastError": target_cast_error,
                                "factorSha256": {"a": digest(reader.tensor_bytes(af)), "b": digest(reader.tensor_bytes(bf))}})
    names = sorted(value["modulePath"] for value in modules)
    # PEFT fullmatches string target_modules. Anchored escaped paths prevent
    # suffix matching from selecting any extra module with the same leaf name.
    config = {"peft_type": "LORA", "task_type": payload["baseModel"]["taskType"], "base_model_name_or_path": payload["baseModel"]["nameOrPath"],
              "revision": payload["baseModel"]["revision"], "target_modules": "^(?:" + "|".join(re.escape(name) for name in names) + ")$",
              "r": common_rank, "lora_alpha": common_rank, "lora_dropout": 0.0, "bias": "none", "fan_in_fan_out": False,
              "inference_mode": True, "init_lora_weights": True, "use_rslora": False, "use_dora": False, "rank_pattern": {}, "alpha_pattern": {}}
    config_raw = encoded(config)
    # Bounded safetensors header before save_file's internal serialization.
    header_bound = 256 + sum(len(name.encode()) + 160 for name in tensors)
    need(tensor_payload_bytes + header_bound + len(config_raw) <= limits["maxOutputBytes"], "Adapter serialized storage budget exceeded")
    budget.reserve(header_bound * 4)
    from safetensors.torch import save_file
    save_file(tensors, str(stage / "adapter_model.safetensors"), metadata={"format": "pt"})
    with (stage / "adapter_config.json").open("xb") as stream:
        stream.write(config_raw); stream.flush(); os.fsync(stream.fileno())
    with (stage / "adapter_model.safetensors").open("rb") as stream:
        os.fsync(stream.fileno())
    # Actual safetensors header + bytes independently match the tensors before
    # publication; reader enforces dtype/shape/ranges/complete payload bounds.
    saved = reader.header(stage / "adapter_model.safetensors", budget)
    need({name for name in saved["header"] if name != "__metadata__"} == tensors.keys(), "Adapter tensor inventory mismatch")
    for name, tensor in tensors.items():
        info = saved["header"][name]
        need(info["shape"] == list(tensor.shape) and info["dtype"] == "F32", "Adapter tensor round-trip metadata mismatch")
        with (stage / "adapter_model.safetensors").open("rb") as stream:
            first, last = info["data_offsets"]
            stream.seek(saved["dataStart"] + first)
            need(stream.read(last-first) == reader.tensor_bytes(tensor), "Adapter tensor round-trip bytes mismatch")
    for name, before in base.before.items():
        need(reader.hash_file(reader.safe(base.root, name), budget) == before, "Immutable checkpoint changed during export")
    for ref in operands.values():
        path = reader.safe(roots[1], ref["sha256"][:2] + "/" + ref["sha256"])
        need(reader.hash_file(path, budget) == {"sha256": ref["sha256"], "sizeBytes": ref["sizeBytes"]}, "Immutable operand changed during export")
    files = [{"path": name, **reader.hash_file(stage / name, budget)} for name in ("adapter_config.json", "adapter_model.safetensors")]
    output_bytes = sum(file["sizeBytes"] for file in files)
    receipt = {"schema": "omnius.nol.lora-export.v1", "exportId": payload["exportId"], "parentModelDigest": model["modelDigest"],
               "architectureMapDigest": model["architectureMapDigest"], "recipeDigest": recipe["recipeDigest"], "parentModelDigests": recipe["parentModelDigests"],
               "binding": binding, "bindingDigest": payload["bindingDigest"], "baseModel": payload["baseModel"], "modelFiles": model["files"], "operations": operations,
               "operands": sorted(operands.values(), key=lambda ref: ref["sha256"]), "exporterSha256": payload["exporterSha256"], "checkpointEditorSha256": payload["checkpointEditorSha256"],
               "runtime": {"python": platform.python_version(), "torch": str(torch.__version__), "safetensors": importlib.metadata.version("safetensors")},
               "moduleBindingProof": "caller_declared_linear_modules_actual_checkpoint_parameter_geometry_verified",
               "numericalPolicy": "cpu_float64_ideal_ordered_delta; concatenate_terms_no_factor_cross_terms; scale_times_alpha_over_rank; adapter_f32_cast; not_original_dtype_round_after_each_operation; native_matmul_reassociation_possible",
               "nativeEquivalence": "required_unvalidated", "inferencePerformed": False, "cudaUsed": False, "modules": module_receipts, "files": files,
               "usage": {"inputBytes": budget.input, "artifactBytes": budget.artifacts, "outputBytes": output_bytes,
                         "reservedWorkingBytes": budget.working, "arithmeticUnits": budget.arithmetic, "durationMs": int((time.monotonic() - budget.started) * 1000)}}
    for _ in range(8):
        total = output_bytes + len(encoded(receipt))
        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 protocol/storage budget exceeded")
    receipt_name = ".omnius-nol-lora." + payload["exportId"] + ".json"
    with (stage / receipt_name).open("xb") as stream:
        stream.write(raw); stream.flush(); os.fsync(stream.fileno())
    budget.check()
    return {"schema": "omnius.nol.lora-export-ready.v1", "exportId": payload["exportId"]}


def publish(payload):
    fields(payload, ("stageDirectory", "outputDirectory", "exportId", "receiptSha256", "receiptPath"))
    need(sys.platform.startswith("linux"), "Atomic no-replace 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(re.fullmatch(r"[a-f0-9-]{36}", payload["exportId"]) is not None and payload["receiptPath"] == ".omnius-nol-lora." + payload["exportId"] + ".json", "Publish receipt identity mismatch")
    path = stage / payload["receiptPath"]
    need(path.is_file() and not path.is_symlink(), "Publish receipt must be regular")
    raw = path.read_bytes()
    receipt = parse(raw)
    need(len(raw) <= 16 * 1024 * 1024 and digest(raw) == payload["receiptSha256"] and receipt["exportId"] == payload["exportId"], "Publish receipt custody mismatch")
    expected_files = {"adapter_config.json", "adapter_model.safetensors"}
    need({value["path"] for value in receipt["files"]} == expected_files and len(receipt["files"]) == 2 and
         {value.name for value in stage.iterdir()} == expected_files | {payload["receiptPath"]}, "Publish complete file inventory mismatch")
    for file in receipt["files"]:
        need(published_file_identity(stage / file["path"]) == {"sha256": file["sha256"], "sizeBytes": file["sizeBytes"]}, "Publish artifact hash mismatch")
    stage_fd = os.open(stage, os.O_RDONLY | getattr(os, "O_DIRECTORY", 0))
    try:
        os.fsync(stage_fd)
    finally:
        os.close(stage_fd)
    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("LoRA export 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 export(payload) if not sys.argv[1:] else (_ for _ in ()).throw(ValueError("Unknown command"))
        sys.stdout.buffer.write(encoded(result)); sys.stdout.flush()
    except BaseException as error:
        sys.stderr.write((type(error).__name__ + ": " + str(error))[-4000:] + "; torchImported=" + str("torch" in sys.modules).lower() + "\n")
        sys.exit(1)
