The guard was there since 16.11. ("keep the last known prompt size, or the
status bar drops to 0 after every tool call") but nothing tested it, so
t0429 stayed open on the one point that had no evidence.
usage_gap_send reports 1000 prompt tokens on the call that starts the round,
9000 on the next, and nothing at all on the answer. Without the guard
last_in becomes 0 on that answer; with it the meter keeps 9000 and the cache
figure 4000, while tokens_in still sums what was actually reported.
Evidence: make check exit 0 (45 binaries + 20 shell tests), tests/check_asan.sh
all 45 clean. Counter-check: with the old unconditional
`last_in = reply.tokens_in > 0 ? ... : 0`, test_agent fails on
last_in == 9000 and last_cached == 4000.
The other four points of t0429 are covered elsewhere and were verified:
the goal sidecar and /continue in test_session.c and test_engine.c, the
plain sidebar page in test_theme.c (flat light/dark: bg white/black, fg the
opposite, input on the same page), and sysinfo was dropped on purpose in
t0469.
840 lines
25 KiB
C
840 lines
25 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 <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.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);
|
|
}
|
|
|
|
/* a stream that thinks, dies without an answer, then answers */
|
|
static int flaky_calls;
|
|
static void no_stream(void *ctx, int kind, const char *text)
|
|
{
|
|
(void)ctx; (void)kind; (void)text;
|
|
}
|
|
static int
|
|
flaky_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err, sp_stream_cb cb, void *cbctx)
|
|
{
|
|
(void)ctx; (void)conv;
|
|
flaky_calls++;
|
|
if (flaky_calls == 1) {
|
|
if (cb != NULL)
|
|
cb(cbctx, SP_STREAM_THINK, "denke nach...");
|
|
sp_buf_puts(err, "openai: stream ended with no answer: ");
|
|
return -1;
|
|
}
|
|
out->status = 200;
|
|
out->content = strdup("answer after the retry");
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* The turn that asks for words is the last resort of the malformed-call
|
|
* retry: a model that reads it answers, and the turn ends with an answer
|
|
* instead of dying on "malformed tool arguments".
|
|
*/
|
|
static int
|
|
asked_for_words(const struct sp_conv *conv)
|
|
{
|
|
size_t i;
|
|
|
|
if (conv == NULL)
|
|
return 0;
|
|
for (i = conv->n; i > 0; i--)
|
|
if (conv->v[i - 1].content != NULL &&
|
|
strstr(conv->v[i - 1].content, "Answer in plain text") != NULL)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* A call the token limit cut off: the arguments stop in the middle, and the
|
|
* provider says why ("finish_reason": "length"). That is a different fault
|
|
* from a model that cannot write JSON - the same request truncates it again -
|
|
* so the correction asks for a smaller call, not for the same one.
|
|
*/
|
|
static int cut_off_calls;
|
|
static struct sp_buf cut_off_second;
|
|
static int
|
|
cut_off_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
(void)ctx;
|
|
(void)err;
|
|
cut_off_calls++;
|
|
if (asked_for_words(conv)) {
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("the call did not fit");
|
|
return out->content != NULL ? 0 : -1;
|
|
}
|
|
if (cut_off_calls == 2 && conv != NULL) {
|
|
size_t i;
|
|
|
|
sp_buf_clear(&cut_off_second);
|
|
for (i = 0; i < conv->n; i++)
|
|
if (conv->v[i].content != NULL) {
|
|
sp_buf_puts(&cut_off_second, conv->v[i].content);
|
|
sp_buf_puts(&cut_off_second, "\n");
|
|
}
|
|
}
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("");
|
|
snprintf(out->finish, sizeof(out->finish), "length");
|
|
out->ncalls = 1;
|
|
out->calls = calloc(1, sizeof(*out->calls));
|
|
if (out->content == NULL || out->calls == NULL)
|
|
return -1;
|
|
out->calls[0].id = strdup("c1");
|
|
out->calls[0].name = strdup("run");
|
|
out->calls[0].args = strdup("{\"command\":\"echo a very long comm");
|
|
out->calls_json = strdup("[{\"id\":\"c1\",\"type\":\"function\","
|
|
"\"function\":{\"name\":\"run\",\"arguments\":"
|
|
"\"{\\\"command\\\":\\\"echo a very long comm\"}}]");
|
|
return out->calls[0].id != NULL && out->calls[0].name != NULL &&
|
|
out->calls[0].args != NULL && out->calls_json != NULL ? 0 : -1;
|
|
}
|
|
|
|
/*
|
|
* Step 1 reports prompt usage and asks for a tool; step 2 reports the real
|
|
* prompt size; step 3 answers without reporting anything. Providers do this (a
|
|
* bridge that drops the usage object on the final chunk), and the meter must
|
|
* not read that as an empty context.
|
|
*/
|
|
static int usage_gap_calls;
|
|
static int
|
|
usage_gap_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
(void)ctx;
|
|
(void)conv;
|
|
(void)err;
|
|
usage_gap_calls++;
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
if (usage_gap_calls == 1 || usage_gap_calls == 2) {
|
|
out->content = strdup("");
|
|
out->tokens_in = usage_gap_calls == 1 ? 1000 : 9000;
|
|
out->tokens_out = 20;
|
|
out->tokens_cached = usage_gap_calls == 1 ? 0 : 4000;
|
|
out->ncalls = 1;
|
|
out->calls = calloc(1, sizeof(*out->calls));
|
|
if (out->content == NULL || out->calls == NULL)
|
|
return -1;
|
|
out->calls[0].id = strdup(usage_gap_calls == 1 ?
|
|
"call-0" : "call-1");
|
|
out->calls[0].name = strdup("echo");
|
|
out->calls[0].args = strdup(usage_gap_calls == 1 ?
|
|
"{\"text\":\"eins\"}" : "{\"text\":\"zwei\"}");
|
|
return out->calls[0].id != NULL &&
|
|
out->calls[0].name != NULL && out->calls[0].args != NULL ?
|
|
0 : -1;
|
|
}
|
|
/* silent: no tokens_in, no tokens_out, no cache */
|
|
out->content = strdup("done without usage");
|
|
return out->content != NULL ? 0 : -1;
|
|
}
|
|
|
|
/* a reply whose tool call carries arguments that are not JSON */
|
|
static int bad_args_calls;
|
|
static struct sp_buf bad_args_second; /* the second request, verbatim */
|
|
static int
|
|
bad_args_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
(void)ctx;
|
|
bad_args_calls++;
|
|
if (asked_for_words(conv)) {
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("nothing ran, here is what I meant");
|
|
return out->content != NULL ? 0 : -1;
|
|
}
|
|
/*
|
|
* The retry has to carry a correction, not the same prompt again:
|
|
* the model answers a request it already answered with a broken
|
|
* call usually with the same broken call.
|
|
*/
|
|
if (bad_args_calls == 2 && conv != NULL) {
|
|
size_t i;
|
|
|
|
sp_buf_clear(&bad_args_second);
|
|
for (i = 0; i < conv->n; i++)
|
|
if (conv->v[i].content != NULL) {
|
|
sp_buf_puts(&bad_args_second,
|
|
conv->v[i].content);
|
|
sp_buf_puts(&bad_args_second, "\n");
|
|
}
|
|
}
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("");
|
|
out->ncalls = 1;
|
|
out->calls = calloc(1, sizeof(*out->calls));
|
|
if (out->content == NULL || out->calls == NULL)
|
|
return -1;
|
|
out->calls[0].id = strdup("c1");
|
|
out->calls[0].name = strdup("echo");
|
|
out->calls[0].args = strdup("{command:bad}");
|
|
out->calls_json = strdup("[{\"id\":\"c1\",\"type\":\"function\","
|
|
"\"function\":{\"name\":\"echo\","
|
|
"\"arguments\":\"{command:bad}\"}}]");
|
|
(void)err;
|
|
return out->calls[0].id != NULL && out->calls[0].name != NULL &&
|
|
out->calls[0].args != NULL && out->calls_json != NULL ? 0 : -1;
|
|
}
|
|
|
|
/*
|
|
* The shape that cost a whole command in a real session: two argument objects
|
|
* in one string. mcp_parse() stops at the first closing brace, so the second
|
|
* command was silently dropped; the model must be told instead.
|
|
*/
|
|
static int joined_args_calls;
|
|
static int
|
|
joined_args_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
(void)ctx;
|
|
(void)err;
|
|
joined_args_calls++;
|
|
if (asked_for_words(conv)) {
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("one call at a time, nothing ran");
|
|
return out->content != NULL ? 0 : -1;
|
|
}
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("");
|
|
out->ncalls = 1;
|
|
out->calls = calloc(1, sizeof(*out->calls));
|
|
if (out->content == NULL || out->calls == NULL)
|
|
return -1;
|
|
out->calls[0].id = strdup("c1");
|
|
out->calls[0].name = strdup("echo");
|
|
out->calls[0].args = strdup(
|
|
"{\"command\":\"echo one\"}{\"command\":\"echo two\"}");
|
|
out->calls_json = strdup("[{\"id\":\"c1\",\"type\":\"function\","
|
|
"\"function\":{\"name\":\"echo\",\"arguments\":"
|
|
"\"{\\\"command\\\":\\\"echo one\\\"}"
|
|
"{\\\"command\\\":\\\"echo two\\\"}\"}}]");
|
|
return out->calls[0].id != NULL && out->calls[0].name != NULL &&
|
|
out->calls[0].args != NULL && out->calls_json != NULL ? 0 : -1;
|
|
}
|
|
|
|
/* one reply with two calls: "zap" breaks the session log, then "echo" */
|
|
static int two_calls_n;
|
|
static int
|
|
two_calls_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
(void)ctx;
|
|
(void)conv;
|
|
(void)err;
|
|
two_calls_n++;
|
|
memset(out, 0, sizeof(*out));
|
|
out->status = 200;
|
|
out->content = strdup("");
|
|
out->ncalls = 2;
|
|
out->calls = calloc(2, sizeof(*out->calls));
|
|
if (out->content == NULL || out->calls == NULL)
|
|
return -1;
|
|
out->calls[0].id = strdup("c1");
|
|
out->calls[0].name = strdup("zap");
|
|
out->calls[0].args = strdup("x");
|
|
out->calls[1].id = strdup("c2");
|
|
out->calls[1].name = strdup("echo");
|
|
out->calls[1].args = strdup("y");
|
|
out->calls_json = strdup("[]");
|
|
return 0;
|
|
}
|
|
|
|
/* removes the session directory: the observation can no longer be logged */
|
|
static int
|
|
zap_tool(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
const char *dir = ctx;
|
|
char path[700];
|
|
|
|
(void)args;
|
|
snprintf(path, sizeof(path), "%s/t.jsonl", dir);
|
|
(void)unlink(path);
|
|
snprintf(path, sizeof(path), "%s/t.meta", dir);
|
|
(void)unlink(path);
|
|
(void)rmdir(dir);
|
|
return sp_buf_puts(out, "zapped");
|
|
}
|
|
|
|
static char note_text[256];
|
|
static int note_seen;
|
|
|
|
static void
|
|
test_note(void *ctx, const char *text)
|
|
{
|
|
(void)ctx;
|
|
if (text != NULL && *text != '\0') {
|
|
snprintf(note_text, sizeof(note_text), "%s", text);
|
|
note_seen++;
|
|
}
|
|
}
|
|
|
|
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;
|
|
cfg.note = test_note;
|
|
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,
|
|
* but not before three retries: four 500s are queued and four
|
|
* requests must be made. The wait is 0 here; the default is 5 s.
|
|
*/
|
|
(void)setenv("SPLINTER_RETRY_WAIT_MS", "0", 1);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 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);
|
|
CHECK(m.requests == 4);
|
|
sp_mock_free(&m);
|
|
|
|
/* 9b. two transient failures, then the answer: the retry recovers */
|
|
note_seen = 0;
|
|
note_text[0] = '\0';
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
|
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer nach zwei fehlern") == 0);
|
|
CHECK(run_turn(&m, &r, "hi", 1, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strstr(answer.p, "nach zwei fehlern") != NULL);
|
|
CHECK(m.requests == 3);
|
|
/* the operator saw why it waited, and the note is cleared after it */
|
|
CHECK(note_seen >= 1);
|
|
CHECK(strstr(note_text, "retrying in") != NULL);
|
|
sp_mock_free(&m);
|
|
(void)unsetenv("SPLINTER_RETRY_WAIT_MS");
|
|
|
|
/*
|
|
* 10. max_steps counts tool rounds, not requests: a turn that spends
|
|
* its whole round budget and then answers must still answer. The
|
|
* shape is the one from session sess-1794213769-672784 (2026-11-09
|
|
* 08:45:38): eight requests, every one of them a tool round, no
|
|
* answer - it died on "step limit (8)". Eight rounds leave the
|
|
* ninth request for the answer.
|
|
*/
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r1") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r2") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r3") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r4") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r5") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r6") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r7") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "tool echo r8") == 0);
|
|
CHECK(sp_mock_push(&m, 200, "answer done") == 0);
|
|
CHECK(run_turn(&m, &r, "eight rounds", 1, 8, 0, &answer, &err, &st) == 0);
|
|
CHECK(strcmp(answer.p, "done") == 0);
|
|
CHECK(st.tool_calls == 8);
|
|
CHECK(m.requests == 9);
|
|
CHECK(st.steps == 9);
|
|
sp_mock_free(&m);
|
|
|
|
/*
|
|
* 11. one tool round more than the budget is still refused, and the
|
|
* refused round does not run: the last observation in the request
|
|
* is round two's, not round three's.
|
|
*/
|
|
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, "too many rounds", 1, 2, 0, &answer, &err, &st) != 0);
|
|
CHECK(strstr(err.p, "step limit") != NULL);
|
|
CHECK(st.tool_calls == 2);
|
|
CHECK(m.requests == 3);
|
|
CHECK(strstr(m.last_request.p, "observe echo b") != NULL);
|
|
CHECK(strstr(m.last_request.p, "observe echo c") == NULL);
|
|
sp_mock_free(&m);
|
|
|
|
/*
|
|
* A stream that only carried reasoning and then ended without an
|
|
* answer must be retried: the thinking is not an answer, and aborting
|
|
* without a retry loses the turn.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
|
|
(void)setenv("SPLINTER_RETRY_WAIT_MS", "0", 1);
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 4;
|
|
cfg.autonomous = 1;
|
|
cfg.stream = no_stream;
|
|
tr.send_stream = flaky_stream;
|
|
a.reg = &r;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
flaky_calls = 0;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_agent_run(&a, "hi", &answer, &err) == 0);
|
|
CHECK(flaky_calls == 2);
|
|
CHECK(strstr(answer.p, "answer after the retry") != NULL);
|
|
sp_conv_free(a.conv);
|
|
(void)unsetenv("SPLINTER_RETRY_WAIT_MS");
|
|
}
|
|
|
|
/*
|
|
* A tool call whose arguments look like an object but are not JSON
|
|
* must not enter the conversation. It made all endpoints answer 500
|
|
* on the next request; the loop retries the model instead and then
|
|
* fails cleanly.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
tr.send = bad_args_send;
|
|
a.reg = &r;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
bad_args_calls = 0;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
/*
|
|
* Five attempts with a correction each, then one request that
|
|
* asks for words: the turn ends with an answer, not with an
|
|
* error the operator cannot act on.
|
|
*/
|
|
CHECK(sp_agent_run(&a, "bad args", &answer, &err) == 0);
|
|
CHECK(bad_args_calls == 6);
|
|
CHECK(strstr(answer.p, "nothing ran") != NULL);
|
|
CHECK(a.stats.tool_calls == 0);
|
|
CHECK(a.conv == NULL || a.conv->n == 0 ||
|
|
strstr(a.conv->v[a.conv->n - 1].content != NULL ?
|
|
a.conv->v[a.conv->n - 1].content : "", "command:bad") == NULL);
|
|
/*
|
|
* The retry is not the same prompt: it carries the arguments
|
|
* the model got wrong and says what was wrong with them.
|
|
*/
|
|
CHECK(bad_args_second.len > 0);
|
|
CHECK(strstr(bad_args_second.p, "{command:bad}") != NULL);
|
|
CHECK(strstr(bad_args_second.p, "not valid JSON") != NULL);
|
|
sp_conv_free(a.conv);
|
|
}
|
|
|
|
/*
|
|
* Two argument objects in one string are not a tool call either: the
|
|
* first would run and the second vanish without a word. Reject the
|
|
* reply and let the model send one call at a time.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
tr.send = joined_args_send;
|
|
a.reg = &r;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
joined_args_calls = 0;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_agent_run(&a, "joined args", &answer, &err) == 0);
|
|
CHECK(joined_args_calls == 6);
|
|
CHECK(strstr(answer.p, "nothing ran") != NULL);
|
|
CHECK(a.stats.tool_calls == 0);
|
|
sp_conv_free(a.conv);
|
|
}
|
|
|
|
/*
|
|
* A call cut off by the token limit ("finish_reason": "length") is not
|
|
* a model that cannot write JSON: the same request truncates it again.
|
|
* The correction has to ask for a smaller call, and the turn still
|
|
* ends with words after five tries.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
|
|
(void)setenv("SPLINTER_RETRY_WAIT_MS", "0", 1);
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
tr.send = cut_off_send;
|
|
a.reg = &r;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
cut_off_calls = 0;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_agent_run(&a, "cut off", &answer, &err) == 0);
|
|
CHECK(cut_off_calls == 6);
|
|
CHECK(strstr(answer.p, "did not fit") != NULL);
|
|
/*
|
|
* The correction names the limit, not just "invalid JSON": only
|
|
* the first sentence tells the model what to do differently.
|
|
*/
|
|
CHECK(strstr(cut_off_second.p, "cut off by the") != NULL);
|
|
CHECK(strstr(cut_off_second.p, "smaller call") != NULL);
|
|
CHECK(a.stats.tool_calls == 0);
|
|
sp_conv_free(a.conv);
|
|
sp_buf_free(&cut_off_second);
|
|
(void)unsetenv("SPLINTER_RETRY_WAIT_MS");
|
|
}
|
|
|
|
/*
|
|
* A tool-call step that reports no usage does not empty the context:
|
|
* zero there is "unknown", not "the prompt became empty". The status
|
|
* bar read a bare last_in, so it dropped to 0 after every tool call
|
|
* (seen on olivia over Telegram). The last known prompt size has to
|
|
* survive the step that did not report one.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
tr.send = usage_gap_send;
|
|
a.reg = &r;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
usage_gap_calls = 0;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_agent_run(&a, "count the context", &answer, &err) == 0);
|
|
CHECK(usage_gap_calls == 3);
|
|
/* 9000 from the step that reported, kept through the silent one */
|
|
CHECK(a.stats.last_in == 9000);
|
|
CHECK(a.stats.last_cached == 4000);
|
|
/*
|
|
* The sum still grows by what was reported: the guard is about
|
|
* the meter, not about the usage total.
|
|
*/
|
|
CHECK(a.stats.tokens_in == 1000 + 9000);
|
|
sp_conv_free(a.conv);
|
|
}
|
|
|
|
/*
|
|
* A round that fails between its calls still answers every call:
|
|
* an assistant message with two tool_calls and one result made each
|
|
* later request fail at the endpoint.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
struct sp_session_info si;
|
|
struct sp_conv c;
|
|
struct sp_registry r2;
|
|
struct sp_tool t;
|
|
char tmpl[] = "/tmp/splinter-agent-XXXXXX";
|
|
char *dir = mkdtemp(tmpl);
|
|
|
|
CHECK(dir != NULL);
|
|
setup(&r2);
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "zap";
|
|
t.description = "break the session log";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = zap_tool;
|
|
t.ctx = dir;
|
|
(void)sp_registry_add(&r2, &t);
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
sp_conv_init(&c);
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
tr.send = two_calls_send;
|
|
a.reg = &r2;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
a.conv = &c;
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
if (dir != NULL &&
|
|
sp_session_open(&si, dir, "t", &err) == 0) {
|
|
a.sess = &si;
|
|
CHECK(sp_agent_run(&a, "two calls", &answer, &err) != 0);
|
|
CHECK(two_calls_n == 1);
|
|
/* user, assistant(calls), tool c1, tool c2 */
|
|
CHECK(c.n == 4);
|
|
if (c.n == 4) {
|
|
CHECK(strcmp(c.v[2].role, "tool") == 0);
|
|
CHECK(strcmp(c.v[2].call_id, "c1") == 0);
|
|
CHECK(strcmp(c.v[3].role, "tool") == 0);
|
|
CHECK(strcmp(c.v[3].call_id, "c2") == 0);
|
|
CHECK(strstr(c.v[3].content, "not run") != NULL);
|
|
}
|
|
} else
|
|
CHECK(0);
|
|
sp_conv_free(&c);
|
|
sp_registry_free(&r2);
|
|
}
|
|
|
|
/*
|
|
* 13. A conversation over max_bytes is compacted, not refused. The core
|
|
* compactor lives behind the host; before this test the engine built
|
|
* the host only when a plugin was configured, so a plugin-less run
|
|
* had no compactor and every long conversation died on the ceiling
|
|
* with "budget exceeded". Long input, small budget, the turn must go
|
|
* through and the conversation must come out smaller.
|
|
*/
|
|
{
|
|
struct sp_agent a;
|
|
struct sp_agent_config cfg;
|
|
struct sp_transport tr;
|
|
struct sp_conv c;
|
|
struct sp_host *h;
|
|
struct sp_registry r3;
|
|
size_t i, before, after;
|
|
|
|
memset(&r3, 0, sizeof(r3));
|
|
setup(&r3);
|
|
CHECK(sp_mock_init(&m) == 0);
|
|
for (i = 0; i < 6; i++)
|
|
CHECK(sp_mock_push(&m, 200, "answer ack") == 0);
|
|
h = sp_host_new(&r3, NULL, NULL);
|
|
CHECK(h != NULL);
|
|
sp_conv_init(&c);
|
|
for (i = 0; i < 24; i++)
|
|
CHECK(sp_conv_push(&c, i % 2 ? "assistant" : "user",
|
|
NULL, "a piece of the conversation that costs bytes, "
|
|
"long enough to matter once there are a few of them")
|
|
== 0);
|
|
memset(&a, 0, sizeof(a));
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
memset(&tr, 0, sizeof(tr));
|
|
cfg.max_steps = 8;
|
|
cfg.autonomous = 1;
|
|
cfg.max_bytes = 2400;
|
|
tr = sp_mock_transport(&m);
|
|
a.reg = &r3;
|
|
a.cfg = &cfg;
|
|
a.tr = tr;
|
|
a.conv = &c;
|
|
a.host = h;
|
|
before = sp_conv_bytes(&c);
|
|
CHECK(before > cfg.max_bytes);
|
|
sp_buf_clear(&answer);
|
|
sp_buf_clear(&err);
|
|
CHECK(sp_agent_run(&a, "go on", &answer, &err) == 0);
|
|
after = sp_conv_bytes(&c);
|
|
CHECK(after < before);
|
|
CHECK(a.stats.compactions > 0);
|
|
sp_conv_free(&c);
|
|
sp_host_free(h);
|
|
sp_registry_free(&r3);
|
|
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;
|
|
}
|