From 0fc81398dc2f1eeb802e406b8fc11fa16a6f74ab Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 12:05:25 +0000 Subject: [PATCH] protocol: a late answer cannot answer the next request The listings were matched to their request by kind alone. A server slower than the client's 5 s timeout left its answer in the socket, and the next request of the same kind read it as its own: the /sessions picker drew one source's sessions under another source's name. Seen live on 2026-11-22 against a --serve instance. A request may now carry "tag":N, the server echoes it in the event that answers it, and the client drops an event whose tag is not the one it waits for. Without a tag both sides behave as before. Test: tests/test_remote_list.c has the server echo the tag and send the first sessions answer twice. With the tag check removed it reports "the unanswered request still drew 2 row(s): erste zweite" and three FAILs, with it green. make check: all tests passed. Also in here: pick_match stops at SP_PICK_ROWS like pick_add does (the two caps are the same constant, the write was only safe because of that). --- docs/PROTOCOL.md | 10 ++++++ src/frontend/curses.c | 8 ++++- src/frontend/protocol.c | 35 ++++++++++++++---- src/frontend/ui_remote.c | 78 +++++++++++++++++++++++++++++++++++++--- tests/test_remote_list.c | 30 +++++++++++++--- 5 files changed, 145 insertions(+), 16 deletions(-) diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index b79c030..a3ffce0 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -149,6 +149,16 @@ Ask for the transcript so far: `"ev":"history"`, `"truncated":bool`, joins instead of an empty pane. The system prompt and tool observations stay 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 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 +read it as its own. A request without a tag gets an answer without one, as +before. + ### `{"cmd":"sessions"}` List the sessions in the server's session directory, newest first: diff --git a/src/frontend/curses.c b/src/frontend/curses.c index dba257c..88c6bba 100644 --- a/src/frontend/curses.c +++ b/src/frontend/curses.c @@ -2477,12 +2477,18 @@ ci_contains(const char *hay, const char *needle) return 0; } +/* + * The matching rows, as indices into the list. `match` must hold SP_PICK_ROWS + * entries: the caller declares it with that size, and pick_add() never puts + * more than that in the list - the two caps are the same constant, so the + * write below cannot run past the array. + */ static size_t pick_match(const struct pick_list *l, const char *filter, size_t *match) { size_t i, n = 0; - for (i = 0; i < l->n; i++) + for (i = 0; i < l->n && n < SP_PICK_ROWS; i++) if (ci_contains(l->v[i].sel, filter) || ci_contains(l->v[i].label, filter)) match[n++] = i; diff --git a/src/frontend/protocol.c b/src/frontend/protocol.c index 3cf718a..d51a878 100644 --- a/src/frontend/protocol.c +++ b/src/frontend/protocol.c @@ -102,6 +102,25 @@ ev_bool(struct sp_buf *b, const char *key, int v) sp_buf_printf(b, ",\"%s\":%s", key, v ? "true" : "false"); } +/* + * The tag of the request this event answers, when the client sent one. + * + * Without it a listing was matched to a request by kind alone: a client that + * gave up after its 5 s timeout left the late answer in the socket, and the + * NEXT request of the same kind read it as its own - the picker drew one + * source's sessions under another source's name (seen 2026-11-22 in a live + * --serve session). The client now numbers its requests and drops an event + * whose tag is not the one it is waiting for. + */ +static void +ev_tag(struct sp_buf *b, const mcp_json *req) +{ + long t = mcp_arg_int(req, "tag", 0); + + if (t > 0) + ev_num(b, "tag", t); +} + static int send_ready(struct client *c) { @@ -455,7 +474,7 @@ fold_pending_steer(struct client *c, struct sp_engine *eng, struct sp_buf *promp /* the sessions in this engine's directory, newest first */ static int -send_sessions(struct client *c, struct proto *p) +send_sessions(struct client *c, struct proto *p, const mcp_json *req) { struct sp_session_brief v[100]; const char *cur = sp_engine_session_id(p->eng); @@ -469,6 +488,7 @@ send_sessions(struct client *c, struct proto *p) if (sp_buf_init(&b) != 0) return -1; ev_start(&b, "sessions"); + ev_tag(&b, req); sp_buf_puts(&b, ",\"items\":["); for (i = 0; i < n; i++) { if (i > 0) @@ -511,7 +531,8 @@ srv_sess_cb(void *cbctx, const char *id, const char *title, int current, } static int -send_source_sessions(struct client *c, struct proto *p, const char *source) +send_source_sessions(struct client *c, struct proto *p, const char *source, + const mcp_json *req) { struct sp_buf b; struct srv_sess s; @@ -520,6 +541,7 @@ send_source_sessions(struct client *c, struct proto *p, const char *source) if (sp_buf_init(&b) != 0) return -1; ev_start(&b, "sessions"); + ev_tag(&b, req); sp_buf_puts(&b, ",\"items\":["); s.b = &b; s.n = 0; @@ -532,7 +554,7 @@ send_source_sessions(struct client *c, struct proto *p, const char *source) /* which sources the /sessions picker offers */ static int -send_sources(struct client *c, struct proto *p) +send_sources(struct client *c, struct proto *p, const mcp_json *req) { struct sp_session_source v[4]; struct sp_buf b; @@ -542,6 +564,7 @@ send_sources(struct client *c, struct proto *p) if (sp_buf_init(&b) != 0) return -1; ev_start(&b, "sources"); + ev_tag(&b, req); sp_buf_puts(&b, ",\"items\":["); for (i = 0; i < n; i++) { if (i > 0) @@ -795,11 +818,11 @@ handle_line(struct client *c, struct proto *p, const char *line) const char *src = mcp_arg_string(v, "source"); if (src != NULL && *src != '\0') - (void)send_source_sessions(c, p, src); + (void)send_source_sessions(c, p, src, v); else - (void)send_sessions(c, p); + (void)send_sessions(c, p, v); } else if (strcmp(cmd, "sources") == 0) { - (void)send_sources(c, p); + (void)send_sources(c, p, v); } else if (strcmp(cmd, "models") == 0) { (void)send_models(c, p, mcp_arg_bool(v, "probe", 0)); } else if (strcmp(cmd, "open") == 0) { diff --git a/src/frontend/ui_remote.c b/src/frontend/ui_remote.c index be443e5..7b2d7a0 100644 --- a/src/frontend/ui_remote.c +++ b/src/frontend/ui_remote.c @@ -74,6 +74,8 @@ struct rem { struct rem_sess sess[100]; int nsess; int got_sessions; + long want_sessions; /* the tag of the listing we wait for */ + long want_sources; struct rem_source src[REM_MAX_SOURCES]; int nsrc; int got_sources; @@ -86,6 +88,7 @@ struct rem { int a_auth; int presubmitted; /* input started the turn already */ int proto; /* the server's protocol version */ + long tag; /* the last request tag this client sent */ int got_cmds; /* the commands event arrived */ char path[700]; /* the socket, for a reconnect */ long next_try; /* epoch ms of the next reconnect attempt */ @@ -165,6 +168,36 @@ send_cmd(struct rem *r, const char *cmd, const char *key, const char *val) return rc; } +/* + * The same, with a number the answer has to carry back. + * + * The listings used to be matched to their request by kind alone. A server + * slower than the client's 5 s timeout left its answer in the socket, and the + * next request of that kind read it as its own - the picker drew one source's + * sessions under the name of another. With a tag, an answer that belongs to an + * older request is recognised and dropped instead of believed. + */ +static int +send_tagged(struct rem *r, const char *cmd, const char *key, const char *val) +{ + struct sp_buf b; + int rc; + + r->tag++; + if (sp_buf_init(&b) != 0) + return -1; + sp_buf_printf(&b, "{\"cmd\":\"%s\",\"tag\":%ld", cmd, r->tag); + if (key != NULL && val != NULL) { + sp_buf_printf(&b, ",\"%s\":\"", key); + (void)sp_json_escape(&b, val); + sp_buf_puts(&b, "\""); + } + sp_buf_puts(&b, "}\n"); + rc = send_all(r->fd, b.p, b.len); + sp_buf_free(&b); + return rc; +} + /* one non-blocking read into the line buffer; 1 data, 0 none, -1 gone */ static int fill(struct rem *r, int timeout_ms) @@ -320,6 +353,22 @@ copy_last(struct rem *r, const char *s) (void)sp_buf_puts(&r->last, s != NULL ? s : ""); } +/* + * Does this answer belong to the request we are waiting for? + * + * 'want' is the tag we sent, 0 when nothing is pending. An answer without a + * tag is taken as before (a server that does not echo one), an answer with a + * different tag is one we already gave up on: it must not be read as the + * answer to a later question. + */ +static int +tag_matches(const mcp_json *v, long want) +{ + if (mcp_arg(v, "tag") == NULL || want == 0) + return 1; + return (long)mcp_arg_int(v, "tag", -1) == want; +} + /* one protocol line into events and caches; the D_* says what it was */ static int dispatch(struct rem *r, const char *line) @@ -373,7 +422,13 @@ dispatch(struct rem *r, const char *line) } r->got_history = 1; } else if (strcmp(ev, "sources") == 0) { - const mcp_json *items = mcp_arg(v, "items"); + const mcp_json *items; + + if (!tag_matches(v, r->want_sources)) { + mcp_json_free(v); + return D_EVENT; + } + items = mcp_arg(v, "items"); size_t i, n = items != NULL ? mcp_array_len(items) : 0; r->nsrc = 0; @@ -391,7 +446,13 @@ dispatch(struct rem *r, const char *line) } r->got_sources = 1; } else if (strcmp(ev, "sessions") == 0) { - const mcp_json *items = mcp_arg(v, "items"); + const mcp_json *items; + + if (!tag_matches(v, r->want_sessions)) { + mcp_json_free(v); + return D_EVENT; + } + items = mcp_arg(v, "items"); size_t i, n = items != NULL ? mcp_array_len(items) : 0; r->nsess = 0; @@ -852,8 +913,10 @@ rem_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int, */ r->nsess = 0; r->got_sessions = 0; - if (r->fd < 0 || send_cmd(r, "sessions", NULL, NULL) != 0) + r->want_sessions = 0; + if (r->fd < 0 || send_tagged(r, "sessions", NULL, NULL) != 0) return 0; + r->want_sessions = r->tag; deadline = now_ms() + 5000; while (!r->got_sessions && now_ms() < deadline) { int rc = next_line(r, 5000); @@ -878,9 +941,12 @@ rem_source_sessions(void *ctx, const char *source, void (*cb)(void *, r->nsess = 0; r->got_sessions = 0; + r->want_sessions = 0; if (r->fd < 0 || - send_cmd(r, "sessions", "source", source != NULL ? source : "") != 0) + send_tagged(r, "sessions", "source", + source != NULL ? source : "") != 0) return 0; + r->want_sessions = r->tag; deadline = now_ms() + 5000; while (!r->got_sessions && now_ms() < deadline) { int rc = next_line(r, 5000); @@ -904,8 +970,10 @@ rem_sources(void *ctx, void (*cb)(void *, const char *, int), void *cbctx) r->nsrc = 0; r->got_sources = 0; - if (r->fd < 0 || send_cmd(r, "sources", NULL, NULL) != 0) + r->want_sources = 0; + if (r->fd < 0 || send_tagged(r, "sources", NULL, NULL) != 0) return 0; + r->want_sources = r->tag; deadline = now_ms() + 5000; while (!r->got_sources && now_ms() < deadline) { int rc = next_line(r, 5000); diff --git a/tests/test_remote_list.c b/tests/test_remote_list.c index bb35c0b..8bb8196 100644 --- a/tests/test_remote_list.c +++ b/tests/test_remote_list.c @@ -65,7 +65,22 @@ collect_source(void *ctx, const char *name, int count) * A server that greets, answers the first request of each kind and then goes * quiet. Which requests it answered is appended to `mark`, so a failing run * can tell "never asked" from "asked and ignored". + * + * The first "sessions" answer is sent TWICE. A client that stops reading at the + * first event leaves the second one in the socket, where the next request of + * the same kind reads it as its own answer - the rows of the old request drawn + * under the new one's name. That is a different failure from "no answer at + * all", and only the duplicate catches it. */ +/* the request's tag, or 0 when it has none: echoed back like the real server */ +static long +tag_of(const char *line) +{ + const char *p = strstr(line, "\"tag\":"); + + return p != NULL ? strtol(p + 6, NULL, 10) : 0; +} + static void serve(int fd, const char *mark) { @@ -85,19 +100,26 @@ serve(int fd, const char *mark) fflush(f); while (fgets(line, sizeof(line), f) != NULL) { if (strstr(line, "\"cmd\":\"sessions\"") != NULL) { + long t = tag_of(line); + if (answered_sessions++) continue; /* the second one is never answered */ - fprintf(f, "{\"ev\":\"sessions\",\"items\":[" + fprintf(f, "{\"ev\":\"sessions\",\"tag\":%ld,\"items\":[" "{\"id\":\"erste\",\"title\":\"erste\",\"mtime\":1}," - "{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n"); + "{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n", t); + fprintf(f, "{\"ev\":\"sessions\",\"tag\":%ld,\"items\":[" + "{\"id\":\"erste\",\"title\":\"erste\",\"mtime\":1}," + "{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n", t); if (m != NULL) fputs("sessions\n", m); } else if (strstr(line, "\"cmd\":\"sources\"") != NULL) { + long t = tag_of(line); + if (answered_sources++) continue; - fprintf(f, "{\"ev\":\"sources\",\"items\":[" + fprintf(f, "{\"ev\":\"sources\",\"tag\":%ld,\"items\":[" "{\"name\":\"alpha\",\"count\":3}," - "{\"name\":\"beta\",\"count\":4}]}\n"); + "{\"name\":\"beta\",\"count\":4}]}\n", t); if (m != NULL) fputs("sources\n", m); } else {