#!/usr/bin/env python3
"""Job-owned NOL compute worker. Stdlib-only until an explicit zero-token load.

Transport: uint32 big-endian UTF-8 JSON frames. Stdout is never a log stream.
The host owns GPU leasing, artifact publication, native receipts and grading.
This process reports observed facts; it cannot attest broker custody by itself.
"""
import contextlib
import gc
import hashlib
import importlib.metadata
import json
import math
import os
from pathlib import Path
import re
import struct
import subprocess
import sys
import traceback

PROTOCOL = "omnius.nol.worker.v1"
WORKER_VERSION = "omnius.nol.transformers.v1"
MAX_FRAME = int(os.environ.get("OMNIUS_NOL_MAX_FRAME_BYTES", "1048576"))
WIRE_OUT = sys.stdout.buffer
OWNER = {"sessionId": os.environ.get("OMNIUS_NOL_WORKER_SESSION", ""),
         "jobId": os.environ.get("OMNIUS_NOL_JOB_ID", ""),
         "ownerId": os.environ.get("OMNIUS_NOL_OWNER_ID", ""),
         "generation": int(os.environ.get("OMNIUS_NOL_GENERATION", "0"))}
UUID = re.compile(r"^GPU-[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$")
SHA = re.compile(r"^[a-f0-9]{64}$")
RUNTIME_OPS = {"NEURON_ADD", "NEURON_GAIN", "NEURON_REPLACE", "RESIDUAL_ADD", "DIRECTION_PROJECT"}
PARAMETER_OPS = {"WEIGHT_ADD", "WEIGHT_GAIN", "WEIGHT_REPLACE", "WEIGHT_PROJECT", "LOW_RANK_UPDATE", "WEIGHT_MERGE"}
NATIVE_CODE = re.compile(r"(?:\.py|\.pyc|\.pyd|\.dll|\.dylib|\.ptx|\.cubin|\.so(?:\.[0-9]+)*)$", re.IGNORECASE)


class Rejected(Exception):
    def __init__(self, code, message, fatal=False):
        super().__init__(message)
        self.code, self.fatal = code, fatal


def require(condition, message, code="INVALID_REQUEST"):
    if not condition:
        raise Rejected(code, message)


def fields(value, required, optional=()):
    require(type(value) is dict and set(required) <= value.keys() and not (value.keys() - set(required) - set(optional)), "Unknown or missing fields")


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


def number(value):
    require(type(value) in (int, float) and math.isfinite(value), "Nonfinite numeric value")
    return value


def private_diagnostic(error):
    sys.stderr.write("".join(traceback.format_exception(type(error), error, error.__traceback__, limit=12))[-8192:] + "\n")
    sys.stderr.flush()


def unique_object(pairs):
    out = {}
    for key, value in pairs:
        require(key not in out, "Duplicate JSON object property")
        out[key] = value
    return out


def parse_json(raw):
    return json.loads(raw, object_pairs_hook=unique_object,
                      parse_int=lambda text: -0.0 if text == "-0" else int(text),
                      parse_constant=lambda _: (_ for _ in ()).throw(Rejected("INVALID_JSON", "Nonfinite JSON number")))


def canonical_simple(value):
    """Only used for runtime/file inventories containing strings and integers.
    Scientific records arrive with their exact host-canonical identity bodies.
    """
    return json.dumps(value, sort_keys=True, ensure_ascii=False, separators=(",", ":"), allow_nan=False)


def digest_bytes(value):
    return hashlib.sha256(value).hexdigest()


def hash_file(path, maximum=None):
    path = Path(path)
    require(path.is_file() and not path.is_symlink(), "Input must be a regular nonsymlink file")
    before = path.stat()
    require(maximum is None or before.st_size <= maximum, "File byte budget exceeded", "CAPACITY")
    h, size = hashlib.sha256(), 0
    with path.open("rb") as stream:
        opened = os.fstat(stream.fileno())
        require((opened.st_dev, opened.st_ino) == (before.st_dev, before.st_ino), "File identity changed")
        while True:
            block = stream.read(1024 * 1024)
            if not block:
                break
            h.update(block)
            size += len(block)
        after = os.fstat(stream.fileno())
    require((before.st_size, before.st_mtime_ns, before.st_ctime_ns) ==
            (size, after.st_mtime_ns, after.st_ctime_ns), "File changed while hashing")
    return {"sha256": h.hexdigest(), "sizeBytes": size}


def safe_path(root, relative):
    require(type(relative) is str and relative and not relative.startswith("/") and "\\" not in relative and "\0" not in relative,
            "Invalid relative file path")
    require(all(part not in ("", ".", "..") for part in relative.split("/")), "Invalid relative path component")
    root = Path(root).resolve(strict=True)
    current = root
    for part in relative.split("/"):
        current = current / part
        require(not current.is_symlink(), "Symlinks are not accepted in frozen input paths")
    require(current.resolve(strict=True).is_relative_to(root), "Input escaped its frozen root")
    return current


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


def verify_identity(raw, expected, expected_hash):
    require(type(raw) is str and SHA.fullmatch(expected_hash or ""), "Missing exact identity body")
    require(digest_bytes(raw.encode("utf-8")) == expected_hash and same_json(parse_json(raw), expected), "Frozen identity digest/body mismatch", "IDENTITY")


def verify_model(root, model, identity):
    require(model.get("schema") == "omnius.nol.model.v1", "Unsupported model manifest")
    require(model.get("format") in ("hf", "safetensors", "pytorch"), "Unsupported checkpoint format", "UNSUPPORTED")
    body = {key: value for key, value in model.items() if key not in ("id", "modelDigest")}
    verify_identity(identity["modelJson"], body, model["modelDigest"])
    verify_identity(identity["tensorSitesJson"], model["tensorSites"], model["architectureMapDigest"])
    tokenizer_files, paths = [], set()
    for part in model["files"]:
        fields(part, ("path", "role", "artifact"))
        require(part["path"] not in paths, "Duplicate checkpoint file")
        paths.add(part["path"])
        actual = hash_file(safe_path(root, part["path"]))
        require(actual["sha256"] == part["artifact"]["sha256"] and actual["sizeBytes"] == part["artifact"]["sizeBytes"], "Checkpoint file digest mismatch", "IDENTITY")
        if part["role"] == "tokenizer":
            tokenizer_files.append({"path": part["path"], **actual})
    require("config.json" in paths and any(part["role"] == "weights" for part in model["files"]), "Checkpoint lacks configuration or weights")
    verify_identity(identity["tokenizerJson"], tokenizer_files, model["tokenizerDigest"])
    # A loader must not read undeclared config/tokenizer/shard files, including
    # malicious local code or index files referencing an external checkpoint.
    actual_paths = set()
    for path in Path(root).rglob("*"):
        require(not path.is_symlink(), "Checkpoint contains a symlink")
        if path.is_file():
            actual_paths.add(path.relative_to(Path(root)).as_posix())
    require(actual_paths == paths, "Checkpoint has undeclared or missing files", "IDENTITY")
    config = parse_json(safe_path(root, "config.json").read_text("utf-8"))
    text = config.get("text_config", {})
    require(config.get("model_type") == "qwen3_5" and config.get("architectures") == ["Qwen3_5ForConditionalGeneration"] and
            text.get("num_hidden_layers") == 32 and text.get("hidden_size") == 2560 and text.get("intermediate_size") == 9216 and
            text.get("vocab_size") == 248320 and text.get("hidden_act") == "silu" and not text.get("num_experts") and
            "quantization_config" not in config and model["quantization"]["method"] == "none",
            "Only the approved dense Qwen3.5-4B layout is supported", "UNSUPPORTED")
    for index_name in ("model.safetensors.index.json", "pytorch_model.bin.index.json"):
        if index_name in paths:
            index = parse_json(safe_path(root, index_name).read_text("utf-8"))
            require(set(index["weight_map"].values()) <= paths, "Shard index references an undeclared file", "IDENTITY")
    require(model["architecture"]["layers"] == 32 and model["architecture"]["hiddenSize"] == 2560, "Manifest architecture differs from checkpoint")
    return config


