Rebuilt from scratch after the previous attempt left no code behind: what exists here is what compiles and what the tests prove, and nothing else is claimed. What is in it: - a turn loop that refuses an over-budget request before sending it, reports unknown tools instead of guessing, and honours authorization classes (read runs, mutate/destructive ask unless --autonomous); - a registry that is authoritative - the model names a tool, the registry decides whether it exists; - an append-only JSONL session (O_APPEND, fsync per record), where a crash can only truncate the tail and the tail is skipped, not repaired; - two transports: a deterministic mock (queue of replies, last request kept) and HTTP via curl(1) with the request body in a temp file; - two builtins that exist to prove the classes: echo (read) and run (destructive, reports its exit status as an observation). Five test binaries, no external framework, no network: mock driver covers the answer path, a tool round, an unknown tool, a denial, autonomy, a failing tool, the step limit, the byte budget (asserting that nothing was sent) and a non-2xx reply. The wire format is splinter's own line protocol (user/observe out, answer/tool in); provider adapters translate. That is why the loop is testable without a model, and why no vendor JSON is parsed in the core.
84 lines
1.9 KiB
C
84 lines
1.9 KiB
C
/* splinter -- append-only session tests. */
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.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);
|
|
|
|
/* 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);
|
|
|
|
sp_buf_free(&err);
|
|
printf("test_session: ok (%s)\n", dir);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|