//! A scripted in-process engine implementing [`Backend`], for tests that must exercise everything //! above the engine without a GPU, a model or a gRPC server. //! //! It is compiled only with the `scripted` feature; production crates never enable it. It speaks //! the same contract as the real backend: one generation at a time, one terminal event per //! generation, `cancel` ends the running generation with `Terminal(Cancelled)`, a generation whose //! receiver is dropped stops, and a stale or unloaded handle is refused. What it returns is //! whatever the test scripted: it computes nothing and makes no statement about any model. //! //! Faults are first-class steps ([`Step::DropWithoutTerminal`], [`Step::Fail`], ...) and engine //! restarts are simulated with [`ScriptedHandle::restart_engine`] so the callers' terminal-event //! guarantees can be checked. use std::{ collections::VecDeque, sync::{ Arc, Mutex, atomic::{AtomicBool, AtomicU64, Ordering}, }, thread, time::{Duration, Instant}, }; use crate::{ Backend, BackendCapabilities, BackendError, BackendIdentity, ErrorCode, FinishReason, GenerationEvent, GenerationRequest, GenerationSink, GenerationStream, GenerationTerminal, LoadedModel, ModelDescriptor, RuntimeConfig, SupportedModelTuple, Usage, }; /// One scripted action of a generation, played in order on the engine's own thread. #[derive(Clone, Debug)] pub enum Step { /// A text delta (valid UTF-8, as the real backend guarantees). Text(String), /// A usage report that is not the terminal. Usage(Usage), /// A native tool-call delta (the real backend advertises none; for protocol-violation tests). ToolCall { index: u32, id: Option, name: Option, arguments_delta: String, }, /// Sleep, ending early when the generation is cancelled. Pause(Duration), /// Sleep and ignore cancellation, a dropped stream and an engine restart: a wedged engine. Unresponsive(Duration), /// Block until cancelled, then end with `Terminal(Cancelled)`. AwaitCancel, /// `Terminal(Completed)`. Finish { reason: FinishReason, usage: Usage }, /// `Terminal(Failed)`. Fail { code: ErrorCode, message: String, retryable: bool, }, /// `Terminal(Cancelled)` that nobody asked for. CancelledUnasked, /// The engine dies: the stream ends without a terminal event. DropWithoutTerminal, } /// The scripted events of one generation. #[derive(Clone, Debug, Default)] pub struct Script { steps: Vec, } type Responder = Box Script + Send>; struct Shared { descriptor: ModelDescriptor, ready: AtomicBool, /// Bumped by every load and every simulated restart; a handle from an older epoch is stale. epoch: AtomicU64, /// Set by `restart_engine`: the running generation ends without a terminal event. kill: AtomicBool, /// The engine process is down: `load` is refused until `reload_engine`. down: AtomicBool, queue: Mutex>, responder: Mutex>, requests: Mutex>, cancels: Mutex>, loads: Mutex>, active: Mutex>, /// What `prefix_cache_summary` reports (a pool summary as JSON). prefix_summary: Mutex>, /// What `memory_report` reports (a memory-governor report as JSON). memory_report: Mutex>, /// The settings documents `reconfigure` has accepted, in order. settings: Mutex>, } struct ActiveGeneration { id: String, cancel: Arc, } /// The test's view of a [`ScriptedEngine`] that was moved into a worker or a server. #[derive(Clone)] pub struct ScriptedHandle { shared: Arc, } /// The engine. Implements [`Backend`]; keep a [`ScriptedHandle`] to script and observe it. pub struct ScriptedEngine { shared: Arc, } #[cfg(test)] mod tests; mod impl_script; #[allow(unused_imports)] pub use impl_script::*; mod impl_backend_for_scriptedengine; #[allow(unused_imports)] pub use impl_backend_for_scriptedengine::*; mod play; #[allow(unused_imports)] pub use play::*; mod shared_ops;