#!/usr/bin/env python3
"""Versioned unmerged PEFT observation extension. No framework import at startup.

Three explicit teacher-forced prefills; never delegates profile to generate.
Facts do not issue host capability or materialized-weight equivalence authority.
"""
import hashlib
import importlib.util
import json
import math
import os
from pathlib import Path
import struct
import sys
import uuid

sys.dont_write_bytecode = True
VERSION = "omnius.nol.transformers-peft-validation.v2"
PEFT_SHA = "acbedcc78edf2565ce8638a85df81a1e4b8a4f98c5d0dfb49b38e2785b247682"
BASE_SHA = "ea1950abcceda7d76c5fb54c3b10a73d5756cd947149d4b3b6d7be83ae12ebc0"
COMPANION = Path(__file__).resolve().parent / "nol-transformers-peft-worker.py"
if COMPANION.is_symlink() or hashlib.sha256(COMPANION.read_bytes()).hexdigest() != PEFT_SHA:
    raise RuntimeError("Frozen PEFT companion identity changed")
spec = importlib.util.spec_from_file_location("_nol_validation_peft", COMPANION)
peft = importlib.util.module_from_spec(spec)
spec.loader.exec_module(peft)
base = peft.base
require, fields, integer, Rejected = base.require, base.fields, base.integer, base.Rejected


def ownership():
    return {key: base.OWNER[key] for key in ("jobId", "ownerId", "generation")}


def runtime_info(lock_path=None, hash_runtime=False, max_bytes=34359738368):
    info = peft.ORIGINAL_BUILD(lock_path, hash_runtime, max_bytes)
    info.update(schema="omnius.nol.transformers-peft-validation-build.v2", workerVersion=VERSION,
                workerSha256=base.hash_file(Path(__file__).resolve())["sha256"],
                peftWorkerSha256=PEFT_SHA, baseWorkerSha256=BASE_SHA,
                reviewedPeftSource=peft.source_pin())
    return info


base.runtime_info = runtime_info
base.WORKER_VERSION = VERSION


def no_target_aliases(state, torch):
    # Whole storage, not only equal Tensor objects, is the conservative boundary.
    originals = state["originals"]
    for name in state["targetHashes"]:
        target = originals[name]
        storage = target.untyped_storage().data_ptr()
        require(target.is_contiguous() and target.storage_offset() == 0 and target.numel() * target.element_size() == target.untyped_storage().nbytes(),
                "Target has unsupported view/storage geometry", "UNSUPPORTED")
        require(all(other_name == name or other.untyped_storage().data_ptr() != storage
                    for other_name, other in originals.items()), "Tied/aliased PEFT target is unsupported", "UNSUPPORTED")
        require(all(buffer.untyped_storage().data_ptr() != storage for buffer in state["model"].buffers()),
                "Target aliases a model buffer", "UNSUPPORTED")
    base_storage = {parameter.untyped_storage().data_ptr() for parameter in originals.values()}
    factor_storage = [factor.untyped_storage().data_ptr() for factor in state["factors"].values()]
    require(len(factor_storage) == len(set(factor_storage)) and not base_storage.intersection(factor_storage),
            "Tied/base-aliased adapter factors are unsupported", "UNSUPPORTED")


def admit_base_sources(payload):
    require(payload.get("model", {}).get("quantization", {}).get("method") == "none",
            "Only unquantized dense bases are supported", "UNSUPPORTED")
    path = base.safe_path(Path(payload["checkpointRoot"]), "config.json")
    require(path.stat().st_size <= 1048576, "Base configuration byte limit", "CAPACITY")
    config_ref = next((part["artifact"] for part in payload["model"].get("files", []) if part["path"] == "config.json"), None)
    require(config_ref is not None and config_ref["sizeBytes"] <= 1048576, "Frozen base config full artifact required", "IDENTITY")
    raw = path.read_bytes()
    require(len(raw) == config_ref["sizeBytes"] and hashlib.sha256(raw).hexdigest() == config_ref["sha256"],
            "Frozen base configuration bytes changed", "IDENTITY")
    config = base.parse_json(raw.decode("utf-8", "strict"))
    text = config.get("text_config", config)
    require(config.get("model_type") == "qwen3_5" and text.get("model_type") == "qwen3_5_text" and
            not config.get("quantization_config") and not text.get("quantization_config") and
            not text.get("num_experts") and not text.get("num_local_experts"),
            "Quantized/MoE/unreviewed base configurations are unsupported", "UNSUPPORTED")


