Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

11.1 KB
Raw Blame
"""Probe a model server through this installation's router container.

A WSL host agent cannot reach a Windows loopback-only listener: WSL localhost
is not Windows localhost. Docker Desktop containers can, through
``host.docker.internal``. The vLLM engine (``LLM_BACKEND=vllm``) publishes no
host port at all; only containers on the ODS network reach it. This module
runs one bounded stdlib request inside the owned, running model-router
container, against exactly the origin of the router endpoint the caller names
(``llama-server-default`` by default, ``vllm-default`` for vLLM), which is the
origin the router itself serves inference from.
"""

from __future__ import annotations

import json
import math
from pathlib import Path
import re
import subprocess
from urllib.parse import urlsplit


ENDPOINT_ID = "llama-server-default"
_ENDPOINT_IDS = frozenset({ENDPOINT_ID, "vllm-default"})
_LIMIT = 65536
_ANSWER_LIMIT = 2097152  # the worker's ANSWER_LIMIT for a probe exchange
_PATHS = frozenset({"/health", "/props", "/v1/models", "/v1/chat/completions", "/metrics"})
_INSPECT = ('{"Id":{{json .Id}},"Running":{{json .State.Running}},'
            '"Project":{{json (index .Config.Labels "com.docker.compose.project")}},'
            '"Service":{{json (index .Config.Labels "com.docker.compose.service")}},'
            '"Mounts":{{json .Mounts}}}')

# Credentials and request data arrive only on stdin. A worker deadline also
# ends a slow-drip response after the local Docker client has disconnected.
_WORKER = r'''
import json
import os
from pathlib import Path
import sys
import threading
import urllib.error
import urllib.request

LIMIT = 65536
# A probe exchange (the envelope) can carry a streamed answer: one JSON event
# per token, about 400 KB for 1,000 tokens of a thinking model.
ANSWER_LIMIT = 2097152

class ProofError(ValueError):
    pass

class NoRedirect(urllib.request.HTTPRedirectHandler):
    def redirect_request(self, req, fp, code, msg, headers, newurl):
        raise urllib.error.HTTPError(req.full_url, code, "redirect rejected", headers, fp)

def deadline():
    sys.stderr.write("runtime request timed out\n")
    sys.stderr.flush()
    os._exit(124)

def serve():
    raw = sys.stdin.buffer.read(LIMIT + 1)
    if len(raw) > LIMIT:
        raise ProofError("request exceeds 64 KiB")
    message = json.loads(raw)
    timer = threading.Timer(message["timeout"], deadline)
    timer.daemon = True
    timer.start()
    try:
        with Path("/config/endpoints.json").open("rb") as stream:
            raw = stream.read(LIMIT + 1)
        if len(raw) > LIMIT:
            raise ProofError("endpoint configuration exceeds 64 KiB")
        endpoints = json.loads(raw)["endpoints"]
        matches = [row for row in endpoints if isinstance(row, dict)
                   and row.get("id") == message["endpoint_id"]]
        if len(matches) != 1 or matches[0].get("baseUrl", "") != message["origin"]:
            raise ProofError("configured runtime endpoint does not match")
        headers = {"Content-Type": "application/json"}
        if message["api_key"]:
            headers["Authorization"] = "Bearer " + message["api_key"]
        payload = message["payload"]
        body = None if payload is None else json.dumps(payload, allow_nan=False).encode("utf-8")
        request = urllib.request.Request(message["origin"] + message["path"],
                                         data=body, headers=headers)
        opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), NoRedirect())
        limit, words = (ANSWER_LIMIT, "2 MiB") if message.get("envelope") else (LIMIT, "64 KiB")
        try:
            with opener.open(request, timeout=message["timeout"]) as response:
                status, body = response.status, response.read(limit + 1)
        except urllib.error.HTTPError as exc:
            # Redirects are never followed. Only an envelope request reports an
            # error status, with its bounded body, instead of failing.
            if not message.get("envelope") or 300 <= exc.code < 400:
                raise
            status, body = exc.code, exc.read(limit + 1)
        if len(body) > limit:
            raise ProofError("runtime response exceeds " + words)
        text = body.decode("utf-8")
        if message.get("envelope"):
            # JSON escaping can at most double the body.
            body = json.dumps({"status": status, "body": text}, ensure_ascii=False).encode("utf-8")
            if len(body) > 2 * limit + 64:
                raise ProofError("runtime response exceeds " + words)
        sys.stdout.buffer.write(body)
        sys.stdout.buffer.flush()
    finally:
        timer.cancel()

try:
    serve()
except urllib.error.HTTPError as exc:
    sys.stderr.write("runtime returned HTTP %d\n" % exc.code)
    sys.exit(1)
except ProofError as exc:
    sys.stderr.write("runtime transport failed: %s\n" % exc)
    sys.exit(1)
except (OSError, ValueError, KeyError, TypeError) as exc:
    sys.stderr.write("runtime transport failed: %s\n" % type(exc).__name__)
    sys.exit(1)
'''


def _docker(arguments: list[str], *, timeout: float, data: bytes | None = None,
            output_limit: int = _LIMIT) -> bytes:
    result = subprocess.run(["docker", *arguments], input=data, capture_output=True,
                            timeout=timeout, check=False)
    if len(result.stdout) > output_limit or len(result.stderr) > _LIMIT:
        raise OSError("Docker runtime transport output exceeds its size limit")
    if result.returncode:
        detail = result.stderr.decode("utf-8", errors="replace").strip()[:512]
        raise OSError(f"Docker runtime transport failed (exit {result.returncode}): {detail}")
    return result.stdout


