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.
247 lines
7.2 KiB
Rust
247 lines
7.2 KiB
Rust
//! Tests for the Inkling output dialect, driving the public token stream the
|
|
//! way `server.py` does (one `feed_token` per engine token) and asserting the
|
|
//! reasoning/content framing the launcher-patched reference produces.
|
|
|
|
use strata_ctl::inkling::dialect::{tag, InklingDialect, Kind, Out};
|
|
|
|
/// Feed a scripted sequence of tokens; `special` marks channel tokens.
|
|
fn run(tokens: &[(&str, bool)], finish: Option<&str>) -> Vec<Out> {
|
|
let mut d = InklingDialect::new();
|
|
let mut out = Vec::new();
|
|
for (piece, is_special) in tokens {
|
|
out.extend(d.feed_token(piece, *is_special));
|
|
}
|
|
out.extend(d.finish(finish));
|
|
out
|
|
}
|
|
|
|
fn content_of(outs: &[Out]) -> String {
|
|
outs.iter()
|
|
.filter(|o| o.kind == Kind::Content)
|
|
.map(|o| o.text.as_str())
|
|
.collect()
|
|
}
|
|
fn reasoning_of(outs: &[Out]) -> String {
|
|
outs.iter()
|
|
.filter(|o| o.kind == Kind::Reasoning)
|
|
.map(|o| o.text.as_str())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn plain_text_answer() {
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
(tag::CONTENT_TEXT, true),
|
|
("Hello world", false),
|
|
(tag::END_MESSAGE, true),
|
|
],
|
|
None,
|
|
);
|
|
assert_eq!(content_of(&outs), "Hello world");
|
|
assert_eq!(reasoning_of(&outs), "");
|
|
}
|
|
|
|
#[test]
|
|
fn thinking_is_streamed_live_then_answers() {
|
|
let mut d = InklingDialect::new();
|
|
let mut reasoning = Vec::new();
|
|
let mut content = Vec::new();
|
|
let feed = |d: &mut InklingDialect,
|
|
p: &str,
|
|
s: bool,
|
|
reasoning: &mut Vec<String>,
|
|
content: &mut Vec<String>| {
|
|
for o in d.feed_token(p, s) {
|
|
match o.kind {
|
|
Kind::Reasoning => reasoning.push(o.text),
|
|
Kind::Content => content.push(o.text),
|
|
}
|
|
}
|
|
};
|
|
feed(
|
|
&mut d,
|
|
tag::MESSAGE_MODEL,
|
|
true,
|
|
&mut reasoning,
|
|
&mut content,
|
|
);
|
|
feed(
|
|
&mut d,
|
|
tag::CONTENT_THINKING,
|
|
true,
|
|
&mut reasoning,
|
|
&mut content,
|
|
);
|
|
feed(&mut d, "Let me ", false, &mut reasoning, &mut content);
|
|
feed(&mut d, "think. ", false, &mut reasoning, &mut content);
|
|
feed(&mut d, tag::END_MESSAGE, true, &mut reasoning, &mut content);
|
|
feed(
|
|
&mut d,
|
|
tag::MESSAGE_MODEL,
|
|
true,
|
|
&mut reasoning,
|
|
&mut content,
|
|
);
|
|
feed(
|
|
&mut d,
|
|
tag::CONTENT_TEXT,
|
|
true,
|
|
&mut reasoning,
|
|
&mut content,
|
|
);
|
|
feed(&mut d, "The answer.", false, &mut reasoning, &mut content);
|
|
feed(&mut d, tag::END_MESSAGE, true, &mut reasoning, &mut content);
|
|
d.finish(None);
|
|
|
|
// reasoning arrived as discrete live deltas, not one blob at the end
|
|
assert_eq!(reasoning, vec!["Let me ", "think. "]);
|
|
assert_eq!(content.join(""), "The answer.");
|
|
}
|
|
|
|
#[test]
|
|
fn quoted_end_message_stays_in_thinking() {
|
|
// #6675: the model literally writes `<|end_message|>` inside its thinking;
|
|
// it must not leak to content nor end thinking early.
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
(tag::CONTENT_THINKING, true),
|
|
("before ", false),
|
|
(tag::END_MESSAGE, true), // quoted, not a real channel end
|
|
("after", false), // ordinary text proves it was quoted
|
|
(tag::ENDOFTEXT, true),
|
|
],
|
|
None,
|
|
);
|
|
assert_eq!(content_of(&outs), "");
|
|
let r = reasoning_of(&outs);
|
|
assert!(r.contains("before "), "{r}");
|
|
assert!(
|
|
r.contains(tag::END_MESSAGE),
|
|
"quoted tag kept in reasoning: {r}"
|
|
);
|
|
assert!(r.contains("after"), "{r}");
|
|
}
|
|
|
|
#[test]
|
|
fn header_overflow_becomes_plain_text() {
|
|
// #6620: no `<|content_text|>`; a newline in the "header" means it is text.
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
("Just prose\ncontinued", false),
|
|
(tag::END_MESSAGE, true),
|
|
],
|
|
None,
|
|
);
|
|
assert_eq!(content_of(&outs), "Just prose\ncontinued");
|
|
}
|
|
|
|
#[test]
|
|
fn tool_json_renders_a_frame() {
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
("get_weather", false),
|
|
(tag::CONTENT_INVOKE_TOOL_JSON, true),
|
|
(r#"{"name":"get_weather","args":{"city":"Rome"}}"#, false),
|
|
(tag::END_MESSAGE, true),
|
|
],
|
|
None,
|
|
);
|
|
let c = content_of(&outs);
|
|
assert!(c.contains(tag::TOOL_CALL_OPEN), "{c}");
|
|
assert!(c.contains(tag::TOOL_CALL_CLOSE), "{c}");
|
|
// the JSON inside round-trips name + arguments
|
|
let json_start = c.find('{').unwrap();
|
|
let json_end = c.rfind('}').unwrap();
|
|
let v: serde_json::Value = serde_json::from_str(&c[json_start..=json_end]).unwrap();
|
|
assert_eq!(v["name"], "get_weather");
|
|
assert_eq!(v["arguments"]["city"], "Rome");
|
|
}
|
|
|
|
#[test]
|
|
fn tool_json_args_as_json_string_is_parsed() {
|
|
// arguments shipped as a JSON *string* must be parsed into an object
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
(tag::CONTENT_INVOKE_TOOL_JSON, true),
|
|
(r#"{"name":"f","args":"{\"a\":1}"}"#, false),
|
|
(tag::END_MESSAGE, true),
|
|
],
|
|
None,
|
|
);
|
|
let c = content_of(&outs);
|
|
let v: serde_json::Value =
|
|
serde_json::from_str(&c[c.find('{').unwrap()..=c.rfind('}').unwrap()]).unwrap();
|
|
assert_eq!(v["arguments"]["a"], 1);
|
|
}
|
|
|
|
#[test]
|
|
fn truncated_tool_falls_back_to_header_name() {
|
|
let mut d = InklingDialect::new();
|
|
let mut content = String::new();
|
|
for (p, s) in [
|
|
(tag::MESSAGE_MODEL, true),
|
|
("lookup", false),
|
|
(tag::CONTENT_INVOKE_TOOL_JSON, true),
|
|
(
|
|
"{\"a\":1", // unterminated
|
|
false,
|
|
),
|
|
] {
|
|
for o in d.feed_token(p, s) {
|
|
if o.kind == Kind::Content {
|
|
content.push_str(&o.text);
|
|
}
|
|
}
|
|
}
|
|
for o in d.finish(Some("length")) {
|
|
if o.kind == Kind::Content {
|
|
content.push_str(&o.text);
|
|
}
|
|
}
|
|
assert!(content.contains(tag::TOOL_CALL_OPEN), "{content}");
|
|
let v: serde_json::Value =
|
|
serde_json::from_str(&content[content.find('{').unwrap()..=content.rfind('}').unwrap()])
|
|
.unwrap();
|
|
assert_eq!(v["name"], "lookup");
|
|
assert_eq!(d.calls, 1);
|
|
}
|
|
|
|
#[test]
|
|
fn thinking_only_at_length_surfaces_reasoning() {
|
|
// #6682: budget expires mid-thinking with no answer.
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
(tag::CONTENT_THINKING, true),
|
|
("pondering", false),
|
|
],
|
|
Some("length"),
|
|
);
|
|
assert!(content_of(&outs).is_empty());
|
|
// the live delta plus the finish-time accumulation both carry it (faithful
|
|
// to the reference: patched emit + _finish_typed #6682)
|
|
assert!(reasoning_of(&outs).contains("pondering"));
|
|
}
|
|
|
|
#[test]
|
|
fn end_sampling_returns_to_idle() {
|
|
// after END_SAMPLING a new turn can open; ensure a following MessageModel
|
|
// does not double-emit the prior content.
|
|
let outs = run(
|
|
&[
|
|
(tag::MESSAGE_MODEL, true),
|
|
(tag::CONTENT_TEXT, true),
|
|
("first", false),
|
|
(tag::END_SAMPLING, true),
|
|
],
|
|
None,
|
|
);
|
|
assert_eq!(content_of(&outs), "first");
|
|
}
|