STRA-338 S23: close the golden gaps of the second review, recorded on the code before S23
29 more cases (159 cases, 318 captures), recorded on08afbf216in a temporary worktree and checked against the new code; none differs. New cases: the request's max_output_tokens reached (text, reasoning, closed reasoning, tools, structured, gpt-oss); Qwen reasoning on with json_schema valid and violated; gpt-oss json_schema, tools with structured output, effort none with tools; Qwen reasoning plus tools at the length limit with calls inside the unclosed reasoning, and without; namespaced tools through the stream and the buffered reading (Qwen with and without reasoning, gpt-oss); DONE before the first token and an error after DONE (all four readings); a worker that reports fewer generated tokens than the reasoning took (the reasoning_tokens clamp, usage output_tokens). New tests of the binary (15 pass on08afbf216and on the new code): the stored response equals the one the client was sent (streamed and not, six cases); a client that leaves at DONE closes the source (five cases, among them the final preview flush); what reaches the worker: gpt-oss gets none of the server defaults for the samplers it lacks (top_k 20 and penalty_last_n 64 included), accepts neutral values, Qwen keeps all five samplers and penalty_last_n, own and default. In the lib: BufferedRun finishing its generation twice returns the shared GENERATION_ENDED error and fail() sends response.failed; a token after the end fails the stream. regenerate_output_golden now refuses unless OUTPUT_GOLDEN_RECORD names the pre-S23 commit and the tree has no output module (checked: it panics on this tree); output_events.recorded_on names08afbf216and a test pins it.
This commit is contained in:
@@ -263,3 +263,6 @@ impl BufferedRun {
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
//! The buffered stream when its generation is read twice.
|
||||
|
||||
use super::*;
|
||||
use crate::responses::tests::{make_request, test_tokenizer};
|
||||
use std::fs;
|
||||
|
||||
/// A generation ends once: reading it again fails the response (`response.failed`) and does not panic.
|
||||
#[tokio::test]
|
||||
async fn finishing_the_generation_twice_fails_the_response() {
|
||||
let (codec, root) = test_tokenizer();
|
||||
let request = make_request(json!({"reasoning": {"effort": "low"}}));
|
||||
let (_worker, source) = mpsc::channel(1);
|
||||
let (mut run, mut client) = BufferedRun::build(request, codec, source, StreamEnv::none());
|
||||
assert!(run.finish_events(Finish::Length).await.is_ok());
|
||||
let again = run.finish_events(Finish::Length).await;
|
||||
assert_eq!(again, Err(GENERATION_ENDED.to_owned()));
|
||||
run.fail(again.err()).await;
|
||||
let event = client.recv().await.unwrap();
|
||||
assert_eq!(event["type"], "response.failed");
|
||||
assert_eq!(event["response"]["error"]["message"], GENERATION_ENDED);
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
|
||||
/// A token pushed after the generation ended is a failure of the stream, not a panic.
|
||||
#[tokio::test]
|
||||
async fn a_token_after_the_end_is_a_failure() {
|
||||
let (codec, root) = test_tokenizer();
|
||||
let request = make_request(json!({"reasoning": {"effort": "low"}}));
|
||||
let (_worker, source) = mpsc::channel(1);
|
||||
let (mut run, _client) = BufferedRun::build(request, codec, source, StreamEnv::none());
|
||||
let _ = run.finish_events(Finish::Length).await;
|
||||
assert_eq!(run.push_token(65).await, Err(GENERATION_ENDED.to_owned()));
|
||||
fs::remove_dir_all(root).unwrap();
|
||||
}
|
||||
@@ -18,4 +18,4 @@ mod text_preview;
|
||||
mod tool_policy;
|
||||
mod tool_replay;
|
||||
mod validation;
|
||||
pub(super) use fixtures::{answer_items, test_marks};
|
||||
pub(super) use fixtures::{answer_items, make_request, test_marks, test_tokenizer};
|
||||
|
||||
@@ -3187,5 +3187,750 @@
|
||||
"partial"
|
||||
],
|
||||
"end": "cancel"
|
||||
},
|
||||
{
|
||||
"name": "q-text-max-output-tokens",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"Hello wor"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-think-max-output-tokens",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"Still thinking"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-think-closed-max-output-tokens",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"r</think>\n\nPart of the ans"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-tools-max-output-tokens",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"Hi\n",
|
||||
"<tool_call>\n<function=lookup>\n<parameter=city>\nOslo\n</parameter>\n<parameter=days>\n2\n</parameter>\n</function>\n</tool_call>",
|
||||
"\nand mo"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-structured-max-output-tokens",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_object"
|
||||
}
|
||||
},
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"{\"a\": 1"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "g-max-output-tokens",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"Hmm, let me"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "g-final-max-output-tokens",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"max_output_tokens": 64
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"Part of"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-think-schema-valid",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"name": "r",
|
||||
"strict": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"hm</think>\n\n",
|
||||
"{\"a\":1}"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "q-think-schema-violation",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"name": "r",
|
||||
"strict": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"hm</think>\n\n",
|
||||
"{\"a\":\"x\"}"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "g-schema-valid",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"name": "r",
|
||||
"strict": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"hm",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"{\"a\":1}",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "g-schema-violation",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_schema",
|
||||
"name": "r",
|
||||
"strict": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"a": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"a"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"hm",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"{\"a\":\"x\"}",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "g-tools-and-structured",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
},
|
||||
"deep": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
],
|
||||
"text": {
|
||||
"format": {
|
||||
"type": "json_object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"{\"a\":1}",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "g-effort-none-tools",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
},
|
||||
"deep": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"hm",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"commentary to=functions.lookup ",
|
||||
"@<|message|>",
|
||||
"{\"city\":\"Oslo\"}",
|
||||
"@<|call|>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "q-think-tools-length-unclosed-calls",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"I will look it up.\n",
|
||||
"<tool_call>\n<function=lookup>\n<parameter=city>\nOslo\n</parameter>\n<parameter=days>\n2\n</parameter>\n</function>\n</tool_call>"
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-think-tools-length-unclosed-text",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"I will look it up."
|
||||
],
|
||||
"end": "length"
|
||||
},
|
||||
{
|
||||
"name": "q-namespaced-call",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": "files",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "read",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"Reading.\n",
|
||||
"<tool_call>\n<function=ns_2b733164e15b550536182f4a>\n<parameter=path>\nanswer.txt\n</parameter>\n</function>\n</tool_call>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "q-think-namespaced-call",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": "files",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "read",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"r</think>\n\n",
|
||||
"<tool_call>\n<function=ns_2b733164e15b550536182f4a>\n<parameter=path>\nanswer.txt\n</parameter>\n</function>\n</tool_call>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "g-namespaced-call",
|
||||
"family": "gptoss",
|
||||
"request": {
|
||||
"tools": [
|
||||
{
|
||||
"type": "namespace",
|
||||
"name": "files",
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "read",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"path"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"r",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"commentary to=functions.ns_2b733164e15b550536182f4a ",
|
||||
"@<|message|>",
|
||||
"{\"path\":\"answer.txt\"}",
|
||||
"@<|call|>"
|
||||
],
|
||||
"end": "stop"
|
||||
},
|
||||
{
|
||||
"name": "q-text-done-first",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"abc"
|
||||
],
|
||||
"end": "done-first"
|
||||
},
|
||||
{
|
||||
"name": "q-think-done-first",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"r</think>\n\nabc"
|
||||
],
|
||||
"end": "done-first"
|
||||
},
|
||||
{
|
||||
"name": "q-tools-done-first",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"Hi\n",
|
||||
"<tool_call>\n<function=lookup>\n<parameter=city>\nOslo\n</parameter>\n<parameter=days>\n2\n</parameter>\n</function>\n</tool_call>"
|
||||
],
|
||||
"end": "done-first"
|
||||
},
|
||||
{
|
||||
"name": "g-done-first",
|
||||
"family": "gptoss",
|
||||
"request": {},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"r",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"abc",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "done-first"
|
||||
},
|
||||
{
|
||||
"name": "q-text-error-after-done",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"abc"
|
||||
],
|
||||
"end": "error-after-done:late failure"
|
||||
},
|
||||
{
|
||||
"name": "q-think-error-after-done",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"r</think>\n\nabc"
|
||||
],
|
||||
"end": "error-after-done:late failure"
|
||||
},
|
||||
{
|
||||
"name": "q-tools-error-after-done",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
},
|
||||
"tools": [
|
||||
{
|
||||
"type": "function",
|
||||
"name": "lookup",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"city": {
|
||||
"type": "string"
|
||||
},
|
||||
"days": {
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"city"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"script": [
|
||||
"Hi\n",
|
||||
"<tool_call>\n<function=lookup>\n<parameter=city>\nOslo\n</parameter>\n<parameter=days>\n2\n</parameter>\n</function>\n</tool_call>"
|
||||
],
|
||||
"end": "error-after-done:late failure"
|
||||
},
|
||||
{
|
||||
"name": "g-error-after-done",
|
||||
"family": "gptoss",
|
||||
"request": {},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"r",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"abc",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "error-after-done:late failure"
|
||||
},
|
||||
{
|
||||
"name": "q-think-generated-clamp",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "low"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"Let me think.",
|
||||
"@</think>",
|
||||
"\n\nParis."
|
||||
],
|
||||
"end": "stop",
|
||||
"generated": 3
|
||||
},
|
||||
{
|
||||
"name": "g-analysis-final-generated-clamp",
|
||||
"family": "gptoss",
|
||||
"request": {},
|
||||
"script": [
|
||||
"@<|channel|>",
|
||||
"analysis",
|
||||
"@<|message|>",
|
||||
"Let me think.",
|
||||
"@<|end|>",
|
||||
"@<|channel|>",
|
||||
"final",
|
||||
"@<|message|>",
|
||||
"Paris.",
|
||||
"@<|return|>"
|
||||
],
|
||||
"end": "stop",
|
||||
"generated": 2
|
||||
},
|
||||
{
|
||||
"name": "q-text-generated-count",
|
||||
"family": "qwen",
|
||||
"request": {
|
||||
"reasoning": {
|
||||
"effort": "none"
|
||||
}
|
||||
},
|
||||
"script": [
|
||||
"Hello world"
|
||||
],
|
||||
"end": "stop",
|
||||
"generated": 100
|
||||
}
|
||||
]
|
||||
|
||||
+59
-1
File diff suppressed because one or more lines are too long
@@ -0,0 +1 @@
|
||||
08afbf216
|
||||
@@ -4,6 +4,7 @@ use std::sync::Arc;
|
||||
|
||||
use axum::{Json, extract::State, response::Response};
|
||||
use http_body_util::BodyExt;
|
||||
use leleka_sampler_ref::RequestSampling;
|
||||
use serde_json::{Value, json};
|
||||
use strata_control::{
|
||||
adapters::ChannelEngine,
|
||||
@@ -16,7 +17,7 @@ use strata_control::{
|
||||
use tokio::{sync::mpsc, task::JoinHandle};
|
||||
|
||||
use crate::{
|
||||
script::{Case, script_ids, worker_events},
|
||||
script::{Case, worker_events},
|
||||
vocab::Codecs,
|
||||
};
|
||||
|
||||
@@ -38,13 +39,15 @@ pub async fn ready_state(
|
||||
(state, received)
|
||||
}
|
||||
|
||||
/// A worker that takes one command and answers it with `events`, buffered or streamed.
|
||||
/// A worker that takes one command and answers it with `events`, buffered or streamed; it reports the sampling it was
|
||||
/// asked for.
|
||||
pub fn answer_once(
|
||||
mut received: mpsc::Receiver<GenerationCommand>,
|
||||
events: Vec<Result<Event, String>>,
|
||||
) -> JoinHandle<()> {
|
||||
) -> JoinHandle<RequestSampling> {
|
||||
tokio::spawn(async move {
|
||||
let command = received.recv().await.unwrap();
|
||||
let sampling = command.request.sampling.clone();
|
||||
match command.result {
|
||||
GenerationResult::Finished => panic!("unexpected finished command"),
|
||||
GenerationResult::Buffered(sender) => {
|
||||
@@ -58,6 +61,7 @@ pub fn answer_once(
|
||||
}
|
||||
}
|
||||
}
|
||||
sampling
|
||||
})
|
||||
}
|
||||
|
||||
@@ -87,8 +91,8 @@ pub fn normalize(text: &str) -> String {
|
||||
times.replace_all(&text, r#""${1}":0"#).into_owned()
|
||||
}
|
||||
|
||||
/// Status, content type and body of a response; a stream is read frame by frame.
|
||||
pub async fn capture(response: Response) -> String {
|
||||
/// Status, content type and body of a response, as sent; a stream is read frame by frame.
|
||||
pub async fn read_response(response: Response) -> String {
|
||||
let status = response.status().as_u16();
|
||||
let kind = response
|
||||
.headers()
|
||||
@@ -101,7 +105,12 @@ pub async fn capture(response: Response) -> String {
|
||||
&frame.unwrap().into_data().unwrap(),
|
||||
));
|
||||
}
|
||||
normalize(&text)
|
||||
text
|
||||
}
|
||||
|
||||
/// What a client reads, with the volatile parts replaced.
|
||||
pub async fn capture(response: Response) -> String {
|
||||
normalize(&read_response(response).await)
|
||||
}
|
||||
|
||||
/// The request body of a case in one mode.
|
||||
@@ -118,10 +127,7 @@ pub fn payload(case: &Case, stream: bool) -> Value {
|
||||
/// The capture of one case in one mode.
|
||||
pub async fn run_case(case: &Case, stream: bool, codecs: &Codecs) -> String {
|
||||
let (state, received) = ready_state(codecs, case.gptoss).await;
|
||||
let worker = answer_once(
|
||||
received,
|
||||
worker_events(&script_ids(case, codecs), &case.end),
|
||||
);
|
||||
let worker = answer_once(received, worker_events(case, codecs));
|
||||
set_tool_call_recovery(case.recovery);
|
||||
let response = create(State(state), Ok(Json(payload(case, stream)))).await;
|
||||
let text = capture(response).await;
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::time::Duration;
|
||||
|
||||
use axum::{Json, body::Body, extract::State};
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::json;
|
||||
use serde_json::{Value, json};
|
||||
use strata_control::{
|
||||
responses::create,
|
||||
wire::{Event, Finish},
|
||||
@@ -14,8 +14,8 @@ use tokio::{sync::mpsc, sync::oneshot, task::JoinHandle};
|
||||
|
||||
use crate::{
|
||||
ONE_AT_A_TIME,
|
||||
capture::ready_state,
|
||||
script::{Case, done_event, script_ids, token_events},
|
||||
capture::{read_response, ready_state},
|
||||
script::{Case, done_event, script_ids, token_events, worker_events},
|
||||
vocab::Codecs,
|
||||
};
|
||||
|
||||
@@ -170,3 +170,97 @@ async fn a_sampler_the_gptoss_worker_lacks_is_refused_and_named() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The `response` of the `response.completed` event of a stream, as the client read it (not normalised).
|
||||
fn completed_response(stream: &str) -> Value {
|
||||
let data = stream
|
||||
.lines()
|
||||
.filter_map(|line| line.strip_prefix("data: "));
|
||||
let events = data.map(|data| serde_json::from_str::<Value>(data).unwrap());
|
||||
let mut events = events;
|
||||
events
|
||||
.find(|event| event["type"] == "response.completed")
|
||||
.unwrap()["response"]
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// A stored response is the one the client was sent, streamed or not.
|
||||
async fn assert_stored_copy_equals_the_sent_one(name: &str, stream: bool) {
|
||||
let codecs = Codecs::new();
|
||||
let case = named(name);
|
||||
let (state, received) = ready_state(&codecs, case.gptoss).await;
|
||||
let worker = crate::capture::answer_once(received, worker_events(&case, &codecs));
|
||||
let mut body = crate::capture::payload(&case, stream);
|
||||
body["store"] = json!(true);
|
||||
let sent = read_response(create(State(state.clone()), Ok(Json(body))).await).await;
|
||||
worker.await.unwrap();
|
||||
let sent = if stream {
|
||||
completed_response(&sent)
|
||||
} else {
|
||||
serde_json::from_str(sent.split_once('\n').unwrap().1).unwrap()
|
||||
};
|
||||
let id = sent["id"].as_str().unwrap().to_owned();
|
||||
let stored = state.services.responses.response(&id).await;
|
||||
assert_eq!(stored, Some(sent), "{name} stream={stream}");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn the_stored_response_is_the_one_the_client_was_sent() {
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
for name in [
|
||||
"q-text",
|
||||
"q-tools-call",
|
||||
"q-think-answer",
|
||||
"q-structured-valid",
|
||||
"g-call",
|
||||
"g-analysis-final",
|
||||
] {
|
||||
for stream in [true, false] {
|
||||
assert_stored_copy_equals_the_sent_one(name, stream).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A client that leaves after the worker's `DONE` (while the response is read, or its last reasoning flushed) does not
|
||||
/// leave the stream running: the source is closed once the stream is over.
|
||||
async fn assert_source_closed_after_disconnect_at_done(name: &str) {
|
||||
let codecs = Codecs::new();
|
||||
let case = named(name);
|
||||
let (state, mut received) = ready_state(&codecs, case.gptoss).await;
|
||||
let events = worker_events(&case, &codecs);
|
||||
let watcher = tokio::spawn(async move {
|
||||
let command = received.recv().await.unwrap();
|
||||
let GenerationResult::Streaming(sender) = command.result else {
|
||||
panic!("a stream was asked for");
|
||||
};
|
||||
for event in events {
|
||||
let _ = sender.send(event).await;
|
||||
}
|
||||
tokio::time::timeout(Duration::from_secs(3), sender.closed())
|
||||
.await
|
||||
.is_ok()
|
||||
});
|
||||
let response = create(State(state), Ok(Json(crate::capture::payload(&case, true)))).await;
|
||||
let mut body = response.into_body();
|
||||
let _ = read_until(&mut body, "response.created").await;
|
||||
drop(body);
|
||||
assert!(
|
||||
watcher.await.unwrap(),
|
||||
"{name}: the source stayed open after the client left"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn a_client_that_leaves_at_done_closes_the_source() {
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
let names = [
|
||||
"q-think-unclosed-length",
|
||||
"q-think-answer",
|
||||
"q-structured-valid",
|
||||
"q-tools-call",
|
||||
"g-analysis-cut-length",
|
||||
];
|
||||
for name in names {
|
||||
assert_source_closed_after_disconnect_at_done(name).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
mod capture;
|
||||
mod live;
|
||||
mod sampling;
|
||||
mod script;
|
||||
mod vocab;
|
||||
|
||||
@@ -65,11 +66,31 @@ async fn gateway_answers_for_scripted_output_match_the_recorded_golden() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Rewrites the expected captures from the current code. Only for recording on the code before a change, never to
|
||||
/// make a changed code pass.
|
||||
/// The commit the expected captures were recorded on: the code before the output decoding moved behind `OutputEvent`.
|
||||
const RECORDED_ON: &str = "08afbf216";
|
||||
|
||||
/// Recording is refused on code that has the output module: a changed code must never pass by recording its own
|
||||
/// captures. Set `OUTPUT_GOLDEN_RECORD` to the commit of the pre-S23 tree the test runs in.
|
||||
fn assert_recording_is_allowed() {
|
||||
let commit = std::env::var("OUTPUT_GOLDEN_RECORD")
|
||||
.expect("set OUTPUT_GOLDEN_RECORD to the commit recorded on");
|
||||
assert!(
|
||||
commit.starts_with(RECORDED_ON),
|
||||
"the golden is recorded on {RECORDED_ON}, not {commit}"
|
||||
);
|
||||
let output =
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../crates/gateway-domain/src/output.rs");
|
||||
assert!(
|
||||
!output.exists(),
|
||||
"this tree has the output module: record on {RECORDED_ON}, not on it"
|
||||
);
|
||||
}
|
||||
|
||||
/// Rewrites the expected captures. Only for recording on the code before the change, never to make changed code pass.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
#[ignore = "regenerates tests/fixtures/golden/output_events.json"]
|
||||
#[ignore = "records tests/fixtures/golden/output_events.json on the code before S23 (OUTPUT_GOLDEN_RECORD)"]
|
||||
async fn regenerate_output_golden() {
|
||||
assert_recording_is_allowed();
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
let mut golden = serde_json::Map::new();
|
||||
for (name, text) in capture_all(&Codecs::new()).await {
|
||||
@@ -77,6 +98,17 @@ async fn regenerate_output_golden() {
|
||||
}
|
||||
let text = serde_json::to_string_pretty(&Value::Object(golden)).unwrap() + "\n";
|
||||
fs::write(fixture("output_events.json"), text).unwrap();
|
||||
fs::write(
|
||||
fixture("output_events.recorded_on"),
|
||||
format!("{RECORDED_ON}\n"),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_captures_name_the_commit_they_were_recorded_on() {
|
||||
let recorded = fs::read_to_string(fixture("output_events.recorded_on")).unwrap();
|
||||
assert_eq!(recorded.trim(), RECORDED_ON);
|
||||
}
|
||||
|
||||
/// The captures do not depend on the run: two runs agree.
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//! What the worker is asked for: the sampling of the request after the server defaults and the model's limit.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{Json, extract::State};
|
||||
use leleka_sampler_ref::RequestSampling;
|
||||
use leleka_serving_policy::shared::ConfigSampling;
|
||||
use serde_json::{Value, json};
|
||||
use strata_control::{responses::create, settings::Settings};
|
||||
|
||||
use crate::{
|
||||
ONE_AT_A_TIME,
|
||||
capture::{answer_once, capture, payload, ready_state},
|
||||
script::worker_events,
|
||||
vocab::Codecs,
|
||||
};
|
||||
|
||||
/// The server defaults of the test: every sampler set.
|
||||
fn busy_defaults() -> ConfigSampling {
|
||||
let sampling = RequestSampling {
|
||||
top_k: Some(20),
|
||||
min_p: Some(0.05),
|
||||
repetition_penalty: Some(1.1),
|
||||
frequency_penalty: Some(0.1),
|
||||
presence_penalty: Some(0.2),
|
||||
penalty_last_n: Some(64),
|
||||
..Default::default()
|
||||
};
|
||||
ConfigSampling {
|
||||
sampling,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// The answer to a request of a case, with `extra` in its body and `defaults` as the server's sampling defaults, and
|
||||
/// the sampling the worker was asked for.
|
||||
async fn ask(
|
||||
name: &str,
|
||||
extra: Value,
|
||||
defaults: Option<ConfigSampling>,
|
||||
) -> (String, RequestSampling) {
|
||||
let codecs = Codecs::new();
|
||||
let case = crate::cases()
|
||||
.into_iter()
|
||||
.find(|case| case.name == name)
|
||||
.unwrap();
|
||||
let (mut state, received) = ready_state(&codecs, case.gptoss).await;
|
||||
if let Some(defaults) = defaults {
|
||||
state.services.settings = Arc::new(Settings::new(defaults));
|
||||
}
|
||||
let worker = answer_once(received, worker_events(&case, &codecs));
|
||||
let mut body = payload(&case, false);
|
||||
body.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(extra.as_object().unwrap().clone());
|
||||
let text = capture(create(State(state), Ok(Json(body))).await).await;
|
||||
(text, worker.await.unwrap())
|
||||
}
|
||||
|
||||
/// gpt-oss takes temperature, top_p and seed: the server defaults for the other samplers do not reach its worker.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn the_server_defaults_of_samplers_gptoss_lacks_do_not_reach_its_worker() {
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
let (text, sampling) = ask("g-final-only", json!({}), Some(busy_defaults())).await;
|
||||
assert!(text.starts_with("200 "), "{text}");
|
||||
assert_eq!(sampling.top_k, None);
|
||||
assert_eq!(sampling.min_p, None);
|
||||
assert_eq!(sampling.repetition_penalty, None);
|
||||
assert_eq!(sampling.frequency_penalty, None);
|
||||
assert_eq!(sampling.presence_penalty, None);
|
||||
assert_eq!(sampling.penalty_last_n, None);
|
||||
}
|
||||
|
||||
/// A sampler set to its neutral value is not a request for the sampler: it is accepted.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn neutral_values_of_samplers_gptoss_lacks_are_accepted() {
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
let neutral =
|
||||
json!({"repetition_penalty": 1.0, "frequency_penalty": 0.0, "presence_penalty": 0.0});
|
||||
let (text, sampling) = ask("g-final-only", neutral, None).await;
|
||||
assert!(text.starts_with("200 "), "{text}");
|
||||
assert_eq!(sampling.repetition_penalty, None);
|
||||
}
|
||||
|
||||
/// Qwen takes every sampler: the request's and the defaults' reach the worker.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn qwen_keeps_every_sampler_of_the_request_and_the_defaults() {
|
||||
let _one = ONE_AT_A_TIME.lock().await;
|
||||
let (text, sampling) = ask("q-text", json!({}), Some(busy_defaults())).await;
|
||||
assert!(text.starts_with("200 "), "{text}");
|
||||
assert_eq!(sampling.top_k, Some(20));
|
||||
assert_eq!(sampling.min_p, Some(0.05));
|
||||
assert_eq!(sampling.repetition_penalty, Some(1.1));
|
||||
assert_eq!(sampling.frequency_penalty, Some(0.1));
|
||||
assert_eq!(sampling.presence_penalty, Some(0.2));
|
||||
assert_eq!(sampling.penalty_last_n, Some(64));
|
||||
let own = json!({
|
||||
"top_k": 5, "min_p": 0.2, "repetition_penalty": 1.3, "frequency_penalty": 0.4, "presence_penalty": 0.5
|
||||
});
|
||||
let (_, sampling) = ask("q-text", own, Some(busy_defaults())).await;
|
||||
assert_eq!(sampling.top_k, Some(5));
|
||||
assert_eq!(sampling.min_p, Some(0.2));
|
||||
assert_eq!(sampling.repetition_penalty, Some(1.3));
|
||||
assert_eq!(sampling.frequency_penalty, Some(0.4));
|
||||
assert_eq!(sampling.presence_penalty, Some(0.5));
|
||||
}
|
||||
@@ -15,6 +15,10 @@ pub enum End {
|
||||
Fail(String),
|
||||
/// The worker closes its side after the tokens without a `DONE` or an error.
|
||||
Close,
|
||||
/// The worker reports `DONE` before the first token.
|
||||
DoneFirst,
|
||||
/// The worker reports a failure after the tokens and their `DONE`.
|
||||
ErrorAfterDone(String),
|
||||
}
|
||||
|
||||
pub struct Case {
|
||||
@@ -25,6 +29,8 @@ pub struct Case {
|
||||
pub end: End,
|
||||
pub recovery: bool,
|
||||
pub reasoning_tokens: Option<u64>,
|
||||
/// The generated count the worker reports, when it is not the number of tokens sent.
|
||||
pub generated: Option<u64>,
|
||||
}
|
||||
|
||||
fn end_of(word: &str) -> End {
|
||||
@@ -33,6 +39,10 @@ fn end_of(word: &str) -> End {
|
||||
"length" => End::Done(Finish::Length),
|
||||
"cancel" => End::Done(Finish::Cancel),
|
||||
"close" => End::Close,
|
||||
"done-first" => End::DoneFirst,
|
||||
other if other.starts_with("error-after-done:") => {
|
||||
End::ErrorAfterDone(other.strip_prefix("error-after-done:").unwrap().to_owned())
|
||||
}
|
||||
other => End::Fail(other.strip_prefix("fail:").unwrap().to_owned()),
|
||||
}
|
||||
}
|
||||
@@ -54,6 +64,7 @@ fn case_of(value: &Value) -> Case {
|
||||
end: end_of(value["end"].as_str().unwrap()),
|
||||
recovery: value["recovery"].as_bool().unwrap_or(false),
|
||||
reasoning_tokens: value["reasoning_tokens"].as_u64(),
|
||||
generated: value["generated"].as_u64(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,13 +127,21 @@ pub fn token_events(ids: &[u32]) -> Vec<Result<Event, String>> {
|
||||
ids.iter().map(|id| Ok(Event::Token(*id))).collect()
|
||||
}
|
||||
|
||||
/// The events a worker sends for the tokens and the way they end.
|
||||
pub fn worker_events(ids: &[u32], end: &End) -> Vec<Result<Event, String>> {
|
||||
let mut events = token_events(ids);
|
||||
match end {
|
||||
End::Done(finish) => events.push(Ok(Event::Done(done_event(ids.len() as u64, *finish)))),
|
||||
/// The events a worker sends for a case: its tokens and the way they end.
|
||||
pub fn worker_events(case: &Case, codecs: &Codecs) -> Vec<Result<Event, String>> {
|
||||
let ids = script_ids(case, codecs);
|
||||
let mut events = token_events(&ids);
|
||||
let generated = case.generated.unwrap_or(ids.len() as u64);
|
||||
let done = |finish| Ok(Event::Done(done_event(generated, finish)));
|
||||
match &case.end {
|
||||
End::Done(finish) => events.push(done(*finish)),
|
||||
End::Fail(message) => events.push(Err(message.clone())),
|
||||
End::Close => {}
|
||||
End::DoneFirst => events.insert(0, done(Finish::Stop)),
|
||||
End::ErrorAfterDone(message) => {
|
||||
events.push(done(Finish::Stop));
|
||||
events.push(Err(message.clone()));
|
||||
}
|
||||
}
|
||||
events
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user