"""The daemon's HTTP control plane. camelCase JSON throughout. Loopback by default; an optional ``X-FT-Token`` shared secret gates everything except the daemon's own ``/health`` liveness probe. Handlers are ``async`` and push every blocking call to an executor so the event loop never blocks. Two executors: a small **lifecycle** pool for start/stop/switch, kept separate from the **proxy/metrics** pool, so a storm of health/metrics polls against a loading serve can never starve an operator's stop.""" from __future__ import annotations import asyncio import collections import functools import json import os import sys from concurrent.futures import ThreadPoolExecutor from typing import Any, Callable from fastapi import Depends, FastAPI, Header, HTTPException, Request from fastapi.responses import JSONResponse, StreamingResponse from pydantic import BaseModel from .accounting import AccountingOutboxError, AccountingPrepareError from .serve_manager import Conflict from .version import DAEMON_VERSION class StartBody(BaseModel): model: str port: int | None = None args: list[str] = [] apiKey: str | None = None class StopBody(BaseModel): force: bool = False class SwitchBody(StartBody): force: bool = False class AccountingAckBody(BaseModel): receiptId: str class CheckpointBody(BaseModel): id: str args: list[str] = [] class CancelBody(BaseModel): id: str class BenchBody(BaseModel): # Raw `ft bench bw` args (e.g. ["--dtype", "nvfp4", "--threshold", "2.5"]); empty = all dtypes. args: list[str] = [] def _bench_profile_path(gpu_uuid: str | None) -> str | None: # per-GPU profiles and no torch here: the serve's own card when its --gpu names one, else the newest file from freetoken.moe.bench_profile import default_profile_path, latest_profile_path # torch-free if gpu_uuid: path = default_profile_path(gpu_uuid) if os.path.isfile(path): return path return latest_profile_path() def _serve_gpu_uuid(args: list[str]) -> str | None: """The full UUID a serve's `--gpu` pins, or None when there is none or it cannot be resolved.""" for i, a in enumerate(args): val = a[len("--gpu="):] if a.startswith("--gpu=") else (args[i + 1] if a == "--gpu" and i + 1 < len(args) else None) if not val: continue from freetoken.gpu_select import resolve_gpu_uuids try: resolved = resolve_gpu_uuids([val]) except ValueError: return None if resolved: return resolved[0] # no NVML: a UUID value still keys the profile file (canonical prefix), an index cannot return "GPU-" + val[len("GPU-"):] if val.upper().startswith("GPU-") else None return None def _read_bench_profile(path: str | None) -> dict | None: if path is None: return None try: with open(path) as f: return json.load(f) except (OSError, ValueError): return None def _bench_sse(event: str, data) -> str: return f"event: {event}\ndata: {json.dumps(data)}\n\n" def _parse_ftbench(line: str) -> dict | None: """``FTBENCH