/* * splinter -- the session plugin: earlier sessions can be listed and searched. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * What it guards: session_search finds text in an older session and names it * (date, id, title), leaves the running session out (that is context, not * history), honours limit/days/session, and session_list shows both with the * current one marked. A plugin that quietly returns the running conversation * would look like it worked while telling the model what it already knows. */ #include #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ #cond); \ failures++; \ } \ } while (0) static int contains(const char *hay, const char *needle) { return hay != NULL && strstr(hay, needle) != NULL; } /* make the session look old, so the days filter has something to drop */ static void age_session(const char *dir, const char *id, long seconds) { char path[700]; struct timespec ts[2]; snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id); ts[0].tv_sec = ts[1].tv_sec = time(NULL) - seconds; ts[0].tv_nsec = ts[1].tv_nsec = 0; (void)utimensat(AT_FDCWD, path, ts, 0); } int main(void) { char tmpl[] = "/tmp/splinter-stools-XXXXXX"; char *dir; struct sp_session_info old, cur; struct sp_buf err, out; struct sp_registry reg; struct sp_agent ag; struct sp_host *host; const struct sp_tool *search, *list; char cmd[800]; dir = mkdtemp(tmpl); CHECK(dir != NULL); if (dir == NULL) return 1; CHECK(sp_buf_init(&err) == 0); CHECK(sp_buf_init(&out) == 0); memset(®, 0, sizeof(reg)); memset(&ag, 0, sizeof(ag)); memset(&old, 0, sizeof(old)); memset(&cur, 0, sizeof(cur)); CHECK(sp_session_open(&old, dir, "sess-old", &err) == 0); sp_buf_clear(&err); CHECK(sp_session_append(&old, "user", "the OLD-NEEDLE question about basalt", &err) == 0); CHECK(sp_session_append(&old, "assistant", "the OLD-NEEDLE answer", &err) == 0); CHECK(sp_session_set_title(dir, "sess-old", "old basalt work", &err) == 0); CHECK(sp_session_open(&cur, dir, "sess-now", &err) == 0); sp_buf_clear(&err); CHECK(sp_session_append(&cur, "user", "the OLD-NEEDLE in the running session", &err) == 0); /* * A session that ran in another folder must never show up: "what did we * do last?" is about this directory, and a search that wanders into * every conversation ever answers a question nobody asked. */ { char other[800], back[512]; struct sp_session_info away; CHECK(getcwd(back, sizeof(back)) != NULL); snprintf(other, sizeof(other), "%s/other", dir); CHECK(mkdir(other, 0700) == 0); CHECK(chdir(other) == 0); memset(&away, 0, sizeof(away)); sp_buf_clear(&err); CHECK(sp_session_open(&away, dir, "sess-elsewhere", &err) == 0); sp_buf_clear(&err); CHECK(sp_session_append(&away, "user", "the OLD-NEEDLE from another folder", &err) == 0); CHECK(chdir(back) == 0); } host = sp_host_new(®, &ag, &cur); CHECK(host != NULL); sp_buf_clear(&err); CHECK(sp_plugin_load(host, "plugins/session/session.so", &err) == 0); search = sp_registry_find(®, "session_search"); list = sp_registry_find(®, "session_list"); CHECK(search != NULL); CHECK(list != NULL); if (search == NULL || list == NULL) { fprintf(stderr, "session plugin not loaded: %s\n", err.p != NULL ? err.p : "?"); sp_host_free(host); sp_registry_free(®); sp_buf_free(&err); sp_buf_free(&out); snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir); (void)system(cmd); return 1; } /* a hit in the older session, with its date, id and title */ CHECK(search->run("{\"query\":\"OLD-NEEDLE\"}", &out, search->ctx) == 0); CHECK(contains(out.p, "sess-old")); CHECK(contains(out.p, "old basalt work")); CHECK(contains(out.p, "OLD-NEEDLE")); /* the running session is context, not history; another folder is another * conversation */ CHECK(!contains(out.p, "sess-now")); CHECK(!contains(out.p, "sess-elsewhere")); sp_buf_clear(&out); /* nothing found is a sentence, not an empty answer */ CHECK(search->run("{\"query\":\"NO-SUCH-TEXT-ANYWHERE\"}", &out, search->ctx) == 0); CHECK(contains(out.p, "no message")); sp_buf_clear(&out); /* limit: the second hit is withheld and said so */ CHECK(search->run("{\"query\":\"OLD-NEEDLE\",\"limit\":1}", &out, search->ctx) == 0); CHECK(contains(out.p, "sess-old")); CHECK(contains(out.p, "limit reached")); sp_buf_clear(&out); /* an explicit id may name the running session */ CHECK(search->run("{\"query\":\"OLD-NEEDLE\",\"session\":\"sess-now\"}", &out, search->ctx) == 0); CHECK(contains(out.p, "sess-now")); CHECK(!contains(out.p, "sess-old")); sp_buf_clear(&out); /* even an explicit id cannot pull in another folder */ CHECK(search->run("{\"query\":\"OLD-NEEDLE\"," "\"session\":\"sess-elsewhere\"}", &out, search->ctx) == 0); CHECK(contains(out.p, "no message")); CHECK(!contains(out.p, "sess-elsewhere")); sp_buf_clear(&out); /* days: the old session is 40 days old, 7 days must not see it */ age_session(dir, "sess-old", 40L * 86400L); CHECK(search->run("{\"query\":\"OLD-NEEDLE\",\"days\":7}", &out, search->ctx) == 0); CHECK(contains(out.p, "no message")); sp_buf_clear(&out); /* the list shows both, newest first, current marked */ CHECK(list->run("{}", &out, list->ctx) == 0); CHECK(contains(out.p, "sess-old")); CHECK(contains(out.p, "sess-now")); CHECK(contains(out.p, "(current)")); CHECK(!contains(out.p, "sess-elsewhere")); sp_buf_clear(&out); /* a directory that is not a git project gets no task-scope paragraph */ { char back[512]; struct sp_buf note; CHECK(getcwd(back, sizeof(back)) != NULL); CHECK(chdir(dir) == 0); CHECK(sp_buf_init(¬e) == 0); CHECK(sp_task_scope_note(¬e) == 0); CHECK(note.len == 0); sp_buf_free(¬e); CHECK(chdir(back) == 0); } /* missing query is an error, not a wildcard search */ CHECK(search->run("{}", &out, search->ctx) == 0); CHECK(contains(out.p, "error: 'query' is required")); sp_buf_clear(&out); /* * The rest of this folder's history: a Claude Code session that ran * here must be found by session_search, though it is not one of our * .jsonl logs. This is the whole point of the delegation -- before it, * the same question answered "no message" while the transcript sat on * disk. * * The work directory has to be the one Claude recorded, so the search's * own getcwd() and the fixture's cwd agree. */ { char here[512], cla[700], proj[800], path2[900]; CHECK(getcwd(here, sizeof(here)) != NULL); snprintf(cla, sizeof(cla), "%s/claude-home", dir); CHECK(mkdir(cla, 0700) == 0); /* Claude Code's slug: the path with every separator as '-' */ snprintf(proj, sizeof(proj), "%s/", cla); { size_t n = strlen(proj), i; for (i = 0; here[i] != '\0' && n + 1 < sizeof(proj); i++) proj[n++] = here[i] == '/' ? '-' : here[i]; proj[n] = '\0'; } CHECK(mkdir(proj, 0700) == 0); snprintf(path2, sizeof(path2), "%s/c1a0de00-0000-0000-0000-" "000000000001.jsonl", proj); { FILE *f = fopen(path2, "w"); CHECK(f != NULL); if (f != NULL) { fprintf(f, "{\"type\":\"user\",\"cwd\":\"%s\"," "\"message\":{\"role\":\"user\",\"content\":" "\"the CLAUDE-NEEDLE fix we did in claude\"}}" "\n", here); fclose(f); } } CHECK(setenv("SPLINTER_FOREIGN_CLAUDE", cla, 1) == 0); /* * The foreign plugin is loaded onto the same host. Loading it * here, after the first search above, is also the proof that a * session plugin without it answers from its own logs only. */ sp_buf_clear(&err); CHECK(sp_plugin_load(host, "plugins/foreign/foreign.so", &err) == 0); CHECK(sp_registry_find(®, "foreign_search") != NULL); CHECK(search->run("{\"query\":\"CLAUDE-NEEDLE\"}", &out, search->ctx) == 0); /* * Not the needle itself: the "no message ... contains * 'CLAUDE-NEEDLE'" sentence carries it too, so that check * passes on the broken code. The id and the source name are * only there when a real hit was rendered. */ CHECK(contains(out.p, "c1a0de00")); CHECK(contains(out.p, "claude")); sp_buf_clear(&out); /* the list sees the same session, so both tools agree */ CHECK(list->run("{}", &out, list->ctx) == 0); CHECK(contains(out.p, "c1a0de00")); CHECK(contains(out.p, "claude")); sp_buf_clear(&out); /* a splinter-only query still answers, and does not claim more */ CHECK(search->run("{\"query\":\"OLD-NEEDLE\"}", &out, search->ctx) == 0); CHECK(contains(out.p, "sess-old")); sp_buf_clear(&out); } sp_host_free(host); sp_registry_free(®); sp_buf_free(&err); sp_buf_free(&out); snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir); (void)system(cmd); if (failures == 0) printf("test_sessiontools: ok\n"); return failures == 0 ? 0 : 1; }