Files
strata-ctl/tests/engine_supervisor.rs
T
adm 5bafd000c8 Respond to code review: sampling defaults, abort race, hardening + README
Bug fixes (from review/README.md):
- Apply config sampling defaults field-by-field, so an absent temperature no
  longer means greedy; expose top_k/min_p/repetition_penalty on ChatRequest;
  top_k=0 is "disabled" (-> 64), not greedy.
- Fix cross-request abort race: Engine::request_stop() sets the cancel flag only;
  the owning generate loop does STOP+drain itself, instead of a spawned abort()
  that could hit a later request holding the lock.
- Initialize tracing-subscriber (RUST_LOG, default info); events were dropped.
- TempFilesGuard removes materialized images on every exit path (error/panic/drop);
  build_prompt runs in spawn_blocking off the async worker; ureq image fetch gets a
  15s timeout, a 32MB cap and a basic SSRF guard (literal loopback/private hosts).
- Non-blocking shutdown (STOP+QUIT without taking the request lock first) and a
  bounded drain_to_done that marks the engine dead on timeout.

Operational hardening:
- Graceful shutdown on SIGTERM as well as SIGINT.
- Warn when binding a non-loopback address (no auth, allowed_hosts not enforced).
- Configurable startup/silence timeouts (optional config keys, default 600s).

Docs:
- Add README.md (English): what strata-ctl is, why Rust for stable server use,
  architecture, usage, honest status, and credits to Strata / Niko1221.

76 tests green + 2 real-file ignored; clippy clean; cargo fmt applied.
Deferred (heavy): automatic engine restart after a crash.
2026-11-24 18:42:56 +01:00

156 lines
4.8 KiB
Rust

//! End-to-end tests of the supervisor against a scripted mock engine
//! (tests/mock_engine.py), which speaks the protocol exactly as `strata
//! --serve` prints it.
use std::time::Duration;
use strata_ctl::engine::{Engine, EngineError, EngineLine, Sampling};
fn mock() -> (String, Vec<String>) {
let script = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/mock_engine.py");
(
"python3".into(),
vec![script.into(), "--marker".to_string()],
)
}
async fn start_mock() -> Engine {
let (exe, args) = mock();
Engine::start(&exe, &args[0..1], None, Duration::from_secs(10))
.await
.expect("mock engine starts")
}
#[tokio::test]
async fn start_reads_ready() {
let engine = start_mock().await;
let ready = engine.ready();
assert_eq!(ready.max_context, 8192);
assert!(ready.can_stop);
assert!(ready.can_hold);
assert!(!ready.can_hold_batch);
assert_eq!(
engine.info().await.get("version").map(String::as_str),
Some("0.0.0-mock")
);
engine.shutdown().await.unwrap();
}
#[tokio::test]
async fn generate_streams_and_finishes() {
let engine = start_mock().await;
let mut seen = Vec::new();
let out = engine
.generate(16, &Sampling::default(), &[1, 2, 3, 4], |line| {
if let EngineLine::Prefill { read, total, .. } = line {
seen.push((*read, *total));
}
})
.await
.unwrap();
assert_eq!(out.tokens, vec![101, 102, 103, 104, 105]);
assert_eq!(out.resumed, 1);
assert_eq!(seen, vec![(2, 4)]);
let done = out.done.unwrap();
assert_eq!((done.generated, done.prompt_tokens), (5, 4));
assert_eq!(done.finish, "stop");
assert_eq!(done.extra, ["3", "4", "1"]);
engine.shutdown().await.unwrap();
}
#[tokio::test]
async fn refusal_is_reported_and_trailing_done_swallowed() {
let engine = start_mock().await;
let err = engine
.generate(8, &Sampling::default(), &[1, 999999], |_| {})
.await
.unwrap_err();
assert!(matches!(&err, EngineError::Refused(m) if m.contains("out of range")));
// the mock printed a DONE after the ERR: it must not leak to the next request
let out = engine
.generate(8, &Sampling::default(), &[1, 2], |_| {})
.await
.unwrap();
assert_eq!(out.tokens.len(), 5);
engine.shutdown().await.unwrap();
}
#[tokio::test]
async fn abort_reaches_a_running_request_and_frees_the_wires() {
let exe_mock = mock();
let engine = std::sync::Arc::new(
Engine::start(
&exe_mock.0,
&[exe_mock.1[0].clone()],
None,
Duration::from_secs(10),
)
.await
.unwrap(),
);
let holder = {
let engine = engine.clone();
tokio::spawn(async move {
engine
.generate(1000, &Sampling::default(), &[888888], |_| {})
.await
})
};
// a second request queues behind the first (FIFO, not a refusal)
let waiter = {
let engine = engine.clone();
tokio::spawn(async move {
engine
.generate(8, &Sampling::default(), &[1, 2], |_| {})
.await
})
};
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(engine.abort().await.unwrap(), "a request was running");
let held = holder.await.unwrap();
assert!(
matches!(&held, Err(EngineError::Refused(m)) if m == "aborted"),
"the held request should end aborted: {held:?}"
);
// the queued waiter ran on the same engine and finished normally
let waited = waiter.await.unwrap();
assert_eq!(waited.unwrap().tokens.len(), 5);
// nothing running now: abort is a no-op
assert!(!engine.abort().await.unwrap());
engine.shutdown().await.unwrap();
}
#[tokio::test]
async fn request_stop_signals_the_owner_without_taking_the_lock() {
let exe_mock = mock();
let engine = std::sync::Arc::new(
Engine::start(
&exe_mock.0,
&[exe_mock.1[0].clone()],
None,
Duration::from_secs(10),
)
.await
.unwrap(),
);
let holder = {
let engine = engine.clone();
tokio::spawn(async move {
engine
.generate(1000, &Sampling::default(), &[888888], |_| {})
.await
})
};
tokio::time::sleep(Duration::from_millis(200)).await;
// sync, takes no lock and sends no STOP from outside: the owning generate
// loop sees the flag and does STOP+drain itself (no race on a later request)
engine.request_stop();
let held = holder.await.unwrap();
assert!(
matches!(&held, Err(EngineError::Refused(m)) if m == "aborted"),
"the held request should end aborted: {held:?}"
);
assert!(engine.is_idle());
engine.shutdown().await.unwrap();
}