def runtime_info(lock_path=None, hash_runtime=False, max_bytes=34359738368):
    packages, code = {}, []
    spent = 0
    for distribution in sorted(importlib.metadata.distributions(), key=lambda item: item.metadata.get("Name", "").lower()):
        name = distribution.metadata.get("Name", "")
        if not name:
            continue
        packages[name] = distribution.version
        if hash_runtime:
            entries = []
            for relative in sorted(distribution.files or [], key=str):
                if not NATIVE_CODE.search(str(relative)):
                    continue
                path = Path(distribution.locate_file(relative))
                # Symlinked libraries are rejected rather than silently hashing
                # a different native installation than the isolated runtime.
                actual = hash_file(path, max_bytes - spent)
                spent += actual["sizeBytes"]
                entries.append({"path": str(relative), **actual})
            code.append({"name": name, "digest": digest_bytes(canonical_simple(entries).encode()), "files": len(entries)})
    info = {"schema": "omnius.nol.transformers-build.v1", "workerVersion": WORKER_VERSION,
            "workerSha256": hash_file(Path(__file__).resolve())["sha256"], "pythonVersion": sys.version.split()[0],
            "packages": packages, "packageCode": code,
            "pythonExecutableSha256": hash_file(Path(sys.executable).resolve())["sha256"]}
    if lock_path:
        info["dependencyLockDigest"] = hash_file(lock_path, 1048576)["sha256"]
    return info


def inspect_checkpoint(root):
    """Safe header inspection only. Never imports a tensor/model framework."""
    root = Path(root).resolve(strict=True)
    config_path = safe_path(root, "config.json")
    require(config_path.stat().st_size <= 1048576, "Configuration byte budget exceeded", "CAPACITY")
    config = parse_json(config_path.read_text("utf-8"))
    tensors, shard_names, total_header = {}, set(), 0
    dtype_bytes = {"F64": 8, "F32": 4, "F16": 2, "BF16": 2, "I64": 8, "I32": 4, "I16": 2, "I8": 1, "U8": 1, "BOOL": 1}
    dtype_names = {"F64": "f64", "F32": "f32", "F16": "f16", "BF16": "bf16", "I64": "i64", "I32": "i32", "I16": "i16", "I8": "i8", "U8": "u8", "BOOL": "bool"}
    for path in sorted(root.rglob("*.safetensors")):
        relative = path.relative_to(root).as_posix()
        path = safe_path(root, relative)
        shard_names.add(relative)
        size = path.stat().st_size
        with path.open("rb") as stream:
            prefix = stream.read(8)
            require(len(prefix) == 8, "Truncated safetensors length")
            length = struct.unpack("<Q", prefix)[0]
            total_header += length
            require(2 <= length <= 16777216 and total_header <= 33554432 and 8 + length <= size,
                    "Safetensors header byte budget or boundary exceeded", "CAPACITY")
            header = parse_json(stream.read(length).decode("utf-8", "strict"))
        require(type(header) is dict, "Safetensors header must be an object")
        spans = []
        for name, tensor in header.items():
            if name == "__metadata__":
                require(type(tensor) is dict and all(type(value) is str for value in tensor.values()), "Invalid safetensors metadata")
                continue
            require(name not in tensors and type(name) is str and len(name) <= 4096, "Duplicate or invalid checkpoint parameter")
            fields(tensor, ("dtype", "shape", "data_offsets"))
            dtype, shape, offsets = tensor["dtype"], tensor["shape"], tensor["data_offsets"]
            require(dtype in dtype_bytes and type(shape) is list and len(shape) <= 8, "Unsupported tensor header dtype/shape", "UNSUPPORTED")
            count = 1
            for dimension in shape:
                count *= integer(dimension, 1)
                require(count <= 9007199254740991, "Unsafe tensor element count")
            require(type(offsets) is list and len(offsets) == 2, "Invalid tensor data offsets")
            begin, end = (integer(offset) for offset in offsets)
            require(begin <= end <= size - 8 - length and end - begin == count * dtype_bytes[dtype], "Tensor offsets or element bytes are out of bounds")
            spans.append((begin, end))
            role = "bias" if name.endswith((".bias", "_bias")) else "embedding" if "embed_tokens" in name else "ffn" if ".mlp." in name else "attention" if "attn" in name else "norm" if "norm" in name else "router" if "router" in name else "other"
            item = {"id": "parameter:" + digest_bytes(name.encode())[:24], "kind": "parameter", "name": name,
                    "shape": shape, "dtype": dtype_names[dtype], "role": role}
            layer = re.search(r"(?:^|\.)language_model\.layers\.([0-9]+)\.", name)
            if layer:
                item["layer"] = int(layer.group(1))
            tensors[name] = {"site": item, "shard": relative}
        previous = 0
        for begin, end in sorted(spans):
            require(begin == previous, "Safetensors data has overlapping or unclaimed ranges")
            previous = end
        require(previous == size - 8 - length, "Safetensors data has an unclaimed tail")
    index_path = root / "model.safetensors.index.json"
    if index_path.exists():
        require(index_path.stat().st_size <= 16777216 and not index_path.is_symlink(), "Invalid safetensors index")
        index = parse_json(index_path.read_text("utf-8"))
        require(type(index.get("weight_map")) is dict and index["weight_map"].keys() == tensors.keys(), "Shard index omits or invents tensors")
        require(all(type(name) is str and name in shard_names and tensors[key]["shard"] == name for key, name in index["weight_map"].items()), "Shard index references missing or inconsistent files")
    elif len(shard_names) > 1:
        raise Rejected("IDENTITY", "A sharded safetensors checkpoint requires its exact index")
    text = config.get("text_config", {})
    approved = config.get("model_type") == "qwen3_5" and text.get("num_hidden_layers") == 32 and text.get("hidden_size") == 2560 and text.get("intermediate_size") == 9216
    sites = [item["site"] for _, item in sorted(tensors.items())]
    if approved:
        for layer in range(32):
            sites.extend([{"id": f"mlp:{layer}", "kind": "mlp_intermediate", "layer": layer, "width": 9216},
                          {"id": f"residual:{layer}", "kind": "residual", "layer": layer, "width": 2560}])
    require(len(sites) <= 100000, "Tensor site inventory exceeds its bound", "CAPACITY")
    return {"config": config, "parameters": [item["site"] for _, item in sorted(tensors.items())], "tensorSites": sites,
            "parameterInventory": "safetensors_headers" if shard_names else "unavailable_no_safe_header",
            "approvedDenseQwen35": approved}


