From 15d6eb188506f7258bfe436f410c478e4ed25a73 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 13:20:10 +0000 Subject: [PATCH] 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. --- src/backend/sse.c | 41 +++++++++++++++++++++++++++++++++++++---- tests/test_sse.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/src/backend/sse.c b/src/backend/sse.c index c01f951..ffdcfc0 100644 --- a/src/backend/sse.c +++ b/src/backend/sse.c @@ -134,6 +134,27 @@ sp_sse_kind(const struct sp_sse *s) return -1; } +/* + * Does this call already hold a complete argument object? Used to tell "the + * next piece of the last call" from "a call of its own" when the stream + * carries no index: OpenAI sends an index on every fragment, so an index-less + * fragment comes from a bridge that replays a non-streaming message, and that + * bridge may replay one whole call per delta. + */ +static int +slot_complete(const struct sse_call *c) +{ + mcp_json *v; + + if (c == NULL || c->args.p == NULL || c->args.p[0] != '{') + return 0; + v = mcp_parse_exact(c->args.p); + if (v == NULL) + return 0; + mcp_json_free(v); + return 1; +} + static struct sse_call * call_slot(struct sp_sse *s, size_t index) { @@ -379,14 +400,26 @@ sp_sse_line(struct sp_sse *s, const char *line, struct sp_buf *out, * A bridge that replays a completed non-streaming * message in one delta has no index on its calls. * Several entries in such a delta are distinct - * calls, not fragments of the last one; a lone - * entry is the next piece of the last call. + * calls, not fragments of the last one. */ if (n > 1) index = (int)i; + else if (s->ncalls == 0) + index = 0; + /* + * A lone entry is the next piece of the last + * call - unless that call already holds a + * complete argument object, which makes this one + * the start of a new call. Gluing them together + * is what turned three commands into one + * unparseable string ("malformed tool + * arguments") whenever the bridge sent a whole + * call per delta. + */ + else if (slot_complete(&s->calls[s->ncalls - 1])) + index = (int)s->ncalls; else - index = (int)(s->ncalls == 0 ? 0 : - s->ncalls - 1); + index = (int)(s->ncalls - 1); } slot = call_slot(s, (size_t)index); if (slot == NULL) { diff --git a/tests/test_sse.c b/tests/test_sse.c index 30c5463..d610291 100644 --- a/tests/test_sse.c +++ b/tests/test_sse.c @@ -145,6 +145,40 @@ main(void) 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);