#!/usr/bin/env python3
"""Host-staged, local-only Laya evaluation service; never permits CPU inference.

Start with ``serve --selection ... --manifest ... --state-dir ... --lease-owner ...``.
Inspect idle.json and the actual process environment, then use authorize-load.
After resident-runtime.json exists, independently preflight and state the GPU
result before writing model_gate's approval to inference-approval.json. Neither
HTTP requests nor the service itself can authorize loading or inference.
"""
import argparse
import hashlib
from http.server import BaseHTTPRequestHandler, HTTPServer
import importlib
import importlib.metadata
import json
import os
from pathlib import Path
import secrets
import socket
import stat
import sys
import threading
import time

import model_gate as gate
import gpu_lease

CODE_REVISION = "c4a805b5160621bbfc53417126990a67d3163bc5"
WEIGHTS_REVISION = "7b928d828b7b0e022f929d9bd2e44165aa270148"
ENCODER_REVISION = "45bb4654a4d5aaff24dd11d4781fa46d39bf8c13"
MAX_REQUEST_BYTES = 96 * 1024
INFERENCE_DEADLINE_SECONDS = 30


class StrictCudaError(gate.GateError):
    """Deliberately not RuntimeError, which upstream catches to retry on CPU."""


def file_sha256(path):
    digest = hashlib.sha256()
    with Path(path).open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            digest.update(block)
    return digest.hexdigest()


def private_json(path):
    path = Path(path)
    info = path.lstat()
    gate.require(stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid()
                 and stat.S_IMODE(info.st_mode) & 0o077 == 0,
                 "host marker must be an owner-only regular file")
    return gate.read_json(path)


def verify_files(directory, entries, suffix=None):
    directory = Path(directory).resolve(strict=True)
    gate.require(isinstance(entries, dict) and 0 < len(entries) <= 1000,
                 "bounded checkpoint/source file manifest required")
    found = {str(path.relative_to(directory)) for path in directory.rglob("*")
             if path.is_file() and (suffix is None or path.suffix == suffix)}
    gate.require(found == set(entries), "checkpoint/source file inventory changed")
    for name, expected in entries.items():
        path = directory / name
        gate.require(not Path(name).is_absolute() and ".." not in Path(name).parts
                     and path.resolve().is_relative_to(directory), "manifest path escapes its directory")
        gate.require(isinstance(expected, str) and len(expected) == 64
                     and file_sha256(path) == expected, f"checkpoint/source hash mismatch: {name}")


def verify_manifest(manifest):
    gate.require(manifest.get("schema") == "omnius.computer-use.laya-checkpoint.v1"
                 and manifest.get("code_revision") == CODE_REVISION
                 and manifest.get("weights_revision") == WEIGHTS_REVISION,
                 "Laya checkpoint/runtime pins differ from reviewed source")
    encoder = manifest.get("encoder", {})
    gate.require(encoder.get("repository") == "answerdotai/ModernBERT-large"
                 and encoder.get("revision") == ENCODER_REVISION,
                 "encoder config must use the pinned ModernBERT revision")
    files = manifest.get("files", {})
    gate.require({"model.safetensors", "rl_agent_config.json", "encoder/config.json",
                  "tokenizer/tokenizer.json", "tokenizer/tokenizer_config.json"}.issubset(files),
                 "resolved local checkpoint is incomplete")
    gate.require(files["encoder/config.json"] == encoder.get("config_sha256"),
                 "encoder config digest differs from source receipt")
    verify_files(manifest["checkpoint"], files)
    package = Path(importlib.metadata.distribution("laya").locate_file("laya")).resolve()
    gate.require(package == Path(manifest["laya_package_root"]).resolve(),
                 "installed Laya package differs from the prepared runtime")
    verify_files(package, manifest["laya_code_files"], suffix=".py")
    for name, version in manifest["versions"].items():
        gate.require(importlib.metadata.version(name) == version, f"runtime version changed: {name}")


