radiance 1.0.4

- oh-my-pi works unmodified on every Qwen model. Its requests carry a top-level
  preserve_thinking, which was refused as an unknown field; it now reaches the chat template the
  way enable_thinking does. Its task tool's `model` parameter is the `not` of every JSON type,
  which the 1.0.1 fix did not cover: any `not` that excludes every value is now an argument no
  call writes, under every chat template and Flash-Next's declared format.
- Command-line defaults and bounds for what a request leaves out. A flag left unset changes
  nothing; /server_info reports the values under request_defaults.
  - Sampling: --typical-p, --presence-penalty, --frequency-penalty, --repetition-penalty,
    --repeat-last-n, --dry-multiplier, --dry-base, --dry-allowed-length, --dry-penalty-last-n,
    --dry-sequence-breakers, --xtc-probability, --xtc-threshold.
  - Requests: --default-max-tokens N|auto, --max-tokens-cap, --max-n, --max-queued-requests,
    --max-stop-strings, --max-stop-bytes.
  - Chat template: --chat-template-kwargs JSON|@FILE sets template variables a request's own
    chat_template_kwargs override; --reasoning-format auto|none.
  - HTTP: --http-threads, --read-timeout, --write-timeout, --keep-alive-timeout, --max-body-mib,
    --retry-after, --no-cors.
  - Media: --image-min-pixels, --image-max-pixels, --video-min-pixels, --video-max-pixels,
    --video-fps, --video-min-frames, --video-max-frames, --video-max-frame-tokens,
    --max-source-pixels.
- Under --reasoning-effort none or minimal, a request that sets enable_thinking true now thinks
  instead of having the deployment default override it.
- --temp, --top-p, --min-p and --top-k refuse a value that is not a number instead of reading
  it as 0.
This commit is contained in:
Deadcode committed 2026-10-04 17:52:35 +02:00
1 parent d008fdb32b
commit 09fb41735b
23 files changed
+2478 -104

No files matched your search

