/* * 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. */ #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); } 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; 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); if (hits == 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]; 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); 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; }