Files
leleka/control-plane/src/backend_worker/conclude.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

137 lines
5.4 KiB
Rust

//! Moved out of the parent module by STRA-338: fn conclude.
#[allow(unused_imports)]
use super::*;
/// The terminal event of the engine, turned into the single terminal event the HTTP layer needs.
pub(super) async fn conclude(
command: &mut GenerationCommand,
status: &RwLock<WorkerStatus>,
mut buffered: Vec<Event>,
terminal: api::GenerationTerminal,
mut state: Conclusion<'_>,
) -> Served {
let ended = Instant::now();
let prompt_ms = state
.first_delta
.unwrap_or(ended)
.duration_since(state.started)
.as_secs_f64()
* 1000.0;
let decode_ms = state
.first_delta
.map_or(0.0, |at| ended.duration_since(at).as_secs_f64() * 1000.0);
let make_done = |reported: Option<&api::Usage>, finish: Finish, delivered: u64| {
let fallback = state.usage.as_ref();
let generated = reported
.or(fallback)
.map_or(delivered, |usage| usage.output_tokens);
Done {
generated,
prompt_tokens: reported
.or(fallback)
.map_or(state.counted_input, |usage| usage.input_tokens),
prompt_ms,
decode_ms,
finish,
drafts_accepted: None,
drafts_offered: None,
reused: reported
.or(fallback)
.map(|usage| usage.reused_input_tokens.min(usage.input_tokens)),
expert_hits: None,
expert_lookups: None,
}
};
let disconnected = matches!(state.cancel, Some(Cancel::Client)) || command.result.is_closed();
match terminal {
api::GenerationTerminal::Completed { reason, usage } => {
match state.cancel.take() {
Some(Cancel::Failure(message)) => {
status.write().await.metrics.fail();
fail_command(command, message).await;
return Served::Done;
}
Some(Cancel::Client) => {
let done = make_done(Some(&usage), Finish::Cancel, state.delivered);
let _ = finish(command, buffered, done, true, status).await;
return Served::Done;
}
Some(Cancel::Marker) => {
let done = make_done(Some(&usage), Finish::Stop, state.delivered);
let _ = finish(command, buffered, done, disconnected, status).await;
return Served::Done;
}
None => {}
}
// The text ended without a marker: whatever was held back was ordinary text.
let tail = state.stops.finish();
if !tail.is_empty() {
match text_tokens(state.tokenizer, &tail) {
Ok(ids) => {
for id in ids {
state.delivered += 1;
deliver(command, &mut buffered, Event::Token(id)).await;
}
}
Err(message) => {
status.write().await.metrics.fail();
fail_command(command, message).await;
return Served::Done;
}
}
}
if usage.input_tokens != state.counted_input {
crate::elog!(
"the engine counted {} input tokens, the control plane {}",
usage.input_tokens,
state.counted_input
);
}
let finish_reason = match reason {
api::FinishReason::Length => Finish::Length,
// Tool calls are parsed from the text by this server, never taken from the engine.
api::FinishReason::Stop | api::FinishReason::ToolCalls => Finish::Stop,
};
let done = make_done(Some(&usage), finish_reason, state.delivered);
let _ = finish(command, buffered, done, disconnected, status).await;
Served::Done
}
api::GenerationTerminal::Cancelled => {
match state.cancel.take() {
Some(Cancel::Marker) => {
let done = make_done(None, Finish::Stop, state.delivered);
let _ = finish(command, buffered, done, disconnected, status).await;
}
Some(Cancel::Client) => {
let done = make_done(None, Finish::Cancel, state.delivered);
let _ = finish(command, buffered, done, true, status).await;
}
Some(Cancel::Failure(message)) => {
status.write().await.metrics.fail();
fail_command(command, message).await;
}
None => {
status.write().await.metrics.fail();
fail_command(
command,
"the engine cancelled the generation without a request".to_owned(),
)
.await;
}
}
Served::Done
}
api::GenerationTerminal::Failed { message, code, .. } => {
status.write().await.metrics.fail();
let message = if message.is_empty() {
format!("engine generation failed ({code:?})")
} else {
message
};
fail_command(command, message).await;
Served::Done
}
}
}