From a5665a4dcce3745557e1ea91d3af8d3806f37259 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 09:01:10 +0000 Subject: [PATCH] attach: a timed-out listing is empty, not the previous one The picker's two remote calls cleared their counters only when the answer arrived. A served engine that did not answer within the 5 s deadline therefore left the last answer in place, and /sessions drew another folder's - or another source's - rows as the ones that were asked for. The counters are now cleared before the request is sent. tests/test_remote_list drives the remote backend against a fake server that answers the first request of each kind and drops the second one. Without the fix it prints "the unanswered request still drew 2 row(s): erste zweite" and fails five checks; with it: ok. Verified: test_remote_list ok, make check: all tests passed. --- Makefile | 9 +- src/frontend/ui_remote.c | 15 ++- tests/test_remote_list.c | 208 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 tests/test_remote_list.c diff --git a/Makefile b/Makefile index 67abd71..aed6c1c 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \ tests/test_transport tests/test_agent tests/test_openai \ tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_corecompact tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self tests/test_fuzz \ tests/test_sessiontools tests/test_embed tests/test_foreign \ - tests/test_foreignopen + tests/test_foreignopen tests/test_remote_list RPATH = -Wl,-rpath,'$$ORIGIN' PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..' @@ -286,6 +286,13 @@ tests/test_foreign: tests/test_foreign.c $(LIB) plugins/foreign/foreign.so tests/test_foreignopen: tests/test_foreignopen.c $(LIB) plugins/foreign/foreign.so $(CC) $(CFLAGS) -o $@ tests/test_foreignopen.c -L. -lsplinter $(TEST_RPATH) $(LIBS) +# ui_remote.o is linked next to the library: the backend under test is front-end +# code (it speaks the protocol over a socket) and libsplinter has no ui_* in it. +tests/test_remote_list: tests/test_remote_list.c $(LIB) src/frontend/ui_remote.o \ + src/frontend/protocol.o + $(CC) $(CFLAGS) -o $@ tests/test_remote_list.c src/frontend/ui_remote.o \ + src/frontend/protocol.o -L. -lsplinter $(TEST_RPATH) $(LIBS) + tests/test_usage: tests/test_usage.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_usage.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_jobs: tests/test_jobs.c $(LIB) $(PLUGIN) diff --git a/src/frontend/ui_remote.c b/src/frontend/ui_remote.c index cff2592..be443e5 100644 --- a/src/frontend/ui_remote.c +++ b/src/frontend/ui_remote.c @@ -844,9 +844,16 @@ rem_sessions(void *ctx, void (*cb)(void *, const char *, const char *, int, long deadline; int i; + /* + * Cleared before the request, not when the answer arrives: a server that + * times out would otherwise leave the previous listing in place and the + * picker would draw another folder's (or another source's) sessions as + * the ones that were asked for. + */ + r->nsess = 0; + r->got_sessions = 0; if (r->fd < 0 || send_cmd(r, "sessions", NULL, NULL) != 0) return 0; - r->got_sessions = 0; deadline = now_ms() + 5000; while (!r->got_sessions && now_ms() < deadline) { int rc = next_line(r, 5000); @@ -869,10 +876,11 @@ rem_source_sessions(void *ctx, const char *source, void (*cb)(void *, long deadline; int i; + r->nsess = 0; + r->got_sessions = 0; if (r->fd < 0 || send_cmd(r, "sessions", "source", source != NULL ? source : "") != 0) return 0; - r->got_sessions = 0; deadline = now_ms() + 5000; while (!r->got_sessions && now_ms() < deadline) { int rc = next_line(r, 5000); @@ -894,9 +902,10 @@ rem_sources(void *ctx, void (*cb)(void *, const char *, int), void *cbctx) long deadline; int i; + r->nsrc = 0; + r->got_sources = 0; if (r->fd < 0 || send_cmd(r, "sources", NULL, NULL) != 0) return 0; - r->got_sources = 0; 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 new file mode 100644 index 0000000..bb35c0b --- /dev/null +++ b/tests/test_remote_list.c @@ -0,0 +1,208 @@ +/* + * splinter -- the remote backend's listings: a late answer is not a row. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * What it guards: ui_remote.c caches the last listing it received (the event + * carries the rows, and the picker's callback loop runs after the answer). The + * counters used to be cleared when the NEXT answer arrived, so a server that + * did not answer at all left the previous listing in place - the picker drew + * one source's sessions under another source's name. Found 2026-11-22. + * + * The fake server speaks the real greeting and then answers exactly one + * "sessions" request; the second one is dropped on purpose. + */ +#include +#include +#include +#include +#include +#include +#include + +#include "splinter.h" +#include "ui_backend.h" + +static int failures; + +#define CHECK(c) do { \ + if (!(c)) { \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ + failures++; \ + } \ +} while (0) + +struct seen { + char id[64][96]; + int n; +}; + +static void +count_row(void *ctx, const char *id, const char *title, int current, long mtime) +{ + struct seen *s = ctx; + + (void)title; + (void)current; + (void)mtime; + if (s->n < 64) + snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", id); +} + +/* the source rows come through a different callback shape */ +static void +collect_source(void *ctx, const char *name, int count) +{ + struct seen *s = ctx; + + (void)count; + if (s->n < 64) + snprintf(s->id[s->n++], sizeof(s->id[0]), "%s", name); +} + +/* + * 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". + */ +static void +serve(int fd, const char *mark) +{ + FILE *f, *m; + char line[1024]; + int answered_sessions = 0, answered_sources = 0; + + f = fdopen(fd, "r+"); + if (f == NULL) + _exit(1); + m = fopen(mark, "a"); + fprintf(f, "{\"ev\":\"ready\",\"protocol\":2,\"version\":\"test\"}\n"); + fprintf(f, "{\"ev\":\"state\",\"session\":\"s-1\",\"dir\":\"/tmp\"," + "\"model\":\"m\",\"agents\":[]}\n"); + fprintf(f, "{\"ev\":\"history\",\"items\":[]}\n"); + fprintf(f, "{\"ev\":\"commands\",\"names\":[]}\n"); + fflush(f); + while (fgets(line, sizeof(line), f) != NULL) { + if (strstr(line, "\"cmd\":\"sessions\"") != NULL) { + if (answered_sessions++) + continue; /* the second one is never answered */ + fprintf(f, "{\"ev\":\"sessions\",\"items\":[" + "{\"id\":\"erste\",\"title\":\"erste\",\"mtime\":1}," + "{\"id\":\"zweite\",\"title\":\"zweite\",\"mtime\":2}]}\n"); + if (m != NULL) + fputs("sessions\n", m); + } else if (strstr(line, "\"cmd\":\"sources\"") != NULL) { + if (answered_sources++) + continue; + fprintf(f, "{\"ev\":\"sources\",\"items\":[" + "{\"name\":\"alpha\",\"count\":3}," + "{\"name\":\"beta\",\"count\":4}]}\n"); + if (m != NULL) + fputs("sources\n", m); + } else { + continue; + } + fflush(f); + if (m != NULL) + fflush(m); + } + /* keep the socket open: a closed one is a different failure */ + for (;;) + pause(); +} + +int +main(void) +{ + char tmpl[] = "/tmp/splinter-remlist-XXXXXX"; + char *dir = mkdtemp(tmpl); + char path[100], mark[200]; + struct sockaddr_un addr; + struct seen s1, s2; + const struct ui_backend *be; + struct sp_buf err; + pid_t child; + int ls, fd, i; + int n1, n2; + + if (dir == NULL) { + perror("mkdtemp"); + return 1; + } + snprintf(path, sizeof(path), "%s/sock", dir); + snprintf(mark, sizeof(mark), "%s/mark", dir); + ls = socket(AF_UNIX, SOCK_STREAM, 0); + if (ls < 0) { + perror("socket"); + return 1; + } + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path); + if (bind(ls, (struct sockaddr *)&addr, sizeof(addr)) != 0 || + listen(ls, 4) != 0) { + perror("bind/listen"); + return 1; + } + child = fork(); + if (child == 0) { + fd = accept(ls, NULL, NULL); + close(ls); + if (fd < 0) + _exit(1); + serve(fd, mark); + _exit(0); + } + close(ls); + sp_buf_init(&err); + be = sp_ui_backend_remote(path, &err); + if (be == NULL) { + fprintf(stderr, "FAIL attach: %s\n", + err.p != NULL ? err.p : "(no reason given)"); + kill(child, SIGKILL); + waitpid(child, NULL, 0); + return 1; + } + + /* the first listing is the one the server really sent */ + memset(&s1, 0, sizeof(s1)); + n1 = be->source_sessions(be->ctx, "splinter", count_row, &s1); + CHECK(n1 == 2); + CHECK(s1.n == 2 && strcmp(s1.id[0], "erste") == 0); + + /* and now a second request the server does not answer */ + memset(&s2, 0, sizeof(s2)); + n2 = be->source_sessions(be->ctx, "claude", count_row, &s2); + if (n2 != 0) { + fprintf(stderr, "the unanswered request still drew %d row(s):", n2); + for (i = 0; i < s2.n; i++) + fprintf(stderr, " %s", s2.id[i]); + fprintf(stderr, "\n"); + } + CHECK(n2 == 0); + CHECK(s2.n == 0); + + /* the source list has the same shape of bug: one answered, one dropped */ + { + int nsrc; + + nsrc = be->sources(be->ctx, collect_source, &s2); + CHECK(nsrc == 2); + CHECK(s2.n == 2 && strcmp(s2.id[0], "alpha") == 0); + memset(&s2, 0, sizeof(s2)); + nsrc = be->sources(be->ctx, collect_source, &s2); + CHECK(nsrc == 0); + CHECK(s2.n == 0); + } + + be->close(be->ctx); + kill(child, SIGKILL); + waitpid(child, NULL, 0); + sp_buf_free(&err); + unlink(path); + rmdir(dir); + if (failures == 0) + printf("test_remote_list: ok\n"); + return failures != 0; +}