sp_session_delete() unlinked the .jsonl, meta, agent, model and the logs, but not emb/<id>.emb. The index is derived data, yet it holds a copy of the conversation text and 42 MB had piled up in ~/.splinter: 41 orphaned files whose session was long gone. A delete that leaves a searchable copy of the deleted session behind is also the wrong promise. tests/test_session.c creates emb/s1.emb, removes the session and checks the file is gone: red before (access(F_OK) == 0), green after. make check: all tests passed, rc 0.
436 lines
12 KiB
C
436 lines
12 KiB
C
/* splinter -- append-only session tests. */
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_session_info s;
|
|
struct sp_buf err, line;
|
|
char tmpl[] = "/tmp/splinter-sess-XXXXXX";
|
|
char *dir;
|
|
char path[640];
|
|
size_t n;
|
|
FILE *f;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
if (dir == NULL) {
|
|
perror("mkdtemp");
|
|
return 1;
|
|
}
|
|
if (sp_buf_init(&err) != 0)
|
|
return 1;
|
|
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
CHECK(strcmp(s.id, "s1") == 0);
|
|
|
|
CHECK(sp_session_count(&s, &n, &err) == 0);
|
|
CHECK(n == 0);
|
|
|
|
CHECK(sp_session_append(&s, "user", "hi \"there\"", &err) == 0);
|
|
CHECK(sp_session_append(&s, "assistant", "hello", &err) == 0);
|
|
CHECK(sp_session_count(&s, &n, &err) == 0);
|
|
CHECK(n == 2);
|
|
|
|
/* the record is valid JSONL with the text escaped */
|
|
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
|
|
f = fopen(path, "r");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
if (sp_buf_init(&line) == 0) {
|
|
char buf[512];
|
|
size_t got = fread(buf, 1, sizeof(buf) - 1, f);
|
|
|
|
buf[got] = '\0';
|
|
CHECK(strstr(buf, "\"role\":\"user\"") != NULL);
|
|
CHECK(strstr(buf, "hi \\\"there\\\"") != NULL);
|
|
CHECK(buf[got - 1] == '\n');
|
|
sp_buf_free(&line);
|
|
}
|
|
fclose(f);
|
|
}
|
|
|
|
/*
|
|
* A torn tail must not swallow what is written after it. A run that
|
|
* hits ENOSPC leaves half a line; appending the next record straight
|
|
* onto it made one unparseable line out of two, and the loader - which
|
|
* skips a torn line - then lost the record behind it too (seen
|
|
* 2026-11-22 when /tmp filled). The writer closes the torn line first.
|
|
*/
|
|
{
|
|
struct sp_conv c;
|
|
size_t loaded = 0;
|
|
|
|
CHECK(sp_session_open(&s, dir, "torn", &err) == 0);
|
|
CHECK(sp_session_append(&s, "user", "vor dem absturz",
|
|
&err) == 0);
|
|
snprintf(path, sizeof(path), "%s/torn.jsonl", dir);
|
|
f = fopen(path, "a");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{\"role\":\"assistant\",\"text\":\"halb", f);
|
|
fclose(f);
|
|
}
|
|
CHECK(sp_session_append(&s, "user", "nach dem absturz",
|
|
&err) == 0);
|
|
sp_conv_init(&c);
|
|
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
|
|
CHECK(loaded == 2);
|
|
CHECK(c.n == 2);
|
|
if (c.n == 2) {
|
|
CHECK(strcmp(c.v[0].content, "vor dem absturz") == 0);
|
|
CHECK(strcmp(c.v[1].content, "nach dem absturz") == 0);
|
|
}
|
|
sp_conv_free(&c);
|
|
CHECK(sp_session_delete(dir, "torn", &err) == 0);
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
|
|
}
|
|
|
|
/* a torn tail must not be counted as a record and must not be an error */
|
|
f = fopen(path, "a");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"torn", f);
|
|
fclose(f);
|
|
}
|
|
CHECK(sp_session_count(&s, &n, &err) == 0);
|
|
CHECK(n == 2);
|
|
|
|
/* the first user line is the title, and it survives reopening */
|
|
{
|
|
struct sp_session_brief v[4];
|
|
char mpath[700];
|
|
FILE *m;
|
|
char buf[512];
|
|
size_t got;
|
|
|
|
snprintf(mpath, sizeof(mpath), "%s/s1.meta", dir);
|
|
m = fopen(mpath, "r");
|
|
CHECK(m != NULL);
|
|
if (m != NULL) {
|
|
got = fread(buf, 1, sizeof(buf) - 1, m);
|
|
buf[got] = '\0';
|
|
CHECK(strstr(buf, "title=hi \"there\"") != NULL);
|
|
fclose(m);
|
|
}
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "hi \"there\"") == 0);
|
|
}
|
|
|
|
/* an explicit title wins and is not lost when the session is opened */
|
|
CHECK(sp_session_set_title(dir, "s1", "Ein Titel", &err) == 0);
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
/* the goal lives in the same sidecar and survives a reopen */
|
|
CHECK(sp_session_set_goal(dir, "s1", "Ship the config file", &err) == 0);
|
|
{
|
|
char goal[1001];
|
|
|
|
CHECK(sp_session_get_goal(dir, "s1", goal, sizeof(goal)) == 0);
|
|
CHECK(strcmp(goal, "Ship the config file") == 0);
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
CHECK(sp_session_get_goal(dir, "s1", goal, sizeof(goal)) == 0);
|
|
CHECK(strcmp(goal, "Ship the config file") == 0);
|
|
}
|
|
{
|
|
struct sp_session_brief v[4];
|
|
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "Ein Titel") == 0);
|
|
}
|
|
|
|
/*
|
|
* A key the core does not own survives every rewrite. A plugin that
|
|
* marks a session in the same sidecar (workspace/foreign.c records
|
|
* foreign=hermes:h-1 there) lost its marker on the first reopen,
|
|
* because meta_write only ever wrote the keys it knew.
|
|
*/
|
|
{
|
|
char mpath[700], line[200], extra[200];
|
|
FILE *mf;
|
|
int found = 0;
|
|
|
|
snprintf(mpath, sizeof(mpath), "%s/s1.meta", dir);
|
|
mf = fopen(mpath, "a");
|
|
CHECK(mf != NULL);
|
|
if (mf != NULL) {
|
|
fputs("foreign=hermes:h-1\n", mf);
|
|
fclose(mf);
|
|
}
|
|
/* reopen, rename and /goal each rewrite the file */
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
CHECK(sp_session_set_title(dir, "s1", "Noch ein Titel",
|
|
&err) == 0);
|
|
CHECK(sp_session_set_goal(dir, "s1", "Ship it", &err) == 0);
|
|
mf = fopen(mpath, "r");
|
|
CHECK(mf != NULL);
|
|
while (mf != NULL && fgets(line, sizeof(line), mf) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
if (strcmp(line, "foreign=hermes:h-1") == 0)
|
|
found = 1;
|
|
}
|
|
if (mf != NULL)
|
|
fclose(mf);
|
|
snprintf(extra, sizeof(extra), "%s", found ? "kept" : "lost");
|
|
CHECK(strcmp(extra, "kept") == 0);
|
|
}
|
|
|
|
/* reopening an existing session appends, it never truncates */
|
|
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
|
CHECK(sp_session_append(&s, "user", "again", &err) == 0);
|
|
|
|
/* removal takes the log, sidecars and the per-session log directory */
|
|
{
|
|
char lpath[700], lfile[760];
|
|
FILE *lf;
|
|
|
|
snprintf(lpath, sizeof(lpath), "%s/logs", dir);
|
|
CHECK(mkdir(lpath, 0700) == 0 || errno == EEXIST);
|
|
snprintf(lpath, sizeof(lpath), "%s/logs/s1", dir);
|
|
CHECK(mkdir(lpath, 0700) == 0);
|
|
snprintf(lfile, sizeof(lfile), "%s/session.log", lpath);
|
|
lf = fopen(lfile, "w");
|
|
CHECK(lf != NULL);
|
|
if (lf != NULL) {
|
|
fputs("x\n", lf);
|
|
fclose(lf);
|
|
}
|
|
/* the /model sidecar the engine writes goes with it */
|
|
snprintf(lfile, sizeof(lfile), "%s/s1.model", dir);
|
|
lf = fopen(lfile, "w");
|
|
CHECK(lf != NULL);
|
|
if (lf != NULL) {
|
|
fputs("main/x\n", lf);
|
|
fclose(lf);
|
|
}
|
|
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
|
|
/*
|
|
* The embedding index is derived data, but it is written to
|
|
* emb/<id>.emb and nothing ever removed it: 41 orphans and
|
|
* 42 MB in ~/.splinter after a few weeks of /rm.
|
|
*/
|
|
{
|
|
char edir[700], efile[800];
|
|
FILE *ef;
|
|
|
|
snprintf(edir, sizeof(edir), "%s/emb", dir);
|
|
(void)mkdir(edir, 0755);
|
|
snprintf(efile, sizeof(efile), "%s/s1.emb", edir);
|
|
ef = fopen(efile, "w");
|
|
CHECK(ef != NULL);
|
|
if (ef != NULL) {
|
|
fputs("{\"sp_embed\":1}\n", ef);
|
|
fclose(ef);
|
|
}
|
|
CHECK(access(efile, F_OK) == 0);
|
|
}
|
|
CHECK(sp_session_delete(dir, "s1", &err) == 0);
|
|
CHECK(access(path, F_OK) != 0);
|
|
CHECK(access(lpath, F_OK) != 0);
|
|
CHECK(access(lfile, F_OK) != 0);
|
|
{
|
|
char efile[800];
|
|
|
|
snprintf(efile, sizeof(efile), "%s/emb/s1.emb", dir);
|
|
CHECK(access(efile, F_OK) != 0);
|
|
}
|
|
{
|
|
struct sp_session_brief v[4];
|
|
|
|
CHECK(sp_session_list(dir, v, 4) == 0);
|
|
}
|
|
CHECK(sp_session_delete(dir, "s1", &err) == -1);
|
|
}
|
|
|
|
/*
|
|
* A session from before titles existed has no title in its meta (or no
|
|
* meta at all): the listing reads the first user line itself.
|
|
*/
|
|
{
|
|
struct sp_session_brief v[4];
|
|
FILE *of;
|
|
|
|
snprintf(path, sizeof(path), "%s/old.jsonl", dir);
|
|
of = fopen(path, "w");
|
|
CHECK(of != NULL);
|
|
if (of != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"alte frage\"}\n",
|
|
of);
|
|
fclose(of);
|
|
}
|
|
CHECK(sp_session_list(dir, v, 4) == 1);
|
|
CHECK(strcmp(v[0].title, "alte frage") == 0);
|
|
CHECK(sp_session_delete(dir, "old", &err) == 0);
|
|
}
|
|
|
|
/*
|
|
* A record longer than any line buffer is loaded whole, the records
|
|
* after it too; an unterminated torn tail is skipped.
|
|
*/
|
|
{
|
|
struct sp_conv c;
|
|
char *big;
|
|
size_t loaded = 0, blen = 20000;
|
|
|
|
big = malloc(blen + 1);
|
|
CHECK(big != NULL);
|
|
if (big != NULL) {
|
|
memset(big, 'a', blen);
|
|
big[blen] = '\0';
|
|
CHECK(sp_session_open(&s, dir, "long", &err) == 0);
|
|
CHECK(sp_session_append(&s, "user", big, &err) == 0);
|
|
CHECK(sp_session_append(&s, "assistant", "after",
|
|
&err) == 0);
|
|
snprintf(path, sizeof(path), "%s/long.jsonl", dir);
|
|
f = fopen(path, "a");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"torn\"}", f);
|
|
fclose(f);
|
|
}
|
|
sp_conv_init(&c);
|
|
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
|
|
CHECK(loaded == 2);
|
|
CHECK(c.n == 2);
|
|
if (c.n == 2) {
|
|
CHECK(strlen(c.v[0].content) == blen);
|
|
CHECK(strcmp(c.v[1].content, "after") == 0);
|
|
}
|
|
sp_conv_free(&c);
|
|
free(big);
|
|
CHECK(sp_session_delete(dir, "long", &err) == 0);
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* A session is an agent transcript, not a chat log: almost every line is
|
|
* an observation. The first loader kept only user and assistant, which
|
|
* handed a resumed run 21 of 1507 lines - questions with no answers and
|
|
* no evidence between them. Everything below must come back.
|
|
*/
|
|
{
|
|
struct sp_conv c;
|
|
struct sp_sess_rec r;
|
|
size_t loaded = 0;
|
|
|
|
CHECK(sp_session_open(&s, dir, "full", &err) == 0);
|
|
memset(&r, 0, sizeof(r));
|
|
r.role = "user";
|
|
r.text = "BAUE EINE BRUECKE";
|
|
CHECK(sp_session_append_rec(&s, &r, &err) == 0);
|
|
r.role = "assistant";
|
|
r.text = "";
|
|
r.calls = "[{\"id\":\"c1\",\"type\":\"function\","
|
|
"\"function\":{\"name\":\"run\",\"arguments\":\"{}\"}}]";
|
|
CHECK(sp_session_append_rec(&s, &r, &err) == 0);
|
|
memset(&r, 0, sizeof(r));
|
|
r.role = "tool";
|
|
r.text = "DIE BEOBACHTUNG";
|
|
r.name = "run";
|
|
r.call_id = "c1";
|
|
CHECK(sp_session_append_rec(&s, &r, &err) == 0);
|
|
r.role = "assistant";
|
|
r.text = "Fertig.";
|
|
r.name = NULL;
|
|
r.call_id = NULL;
|
|
CHECK(sp_session_append_rec(&s, &r, &err) == 0);
|
|
|
|
sp_conv_init(&c);
|
|
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
|
|
CHECK(loaded == 4);
|
|
CHECK(c.n == 4);
|
|
if (c.n == 4) {
|
|
CHECK(strcmp(c.v[0].role, "user") == 0);
|
|
CHECK(strcmp(c.v[1].role, "assistant") == 0);
|
|
CHECK(c.v[1].calls != NULL);
|
|
CHECK(strcmp(c.v[2].role, "tool") == 0);
|
|
CHECK(c.v[2].name != NULL &&
|
|
strcmp(c.v[2].name, "run") == 0);
|
|
CHECK(c.v[2].call_id != NULL &&
|
|
strcmp(c.v[2].call_id, "c1") == 0);
|
|
CHECK(strcmp(c.v[2].content, "DIE BEOBACHTUNG") == 0);
|
|
}
|
|
sp_conv_free(&c);
|
|
CHECK(sp_session_delete(dir, "full", &err) == 0);
|
|
}
|
|
|
|
/* empty text is not a message: it must not reach the file */
|
|
{
|
|
struct sp_conv c;
|
|
size_t loaded = 0;
|
|
char line[800];
|
|
FILE *g;
|
|
int has_blank = 0;
|
|
|
|
CHECK(sp_session_open(&s, dir, "blank", &err) == 0);
|
|
CHECK(sp_session_append(&s, "user", "", &err) == 0);
|
|
CHECK(sp_session_append(&s, "user", "ECHT", &err) == 0);
|
|
snprintf(path, sizeof(path), "%s/blank.jsonl", dir);
|
|
g = fopen(path, "r");
|
|
CHECK(g != NULL);
|
|
if (g != NULL) {
|
|
while (fgets(line, sizeof(line), g) != NULL)
|
|
if (strstr(line, "\"text\":\"\"") != NULL)
|
|
has_blank = 1;
|
|
fclose(g);
|
|
}
|
|
CHECK(!has_blank);
|
|
sp_conv_init(&c);
|
|
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
|
|
CHECK(loaded == 1);
|
|
sp_conv_free(&c);
|
|
CHECK(sp_session_delete(dir, "blank", &err) == 0);
|
|
}
|
|
|
|
/*
|
|
* A file written before the format: an observation with no name, no
|
|
* call_id and no assistant turn carrying calls. It must still come back;
|
|
* dropping it is what emptied every resumed session.
|
|
*/
|
|
{
|
|
struct sp_conv c;
|
|
size_t loaded = 0;
|
|
int found = 0;
|
|
|
|
snprintf(path, sizeof(path), "%s/old.jsonl", dir);
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"AUFGABE\"}\n", f);
|
|
fputs("{\"role\":\"observation\",\"text\":\"ALTEBEFUND\"}\n",
|
|
f);
|
|
fclose(f);
|
|
}
|
|
CHECK(sp_session_open(&s, dir, "old", &err) == 0);
|
|
sp_conv_init(&c);
|
|
CHECK(sp_session_load(&s, &c, &loaded, &err) == 0);
|
|
for (size_t k = 0; k < c.n; k++)
|
|
if (c.v[k].content != NULL &&
|
|
strstr(c.v[k].content, "ALTEBEFUND") != NULL)
|
|
found = 1;
|
|
CHECK(found);
|
|
sp_conv_free(&c);
|
|
CHECK(sp_session_delete(dir, "old", &err) == 0);
|
|
}
|
|
|
|
sp_buf_free(&err);
|
|
printf("test_session: ok (%s)\n", dir);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|