def validate_request(value, model):
    gate.require(isinstance(value, dict) and set(value) == {"model", "state", "questions"},
                 "request must contain only model, state and questions")
    gate.require(value["model"] == model, "request model is not the exact loaded model")
    gate.require(isinstance(value["state"], str) and 0 < len(value["state"]) <= 20_000,
                 "state must be a bounded redacted string")
    questions = value["questions"]
    gate.require(isinstance(questions, dict) and set(questions) == {"action"},
                 "only one finite action question is supported")
    action = questions["action"]
    gate.require(isinstance(action, dict) and set(action) == {"type", "instructions", "criteria"}
                 and action["type"] == "choice", "finite choice question required")
    gate.require(isinstance(action["instructions"], str) and 0 < len(action["instructions"]) <= 2000,
                 "bounded action instructions required")
    criteria = action["criteria"]
    gate.require(isinstance(criteria, dict) and 2 <= len(criteria) <= 50,
                 "require two to fifty finite choices")
    for key, description in criteria.items():
        gate.require(isinstance(key, str) and gate.ACTION_ID.fullmatch(key)
                     and isinstance(description, str) and 0 < len(description) <= 1000,
                     "invalid candidate ID or description")
    return value


class StrictCudaGuard:
    """Install before Agent's first model.to and before any possible forward."""
    def __init__(self, authorized, is_tensor):
        self.authorized = authorized
        self.is_tensor = is_tensor

    @staticmethod
    def require_device(device):
        if str(device) != "cuda:0":
            raise StrictCudaError("strict CUDA forbids CPU fallback or another device")

    def assert_resident(self, model):
        parameters = list(model.parameters())
        if not parameters:
            raise StrictCudaError("loaded model has no resident parameters")
        for tensor in parameters + list(model.buffers()):
            self.require_device(tensor.device)

    def before_forward(self, model, args, kwargs):
        if not self.authorized():
            raise StrictCudaError("forward pass is forbidden before host inference approval")
        self.assert_resident(model)
        pending = [args, kwargs]
        while pending:
            value = pending.pop()
            if self.is_tensor(value):
                self.require_device(value.device)
            elif isinstance(value, (tuple, list)):
                pending.extend(value)
            elif isinstance(value, dict):
                pending.extend(value.values())

    def install(self, model):
        original_to = model.to

        def guarded_to(*args, **kwargs):
            device = kwargs.get("device", args[0] if args else None)
            self.require_device(device)
            return original_to(*args, **kwargs)

        model.to = guarded_to
        model.register_forward_pre_hook(self.before_forward, with_kwargs=True)
        return model


def deny_outbound_runtime_connections():
    """Dedicated service process: preserve incoming HTTP, deny network clients."""
    original_connect, original_connect_ex = socket.socket.connect, socket.socket.connect_ex

    def guarded(original):
        def connect(stream, address):
            if stream.family in (socket.AF_INET, socket.AF_INET6):
                raise StrictCudaError("outbound runtime networking is disabled")
            return original(stream, address)
        return connect

    socket.socket.connect = guarded(original_connect)
    socket.socket.connect_ex = guarded(original_connect_ex)


class LocalLayaAdapter:
    def __init__(self, manifest, authorized):
        # No torch/laya import occurs until the host load marker was verified.
        verify_manifest(manifest)
        os.environ.update(HF_HUB_OFFLINE="1", TRANSFORMERS_OFFLINE="1",
                          HF_HUB_DISABLE_TELEMETRY="1", TOKENIZERS_PARALLELISM="false")
        deny_outbound_runtime_connections()
        import torch
        gate.require(torch.cuda.is_available() and torch.cuda.device_count() == 1,
                     "strict CUDA requires exactly the reserved visible GPU")
        properties = torch.cuda.get_device_properties(0)
        gate.require("A100" in properties.name and properties.total_memory >= 80_000 * 1024**2
                     and properties.major >= 8, "load probe requires the reserved capable A100 80 GB")
        module = importlib.import_module("laya.agent")
        self.guard = StrictCudaGuard(authorized, torch.is_tensor)
        original_factory = module.build_model

        def guarded_factory(*args, **kwargs):
            return self.guard.install(original_factory(*args, **kwargs))

        module.build_model = guarded_factory
        try:
            self.agent = module.Agent(manifest["checkpoint"], device="cuda:0", fast=False,
                                      compile=False, compile_warmup=False,
                                      expected_sha256=manifest["files"])
        finally:
            module.build_model = original_factory
        self.assert_resident()
        gate.require(self.agent.backend == "eager", "only the reviewed eager backend is permitted")
        self.synchronize()

    def assert_resident(self):
        self.guard.require_device(self.agent.device)
        self.guard.assert_resident(self.agent.model)

    def predict(self, request):
        self.assert_resident()
        result = self.agent.system_one(request["state"], request["questions"],
                                       max_len=512, head_max_len=192)
        self.assert_resident()
        return result

    def synchronize(self):
        import torch
        self.assert_resident()
        torch.cuda.synchronize(0)
        self.assert_resident()


