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.
198 lines
5.6 KiB
C
198 lines
5.6 KiB
C
/*
|
|
* splinter -- turn loop tests.
|
|
*
|
|
* Every case drives the real loop through the mock transport: no network, no
|
|
* model, no key. What is asserted is what the loop actually did - the answer,
|
|
* the request it sent, the observations it produced, the counters.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.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 int
|
|
echo_tool(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
(void)ctx;
|
|
return sp_buf_puts(out, args);
|
|
}
|
|
|
|
static int
|
|
failing_tool(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
(void)args;
|
|
(void)out;
|
|
(void)ctx;
|
|
return -1;
|
|
}
|
|
|
|
static void
|
|
setup(struct sp_registry *r)
|
|
{
|
|
struct sp_tool t;
|
|
|
|
memset(r, 0, sizeof(*r));
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "echo";
|
|
t.description = "echo the argument";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = echo_tool;
|
|
(void)sp_registry_add(r, &t);
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "write";
|
|
t.description = "pretend to mutate";
|
|
t.auth = SP_AUTH_MUTATE;
|
|
t.run = echo_tool;
|
|
(void)sp_registry_add(r, &t);
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "boom";
|
|
t.description = "always fails";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = failing_tool;
|
|
(void)sp_registry_add(r, &t);
|
|
}
|
|
|
|
static int
|
|
run_turn(struct sp_mock *m, struct sp_registry *r, const char *prompt,
|
|
int autonomous, size_t steps, size_t max_bytes, struct sp_buf *answer,
|
|
struct sp_buf *err, struct sp_agent_stats *stats)
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
cfg.max_steps = steps ? steps : 8;
|
|
cfg.max_bytes = max_bytes;
|
|
cfg.autonomous = autonomous;
|
|
a.reg = r;
|
|
a.cfg = &cfg;
|
|
a.tr = sp_mock_transport(m);
|
|
{
|
|
int rc = sp_agent_run(&a, prompt, answer, err);
|
|
|
|
if (stats != NULL)
|
|
*stats = a.stats;
|
|
return rc;
|
|
}
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_registry r;
|
|
struct sp_buf answer, err;
|
|
struct sp_agent_stats st;
|
|
struct sp_mock m;
|
|
|
|
setup(&r);
|
|
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0)
|
|
return 1;
|
|
|
|
/* 1. a plain answer needs no tool and sends exactly one request */
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer hello") == 0);
|
|
CHECK(run_turn(&m, &r, "hi", 0, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strcmp(answer.p, "hello") == 0);
|
|
CHECK(st.steps == 1 && st.tool_calls == 0);
|
|
CHECK(strcmp(m.last_request.p, "user hi\n") == 0);
|
|
sp_mock_free(&m);
|
|
|
|
/* 2. a read tool runs and its observation goes back to the transport */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo ping") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer done") == 0);
|
|
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strcmp(answer.p, "done") == 0);
|
|
CHECK(st.tool_calls == 1 && st.denied == 0);
|
|
CHECK(strstr(m.last_request.p, "observe echo ping") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 3. an unknown tool is reported, not guessed at */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool nope x") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
|
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strstr(m.last_request.p, "unknown tool 'nope'") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 4. a mutating tool without consent is denied, and the denial is the
|
|
* observation the model sees (fail closed: no approval input) */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
|
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(st.denied == 1);
|
|
CHECK(strstr(m.last_request.p, "denied by operator") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 5. the same tool runs when the operator allowed autonomy */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
|
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(st.denied == 0);
|
|
CHECK(strstr(m.last_request.p, "observe write x") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 6. a failing tool becomes an observation, not a crash */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool boom x") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
|
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strstr(m.last_request.p, "tool 'boom' failed") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 7. an endless tool loop stops at the step limit */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo a") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo b") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo c") == 0);
|
|
CHECK(run_turn(&m, &r, "loop", 1, 2, 0, &answer, &err, &st) != 0);
|
|
CHECK(strstr(err.p, "step limit") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/* 8. an over-budget request is refused before anything is sent */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(run_turn(&m, &r, "this prompt is far longer than ten bytes",
|
|
1, 8, 10, &answer, &err, &st) != 0);
|
|
CHECK(strstr(err.p, "budget exceeded") != NULL);
|
|
CHECK(m.requests == 0);
|
|
sp_mock_free(&m);
|
|
|
|
/* 9. a non-2xx reply fails the turn instead of being treated as text */
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
|
CHECK(run_turn(&m, &r, "hi", 1, 8, 0, &answer, &err, &st) != 0);
|
|
CHECK(strstr(err.p, "status 500") != NULL);
|
|
sp_mock_free(&m);
|
|
|
|
sp_buf_free(&answer);
|
|
sp_buf_free(&err);
|
|
sp_registry_free(&r);
|
|
|
|
if (failures == 0)
|
|
printf("test_agent: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|