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.
This commit is contained in:
1 parent
fd72b688c1
commit
c8b382c84e
12 files changed
+815
-7
No files matched your search
@@ -30,7 +30,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/cli/cli.so plugins/focus/focus.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
|
||||
tests/test_theme tests/test_plugin tests/test_builtin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -116,6 +116,8 @@ tests/test_theme: tests/test_theme.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_theme.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_focus: tests/test_focus.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_focus.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_builtin: tests/test_builtin.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_builtin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_i18n: tests/test_i18n.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_i18n.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_ascii: tests/test_ascii.c $(LIB) $(BINOBJ)
|
||||
|
||||
@@ -184,6 +184,28 @@ key that `en.msg` has.
|
||||
|
||||
Ships with `en` and `de`. Plugins translate their own strings with `sp_t()`.
|
||||
|
||||
## Tools
|
||||
|
||||
Every tool declares its class, and the loop enforces it - never the model, never
|
||||
the prompt.
|
||||
|
||||
- `read` (read): numbered lines, offset and limit, refuses binary files, says
|
||||
where it stopped.
|
||||
- `search` (read): literal string over a directory tree, capped at 100 hits.
|
||||
- `list` (read): a directory.
|
||||
- `write` (mutate): writes a file, up to 4 MiB.
|
||||
- `edit` (mutate): replaces exactly one occurrence; a non-unique `old` is an
|
||||
error, because an ambiguous edit is a wrong edit waiting to happen.
|
||||
- `run` (destructive): a shell command; the exit status is an observation.
|
||||
- `echo` (read), `clock` (read).
|
||||
|
||||
Mutate and destructive tools ask first, unless `--autonomous` is set. In the
|
||||
chat the question sits where the prompt is - `run (destructive) touch x? y/N` -
|
||||
and the turn waits on it; `n` or Esc denies, and the model is told
|
||||
`error: denied by operator`. This used to be dead code in the TUI: the prompt
|
||||
read stdin behind curses' back, so with `--autonomous` off every such tool was
|
||||
silently denied.
|
||||
|
||||
## Shell escape
|
||||
|
||||
`!command` runs it here and puts the output into the conversation, so the model
|
||||
@@ -205,10 +227,8 @@ ABI in `docs/PLUGINS.md`.
|
||||
## Limits, stated
|
||||
|
||||
- Tool arguments reach the tool as one string; each tool validates its own.
|
||||
- The loop has no JSON tool-call parser yet. Wire formats live in adapters, so
|
||||
the loop stays testable; an OpenAI adapter is the next piece.
|
||||
- No compaction, no retrieval tiers. When a turn does not fit the budget it
|
||||
fails loudly, which is the honest behaviour until compaction exists.
|
||||
- No retrieval tiers over old sessions. Compaction is a plugin (lcm); it cuts the
|
||||
conversation, it does not remember what it cut.
|
||||
- A `tick()` that never returns hangs the exit: the host can stop a thread only
|
||||
between ticks, not inside one.
|
||||
- Plugins are not sandboxed. They run in the process with full rights; the
|
||||
|
||||
@@ -187,6 +187,14 @@ struct sp_agent_config {
|
||||
int autonomous; /* 1: run mutate tools without asking */
|
||||
FILE *approval_in; /* NULL with !autonomous means deny */
|
||||
FILE *approval_out;
|
||||
/*
|
||||
* UI path for approval: when set, it is asked instead of the streams, so a
|
||||
* curses program can put the question on the screen instead of reading
|
||||
* stdin behind its own back. Returns 1 to allow, 0 to deny.
|
||||
*/
|
||||
int (*approve)(void *ctx, const char *name, int auth,
|
||||
const char *args);
|
||||
void *approve_ctx;
|
||||
|
||||
/*
|
||||
* Steering: while a turn runs, the operator may type. The UI cannot
|
||||
|
||||
Binary file not shown.
@@ -57,3 +57,6 @@ focus.widget_none = kein Ziel (/goal <text>)
|
||||
ui.prompt_mark = >
|
||||
sidebar.ms = %ld ms
|
||||
ui.empty = -
|
||||
ui.approve = %s ausfuehren (%s) %.60s? y/N
|
||||
auth.mutate = aendert Dateien
|
||||
auth.destructive = zerstoerend
|
||||
@@ -56,3 +56,6 @@ focus.widget_none = no goal (/goal <text>)
|
||||
ui.prompt_mark = >
|
||||
sidebar.ms = %ld ms
|
||||
ui.empty = -
|
||||
ui.approve = run %s (%s) %.60s? y/N
|
||||
auth.mutate = changes files
|
||||
auth.destructive = destructive
|
||||
@@ -28,6 +28,9 @@ ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args)
|
||||
{
|
||||
char line[64];
|
||||
|
||||
if (a->cfg->approve != NULL)
|
||||
return a->cfg->approve(a->cfg->approve_ctx, t->name, (int)t->auth,
|
||||
args);
|
||||
if (a->cfg->approval_out != NULL) {
|
||||
fprintf(a->cfg->approval_out, "run %s (%s) %s? [y/N] ", t->name,
|
||||
t->auth == SP_AUTH_MUTATE ? "mutate" : "destructive", args);
|
||||
|
||||
+506
-1
@@ -13,7 +13,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "mcp.h"
|
||||
@@ -113,6 +116,442 @@ tool_run(const char *args, struct sp_buf *out, void *ctx)
|
||||
return sp_buf_printf(out, "\n[killed by signal %d]", WTERMSIG(status));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* File tools.
|
||||
*
|
||||
* The harness had echo, run and clock - a chat with a shell, not a coding
|
||||
* agent. These five are the minimum for real work: look at a file, change one,
|
||||
* find something, list a directory, create something.
|
||||
*
|
||||
* Nothing here is allowed to be clever. Writes are mutate, edits refuse an
|
||||
* ambiguous match, reads are capped and say when they cut, and a binary file is
|
||||
* reported instead of sprayed into the conversation.
|
||||
*/
|
||||
#define TOOL_MAX_READ 65536
|
||||
#define TOOL_MAX_LINES 2000
|
||||
#define TOOL_MAX_FILE (4 * 1024 * 1024)
|
||||
#define TOOL_MAX_HITS 100
|
||||
|
||||
static int count_lines(const char *s, size_t upto);
|
||||
|
||||
static int
|
||||
arg_int(const char *args, const char *key, int dflt)
|
||||
{
|
||||
mcp_json *v;
|
||||
int n;
|
||||
|
||||
if (args == NULL || args[0] != '{')
|
||||
return dflt;
|
||||
v = mcp_parse(args);
|
||||
if (v == NULL)
|
||||
return dflt;
|
||||
n = mcp_arg_int(v, key, dflt);
|
||||
mcp_json_free(v);
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
looks_binary(const unsigned char *b, size_t n)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
if (b[i] == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_read(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct sp_buf keep;
|
||||
const char *path;
|
||||
char line[2048];
|
||||
FILE *f;
|
||||
int offset, limit, no = 0, printed = 0;
|
||||
size_t total = 0;
|
||||
|
||||
(void)ctx;
|
||||
if (sp_buf_init(&keep) != 0)
|
||||
return -1;
|
||||
path = arg_text(args, "path", &keep);
|
||||
if (path == NULL) {
|
||||
sp_buf_free(&keep);
|
||||
return sp_buf_puts(out, "error: 'path' is required");
|
||||
}
|
||||
offset = arg_int(args, "offset", 1);
|
||||
limit = arg_int(args, "limit", 200);
|
||||
if (offset < 1)
|
||||
offset = 1;
|
||||
if (limit < 1 || limit > TOOL_MAX_LINES)
|
||||
limit = TOOL_MAX_LINES;
|
||||
|
||||
f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
||||
sp_buf_free(&keep);
|
||||
return 0;
|
||||
}
|
||||
{
|
||||
unsigned char probe[512];
|
||||
size_t n = fread(probe, 1, sizeof(probe), f);
|
||||
|
||||
if (looks_binary(probe, n)) {
|
||||
sp_buf_printf(out, "error: %s is binary (%zu bytes read)", path, n);
|
||||
fclose(f);
|
||||
sp_buf_free(&keep);
|
||||
return 0;
|
||||
}
|
||||
rewind(f);
|
||||
}
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
no++;
|
||||
if (no < offset)
|
||||
continue;
|
||||
if (printed >= limit || total + strlen(line) > TOOL_MAX_READ) {
|
||||
sp_buf_printf(out, "[%s: stopped at line %d]\n", path, no);
|
||||
break;
|
||||
}
|
||||
sp_buf_printf(out, "%6d\t%s", no, line);
|
||||
total += strlen(line);
|
||||
printed++;
|
||||
}
|
||||
fclose(f);
|
||||
if (printed == 0 && no < offset)
|
||||
sp_buf_printf(out, "[%s: no lines at offset %d (file has %d)]\n",
|
||||
path, offset, no);
|
||||
sp_buf_free(&keep);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_write(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct sp_buf keep, content;
|
||||
const char *path, *text;
|
||||
FILE *f;
|
||||
size_t n;
|
||||
|
||||
(void)ctx;
|
||||
if (sp_buf_init(&keep) != 0 || sp_buf_init(&content) != 0)
|
||||
return -1;
|
||||
path = arg_text(args, "path", &keep);
|
||||
text = arg_text(args, "content", &content);
|
||||
if (path == NULL || text == NULL) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&content);
|
||||
return sp_buf_puts(out, "error: 'path' and 'content' are required");
|
||||
}
|
||||
n = strlen(text);
|
||||
if (n > TOOL_MAX_FILE) {
|
||||
sp_buf_printf(out, "error: refusing to write %zu bytes (limit %d)",
|
||||
n, TOOL_MAX_FILE);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&content);
|
||||
return 0;
|
||||
}
|
||||
f = fopen(path, "w");
|
||||
if (f == NULL) {
|
||||
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&content);
|
||||
return 0;
|
||||
}
|
||||
if (n > 0 && fwrite(text, 1, n, f) != n) {
|
||||
sp_buf_printf(out, "error: short write: %s", strerror(errno));
|
||||
fclose(f);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&content);
|
||||
return 0;
|
||||
}
|
||||
fclose(f);
|
||||
sp_buf_printf(out, "wrote %zu bytes to %s", n, path);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&content);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read a whole file into memory; NULL when it is too large or binary. */
|
||||
static char *
|
||||
slurp(const char *path, size_t *lenp, struct sp_buf *err)
|
||||
{
|
||||
FILE *f;
|
||||
struct sp_buf buf;
|
||||
char chunk[4096];
|
||||
size_t got;
|
||||
|
||||
f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
sp_buf_printf(err, "error: %s: %s", path, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
if (sp_buf_init(&buf) != 0) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) {
|
||||
if (buf.len + got > TOOL_MAX_FILE) {
|
||||
sp_buf_printf(err, "error: %s is larger than %d bytes",
|
||||
path, TOOL_MAX_FILE);
|
||||
sp_buf_free(&buf);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
if (sp_buf_add(&buf, chunk, got) != 0) {
|
||||
sp_buf_free(&buf);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
if (buf.p == NULL) {
|
||||
sp_buf_free(&buf);
|
||||
sp_buf_printf(err, "error: %s is empty", path);
|
||||
return NULL;
|
||||
}
|
||||
*lenp = buf.len;
|
||||
return buf.p; /* caller owns p */
|
||||
}
|
||||
|
||||
static int
|
||||
count_occurrences(const char *hay, const char *needle)
|
||||
{
|
||||
int n = 0;
|
||||
size_t nl = strlen(needle);
|
||||
const char *p = hay;
|
||||
|
||||
if (nl == 0)
|
||||
return -1;
|
||||
while ((p = strstr(p, needle)) != NULL) {
|
||||
n++;
|
||||
p += nl;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_edit(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct sp_buf keep, bufs[2];
|
||||
const char *path, *old, *new;
|
||||
char *text, *hit, *result;
|
||||
size_t len, oldlen, newlen, prefix;
|
||||
int count;
|
||||
FILE *f;
|
||||
|
||||
(void)ctx;
|
||||
if (sp_buf_init(&keep) != 0 || sp_buf_init(&bufs[0]) != 0 ||
|
||||
sp_buf_init(&bufs[1]) != 0)
|
||||
return -1;
|
||||
path = arg_text(args, "path", &keep);
|
||||
old = arg_text(args, "old", &bufs[0]);
|
||||
new = arg_text(args, "new", &bufs[1]);
|
||||
if (path == NULL || old == NULL || new == NULL) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return sp_buf_puts(out, "error: 'path', 'old' and 'new' are required");
|
||||
}
|
||||
oldlen = strlen(old);
|
||||
newlen = strlen(new);
|
||||
if (oldlen == 0) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return sp_buf_puts(out, "error: 'old' is empty");
|
||||
}
|
||||
|
||||
text = slurp(path, &len, out);
|
||||
if (text == NULL) {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return 0;
|
||||
}
|
||||
count = count_occurrences(text, old);
|
||||
if (count != 1) {
|
||||
/* an ambiguous edit is a wrong edit waiting to happen */
|
||||
sp_buf_printf(out, "error: 'old' occurs %d times in %s; "
|
||||
"make it unique", count, path);
|
||||
free(text);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return 0;
|
||||
}
|
||||
hit = strstr(text, old);
|
||||
prefix = (size_t)(hit - text);
|
||||
result = malloc(len - oldlen + newlen + 1);
|
||||
if (result == NULL) {
|
||||
sp_buf_puts(out, "error: out of memory");
|
||||
free(text);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return 0;
|
||||
}
|
||||
memcpy(result, text, prefix);
|
||||
memcpy(result + prefix, new, newlen);
|
||||
memcpy(result + prefix + newlen, text + prefix + oldlen,
|
||||
len - prefix - oldlen);
|
||||
result[len - oldlen + newlen] = '\0';
|
||||
|
||||
f = fopen(path, "w");
|
||||
if (f == NULL) {
|
||||
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
||||
} else {
|
||||
size_t outlen = len - oldlen + newlen;
|
||||
|
||||
if (fwrite(result, 1, outlen, f) != outlen)
|
||||
sp_buf_printf(out, "error: short write: %s",
|
||||
strerror(errno));
|
||||
else
|
||||
sp_buf_printf(out, "edited %s (line %d)", path,
|
||||
1 + count_lines(text, prefix));
|
||||
fclose(f);
|
||||
}
|
||||
free(result);
|
||||
free(text);
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&bufs[0]);
|
||||
sp_buf_free(&bufs[1]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
count_lines(const char *s, size_t upto)
|
||||
{
|
||||
size_t i;
|
||||
int n = 0;
|
||||
|
||||
for (i = 0; i < upto; i++)
|
||||
if (s[i] == '\n')
|
||||
n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
static int
|
||||
skip_name(const char *name)
|
||||
{
|
||||
return strcmp(name, ".git") == 0 || strcmp(name, ".") == 0 ||
|
||||
strcmp(name, "..") == 0 || strcmp(name, "node_modules") == 0;
|
||||
}
|
||||
|
||||
static void
|
||||
search_dir(const char *dir, const char *pattern, struct sp_buf *out, int *hits,
|
||||
int depth)
|
||||
{
|
||||
DIR *d;
|
||||
struct dirent *e;
|
||||
char path[1024], line[2048];
|
||||
struct stat st;
|
||||
|
||||
if (depth > 12 || *hits >= TOOL_MAX_HITS)
|
||||
return;
|
||||
d = opendir(dir);
|
||||
if (d == NULL)
|
||||
return;
|
||||
while ((e = readdir(d)) != NULL && *hits < TOOL_MAX_HITS) {
|
||||
FILE *f;
|
||||
|
||||
if (skip_name(e->d_name))
|
||||
continue;
|
||||
snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
|
||||
if (stat(path, &st) != 0)
|
||||
continue;
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
search_dir(path, pattern, out, hits, depth + 1);
|
||||
continue;
|
||||
}
|
||||
if (!S_ISREG(st.st_mode) || st.st_size > TOOL_MAX_FILE)
|
||||
continue;
|
||||
f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
continue;
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
if (strstr(line, pattern) == NULL)
|
||||
continue;
|
||||
sp_buf_printf(out, "%s: %s", path, line);
|
||||
if (out->len > 0 && out->p[out->len - 1] != '\n')
|
||||
sp_buf_puts(out, "\n");
|
||||
(*hits)++;
|
||||
if (*hits >= TOOL_MAX_HITS) {
|
||||
sp_buf_puts(out, "[stopped at the hit limit]\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
|
||||
static int
|
||||
tool_search(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct sp_buf keep, pbuf;
|
||||
const char *pattern, *root;
|
||||
int hits = 0;
|
||||
|
||||
(void)ctx;
|
||||
if (sp_buf_init(&keep) != 0 || sp_buf_init(&pbuf) != 0)
|
||||
return -1;
|
||||
pattern = arg_text(args, "pattern", &keep);
|
||||
root = arg_text(args, "path", &pbuf);
|
||||
if (pattern == NULL || *pattern == '\0') {
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&pbuf);
|
||||
return sp_buf_puts(out, "error: 'pattern' is required");
|
||||
}
|
||||
search_dir(root != NULL ? root : ".", pattern, out, &hits, 0);
|
||||
if (hits == 0)
|
||||
sp_buf_puts(out, "(no match)");
|
||||
sp_buf_free(&keep);
|
||||
sp_buf_free(&pbuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
struct sp_buf keep;
|
||||
const char *path;
|
||||
DIR *d;
|
||||
struct dirent *e;
|
||||
int n = 0;
|
||||
|
||||
(void)ctx;
|
||||
if (sp_buf_init(&keep) != 0)
|
||||
return -1;
|
||||
path = arg_text(args, "path", &keep);
|
||||
if (path == NULL)
|
||||
path = ".";
|
||||
d = opendir(path);
|
||||
if (d == NULL) {
|
||||
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
||||
sp_buf_free(&keep);
|
||||
return 0;
|
||||
}
|
||||
while ((e = readdir(d)) != NULL && n < 300) {
|
||||
char full[1024];
|
||||
struct stat st;
|
||||
|
||||
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
|
||||
continue;
|
||||
snprintf(full, sizeof(full), "%s/%s", path, e->d_name);
|
||||
if (stat(full, &st) == 0 && S_ISDIR(st.st_mode))
|
||||
sp_buf_printf(out, "%s/\n", e->d_name);
|
||||
else
|
||||
sp_buf_printf(out, "%s\n", e->d_name);
|
||||
n++;
|
||||
}
|
||||
closedir(d);
|
||||
if (n == 0)
|
||||
sp_buf_puts(out, "(empty)");
|
||||
sp_buf_free(&keep);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_builtin_register(struct sp_registry *r)
|
||||
{
|
||||
@@ -137,5 +576,71 @@ sp_builtin_register(struct sp_registry *r)
|
||||
"\"required\":[\"command\"]}";
|
||||
t.auth = SP_AUTH_DESTRUCTIVE;
|
||||
t.run = tool_run;
|
||||
return sp_registry_add(r, &t);
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "read";
|
||||
t.description = "Read a text file. Returns numbered lines and says "
|
||||
"where it stopped.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"path\":{\"type\":\"string\",\"description\":\"file to read\"},"
|
||||
"\"offset\":{\"type\":\"integer\",\"description\":\"first line, 1-based\"},"
|
||||
"\"limit\":{\"type\":\"integer\",\"description\":\"max lines\"}},"
|
||||
"\"required\":[\"path\"]}";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = tool_read;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "write";
|
||||
t.description = "Write a file, replacing it if it exists.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"path\":{\"type\":\"string\"},"
|
||||
"\"content\":{\"type\":\"string\"}},"
|
||||
"\"required\":[\"path\",\"content\"]}";
|
||||
t.auth = SP_AUTH_MUTATE;
|
||||
t.run = tool_write;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "edit";
|
||||
t.description = "Replace one occurrence of 'old' with 'new'. Refuses "
|
||||
"when 'old' is not unique.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"path\":{\"type\":\"string\"},"
|
||||
"\"old\":{\"type\":\"string\",\"description\":\"exact text to replace\"},"
|
||||
"\"new\":{\"type\":\"string\",\"description\":\"replacement\"}},"
|
||||
"\"required\":[\"path\",\"old\",\"new\"]}";
|
||||
t.auth = SP_AUTH_MUTATE;
|
||||
t.run = tool_edit;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "search";
|
||||
t.description = "Search files under a directory for a literal string.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"pattern\":{\"type\":\"string\"},"
|
||||
"\"path\":{\"type\":\"string\",\"description\":\"directory, default .\"}},"
|
||||
"\"required\":[\"pattern\"]}";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = tool_search;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "list";
|
||||
t.description = "List a directory.";
|
||||
t.params = "{\"type\":\"object\",\"properties\":{"
|
||||
"\"path\":{\"type\":\"string\",\"description\":\"directory, default .\"}}}";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = tool_list;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -103,6 +103,13 @@ struct turn {
|
||||
char tools_done[16][256]; /* finished calls, in order */
|
||||
int ntools_done;
|
||||
int tool_tick;
|
||||
/* a mutate/destructive tool is waiting for the operator */
|
||||
int approve_pending;
|
||||
int approve_result;
|
||||
char approve_name[64];
|
||||
char approve_args[256];
|
||||
int approve_auth;
|
||||
pthread_cond_t cond;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
@@ -112,6 +119,37 @@ struct turn {
|
||||
* operator sees which tool ran with which arguments, not just that something
|
||||
* happened.
|
||||
*/
|
||||
/*
|
||||
* Approval, asked on the screen.
|
||||
*
|
||||
* The turn runs on its own thread and must block here until a human answers;
|
||||
* the UI thread keeps reading keys meanwhile. That is the whole reason this is
|
||||
* a condition variable and not a prompt on stdout - a curses program cannot
|
||||
* read stdin behind its own back without eating the keystrokes it needs.
|
||||
*/
|
||||
static int
|
||||
approve_cb(void *ctx, const char *name, int auth, const char *args)
|
||||
{
|
||||
struct turn *t = ctx;
|
||||
int answer;
|
||||
|
||||
pthread_mutex_lock(&t->lock);
|
||||
snprintf(t->approve_name, sizeof(t->approve_name), "%s",
|
||||
name != NULL ? name : "?");
|
||||
snprintf(t->approve_args, sizeof(t->approve_args), "%.200s",
|
||||
args != NULL ? args : "");
|
||||
t->approve_auth = auth;
|
||||
t->approve_result = 0;
|
||||
t->approve_pending = 1;
|
||||
pthread_cond_broadcast(&t->cond);
|
||||
while (t->approve_pending && !t->cancel)
|
||||
pthread_cond_wait(&t->cond, &t->lock);
|
||||
answer = t->cancel ? 0 : t->approve_result;
|
||||
t->approve_pending = 0;
|
||||
pthread_mutex_unlock(&t->lock);
|
||||
return answer;
|
||||
}
|
||||
|
||||
static void
|
||||
tool_begin_cb(void *ctx, const char *name, const char *args)
|
||||
{
|
||||
@@ -1039,11 +1077,14 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
sp_buf_init(&tu.live);
|
||||
sp_buf_init(&tu.think);
|
||||
pthread_mutex_init(&tu.lock, NULL);
|
||||
pthread_cond_init(&tu.cond, NULL);
|
||||
cfg.cancel = &tu.cancel;
|
||||
cfg.steer = steer_cb;
|
||||
cfg.steer_ctx = &tu;
|
||||
cfg.stream = stream_cb;
|
||||
cfg.stream_ctx = &tu;
|
||||
cfg.approve = approve_cb;
|
||||
cfg.approve_ctx = &tu;
|
||||
sp_openai_set_cancel(&tu.cancel);
|
||||
cfg.tool_begin = tool_begin_cb;
|
||||
cfg.tool_begin_ctx = &tu;
|
||||
@@ -1082,6 +1123,50 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
pthread_mutex_unlock(&tu.lock);
|
||||
|
||||
c = wgetch(ui_input);
|
||||
/*
|
||||
* A tool needs permission. The question goes
|
||||
* where the prompt is, and y/N answers it; the
|
||||
* turn thread is parked on a condition variable
|
||||
* until it does.
|
||||
*/
|
||||
{
|
||||
int pending;
|
||||
char nm[64], ar[256];
|
||||
int auth = SP_AUTH_READ;
|
||||
|
||||
pthread_mutex_lock(&tu.lock);
|
||||
pending = tu.approve_pending;
|
||||
if (pending) {
|
||||
snprintf(nm, sizeof(nm), "%s",
|
||||
tu.approve_name);
|
||||
snprintf(ar, sizeof(ar), "%s",
|
||||
tu.approve_args);
|
||||
auth = tu.approve_auth;
|
||||
}
|
||||
pthread_mutex_unlock(&tu.lock);
|
||||
if (pending) {
|
||||
werase(ui_input);
|
||||
mvwprintw(ui_input, 0, 0,
|
||||
sp_t("ui.approve"), nm,
|
||||
auth == SP_AUTH_MUTATE ?
|
||||
sp_t("auth.mutate") :
|
||||
sp_t("auth.destructive"),
|
||||
ar);
|
||||
wrefresh(ui_input);
|
||||
if (c == 'y' || c == 'Y' ||
|
||||
c == 'n' || c == 'N' ||
|
||||
c == 27) {
|
||||
pthread_mutex_lock(&tu.lock);
|
||||
tu.approve_result =
|
||||
(c == 'y' ||
|
||||
c == 'Y');
|
||||
tu.approve_pending = 0;
|
||||
pthread_cond_broadcast(&tu.cond);
|
||||
pthread_mutex_unlock(&tu.lock);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (c == ERR) {
|
||||
char think[4096];
|
||||
char live[8192];
|
||||
@@ -1272,7 +1357,8 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
|
||||
sp_buf_free(&tu.err);
|
||||
sp_buf_free(&tu.live);
|
||||
sp_buf_free(&tu.think);
|
||||
pthread_mutex_destroy(&tu.lock);
|
||||
pthread_cond_destroy(&tu.cond);
|
||||
pthread_mutex_destroy(&tu.lock);
|
||||
}
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &t1);
|
||||
|
||||
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
Reference in new issue
Block a user