Files
splinter/tests/test_sse.c
T
lotherk 15d6eb1885 sse: a whole tool call per delta is a call of its own, not a fragment
A bridge that replays a non-streaming message may send one complete call per
delta and no index with it. call_slot() treated every index-less entry as the
next piece of the last call, so three finished argument objects were glued
into one string and the round died with "agent: model sent malformed tool
arguments" - the operator saw that on olivia in telegram, often.

An index-less lone entry now starts a new call when the last slot already
holds a complete argument object. Fragment-by-fragment streaming keeps its
behaviour: a partial object is not complete, so it still lands in the slot it
belongs to.

Test: tests/test_sse.c feeds exactly the three-delta shape from the logs and
expects three calls with their own arguments; without the fix it is 1 call
and the test fails. The indexed two-call case and a call split over three
deltas both still pass. make check exit 0, check_asan.sh 45/45 clean.
2026-11-22 13:20:10 +00:00

307 lines
11 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);
/*
* ---- a bridge that replays a whole non-stream message in one delta ----
*
* Non-streaming tool_calls carry no index. Several entries in one delta are
* then distinct calls, not pieces of the same one: junction used to emit
* exactly that and splinter merged four calls into one whose arguments were
* several JSON objects ("{}{...}{...}"). A lone index-less entry stays the
* next fragment of the last call, which the piecewise test above covers.
*/
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":["
"{\"id\":\"c0\",\"function\":{\"name\":\"a\",\"arguments\":\"{}\"}},"
"{\"id\":\"c1\",\"function\":{\"name\":\"b\",\"arguments\":\"{}\"}},"
"{\"id\":\"c2\",\"function\":{\"name\":\"c\",\"arguments\":\"{}\"}}"
"]}}]}\n", &out, &err) > 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 3);
CHECK(strcmp(r.calls[0].name, "a") == 0);
CHECK(strcmp(r.calls[1].name, "b") == 0);
CHECK(strcmp(r.calls[2].name, "c") == 0);
CHECK(strcmp(r.calls[0].args, "{}") == 0);
CHECK(strcmp(r.calls[1].args, "{}") == 0);
CHECK(strcmp(r.calls[2].args, "{}") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/*
* ---- the same bridge, one finished call per delta, still no index ----
*
* This is what the operator saw on olivia: "run: {command: ls A}{command:
* ls B}{command: ls C}". Three complete argument objects arrived in three
* deltas, none carried an index, so each was glued onto the last slot and
* the round failed as "malformed tool arguments". A fragment that starts a
* new object after the slot already holds a complete one is a new call.
*/
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":["
"{\"function\":{\"name\":\"run\",\"arguments\":"
"\"{\\\"command\\\":\\\"ls A\\\"}\"}}]}}]}\n", &out, &err) > 0);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":["
"{\"function\":{\"name\":\"run\",\"arguments\":"
"\"{\\\"command\\\":\\\"ls B\\\"}\"}}]}}]}\n", &out, &err) >= 0);
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":["
"{\"function\":{\"name\":\"run\",\"arguments\":"
"\"{\\\"command\\\":\\\"ls C\\\"}\"}}]}}]}\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 == 3);
if (r.ncalls == 3) {
CHECK(strcmp(r.calls[0].args, "{\"command\":\"ls A\"}") == 0);
CHECK(strcmp(r.calls[1].args, "{\"command\":\"ls B\"}") == 0);
CHECK(strcmp(r.calls[2].args, "{\"command\":\"ls C\"}") == 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);
/*
* ---- one broken '{' line must not swallow the rest of the stream ----
* A later line that is a whole object by itself is a new event.
*/
s = sp_sse_new();
sp_buf_clear(&out);
sp_buf_clear(&err);
CHECK(sp_sse_line(s, "data: {kaputt\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "\n", &out, &err) == 0);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"a\"}}]}\n", &out, &err) == 1);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"content\":"
"\"b\"}}]}\n", &out, &err) == 1);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.content != NULL && strcmp(r.content, "ab") == 0);
sp_reply_free(&r);
sp_sse_free(s);
/* ---- calls without an id get one each, not all "call-0" ---- */
s = sp_sse_new();
sp_buf_clear(&out);
CHECK(sp_sse_line(s, "data: {\"choices\":[{\"delta\":{\"tool_calls\":["
"{\"index\":0,\"function\":{\"name\":\"a\",\"arguments\":\"{}\"}},"
"{\"index\":1,\"function\":{\"name\":\"b\",\"arguments\":\"{}\"}}"
"]}}]}\n", &out, &err) > 0);
memset(&r, 0, sizeof(r));
CHECK(sp_sse_finish(s, &r, &err) == 0);
CHECK(r.ncalls == 2 && strcmp(r.calls[0].id, "call-0") == 0 &&
strcmp(r.calls[1].id, "call-1") == 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;
}