+31
View File
@@ -916,6 +916,37 @@ TEST(an_argument_no_value_satisfies_has_no_arm_in_either_grammar) {
CHECK_EQ(g.text, b->prompt().grammar);
}
/* OH-MY-PI 18.6.0's TOOLS AS AN UNMODIFIED CLIENT SENDS THEM (tests/data/chat), descriptions aside.
* Its `task` tool spells the optional `model` as the `not` of a union of all six JSON types rather
* than `{"not": {}}`, which is `never` all the same; the declared format must give it no arm and
* agree with the derived format about the whole tool set, as it does about every other. */
TEST(oh_my_pis_tools_build_the_same_grammar_in_either_format) {
std::ifstream f(std::string(RAD_CHAT_FIXTURES) + "/oh-my-pi-18.6.0-tools.json");
std::ostringstream ss;
ss << f.rdbuf();
const json tools = json::parse(ss.str());
REQUIRE(tools.is_array() && tools.size() == 11);
RadChatFormat c = qwen_abi();
ChatFormat declared;
std::string why;
CHECK_OK(chat_format_from_abi(&c, &declared, &why));
ChatGrammarRequest gq;
gq.tools = &tools;
ChatGrammar g;
CHECK_OK(chat_format_grammar(declared, gq, &g, &why));
if (!why.empty()) fprintf(stderr, " %s\n", why.c_str());
CHECK(!g.text.empty());
CHECK(g.text.find("task-arg-model") == std::string::npos);
CHECK(g.text.find("task-arg-tasks") != std::string::npos);
Bench* b = bench(models()[0], ChatRequest{}, tools);
if (!b) return;
gq.generation_prompt = b->prompt().generation_prompt;
CHECK_OK(chat_format_grammar(declared, gq, &g, &why));
CHECK_EQ(g.text, b->prompt().grammar);
}
/* THE STRUCT GROWS AT ITS END. A plugin built against a header with fewer fields reports a smaller
* struct_size, and the fields past it read as zero; one built against a larger header has its
* extra fields ignored. Nothing is read past what the plugin said it has. */
+251
View File
@@ -23,6 +23,7 @@
#include <cstdio>
#include <string>
#include <unistd.h>
#include <cstring>
#include <vector>
using namespace rad;
@@ -52,6 +53,13 @@ int parse(std::initializer_list<const char*> v, Config* c) {
return config_parse((int)argv.size(), argv.data(), c);
}
/* The same into a Config of its own, for a case whose answer depends on flags not carrying over
* from an earlier parse. */
int parse_fresh(std::initializer_list<const char*> v) {
Config c;
return parse(v, &c);
}
/* The level `-v` moves and every other test in this binary reads. */
struct LogGuard {
Log saved = log_level();
@@ -383,6 +391,243 @@ TEST(the_sampler_defaults_are_range_checked_at_startup) {
CHECK(d.sample_min_p < 0.0f);
}
/* A NUMBER IS THE WHOLE ARGUMENT. atof read "high" as 0, which for --temp is greedy decoding. */
TEST(a_sampler_default_that_is_not_a_number_is_refused) {
Config c;
CHECK_EQ(parse({ "--temp", "high" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--temp", "0.7x" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--temp", "" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--top-k", "20.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--top-p", "inf" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--presence-penalty", "nan" }, &c), RAD_E_INVAL);
}
/* ================================================================== request defaults and bounds
*
* NONE OF THEM MOVES ANYTHING UNTIL IT IS GIVEN. Each field's unset value is the one the server
* used before the flag existed (or a marker the engine reads as "leave it"), so a command line
* that names none of them serves exactly what it served before. */
TEST(the_request_defaults_and_bounds_are_unset_until_given) {
Config d;
CHECK_EQ(parse({}, &d), RAD_OK);
CHECK(!d.sample_typical_p && !d.sample_rep_penalty && !d.sample_pres_penalty &&
!d.sample_freq_penalty && !d.sample_dry_multiplier && !d.sample_dry_base &&
!d.sample_xtc_probability && !d.sample_xtc_threshold);
CHECK(!d.sample_penalty_last_n && !d.sample_dry_allowed_length &&
!d.sample_dry_penalty_last_n && !d.sample_dry_seq_breakers);
CHECK_EQ(d.default_max_tokens, -1ll);
CHECK_EQ(d.max_tokens_cap, 0ll);
CHECK_EQ(d.max_n, 0ll);
CHECK_EQ(d.max_queued_requests, 0ll);
CHECK_EQ(d.max_stop_strings, 64ll);
CHECK_EQ(d.max_stop_bytes, 4096ll);
CHECK(d.chat_template_kwargs.empty());
CHECK_EQ(d.reasoning_format, std::string("auto"));
/* The transport's own values (server/http.h TransportOptions). */
CHECK_EQ(d.http_threads, 0);
CHECK_EQ(d.http_read_timeout_s, 30);
CHECK_EQ(d.http_write_timeout_s, 600);
CHECK_EQ(d.http_keep_alive_timeout_s, 5);
CHECK_EQ(d.http_max_body_mib, 512ll);
CHECK_EQ(d.retry_after_s, 1);
CHECK(d.cors);
CHECK(!d.media_flags_set());
}
TEST(the_request_defaults_reach_their_fields) {
Config c;
CHECK_EQ(parse({ "--typical-p", "0.9", "--presence-penalty", "1.5",
"--frequency-penalty", "-0.5", "--repetition-penalty", "1.05",
"--repeat-last-n", "-1", "--dry-multiplier", "0.8", "--dry-base", "1.75",
"--dry-allowed-length", "3", "--dry-penalty-last-n", "256",
"--dry-sequence-breakers", R"(["\n", ":"])",
"--xtc-probability", "0.5", "--xtc-threshold", "0.1",
"--default-max-tokens", "4096", "--max-tokens-cap", "8192",
"--max-n", "4", "--max-queued-requests", "64",
"--max-stop-strings", "8", "--max-stop-bytes", "256",
"--reasoning-format", "none" }, &c), RAD_OK);
CHECK_NEAR(*c.sample_typical_p, 0.9f, 1e-6);
CHECK_NEAR(*c.sample_pres_penalty, 1.5f, 1e-6);
CHECK_NEAR(*c.sample_freq_penalty, -0.5f, 1e-6);
CHECK_NEAR(*c.sample_rep_penalty, 1.05f, 1e-6);
CHECK_EQ(*c.sample_penalty_last_n, -1);
CHECK_NEAR(*c.sample_dry_multiplier, 0.8f, 1e-6);
CHECK_NEAR(*c.sample_dry_base, 1.75f, 1e-6);
CHECK_EQ(*c.sample_dry_allowed_length, 3);
CHECK_EQ(*c.sample_dry_penalty_last_n, 256);
CHECK(*c.sample_dry_seq_breakers == std::vector<std::string>({ "\n", ":" }));
CHECK_NEAR(*c.sample_xtc_probability, 0.5f, 1e-6);
CHECK_NEAR(*c.sample_xtc_threshold, 0.1f, 1e-6);
CHECK_EQ(c.default_max_tokens, 4096ll);
CHECK_EQ(c.max_tokens_cap, 8192ll);
CHECK_EQ(c.max_n, 4ll);
CHECK_EQ(c.max_queued_requests, 64ll);
CHECK_EQ(c.max_stop_strings, 8ll);
CHECK_EQ(c.max_stop_bytes, 256ll);
CHECK_EQ(c.reasoning_format, std::string("none"));
/* `auto` is 0, which the server resolves per request to what the context leaves. */
Config a;
CHECK_EQ(parse({ "--default-max-tokens", "auto" }, &a), RAD_OK);
CHECK_EQ(a.default_max_tokens, 0ll);
/* An empty breaker list is a stated value -- DRY with no breakers -- and stays one. */
Config e;
CHECK_EQ(parse({ "--dry-sequence-breakers", "[]" }, &e), RAD_OK);
CHECK(e.sample_dry_seq_breakers && e.sample_dry_seq_breakers->empty());
/* A negative penalty is a value a request can send, so the flag can state it. */
Config n;
CHECK_EQ(parse({ "--presence-penalty", "-2" }, &n), RAD_OK);
CHECK_NEAR(*n.sample_pres_penalty, -2.0f, 1e-6);
}
/* THE RANGES A REQUEST IS HELD TO, at startup: a default outside them would be a 400 on every
* request that leaves the field to it. */
TEST(the_request_defaults_are_range_checked_like_a_request) {
Config c;
CHECK_EQ(parse({ "--typical-p", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--typical-p", "1.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--presence-penalty", "2.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--frequency-penalty", "-3" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--repetition-penalty", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--repeat-last-n", "-2" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--dry-base", "0.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--dry-penalty-last-n", "-2" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--xtc-probability", "1.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--xtc-threshold", "-0.1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--dry-allowed-length", "3.5" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--dry-sequence-breakers", R"("\n")" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--dry-sequence-breakers", "[1]" }, &c), RAD_E_INVAL);
/* The sampler takes at most 16 (sample/host_ref.h); every DRY request would be refused. */
CHECK_EQ(parse({ "--dry-sequence-breakers",
R"(["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q"])" },
&c), RAD_E_INVAL);
CHECK_EQ(parse({ "--default-max-tokens", "0" }, &c), RAD_E_INVAL); /* auto is the word */
CHECK_EQ(parse({ "--default-max-tokens", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--default-max-tokens", "AUTO" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--default-max-tokens", "4294967296" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-tokens-cap", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-n", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-queued-requests", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-stop-strings", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-stop-bytes", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--reasoning-format", "deepseek" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--reasoning-format", "" }, &c), RAD_E_INVAL);
}
/* BOUNDS THAT CONTRADICT EACH OTHER. A default above the cap is a default no request is served
* at; an `n` above the queue is a request that is 429 however long its caller waits. */
TEST(request_bounds_that_contradict_each_other_are_refused) {
CHECK_EQ(parse_fresh({ "--default-max-tokens", "4096", "--max-tokens-cap", "1024" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--default-max-tokens", "1024", "--max-tokens-cap", "1024" }), RAD_OK);
CHECK_EQ(parse_fresh({ "--default-max-tokens", "auto", "--max-tokens-cap", "1024" }), RAD_OK);
CHECK_EQ(parse_fresh({ "--max-n", "65", "--max-queued-requests", "64" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--max-n", "64", "--max-queued-requests", "64" }), RAD_OK);
/* The queue's default is 8 x --max-num-seqs, and the check reads it. */
CHECK_EQ(parse_fresh({ "--max-num-seqs", "2", "--max-n", "17" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--max-num-seqs", "2", "--max-n", "16" }), RAD_OK);
/* And a stated queue smaller than the default n (8, or --max-num-seqs). */
CHECK_EQ(parse_fresh({ "--max-queued-requests", "4" }), RAD_E_INVAL);
}
TEST(the_template_variables_merge_key_by_key_and_later_wins) {
Config c;
CHECK_EQ(parse({ "--chat-template-kwargs", R"({"enable_thinking": false, "custom": "a"})",
"--chat-template-kwargs", R"({"custom": "b", "n": 3})" }, &c), RAD_OK);
CHECK_EQ(c.chat_template_kwargs.size(), 3u);
CHECK_EQ(c.chat_template_kwargs["enable_thinking"], std::string("false"));
CHECK_EQ(c.chat_template_kwargs["custom"], std::string("\"b\"")); /* JSON text, quoted */
CHECK_EQ(c.chat_template_kwargs["n"], std::string("3"));
/* @FILE reads the object from a file. */
char path[] = "/tmp/rad_kwargs_XXXXXX";
const int fd = mkstemp(path);
REQUIRE(fd >= 0);
const std::string body = R"({"date_string": "01 Jan 2026"})";
CHECK_EQ((size_t)write(fd, body.data(), body.size()), body.size());
close(fd);
Config f;
const std::string at = std::string("@") + path;
CHECK_EQ(parse({ "--chat-template-kwargs", at.c_str() }, &f), RAD_OK);
CHECK_EQ(f.chat_template_kwargs["date_string"], std::string("\"01 Jan 2026\""));
unlink(path);
CHECK_EQ(parse({ "--chat-template-kwargs", at.c_str() }, &f), RAD_E_INVAL); /* gone */
}
TEST(a_template_variable_the_server_owns_or_cannot_read_is_refused) {
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", "[1]" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", "{nope" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", R"("enable_thinking")" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", R"({"enable_thinking": "no"})" }), RAD_E_INVAL);
/* The renderer writes these over the context; one here would replace every conversation. */
for (const char* k : { "messages", "tools", "bos_token", "eos_token", "add_generation_prompt" }) {
const std::string j = std::string("{\"") + k + "\": 1}";
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", j.c_str() }), RAD_E_INVAL);
}
/* Two defaults for thinking, in two flags, with no order between them. */
CHECK_EQ(parse_fresh({ "--reasoning-effort", "low",
"--chat-template-kwargs", R"({"reasoning_effort": "high"})" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--chat-template-kwargs", R"({"enable_thinking": true})",
"--reasoning-effort", "none" }), RAD_E_INVAL);
CHECK_EQ(parse_fresh({ "--reasoning-effort", "low",
"--chat-template-kwargs", R"({"custom": 1})" }), RAD_OK);
}
TEST(the_transport_flags_reach_their_fields_and_hold_their_floors) {
Config c;
CHECK_EQ(parse({ "--http-threads", "16", "--read-timeout", "10", "--write-timeout", "120",
"--keep-alive-timeout", "30", "--max-body-mib", "64", "--retry-after", "5",
"--no-cors" }, &c), RAD_OK);
CHECK_EQ(c.http_threads, 16);
CHECK_EQ(c.http_read_timeout_s, 10);
CHECK_EQ(c.http_write_timeout_s, 120);
CHECK_EQ(c.http_keep_alive_timeout_s, 30);
CHECK_EQ(c.http_max_body_mib, 64ll);
CHECK_EQ(c.retry_after_s, 5);
CHECK(!c.cors);
/* The transport keeps four workers whatever it is told, so fewer is refused, not ignored. */
CHECK_EQ(parse({ "--http-threads", "2" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--read-timeout", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--write-timeout", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--keep-alive-timeout", "x" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-body-mib", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--retry-after", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--retry-after", "0" }, &c), RAD_OK);
}
TEST(the_media_flags_reach_their_fields_and_refuse_an_empty_band) {
Config c;
CHECK_EQ(parse({ "--image-min-pixels", "3136", "--image-max-pixels", "1003520",
"--video-min-pixels", "4096", "--video-max-pixels", "8388608",
"--video-fps", "1.5", "--video-min-frames", "2", "--video-max-frames", "64",
"--video-max-frame-tokens", "0", "--max-source-pixels", "16777216" }, &c),
RAD_OK);
CHECK(c.media_flags_set());
CHECK_EQ(*c.image_min_pixels, 3136ll);
CHECK_EQ(*c.image_max_pixels, 1003520ll);
CHECK_EQ(*c.video_min_pixels, 4096ll);
CHECK_EQ(*c.video_max_pixels, 8388608ll);
CHECK_NEAR(*c.video_fps, 1.5, 1e-9);
CHECK_EQ(*c.video_min_frames, 2);
CHECK_EQ(*c.video_max_frames, 64);
CHECK_EQ(*c.video_max_frame_tokens, 0); /* 0 is "no per-frame cap" */
CHECK_EQ(*c.max_source_pixels, 16777216ll);
CHECK_EQ(parse({ "--image-min-pixels", "2000", "--image-max-pixels", "1000" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-min-pixels", "2000", "--video-max-pixels", "1000" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-min-frames", "8", "--video-max-frames", "4" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--image-max-pixels", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-fps", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-fps", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-min-frames", "0" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--video-max-frame-tokens", "-1" }, &c), RAD_E_INVAL);
CHECK_EQ(parse({ "--max-source-pixels", "0" }, &c), RAD_E_INVAL);
}
/* REFUSED, NOT QUIETLY IGNORED. Accepting it and then serving the container's own drafter -- or
* none -- hands the caller a server that is not the one they asked for, with nothing in the
* output to say so. A caller coming from vLLM will reach for this flag. */
@@ -537,6 +782,12 @@ TEST(usage_names_every_flag_it_documents) {
CHECK(text.find("--tp-wire") != std::string::npos);
CHECK(text.find("--kld-record") != std::string::npos);
CHECK(text.find("--kld-ref") != std::string::npos);
for (const char* f : { "--default-max-tokens", "--max-tokens-cap", "--presence-penalty",
"--dry-sequence-breakers", "--chat-template-kwargs",
"--reasoning-format", "--max-n", "--max-queued-requests",
"--max-stop-strings", "--http-threads", "--max-body-mib", "--no-cors",
"--image-max-pixels", "--video-fps", "--max-source-pixels" })
CHECK(text.find(f) != std::string::npos);
CHECK(text.size() > 1000);
}
+484
View File
@@ -0,0 +1,484 @@
[
{
"type": "function",
"function": {
"name": "read",
"description": "read",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"path": {
"type": "string"
}
},
"required": [
"path",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "bash",
"description": "bash",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"command": {
"type": "string"
},
"timeout": {
"type": "number"
},
"cwd": {
"type": "string"
},
"pty": {
"type": "boolean"
},
"async": {
"type": "boolean"
},
"name": {
"type": "string",
"maxLength": 48
},
"ready": {
"type": "object",
"properties": {
"log": {
"type": "string"
},
"port": {
"type": "number"
},
"host": {
"type": "string"
},
"timeout": {
"type": "number"
}
},
"additionalProperties": false
}
},
"required": [
"command",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "edit",
"description": "edit",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"input": {
"type": "string"
}
},
"required": [
"input",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "eval",
"description": "eval",
"parameters": {
"type": "object",
"properties": {
"language": {
"enum": [
"py",
"js"
],
"type": "string"
},
"code": {
"type": "string"
},
"title": {
"type": "string"
},
"timeout": {
"type": "number"
},
"reset": {
"type": "boolean"
}
},
"required": [
"language",
"code"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "glob",
"description": "glob",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"path": {
"type": "string"
},
"hidden": {
"type": "boolean"
},
"gitignore": {
"type": "boolean"
},
"limit": {
"type": "number"
}
},
"additionalProperties": false,
"required": [
"i"
]
}
}
},
{
"type": "function",
"function": {
"name": "grep",
"description": "grep",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"pattern": {
"type": "string"
},
"path": {
"type": "string"
},
"case": {
"type": "boolean"
},
"gitignore": {
"type": "boolean"
},
"skip": {
"type": [
"number",
"null"
]
}
},
"required": [
"pattern",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "task",
"description": "task",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"context": {
"type": "string"
},
"tasks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"task": {
"type": "string"
},
"solutionSpace": {
"type": "string"
},
"name": {
"type": "string"
},
"agent": {
"type": "string",
"default": "task"
},
"model": {
"anyOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"outputSchema": {
"anyOf": [
{
"type": "object"
},
{
"type": "boolean"
},
{
"type": "string"
},
{
"type": "null"
}
]
},
"schemaMode": {
"enum": [
"permissive",
"strict"
],
"type": "string"
},
"tools": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"task",
"solutionSpace"
],
"additionalProperties": false
}
},
"model": {
"not": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "boolean"
},
{
"type": "object"
},
{
"type": "array"
},
{
"type": "null"
}
]
}
}
},
"required": [
"context",
"tasks",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "wait",
"description": "wait",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
}
},
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "todo",
"description": "todo",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"op": {
"enum": [
"init",
"start",
"done",
"rm",
"drop",
"block",
"unblock",
"append",
"view"
],
"type": "string"
},
"list": {
"type": "array",
"items": {
"type": "object",
"properties": {
"phase": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1
}
},
"required": [
"phase",
"items"
],
"additionalProperties": false
}
},
"task": {
"type": "string"
},
"phase": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"type": "string"
}
},
"reason": {
"type": "string"
}
},
"required": [
"op",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "web_search",
"description": "web_search",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"query": {
"type": "string"
},
"recency": {
"enum": [
"day",
"week",
"month",
"year"
],
"type": "string"
},
"limit": {
"type": "number"
},
"max_tokens": {
"type": "number"
},
"temperature": {
"type": "number"
},
"num_search_results": {
"type": "number"
}
},
"required": [
"query",
"i"
],
"additionalProperties": false
}
}
},
{
"type": "function",
"function": {
"name": "write",
"description": "write",
"parameters": {
"type": "object",
"properties": {
"i": {
"type": "string"
},
"path": {
"type": "string"
},
"content": {
"type": "string"
}
},
"required": [
"path",
"i"
],
"additionalProperties": false
}
}
}
]
+82
View File
@@ -850,6 +850,88 @@ TEST(gbnf_a_property_no_value_satisfies_is_a_key_never_written) {
CHECK(built >= 3);
}
/* THE `not` OF A SCHEMA EVERY VALUE SATISFIES is `never` too, whatever spelling the universal
* schema takes. oh-my-pi 18.6.0 sends its `task` tool's optional `model` as the `not` of a union
* of all six JSON types, not as `{"not": {}}`; refused, it failed the tool-call grammar of every
* request carrying the tool under every template that constrains arguments. A `not` of anything
* narrower is a real complement and stays refused. */
TEST(gbnf_a_not_of_every_json_type_is_never_too) {
auto bv = bpe_like(29, 1500);
auto whole = [&](const std::string& gbnf, const std::string& text) {
std::unique_ptr<GbnfGrammar> g;
std::string err;
if (GbnfGrammar::create(gbnf, "root", &bv->v, false, {}, {}, &g, &err) < 0) return false;
return g->accept_str(text) >= 0 && g->complete();
};
auto optional_model = [](const std::string& never) {
return std::string(R"({"type":"object","properties":{"context":{"type":"string"},"model":)") +
never + R"(},"required":["context"]})";
};
for (const char* never : {
R"({"not":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},)"
R"({"type":"object"},{"type":"array"},{"type":"null"}]}})",
R"({"not":{"type":["string","number","boolean","object","array","null"]}})",
R"({"not":{"anyOf":[{"type":["string","number"]},{"type":["boolean","object"]},)"
R"({"type":"array","description":"a list"},{"type":"null"}]}})",
R"({"not":{"anyOf":[{"type":"string","minLength":1},{}]}})",
R"({"not":true})",
R"({"not":{"description":"anything"}})" }) {
std::string gbnf, err;
CHECK_OK(grammar_from_json_schema(optional_model(never), "root", &gbnf, &err));
CHECK(whole(gbnf, "{\"context\": \"x\"}"));
CHECK(!whole(gbnf, "{\"context\": \"x\", \"model\": \"y\"}"));
CHECK(!whole(gbnf, "{\"context\": \"x\", \"model\": null}"));
}
for (const char* complement : {
/* no null among the six */
R"({"not":{"anyOf":[{"type":"string"},{"type":"number"},{"type":"boolean"},)"
R"({"type":"object"},{"type":"array"}]}})",
/* integer is not number */
R"({"not":{"type":["string","integer","boolean","object","array","null"]}})",
/* an alternative that constrains more than its type is not all of that type */
R"({"not":{"anyOf":[{"type":"string","minLength":1},{"type":"number"},)"
R"({"type":"boolean"},{"type":"object"},{"type":"array"},{"type":"null"}]}})",
/* a keyword beside the type narrows it */
R"({"not":{"type":["string","number","boolean","object","array","null"],"minimum":0}})",
R"({"not":{"type":"string"}})" }) {
std::string gbnf, err;
CHECK(grammar_from_json_schema(optional_model(complement), "root", &gbnf, &err) < 0);
CHECK(err.find("not") != std::string::npos);
}
}
/* OH-MY-PI 18.6.0's TOOLS AS IT SENDS THEM, captured from an unmodified client and kept whole but
* for their descriptions (tests/data/chat/oh-my-pi-18.6.0-tools.json). Every served template must
* render them and every grammar it writes must compile, which is what admission does with it. */
TEST(gbnf_oh_my_pi_tools_build_and_compile_under_every_template) {
auto bv = bpe_like(31, 3000);
const ojson tools = ojson::parse(slurp(fixture("../chat/oh-my-pi-18.6.0-tools.json")));
REQUIRE(tools.is_array() && tools.size() == 11);
const ojson messages = ojson::array({ ojson{ { "role", "user" }, { "content", "hi" } } });
int built = 0;
for (const TemplateCase& tc : kTemplates) {
ChatTemplate ct;
REQUIRE(ct.load(slurp(fixture(tc.file)), tc.bos, tc.eos) >= 0);
for (int par = 0; par < 2; ++par) {
ChatOptions opt;
opt.tool_choice = "auto";
opt.parallel_tool_calls = par != 0;
opt.template_kwargs["preserve_thinking"] = "true";
ChatPrompt cp;
CHECK(ct.apply(messages, tools, opt, &cp) >= 0);
if (cp.grammar.empty()) continue;
++built;
std::unique_ptr<GbnfGrammar> g;
std::string err;
CHECK(GbnfGrammar::create(cp.grammar, "root", &bv->v, false, {}, {}, &g, &err) >= 0);
if (!err.empty()) fprintf(stderr, " %s: %s\n", tc.file, err.c_str());
}
}
CHECK(built >= 2);
}
/* The grammars a chat template writes for its own tool-call syntax, from the three templates the
* engine serves: walked, and replayed over four agent calls wherever the syntax is the one the
* calls are written in. */
+29
View File
@@ -303,4 +303,33 @@ TEST(every_admitted_video_format_decodes) {
}
}
/* THE BANDS ARE CHECKED AS A WHOLE, because the command line can replace one end of a band and
* leave the other to the container: a minimum stated alone can land above the container's maximum. */
TEST(a_media_configuration_with_an_empty_band_names_it) {
mm::VisionConfig c;
c.image_min_pixels = 65536;
c.image_max_pixels = 16777216;
c.video_min_pixels = 4096;
c.video_max_pixels = 25165824;
std::string why;
CHECK_EQ(c.check(&why), RAD_OK);
mm::VisionConfig b = c;
b.image_min_pixels = 32 << 20;
CHECK_EQ(b.check(&why), RAD_E_INVAL);
CHECK(why.find("image_max_pixels") != std::string::npos);
b = c;
b.video_max_pixels = 1000;
CHECK_EQ(b.check(&why), RAD_E_INVAL);
CHECK(why.find("video_max_pixels") != std::string::npos);
b = c;
b.fps = 0.0;
CHECK_EQ(b.check(&why), RAD_E_INVAL);
b = c;
b.min_frames = 10;
b.max_frames = 5;
CHECK_EQ(b.check(&why), RAD_E_INVAL);
CHECK(why.find("max_frames") != std::string::npos);
}
RAD_TEST_MAIN()
+437 -2
View File
@@ -29,6 +29,7 @@
#include <atomic>
#include <chrono>
#include <cstring>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
@@ -89,9 +90,15 @@ struct FakeChatTemplate : IChatTemplate {
mutable int last_n_tools = 0;
int apply(const json& messages, const json& tools, const ChatRenderOptions& opt,
ChatRender* out, std::string*) const override {
ChatRender* out, std::string* why) const override {
last_opt = opt;
last_n_tools = (int)tools.size();
/* A template that raise_exception's on one variable, as Qwen3.8's does on an effort it
* does not know. */
if (!refuse_kwarg.empty() && opt.template_kwargs.count(refuse_kwarg)) {
if (why) *why = "the template does not take " + refuse_kwarg;
return RAD_E_UNSUPPORTED;
}
std::string s;
if (!tools.empty()) s += "[tools:" + std::to_string(tools.size()) + "]";
if (!opt.enable_thinking) s += "[nothink]";
@@ -125,6 +132,7 @@ struct FakeChatTemplate : IChatTemplate {
bool with_parser = false;
std::string refusal;
std::string refuse_kwarg;
/* Reads "TOOL:<name>:<json...>" -- everything after the second colon is the arguments,
* however incomplete, and it arrives a piece at a time. That is exactly the shape the
@@ -945,6 +953,349 @@ TEST(the_ways_of_turning_thinking_off_all_reach_the_template) {
CHECK(f.tmpl.last_opt.enable_thinking);
}
/* ================================================================== deployment defaults
*
* Every default the command line can move, seen from a request: it reaches a request that leaves
* the field out, and loses to one that names it. */
static OaiRequest chat_with(Fixture& f, const OaiLimits& lim, const std::string& extra) {
OaiRequest r;
ApiError e;
const std::string body =
R"({"messages":[{"role":"user","content":"hi"}])" + (extra.empty() ? "" : "," + extra) + "}";
CHECK_OK(parse_chat_request(body, f.odeps(), lim, &r, &e));
return r;
}
TEST(the_rest_of_the_sampler_is_seeded_from_the_deployment_too) {
Fixture f;
OaiLimits lim = f.limits();
lim.default_sampling.pres_penalty = 1.5f;
lim.default_sampling.rep_penalty = 1.05f;
lim.default_sampling.penalty_last_n = 256;
lim.default_sampling.typical_p = 0.9f;
lim.default_sampling.dry_multiplier = 0.8f;
lim.default_sampling.xtc_probability = 0.5f;
OaiRequest r = chat_with(f, lim, "");
CHECK_NEAR(r.sp.pres_penalty, 1.5f, 1e-6);
CHECK_NEAR(r.sp.rep_penalty, 1.05f, 1e-6);
CHECK_EQ(r.sp.penalty_last_n, 256);
CHECK_NEAR(r.sp.typical_p, 0.9f, 1e-6);
CHECK_NEAR(r.sp.dry_multiplier, 0.8f, 1e-6);
CHECK_NEAR(r.sp.xtc_probability, 0.5f, 1e-6);
OaiRequest r2 = chat_with(f, lim, R"("presence_penalty":0,"repeat_last_n":64)");
CHECK_NEAR(r2.sp.pres_penalty, 0.0f, 1e-6);
CHECK_EQ(r2.sp.penalty_last_n, 64);
CHECK_NEAR(r2.sp.rep_penalty, 1.05f, 1e-6);
/* A STATED EMPTY BREAKER LIST STAYS EMPTY, which SamplingParams alone cannot say: unstated is
* llama-server's four. A request's own list still wins. */
lim.default_sampling.dry_seq_breakers.clear();
lim.default_dry_breakers_set = true;
CHECK(chat_with(f, lim, "").sp.dry_seq_breakers.empty());
CHECK(chat_with(f, lim, R"("dry_sequence_breakers":["x"])").sp.dry_seq_breakers ==
std::vector<std::string>({ "x" }));
lim.default_sampling.dry_seq_breakers = { "\n" };
CHECK(chat_with(f, lim, "").sp.dry_seq_breakers == std::vector<std::string>({ "\n" }));
}
/* A sampler field's range is ONE table for the request and the flag; this holds the request half
* of it to the exact sentences it has always carried. */
TEST(the_shared_sampler_ranges_refuse_in_the_words_a_request_always_got) {
struct Case { const char* body; const char* param; const char* msg; };
const Case cases[] = {
{ R"("temperature":2.5)", "temperature", "must be in [0, 2], got 2.5" },
{ R"("top_p":0)", "top_p", "must be in (0, 1]" },
{ R"("top_k":-1)", "top_k", "must be >= 0 (0 disables it)" },
{ R"("min_p":1.5)", "min_p", "must be in [0, 1], got 1.5" },
{ R"("typical_p":0)", "typical_p", "must be in (0, 1]" },
{ R"("presence_penalty":3)", "presence_penalty", "must be in [-2, 2], got 3" },
{ R"("frequency_penalty":-3)", "frequency_penalty", "must be in [-2, 2], got -3" },
{ R"("repetition_penalty":0)", "repetition_penalty", "must be > 0" },
{ R"("repeat_last_n":-2)", "repeat_last_n", "must be >= -1" },
{ R"("xtc_probability":2)", "xtc_probability", "must be in [0, 1], got 2" },
{ R"("xtc_threshold":-1)", "xtc_threshold", "must be in [0, 1], got -1" },
{ R"("dry_base":0.5)", "dry_base", "must be >= 1" },
{ R"("dry_penalty_last_n":-2)", "dry_penalty_last_n", "must be >= -1" },
};
Fixture f;
for (const Case& c : cases) {
OaiRequest r;
ApiError e;
const std::string body =
std::string(R"({"messages":[{"role":"user","content":"hi"}],)") + c.body + "}";
CHECK_EQ(parse_chat_request(body, f.odeps(), f.limits(), &r, &e), RAD_E_INVAL);
CHECK_EQ(e.param, std::string(c.param));
CHECK_EQ(e.message, std::string(c.param) + ": " + c.msg);
}
/* The other half: the same function, as config.cpp calls it. */
std::string why;
CHECK(sampler_value_ok("presence_penalty", -2.0, &why));
CHECK(!sampler_value_ok("presence_penalty", std::nan(""), &why));
CHECK(!sampler_value_ok("temperature", std::nan(""), &why));
CHECK(sampler_value_ok("dry_multiplier", -5.0, &why)); /* unbounded, as a request is */
}
/* `--default-max-tokens auto` IS WHAT THE CONTEXT LEAVES, and `--max-tokens-cap` CLAMPS like the
* context does: a request asking for more is served up to the cap, not refused. */
TEST(the_max_tokens_default_and_cap_bound_a_request_like_the_context) {
Fixture f;
OaiLimits lim = f.limits();
lim.max_ctx = 64;
lim.default_max_tokens = 0;
OaiRequest r = chat_with(f, lim, "");
CHECK_EQ((int64_t)r.prompts[0].tokens.size() + r.max_tokens, lim.max_ctx);
CHECK_EQ(chat_with(f, lim, R"("max_tokens":5)").max_tokens, 5);
lim.max_tokens_cap = 10;
CHECK_EQ(chat_with(f, lim, "").max_tokens, 10); /* auto, then the cap */
CHECK_EQ(chat_with(f, lim, R"("max_tokens":9999)").max_tokens, 10);
CHECK_EQ(chat_with(f, lim, R"("max_tokens":5)").max_tokens, 5);
/* The cap holds where no context is declared, and on /v1/completions. */
lim.max_ctx = 0;
lim.default_max_tokens = 512;
CHECK_EQ(chat_with(f, lim, "").max_tokens, 10);
OaiRequest c;
ApiError e;
CHECK_OK(parse_completion_request(R"({"prompt":"ab","max_tokens":100})", f.odeps(), lim, &c, &e));
CHECK_EQ(c.max_tokens, 10);
/* Unset, nothing moves: the fixture's default with no cap and no context. */
CHECK_EQ(chat_with(f, f.limits(), "").max_tokens, 32);
CHECK_EQ(chat_with(f, f.limits(), R"("max_tokens":100000)").max_tokens, 100000);
}
TEST(the_stop_bounds_are_the_deployments) {
Fixture f;
OaiLimits lim = f.limits();
lim.max_stops = 2;
lim.max_stop_bytes = 3;
OaiRequest r;
ApiError e;
CHECK_OK(parse_chat_request(R"({"messages":[{"role":"user","content":"hi"}],"stop":["a","bcd"]})",
f.odeps(), lim, &r, &e));
CHECK_EQ(parse_chat_request(R"({"messages":[{"role":"user","content":"hi"}],"stop":["a","b","c"]})",
f.odeps(), lim, &r, &e), RAD_E_INVAL);
CHECK(has(e.message, "at most 2 stop strings"));
CHECK_EQ(parse_chat_request(R"({"messages":[{"role":"user","content":"hi"}],"stop":"abcd"})",
f.odeps(), lim, &r, &e), RAD_E_INVAL);
CHECK(has(e.message, "at most 3 bytes"));
}
/* THE DEPLOYMENT'S TEMPLATE VARIABLES go under the request's, key by key. */
TEST(the_deployments_template_variables_reach_the_template_under_the_requests) {
Fixture f;
OaiLimits lim = f.limits();
lim.default_template_kwargs = { { "custom", "\"a\"" }, { "depth", "2" } };
chat_with(f, lim, "");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("custom"), std::string("\"a\""));
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("depth"), std::string("2"));
chat_with(f, lim, R"("chat_template_kwargs":{"custom":"b"})");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("custom"), std::string("\"b\""));
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("depth"), std::string("2"));
lim.reasoning_format = "none";
chat_with(f, lim, "");
CHECK_EQ(f.tmpl.last_opt.reasoning_format, std::string("none"));
chat_with(f, f.limits(), "");
CHECK_EQ(f.tmpl.last_opt.reasoning_format, std::string("auto"));
CHECK(f.tmpl.last_opt.template_kwargs.empty());
/* /tokenize renders with the same variables, so the prompt it counts is the prompt chat
* serves -- and with none set it renders exactly as before. */
FakeScheduler sched;
FakeTokenizer tok;
FakeChatTemplate tmpl;
Deps d;
d.sched = &sched;
d.tok = &tok;
d.chat = &tmpl;
const std::string body = R"({"messages":[{"role":"user","content":"hi"}]})";
{
ServerOptions o;
o.default_template_kwargs = { { "enable_thinking", "false" }, { "custom", "\"a\"" } };
Server srv(d, o);
CHECK_EQ(srv.router().call("POST", "/tokenize", body).status, 200);
CHECK(!tmpl.last_opt.enable_thinking);
CHECK_EQ(tmpl.last_opt.template_kwargs.at("custom"), std::string("\"a\""));
CHECK_EQ(srv.router().call("POST", "/tokenize",
R"({"messages":[{"role":"user","content":"hi"}],
"chat_template_kwargs":{"enable_thinking":true}})").status, 200);
CHECK_EQ(tmpl.last_opt.template_kwargs.at("enable_thinking"), std::string("true"));
}
{
ServerOptions o;
Server srv(d, o);
CHECK_EQ(srv.router().call("POST", "/tokenize", body).status, 200);
CHECK(tmpl.last_opt.enable_thinking);
CHECK(tmpl.last_opt.template_kwargs.empty());
}
}
/* THINKING IS ONE SETTING WITH THREE SPELLINGS, and a deployment default in one spelling must not
* outlive a request that used another. */
TEST(a_request_that_says_anything_about_thinking_replaces_the_deployments_default) {
Fixture f;
OaiLimits off = f.limits();
off.default_template_kwargs = { { "enable_thinking", "false" } };
chat_with(f, off, "");
CHECK(!f.tmpl.last_opt.enable_thinking);
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("enable_thinking"), std::string("false"));
for (const char* extra : { R"("enable_thinking":true)",
R"("chat_template_kwargs":{"enable_thinking":true})",
R"("reasoning_effort":"high")",
R"("chat_template_kwargs":{"reasoning_effort":"high"})" }) {
chat_with(f, off, extra);
CHECK(f.tmpl.last_opt.enable_thinking);
}
chat_with(f, off, R"("reasoning_effort":"high")");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"high\""));
CHECK(!f.tmpl.last_opt.template_kwargs.count("enable_thinking"));
/* An empty effort is no effort, and leaves the default standing. */
chat_with(f, off, R"("reasoning_effort":"")");
CHECK(!f.tmpl.last_opt.enable_thinking);
/* --reasoning-effort none is the same default in its other spelling. */
OaiLimits none = f.limits();
none.default_reasoning_effort = "none";
chat_with(f, none, "");
CHECK(!f.tmpl.last_opt.enable_thinking);
chat_with(f, none, R"("enable_thinking":true)");
CHECK(f.tmpl.last_opt.enable_thinking);
chat_with(f, none, R"("reasoning_effort":"low")");
CHECK(f.tmpl.last_opt.enable_thinking);
/* A LEVEL is an effort default: a request that only turns thinking on keeps it, as before,
* and one that names its own effort replaces it. */
OaiLimits low = f.limits();
low.default_reasoning_effort = "low";
chat_with(f, low, R"("enable_thinking":true)");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"low\""));
chat_with(f, low, R"("reasoning_effort":"xhigh")");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"xhigh\""));
chat_with(f, low, R"("chat_template_kwargs":{"reasoning_effort":"xhigh"})");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"xhigh\""));
OaiLimits kw_low = f.limits();
kw_low.default_template_kwargs = { { "reasoning_effort", "\"low\"" } };
chat_with(f, kw_low, R"("enable_thinking":true)");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"low\""));
chat_with(f, kw_low, R"("reasoning_effort":"xhigh")");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("reasoning_effort"), std::string("\"xhigh\""));
}
/* A TEMPLATE DEFAULT THE TEMPLATE REFUSES is found at startup, and attributed to the flag that
* set it. A render that fails without any default blames nothing. */
TEST(a_template_default_the_template_refuses_is_found_before_a_caller_sends_one) {
auto run = [](const std::map<std::string, std::string>& kw, const std::string& effort,
const std::string& refuse, std::string* kw_why, std::string* eff_why) {
FakeScheduler sched;
FakeTokenizer tok;
FakeChatTemplate tmpl;
tmpl.refuse_kwarg = refuse;
Deps d;
d.sched = &sched;
d.tok = &tok;
d.chat = &tmpl;
ServerOptions o;
o.default_template_kwargs = kw;
o.default_reasoning_effort = effort;
Server srv(d, o);
srv.check_template_defaults(kw_why, eff_why);
};
std::string kw_why, eff_why;
run({ { "bad", "1" } }, "", "bad", &kw_why, &eff_why);
CHECK(has(kw_why, "does not take bad"));
CHECK(eff_why.empty());
run({ { "fine", "1" } }, "high", "reasoning_effort", &kw_why, &eff_why);
CHECK(kw_why.empty());
CHECK(has(eff_why, "does not take reasoning_effort"));
run({ { "fine", "1" } }, "high", "", &kw_why, &eff_why);
CHECK(kw_why.empty() && eff_why.empty());
/* "none" is enable_thinking false, never a template variable, so nothing to refuse. */
run({}, "none", "reasoning_effort", &kw_why, &eff_why);
CHECK(kw_why.empty() && eff_why.empty());
}
TEST(server_info_reports_what_a_request_that_leaves_a_field_out_gets) {
FakeScheduler sched;
FakeTokenizer tok;
FakeChatTemplate tmpl;
Deps d;
d.sched = &sched;
d.tok = &tok;
d.chat = &tmpl;
{
ServerOptions o;
Server srv(d, o);
const json j = json::parse(srv.router().call("GET", "/server_info").body);
CHECK_EQ(j["server"]["default_max_tokens"].get<int64_t>(), 512);
CHECK(j["server"]["max_tokens_cap"].is_null());
CHECK_EQ(j["server"]["max_stop_strings"].get<int64_t>(), 64);
const json& r = j["request_defaults"];
CHECK_NEAR(r["temperature"].get<double>(), 1.0, 1e-9);
CHECK_NEAR(r["repetition_penalty"].get<double>(), 1.0, 1e-9);
CHECK_EQ(r["dry_sequence_breakers"].size(), 4u);
CHECK(r["reasoning_effort"].is_null());
CHECK(r["chat_template_kwargs"].is_object() && r["chat_template_kwargs"].empty());
CHECK_EQ(r["reasoning_format"].get<std::string>(), std::string("auto"));
}
{
ServerOptions o;
o.default_max_tokens = 0;
o.max_tokens_cap = 4096;
o.default_sampling.pres_penalty = 1.5f;
o.default_template_kwargs = { { "enable_thinking", "false" }, { "tag", "\"x\"" } };
Server srv(d, o);
const json j = json::parse(srv.router().call("GET", "/server_info").body);
CHECK(j["server"]["default_max_tokens"].is_null()); /* auto */
CHECK_EQ(j["server"]["max_tokens_cap"].get<int64_t>(), 4096);
const json& r = j["request_defaults"];
CHECK_NEAR(r["presence_penalty"].get<double>(), 1.5, 1e-6);
CHECK_EQ(r["chat_template_kwargs"]["enable_thinking"].get<bool>(), false);
CHECK_EQ(r["chat_template_kwargs"]["tag"].get<std::string>(), std::string("x"));
}
}
/* `preserve_thinking` AT THE TOP LEVEL IS THE TEMPLATE VARIABLE, as `enable_thinking` is. oh-my-pi
* sends it there and in chat_template_kwargs on every request to a Qwen model, and refusing it as
* an unknown field failed all of them. */
TEST(preserve_thinking_at_the_top_level_reaches_the_template) {
Fixture f;
OaiLimits lim = f.limits();
chat_with(f, lim, R"("preserve_thinking":true)");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("preserve_thinking"), std::string("true"));
CHECK(f.tmpl.last_opt.enable_thinking); /* a history knob, not a thinking one */
/* oh-my-pi's exact pair, and the top-level spelling winning a disagreement. */
chat_with(f, lim, R"("preserve_thinking":true,"chat_template_kwargs":{"preserve_thinking":true})");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("preserve_thinking"), std::string("true"));
chat_with(f, lim, R"("preserve_thinking":false,"chat_template_kwargs":{"preserve_thinking":true})");
CHECK_EQ(f.tmpl.last_opt.template_kwargs.at("preserve_thinking"), std::string("false"));
chat_with(f, lim, "");
CHECK(!f.tmpl.last_opt.template_kwargs.count("preserve_thinking"));
OaiRequest r;
ApiError e;
CHECK_EQ(parse_chat_request(R"({"messages":[{"role":"user","content":"hi"}],"preserve_thinking":"yes"})",
f.odeps(), lim, &r, &e), RAD_E_INVAL);
CHECK_EQ(e.param, std::string("preserve_thinking"));
/* A chat field, not a completion one: /v1/completions has no template to hand it to. */
CHECK_EQ(parse_completion_request(R"({"prompt":"ab","preserve_thinking":true})", f.odeps(), lim,
&r, &e), RAD_E_INVAL);
}
/* parallel_tool_calls reaches the template, because it is the one tool-choice knob the generated
* grammar actually honours -- it narrows the repetition rule rather than being advice. */
TEST(parallel_tool_calls_false_reaches_the_template) {
@@ -1852,7 +2203,8 @@ struct LiveServer {
std::vector<int> fds;
/* Generations never finish on their own; each one holds its worker until its peer leaves. */
LiveServer(int n_threads, int64_t max_seqs) {
LiveServer(int n_threads, int64_t max_seqs,
const std::function<void(ServerOptions&)>& tweak = nullptr) {
sched.reply = "abcdefgh";
sched.hang = true;
Deps d;
@@ -1864,6 +2216,7 @@ struct LiveServer {
o.n_threads = n_threads;
o.max_seqs = max_seqs;
o.default_max_tokens = 32;
if (tweak) tweak(o);
srv.reset(new Server(d, o));
serving = std::thread([this] { srv->run(); });
for (int i = 0; i < 500 && srv->port() == 0; ++i)
@@ -1912,4 +2265,86 @@ TEST(a_probe_and_a_429_are_answered_while_every_admitted_request_holds_a_worker)
CHECK(has(status_line(probe), " 200 "));
}
/* The response head on `fd`: everything up to the blank line, or what arrived before the read
* timeout. */
static std::string response_head(int fd) {
std::string got;
char buf[512];
while (got.find("\r\n\r\n") == std::string::npos) {
const ssize_t n = ::recv(fd, buf, sizeof buf, 0);
if (n <= 0) break;
got.append(buf, (size_t)n);
}
return got.substr(0, got.find("\r\n\r\n"));
}
/* THE TRANSPORT SETTINGS REACH THE LISTENER. CORS headers are on unless turned off, and a body
* past --max-body-mib is refused before it is read. */
TEST(the_transport_settings_reach_the_listener) {
const std::string probe = "GET /health HTTP/1.1\r\nHost: t\r\nConnection: close\r\n\r\n";
{
LiveServer s(4, 1);
REQUIRE(s.srv->port() > 0);
const int fd = s.open(probe);
REQUIRE(fd >= 0);
CHECK(has(response_head(fd), "Access-Control-Allow-Origin: *"));
}
{
LiveServer s(4, 1, [](ServerOptions& o) {
o.cors = false;
o.max_body_bytes = 64;
});
REQUIRE(s.srv->port() > 0);
const int fd = s.open(probe);
REQUIRE(fd >= 0);
const std::string head = response_head(fd);
CHECK(has(head, " 200 "));
CHECK(!has(head, "Access-Control-Allow-Origin"));
const std::string big = std::string(R"({"prompt":")") + std::string(100, 'x') + "\"}";
const int b = s.open("POST /v1/completions HTTP/1.1\r\nHost: t\r\nConnection: close\r\n"
"Content-Type: application/json\r\nContent-Length: " +
std::to_string(big.size()) + "\r\n\r\n" + big);
REQUIRE(b >= 0);
CHECK(has(status_line(b), " 413 "));
}
}
/* The Retry-After a 429 carries is the deployment's. */
TEST(a_429_carries_the_deployments_retry_after) {
FakeScheduler sched;
FakeTokenizer tok;
sched.reply = "abcdefgh";
sched.hang = true;
Deps d;
d.sched = &sched;
d.tok = &tok;
ServerOptions o;
o.max_seqs = 1;
o.queue_depth = 1;
o.retry_after_s = 7;
o.default_max_tokens = 32;
auto srv = std::make_unique<Server>(d, o);
std::atomic<bool> never{false};
std::vector<HttpResponse> live;
std::string retry;
for (int i = 0; i < 2; ++i) {
HttpRequest q;
q.method = "POST";
q.path = "/v1/completions";
q.body = R"({"prompt":"hi","stream":true})";
q.closed = [&never] { return never.load(); };
HttpResponse r = srv->router().dispatch(q);
if (r.status == 429) {
for (auto& h : r.headers) if (h.first == "Retry-After") retry = h.second;
} else {
live.push_back(std::move(r));
}
}
CHECK_EQ(retry, std::string("7"));
for (auto& r : live) if (r.on_close) r.on_close(false);
sched.join_all();
srv.reset();
}
RAD_TEST_MAIN()