Files
splinter/tests/test_session.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

225 lines
5.8 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);
}
/* 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);
}
}
sp_buf_free(&err);
printf("test_session: ok (%s)\n", dir);
return failures == 0 ? 0 : 1;
}