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.
This commit is contained in:
lotherk committed 2026-11-18 06:28:23 +00:00
1 parent 0f8c5c7e96
commit ddf0ec5462
9 files changed
+128 -11

No files matched your search

+8 -4
View File
@@ -311,10 +311,14 @@ stdout as it comes.
A data object split across SSE events, a blank line between its lines and a
truncated trailing fragment are all tolerated: the parser buffers until the
object is complete and drops a fragment that never is, instead of failing a
turn that already has its answer. `test_sse` feeds recorded lines: plain text,
a tool call split across three chunks, two calls in one turn, a pretty-printed
object across events, a truncated tail, and the keepalive lines that carry
nothing.
turn that already has its answer. An error object in the stream
(`data: {"error":{"message":...,"code":...}}`) is not ignored: the message is
reported and the code becomes the reply's status, so a judge's 400 does not
turn into "stream ended with no answer" and is not retried as if it were a
broken connection. `test_sse` feeds recorded lines: plain text, a tool call
split across three chunks, two calls in one turn, a pretty-printed object
across events, a truncated tail, an in-stream 400, and the keepalive lines that
carry nothing.
A request that fails before anything was streamed is tried again three times,
five seconds apart; a broken connection, a missing status, a 5xx and a stream
+6
View File
@@ -127,6 +127,12 @@ A plugin registers tools with the same authorization classes the builtins use:
struct sp_tool t = { 0 };
t.name = "clock";
t.description = "Report the local date and time.";
/*
* parameters is a JSON schema, and its root must say "type":"object".
* A bare {} is rejected by judges that validate the tool list (Claude did,
* and the whole request failed), even when the model itself would accept it.
*/
t.params = "{\"type\":\"object\",\"properties\":{}}";
t.auth = SP_AUTH_READ;
t.run = clock_tool;
sp_host_add_tool(host, &t);
+2
View File
@@ -533,6 +533,8 @@ int sp_sse_line(struct sp_sse *, const char *line, struct sp_buf *out,
/* Turn the accumulated state into a reply. Returns 0 or -1. */
int sp_sse_finish(struct sp_sse *, struct sp_reply *out, struct sp_buf *err);
int sp_sse_done(const struct sp_sse *);
/* the code an in-stream error event carried, 0 if none */
int sp_sse_error_code(const struct sp_sse *);
int sp_sse_usage(const struct sp_sse *, long *in, long *out);
const char *sp_sse_tool_start(const struct sp_sse *, size_t index);
/* What the last line carried: SP_STREAM_THINK, SP_STREAM_TEXT, SP_STREAM_TOOL,
+7 -1
View File
@@ -541,8 +541,14 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply,
sp_reply_free(reply);
return 1;
}
/*
* 4xx is the request's answer, not a transient fault: an
* in-stream 400 (a bad tool schema at the judge) must be shown
* once, not retried three times. No status at all is a broken
* connection and is retried.
*/
if (attempt < SP_NET_RETRIES && !guard.produced &&
(sent_rc != 0 || reply->status == 0 ||
((sent_rc != 0 && reply->status < 400) ||
reply->status >= 500)) {
sp_logf("agent", "retry %d/%d in %dms: %s",
attempt + 1, SP_NET_RETRIES, retry_wait_ms(),
+2 -2
View File
@@ -2524,7 +2524,7 @@ sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg,
memset(&t, 0, sizeof(t));
t.name = "delegate_due";
t.description = "List durable delegation jobs due for retry/poll now (waiting_quota/waiting_poll/queued with run_after/next_poll_at reached). Scheduler view.";
t.params = "{}";
t.params = "{\"type\":\"object\",\"properties\":{}}";
t.auth = SP_AUTH_READ;
t.run = tool_delegate_due;
t.ctx = &ctx;
@@ -2556,7 +2556,7 @@ sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg,
memset(&t, 0, sizeof(t));
t.name = "delegate_jobs";
t.description = "List durable delegation jobs (submit/poll): id, state, delegate, exit, times. Jobs persist in the session so results can be polled later.";
t.params = "{}";
t.params = "{\"type\":\"object\",\"properties\":{}}";
t.auth = SP_AUTH_READ;
t.run = tool_delegate_jobs;
t.ctx = &ctx;
+32
View File
@@ -41,6 +41,7 @@ struct sp_sse {
long tokens_out;
long tokens_cached;
double cost;
int error_code; /* from a data: {"error":...} event */
};
#define PENDING_MAX (1u << 20) /* a huge "object" is not a kept promise */
@@ -92,6 +93,13 @@ sp_sse_usage(const struct sp_sse *s, long *in, long *out)
return (s->tokens_in > 0 || s->tokens_out > 0) ? 1 : 0;
}
/* the HTTP-ish code an error event carried, 0 if there was none */
int
sp_sse_error_code(const struct sp_sse *s)
{
return s != NULL ? s->error_code : 0;
}
int
sp_sse_done(const struct sp_sse *s)
{
@@ -227,6 +235,30 @@ sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out,
}
sp_buf_clear(&s->pending);
/*
* A router can put the upstream's failure in the stream instead of the
* HTTP status: data: {"error":{...,"message":"...","code":400}}. Ignoring
* it made the turn end with "no answer" and hid the one line that said
* why. The transport turns -1 and the code into a failed reply.
*/
{
const mcp_json *error = mcp_arg(v, "error");
if (error != NULL) {
const char *msg = mcp_arg_string(error, "message");
const mcp_json *code = mcp_arg(error, "code");
if (msg == NULL)
msg = mcp_json_string(error);
s->error_code = code != NULL ?
(int)mcp_arg_int(error, "code", 0) : 0;
sp_buf_printf(err, "sse: %s",
msg != NULL ? msg : "the endpoint sent an error");
mcp_json_free(v);
return -1;
}
}
/* llama.cpp and OpenAI both attach usage to the last chunk */
{
const mcp_json *usage = mcp_arg(v, "usage");
+19 -4
View File
@@ -564,6 +564,8 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
sp_buf_clear(&delta);
produced = sp_sse_line(sse, ln, &delta, err);
if (produced < 0) {
out->status =
sp_sse_error_code(sse);
sp_buf_free(&lbuf);
fclose(p);
(void)kill(child, SIGKILL);
@@ -607,11 +609,24 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
else if (ln[0] != '\0' && tail.len < 4096)
sp_buf_puts(&tail, ln);
sp_buf_clear(&delta);
if (sp_sse_line(sse, ln, &delta, err) > 0 && cb != NULL &&
delta.len > 0) {
int k = sp_sse_kind(sse);
{
int pr = sp_sse_line(sse, ln, &delta, err);
cb(cbctx, k < 0 ? SP_STREAM_TEXT : k, delta.p);
if (pr < 0) {
out->status = sp_sse_error_code(sse);
sp_buf_free(&lbuf);
fclose(p);
(void)kill(child, SIGKILL);
(void)waitpid(child, NULL, 0);
unlink(path);
goto out;
}
if (pr > 0 && cb != NULL && delta.len > 0) {
int k = sp_sse_kind(sse);
cb(cbctx, k < 0 ? SP_STREAM_TEXT : k,
delta.p);
}
}
}
sp_buf_free(&lbuf);
+35
View File
@@ -21,6 +21,7 @@
#include "splinter_engine.h"
#include "config.h"
#include "i18n.h"
#include "mcp.h"
static int failures;
@@ -181,6 +182,40 @@ main(void)
}
run_turn(eng);
/*
* Every tool schema must be a valid JSON schema object. A judge model
* (Claude) validates them and rejects the whole request when one has
* "parameters": {} without "type": "object" - delegate_due and
* delegate_jobs did, and three sessions ended in an empty stream.
*/
{
size_t n = 0, i;
const struct sp_tool *tools = sp_registry_tools(
sp_engine_registry(eng), &n);
CHECK(n > 0);
for (i = 0; i < n; i++) {
mcp_json *v;
const char *type;
if (tools[i].params == NULL)
continue;
v = mcp_parse(tools[i].params);
CHECK(v != NULL);
if (v == NULL) {
fprintf(stderr, "tool %s: unparseable schema\n",
tools[i].name);
continue;
}
type = mcp_arg_string(v, "type");
if (type == NULL || strcmp(type, "object") != 0)
fprintf(stderr, "tool %s: parameters.type is %s\n",
tools[i].name, type != NULL ? type : "(missing)");
CHECK(type != NULL && strcmp(type, "object") == 0);
mcp_json_free(v);
}
}
/* the /model picker's list: providers first, then pools */
{
struct sp_model_choice mv[SP_MODEL_CHOICES];
+17
View File
@@ -185,6 +185,23 @@ main(void)
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);