class StagedService:
    def __init__(self, selection, lock, manifest, manifest_sha, state_dir, lease_owner,
                 adapter_factory=LocalLayaAdapter, identity=None, cuda_scope=None,
                 boot_id=None, clock=time.time, preflight=None, timer_factory=threading.Timer,
                 terminate=os._exit, lease=None):
        gate.validate_selection(selection, lock)
        endpoint = gate.local_endpoint(selection["endpoint"])
        gate.require(selection["model_id"] == "laya" and selection["protocol"] == "systemone"
                     and selection["weights_revision"] == WEIGHTS_REVISION
                     and selection["runtime_revision"] == CODE_REVISION,
                     "service requires the exact pinned Laya selection")
        gate.require(endpoint.hostname == "127.0.0.1" and endpoint.path == "/v1/systemone"
                     and endpoint.port and len(selection["gpu_uuids"]) == 1,
                     "bind an exact literal loopback port and one leased GPU")
        gate.require(isinstance(lease_owner, str) and 1 <= len(lease_owner) <= 256,
                     "explicit lease owner required")
        self.identity = identity or gate.process_identity(os.getpid())
        self.cuda_scope = cuda_scope or gate.process_cuda_scope(os.getpid())
        gate.require(self.cuda_scope == selection["gpu_uuids"], "actual process CUDA visibility differs from selection")
        self.boot_id = boot_id or Path("/proc/sys/kernel/random/boot_id").read_text().strip()
        self.selection, self.lock, self.manifest, self.manifest_sha = selection, lock, manifest, manifest_sha
        self.state_dir = Path(state_dir)
        self.state_dir.mkdir(parents=True, exist_ok=True, mode=0o700)
        info = self.state_dir.stat()
        gate.require(info.st_uid == os.getuid() and stat.S_IMODE(info.st_mode) & 0o077 == 0,
                     "state directory must be owner-only")
        self.lease_owner, self.clock, self.adapter_factory = lease_owner, clock, adapter_factory
        self.preflight = preflight or (lambda receipt: gate.verify_preflight(
            selection, lock, receipt, gate.collect_snapshot(selection, receipt)))
        self.timer_factory, self.terminate = timer_factory, terminate
        self.lease = lease or gpu_lease.ScopedGpuLease(lease_owner, self.cuda_scope)
        self.stage, self.adapter, self.receipt = "idle", None, None
        self.inference_active = False
        self.lease_transition = None
        self.mutex = threading.Lock()
        self.nonce = secrets.token_hex(24)
        self.service_code_hashes = self.current_code_hashes()
        self.idle = {"schema": "omnius.computer-use.laya-idle.v1", "stage": self.stage,
                     "loaded": False, "server": self.identity, "startup_nonce": self.nonce,
                     "manifest_sha256": manifest_sha, "selection": selection,
                     "cuda_visible_devices": self.cuda_scope, "strict_cuda": True,
                     "forward_calls": 0, "network_runtime": "disabled", "lease_owner": lease_owner,
                     "service_code_hashes": self.service_code_hashes}
        gate.write_json(self.state_dir / "idle.json", self.idle)
        self.write_state()

    @staticmethod
    def current_code_hashes():
        return {"laya_service.py": file_sha256(__file__), "model_gate.py": file_sha256(gate.__file__),
                "gpu_lease.py": file_sha256(gpu_lease.__file__)}

    def verify_service_code(self):
        gate.require(self.current_code_hashes() == self.service_code_hashes,
                     "reviewed service or model gate source changed after startup")

    def write_state(self, error=None):
        gate.write_json(self.state_dir / "state.json", {
            "schema": "omnius.computer-use.laya-service-state.v1", "stage": self.stage,
            "server": self.identity, "startup_nonce": self.nonce,
            "loaded": self.receipt is not None, "strict_cuda": True,
            "lease_transition": self.lease_transition,
            **({"error": str(error)[:2000]} if error else {})})

    def fail(self, error):
        self.stage = "failed"
        self.inference_active = False
        self.write_state(error)

    def verify_load_marker(self, marker):
        gate.require(marker.get("schema") == "omnius.computer-use.load-authorization.v1",
                     "host load authorization required")
        for key in ("server", "startup_nonce", "manifest_sha256", "selection", "cuda_visible_devices", "service_code_hashes"):
            gate.require(marker.get(key) == self.idle[key], f"load authorization mismatch: {key}")
        issued, expires = marker.get("issued_at", 0), marker.get("expires_at", 0)
        gate.require(isinstance(issued, (int, float)) and isinstance(expires, (int, float))
                     and issued <= self.clock() < expires and 0 < expires - issued <= 120,
                     "load authorization is stale")
        gate.require(isinstance(marker.get("preload_statement"), str)
                     and 20 <= len(marker["preload_statement"]) <= 2000,
                     "record actual process CUDA environment inspection before load")

    def advance(self):
        marker = self.state_dir / "load-authorization.json"
        if self.stage != "idle" or not marker.exists():
            return
        with self.mutex:
            try:
                self.verify_service_code()
                self.verify_load_marker(private_json(marker))
                self.stage = "loading"
                self.write_state()
                self.adapter = self.adapter_factory(self.manifest, lambda: self.inference_active)
                self.adapter.assert_resident()
                self.receipt = {**self.selection, "schema": "omnius.computer-use.resident-runtime.v1",
                                "service_mode": "leased-external", "loaded": True,
                                "attestation_source": "host-launcher", "server": self.identity,
                                "runners": [self.identity], "boot_id": self.boot_id,
                                "cuda_visible_devices": self.cuda_scope, "lease_owner": self.lease_owner,
                                "manifest_sha256": self.manifest_sha, "strict_cuda": True,
                                "backend": "eager", "warmup_performed": False,
                                "service_code_hashes": self.service_code_hashes}
                gate.write_json(self.state_dir / "resident-runtime.json", self.receipt)
                self.stage = "resident"
                self.write_state()
            except Exception as error:
                self.fail(error)

    def infer(self, request):
        request = validate_request(request, self.selection["model"])
        gate.require(self.mutex.acquire(blocking=False), "another request is in progress")
        try:
            gate.require(self.stage in ("resident", "ready") and self.adapter is not None,
                         "service is not resident and authorized for inference")
            self.verify_service_code()
            approval = private_json(self.state_dir / "inference-approval.json")
            preflight = self.preflight(self.receipt)
            gate.verify_approval(approval, preflight, self.clock())
            self.adapter.assert_resident()

            def timeout():
                self.fail("inference exceeded its hard 30 second deadline; process terminated")
                self.terminate(124)

            watchdog = self.timer_factory(INFERENCE_DEADLINE_SECONDS, timeout)
            watchdog.daemon = True
            watchdog.start()
            try:
                # Preflight above required an active exact lease. Prepare then
                # owns a pending growth interval; do not re-run the active-state
                # gate inside that interval or release it before CUDA settles.
                self.lease.prepare()
                self.lease_transition = {"action": "prepare", "state": "pending", "confirmed_at": self.clock()}
                self.write_state()
                self.inference_active = True
                answer = gate.finite_answer(self.adapter.predict(request), request)
                self.adapter.assert_resident()
                self.adapter.synchronize()
                self.inference_active = False
                self.lease.ready()
                self.lease_transition = {"action": "ready", "state": "active", "confirmed_at": self.clock()}
            except Exception as error:
                self.fail(error)
                # Never mark an unknown/failed CUDA interval ready. The scoped
                # supervisor waits for process exit and freed VRAM, then releases.
                self.terminate(125)
                raise
            finally:
                self.inference_active = False
                watchdog.cancel()
            self.stage = "ready"
            self.write_state()
            return {"model": self.selection["model"], "revision": WEIGHTS_REVISION,
                    "answers": {"action": {"type": "choice", "choice": answer["choice"],
                        **({"probabilities": answer["probabilities"]} if answer["probabilities"] is not None else {})}}}
        finally:
            self.mutex.release()


