/* * splinter -- the session plugin: what earlier conversations in this folder said. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * The conversation that is running is the context; this is the rest of the * folder's history. Two tools: * * session_search text in the logs of earlier sessions, with date, id and * title, so the model can quote where it found something * session_list the earlier sessions themselves (date, size, title) * * Both read the same .jsonl files the engine writes; nothing else is kept. The * running session is left out on purpose -- it is already in the prompt, and * the lcm plugin's recall covers what compaction dropped from it. * * "This folder's history" is not only splinter's. Claude Code, Hermes and * opencode keep their own sessions, and the same question -- what did we do * here? -- is answered wrongly when they are left out. This plugin does not * learn their three storage formats: the foreign plugin already knows them and * says so for the same working directory, so its tools are called through the * registry. When it is not loaded, the splinter logs are the whole answer and * the note at the end says so instead of pretending the machine is empty. */ #include #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" #include "mcp.h" #define MAX_SCAN 400 /* sessions looked at, newest first */ #define HIT_BYTES 240 /* text shown around a match */ #define TOTAL_BYTES 12000 /* the whole answer, so it stays cheap */ #define SCAN_BYTES (64 * 1024 * 1024) /* case-blind substring, like the lcm plugin's recall */ static int has_ci(const char *hay, const char *needle) { size_t n = strlen(needle), i; const char *p; for (p = hay; *p != '\0'; p++) { for (i = 0; i < n && p[i] != '\0'; i++) if (tolower((unsigned char)p[i]) != tolower((unsigned char)needle[i])) break; if (i == n) return 1; } return 0; } /* where the needle is, case-blind, or -1 */ static long find_ci(const char *hay, const char *needle) { size_t n = strlen(needle), i; const char *p; for (p = hay; *p != '\0'; p++) { for (i = 0; i < n && p[i] != '\0'; i++) if (tolower((unsigned char)p[i]) != tolower((unsigned char)needle[i])) break; if (i == n) return (long)(p - hay); } return -1; } /* the readable part around a match: control characters become spaces */ static void snippet(struct sp_buf *b, const char *text, const char *query) { long at = find_ci(text, query); size_t len = strlen(text), from = 0, n, i; if (at > 60) from = (size_t)at - 60; n = len - from; if (n > HIT_BYTES) n = HIT_BYTES; if (from > 0) sp_buf_puts(b, "..."); for (i = 0; i < n; i++) { char c = text[from + i]; if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7f) c = ' '; (void)sp_buf_add(b, &c, 1); } if (from + n < len) sp_buf_puts(b, "..."); } static void stamp_of(long mtime, char *buf, size_t cap) { struct tm tm; if (localtime_r(&(time_t){ mtime }, &tm) == NULL) { snprintf(buf, cap, "?"); return; } strftime(buf, cap, "%Y-%m-%d %H:%M", &tm); } /* * The other harnesses, through the foreign plugin's own tools. * * Why not read Claude's jsonl and the two sqlite stores here: the foreign * plugin already does, including the schemas, the sidechain filter and which * Hermes profile has a state.db. A second copy would be a second thing to keep * right, and the first store that changed shape would make the two disagree * about the same folder. * * The call is by tool name through the registry, so this works whenever the * foreign plugin is loaded and does nothing when it is not. `args` is the * foreign tool's own arguments; the answer is appended as-is, because it is * written in the same shape this plugin writes (date, source, id, role, * snippet). */ static int ask_foreign(struct sp_host *h, const char *tool, const char *args, struct sp_buf *out) { struct sp_registry *reg = sp_host_registry(h); const struct sp_tool *t; struct sp_buf tmp; int rc; if (reg == NULL || out == NULL) return 0; t = sp_registry_find(reg, tool); if (t == NULL || t->run == NULL) return 0; /* plugin not loaded: nothing to ask */ if (sp_buf_init(&tmp) != 0) return 0; rc = t->run(args, &tmp, t->ctx); if (rc == 0 && tmp.len > 0 && tmp.p != NULL) { size_t before = out->len; (void)sp_buf_add(out, tmp.p, tmp.len); if (out->len > before && out->p[out->len - 1] != '\n') (void)sp_buf_puts(out, "\n"); } sp_buf_free(&tmp); return rc == 0 ? 1 : 0; } /* JSON string literal for one argument, escaped */ static void arg_str(struct sp_buf *b, const char *key, const char *val) { (void)sp_buf_puts(b, ",\""); (void)sp_buf_puts(b, key); (void)sp_buf_puts(b, "\":\""); (void)sp_json_escape(b, val); (void)sp_buf_puts(b, "\""); } /* the same for an integer argument; 0 means "not given" for both callers */ static void arg_int(struct sp_buf *b, const char *key, int val) { char num[32]; if (val <= 0) return; snprintf(num, sizeof(num), ",\"%s\":%d", key, val); (void)sp_buf_puts(b, num); } static int session_search(const char *args, struct sp_buf *out, void *ctx) { struct sp_host *h = ctx; const char *dir = sp_host_session_dir(h); const char *cur = sp_host_session_id(h); struct sp_session_brief *v; char query[200] = "", only[64] = "", cwd[512]; int limit = 10, days = 0, nv, i, hits = 0, scanned = 0; int fhits = 0; long now = (long)time(NULL), budget = SCAN_BYTES; mcp_json *a; if (dir == NULL || *dir == '\0') return sp_buf_puts(out, "error: no session directory"); /* Only sessions that ran in this working directory: "what did we do * last?" is about this folder, not about every conversation ever. */ if (getcwd(cwd, sizeof(cwd)) == NULL) return sp_buf_puts(out, "error: cannot determine the working " "directory"); if (args != NULL && args[0] == '{' && (a = mcp_parse(args)) != NULL) { const char *q = mcp_arg_string(a, "query"); const char *s = mcp_arg_string(a, "session"); if (q != NULL) snprintf(query, sizeof(query), "%s", q); if (s != NULL) snprintf(only, sizeof(only), "%s", s); limit = mcp_arg_int(a, "limit", 10); days = mcp_arg_int(a, "days", 0); mcp_json_free(a); } if (query[0] == '\0') return sp_buf_puts(out, "error: 'query' is required"); if (limit < 1 || limit > 50) limit = 10; v = calloc(MAX_SCAN, sizeof(*v)); if (v == NULL) return sp_buf_puts(out, "error: out of memory"); nv = sp_session_list(dir, v, MAX_SCAN); if (nv < 0) { free(v); return sp_buf_puts(out, "error: cannot read the session directory"); } for (i = 0; i < nv && hits < limit; i++) { char path[800], *line = NULL; size_t cap = 0; ssize_t len; FILE *f; if (only[0] == '\0' && cur != NULL && strcmp(v[i].id, cur) == 0) continue; /* running session: context, not history */ if (only[0] != '\0' && strcmp(v[i].id, only) != 0) continue; { char scwd[512]; if (sp_session_cwd(dir, v[i].id, scwd, sizeof(scwd)) != 0 || strcmp(scwd, cwd) != 0) continue; } if (days > 0 && v[i].mtime < now - (long)days * 86400) continue; if (budget <= 0) break; snprintf(path, sizeof(path), "%s/%s.jsonl", dir, v[i].id); f = fopen(path, "r"); if (f == NULL) continue; scanned++; while (hits < limit && (len = getline(&line, &cap, f)) != -1) { mcp_json *m; const char *role, *text; char stamp[32], title[96]; budget -= (long)len; if (!has_ci(line, query)) continue; m = mcp_parse(line); if (m == NULL) continue; role = mcp_arg_string(m, "role"); text = mcp_arg_string(m, "text"); if (text == NULL || !has_ci(text, query)) { mcp_json_free(m); continue; } stamp_of(v[i].mtime, stamp, sizeof(stamp)); snprintf(title, sizeof(title), "%s", v[i].title[0] != '\0' ? v[i].title : "(untitled)"); sp_buf_printf(out, "%s %s \"%s\" %s: ", stamp, v[i].id, title, role != NULL ? role : "?"); snippet(out, text, query); sp_buf_puts(out, "\n"); hits++; mcp_json_free(m); if (out->len >= TOTAL_BYTES) break; } free(line); fclose(f); } free(v); /* * The rest of this folder's history: the same three stores the foreign * plugin reads. Only when the answer still has room -- a search that * already filled its limit has nothing to add, and the caller asked for * `limit` rows, not for a report about every harness. * * `session` is not passed on: it names a splinter session id, which the * other harnesses cannot have. A search restricted to one id stays a * pure splinter search instead of listing foreign hits under it. */ if (only[0] == '\0' && hits < limit && out->len < TOTAL_BYTES) { struct sp_buf fa; char num[64]; if (sp_buf_init(&fa) == 0) { (void)sp_buf_puts(&fa, "{\"query\":\""); (void)sp_json_escape(&fa, query); (void)sp_buf_puts(&fa, "\""); snprintf(num, sizeof(num), ",\"limit\":%d", limit - hits); (void)sp_buf_puts(&fa, num); arg_int(&fa, "days", days); (void)sp_buf_puts(&fa, "}"); fhits = ask_foreign(h, "foreign_search", fa.p, out) ? 1 : 0; sp_buf_free(&fa); } } if (hits == 0 && fhits == 0) sp_buf_printf(out, "no message in %d earlier session%s contains " "'%s'", scanned, scanned == 1 ? "" : "s", query); else if (hits >= limit || out->len >= TOTAL_BYTES) sp_buf_puts(out, "... (limit reached; narrow the query or raise " "limit)\n"); return 0; } static int session_list_tool(const char *args, struct sp_buf *out, void *ctx) { struct sp_host *h = ctx; const char *dir = sp_host_session_dir(h); const char *cur = sp_host_session_id(h); struct sp_session_brief *v; char cwd[512], num[32]; int limit = 15, days = 0, nv, i, shown = 0; long now = (long)time(NULL); mcp_json *a; if (dir == NULL || *dir == '\0') return sp_buf_puts(out, "error: no session directory"); if (getcwd(cwd, sizeof(cwd)) == NULL) return sp_buf_puts(out, "error: cannot determine the working " "directory"); if (args != NULL && args[0] == '{' && (a = mcp_parse(args)) != NULL) { limit = mcp_arg_int(a, "limit", 15); days = mcp_arg_int(a, "days", 0); mcp_json_free(a); } if (limit < 1 || limit > 100) limit = 15; v = calloc(MAX_SCAN, sizeof(*v)); if (v == NULL) return sp_buf_puts(out, "error: out of memory"); nv = sp_session_list(dir, v, MAX_SCAN); if (nv < 0) { free(v); return sp_buf_puts(out, "error: cannot read the session directory"); } for (i = 0; i < nv && shown < limit; i++) { char stamp[32], scwd[512]; if (sp_session_cwd(dir, v[i].id, scwd, sizeof(scwd)) != 0 || strcmp(scwd, cwd) != 0) continue; if (days > 0 && v[i].mtime < now - (long)days * 86400) continue; stamp_of(v[i].mtime, stamp, sizeof(stamp)); sp_buf_printf(out, "%s %s %6.1f KiB %s%s\n", stamp, v[i].id, (double)v[i].bytes / 1024.0, v[i].title[0] != '\0' ? v[i].title : "(untitled)", (cur != NULL && strcmp(v[i].id, cur) == 0) ? " (current)" : ""); shown++; } free(v); /* * The other harnesses' sessions in the same folder, through the foreign * plugin. Same reason as in session_search: it already knows the three * stores. A cwd that cannot be read is not passed as a filter -- the * foreign side would then list every folder, which is a different * question than the one asked. */ if (shown < limit) { struct sp_buf fa; if (sp_buf_init(&fa) == 0) { (void)sp_buf_puts(&fa, "{\"limit\":"); snprintf(num, sizeof(num), "%d", limit - shown); (void)sp_buf_puts(&fa, num); arg_str(&fa, "cwd", cwd); (void)sp_buf_puts(&fa, "}"); (void)ask_foreign(h, "foreign_list", fa.p, out); sp_buf_free(&fa); } } if (shown == 0) sp_buf_puts(out, "no sessions"); else if (shown >= limit) sp_buf_puts(out, "... (limit reached)\n"); return 0; } static const struct sp_tool search_tool = { "session_search", "Search the logs of earlier sessions that ran in this working directory: " "what the user and the tools said, with date, session id and title. " "Sessions from other folders are not searched -- this answers \"what did " "we do here?\". Use it before asking the user something the fleet may " "already have answered, and when the prompt says this directory has " "earlier sessions. The running session is not searched; it is already in " "your context.", "{\"type\":\"object\",\"properties\":{" "\"query\":{\"type\":\"string\",\"description\":\"text to find " "(case-blind)\"}," "\"limit\":{\"type\":\"integer\",\"description\":\"max matches, default " "10, max 50\"}," "\"days\":{\"type\":\"integer\",\"description\":\"only sessions changed " "in the last N days\"}," "\"session\":{\"type\":\"string\",\"description\":\"only this session id " "from session_list\"}}," "\"required\":[\"query\"]}", SP_AUTH_READ, session_search, NULL, NULL }; static const struct sp_tool list_tool = { "session_list", "List the earlier sessions that ran in this working directory, newest " "first: date, id, size and title. Sessions from other folders are left " "out. Use it to see what was worked on here before, then session_search " "to read the part you need.", "{\"type\":\"object\",\"properties\":{" "\"limit\":{\"type\":\"integer\",\"description\":\"max sessions, default " "15, max 100\"}," "\"days\":{\"type\":\"integer\",\"description\":\"only sessions changed " "in the last N days\"}}}", SP_AUTH_READ, session_list_tool, NULL, NULL }; static int session_init(struct sp_host *h) { static struct sp_tool st = search_tool; static struct sp_tool lt = list_tool; st.ctx = h; lt.ctx = h; if (sp_host_add_tool(h, &st) != 0) return -1; if (sp_host_add_tool(h, <) != 0) return -1; return 0; } static const struct sp_plugin session_plugin = { SP_PLUGIN_ABI, "session", "1.0.0", session_init, NULL, NULL }; const struct sp_plugin * sp_plugin_describe(void) { return &session_plugin; }