Files
leleka/desktop-core/src/runtime_rpc.rs
T
Pavlo Ratushnyi 7610ba3568 STRA-338 size-gw: the 19 oversize files of control-plane and desktop-core split by topic (no file over 400 lines); mechanical child modules renamed
Tests become directory modules named after what they test, with a shared fixtures/harness module:
control-plane/src/{responses,ggml_bridge,gguf_f32_stream,prepare_preview}/tests/, desktop-core/src/runtime_rpc/tests/;
integration targets become tests/<name>/main.rs plus topic modules and a harness (backend_worker_http, vision_http,
memory_gate_http, tool_corpus, mcp_hub, api_over_runtime, runtime_gateway_sdet, native_tools_sdet,
native_tools_retry_sdet, managed_worker, gateway); target names are unchanged. Sources: leleka-inference.rs becomes
src/bin/leleka-inference/ (main.rs plus one module per operator option group), the cp_replay example becomes
examples/cp_replay/, and the tonic service impl of runtime_rpc delegates to session_rpc, model_rpc and generate_rpc
(bodies unchanged). impl_* modules are renamed after what they hold (base_provenance, count_tokens, manifest_fetch,
fetcher, record, capture, in_flight, tool_policy, tool_plan, server). The self-spawning tests that name themselves
(credential_child, the managed worker fixture) use the new module path or stay at the target root.

Test ids: 543 before and after in both crates (strata-control, strata-desktop-core, --all-targets), 0 missing, 0 new,
0 outcome changes; 241 ids differ only in the module path. Warnings in the two crates: 0 before and after.
Counts (strata-control / strata-desktop-core): oversize files 19 -> 0 (13 + 6). Functions over 30 lines, complexity 10,
nesting 4, wide lines, prod: 187/135/17/199 -> 187/135/17/196 and 81/59/6/54 -> 81/59/6/54; test: 214/17/30/133 ->
214/17/30/133 and 65/3/3/30 -> 65/3/3/27. The 211 functions that moved verbatim are in limits-allowlist.json (owner
STRA-338) because the diff gate treats a moved function as added; the entries fall as the functions are shortened.
2026-11-06 13:13:59 +01:00

108 lines
3.0 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 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::*;