def capture_prefills(state, torch, token_ids, positions, forward, capture_limit, reset=None):
    """CPU-testable actual PEFT Linear seam, independent of Runtime.load.

    `forward` performs exactly one explicit cold prefill. No generation, sampling,
    fallback, model copy or phase-input substitution is used here.
    """
    no_target_aliases(state, torch)
    require(type(token_ids) is list and 0 < len(token_ids) <= 4096 and all(type(v) is int and 0 <= v < 2**31 for v in token_ids), "Invalid token IDs")
    require(type(positions) is list and 0 < len(positions) <= 64 and positions == sorted(set(positions)) and
            all(type(p) is int and 0 <= p < len(token_ids) for p in positions), "Invalid capture positions")
    integer(capture_limit, 1, 268435456)
    arrays, metadata, states, calls = {}, [], [], 0
    used = 0

    def save(key, value, width=None):
        nonlocal used
        require(key not in arrays and type(value) is torch.Tensor and value.ndim == 3 and value.shape[0] == 1 and value.shape[1] == len(token_ids),
                "Target invocation/shape is not one full prefill", "UNSUPPORTED")
        require(value.dtype in (torch.float32, torch.float16, torch.bfloat16) and (width is None or value.shape[2] == width), "Capture dtype/geometry changed", "IDENTITY")
        # Admission preceded forward. This check also detects unexpected hook geometry.
        amount = len(positions) * value.shape[2] * 4
        require(used + amount <= capture_limit, "Capture byte budget exceeded", "CAPACITY")
        selected = value.detach()[0, positions, :].to(dtype=torch.float32, device="cpu").contiguous()
        require(bool(torch.isfinite(selected).all()), "Nonfinite native capture", "NUMERIC")
        arrays[key] = selected.numpy().copy()
        used += amount

    try:
        for index, item in enumerate(state["approved"]["geometry"]):
            module = state["modules"][item["modulePath"]]
            require(not module.training and module.scaling == {"default": 1.0} and (type(module.lora_dropout["default"]) is torch.nn.Identity or (type(module.lora_dropout["default"]) is torch.nn.Dropout and module.lora_dropout["default"].p == 0)),
                    "Only eval scale-one/dropout-zero PEFT is supported", "UNSUPPORTED")
            info = {"index": index, "modulePath": item["modulePath"], "shape": item["shape"], "rank": item["rank"],
                    "baseDtype": str(module.base_layer.weight.dtype).removeprefix("torch."), "factorHashes": {}}
            for part in ("A", "B"):
                factor = getattr(module, "lora_" + part)["default"].weight
                amount = factor.numel() * 4
                require(used + amount <= capture_limit, "Factor capture byte budget exceeded", "CAPACITY")
                arrays[f"m{index}_factor_{part}"] = factor.detach().to("cpu").contiguous().numpy().copy()
                used += amount
                info["factorHashes"][part] = peft.tensor_sha(factor, torch)
            metadata.append(info)
        for phase in ("disabled", "enabled", "restored"):
            enabled = phase == "enabled"
            reset_before = reset() if reset else None
            state["model"].base_model.disable_adapter_layers()
            peft.verify_state(state, torch, False)
            if enabled:
                state["model"].base_model.enable_adapter_layers()
                for parameter in state["model"].parameters(): parameter.requires_grad_(False)
            before = peft.verify_state(state, torch, enabled)
            handles = []
            try:
                for info in metadata:
                    i, module = info["index"], state["modules"][info["modulePath"]]
                    prefix = f"m{i}_{phase}_"
                    def pre(mod, args, key=prefix + "input", width=info["shape"][1]):
                        require(len(args) >= 1, "Missing actual Linear input", "IDENTITY")
                        save(key, args[0], width)
                    def hook(key, width):
                        return lambda mod, args, output: save(key, output, width)
                    handles.append(module.register_forward_pre_hook(pre))
                    handles.append(module.base_layer.register_forward_hook(hook(prefix + "base", info["shape"][0])))
                    handles.append(module.register_forward_hook(hook(prefix + "after", info["shape"][0])))
                    if enabled:
                        handles.append(module.lora_A["default"].register_forward_hook(hook(prefix + "a", info["rank"])))
                        handles.append(module.lora_B["default"].register_forward_hook(hook(prefix + "branch", info["shape"][0])))
                with torch.inference_mode():
                    result = forward()
                calls += 1
                require(getattr(result, "past_key_values", None) is None, "Prefill retained KV state", "RESET")
                # Caller discards logits; only fixed selected coordinates are kept.
                del result
            finally:
                for handle in handles: handle.remove()
                state["model"].base_model.disable_adapter_layers()
            after = peft.verify_state(state, torch, False)
            reset_after = reset() if reset else None
            states.append({"phase": phase, "before": before, "after": after, "resetBefore": reset_before, "resetAfter": reset_after})
        expected = len(metadata) * 13
        require(len(arrays) == expected, "Missing/repeated native target invocation", "IDENTITY")
        for info in metadata:
            for field in ("input", "base", "after"):
                a, b = arrays[f'm{info["index"]}_disabled_{field}'], arrays[f'm{info["index"]}_restored_{field}']
                require(a.tobytes() == b.tobytes(), "Disabled/restored capture differs", "RESET")
        return arrays, {"modules": metadata, "positions": positions, "tokenIds": token_ids,
                        "states": states, "forwardCalls": calls, "inputTokens": 3 * len(token_ids), "sampledTokens": 0,
                        "captureTensorBytes": used, "parameterMergePerformed": False,
                        "numericalPolicy": "native_f32_A_then_native_f32_B_then_add_and_cast_to_base_output_dtype",
                        "materializedWeightEquivalenceClaim": False,
                        "resetMode": "native_reset_between_every_prefill" if reset else "cpu_fixture_no_native_runtime_reset"}
    finally:
        state["model"].base_model.disable_adapter_layers()
        peft.verify_state(state, torch, False)


