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.
This commit is contained in:
adm committed 2026-11-24 18:42:56 +01:00
1 parent 597ac432e1
commit 5bafd000c8
13 files changed
+651 -51

No files matched your search

+34
View File
@@ -119,3 +119,37 @@ async fn abort_reaches_a_running_request_and_frees_the_wires() {
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();
}
+48
View File
@@ -13,6 +13,7 @@ use tokenizers::{AddedToken, Tokenizer};
use strata_ctl::api::formatter::{Formatter, Piece};
use strata_ctl::api::types::ChatRequest;
use strata_ctl::engine::Sampling;
use strata_ctl::inkling::{ChatTemplate, InklingFormatter, InklingTokenizer};
static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
@@ -66,6 +67,7 @@ fn formatter() -> (InklingFormatter, Arc<InklingTokenizer>, std::path::PathBuf)
template,
"test-model".into(),
Some("low".into()),
Sampling::default(),
);
(fmt, tok, path)
}
@@ -220,3 +222,49 @@ fn tool_choice_none_and_auto_accepted() {
}
let _ = std::fs::remove_file(&path);
}
#[test]
fn config_sampling_defaults_are_applied_and_overridable() {
let (tok, path) = fixture_tokenizer();
let template = ChatTemplate::new(TEMPLATE).unwrap();
let defaults = Sampling {
temperature: Some(0.7),
top_p: Some(0.9),
min_p: Some(0.05),
repetition_penalty: Some(1.1),
..Default::default()
};
let fmt = InklingFormatter::new(
tok.clone(),
template,
"test-model".into(),
Some("low".into()),
defaults,
);
// request omits every knob -> config defaults apply (not greedy)
let req: ChatRequest = serde_json::from_value(
json!({"model": "test-model", "messages": [{"role": "user", "content": "hello"}]}),
)
.unwrap();
let s = fmt.build_prompt(&req).unwrap().sampling;
assert_eq!(s.temperature, Some(0.7));
assert_eq!(s.top_p, Some(0.9));
assert_eq!(s.min_p, Some(0.05));
assert_eq!(s.repetition_penalty, Some(1.1));
// a per-request knob wins; the others still fall back to the defaults
let req2: ChatRequest = serde_json::from_value(json!({
"model": "test-model",
"messages": [{"role": "user", "content": "hello"}],
"temperature": 0.2,
"top_k": 0
}))
.unwrap();
let s2 = fmt.build_prompt(&req2).unwrap().sampling;
assert_eq!(s2.temperature, Some(0.2));
assert_eq!(s2.top_p, Some(0.9));
assert_eq!(s2.top_k, Some(0)); // carried through; keys() maps 0 -> 64
assert!(s2.keys().contains("top_k=64"));
let _ = std::fs::remove_file(&path);
}