def placement(uuid):
    rows = subprocess.run(["nvidia-smi", "--query-compute-apps=pid,gpu_uuid,used_memory", "--format=csv,noheader,nounits"],
                          check=True, capture_output=True, timeout=10, text=True)
    require(len(rows.stdout.encode()) <= 1048576, "Placement inventory exceeds its byte bound")
    resident = [row.split(",") for row in rows.stdout.splitlines() if row.split(",")[0].strip() == str(os.getpid())]
    require(resident and all(row[1].strip() == uuid and float(row[2].strip()) > 0 for row in resident), "Actual CUDA residency differs from the reserved UUID", "PLACEMENT")
    stat = Path(f"/proc/{os.getpid()}/stat").read_text()
    return {"pid": os.getpid(), "gpuUuid": uuid, "startIdentity": stat[stat.rfind(")") + 2:].split()[19]}


class Runtime:
    def __init__(self):
        self.model = self.tokenizer = self.torch = None
        self.loaded = self.confirmed = False
        self.hooks, self.backups, self.edited = [], {}, {}
        self.binding = self.manifest = self.limits = None
        self.artifact_root = self.gpu_uuid = None
        self.sequence_length = self.prompt_length = 0
        self.cache_key = None
        self.reset_epoch = 0
        self.working_bytes = self.arithmetic = 0
        self.last_reset = None

    def inspect(self, payload):
        fields(payload, (), ("dependencyLockPath", "hashRuntime", "maxRuntimeBytes", "checkpointRoot"))
        build = runtime_info(payload.get("dependencyLockPath"), payload.get("hashRuntime") is True,
                             integer(payload.get("maxRuntimeBytes", 34359738368), 1))
        result = {"runtimeBuild": build, "runtimeBuildDigest": digest_bytes(canonical_simple(build).encode())}
        if "checkpointRoot" in payload:
            result["checkpoint"] = inspect_checkpoint(payload["checkpointRoot"])
        return result

    def load(self, payload):
        fields(payload, ("checkpointRoot", "artifactRoot", "model", "binding", "gpuUuid", "dtype", "identity", "dependencyLockPath", "limits"))
        require(not self.loaded, "Worker is already loaded")
        uuid = payload["gpuUuid"]
        require(type(uuid) is str and UUID.fullmatch(uuid) and os.environ.get("CUDA_VISIBLE_DEVICES") == uuid,
                "Explicit exact single-GPU CUDA scope is required", "PLACEMENT")
        fields(payload["limits"], ("maxInputTokens", "maxOutputTokens", "maxTensorBytes", "maxWorkingBytes", "maxMultiplyAdds"))
        limits = {key: integer(value, 1) for key, value in payload["limits"].items()}
        identity = payload["identity"]
        fields(identity, ("modelJson", "tensorSitesJson", "tokenizerJson", "runtimeBuildJson"))
        root = Path(payload["checkpointRoot"]).resolve(strict=True)
        verify_model(root, payload["model"], identity)
        build = runtime_info(payload["dependencyLockPath"], True)
        verify_identity(identity["runtimeBuildJson"], build, payload["binding"]["runtimeBuildDigest"])
        require(build["dependencyLockDigest"] == payload["binding"]["dependencyLockDigest"], "Dependency lock mismatch", "IDENTITY")
        binding, manifest = payload["binding"], payload["model"]
        require(binding["schema"] == "omnius.nol.runtime.v1" and binding["backend"] == "transformers" and
                all(binding[key] == manifest[key] for key in ("modelDigest", "architectureMapDigest", "tokenizerDigest", "chatTemplateDigest")),
                "Runtime/model binding mismatch", "IDENTITY")
        require(limits["maxInputTokens"] + limits["maxOutputTokens"] <= binding["contextLimit"] <= manifest["contextLength"], "Context budget mismatch", "CAPACITY")
        require(payload["dtype"] in ("float32", "float16", "bfloat16"), "Unsupported compute dtype", "UNSUPPORTED")
        # No import, initialization or model load happens above this boundary.
        os.environ["HF_HUB_OFFLINE"] = "1"
        os.environ["TRANSFORMERS_OFFLINE"] = "1"
        os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
        os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":4096:8")
        import torch
        from transformers import AutoTokenizer, Qwen3_5ForConditionalGeneration
        require(torch.cuda.is_available() and torch.cuda.device_count() == 1, "CUDA scope is unavailable; CPU fallback is prohibited", "PLACEMENT")
        capability = torch.cuda.get_device_capability(0)
        require(capability >= ((8, 0) if payload["dtype"] == "bfloat16" else (7, 0)), "GPU capability is below the selected runtime minimum", "PLACEMENT")
        torch.use_deterministic_algorithms(True)
        self.torch, self.limits, self.binding, self.manifest = torch, limits, binding, manifest
        self.gpu_uuid, self.artifact_root = uuid, Path(payload["artifactRoot"]).resolve(strict=True)
        try:
            self.tokenizer = AutoTokenizer.from_pretrained(str(root), local_files_only=True, trust_remote_code=False)
            template = self.tokenizer.get_chat_template()
            require(digest_bytes(template.encode()) == manifest["chatTemplateDigest"], "Effective chat template differs from frozen identity", "IDENTITY")
            for name, token_id in manifest["specialTokens"].items():
                actual = getattr(self.tokenizer, name + "_token_id", None) if name in ("bos", "eos", "pad", "unk", "sep", "cls", "mask") else self.tokenizer.get_vocab().get(name)
                require(actual == token_id, "Special-token mapping differs from frozen identity", "IDENTITY")
            self.model = Qwen3_5ForConditionalGeneration.from_pretrained(str(root), local_files_only=True, trust_remote_code=False,
                         weights_only=True, dtype=getattr(torch, payload["dtype"]), device_map={"": "cuda:0"}, attn_implementation="eager").eval()
            require(all(parameter.device.type == "cuda" and parameter.device.index == 0 for parameter in self.model.parameters()),
                    "Loaded parameters escaped the exact CUDA device", "PLACEMENT")
            self.layers = self.model.model.language_model.layers
            self.parameters = dict(self.model.named_parameters(remove_duplicate=False))
            self.sites = {}
            for site in manifest["tensorSites"]:
                require(site["id"] not in self.sites, "Duplicate tensor-site identity")
                if site["kind"] == "mlp_intermediate":
                    layer = self.layers[integer(site["layer"], 0, len(self.layers) - 1)]
                    require(type(layer.mlp).__name__ == "Qwen3_5MLP" and layer.mlp.down_proj.in_features == site["width"], "Unsupported MLP hook layout", "UNSUPPORTED")
                elif site["kind"] == "residual":
                    layer = self.layers[integer(site["layer"], 0, len(self.layers) - 1)]
                    require(type(layer).__name__ == "Qwen3_5DecoderLayer" and layer.hidden_size == site["width"], "Unsupported residual hook layout", "UNSUPPORTED")
                elif site["kind"] == "parameter":
                    parameter = self.parameters.get(site["name"])
                    dtype_name = str(parameter.dtype).removeprefix("torch.") if parameter is not None else ""
                    alias = {"float32": "f32", "float64": "f64", "float16": "f16", "bfloat16": "bf16"}.get(dtype_name)
                    require(parameter is not None and list(parameter.shape) == site["shape"] and alias == site["dtype"], "Parameter site shape/dtype mismatch", "IDENTITY")
                else:
                    continue  # No capability is issued for this site kind.
                self.sites[site["id"]] = site
            torch.cuda.synchronize()
            observed = placement(uuid)
            self.loaded = True
            self.reset_epoch = 1
            return {"binding": binding, "placement": observed, "confirmed": False,
                    "implementation": WORKER_VERSION, "supportedRuntimeOperations": sorted(RUNTIME_OPS),
                    "supportedParameterOperations": sorted(PARAMETER_OPS - {"WEIGHT_MERGE"}), "siteIds": sorted(self.sites),
                    "operationRestrictions": {"units": "raw_only", "neuronReplacement": "scalar_only", "weightMerge": "unsupported_source_reader", "mixedRuntimeParameterRecipes": "unsupported"},
                    "usesCache": False, "tokenScopeBasis": "edited_computation_positions",
                    "firstGeneratedTokenUsesPromptComputation": True}
        except BaseException as error:
            private_diagnostic(error)
            self.close()
            raise Rejected("LOAD_FAILED", type(error).__name__, True) from error

    def confirm(self, payload):
        fields(payload, ("pid", "gpuUuid", "startIdentity"))
        require(self.loaded and same_json(payload, placement(self.gpu_uuid)), "Host placement confirmation does not match the actual resident worker", "PLACEMENT")
        self.confirmed = True
        return {"confirmed": True, "binding": self.binding, "placement": payload}

    def ready(self):
        require(self.loaded and self.confirmed, "Model requires actual host-confirmed CUDA placement", "PLACEMENT")

    def tokenize(self, payload):
        self.ready()
        fields(payload, ("messages",))
        messages = payload["messages"]
        require(type(messages) is list and 0 < len(messages) <= 1000, "Invalid message inventory")
        for message in messages:
            fields(message, ("role", "content"))
            require(message["role"] in ("system", "user", "assistant") and type(message["content"]) is str, "Only plain text messages are supported", "UNSUPPORTED")
        ids = self.tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True,
                                                 enable_thinking=False, return_dict=False, return_tensors=None)
        require(type(ids) is list and all(type(token) is int and 0 <= token < 248320 for token in ids),
                "Tokenizer must return a flat integer token sequence", "IDENTITY")
        require(0 < len(ids) <= self.limits["maxInputTokens"], "Prompt exceeds the frozen native token budget", "CAPACITY")
        return {"tokenIds": ids, "promptTokens": len(ids), "chatTemplateDigest": self.binding["chatTemplateDigest"]}

    def charge(self, amount):
        self.arithmetic += integer(amount)
        require(self.arithmetic <= self.limits["maxMultiplyAdds"], "Arithmetic budget exceeded", "CAPACITY")

    def tensor(self, ref):
        fields(ref, ("artifact", "shape", "dtype"))
        artifact = ref["artifact"]
        fields(artifact, ("uri", "sha256", "sizeBytes", "mediaType"))
        require(SHA.fullmatch(artifact["sha256"]) and artifact["uri"] == "omnius-artifact://sha256/" + artifact["sha256"], "Invalid tensor artifact identity")
        require(artifact["mediaType"] == "application/json", "Only bounded NOL JSON tensor operands are supported", "UNSUPPORTED")
        require(artifact["sizeBytes"] <= self.limits["maxTensorBytes"], "Tensor artifact budget exceeded", "CAPACITY")
        path = safe_path(self.artifact_root, artifact["sha256"][:2] + "/" + artifact["sha256"])
        require(hash_file(path, self.limits["maxTensorBytes"]) == {"sha256": artifact["sha256"], "sizeBytes": artifact["sizeBytes"]}, "Tensor operand bytes changed", "IDENTITY")
        data = parse_json(path.read_text("utf-8"))
        fields(data, ("schema", "shape", "dtype", "data"))
        require(data["schema"] == "omnius.nol.tensor.v1" and data["shape"] == ref["shape"] and data["dtype"] == ref["dtype"], "Tensor metadata mismatch", "IDENTITY")
        count = 1
        for dimension in data["shape"]:
            count *= integer(dimension, 1)
        require(type(data["data"]) is list and len(data["data"]) == count, "Tensor element count mismatch")
        for value in data["data"]:
            number(value)
        dtype = {"f32": self.torch.float32, "f64": self.torch.float64, "f16": self.torch.float16, "bf16": self.torch.bfloat16}.get(data["dtype"])
        require(dtype is not None, "Unsupported tensor operand dtype", "UNSUPPORTED")
        self.working_bytes += count * 16
        require(self.working_bytes <= self.limits["maxWorkingBytes"], "Tensor working budget exceeded", "CAPACITY")
        value = self.torch.tensor(data["data"], device="cuda:0", dtype=dtype).reshape(data["shape"])
        require(bool(self.torch.isfinite(value).all()), "Tensor data is not finite in its declared dtype")
        return value.to(self.torch.float64)

    def positions(self, scope):
        fields(scope, ("kind",), ("positions",))
        kind = scope["kind"]
        require(kind in ("all", "prompt", "generated", "positions"), "Unknown computation token scope", "UNSUPPORTED")
        if kind == "all":
            return list(range(self.sequence_length))
        if kind == "prompt":
            return list(range(min(self.prompt_length, self.sequence_length)))
        if kind == "generated":
            return list(range(self.prompt_length, self.sequence_length))
        values = scope.get("positions")
        require(type(values) is list and len(values) == len(set(values)), "Invalid computation position mask")
        for value in values:
            integer(value)
        return [value for value in values if value < self.sequence_length]

    def reset(self):
        if not self.loaded:
            return {"loaded": False, "hooks": 0, "restoredParameters": [], "retainedCache": False}
        for handle in self.hooks:
            handle.remove()
        self.hooks = []
        restored = []
        with self.torch.no_grad():
            for name, backup in self.backups.items():
                parameter = self.parameters[name]
                parameter.copy_(backup)
                require(self.torch.equal(parameter.contiguous().view(self.torch.uint8), backup.contiguous().view(self.torch.uint8)), "Parameter restoration failed", "RESET")
                restored.append(name)
        self.backups.clear()
        self.edited = {}
        self.cache_key = None
        self.sequence_length = self.prompt_length = 0
        self.working_bytes = self.arithmetic = 0
        self.torch.cuda.synchronize()
        self.torch.cuda.empty_cache()
        self.reset_epoch += 1
        self.last_reset = {"loaded": True, "binding": self.binding, "epoch": self.reset_epoch, "hooks": 0,
                           "restoredParameters": restored, "retainedCache": False, "placement": placement(self.gpu_uuid)}
        return self.last_reset

    def basis(self, ref, dimension):
        basis = self.tensor(ref)
        if basis.ndim == 1:
            basis = basis.unsqueeze(0)
        require(basis.ndim == 2 and basis.shape[1] == dimension and basis.shape[0] <= dimension,
                "Projection basis dimensions differ from canonical row basis")
        self.charge((2 * dimension + 1) * basis.shape[0] ** 2)
        gram = basis @ basis.T
        require(self.torch.allclose(gram, self.torch.eye(basis.shape[0], dtype=basis.dtype, device=basis.device), atol=1e-8, rtol=0), "Projection basis is not orthonormal")
        return basis

    def parameter_edit(self, op, site):
        parameter = self.parameters[site["name"]]
        if site["name"] not in self.backups:
            self.working_bytes += parameter.numel() * parameter.element_size()
            require(self.working_bytes <= self.limits["maxWorkingBytes"], "Parameter backup budget exceeded", "CAPACITY")
            self.backups[site["name"]] = parameter.detach().clone()
        kind, n = op["operation"], parameter.numel()
        with self.torch.no_grad():
            if kind == "WEIGHT_ADD":
                delta = op["delta"]
                value = number(delta["value"]) if delta["kind"] == "scalar" else self.tensor(delta["tensor"])
                if not isinstance(value, (int, float)):
                    require(list(value.shape) == list(parameter.shape), "Weight addition shape mismatch")
                self.charge(n)
                if isinstance(value, (int, float)) and value == 0:
                    return
                parameter.add_(value)
            elif kind == "WEIGHT_GAIN":
                gain = op["gain"]
                if gain["kind"] == "scalar":
                    factor = number(gain["factor"])
                    if factor == 1:
                        return
                else:
                    axis = integer(gain["axis"], 0, parameter.ndim - 1)
                    factor = self.tensor(gain["factors"])
                    require(list(factor.shape) == [parameter.shape[axis]], "Axis gain shape mismatch")
                    shape = [1] * parameter.ndim
                    shape[axis] = parameter.shape[axis]
                    factor = factor.reshape(shape)
                self.charge(n)
                parameter.mul_(factor)
            elif kind == "WEIGHT_REPLACE":
                value = self.tensor(op["replacement"])
                require(list(value.shape) == list(parameter.shape), "Weight replacement shape mismatch")
                self.charge(n)
                parameter.copy_(value)
            elif kind == "WEIGHT_PROJECT":
                require(parameter.ndim == 2 and op["normalization"] == "orthonormal", "Projection requires an orthonormal matrix basis")
                side = op["side"]
                require(side in ("left", "right"), "Invalid projection side")
                basis = self.basis(op["basis"], parameter.shape[0 if side == "left" else 1])
                strength = number(op["strength"])
                if strength == 0:
                    return
                # Six dense float64 buffers conservatively cover conversions,
                # both products, strength multiplication and final subtraction.
                require(self.working_bytes + n * 48 <= self.limits["maxWorkingBytes"], "Projection workspace exceeds budget", "CAPACITY")
                self.charge(5 * n * basis.shape[0])
                value = parameter.to(self.torch.float64)
                projected = basis.T @ (basis @ value) if side == "left" else (value @ basis.T) @ basis
                parameter.copy_(value - strength * projected)
            elif kind == "LOW_RANK_UPDATE":
                require(parameter.ndim == 2 and op["scaling"] == "alpha_over_rank", "Unsupported low-rank semantics")
                rank = integer(op["rank"], 1)
                a, b = self.tensor(op["a"]), self.tensor(op["b"])
                require(list(a.shape) == [rank, parameter.shape[1]] and list(b.shape) == [parameter.shape[0], rank], "Low-rank operand shape mismatch")
                scale = number(op["scale"]) * (number(op["alpha"]) / rank)
                number(scale)
                if scale == 0:
                    return
                require(self.working_bytes + n * 48 <= self.limits["maxWorkingBytes"], "Low-rank workspace exceeds budget", "CAPACITY")
                self.charge(2 * n * rank + 2 * n)
                parameter.copy_(parameter.to(self.torch.float64) + scale * (b @ a))
            elif kind == "WEIGHT_MERGE":
                # A JSON tensor claiming a checkpoint label cannot prove source
                # identity. Dedicated source-checkpoint readers are not implicit.
                raise Rejected("UNSUPPORTED", "Runtime weight merge requires a verified source-checkpoint reader")
            require(bool(self.torch.isfinite(parameter).all()), "Weight edit produced nonfinite parameters")

    def install(self, recipe):
        if recipe is None:
            return []
        require(recipe.get("schema") == "omnius.nol.recipe.v1" and recipe["modelDigest"] == self.binding["modelDigest"] and
                recipe["architectureMapDigest"] == self.binding["architectureMapDigest"], "Recipe/model binding mismatch", "IDENTITY")
        operations = recipe["operations"]
        require(type(operations) is list and 0 < len(operations) <= 1000, "Invalid bounded operation inventory")
        require(len({op["id"] for op in operations}) == len(operations), "Duplicate operation ID")
        require(not (any(op.get("operation") in PARAMETER_OPS for op in operations) and any(op.get("operation") in RUNTIME_OPS for op in operations)), "Mixed runtime/parameter ordering requires a separately validated lowering", "UNSUPPORTED")
        groups, actual, storage = {}, [], {}
        allowed = {
            "NEURON_ADD": {"neuron", "amount", "units", "calibrationId"}, "NEURON_GAIN": {"neuron", "factor"},
            "NEURON_REPLACE": {"neuron", "replacement", "units", "calibrationId"}, "RESIDUAL_ADD": {"vector", "scale", "units", "calibrationId"},
            "DIRECTION_PROJECT": {"basis", "strength", "normalization"}, "WEIGHT_ADD": {"delta"}, "WEIGHT_GAIN": {"gain"},
            "WEIGHT_REPLACE": {"replacement"}, "WEIGHT_PROJECT": {"side", "basis", "strength", "normalization"},
            "LOW_RANK_UPDATE": {"a", "b", "rank", "alpha", "scale", "scaling"}}
        for op in operations:
            require(op.get("operation") in allowed, "Unsupported operation", "UNSUPPORTED")
            require(set(op) <= {"id", "operation", "siteId", "tokenScope"} | allowed[op["operation"]], "Unknown operation field")
            require("calibrationId" not in op and op.get("units", "raw") == "raw", "Only explicit raw-unit operands are implemented", "UNSUPPORTED")
            site = self.sites.get(op["siteId"])
            require(site is not None, "Unknown verified tensor site", "IDENTITY")
            kind = op["operation"]
            expected = "parameter" if kind in PARAMETER_OPS else "mlp_intermediate" if kind.startswith("NEURON_") else "residual"
            require(site["kind"] == expected, "Operation/site type mismatch")
            self.edited[op["id"]] = set()
            actual.append({"operationId": op["id"], "operation": kind, "siteId": op["siteId"], "tokenScope": op["tokenScope"],
                           "implementation": WORKER_VERSION + ":" + kind, "equivalence": "exact"})
            if kind in PARAMETER_OPS:
                require(op["tokenScope"] == {"kind": "all"}, "Parameter edits have all-token semantics only", "UNSUPPORTED")
                parameter = self.parameters[site["name"]]
                pointer = parameter.data_ptr()
                require(pointer not in storage or storage[pointer] == op["siteId"], "Overlapping tied-parameter aliases need explicit lowering", "UNSUPPORTED")
                storage[pointer] = op["siteId"]
                self.parameter_edit(op, site)
            else:
                prepared = dict(op)
                if kind in ("NEURON_ADD", "NEURON_GAIN", "NEURON_REPLACE"):
                    integer(op["neuron"], 0, site["width"] - 1)
                if kind == "RESIDUAL_ADD":
                    prepared["operand"] = self.tensor(op["vector"])
                    require(list(prepared["operand"].shape) == [site["width"]], "Residual vector width mismatch")
                elif kind == "DIRECTION_PROJECT":
                    require(op["normalization"] == "orthonormal", "Unsupported projection normalization")
                    prepared["operand"] = self.basis(op["basis"], site["width"])
                elif kind == "NEURON_REPLACE":
                    require(op["replacement"]["kind"] == "scalar", "Captured activation replacement is not implemented", "UNSUPPORTED")
                groups.setdefault(op["siteId"], []).append(prepared)
        for site_id, ops in groups.items():
            site = self.sites[site_id]
            layer = self.layers[site["layer"]]
            def edit(value, selected=ops):
                require(value.ndim == 3 and value.shape[0] == 1 and value.shape[1] == self.sequence_length, "Runtime hook shape changed", "IDENTITY")
                require(self.working_bytes + value.numel() * (value.element_size() * 2 + 24) <= self.limits["maxWorkingBytes"], "Hook workspace exceeds budget", "CAPACITY")
                result = value.clone()
                for operation in selected:
                    positions = self.positions(operation["tokenScope"])
                    if not positions:
                        continue
                    kind = operation["operation"]
                    self.edited[operation["id"]].update(positions)
                    if kind.startswith("NEURON_"):
                        neuron = operation["neuron"]
                        current = result[:, positions, neuron]
                        self.charge(current.numel())
                        if kind == "NEURON_ADD":
                            amount = number(operation["amount"])
                            if amount != 0:
                                result[:, positions, neuron] = current + amount
                        elif kind == "NEURON_GAIN":
                            factor = number(operation["factor"])
                            if factor != 1:
                                result[:, positions, neuron] = current * factor
                        else:
                            result[:, positions, neuron] = number(operation["replacement"]["value"])
                    elif kind == "RESIDUAL_ADD":
                        scale = number(operation["scale"])
                        if scale != 0:
                            self.charge(2 * len(positions) * value.shape[-1])
                            result[:, positions, :] = result[:, positions, :] + scale * operation["operand"].to(value.dtype)
                    else:
                        strength = number(operation["strength"])
                        if strength != 0:
                            basis = operation["operand"]
                            self.charge(5 * len(positions) * value.shape[-1] * basis.shape[0])
                            current = result[:, positions, :].to(self.torch.float64)
                            result[:, positions, :] = (current - strength * ((current @ basis.T) @ basis)).to(value.dtype)
                require(bool(self.torch.isfinite(result).all()), "Hook produced a nonfinite activation")
                return result
            if site["kind"] == "mlp_intermediate":
                self.hooks.append(layer.mlp.down_proj.register_forward_pre_hook(lambda module, args, fn=edit: (fn(args[0]), *args[1:])))
            else:
                self.hooks.append(layer.register_forward_hook(lambda module, args, output, fn=edit: fn(output)))
        return actual

    def generate(self, payload):
        self.ready()
        fields(payload, ("messages", "recipe", "recipeIdentityJson", "cacheKeyDigest", "seed", "maxTokens", "decoding"))
        require(not self.hooks and not self.backups and self.cache_key is None, "Worker requires a clean reset", "RESET")
        maximum = integer(payload["maxTokens"], 1, self.limits["maxOutputTokens"])
        seed = integer(payload["seed"], 0, 4294967295)
        require(SHA.fullmatch(payload["cacheKeyDigest"]), "Invalid cold cache identity")
        decoding = payload["decoding"]
        fields(decoding, ("temperature", "topP", "topK", "constrainedDecoding"))
        temperature, top_p, top_k = number(decoding["temperature"]), number(decoding["topP"]), integer(decoding["topK"])
        require(temperature >= 0 and 0 < top_p <= 1 and decoding["constrainedDecoding"] == "none", "Unsupported sampling or constrained decoding", "UNSUPPORTED")
        prompt = self.tokenize({"messages": payload["messages"]})["tokenIds"]
        require(len(prompt) + maximum <= self.binding["contextLimit"], "Physical context limit exceeded", "CAPACITY")
        if payload["recipe"] is not None:
            recipe = payload["recipe"]
            verify_identity(payload["recipeIdentityJson"], {key: value for key, value in recipe.items() if key != "recipeDigest"}, recipe["recipeDigest"])
        else:
            require(payload["recipeIdentityJson"] is None, "Baseline cannot carry a recipe identity")
        require(self.last_reset is not None, "Explicit host reset is required before generation", "RESET")
        before = self.last_reset
        self.prompt_length = len(prompt)
        self.cache_key = payload["cacheKeyDigest"]
        outputs, actual = [], []
        self.torch.manual_seed(seed)
        self.torch.cuda.manual_seed_all(seed)
        try:
            actual = self.install(payload["recipe"])
            with self.torch.inference_mode():
                for _ in range(maximum):
                    self.sequence_length = len(prompt) + len(outputs)
                    ids = self.torch.tensor([prompt + outputs], device="cuda:0", dtype=self.torch.long)
                    result = self.model(input_ids=ids, attention_mask=self.torch.ones_like(ids), use_cache=False, logits_to_keep=1)
                    require(result.past_key_values is None, "Runtime retained an unadvertised cache", "RESET")
                    logits = result.logits[0, -1].float()
                    require(bool(self.torch.isfinite(logits).all()), "Model produced nonfinite logits")
                    if temperature == 0:
                        token = int(logits.argmax().item())
                    else:
                        logits = logits / temperature
                        if 0 < top_k < logits.numel():
                            logits[logits < self.torch.topk(logits, top_k).values[-1]] = -self.torch.inf
                        sorted_logits, order = self.torch.sort(logits, descending=True)
                        probabilities = self.torch.softmax(sorted_logits, dim=-1)
                        mask = probabilities.cumsum(-1) - probabilities > top_p
                        sorted_logits[mask] = -self.torch.inf
                        picked = self.torch.multinomial(self.torch.softmax(sorted_logits, -1), 1)
                        token = int(order[picked].item())
                    outputs.append(token)
                    del result, ids, logits
                    if token == self.tokenizer.eos_token_id:
                        break
                    require(len(self.tokenizer.decode(outputs).encode()) < MAX_FRAME // 2, "Output byte budget exceeded", "CAPACITY")
            text = self.tokenizer.decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=False)
            observed = {key: sorted(value) for key, value in self.edited.items()}
            for operation in actual:
                if operation["operation"] in PARAMETER_OPS:
                    observed[operation["operationId"]] = list(range(len(prompt) + max(0, len(outputs) - 1)))
            reset = self.reset()
            return {"text": text, "promptTokenIds": prompt, "tokenIds": outputs,
                    "usage": {"promptTokens": len(prompt), "completionTokens": len(outputs)},
                    "finishReason": "stop" if outputs and outputs[-1] == self.tokenizer.eos_token_id else "length",
                    "native": {"binding": self.binding, "implementation": WORKER_VERSION, "operations": actual,
                               "editedPositions": observed, "cacheKeyDigest": payload["cacheKeyDigest"],
                               "resetBefore": before, "resetAfter": reset, "usesCache": False,
                               "recipeDigest": payload["recipe"]["recipeDigest"] if payload["recipe"] else None}}
        except BaseException:
            try:
                self.reset()
            except BaseException as error:
                raise Rejected("RESET", "Failed trial could not restore clean state", True) from error
            raise

    def profile(self, payload):
        """One real forward pass, bounded coordinate moments, no sampled tokens."""
        self.ready()
        fields(payload, ("messages", "recipe", "recipeIdentityJson", "sourceArtifact", "capture"))
        require(not self.hooks and not self.backups and self.last_reset is not None and self.cache_key is None, "Profile requires an explicit clean host reset", "RESET")
        source = payload["sourceArtifact"]
        fields(source, ("uri", "sha256", "sizeBytes", "mediaType"))
        require(SHA.fullmatch(source["sha256"]) and source["uri"] == "omnius-artifact://sha256/" + source["sha256"], "Invalid profile source artifact")
        require(source["mediaType"] == "application/json", "Profile source must be a versioned JSON input")
        source_path = safe_path(self.artifact_root, source["sha256"][:2] + "/" + source["sha256"])
        require(hash_file(source_path, self.limits["maxTensorBytes"]) == {"sha256": source["sha256"], "sizeBytes": source["sizeBytes"]}, "Profile source artifact changed", "IDENTITY")
        with source_path.open("rb") as source_stream:
            source_bytes = source_stream.read(source["sizeBytes"] + 1)
        require(len(source_bytes) == source["sizeBytes"] and digest_bytes(source_bytes) == source["sha256"], "Profile source changed before parsing", "IDENTITY")
        source_input = parse_json(source_bytes.decode("utf-8", "strict"))
        fields(source_input, ("schema", "messages"))
        require(source_input["schema"] == "omnius.nol.profile-input.v1" and same_json(source_input["messages"], payload["messages"]),
                "Profile messages differ from the exact versioned source artifact", "IDENTITY")
        capture = payload["capture"]
        fields(capture, ("sites", "maxPositions", "maxCoordinates"))
        positions_limit = integer(capture["maxPositions"], 1, min(4096, self.limits["maxInputTokens"]))
        coordinate_limit = integer(capture["maxCoordinates"], 1, 1024)
        require(type(capture["sites"]) is list and 0 < len(capture["sites"]) <= 16, "Invalid profile site budget", "CAPACITY")
        selected, coordinates = {}, 0
        for spec in capture["sites"]:
            fields(spec, ("siteId", "indices"))
            site = self.sites.get(spec["siteId"])
            require(site and site["kind"] in ("mlp_intermediate", "residual") and spec["siteId"] not in selected, "Unknown or duplicate profile site", "UNSUPPORTED")
            require(type(spec["indices"]) is list and 0 < len(spec["indices"]) <= 1024 and len(spec["indices"]) == len(set(spec["indices"])), "Invalid profile coordinates")
            for index in spec["indices"]:
                integer(index, 0, site["width"] - 1)
            coordinates += len(spec["indices"])
            selected[spec["siteId"]] = spec
        require(coordinates <= coordinate_limit, "Profile coordinate budget exceeded", "CAPACITY")
        prompt = self.tokenize({"messages": payload["messages"]})["tokenIds"]
        if payload["recipe"] is not None:
            verify_identity(payload["recipeIdentityJson"], {key: value for key, value in payload["recipe"].items() if key != "recipeDigest"}, payload["recipe"]["recipeDigest"])
        else:
            require(payload["recipeIdentityJson"] is None, "Baseline profile carries a recipe identity")
        before, observed = self.last_reset, {}
        self.prompt_length = self.sequence_length = len(prompt)
        try:
            actual = self.install(payload["recipe"])
            for site_id, spec in selected.items():
                site, indices = self.sites[site_id], spec["indices"]
                def collect(value, key=site_id, columns=indices, kind=site["kind"]):
                    require(key not in observed and value.ndim == 3 and value.shape[0] == 1 and value.shape[1] == len(prompt), "Profile hook shape/call count changed", "IDENTITY")
                    count = min(value.shape[1], positions_limit)
                    require(self.working_bytes + count * len(columns) * 64 <= self.limits["maxWorkingBytes"], "Profile workspace exceeds budget", "CAPACITY")
                    self.charge(count * len(columns) * 16)
                    index = self.torch.tensor(columns, dtype=self.torch.long, device=value.device)
                    sample = value[0, :count, :].index_select(1, index).to(self.torch.float64)
                    require(bool(self.torch.isfinite(sample).all()), "Profile captured nonfinite activations")
                    mean = sample.mean(dim=0)
                    variance = ((sample - mean) ** 2).mean(dim=0)
                    observed[key] = {"siteId": key, "siteKind": kind, "indices": columns, "count": count,
                                     "mean": mean.tolist(), "std": variance.sqrt().tolist(),
                                     "rms": (sample.square().mean(dim=0)).sqrt().tolist(),
                                     "minimum": sample.amin(dim=0).tolist(), "maximum": sample.amax(dim=0).tolist()}
                layer = self.layers[site["layer"]]
                if site["kind"] == "mlp_intermediate":
                    def pre(module, args, fn=collect):
                        fn(args[0])
                    self.hooks.append(layer.mlp.down_proj.register_forward_pre_hook(pre))
                else:
                    def post(module, args, output, fn=collect):
                        fn(output)
                    self.hooks.append(layer.register_forward_hook(post))
            with self.torch.inference_mode():
                ids = self.torch.tensor([prompt], device="cuda:0", dtype=self.torch.long)
                result = self.model(input_ids=ids, attention_mask=self.torch.ones_like(ids), use_cache=False, logits_to_keep=1)
                require(result.past_key_values is None and len(observed) == len(selected), "Profile did not execute every selected hook cleanly", "RESET")
                del result, ids
            positions = {key: sorted(value) for key, value in self.edited.items()}
            after = self.reset()
            return {"sourceArtifact": source, "promptTokenIds": prompt, "promptTokens": len(prompt), "sampledTokens": 0,
                    "observationUnit": "single_forward_computation_positions", "summaries": list(observed.values()),
                    "native": {"binding": self.binding, "implementation": WORKER_VERSION, "operations": actual,
                               "editedPositions": positions, "resetBefore": before, "resetAfter": after, "usesCache": False}}
        except BaseException:
            try:
                self.reset()
            except BaseException as error:
                raise Rejected("RESET", "Profile could not restore clean state", True) from error
            raise

    def close(self):
        if self.model is not None:
            self.reset()
        self.model = self.tokenizer = None
        self.parameters = self.layers = self.sites = None
        gc.collect()
        if self.torch is not None:
            self.torch.cuda.synchronize()
            self.torch.cuda.empty_cache()
        self.loaded = self.confirmed = False
        return {"closed": True, "pid": os.getpid(), "allocationReleaseRequiresHostVerification": True}


