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

+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);