def _owned_router(install_dir: Path, project: str) -> str:
    candidates = _docker(["ps", "--no-trunc", "--quiet", "--filter", "status=running",
                          "--filter", f"label=com.docker.compose.project={project}",
                          "--filter", "label=com.docker.compose.service=model-router"],
                         timeout=10).decode("ascii").splitlines()
    if len(candidates) != 1 or not re.fullmatch(r"[0-9a-f]{64}", candidates[0]):
        raise OSError("Expected exactly one running ODS model-router container")
    container_id = candidates[0]
    info = json.loads(_docker(["inspect", "--type", "container", "--format", _INSPECT,
                              container_id], timeout=10))
    if (not isinstance(info, dict) or info.get("Id") != container_id or info.get("Running") is not True
            or info.get("Project") != project or info.get("Service") != "model-router"):
        raise OSError("ODS model-router ownership or running state changed")
    all_mounts = info.get("Mounts")
    if not isinstance(all_mounts, list) or any(not isinstance(mount, dict) for mount in all_mounts):
        raise OSError("ODS model-router mount ownership metadata is invalid")
    root = Path(install_dir).resolve()
    for target, relative in (("/state", "data"),
                             ("/config", "config/model-router")):
        mounts = [mount for mount in all_mounts if mount.get("Destination") == target]
        if (len(mounts) != 1 or mounts[0].get("Type") != "bind"
                or mounts[0].get("RW") is not False
                or not isinstance(mounts[0].get("Source"), str)
                or Path(mounts[0].get("Source", "")) != root / relative):
            raise OSError(f"ODS model-router mount does not belong to this installation: {target}")
    return container_id


def request(install_dir: Path, origin: str, path: str,
            payload: dict | None = None, api_key: str = "", timeout: float = 5,
            *, project: str = "ods", endpoint_id: str = ENDPOINT_ID) -> str:
    """Return bounded HTTP text; never grant readiness or publish model state.

    ``origin`` is the base URL, without a path, of the router endpoint
    ``endpoint_id``; ``path`` is one of the proof and telemetry routes. An HTTP
    error status fails the request.
    """
    return _send(install_dir, origin, path, payload, api_key, timeout, project,
                 envelope=False, endpoint_id=endpoint_id)


def exchange(install_dir: Path, origin: str, path: str,
             payload: dict | None = None, api_key: str = "", timeout: float = 5,
             *, project: str = "ods", endpoint_id: str = ENDPOINT_ID) -> tuple[int, str]:
    """Return ``(http_status, text)``, error statuses included, for capability probes.

    Same ownership, route and size rules as ``request``. A model's own error
    text (llama-server's 400 "Unable to generate parser for this template")
    is evidence a probe classifies, so it comes back instead of failing.
    Redirects are still refused.
    """
    raw = _send(install_dir, origin, path, payload, api_key, timeout, project,
                envelope=True, endpoint_id=endpoint_id)
    try:
        envelope = json.loads(raw)
    except ValueError as exc:
        raise OSError("Runtime transport returned a malformed envelope") from exc
    status = envelope.get("status") if isinstance(envelope, dict) else None
    text = envelope.get("body") if isinstance(envelope, dict) else None
    if not isinstance(status, int) or isinstance(status, bool) or not isinstance(text, str):
        raise OSError("Runtime transport returned a malformed envelope")
    return status, text


def _send(install_dir: Path, origin: str, path: str, payload: dict | None, api_key: str,
          timeout: float, project: str, *, envelope: bool, endpoint_id: str) -> str:
    if endpoint_id not in _ENDPOINT_IDS:
        raise ValueError("Unsupported runtime endpoint")
    parsed = urlsplit(origin)
    if (parsed.scheme not in {"http", "https"} or not parsed.hostname
            or parsed.username is not None or parsed.password is not None
            or parsed.query or parsed.fragment or parsed.path != ""
            or any(ord(char) <= 32 or ord(char) == 127 for char in origin)):
        raise ValueError("Runtime origin must be a credential-free HTTP(S) origin without a path")
    _ = parsed.port
    if path not in _PATHS:
        raise ValueError("Unsupported runtime observation route")
    if (path == "/v1/chat/completions") != isinstance(payload, dict):
        raise ValueError("Only a chat-completion proof accepts a JSON payload")
    if not isinstance(api_key, str) or "\r" in api_key or "\n" in api_key:
        raise ValueError("Invalid runtime API key")
    if not math.isfinite(timeout) or not 0 < timeout <= 900:
        raise ValueError("Runtime proof timeout must be between 0 and 900 seconds")
    if not re.fullmatch(r"[a-z0-9][a-z0-9_-]*", project):
        raise ValueError("Invalid Compose project")
    message = dict(endpoint_id=endpoint_id, origin=origin, path=path, payload=payload,
                   api_key=api_key, timeout=timeout)
    if envelope:
        message["envelope"] = True
    data = json.dumps(message, allow_nan=False).encode("utf-8")
    if len(data) > _LIMIT:
        raise ValueError("Runtime proof request exceeds 64 KiB")
    container_id = _owned_router(install_dir, project)
    body = _docker(["exec", "-i", container_id, "python", "-I", "-S", "-c", _WORKER],
                   data=data, timeout=timeout + 10,
                   output_limit=2 * _ANSWER_LIMIT + 64 if envelope else _LIMIT)
    return body.decode("utf-8")