Files
strata-ctl/tests/inkling_dialect.rs
T

274 lines
8.0 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. Thinking is streamed
// live by emit(), so finish() must NOT re-emit it: the whole CoT appears
// exactly once (BUG-06 — duplication was amplified by the 16384 budget).
let outs = run(
&[
(tag::MESSAGE_MODEL, true),
(tag::CONTENT_THINKING, true),
("pondering", false),
],
Some("length"),
);
assert!(content_of(&outs).is_empty());
assert_eq!(
reasoning_of(&outs),
"pondering",
"thinking must appear exactly once, not duplicated at finish"
);
}
#[test]
fn quoted_end_message_in_think_end_streams_live() {
// #6675: two consecutive end-messages in thinking — the first is quoted text
// and must surface in reasoning live (mirrors TextEnd), not be lost.
let outs = run(
&[
(tag::MESSAGE_MODEL, true),
(tag::CONTENT_THINKING, true),
("x", false),
(tag::END_MESSAGE, true),
(tag::END_MESSAGE, true),
],
None,
);
let r = reasoning_of(&outs);
assert!(r.contains('x'), "{r}");
assert!(
r.contains(tag::END_MESSAGE),
"quoted tag must stream live: {r}"
);
assert!(content_of(&outs).is_empty());
}
#[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");
}