def read_exact(stream, count):
    data = bytearray()
    while len(data) < count:
        part = stream.read(count - len(data))
        if not part:
            require(not data, "Truncated worker frame")
            return None
        data.extend(part)
    return bytes(data)


def serve():
    sys.stdout = sys.stderr  # Native libraries and warnings cannot corrupt framing.
    require(0 < MAX_FRAME <= 67108864 and all(OWNER[key] for key in OWNER) and type(OWNER["generation"]) is int,
            "Missing private worker ownership environment")
    runtime, sequence = Runtime(), 0
    while True:
        header = read_exact(sys.stdin.buffer, 4)
        if header is None:
            break
        size = struct.unpack(">I", header)[0]
        require(0 < size <= MAX_FRAME, "Worker input frame exceeds byte limit", "CAPACITY")
        raw = read_exact(sys.stdin.buffer, size)
        require(raw is not None, "Truncated worker request")
        request = parse_json(raw.decode("utf-8", "strict"))
        fields(request, ("schema", "kind", "sessionId", "jobId", "ownerId", "generation", "id", "action", "payload"))
        require(request["schema"] == PROTOCOL and request["kind"] == "request" and
                all(request[key] == value for key, value in OWNER.items()) and request["id"] == sequence + 1,
                "Worker request ownership/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": PROTOCOL, "workerVersion": WORKER_VERSION, "pid": os.getpid(), "workerSha256": hash_file(Path(__file__).resolve())["sha256"],
                          "pythonVersion": sys.version.split()[0], "loaded": runtime.loaded, "torchImported": "torch" in sys.modules}
            elif action == "inspect":
                result = runtime.inspect(payload)
            elif action == "load":
                result = runtime.load(payload)
            elif action == "confirm_placement":
                result = runtime.confirm(payload)
            elif action == "tokenize":
                result = runtime.tokenize(payload)
            elif action == "reset":
                fields(payload, ())
                result = runtime.reset()
            elif action == "generate":
                result = runtime.generate(payload)
            elif action == "profile":
                result = runtime.profile(payload)
            elif action == "close":
                fields(payload, ())
                result, closing = runtime.close(), True
            else:
                raise Rejected("UNSUPPORTED", "Unknown worker command")
            response["result"] = result
        except Rejected as error:
            closing = error.fatal or action == "load"
            response["error"] = {"code": error.code, "message": str(error)[:4096], "fatal": closing}
        except BaseException as error:
            private_diagnostic(error)
            # No model text, arbitrary traceback or local paths enter protocol errors.
            response["error"] = {"code": "RUNTIME_FAILURE", "message": type(error).__name__, "fatal": True}
            closing = True
        body = json.dumps(response, ensure_ascii=False, separators=(",", ":"), allow_nan=False).encode("utf-8")
        require(len(body) <= MAX_FRAME, "Worker response frame exceeds byte limit", "CAPACITY")
        WIRE_OUT.write(struct.pack(">I", len(body)) + body)
        WIRE_OUT.flush()
        if closing:
            break
    runtime.close()


if __name__ == "__main__":
    try:
        serve()
    except BaseException as error:
        sys.stderr.write("NOL worker retired: " + type(error).__name__ + "\n")
        sys.exit(1)
