Files
splinter/tests/test_builtin.c
T
Konrad c8b382c84e splinter: file tools, and approval asked on the screen
The harness had echo, run and clock - a chat with a shell. A coding agent needs
to look at a file, change one, find something, list a directory:

  read   (read)   numbered lines, offset/limit, refuses binary, says where it
                  stopped
  search (read)   literal search over a tree, capped at 100 hits
  list   (read)   a directory
  write  (mutate) up to 4 MiB
  edit   (mutate) exactly one occurrence; a non-unique 'old' is an error
  run    (destructive, already there)

Approval was dead code in the chat: ask_operator() read stdin, which a curses
program cannot do without eating its own keystrokes, so with --autonomous off
every mutate tool was silently denied ('fail closed'). The agent now asks through
a callback; the UI puts the question in the prompt line and parks the turn
thread on a condition variable until y or n arrives.

Measured against kontron: prompt appeared 6 s into the turn, 'y' ran the tool and
the answer carried its output; 'n' left the target file absent and the model saw
'error: denied by operator'.

test_builtin covers the refusals, not just the happy path: ambiguous edit writes
nothing, binary read reports instead of spraying, missing arguments say what is
required. README no longer claims there is no tool-call parser and no compaction.
2026-11-08 09:29:48 +00:00

179 lines
4.9 KiB
C

/*
* splinter -- builtin file tools.
*
* The tools are the part of the harness a model can actually break something
* with, so each of them is tested against the failure it is supposed to refuse:
* an ambiguous edit, a binary file, a write that is too large.
*/
#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)
static char dir[600];
static const struct sp_tool *
tool(struct sp_registry *r, const char *name)
{
const struct sp_tool *t = sp_registry_find(r, name);
CHECK(t != NULL);
return t;
}
static void
write_file(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(text, f);
fclose(f);
}
}
int
main(void)
{
struct sp_registry r;
struct sp_buf out;
char path[700], big[1600];
const char *copy;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-test-XXXXXX", tmp);
CHECK(mkdtemp(dir) != NULL);
memset(&r, 0, sizeof(r));
if (sp_buf_init(&out) != 0)
return 1;
CHECK(sp_builtin_register(&r) == 0);
/* the five tools exist and declare their class */
CHECK(tool(&r, "read")->auth == SP_AUTH_READ);
CHECK(tool(&r, "write")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "edit")->auth == SP_AUTH_MUTATE);
CHECK(tool(&r, "search")->auth == SP_AUTH_READ);
CHECK(tool(&r, "list")->auth == SP_AUTH_READ);
CHECK(tool(&r, "run")->auth == SP_AUTH_DESTRUCTIVE);
snprintf(path, sizeof(path), "%s/a.txt", dir);
write_file(path, "erste zeile\nzweite zeile\ndritte zeile\n");
/* ---- read ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite zeile") != NULL);
CHECK(strstr(out.p, "1\terste") != NULL);
/* offset and limit */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"offset\":2,\"limit\":1}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "zweite") != NULL);
CHECK(strstr(out.p, "erste") == NULL);
/* a missing file is an observation, not a crash */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s/nope\"}", dir);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "error:") != NULL);
/* ---- write ---- */
sp_buf_clear(&out);
snprintf(path, sizeof(path), "%s/neu.txt", dir);
snprintf(big, sizeof(big), "{\"path\":\"%s\",\"content\":\"hallo\"}", path);
CHECK(tool(&r, "write")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "wrote 5 bytes") != NULL);
/* ---- edit: exactly one match ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"hallo\",\"new\":\"welt\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "edited") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "welt") != NULL);
/* ---- edit: two matches are refused ---- */
write_file(path, "x x\n");
sp_buf_clear(&out);
snprintf(big, sizeof(big),
"{\"path\":\"%s\",\"old\":\"x\",\"new\":\"y\"}", path);
CHECK(tool(&r, "edit")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "occurs 2 times") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", path);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "y") == NULL); /* nothing was written */
/* ---- read refuses a binary file ---- */
{
FILE *f;
char bin[700];
snprintf(bin, sizeof(bin), "%s/bin.dat", dir);
f = fopen(bin, "wb");
CHECK(f != NULL);
if (f != NULL) {
fwrite("abc\0def", 1, 7, f);
fclose(f);
}
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", bin);
CHECK(tool(&r, "read")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "binary") != NULL);
}
/* ---- search and list ---- */
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"pattern\":\"zweite\",\"path\":\"%s\"}", dir);
CHECK(tool(&r, "search")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
sp_buf_clear(&out);
snprintf(big, sizeof(big), "{\"path\":\"%s\"}", dir);
CHECK(tool(&r, "list")->run(big, &out, NULL) == 0);
CHECK(strstr(out.p, "a.txt") != NULL);
/* ---- missing arguments are reported, never guessed ---- */
sp_buf_clear(&out);
CHECK(tool(&r, "read")->run("{}", &out, NULL) == 0);
CHECK(strstr(out.p, "required") != NULL);
copy = out.p;
CHECK(copy != NULL);
sp_registry_free(&r);
sp_buf_free(&out);
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed: %s\n", strerror(errno));
}
if (failures == 0)
printf("test_builtin: ok\n");
return failures == 0 ? 0 : 1;
}