def admission(state, tokens, positions, geometry, limits):
    fields(geometry, ("layers", "hiddenSize", "intermediateSize", "attentionHeads", "vocabSize"))
    g = {key: integer(value, 1, 1000000) for key, value in geometry.items()}
    fields(limits, ("maxForwardTokens", "maxCaptureBytes", "maxWorkingBytes", "maxMultiplyAdds"))
    limits = {key: integer(value, 1) for key, value in limits.items()}
    t, h, n, v, heads = tokens, g["hiddenSize"], g["layers"], g["vocabSize"], g["attentionHeads"]
    parameter_elements = sum(parameter.numel() for parameter in state["originals"].values())
    require(parameter_elements > 0, "Actual base parameter inventory required", "IDENTITY")
    attention_width_bound = max(h, 65536)
    # Deliberately loose bounds over actual full parameter inventory and reviewed
    # dense Qwen attention/recurrent widths, not the smaller 4*H^2 approximation.
    require(heads <= 256 and h <= 65536 and g["intermediateSize"] <= 262144, "Unsupported compute geometry", "UNSUPPORTED")
    multiply = 3 * (16*t*parameter_elements + 128*n*t*t*attention_width_bound + t*h*v)
    capture = 65536
    for item in state["approved"]["geometry"]:
        out, inp = item["shape"]; rank = item["rank"]
        multiply += 3*t*rank*(inp+out)
        capture += (rank*(inp+out) + positions*(3*inp+7*out+rank))*4 + 13*1024
    working = 4*t*h*(256*n+128) + 32*t*g["intermediateSize"]*n + 64*t*t*heads*n + 16*t*v + capture*3 + state["approved"]["totalBytes"]*2 + state["approved"]["factorBytes"]*2
    require(3*t <= limits["maxForwardTokens"] and capture <= limits["maxCaptureBytes"] and
            working <= limits["maxWorkingBytes"] and multiply <= limits["maxMultiplyAdds"], "Pre-forward compute/capture/working admission denied", "CAPACITY")
    return {"forwardTokens": 3*t, "captureBytesBound": capture, "workingBytesBound": working,
            "multiplyAddsBound": multiply, "baseParameterElements": parameter_elements, "basis": "conservative_actual_parameter_inventory_dense_qwen_prefill_planning_not_measured_flops_or_peak_memory"}


