#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Policy {
#[default]
Supported,
Certified,
Reference,
RequireAccelerated,
Experimental,
}
impl Policy {
pub(crate) fn admits_unjudged_device(self) -> bool {
matches!(self, Self::Supported | Self::RequireAccelerated)
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Device {
#[default]
Auto,
Cpu,
Cuda(u32),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum GpuDelivery {
#[default]
Auto,
Prebuilt,
Jit,
Disabled,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Backend {
HuggingFace,
FastCpu,
Gpu,
}
impl Backend {
pub(crate) fn from_internal(value: &str) -> Self {
match value {
"gpu" => Self::Gpu,
"fast_cpu" => Self::FastCpu,
_ => Self::HuggingFace,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum ReasonCode {
InputBelowGpuThreshold,
InputAddedToken,
InputGuardRouted,
ExecutionFault,
InputPostprocessRouted,
SessionNoSafeCut,
InvalidPriorState,
GpuUnavailable,
GpuUncertified,
CpuUncertified,
RuntimeBuildUncertified,
ReferenceRequested,
Other(String),
}
pub(crate) const R_SESSION_NO_SAFE_CUT: &str = "R_SESSION_NO_SAFE_CUT";
pub(crate) const R_INVALID_PRIOR_STATE: &str = "R_INVALID_PRIOR_STATE";
impl ReasonCode {
pub(crate) fn from_ledger_code(code: &str) -> Self {
match code {
toktier_routing_core::R_INPUT_BELOW_GPU_THRESHOLD => Self::InputBelowGpuThreshold,
toktier_routing_core::R_INPUT_ADDED_TOKEN => Self::InputAddedToken,
toktier_routing_core::R_INPUT_GUARD_ROUTED => Self::InputGuardRouted,
toktier_routing_core::R_EXEC_FAULT => Self::ExecutionFault,
toktier_routing_core::R_INPUT_POSTPROCESS_ROUTED => Self::InputPostprocessRouted,
R_SESSION_NO_SAFE_CUT => Self::SessionNoSafeCut,
R_INVALID_PRIOR_STATE => Self::InvalidPriorState,
other => Self::Other(other.to_owned()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum Certification {
Certified,
CertifiedSource,
Supported,
LocallyVerified,
Experimental,
Reference,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct RoutePlan {
pub family: String,
pub artifact_sha256: String,
pub backends: Vec<Backend>,
pub gpu_min_bytes: u64,
pub certification: Certification,
pub reasons: Vec<ReasonCode>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct ExecutionFacts {
pub backend: Backend,
pub path: String,
pub source: Option<String>,
pub input_bytes: u64,
pub certification: Certification,
pub reason: Option<ReasonCode>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct DoctorFacts {
pub crate_version: String,
pub oracle: String,
pub registry_verified: bool,
pub python_required: bool,
pub sqlite_compiled: bool,
pub prebuilt_gpu_compiled: bool,
pub jit_compiled: bool,
pub network_compiled: bool,
pub runtime_build: RuntimeBuildFacts,
pub cuda: Option<CudaFacts>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct RuntimeBuildFacts {
pub source_digest: String,
pub fast_cpu_source_digest: String,
pub native_host_source_digest: String,
pub toolchain: String,
pub build_flags: Vec<String>,
pub dependency_closure: String,
pub build_flag_divergence: Option<String>,
pub certified: bool,
pub core_closure: String,
pub dependency_advisory: Option<String>,
pub behavior_versions: Vec<BehaviorVersion>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct BehaviorVersion {
pub unit: String,
pub observed: Option<String>,
pub judged: String,
pub source: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct CudaFacts {
pub device_ordinal: u32,
pub available: bool,
pub architecture: Option<String>,
pub driver_api_version: Option<i32>,
pub error: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub struct StoreStats {
pub format: String,
pub session_count: u64,
pub node_count: u64,
pub puts: u64,
pub appends: u64,
pub forks: u64,
pub lookups: u64,
pub lookup_hits: u64,
pub lookup_misses: u64,
pub checksum_rejects: u64,
pub sessions_evicted: u64,
pub import_rejects: u64,
pub revision_conflicts: u64,
pub path_counts: BTreeMap<String, u64>,
}
impl From<toktier_store_core::StatsSnapshot> for StoreStats {
fn from(value: toktier_store_core::StatsSnapshot) -> Self {
Self {
format: value.format.to_owned(),
session_count: value.session_count,
node_count: value.node_count,
puts: value.puts,
appends: value.extends,
forks: value.forks,
lookups: value.lookups,
lookup_hits: value.lookup_hits,
lookup_misses: value.lookup_misses,
checksum_rejects: value.checksum_rejects,
sessions_evicted: value.sessions_evicted,
import_rejects: value.import_rejects,
revision_conflicts: value.revision_conflicts,
path_counts: value.path_counts,
}
}
}
use std::collections::BTreeMap;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_recorded_reason_maps_onto_the_frozen_namespace() {
for (code, expected) in [
(
toktier_routing_core::R_INPUT_BELOW_GPU_THRESHOLD,
ReasonCode::InputBelowGpuThreshold,
),
(
toktier_routing_core::R_INPUT_ADDED_TOKEN,
ReasonCode::InputAddedToken,
),
(
toktier_routing_core::R_INPUT_GUARD_ROUTED,
ReasonCode::InputGuardRouted,
),
(
toktier_routing_core::R_EXEC_FAULT,
ReasonCode::ExecutionFault,
),
(
toktier_routing_core::R_INPUT_POSTPROCESS_ROUTED,
ReasonCode::InputPostprocessRouted,
),
] {
assert_eq!(ReasonCode::from_ledger_code(code), expected, "for {code}");
}
assert_eq!(
ReasonCode::from_ledger_code(R_INVALID_PRIOR_STATE),
ReasonCode::InvalidPriorState
);
assert_eq!(
ReasonCode::from_ledger_code("R_SOMETHING_LATER"),
ReasonCode::Other("R_SOMETHING_LATER".to_owned())
);
}
}