strata-ctl: Rust control layer for the Strata engine (OpenAI-compatible API)

From-scratch rewrite of the Python serve (Python kept as behavioral spec).

- engine: line-protocol supervisor (GEN/T/DONE/STOP/QUIT), silence watchdog,
  abort, is_idle probe.
- config: tolerant shared-JSON loader; foreign keys ignored so one file drives
  strata-ctl and uostream; CLI --config/--host/--port/--serve.
- inkling: o200k tokenizer wrapper + incremental UTF-8-safe detok; chat template
  via minijinja; channel-token dialect (reasoning streamed live); image bridge
  (in-process Vision, image=<path>); formatter assembling the API seam.
- api: /v1/chat/completions (stream SSE + non-stream), /v1/models, /health;
  reasoning_content; structured tool_calls (llama.cpp --jinja style, client-side
  execution); stop sequences with hold-back; abort on client disconnect.

69 tests green + 2 real-file ignored; clippy clean. Text, images and tools
validated live against the real Inkling engine.
This commit is contained in:
adm committed 2026-11-24 05:28:56 +01:00
commit 597ac432e1
30 files changed
+7541

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
//! 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();
}