From 3b0de63e0611ff196a443fc7c85cdec3f0c34c7f Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 12:54:36 +0000 Subject: [PATCH] protocol: the model list carries its tag too The listings got a request tag so a late answer could not be read as the next request's; `models` was left out. A probe that outlived the client's 15 s timeout stayed in the socket and the following plain `models` read it as its own answer - /model then drew the endpoint's models where the configuration was asked for, and the picker's second request drew a stale list. Test: tests/test_remote_list.c, fake server answers the first models request twice and never the second. Before the fix the second call returned 2 rows (FAIL nm == 0), after it 0. Full suite green, ASan clean on the 46 test binaries. --- docs/PROTOCOL.md | 7 ++++++- src/frontend/protocol.c | 5 +++-- src/frontend/ui_remote.c | 28 ++++++++++++++++++++++--- tests/test_remote_list.c | 45 +++++++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index a3ffce0..fd33d2c 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -152,7 +152,7 @@ out — this is what the person and the model said. ### `"tag"`: which answer belongs to which request A request may carry `"tag":N` (any positive number). The server echoes it in -the event that answers it — `sessions`, `sources` and the other listings — and +the event that answers it — `sessions`, `sources`, `models` — and the client drops an event whose tag is not the one it is waiting for. Without it a listing was matched by kind alone, so a server slower than the client's 5 s timeout left its answer in the socket and the next request of that kind @@ -213,6 +213,11 @@ configuration (providers, then pools), so it answers at once. With is a network call and may take a few seconds. `/model ` through `input` does the switch and stores the choice in the session. +The answer carries the request's `tag` like the other listings, so a probe that +took longer than the client's timeout cannot be read as the answer to the next +`models` request: `/model` would then draw the endpoint's models where the +configuration was asked for. + ### `{"cmd":"ping"}` / `{"cmd":"quit"}` `pong`; and a clean shutdown: `quit` event, server closes the socket and exits diff --git a/src/frontend/protocol.c b/src/frontend/protocol.c index d51a878..bf276ee 100644 --- a/src/frontend/protocol.c +++ b/src/frontend/protocol.c @@ -585,7 +585,7 @@ send_sources(struct client *c, struct proto *p, const mcp_json *req) * the picker asks for the configured list first and this after it is visible. */ static int -send_models(struct client *c, struct proto *p, int probe) +send_models(struct client *c, struct proto *p, const mcp_json *req, int probe) { struct sp_model_choice v[SP_MODEL_CHOICES]; size_t n, i; @@ -599,6 +599,7 @@ send_models(struct client *c, struct proto *p, int probe) if (sp_buf_init(&b) != 0) return -1; ev_start(&b, "models"); + ev_tag(&b, req); sp_buf_puts(&b, ",\"items\":["); for (i = 0; i < n; i++) { if (i > 0) @@ -824,7 +825,7 @@ handle_line(struct client *c, struct proto *p, const char *line) } else if (strcmp(cmd, "sources") == 0) { (void)send_sources(c, p, v); } else if (strcmp(cmd, "models") == 0) { - (void)send_models(c, p, mcp_arg_bool(v, "probe", 0)); + (void)send_models(c, p, v, mcp_arg_bool(v, "probe", 0)); } else if (strcmp(cmd, "open") == 0) { (void)proto_open(c, p, mcp_arg_string(v, "session"), mcp_arg_bool(v, "new", 0)); diff --git a/src/frontend/ui_remote.c b/src/frontend/ui_remote.c index 7b2d7a0..eb72d69 100644 --- a/src/frontend/ui_remote.c +++ b/src/frontend/ui_remote.c @@ -76,6 +76,7 @@ struct rem { int got_sessions; long want_sessions; /* the tag of the listing we wait for */ long want_sources; + long want_models; /* the tag of the model list we wait for */ struct rem_source src[REM_MAX_SOURCES]; int nsrc; int got_sources; @@ -821,13 +822,28 @@ rem_models_probe(void *ctx, /* the server reads "probe" with mcp_arg_bool: a JSON true, not "true" */ if (r->fd < 0) return 0; + /* + * Tagged like the listings: a probe that outlived its timeout would + * otherwise be read as the answer to the next /model request, and the + * picker would draw the endpoint's models under the configured list. + */ if (probe) { - static const char cmd[] = "{\"cmd\":\"models\",\"probe\":true}\n"; + struct sp_buf b; - if (send_all(r->fd, cmd, sizeof(cmd) - 1) != 0) + r->tag++; + if (sp_buf_init(&b) != 0) return 0; - } else if (send_cmd(r, "models", NULL, NULL) != 0) + sp_buf_printf(&b, "{\"cmd\":\"models\",\"probe\":true," + "\"tag\":%ld}\n", r->tag); + if (send_all(r->fd, b.p, b.len) != 0) { + sp_buf_free(&b); + return 0; + } + sp_buf_free(&b); + } else if (send_tagged(r, "models", NULL, NULL) != 0) { return 0; + } + r->want_models = r->tag; deadline = now_ms() + (probe ? 15000 : 5000); while (now_ms() < deadline) { mcp_json *v; @@ -838,6 +854,12 @@ rem_models_probe(void *ctx, break; v = mcp_parse(r->line.p); ev = v != NULL ? mcp_arg_string(v, "ev") : NULL; + if (ev != NULL && strcmp(ev, "models") == 0 && + !tag_matches(v, r->want_models)) { + /* an answer to an earlier request: not this one's */ + mcp_json_free(v); + continue; + } if (ev != NULL && strcmp(ev, "models") == 0) { const mcp_json *items = mcp_arg(v, "items"); size_t i, len = mcp_array_len(items); diff --git a/tests/test_remote_list.c b/tests/test_remote_list.c index 8bb8196..d6150ce 100644 --- a/tests/test_remote_list.c +++ b/tests/test_remote_list.c @@ -50,6 +50,18 @@ count_row(void *ctx, const char *id, const char *title, int current, long mtime) snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", id); } +/* model rows: selector, label, current */ +static void +collect_model(void *ctx, const char *sel, const char *label, int current) +{ + struct seen *s = ctx; + + (void)label; + (void)current; + if (s->n < 64) + snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", sel); +} + /* the source rows come through a different callback shape */ static void collect_source(void *ctx, const char *name, int count) @@ -86,7 +98,7 @@ serve(int fd, const char *mark) { FILE *f, *m; char line[1024]; - int answered_sessions = 0, answered_sources = 0; + int answered_sessions = 0, answered_sources = 0, answered_models = 0; f = fdopen(fd, "r+"); if (f == NULL) @@ -122,6 +134,19 @@ serve(int fd, const char *mark) "{\"name\":\"beta\",\"count\":4}]}\n", t); if (m != NULL) fputs("sources\n", m); + } else if (strstr(line, "\"cmd\":\"models\"") != NULL) { + long t = tag_of(line); + + if (answered_models++) + continue; + fprintf(f, "{\"ev\":\"models\",\"tag\":%ld,\"items\":[" + "{\"sel\":\"m1\",\"label\":\"m1\",\"current\":false}," + "{\"sel\":\"m2\",\"label\":\"m2\",\"current\":false}]}\n", t); + fprintf(f, "{\"ev\":\"models\",\"tag\":%ld,\"items\":[" + "{\"sel\":\"m1\",\"label\":\"m1\",\"current\":false}," + "{\"sel\":\"m2\",\"label\":\"m2\",\"current\":false}]}\n", t); + if (m != NULL) + fputs("models\n", m); } else { continue; } @@ -218,6 +243,24 @@ main(void) CHECK(s2.n == 0); } + /* + * And models: the picker's list has the same shape. The duplicate answer + * must not be read as the answer to the next request, or /model draws a + * stale list (or the probe's rows under the plain request's name). + */ + { + int nm; + + memset(&s2, 0, sizeof(s2)); + nm = be->models(be->ctx, collect_model, &s2); + CHECK(nm == 2); + CHECK(s2.n == 2 && strcmp(s2.id[0], "m1") == 0); + memset(&s2, 0, sizeof(s2)); + nm = be->models(be->ctx, collect_model, &s2); + CHECK(nm == 0); + CHECK(s2.n == 0); + } + be->close(be->ctx); kill(child, SIGKILL); waitpid(child, NULL, 0);