A session file was written as if it were a chat: role and text, nothing
else. Reading it back kept "user" and "assistant" and dropped everything
else - and nearly every line of an agent transcript is an observation.
Measured on three real sessions: 21 of 1507 messages came back, 8 of 1356,
3 of 317. --resume then sent 1% of the conversation and the model had no
idea what it was continuing.
Three writers had to change with the loader:
- A turn that asked for tools wrote no line at all (agent.c only wrote
when ncalls == 0). It now goes out with its tool_calls, and each result
with the name of its tool and its call id; a provider rejects a tool
message without them.
- {"role":"user","text":""} came from the steer path and showed the model
a blank human turn it answered. sp_session_append_rec writes nothing
when text and calls are both empty.
- A record from the old format has no name and no call id. It is paired
with the next unanswered call of its pending assistant turn, or given a
synthetic one (recovered-N). Dropping it is what emptied every resume;
keeping it unpaired is what a provider answers with 400.
The same pass gives the core its own compactor (compact.c): a thread digest
- one line per dropped message, OPERATOR: 800 chars, assistant: 300, tool
result 160 - so the model keeps the story, not a count. lcm stays a plugin
that runs after it; sp_core_compactor() is the fallback only when no plugin
registered one, because running both eats the conversation before lcm sees
work to do.
Two bugs fell out of the same area:
- The plugin host was only built when a plugin was configured, so a
plugin-less run had no compactor at all: over max_bytes the turn was
refused instead of compacted (90 KB session, budget 8000, "nothing
sent").
- lcm walked *forward* past tool results at the tail boundary, which
silently shrank the tail: a turn with two results kept four messages
where keep_tail says six. Walking back to the assistant that carries
the call keeps six and no orphan.
And the .meta sidecar is shared now: the core rewrote it with only the keys
it knew, so anything a plugin put there was gone on the next open. It
carries unknown lines through (extra[8][160]), which is what lets a plugin
mark a session in the core's own file.
Tests: tests/test_corecompact.c (10 checks, no plugin), the resume cases in
tests/test_session.c, and the tail-size check in tests/test_compactor.c.
Each was seen red on the old code before it was green on the new.
373 lines
10 KiB
C
373 lines
10 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 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);
|
|
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);
|
|
{
|
|
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;
|
|
}
|