Window: MAIN_AREA 30 + BUFFER 20 (same 50 cap) halves rotation frequency. context: shared compose_prefix (byte-exact prefix of compose) + notepad_fingerprint (rendered header body, excludes updated_at). inkling: build_prefix_ids renders without finish_prompt. service: prewarm() sends GEN 0 directly (no PerfMeter/DB). sessions: schedule_prewarm at turn end on pending rotation OR notepad-dirty (ws=0 normalized); session_chat loads only the window subset. examples/prewarm_bench.rs: joint GEN 0 -> RESUME bench, PASS on HIP (cached==L==385). Verified live all-agent: notepad write -> pre-warm 1468 -> next turn cached_tokens==1468. Tests: prefix property, opener-skip, GEN 0 primitive, notepad fingerprint. Suite 162 passed + 3 ignored.
175 lines
5.5 KiB
Rust
175 lines
5.5 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();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gen_zero_prefills_without_generating() {
|
|
// The cache pre-warm primitive: GEN 0 prefills the ids and emits one DONE
|
|
// with zero generated tokens (no T lines), prompt_tokens = the prefix length.
|
|
let engine = start_mock().await;
|
|
let out = engine
|
|
.generate(0, &Sampling::default(), &[1, 2, 3, 4], |_| {})
|
|
.await
|
|
.unwrap();
|
|
assert!(out.tokens.is_empty(), "GEN 0 generates no tokens");
|
|
let done = out.done.unwrap();
|
|
assert_eq!(done.generated, 0);
|
|
assert_eq!(done.prompt_tokens, 4);
|
|
assert_eq!(done.finish, "length");
|
|
// the wires are free again: a real turn queues/runs normally after the warm
|
|
assert!(engine.is_idle());
|
|
engine.shutdown().await.unwrap();
|
|
}
|