session: a plugin that reads earlier sessions, and a prompt that points at them
session_search walks the logs of earlier sessions in this folder newest first
(query, limit, days, session) and answers with date, session id, title and a
snippet around the match; session_list shows the sessions themselves. The
running session is left out -- that is context, and lcm's recall covers what
compaction dropped -- unless a call names it with session=.
The system prompt now carries two things. When the working directory has
earlier sessions, a paragraph says how many, the newest, and that
session_search/session_list read them, so the model looks before asking the
user something that was already answered. And the default prompt tells it to
use the mcp_* tools for tasks, memory, skills and knowledge bases actively, not
only when asked.
test_sessiontools drives both tools (hit, no hit, limit, days, explicit id,
list with the current session marked); test_engine checks the prompt paragraph
and the default text. make check: all tests passed.
make check-asan: all 41 test binaries clean.
The user's ~/.splinter/splinter.conf gained "plugin session { }" (backup:
splinter.conf.bak-session).
This commit is contained in:
1 parent
f07128cf9b
commit
11d119be04
11 files changed
+649
-2
No files matched your search
@@ -44,10 +44,12 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/a2a/a2a.so \
|
||||
plugins/junction/junction.so \
|
||||
plugins/self/self.so \
|
||||
plugins/cache/cache.so
|
||||
plugins/cache/cache.so \
|
||||
plugins/session/session.so
|
||||
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_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_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_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
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -103,6 +105,10 @@ plugins/lcm/lcm.so: plugins/lcm/lcm.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/lcm/lcm.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
|
||||
plugins/session/session.so: plugins/session/session.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/session/session.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
|
||||
plugins/cli/cli.so: plugins/cli/cli.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/cli/cli.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
@@ -261,6 +267,9 @@ tests/test_self: tests/test_self.c $(LIB) plugins/self/self.so
|
||||
tests/test_fuzz: tests/test_fuzz.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_fuzz.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
|
||||
tests/test_sessiontools: tests/test_sessiontools.c $(LIB) plugins/session/session.so
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_sessiontools.c -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)
|
||||
|
||||
@@ -577,6 +577,12 @@ threshold is the knob that trades cache hits for a smaller context. What was
|
||||
dropped stays in the session log, and the `recall` tool searches it: the model
|
||||
can look an earlier turn up instead of guessing.
|
||||
|
||||
The `session` plugin reaches further back: `session_search` finds text in the
|
||||
logs of earlier sessions in this folder (with date, session id, title and a
|
||||
snippet) and `session_list` shows them. When the working directory has history,
|
||||
the system prompt says so and tells the model to look there before asking the
|
||||
user something that was already answered.
|
||||
|
||||
`/cost` reports what the session has cost: provider-reported dollars (if the
|
||||
provider puts `cost` in its usage object), tokens, and the cache share of the
|
||||
last request. It splits turns into `judged` and `plain`, because a judged model
|
||||
|
||||
@@ -340,6 +340,7 @@ plugin lcm {
|
||||
tool_trim = 8192 # older tool outputs are cut to this
|
||||
}
|
||||
plugin junction { } # url/header from the mcp junction server
|
||||
plugin session { } # old sessions: session_search/session_list
|
||||
plugin mine {
|
||||
path = ~/my/plugin.so # not shipped: say where it is
|
||||
foo = bar
|
||||
|
||||
@@ -520,3 +520,21 @@ progress while a subscription is open, so a live feed would be a promise the
|
||||
server cannot keep. Beyond loopback
|
||||
`SPLINTER_A2A_SERVER_TOKEN` is required and checked as
|
||||
`Authorization: Bearer ...`; there is no TLS.
|
||||
|
||||
## session
|
||||
|
||||
`session_search` and `session_list` read the logs of earlier sessions in this
|
||||
folder. Both are read-only; the running session is left out (it is already in
|
||||
the prompt), unless a call names it with `session=`.
|
||||
|
||||
`session_search { query, limit?, days?, session? }` walks the sessions newest
|
||||
first, one `getline` per JSONL record, and answers one line per hit:
|
||||
`2026-11-18 20:01 sess-... "title" user: ...text around the match...`.
|
||||
`limit` defaults to 10 (max 50), `days` keeps only recently changed sessions,
|
||||
and the whole answer stops at 12 KB; a hidden remainder says so. `session_list`
|
||||
shows date, id, size and title for the same set, with `(current)` on the open
|
||||
one.
|
||||
|
||||
The engine appends a paragraph to the system prompt when the working directory
|
||||
has earlier sessions: how many, the newest, and that `session_search` /
|
||||
`session_list` read them. No history, no paragraph.
|
||||
@@ -269,6 +269,13 @@ int sp_session_last_for(const char *dir, const char *cwd, char *id,
|
||||
/* What happened in a directory: sessions, turns, last question. */
|
||||
int sp_session_folder_history(const char *dir, const char *cwd,
|
||||
unsigned limit, struct sp_buf *out);
|
||||
/*
|
||||
* A paragraph for the system prompt when this working directory has earlier
|
||||
* sessions: how many, the newest, and the tools that read them. Empty output
|
||||
* when there are none.
|
||||
*/
|
||||
int sp_session_folder_note(const char *dir, const char *skip_id,
|
||||
struct sp_buf *out);
|
||||
int sp_session_load(const struct sp_session_info *, struct sp_conv *conv,
|
||||
size_t *loaded, struct sp_buf *err);
|
||||
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* 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 <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
|
||||
#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] = "";
|
||||
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");
|
||||
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;
|
||||
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;
|
||||
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 (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];
|
||||
|
||||
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 in this folder: what the user and "
|
||||
"the tools said, with date, session id and title. 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 sessions of this folder, newest first: date, id, size and "
|
||||
"title. 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;
|
||||
}
|
||||
@@ -3174,6 +3174,29 @@ setup_conversation(struct sp_engine *e, const struct sp_engine_opts *o,
|
||||
sp_prompt_expand(e->base.system_prompt, e->prompt_expanded,
|
||||
sizeof(e->prompt_expanded), o->model);
|
||||
e->base.system_prompt = e->prompt_expanded;
|
||||
/*
|
||||
* A folder that has history says so: the model should look in the
|
||||
* earlier sessions before asking the user something they already
|
||||
* answered. session_search and session_list (the session plugin) read
|
||||
* them; the note stays out when there is nothing to find.
|
||||
*/
|
||||
{
|
||||
struct sp_buf note;
|
||||
size_t used;
|
||||
|
||||
if (sp_buf_init(¬e) == 0) {
|
||||
(void)sp_session_folder_note(e->sess.dir, e->sess.id,
|
||||
¬e);
|
||||
used = strlen(e->prompt_expanded);
|
||||
if (note.len > 0 && note.p != NULL &&
|
||||
used + note.len + 2 < sizeof(e->prompt_expanded)) {
|
||||
e->prompt_expanded[used] = '\n';
|
||||
memcpy(e->prompt_expanded + used + 1, note.p,
|
||||
note.len + 1);
|
||||
}
|
||||
sp_buf_free(¬e);
|
||||
}
|
||||
}
|
||||
if (e->base.system_prompt != NULL &&
|
||||
e->base.system_prompt[0] != '\0' &&
|
||||
sp_conv_push(&e->conv, "system", NULL,
|
||||
|
||||
@@ -35,6 +35,13 @@
|
||||
"plugins, skills. Reinventing a thing that is already there costs the user time " \
|
||||
"and trust.\n" \
|
||||
"\n" \
|
||||
"Use the knowledge tools actively, not only when asked: the mcp_* tools for " \
|
||||
"tasks (task_*), memory (memory_*), skills (skill_*) and knowledge bases " \
|
||||
"(wiki_*, page_*). A multi-step job belongs in the task list before you start, " \
|
||||
"something you learned belongs back in a skill or the wiki, and a decision or " \
|
||||
"preference belongs in memory. A finding that lives only in this conversation " \
|
||||
"is lost.\n" \
|
||||
"\n" \
|
||||
"Work through errors by changing the approach, not by repeating the same attempt. " \
|
||||
"Keep a short record of what you built and where it lives.\n" \
|
||||
"\n" \
|
||||
|
||||
@@ -766,6 +766,63 @@ sp_session_folder_history(const char *dir, const char *cwd, unsigned limit,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* One paragraph for the system prompt: does this working directory have earlier
|
||||
* sessions? Then the model should look there before asking the user or starting
|
||||
* from scratch. Empty when there is nothing to find, so the prompt never
|
||||
* promises a history that does not exist.
|
||||
*/
|
||||
int
|
||||
sp_session_folder_note(const char *dir, const char *skip_id, struct sp_buf *out)
|
||||
{
|
||||
struct sp_session_brief v[200];
|
||||
struct sp_session_brief *newest = NULL;
|
||||
char cwd[512];
|
||||
int n, i, count = 0;
|
||||
|
||||
if (dir == NULL || out == NULL)
|
||||
return -1;
|
||||
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
||||
return -1;
|
||||
n = sp_session_list(dir, v, sizeof(v) / sizeof(v[0]));
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
for (i = 0; i < n; i++) {
|
||||
char scwd[512];
|
||||
|
||||
if (skip_id != NULL && strcmp(v[i].id, skip_id) == 0)
|
||||
continue;
|
||||
if (meta_cwd(dir, v[i].id, scwd, sizeof(scwd)) != 0)
|
||||
continue;
|
||||
if (strcmp(scwd, cwd) != 0)
|
||||
continue;
|
||||
count++;
|
||||
if (newest == NULL)
|
||||
newest = &v[i]; /* the list is newest first */
|
||||
}
|
||||
if (count == 0 || newest == NULL)
|
||||
return 0;
|
||||
{
|
||||
char stamp[32], title[96];
|
||||
struct tm tm;
|
||||
time_t when = (time_t)newest->mtime;
|
||||
|
||||
if (localtime_r(&when, &tm) == NULL)
|
||||
snprintf(stamp, sizeof(stamp), "?");
|
||||
else
|
||||
strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M", &tm);
|
||||
snprintf(title, sizeof(title), "%s",
|
||||
newest->title[0] != '\0' ? newest->title : "(untitled)");
|
||||
sp_buf_printf(out,
|
||||
"This working directory has earlier sessions: %d, the newest "
|
||||
"from %s (\"%s\"). Before you ask the user something or start "
|
||||
"from scratch, look there: session_search finds text in older "
|
||||
"sessions and session_list shows them. Name the session you "
|
||||
"used.", count, stamp, title);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_set_title(const char *dir, const char *id, const char *title,
|
||||
struct sp_buf *err)
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "config.h"
|
||||
#include "i18n.h"
|
||||
#include "mcp.h"
|
||||
#include "prompt.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
@@ -179,10 +180,42 @@ main(void)
|
||||
opts.session_dir = path;
|
||||
opts.session_id = "sess-engine";
|
||||
opts.no_transport = 0;
|
||||
/*
|
||||
* One earlier session in this directory, so setup_conversation has a
|
||||
* history to point at.
|
||||
*/
|
||||
{
|
||||
struct sp_session_info prev;
|
||||
|
||||
memset(&prev, 0, sizeof(prev));
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_session_open(&prev, path, "sess-prev", &err) == 0);
|
||||
CHECK(sp_session_append(&prev, "user",
|
||||
"an earlier question in this folder", &err) == 0);
|
||||
}
|
||||
if (sp_engine_open(&eng, &opts, &err) != 0) {
|
||||
fprintf(stderr, "engine: %s\n", err.p != NULL ? err.p : "?");
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
* The prompt knows this directory has history and says how to read it;
|
||||
* the default prompt tells the model to use the knowledge tools (tasks,
|
||||
* memory, skills, knowledge bases) actively.
|
||||
*/
|
||||
{
|
||||
const struct sp_conv *c = sp_engine_conv(eng);
|
||||
char def[4096];
|
||||
|
||||
CHECK(c != NULL && c->n > 0);
|
||||
CHECK(c != NULL && c->n > 0 &&
|
||||
contains(c->v[0].content, "earlier sessions"));
|
||||
CHECK(c != NULL && c->n > 0 &&
|
||||
contains(c->v[0].content, "session_search"));
|
||||
sp_prompt_default(def, sizeof(def), "test-model");
|
||||
CHECK(contains(def, "knowledge tools actively"));
|
||||
CHECK(contains(def, "knowledge bases"));
|
||||
CHECK(contains(def, "task_*"));
|
||||
}
|
||||
run_turn(eng);
|
||||
|
||||
/*
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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 <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
|
||||
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 */
|
||||
CHECK(!contains(out.p, "sess-now"));
|
||||
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);
|
||||
|
||||
/* 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)"));
|
||||
sp_buf_clear(&out);
|
||||
|
||||
/* 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);
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user