def publish_capture(work_root, arrays, maximum):
    import numpy as np
    root = Path(work_root)
    require(root.is_absolute() and not root.is_symlink() and root.is_dir() and root.resolve() == root and
            root.stat().st_mode & 0o077 == 0, "Private existing capture root required", "IDENTITY")
    # Bound uncompressed NPZ headers/ZIP overhead BEFORE opening/writing.
    bound = sum(value.nbytes + 1024 for value in arrays.values()) + 65536
    require(bound <= maximum, "Capture publication reservation too small", "CAPACITY")
    path = root / ("peft-capture-" + str(uuid.uuid4()) + ".npz")
    fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
    try:
        with os.fdopen(fd, "wb") as output:
            np.savez(output, **arrays)
            output.flush(); os.fsync(output.fileno())
        actual = base.hash_file(path, maximum)
        return {"path": str(path), **actual, "mediaType": "application/x-npz"}
    except BaseException:
        try: path.unlink()
        except FileNotFoundError: pass
        raise


class Runtime(peft.Runtime):
    def __init__(self):
        super().__init__()
        self.used_keys = set()
        self.forward_confirmed = False

    def load(self, payload):
        admit_base_sources(payload)
        result = super().load(payload)
        no_target_aliases(self.peft_state, self.torch)
        result.update(implementation=VERSION, ownership=ownership(), peftWorkerSha256=PEFT_SHA, baseWorkerSha256=BASE_SHA,
                      supportedAssets=["standard_peft_unmerged_lora"], nativeCapabilityAuthority=False)
        return result

    def consume(self, key):
        require(type(key) is str and base.SHA.fullmatch(key) and key not in self.used_keys and len(self.used_keys) < 10000,
                "Missing/replayed caller cache key", "IDENTITY")
        self.used_keys.add(key)  # No refund after failure/reset.

    def confirm_forward(self, payload):
        self.ready(); fields(payload, ("pid", "startIdentity"))
        actual = base.placement(self.gpu_uuid)
        require(payload == {"pid": actual["pid"], "startIdentity": actual["startIdentity"]}, "First-forward gate placement changed", "PLACEMENT")
        self.forward_confirmed = True
        return {"confirmed": True, "placement": actual, "nativeCapabilityAuthority": False}

    def generate(self, payload):
        self.ready(); require(self.forward_confirmed, "Explicit host first-forward confirmation required", "PLACEMENT")
        self.consume(payload.get("cacheKeyDigest"))
        config = getattr(self.model.config, "text_config", self.model.config)
        require(getattr(config, "model_type", None) == "qwen3_5_text" and not getattr(config, "num_experts", 0), "Unsupported generation architecture", "UNSUPPORTED")
        prompt = self.tokenize({"messages": payload["messages"]})["tokenIds"]
        maximum = integer(payload["maxTokens"], 1, self.limits["maxOutputTokens"])
        geometry = {"layers": config.num_hidden_layers, "hiddenSize": config.hidden_size,
                    "intermediateSize": config.intermediate_size, "attentionHeads": config.num_attention_heads,
                    "vocabSize": config.vocab_size}
        # Bound every cold autoregressive forward using the largest prefix;
        # generation retains no NPZ, but the larger profile working bound is safe.
        factor = (maximum + 2) // 3
        bounds = admission(self.peft_state, len(prompt) + maximum, 1, geometry,
                           {"maxForwardTokens": 3*(len(prompt)+maximum), "maxCaptureBytes": 268435456,
                            "maxWorkingBytes": self.limits["maxWorkingBytes"],
                            "maxMultiplyAdds": self.limits["maxMultiplyAdds"] // factor})
        bounds["multiplyAddsBound"] *= factor
        bounds["forwardTokens"] = maximum*(len(prompt)+maximum)
        bounds["maxForwardCalls"] = maximum
        self.charge(bounds["multiplyAddsBound"])
        result = super().generate(payload)
        result["native"]["generationAdmission"] = bounds
        # Raw native base binding is preserved; composition is a distinct fact.
        result["native"]["implementation"] = VERSION
        result["native"]["ownership"] = ownership()
        result["native"]["peftComposition"] = {"adapterModelDigest": self.peft_state["approved"]["manifest"]["modelDigest"],
                "nativeBaseBinding": self.binding, "parameterMergePerformed": False}
        return result

    def profile(self, payload):
        if type(payload) is dict and payload.get("mode") == "confirm_first_forward":
            fields(payload, ("mode", "pid", "startIdentity"))
            return self.confirm_forward({key: payload[key] for key in ("pid", "startIdentity")})
        self.ready(); require(self.forward_confirmed, "Explicit host first-forward confirmation required", "PLACEMENT")
        fields(payload, ("mode", "messages", "expectedTokenIds", "positions", "cacheKeyDigest", "workRoot", "geometry", "limits"))
        require(payload["mode"] == "unmerged_linear_conformance", "Unknown observation mode", "UNSUPPORTED")
        require(self.last_reset is not None and self.last_reset.get("peft", {}).get("enabled") is False, "Explicit disabled reset required", "RESET")
        tokens = self.tokenize({"messages": payload["messages"]})["tokenIds"]
        require(tokens == payload["expectedTokenIds"], "Teacher-forced token identity mismatch", "IDENTITY")
        positions = payload["positions"]
        require(type(positions) is list and 0 < len(positions) <= 64 and positions == sorted(set(positions)) and
                all(type(p) is int and 0 <= p < len(tokens) for p in positions), "Invalid capture positions")
        # Only the reviewed dense Qwen3.5 model is admitted by this bound.
        config = self.model.config
        config = getattr(config, "text_config", config)
        require(getattr(config, "model_type", None) == "qwen3_5_text" and not getattr(config, "num_experts", 0), "Unsupported profile architecture", "UNSUPPORTED")
        actual_geometry = {"layers": config.num_hidden_layers, "hiddenSize": config.hidden_size,
                           "intermediateSize": config.intermediate_size, "attentionHeads": config.num_attention_heads,
                           "vocabSize": config.vocab_size}
        require(base.same_json(payload["geometry"], actual_geometry), "Frozen model compute geometry changed", "IDENTITY")
        root = Path(payload["workRoot"])
        require(root.is_absolute() and not root.is_symlink() and root.is_dir() and root.resolve() == root and root.stat().st_mode & 0o077 == 0,
                "Private existing capture root required before forward", "IDENTITY")
        bounds = admission(self.peft_state, len(tokens), len(positions), payload["geometry"], payload["limits"])
        require(payload["limits"]["maxWorkingBytes"] <= self.limits["maxWorkingBytes"] and
                payload["limits"]["maxMultiplyAdds"] <= self.limits["maxMultiplyAdds"], "Profile exceeds load budget", "CAPACITY")
        self.consume(payload["cacheKeyDigest"])
        self.charge(bounds["multiplyAddsBound"])
        torch = self.torch
        ids = torch.tensor([tokens], device="cuda:0", dtype=torch.long)
        def forward():
            return self.model(input_ids=ids, attention_mask=torch.ones_like(ids), use_cache=False, logits_to_keep=1)
        try:
            arrays, facts = capture_prefills(self.peft_state, torch, tokens, positions, forward, payload["limits"]["maxCaptureBytes"], self.reset)
            torch.cuda.synchronize()
            capture = publish_capture(payload["workRoot"], arrays, payload["limits"]["maxCaptureBytes"])
            reset = self.reset()
            return {"schema": "omnius.nol.peft-linear-capture.v1", "implementation": VERSION, "ownership": ownership(),
                    "binding": self.binding, "adapterModelDigest": self.peft_state["approved"]["manifest"]["modelDigest"],
                    "workerSha256": base.hash_file(Path(__file__).resolve())["sha256"], "peftWorkerSha256": PEFT_SHA,
                    "baseWorkerSha256": BASE_SHA, "cacheKeyDigest": payload["cacheKeyDigest"],
                    "capture": capture, "admission": bounds, "resetAfter": reset, **facts}
        except BaseException:
            try: self.reset()
            except BaseException: self.poisoned = True
            raise


def serve():
    sys.stdout = sys.stderr
    require(0 < base.MAX_FRAME <= 67108864 and all(base.OWNER.values()), "Private ownership and frame bounds required")
    runtime, sequence = Runtime(), 0
    try:
        while True:
            header = base.read_exact(sys.stdin.buffer, 4)
            if header is None: break
            size = struct.unpack(">I", header)[0]
            require(0 < size <= base.MAX_FRAME, "Frame bound exceeded", "CAPACITY")
            raw = base.read_exact(sys.stdin.buffer, size)
            require(raw is not None, "Truncated request")
            request = base.parse_json(raw.decode("utf-8", "strict"))
            fields(request, ("schema", "kind", "sessionId", "jobId", "ownerId", "generation", "id", "action", "payload"))
            require(request["schema"] == base.PROTOCOL and request["kind"] == "request" and
                    all(request[key] == value for key, value in base.OWNER.items()) and request["id"] == sequence + 1, "Owner/generation/sequence mismatch", "IDENTITY")
            sequence += 1
            response = {key: request[key] for key in ("schema", "sessionId", "jobId", "ownerId", "generation", "id", "action")}
            response["kind"] = "response"
            closing = False
            try:
                action, payload = request["action"], request["payload"]
                if action == "hello":
                    fields(payload, ())
                    result = {"protocol": base.PROTOCOL, "workerVersion": VERSION, "ownership": ownership(),
                              "workerSha256": base.hash_file(Path(__file__).resolve())["sha256"], "peftWorkerSha256": PEFT_SHA,
                              "baseWorkerSha256": BASE_SHA, "pid": os.getpid(), "loaded": runtime.loaded,
                              "torchImported": "torch" in sys.modules, "nativeCapabilityAuthority": False}
                elif action in ("inspect", "load", "tokenize", "generate", "profile"):
                    result = getattr(runtime, action)(payload)
                elif action == "confirm_placement": result = runtime.confirm(payload)
                elif action in ("reset", "close"):
                    fields(payload, ()); result = getattr(runtime, action)(); closing = action == "close"
                else: raise Rejected("UNSUPPORTED", "Unknown validation worker action")
                response["result"] = result
            except Rejected as error:
                closing = error.fatal or action in ("load", "close", "reset") or runtime.poisoned
                response["error"] = {"code": error.code, "message": str(error)[:4096], "fatal": closing}
            except BaseException as error:
                base.private_diagnostic(error); closing = True
                response["error"] = {"code": "RUNTIME_FAILURE", "message": type(error).__name__, "fatal": True}
            encoded = json.dumps(response, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode()
            require(len(encoded) <= base.MAX_FRAME, "Response frame bound exceeded", "CAPACITY")
            base.WIRE_OUT.write(struct.pack(">I", len(encoded)) + encoded); base.WIRE_OUT.flush()
            if closing: break
    finally:
        runtime.close()


if __name__ == "__main__":
    try: serve()
    except BaseException as error:
        base.private_diagnostic(error)
        sys.exit(1)