class LocalServer(HTTPServer):
    allow_reuse_address = False
    request_queue_size = 4

    def service_actions(self):
        self.service.advance()


class Handler(BaseHTTPRequestHandler):
    def setup(self):
        super().setup()
        self.connection.settimeout(5)

    def log_message(self, *_args):
        pass  # Never log states, criteria, or request bodies.

    def reply(self, status, value):
        raw = json.dumps(value, allow_nan=False, separators=(",", ":")).encode()
        self.send_response(status)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(raw)))
        self.send_header("Connection", "close")
        self.end_headers()
        self.wfile.write(raw)

    def do_GET(self):
        if self.path != "/health":
            return self.reply(404, {"error": "unknown route"})
        self.reply(200, {"stage": self.server.service.stage, "strict_cuda": True,
                         "pid": os.getpid()})

    def do_POST(self):
        if self.path != "/v1/systemone":
            return self.reply(404, {"error": "unknown route"})
        try:
            gate.require(not self.headers.get("Transfer-Encoding"), "chunked requests are unsupported")
            lengths = self.headers.get_all("Content-Length", [])
            gate.require(len(lengths) == 1 and lengths[0].isascii() and lengths[0].isdigit(),
                         "one bounded Content-Length is required")
            length = int(lengths[0])
            gate.require(0 < length <= MAX_REQUEST_BYTES, "request body exceeds bound")
            gate.require(self.headers.get_content_type() == "application/json", "JSON content type required")
            raw = self.rfile.read(length)
            gate.require(len(raw) == length, "request body ended early")
            request = validate_request(gate.strict_json(raw), self.server.service.selection["model"])
        except (gate.GateError, OSError, ValueError) as error:
            return self.reply(400, {"error": str(error)[:1000]})
        try:
            self.reply(200, self.server.service.infer(request))
        except Exception as error:
            self.reply(503, {"error": str(error)[:1000], "stage": self.server.service.stage})


