STRA-338 S19c: the conversation policy (cache, manager, snapshot, kv, state, model, binding) moves to leleka-engine-core::conversation; the 17 dyn-trait uses and 3 oversize test files move with it (runtime 68 -> 51, core 0 -> 17: a transfer, not growth); SessionDevice::available_memory becomes required, OriginalDevice reads the governor as before

This commit is contained in:
Pavlo Ratushnyi
2026-11-06 11:02:14 +01:00
parent d717c69777
commit 93afb07bfb
40 changed files with 100 additions and 83 deletions
+5 -4
View File
@@ -66,6 +66,9 @@
"crates/deploy/src/sealed.rs": 592,
"crates/deploy/src/supervisor.rs": 1863,
"crates/deploy/tests/e2e.rs": 2118,
"crates/engine-core/src/conversation/cache/tests.rs": 407,
"crates/engine-core/src/conversation/manager_tests.rs": 1129,
"crates/engine-core/src/conversation/snapshot_tests.rs": 1596,
"crates/engine-core/src/prefix_cache/store/tests.rs": 582,
"crates/experimental-ref/tests/emulated_kernels.rs": 467,
"crates/gateway-domain/src/responses/render.rs": 405,
@@ -81,9 +84,6 @@
"crates/igpu/src/shaders.rs": 440,
"crates/leleka-memory/src/lock.rs": 432,
"crates/leleka-runtime/src/backend_original_serve.rs": 1113,
"crates/leleka-runtime/src/conversation/cache/tests.rs": 407,
"crates/leleka-runtime/src/conversation/manager_tests.rs": 1129,
"crates/leleka-runtime/src/conversation/snapshot_tests.rs": 1596,
"crates/leleka-runtime/src/gpu_proofs/backend_original_serve_memory_smoke.rs": 851,
"crates/leleka-runtime/src/gpu_proofs/backend_original_serve_prefix_smoke.rs": 447,
"crates/leleka-runtime/src/gpu_proofs/backend_original_serve_smoke.rs": 979,
@@ -132,6 +132,7 @@
"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-experimental-ref": 3,
"dyn-trait:leleka-gateway-domain": 5,
"dyn-trait:leleka-gptoss": 10,
@@ -140,7 +141,7 @@
"dyn-trait:leleka-gpu-npu-worker": 6,
"dyn-trait:leleka-memory": 17,
"dyn-trait:leleka-model-format": 8,
"dyn-trait:leleka-runtime": 68,
"dyn-trait:leleka-runtime": 51,
"dyn-trait:leleka-tokenizer": 2,
"dyn-trait:strata-control": 16,
"dyn-trait:strata-desktop-core": 2,
@@ -6,7 +6,6 @@
use super::cache::{ConversationCheckpoint, ConversationKvReuse, SavedConversation};
use super::manager::SessionEngine;
use super::memory::available_memory;
use super::model::{ConvError, ConvResult, ModelGeometry, QsaDesc, SessionSpec, StateMemory};
use super::snapshot::{
ConversationView, RestoreOutcome, snapshot_bytes, snapshot_capture_bytes, snapshot_restore,
@@ -22,10 +21,9 @@ pub trait SessionDevice: StateMemory {
fn restore_draft(&mut self, resume: usize) -> Result<(), String>;
/// Control-vector enable state for the next request.
fn set_steering(&mut self, _enabled: bool) {}
/// Available host physical memory; unknown by default off Linux.
fn available_memory(&mut self) -> Option<u64> {
available_memory()
}
/// Available host physical memory right now; `None` when unknown. The runtime's devices read the memory governor
/// or `/proc/meminfo` (`leleka-runtime::conversation::memory::available_memory`).
fn available_memory(&mut self) -> Option<u64>;
}
/// [`SessionEngine`] over one session's geometry, carve and draft state.
@@ -229,7 +229,7 @@ impl ConversationBuffer {
/// Address of the first byte of the first segment (tests only).
#[cfg(test)]
pub(crate) fn first_address(&self) -> Option<*const u8> {
pub fn first_address(&self) -> Option<*const u8> {
self.segments.first().map(|segment| segment.as_ptr())
}
@@ -12,7 +12,7 @@ use super::cache::{
use super::manager::{ConversationManager, ConversationOptions, SessionEngine};
use super::model::{ConvError, ConvResult};
use super::snapshot::{ConversationView, RestoreOutcome};
use crate::backend::original_prompt::{PromptOptions, PromptPath, PromptPlan};
use crate::prompt::{PromptOptions, PromptPath, PromptPlan};
use serde_json::Value;
const PAYLOAD: usize = 100;
@@ -28,7 +28,7 @@ use super::memory::memory_admit;
use super::model::{ConvError, ConvResult};
use super::retention::eviction_victim;
use super::snapshot::{ConversationView, RestoreOutcome};
use crate::backend::original_prompt::{PromptAction, PromptPath};
use crate::prompt::{PromptAction, PromptPath};
/// Everything the policy needs from the device side.
pub trait SessionEngine {
@@ -210,7 +210,7 @@ impl ConversationManager {
/// and the turn/root checkpoint when the action ends its segment.
// Used by the Original-path prompt driver (the plan type is crate-private).
#[allow(dead_code)]
pub(crate) fn after_prompt_action(
pub fn after_prompt_action(
&mut self,
engine: &mut dyn SessionEngine,
ids: &[i32],
@@ -18,7 +18,7 @@ use super::model::{
use super::snapshot_tests::{Fixture, kv_region};
use super::state::state_sizes;
use super::testing::HostMemory;
use crate::backend::original_prompt::{PromptOptions, PromptPath, PromptPlan};
use crate::prompt::{PromptOptions, PromptPath, PromptPlan};
const TURN: i32 = 248045;
const MAX_CELLS: usize = 256;
@@ -0,0 +1,27 @@
//! Conversation snapshot, parked-conversation cache and prompt-resume policy:
//! the Rust port of Strata's `ConversationCache` family.
//!
//! The pure policy modules depend on `std` only, so they build and test on any
//! host; the CUDA-owner bindings live next to the session owners in `leleka-runtime`.
pub mod binding;
pub mod buffer;
pub mod cache;
pub mod checked;
#[cfg(test)]
mod golden_cache;
#[cfg(test)]
mod golden_manager;
pub mod kv;
pub mod manager;
#[cfg(test)]
mod manager_tests;
pub use crate::memory;
pub mod model;
pub mod retention;
pub mod snapshot;
#[cfg(test)]
mod snapshot_tests;
pub mod state;
#[cfg(any(test, feature = "testing"))]
pub mod testing;
@@ -334,7 +334,7 @@ impl std::error::Error for ConvError {}
pub type ConvResult<T> = Result<T, ConvError>;
pub(crate) fn fail<T>(message: &str) -> ConvResult<T> {
pub fn fail<T>(message: &str) -> ConvResult<T> {
Err(ConvError(format!("conversation snapshot: {message}")))
}
@@ -142,7 +142,7 @@ pub fn checkpoint_targets(
/// `exact_zeroed`: a zero-filled vector whose capacity is exactly `n` (the
/// cache charges capacity), reporting allocation failure instead of aborting.
pub(crate) fn exact_zeroed(n: usize) -> ConvResult<Vec<u8>> {
pub fn exact_zeroed(n: usize) -> ConvResult<Vec<u8>> {
let mut v = Vec::new();
v.try_reserve_exact(n)
.map_err(|_| ConvError("conversation snapshot: allocation failed".into()))?;
+1
View File
@@ -3,6 +3,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 memory;
pub mod prefix_cache;
pub mod prompt;
+47
View File
@@ -1,6 +1,53 @@
//! Host physical-memory admission, the pure half of `conversation_memory.{hpp,cpp}`.
use std::io::BufRead;
/// Whether `allocation` fits in `available` while leaving `floor` free.
pub fn memory_admit(available: Option<u64>, allocation: u64, floor: u64) -> bool {
available.is_some_and(|available| available >= floor && allocation <= available - floor)
}
const KEY: &[u8] = b"MemAvailable:";
fn fields(line: &[u8]) -> Vec<&[u8]> {
// The C++ stream extraction splits on the C-locale isspace set.
line.split(|b| matches!(b, b' ' | b'\t' | b'\n' | 0x0b | 0x0c | b'\r'))
.filter(|field| !field.is_empty())
.collect()
}
fn parse_kb(value: &[u8]) -> Option<u64> {
// `std::from_chars` for an unsigned type: digits only, no sign, no
// whitespace, whole token consumed, no overflow.
if value.is_empty() || !value.iter().all(u8::is_ascii_digit) {
return None;
}
std::str::from_utf8(value).ok()?.parse::<u64>().ok()
}
/// `MemAvailable` in bytes from `/proc/meminfo`-formatted text.
pub fn mem_available(mut meminfo: impl BufRead) -> Option<u64> {
let mut result: Option<u64> = None;
let mut line = Vec::new();
loop {
line.clear();
match meminfo.read_until(b'\n', &mut line) {
Ok(0) => break,
Ok(_) => {}
Err(_) => return None,
}
let parts = fields(&line);
if parts.first().copied() != Some(KEY) {
continue;
}
if result.is_some() || parts.len() != 3 || parts[2] != b"kB" {
return None;
}
let kb = parse_kb(parts[1])?;
if kb > u64::MAX / 1024 {
return None;
}
result = Some(kb * 1024);
}
result
}
@@ -7,52 +7,7 @@
use std::io::BufRead;
const KEY: &[u8] = b"MemAvailable:";
fn fields(line: &[u8]) -> Vec<&[u8]> {
// The C++ stream extraction splits on the C-locale isspace set.
line.split(|b| matches!(b, b' ' | b'\t' | b'\n' | 0x0b | 0x0c | b'\r'))
.filter(|field| !field.is_empty())
.collect()
}
fn parse_kb(value: &[u8]) -> Option<u64> {
// `std::from_chars` for an unsigned type: digits only, no sign, no
// whitespace, whole token consumed, no overflow.
if value.is_empty() || !value.iter().all(u8::is_ascii_digit) {
return None;
}
std::str::from_utf8(value).ok()?.parse::<u64>().ok()
}
/// `MemAvailable` in bytes from `/proc/meminfo`-formatted text.
pub fn mem_available(mut meminfo: impl BufRead) -> Option<u64> {
let mut result: Option<u64> = None;
let mut line = Vec::new();
loop {
line.clear();
match meminfo.read_until(b'\n', &mut line) {
Ok(0) => break,
Ok(_) => {}
Err(_) => return None,
}
let parts = fields(&line);
if parts.first().copied() != Some(KEY) {
continue;
}
if result.is_some() || parts.len() != 3 || parts[2] != b"kB" {
return None;
}
let kb = parse_kb(parts[1])?;
if kb > u64::MAX / 1024 {
return None;
}
result = Some(kb * 1024);
}
result
}
pub use leleka_engine_core::memory::memory_admit;
pub use leleka_engine_core::memory::{mem_available, memory_admit};
/// Available host physical memory right now; `None` when unknown (any
/// non-Linux target, unreadable or malformed `/proc/meminfo`).
+5 -21
View File
@@ -1,30 +1,14 @@
//! Conversation snapshot, parked-conversation cache and prompt-resume policy:
//! the Rust port of Strata's `ConversationCache` family.
//!
//! The pure policy modules depend on `std` only, so they build and test on any
//! host; the CUDA-owner bindings live next to the session owners.
//! The pure policy modules live in `leleka-engine-core::conversation` and are re-exported here under their old paths;
//! the CUDA-owner bindings live next to the session owners.
pub mod binding;
pub mod buffer;
pub mod cache;
pub mod checked;
pub use leleka_engine_core::conversation::{binding, buffer, cache, checked, kv, manager, model, retention, snapshot, state};
#[cfg(test)]
mod golden_cache;
#[cfg(test)]
mod golden_manager;
pub mod kv;
pub mod manager;
#[cfg(test)]
mod manager_tests;
pub(crate) use leleka_engine_core::conversation::testing;
pub mod memory;
pub mod model;
#[cfg(any(test, all(any(target_os = "linux", target_os = "windows"), target_arch = "x86_64")))]
pub mod original;
pub mod resident;
pub mod retention;
pub mod snapshot;
#[cfg(test)]
mod snapshot_tests;
pub mod state;
#[cfg(test)]
pub(crate) mod testing;
@@ -218,6 +218,10 @@ impl<T: OriginalSessionBytes + OriginalSessionControl> SessionDevice for Origina
self.session.restore_draft(resume).map_err(text)
}
fn available_memory(&mut self) -> Option<u64> {
super::memory::available_memory()
}
fn set_steering(&mut self, enabled: bool) {
self.session.set_steering(enabled);
}