Files
leleka/desktop-core/src/runtime_rpc.rs
T
Pavlo Ratushnyi 6b5fcd242e STRA-338 S12b: fns and impl blocks of 59 cold oversize modules move into child modules (model-format, gateway, desktop-core, leaf libraries)
scripts/dev/split_module.py (lexer-based, with 16 self-tests) moves the top-level fn and impl items of 59 oversize module files in control-plane, desktop-core, model-format, gateway-text, leleka-memory, experimental-ref, context, serving-policy, tokenizer, backend-api, quant-core, harmony, igpu, libc-shim, npu-service and runtime-client into foo/<name>.rs groups of about 250 lines; types, constants, macros, nested modules and tests stay, every path stays valid through pub use re-exports, restricted visibilities keep their scope one level down, include paths and super:: paths are shifted. Not touched: leleka-runtime, cuda-driver, native-cpu-expert, sampler-ref and cpu-* (hot path, next wave) and the files kar-off-ab edits.

The baseline drops from 311 to 263 oversize files and from 273,600 to about 233,000 lines; the path-include ratchet ignores dir/name.rs child declarations.
2026-11-05 15:29:39 +01:00

107 lines
3.1 KiB
Rust

//! 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<T> = Pin<Box<dyn Stream<Item = Result<T, Status>> + Send>>;
#[derive(Clone)]
pub struct RuntimeServer {
inner: Arc<Inner>,
}
struct Inner {
backend: Mutex<Box<dyn api::Backend>>,
token: String,
state: Mutex<State>,
changes: broadcast::Sender<proto::Snapshot>,
controls: Arc<Semaphore>,
watchers: Arc<Semaphore>,
}
struct State {
epoch: String,
sequence: u64,
loaded: Option<api::LoadedModel>,
capabilities: api::BackendCapabilities,
operations: HashMap<String, Operation>,
requests: api::RecentIds,
lifecycle_busy: bool,
active: Option<String>,
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<Inner>,
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<Inner>,
id: String,
armed: bool,
}
struct GenerationResponse {
receiver: ReceiverStream<Result<proto::GenerationFrame, Status>>,
inner: Arc<Inner>,
id: String,
terminal: bool,
}
#[cfg(test)]
mod tests;
mod lock;
#[allow(unused_imports)]
pub use lock::*;
mod impl_runtimeserver;
#[allow(unused_imports)]
pub use impl_runtimeserver::*;
mod impl_proto_runtime_server_runtime;
#[allow(unused_imports)]
pub use impl_proto_runtime_server_runtime::*;
mod clear_active;
#[allow(unused_imports)]
pub use clear_active::*;
mod event_to_proto;
#[allow(unused_imports)]
pub use event_to_proto::*;