Files
leleka/crates/gateway-domain/src/reasoning_budget.rs
T
Pavlo Ratushnyi 651ed44e90 Drop 49 allow(unused_imports) that no compiler warning needs (STRA-338)
The re-exports left by the mechanical module splits carried a blanket allow(unused_imports). 49 of them cover nothing: cargo check --all-targets on backend-api, control-plane, gateway-domain, gateway-text and libc-shim gives no new unused-import or glob-import warning without them. 39 others in those crates still silence a real warning and stay; the 8 in desktop-core (engine-host) could not be checked because that crate needs the engine, which does not build on Windows (STRA-390), and stay too.
2026-11-24 08:51:57 +01:00

85 lines
2.7 KiB
Rust

//! `reasoning_budget_tokens` (#123): the most tokens a request may think before the thinking is closed for it.
//!
//! Port of `Service.reasoning_budget` and of the budget part of `Service.run` in `Strata 41cf05a0, serve/server.py`.
//! The driver is generic over the engine and the tokenizer so the exact control flow (the clean-point rule, the
//! wrap-up, the continuation prompt, the room check) is testable without a model; `tests/reasoning_budget_oracle.rs`
//! replays cases produced by the unmodified Python `Service.run`.
use crate::think_marker;
use std::collections::HashSet;
use serde_json::Value;
/// What closes the thinking when it reaches the budget (the model's own end-of-thinking tag follows the sentence).
pub const REASONING_WRAP_UP: &str =
"\n\nI have thought about this long enough; time to give my answer.\n</think>\n\n";
/// An incremental UTF-8 decoder with replacement (`codecs.getincrementaldecoder("utf-8")(errors="replace")`): complete
/// characters come out, a character split across tokens is held until it is whole.
#[derive(Debug, Default)]
pub struct Utf8Stream {
held: Vec<u8>,
}
/// The part of `OutputParser` the budget depends on: whether the output is still inside the thinking, and the text it
/// holds back (a possible start of `</think>`).
#[derive(Debug)]
struct ReasoningScan {
reasoning: bool,
held: String,
}
/// The engine as the loop sees it: tokens for a prompt, at most `max_new` of them. Dropping the iterator is the
/// `gen.close()` (stop and drain).
pub trait TokenEngine {
fn generate<'a>(
&'a mut self,
prompt: &[u32],
max_new: usize,
) -> impl Iterator<Item = Result<u32, String>> + 'a;
}
/// The tokenizer as the loop sees it.
pub trait TokenCodec {
fn token_bytes(&self, id: u32) -> Vec<u8>;
/// `encode(REASONING_WRAP_UP, parse_special=True)`.
fn wrap_up_ids(&self) -> Vec<u32>;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Finish {
Length,
Stop,
Cancel,
}
pub struct RunRequest<'a> {
pub prompt: &'a [u32],
pub thinking: bool,
/// From [`reasoning_budget`]; ignored unless `thinking`.
pub budget: Option<u64>,
pub max_new: usize,
pub stop_ids: &'a HashSet<u32>,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Run {
pub finish: Finish,
/// Every counted token: the model's, the stop token, and the wrap-up.
pub completion_tokens: usize,
/// Every generated id, stop token and wrap-up included (`raw_ids`).
pub generated: Vec<u32>,
/// The thinking tokens when the budget closed it.
pub wrapped_after: Option<u64>,
/// What the output parser was fed.
pub text: String,
}
#[cfg(test)]
mod tests;
mod budget;
pub use budget::*;
mod generation_loop;
pub use generation_loop::*;