def authorize_load(state_dir, statement, now=None):
    now = time.time() if now is None else now
    idle = private_json(Path(state_dir) / "idle.json")
    gate.require(idle.get("schema") == "omnius.computer-use.laya-idle.v1" and idle.get("loaded") is False,
                 "fresh idle process receipt required")
    gate.require(isinstance(statement, str) and 20 <= len(statement) <= 2000,
                 "record the actual process CUDA environment inspection")
    marker = {**{key: idle[key] for key in ("server", "startup_nonce", "manifest_sha256", "selection", "cuda_visible_devices", "service_code_hashes")},
              "schema": "omnius.computer-use.load-authorization.v1", "issued_at": now,
              "expires_at": now + 120, "preload_statement": statement}
    gate.write_json(Path(state_dir) / "load-authorization.json", marker)


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    commands = parser.add_subparsers(dest="command", required=True)
    serve = commands.add_parser("serve")
    serve.add_argument("--selection", required=True, type=Path)
    serve.add_argument("--manifest", required=True, type=Path)
    serve.add_argument("--state-dir", required=True, type=Path)
    serve.add_argument("--lease-owner", required=True)
    serve.add_argument("--lock", type=Path, default=gate.DEFAULT_LOCK)
    authorize = commands.add_parser("authorize-load")
    authorize.add_argument("--state-dir", required=True, type=Path)
    authorize.add_argument("--statement", required=True)
    args = parser.parse_args(argv)
    try:
        if args.command == "authorize-load":
            authorize_load(args.state_dir, args.statement)
            return 0
        selection, lock = gate.read_json(args.selection), gate.read_json(args.lock)
        manifest, manifest_sha = gate.read_json_hashed(args.manifest)
        service = StagedService(selection, lock, manifest, manifest_sha, args.state_dir, args.lease_owner)
        endpoint = gate.local_endpoint(selection["endpoint"])
        with LocalServer(("127.0.0.1", endpoint.port), Handler) as server:
            server.service = service
            server.serve_forever(poll_interval=0.25)
        return 0
    except (gate.GateError, OSError, ValueError) as error:
        print(json.dumps({"status": "blocked", "error": str(error)}), file=sys.stderr)
        return 2


if __name__ == "__main__":
    raise SystemExit(main())
