STRA-338 S20: EngineFamily seam in leleka-engine-core, Qwen35Cuda family in leleka-runtime, the scripted engine as leleka-engine-fake; the inference binary and the full-chain test start their engines through spawn::<F>. Ratchets raised by the Box<dyn Backend> signatures of the seam (core +2, fake +1, runtime +1) and 2 test lines: the Backend trait object is the process boundary the gRPC server already held

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 11:20:57 +01:00
parent 58e3d864e1
commit e049e24fe3
15 changed files with 194 additions and 15 deletions
Generated
+11
View File
@@ -1317,10 +1317,19 @@ dependencies = [
name = "leleka-engine-core"
version = "0.1.0"
dependencies = [
"leleka-backend-api",
"serde",
"serde_json",
]
[[package]]
name = "leleka-engine-fake"
version = "0.1.0"
dependencies = [
"leleka-backend-api",
"leleka-engine-core",
]
[[package]]
name = "leleka-experimental-ref"
version = "0.1.0"
@@ -2858,6 +2867,8 @@ dependencies = [
"hex",
"jsonschema",
"leleka-backend-api",
"leleka-engine-core",
"leleka-engine-fake",
"leleka-gateway-text",
"leleka-obs",
"leleka-runtime",
+2 -2
View File
@@ -1,7 +1,7 @@
[workspace]
resolver = "3"
members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "control-plane", "desktop-core", "crates/libc-shim", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/cuda-driver", "crates/leleka-runtime", "crates/leleka-memory", "crates/deploy", "crates/obs", "crates/native-cpu-expert", "crates/native-parity-bench", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy"]
default-members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "control-plane", "desktop-core", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/leleka-memory", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy", "crates/deploy", "crates/obs"]
members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "crates/engine-fake", "control-plane", "desktop-core", "crates/libc-shim", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/cuda-driver", "crates/leleka-runtime", "crates/leleka-memory", "crates/deploy", "crates/obs", "crates/native-cpu-expert", "crates/native-parity-bench", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy"]
default-members = ["crates/backend-api", "crates/worker-protocol", "crates/cpu-kernels", "crates/cpu-sys", "crates/cpu-worker", "crates/gguf", "crates/gptoss", "crates/gptoss-igpu", "crates/harmony", "crates/igpu", "crates/kar-codec", "crates/igpu-worker", "crates/shm", "crates/openvino", "crates/npu-service", "crates/gptoss-npu", "crates/npu-worker", "crates/gpu-npu-worker", "crates/tokenizer", "crates/gateway-text", "crates/gateway-domain", "crates/engine-core", "crates/engine-fake", "control-plane", "desktop-core", "crates/runtime-client", "crates/model-format", "crates/offline-tools", "crates/quant-core", "crates/context", "crates/leleka-memory", "crates/sampler-ref", "crates/experimental-ref", "crates/serving-policy", "crates/deploy", "crates/obs"]
[workspace.package]
edition = "2024"
+4 -3
View File
@@ -120,7 +120,7 @@
"desktop-core/src/bin/leleka-inference.rs": 1487,
"desktop-core/src/runtime_rpc/impl_proto_runtime_server_runtime.rs": 463,
"desktop-core/src/runtime_rpc/tests.rs": 628,
"desktop-core/tests/api_over_runtime.rs": 644,
"desktop-core/tests/api_over_runtime.rs": 646,
"desktop-core/tests/gateway.rs": 403,
"desktop-core/tests/managed_worker.rs": 500,
"desktop-core/tests/native_tools_retry_sdet.rs": 552,
@@ -132,7 +132,8 @@
"dyn-trait:leleka-cpu-worker": 4,
"dyn-trait:leleka-cuda-driver": 48,
"dyn-trait:leleka-deploy": 17,
"dyn-trait:leleka-engine-core": 17,
"dyn-trait:leleka-engine-core": 19,
"dyn-trait:leleka-engine-fake": 1,
"dyn-trait:leleka-experimental-ref": 3,
"dyn-trait:leleka-gateway-domain": 5,
"dyn-trait:leleka-gptoss": 10,
@@ -141,7 +142,7 @@
"dyn-trait:leleka-gpu-npu-worker": 6,
"dyn-trait:leleka-memory": 17,
"dyn-trait:leleka-model-format": 8,
"dyn-trait:leleka-runtime": 51,
"dyn-trait:leleka-runtime": 52,
"dyn-trait:leleka-tokenizer": 2,
"dyn-trait:strata-control": 16,
"dyn-trait:strata-desktop-core": 2,
+2 -1
View File
@@ -15,7 +15,8 @@
"L3": [
"leleka-libc-shim", "leleka-model-format", "leleka-cuda-driver", "leleka-native-cpu-expert", "leleka-gptoss", "leleka-igpu",
"leleka-gptoss-igpu", "leleka-gptoss-npu", "leleka-npu-service", "leleka-cpu-worker", "leleka-igpu-worker", "leleka-npu-worker",
"leleka-gpu-npu-worker", "leleka-offline-tools", "leleka-runtime-client"
"leleka-gpu-npu-worker", "leleka-offline-tools", "leleka-runtime-client",
"leleka-engine-fake"
],
"L4": ["leleka-runtime", "strata-desktop-core", "strata-control", "leleka-deploy", "leleka-native-parity-bench"]
},
+2 -1
View File
@@ -1,12 +1,13 @@
[package]
name = "leleka-engine-core"
version = "0.1.0"
description = "Pure domain of the Leleka serving engine: the cross-session prefix cache, host-memory admission. No CUDA, no async runtime, no env, no filesystem, no clock."
description = "Pure domain of the Leleka serving engine: the cross-session prefix cache, conversation policy, prompt plan, speculative-decoding policies, request lifecycle and the engine-family seam. No CUDA, no async runtime, no env, no filesystem, no clock."
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
leleka-backend-api = { path = "../backend-api" }
serde = { version = "1", features = ["derive"] }
[features]
+25
View File
@@ -0,0 +1,25 @@
//! The engine family seam: what the composition root needs to start an engine, whatever model family and device sit behind
//! it. A family is a zero-sized marker that knows its start-up configuration and how to turn it into a running
//! [`Backend`]; the composition root is generic over it (`spawn::<F>`), so the choice of family is one `match` and every
//! family below it is monomorphised, with no trait object between the root and the family's own code.
//!
//! Adapters: `leleka_runtime::backend::Qwen35Cuda` (the product engine) and `leleka-engine-fake` (a scripted engine for
//! GPU-free tests).
use leleka_backend_api::{Backend, BackendError};
/// One model family on one device stack.
pub trait EngineFamily {
/// Everything the family needs to start; owned by the caller until [`EngineFamily::spawn`].
type Config;
/// Stable name for logs and status.
const NAME: &'static str;
/// Start the engine; the returned backend owns its device state and threads.
fn spawn(config: Self::Config) -> Result<Box<dyn Backend>, BackendError>;
}
/// Start the engine of family `F`.
pub fn spawn<F: EngineFamily>(config: F::Config) -> Result<Box<dyn Backend>, BackendError> {
F::spawn(config)
}
+1
View File
@@ -4,6 +4,7 @@
//! The modules keep the names and paths they had inside `leleka-runtime`, which re-exports them under the old paths.
pub mod conversation;
pub mod family;
pub mod memory;
pub mod prefix_cache;
pub mod prompt;
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "leleka-engine-fake"
version = "0.1.0"
description = "A scripted engine family for GPU-free tests of everything above the engine. Never a dependency of a production crate."
edition.workspace = true
license.workspace = true
rust-version.workspace = true
[dependencies]
leleka-backend-api = { path = "../backend-api", features = ["scripted"] }
leleka-engine-core = { path = "../engine-core" }
[lints]
workspace = true
+45
View File
@@ -0,0 +1,45 @@
//! The scripted engine as an [`EngineFamily`], so the composition root starts it exactly as it starts the product engine
//! (`spawn::<EngineFake>`) and a test drives everything above the engine without a GPU, a model or a gRPC server.
//!
//! It computes nothing and makes no statement about any model: what it returns is what the test scripted through the
//! [`ScriptedHandle`] it receives at start-up.
use std::sync::mpsc::{Receiver, Sender, channel};
use leleka_backend_api::{
Backend, BackendError, ModelDescriptor,
scripted::{ScriptedEngine, ScriptedHandle},
};
use leleka_engine_core::family::EngineFamily;
/// The scripted engine family.
pub struct EngineFake;
/// How to start [`EngineFake`]: the model it claims to serve and where to send the handle that scripts and observes it.
pub struct FakeStart {
pub descriptor: ModelDescriptor,
pub handle: Sender<ScriptedHandle>,
}
impl FakeStart {
/// The fixture model of the scripted engine, and the receiving end of its handle.
pub fn fixture() -> (Self, Receiver<ScriptedHandle>) {
let (handle, receiver) = channel();
(Self { descriptor: ScriptedEngine::fixture_descriptor(), handle }, receiver)
}
}
impl EngineFamily for EngineFake {
type Config = FakeStart;
const NAME: &'static str = "engine-fake";
fn spawn(config: FakeStart) -> Result<Box<dyn Backend>, BackendError> {
let (engine, handle) = ScriptedEngine::new(config.descriptor);
// the receiver may be gone when a test does not script the engine
let _ = config.handle.send(handle);
Ok(Box::new(engine))
}
}
#[cfg(test)]
mod tests;
+47
View File
@@ -0,0 +1,47 @@
use super::*;
use leleka_backend_api::{
FinishReason, GenerationEvent, GenerationRequest, GenerationTerminal, InferenceMode, RuntimeConfig, Usage,
scripted::Script,
};
use leleka_engine_core::family::spawn;
fn config() -> RuntimeConfig {
RuntimeConfig {
load_config_version: leleka_backend_api::LOAD_CONFIG_VERSION,
inference_mode: InferenceMode::Native,
gpu_memory_limit_bytes: None,
allow_experimental: false,
}
}
#[test]
fn the_family_starts_through_the_composition_root_and_plays_its_script() {
let (start, receiver) = FakeStart::fixture();
let mut backend = spawn::<EngineFake>(start).expect("the fake engine always starts");
let handle = receiver.recv().expect("the handle is delivered at start-up");
let usage = Usage { input_tokens: 3, output_tokens: 2, reused_input_tokens: 0 };
handle.push(Script::stop_with(&["he", "llo"], usage));
let model = backend.load(ScriptedEngine::fixture_descriptor(), config()).expect("the fixture model loads");
let request = GenerationRequest {
request_id: "r1".into(),
prompt: "p".into(),
max_output_tokens: 8,
temperature_milli: 0,
sampling: Default::default(),
};
let mut stream = backend.start_generation(&model, request).expect("a loaded engine admits a request");
let mut text = String::new();
let mut terminal = None;
while let Ok(Some(event)) = stream.recv() {
match event {
GenerationEvent::TextDelta { text: delta } => text.push_str(&delta),
GenerationEvent::Terminal(t) => terminal = Some(t),
_ => {}
}
}
assert_eq!(text, "hello");
assert!(matches!(terminal, Some(GenerationTerminal::Completed { reason: FinishReason::Stop, .. })));
assert_eq!(handle.requests().len(), 1);
assert_eq!(<EngineFake as EngineFamily>::NAME, "engine-fake");
}
+2
View File
@@ -62,6 +62,8 @@ mod spawn_optimized;
pub use spawn_optimized::*;
mod spawn_original;
pub use spawn_original::*;
mod family;
pub use family::{Qwen35Cuda, Qwen35CudaStart};
mod sources;
pub use sources::*;
mod proxy;
@@ -0,0 +1,27 @@
//! The Qwen3.5 CUDA engine as an [`EngineFamily`] of the composition root (STRA-338 S20). Starting it is the existing
//! `spawn_backend*` code; this file only gives the choice between its two product entry points a single typed door.
use super::*;
use leleka_engine_core::family::EngineFamily;
/// The product engine: Qwen3.5 on the CUDA owner thread.
pub struct Qwen35Cuda;
/// How to start [`Qwen35Cuda`].
pub enum Qwen35CudaStart {
/// The default native backend (`spawn_backend`).
Native(NativeBackendConfig),
/// The Original serve path (`spawn_backend_original_serve`), with its options.
OriginalServe(NativeBackendConfig, OriginalServeOptions),
}
impl EngineFamily for Qwen35Cuda {
type Config = Qwen35CudaStart;
const NAME: &'static str = "qwen35-cuda";
fn spawn(config: Qwen35CudaStart) -> Result<Box<dyn Backend>, BackendError> {
match config {
Qwen35CudaStart::Native(native) => spawn_backend(native),
Qwen35CudaStart::OriginalServe(native, options) => spawn_backend_original_serve(native, options),
}
}
}
+2
View File
@@ -11,6 +11,7 @@ path = "src/main.rs"
[dependencies]
leleka-backend-api = { path = "../crates/backend-api" }
leleka-engine-core = { path = "../crates/engine-core" }
leleka-obs = { path = "../crates/obs" }
leleka-runtime = { path = "../crates/leleka-runtime" }
leleka-runtime-client = { path = "../crates/runtime-client" }
@@ -43,6 +44,7 @@ jsonschema = { version = "=0.58.5", default-features = false }
rustix = { version = "=1.1.5", features = ["fs"] }
[dev-dependencies]
leleka-engine-fake = { path = "../crates/engine-fake" }
# The full chain test: the real API router in front of the real RuntimeServer and a scripted engine.
leleka-backend-api = { path = "../crates/backend-api", features = ["scripted"] }
strata-control = { path = "../control-plane" }
+6 -6
View File
@@ -1,8 +1,8 @@
//! Linux CUDA inference process. Operator-owned artifact paths never enter RPCs.
use leleka_runtime::backend::{
InheritedKarArchive, NativeBackendConfig, OriginalServeChoice, OriginalServeMode,
OriginalServeOptions, RankedExpertProfileSource, original_serve_choice, set_archive_attested,
set_verify_stamps, spawn_backend,
OriginalServeOptions, Qwen35Cuda, Qwen35CudaStart, RankedExpertProfileSource, original_serve_choice,
set_archive_attested, set_verify_stamps,
spawn_backend_experimental_native_integer_iq3_windowed256_retained_gr,
spawn_backend_experimental_optimized_windowed256,
spawn_backend_experimental_optimized_windowed256_adaptive_slots,
@@ -10,9 +10,9 @@ use leleka_runtime::backend::{
spawn_backend_experimental_optimized_windowed256_resident_owners,
spawn_backend_experimental_optimized_windowed256_retained_scratch,
spawn_backend_experimental_optimized_windowed256_streamed_spill,
spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots,
spawn_backend_original_serve, verify_sources_only,
spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots, verify_sources_only,
};
use leleka_engine_core::family::spawn;
use std::{net::SocketAddr, path::PathBuf};
use strata_desktop_core::runtime_rpc::RuntimeServer;
#[path = "../evaluation_measurement.rs"]
@@ -214,7 +214,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("leleka-inference: backend=preview ({reason})");
}
let backend = if matches!(choice, OriginalServeChoice::Original) {
spawn_backend_original_serve(config, original_options)
spawn::<Qwen35Cuda>(Qwen35CudaStart::OriginalServe(config, original_options))
} else if let Some(observer) = observer {
spawn_backend_observed_experimental_optimized_windowed256_adaptive_slots(
config,
@@ -251,7 +251,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} else if experimental_integer_windowed {
spawn_backend_experimental_native_integer_iq3_windowed256_retained_gr(config)
} else {
spawn_backend(config)
spawn::<Qwen35Cuda>(Qwen35CudaStart::Native(config))
}
.map_err(|error| {
eprintln!("leleka-inference: {}", error.message);
+4 -2
View File
@@ -106,8 +106,10 @@ struct Runtime {
impl Runtime {
async fn start(address: Option<SocketAddr>) -> Self {
let (engine, handle) = ScriptedEngine::new(ScriptedEngine::fixture_descriptor());
let server = RuntimeServer::new(Box::new(engine), TOKEN.into()).unwrap();
let (start, handle) = leleka_engine_fake::FakeStart::fixture();
let engine = leleka_engine_core::family::spawn::<leleka_engine_fake::EngineFake>(start).unwrap();
let handle = handle.recv().unwrap();
let server = RuntimeServer::new(engine, TOKEN.into()).unwrap();
let listener =
tokio::net::TcpListener::bind(address.unwrap_or("127.0.0.1:0".parse().unwrap()))
.await