Files
splinter/tests/test_sse.c
T
lotherk ddf0ec5462 tools: valid schemas; sse: surface in-stream errors
A judged request sends the whole tool list to the judge. Claude validates the
schemas and rejected the request with 400
"tools.22.function.parameters.type: Field required": delegate_due and
delegate_jobs declared "parameters": {} instead of an object schema. junction
wrapped that 400 in the stream as data: {"error":{...,"code":400}}, but the SSE
parser ignored anything that was not a choice - so the turn ended with
"stream ended with no answer" and the one line explaining it was lost. Three
sessions stopped that way at the same minute.

- delegate_due/delegate_jobs now carry {"type":"object","properties":{}}.
- test_engine walks every registered tool and fails when parameters.type is
  not "object"; this catches the next schema before a judge does.
- sp_sse_line recognises a top-level error object, reports its message and
  remembers its code; the transport puts the code in the reply status.
- a 4xx from the stream ends the turn with the real message and is not
  retried; an empty 2xx stream and a 5xx still are. 402/429/529 stay with the
  usage-limit path, which already handles them.
- test_sse feeds the junction 400 event and checks message and code.
- README and docs/PLUGINS say why a tool schema needs type:object.

make check: all tests passed.
2026-11-18 06:28:23 +00:00

212 lines
7.2 KiB
C

/*
* splinter -- SSE accumulation tests, fed with recorded lines.
*
* No socket: the parser takes one line at a time, so these are the exact lines
* an OpenAI-compatible endpoint sends, in the order it sends them.
*/
#include <stdio.h>
#include <string.h>
#include "splinter.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static int failures_now;
int
main(void)
{
struct sp_sse *s;
struct sp_buf out, err;
struct sp_reply r;
(void)failures_now;
if (sp_buf_init(&out) != 0 || sp_buf_init(&err) != 0)
return 1;
/* ---- plain text stream ---- */
s = sp_sse_new();
CHECK(s != NULL);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, ": keepalive\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"role\":"
"\"assistant\"}}]}\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"Hal\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "Hal") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_TEXT);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"lo\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "lo") == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
CHECK(sp_sse_done(s) == 1);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.content != NULL && strcmp(r.content, "Hallo") == 0);
CHECK(r.ncalls == 0);
CHECK(r.calls_json == NULL);
sp_reply_free(&r);
/* ---- reasoning is its own kind, never the answer ---- */
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{"
"\"reasoning_content\":\"hmm\"}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "hmm") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_THINK);
sp_sse_free(s);
/*
* ---- a tool call in pieces, the way OpenAI sends them ----
*
* With `"content":null` beside the call, which is the shape the provider
* actually sends. A transport that guessed "text" from the raw line put
* the tool's name into the answer - two read calls showed up as
* "<model> readread" - so the kind comes from the parser now.
*/
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":null,"
"\"tool_calls\":"
"[{\"index\":0,\"id\":\"call-a\",\"function\":{\"name\":\"echo\","
"\"arguments\":\"\"}}]}}]}\n", &out, &err) == 1);
CHECK(strcmp(out.p, "echo") == 0);
CHECK(sp_sse_kind(s) == SP_STREAM_TOOL);
CHECK(sp_sse_tool_start(s, 0) != NULL);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"function\":{\"arguments\":\"{\\\"text\\\":\"}}]}}]}\n",
&out, &err) == 0);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"function\":{\"arguments\":\"\\\"ping\\\"}\"}}]}}]}\n",
&out, &err) == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 1);
CHECK(strcmp(r.calls[0].id, "call-a") == 0);
CHECK(strcmp(r.calls[0].name, "echo") == 0);
CHECK(strcmp(r.calls[0].args, "{\"text\":\"ping\"}") == 0);
/* the reconstructed array is valid JSON text with the same values */
CHECK(r.calls_json != NULL);
CHECK(strstr(r.calls_json, "\"call-a\"") != NULL);
CHECK(strstr(r.calls_json, "\\\"text\\\":\\\"ping\\\"") != NULL);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- two calls in one turn keep their indexes apart ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":"
"[{\"index\":0,\"id\":\"c0\",\"function\":{\"name\":\"a\","
"\"arguments\":\"{}\"}},{\"index\":1,\"id\":\"c1\",\"function\":"
"{\"name\":\"b\",\"arguments\":\"{}\"}}]}}]}\n", &out, &err) == 1);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 2);
CHECK(strcmp(r.calls[0].name, "a") == 0);
CHECK(strcmp(r.calls[1].name, "b") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- usage carries the cache share of the prompt ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[],\"usage\":{\"prompt_tokens\":"
"100,\"completion_tokens\":5,\"prompt_tokens_details\":"
"{\"cached_tokens\":80}}}\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [DONE]\n", &out, &err) == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.tokens_in == 100 && r.tokens_out == 5);
CHECK(r.tokens_cached == 80);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- proxy keepalives wear a data: prefix but are not JSON ---- */
s = sp_sse_new();
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: ping\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: \n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [PING]\n", &out, &err) == 0);
CHECK(err.len == 0);
sp_sse_free(s);
/* ---- pretty-printed JSON arrives over several data: lines ---- */
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {\"choices\":\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [{\"delta\":{\"content\":\"hi\"}}]}\n", &out,
&err) == SP_STREAM_TEXT);
CHECK(strcmp(out.p, "hi") == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
sp_sse_free(s);
/*
* ---- a data object split across events, and a trailing fragment ----
*
* Real proxies put blank lines between the lines of a pretty-printed
* object and may truncate the last one. Neither is a failed turn: the
* blank line keeps the buffer, and a fragment that never completes is
* dropped at the end.
*/
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {\"choices\":\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: [{\"delta\":{\"content\":\"ok\"}}]}\n",
&out, &err) == 1);
CHECK(strcmp(out.p, "ok") == 0);
CHECK(sp_sse_line(s, "data: {kaputt\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(err.len == 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(err.len == 0);
CHECK(r.content != NULL && strcmp(r.content, "ok") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- non-data lines carry nothing and are not errors ---- */
s = sp_sse_new();
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "event: ping\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(err.len == 0);
sp_sse_free(s);
/*
* ---- an error event is not "no answer" ----
*
* junction puts an upstream 400 in the stream: data: {"error":{...}}.
* It used to be dropped, so the turn said "stream ended with no answer"
* and the real message (a tool schema the judge rejected) was lost.
*/
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {\"error\":{\"message\":\"upstream 400: "
"tools.22.function.parameters.type: Field required\","
"\"code\":400}}\n", &out, &err) == -1);
CHECK(err.p != NULL && strstr(err.p, "tools.22") != NULL);
CHECK(sp_sse_error_code(s) == 400);
sp_sse_free(s);
sp_buf_free(&out);
sp_buf_free(&err);
if (failures == 0)
printf("test_sse: ok\n");
return failures == 0 ? 0 : 1;
}