//! Bounded, authenticated inference-process transport. CUDA owners stay in Backend. use futures_util::Stream; use leleka_backend_api as api; use prost::Message; use sha2::{Digest, Sha256}; use std::{ collections::HashMap, pin::Pin, sync::{Arc, Mutex}, task::{Context, Poll}, }; use subtle::ConstantTimeEq; use tokio::{ net::TcpListener, sync::{OwnedSemaphorePermit, Semaphore, broadcast, mpsc}, }; use tokio_stream::wrappers::ReceiverStream; use tonic::{Request, Response, Status}; use uuid::Uuid; pub(crate) use leleka_runtime_client::convert::{literal_loopback, refusal, valid_token}; pub use leleka_runtime_client::{ FRAME_LIMIT, MESSAGE_LIMIT, PROTOCOL_VERSION, convert::{ config_from_proto, config_to_proto, descriptor_from_proto, descriptor_to_proto, loaded_to_proto, mode_to_proto, }, proto, }; const MAX_IDS: usize = 256; const EVENT_QUEUE: usize = 16; type WireStream = Pin> + Send>>; #[derive(Clone)] pub struct RuntimeServer { inner: Arc, } struct Inner { backend: Mutex>, token: String, state: Mutex, changes: broadcast::Sender, controls: Arc, watchers: Arc, } struct State { epoch: String, sequence: u64, loaded: Option, capabilities: api::BackendCapabilities, operations: HashMap, requests: api::RecentIds, lifecycle_busy: bool, active: Option, stopping: bool, /// The backend's prefix-cache summary as of the last observation (`Backend::prefix_cache_summary`). prefix_cache_json: String, /// The backend's memory-governor report as of the last observation (`Backend::memory_report`). memory_json: String, /// The backend's telemetry document as of the last observation (`Backend::telemetry_report`). telemetry_json: String, } struct Operation { digest: [u8; 32], result: proto::OperationResult, } /// A load/unload worker that panics must not leave the lifecycle gate shut /// with a stale "accepted" operation: it ends as indeterminate and model /// admission closes, exactly like any other uncertain lifecycle outcome. struct LifecycleGuard { inner: Arc, operation_id: String, /// Disarmed under the same state lock that publishes the operation's own /// completion, so a worker that finished can never touch a later operation. armed: bool, } struct AcceptanceGuard { inner: Arc, id: String, armed: bool, } struct GenerationResponse { receiver: ReceiverStream>, inner: Arc, id: String, terminal: bool, } #[cfg(test)] mod tests; mod lock; #[allow(unused_imports)] pub use lock::*; mod server; #[allow(unused_imports)] pub use server::*; mod clear_active; mod generate_rpc; mod model_rpc; mod service; mod session_rpc; #[allow(unused_imports)] pub use clear_active::*; mod event_to_proto; #[allow(unused_imports)] pub use event_to_proto::*;