The Telegram daemon runs in $HOME, not in a project; sp_task_scope_note would have called the scope "home". .git in the working directory is the signal. test_sessiontools: a plain temp directory gets no paragraph; test_engine still sees the note in the repository root.
216 lines
6.4 KiB
C
216 lines
6.4 KiB
C
/*
|
|
* 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);
|
|
|
|
/*
|
|
* 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);
|
|
|
|
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;
|
|
}
|