Files
ModelForge/backend/src/modelforge_api/domain/runtime.py
T

250 lines
7.8 KiB
Python

from __future__ import annotations
import re
import uuid
from datetime import datetime
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
RuntimeAdapterName = Literal[
"sentence_transformers",
"qwen3_reranker",
"transformers_trocr",
"transformers_siglip2",
"transformers_whisper",
"transformers",
"vllm",
"llama_cpp",
"diffusers",
"custom",
]
CompatibilityStatus = Literal["compatible", "incompatible", "unknown", "requires_probe", "blocked"]
ProbeStatus = Literal[
"queued",
"preparing",
"loading",
"healthchecking",
"ready",
"unloading",
"completed",
"failed",
"cancelled",
]
class RuntimeModel(BaseModel):
model_config = ConfigDict(extra="forbid", from_attributes=True)
class RuntimeEnvironmentCreate(RuntimeModel):
name: str = Field(min_length=1, max_length=255)
adapter: RuntimeAdapterName
runtime_version: str = Field(min_length=1, max_length=128)
image_repository: str = Field(min_length=1, max_length=255)
image_digest: str = Field(pattern=r"^sha256:[a-f0-9]{64}$")
python_version: str = Field(min_length=1, max_length=64)
cuda_runtime_version: str | None = Field(default=None, max_length=64)
package_versions: dict[str, str] = Field(default_factory=dict)
supported_model_types: list[str] = Field(default_factory=list)
supported_formats: list[str] = Field(default_factory=list)
supported_modalities: list[str] = Field(default_factory=list)
network_policy: Literal["offline_control_plane_only"] = "offline_control_plane_only"
class RuntimeEnvironmentResponse(RuntimeEnvironmentCreate):
id: uuid.UUID
fingerprint: str
immutable_at: datetime
created_at: datetime
class RuntimeProfileCreate(RuntimeModel):
name: str = Field(min_length=1, max_length=255)
runtime_environment_id: uuid.UUID
artifact_set_id: uuid.UUID
dtype: Literal["float32", "float16", "bfloat16"] = "bfloat16"
quantization: str | None = Field(default=None, max_length=64)
modality: Literal[
"embedding", "reranking", "text_generation", "vision", "document", "audio", "diffusion"
]
max_sequence_length: int = Field(default=128, ge=1, le=131072)
batch_size: int = Field(default=1, ge=1, le=128)
concurrency: int = Field(default=1, ge=1, le=128)
device_policy: Literal["cuda_required", "cuda_preferred", "cpu_only"] = "cuda_required"
gpu_memory_policy: dict[str, Any] = Field(default_factory=dict)
launch_parameters: dict[str, Any] = Field(default_factory=dict)
environment_variables: dict[str, str] = Field(default_factory=dict)
trust_remote_code: Literal[False] = False
network_egress: Literal[False] = False
@field_validator("environment_variables")
@classmethod
def reject_secrets(cls, value: dict[str, str]) -> dict[str, str]:
forbidden = {"TOKEN", "SECRET", "PASSWORD", "KEY", "CREDENTIAL"}
if any(
forbidden.intersection(filter(None, re.split(r"[^A-Z0-9]+", key.upper())))
for key in value
):
raise ValueError("runtime profile environment cannot contain secrets")
return value
@model_validator(mode="after")
def fixed_probe_shape(self) -> RuntimeProfileCreate:
if self.modality == "embedding" and self.batch_size != 1:
raise ValueError("M4 embedding probes require batch_size=1")
return self
class RuntimeProfileResponse(RuntimeProfileCreate):
id: uuid.UUID
adapter: str
runtime_version: str
image_digest: str
version: int
fingerprint: str
health_contract: dict[str, Any]
immutable_at: datetime
created_at: datetime
class CompatibilityAssessmentCreate(RuntimeModel):
runtime_profile_id: uuid.UUID
compute_node_id: uuid.UUID
class CompatibilityAssessmentResponse(RuntimeModel):
id: uuid.UUID
artifact_set_id: uuid.UUID
runtime_profile_id: uuid.UUID
compute_node_id: uuid.UUID
adapter: str
runtime_version: str
status: CompatibilityStatus
static_result: dict[str, Any]
evidence: dict[str, Any]
blockers: list[str]
warnings: list[str]
required_approvals: list[str]
hardware_facts: dict[str, Any]
artifact_facts: dict[str, Any]
environment_fingerprint: str
stale: bool
stale_reason: str | None
created_at: datetime
class ExecutionApprovalCreate(RuntimeModel):
scope: Literal["lab_execution"] = "lab_execution"
reason: str = Field(min_length=8, max_length=2000)
approved_by: str = Field(min_length=1, max_length=255)
expires_at: datetime | None = None
class ExecutionApprovalResponse(RuntimeModel):
id: uuid.UUID
artifact_set_id: uuid.UUID
scope: str
status: str
evidence_fingerprint: str
reason: str
approved_by: str
approved_at: datetime
expires_at: datetime | None
revoked_at: datetime | None
stale: bool
class RuntimeProbeCreate(RuntimeModel):
compatibility_assessment_id: uuid.UUID
execution_approval_id: uuid.UUID
input_text: Literal["ModelForge runtime compatibility probe"] = (
"ModelForge runtime compatibility probe"
)
class RuntimeProbeResponse(RuntimeModel):
id: uuid.UUID
artifact_set_id: uuid.UUID
runtime_profile_id: uuid.UUID
compute_node_id: uuid.UUID
compatibility_assessment_id: uuid.UUID
execution_approval_id: uuid.UUID
status: ProbeStatus
phase: str | None
attempt_count: int
cancel_requested: bool
load_result: dict[str, Any]
health_result: dict[str, Any]
inference_result: dict[str, Any]
unload_result: dict[str, Any]
measured_resources: dict[str, Any]
runtime_facts: dict[str, Any]
environment_fingerprint: str
failure_code: str | None
failure_message: str | None
logs_reference: str | None
started_at: datetime | None
finished_at: datetime | None
created_at: datetime
updated_at: datetime
class DeploymentCandidateResponse(RuntimeModel):
id: uuid.UUID
artifact_set_id: uuid.UUID
runtime_profile_id: uuid.UUID
compute_node_id: uuid.UUID
compatibility_assessment_id: uuid.UUID
runtime_probe_id: uuid.UUID
channel: Literal["lab"]
status: Literal["lab_ready"]
production: Literal[False]
health_contract: dict[str, Any]
measured_resources: dict[str, Any]
created_at: datetime
class AgentRuntimeProbeLease(RuntimeModel):
probe_id: uuid.UUID
lease_token: str
lease_expires_at: datetime
artifact_set_id: uuid.UUID
revision_sha: str = Field(pattern=r"^[a-f0-9]{40,64}$")
artifact_root: str
artifact_relative_path: str
expected_manifest: dict[str, Any]
runtime_profile: dict[str, Any]
runtime_environment: dict[str, Any]
probe_input: Literal["ModelForge runtime compatibility probe"]
class AgentRuntimeProbeProgress(RuntimeModel):
lease_token: str = Field(min_length=32, max_length=512)
status: Literal["preparing", "loading", "healthchecking", "ready", "unloading"]
details: dict[str, Any] = Field(default_factory=dict)
class AgentRuntimeProbeControl(RuntimeModel):
accepted: bool
cancel_requested: bool
lease_expires_at: datetime
class AgentRuntimeProbeComplete(RuntimeModel):
lease_token: str = Field(min_length=32, max_length=512)
load_result: dict[str, Any]
health_result: dict[str, Any]
inference_result: dict[str, Any]
unload_result: dict[str, Any]
measured_resources: dict[str, Any]
runtime_facts: dict[str, Any]
environment_fingerprint: str = Field(pattern=r"^[a-f0-9]{64}$")
class AgentRuntimeProbeFailure(RuntimeModel):
lease_token: str = Field(min_length=32, max_length=512)
failure_code: str = Field(min_length=1, max_length=64)
failure_message: str = Field(min_length=1, max_length=2000)
details: dict[str, Any] = Field(default_factory=dict)