Files
splinter/tests/test_anthropic.c
lotherk 21287b88a9 agent: free the reply of a terminal failure, and make the tests leak-clean
ASAN over the unit tests found a real leak the normal gate cannot see: a
transport failure that is not retried (the fourth 500, a 4xx, a dead stream)
returned -1 with the last reply's body and calls still allocated. The caller
only reads the reply on success, so it leaked on every terminal failure -- a
long-running daemon with a flapping provider grows by one reply per failure.
test_agent's four-500 case leaked exactly the last "boom"; it is clean now.

The same run showed the tests leaking their own fixtures: test_anthropic and
test_tasks never freed the registry they filled, test_cli forgot host4 and
host5, test_confblock forgot its error buffer, test_mcpclient its registries.
Fixed; all 39 unit binaries are now clean under ASAN+UBSAN with leak detection.

That gate is `make check-asan`: it rebuilds the tree with the sanitizers, runs
the unit binaries with LSAN, and rebuilds normally afterwards. Three MCP-plugin
teardown allocations are suppressed in tests/asan.supp: its server thread is
detached, shutdown kills the child but cannot join the thread, so the line
buffer (bounded at RPC_MAX_LINE) and the tools registered by that thread can
outlive the host. Bounded, teardown-only, tracked separately.

make check: all tests passed. make check-asan: all 39 test binaries clean.
2026-11-18 23:16:26 +00:00

302 lines
11 KiB
C

/*
* splinter -- Anthropic Messages API shaping, without a network.
*
* Four things must hold, and each is checked against a body a real endpoint
* accepts (the shapes here were verified against api.anthropic.com):
*
* - the system prompt is the top-level `system` field, not a message;
* - a tool call is a `tool_use` block and its answer a `tool_result`;
* - observations of one parallel round merge into one user message, because
* the Messages API wants alternating roles;
* - a stream of events becomes the same reply the non-streaming body does.
*/
#include <stdio.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);
}
int
main(void)
{
struct sp_conv conv;
struct sp_registry reg;
struct sp_buf req, err, delta;
struct sp_reply rep;
struct sp_tool t;
struct sp_as_sse *sse;
sp_conv_init(&conv);
memset(&reg, 0, sizeof(reg));
memset(&t, 0, sizeof(t));
t.name = "read";
t.description = "read a file";
t.auth = SP_AUTH_READ;
t.params = "{\"type\":\"object\",\"properties\":{\"path\":"
"{\"type\":\"string\"}},\"required\":[\"path\"]}";
t.run = echo_tool;
(void)sp_registry_add(&reg, &t);
if (sp_buf_init(&req) != 0 || sp_buf_init(&err) != 0 ||
sp_buf_init(&delta) != 0)
return 1;
/* ---- request: system is a field, tools use input_schema ---- */
CHECK(sp_conv_push(&conv, "system", NULL, "be brief") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "say \"hi\"") == 0);
CHECK(sp_anthropic_request(&conv, "claude-x", &reg, 0, 0, &req) == 0);
CHECK(strstr(req.p, "\"model\":\"claude-x\"") != NULL);
CHECK(strstr(req.p, "\"max_tokens\":") != NULL);
CHECK(strstr(req.p, "\"system\":[{\"type\":\"text\",\"text\":"
"\"be brief\"}]") != NULL);
CHECK(strstr(req.p, "\"role\":\"system\"") == NULL);
CHECK(strstr(req.p, "\"content\":[{\"type\":\"text\","
"\"text\":\"say \\\"hi\\\"\"}]") != NULL);
CHECK(strstr(req.p, "\"stream\":false") != NULL);
CHECK(strstr(req.p, "\"name\":\"read\"") != NULL);
CHECK(strstr(req.p, "\"input_schema\":") != NULL);
CHECK(strstr(req.p, "\"parameters\"") == NULL);
/* streaming flips the flag and nothing else */
CHECK(sp_anthropic_request(&conv, "claude-x", &reg, 1, 0, &req) == 0);
CHECK(strstr(req.p, "\"stream\":true") != NULL);
/*
* An OAuth token needs Claude Code's first system block, because the API
* refuses the token otherwise. The operator's own prompt follows it.
*/
CHECK(sp_anthropic_request(&conv, "claude-x", &reg, 0, 1, &req) == 0);
CHECK(strstr(req.p, "\"system\":[{\"type\":\"text\",\"text\":"
"\"You are a Claude agent, built on Anthropic's Claude Agent SDK.\"},"
"{\"type\":\"text\",\"text\":\"be brief\"}]") != NULL);
/* ---- a tool round: tool_use in, tool_result back ---- */
{
struct sp_msg am;
memset(&am, 0, sizeof(am));
am.role = "assistant";
am.content = "I will read it.";
am.calls = "[{\"id\":\"toolu_01\",\"name\":\"read\","
"\"input\":{\"path\":\"/etc/hosts\"}}]";
CHECK(sp_conv_add(&conv, &am) == 0);
}
{
struct sp_msg tm;
memset(&tm, 0, sizeof(tm));
tm.role = "tool";
tm.name = "read";
tm.call_id = "toolu_01";
tm.content = "127.0.0.1 localhost";
CHECK(sp_conv_add(&conv, &tm) == 0);
}
CHECK(sp_anthropic_request(&conv, "claude-x", &reg, 0, 0, &req) == 0);
CHECK(strstr(req.p, "\"type\":\"tool_use\"") != NULL);
CHECK(strstr(req.p, "\"input\":{\"path\":\"/etc/hosts\"}") != NULL);
CHECK(strstr(req.p, "\"type\":\"tool_result\"") != NULL);
CHECK(strstr(req.p, "\"tool_use_id\":\"toolu_01\"") != NULL);
CHECK(strstr(req.p, "\"content\":[{\"type\":\"text\",\"text\":"
"\"127.0.0.1 localhost\"}]") != NULL);
/* a second observation of the same round merges into that user message */
{
struct sp_msg am, tm;
memset(&am, 0, sizeof(am));
am.role = "assistant";
am.calls = "[{\"id\":\"toolu_02\",\"name\":\"read\","
"\"input\":{\"path\":\"/etc/hostname\"}},"
"{\"id\":\"toolu_03\",\"name\":\"read\","
"\"input\":{\"path\":\"/etc/timezone\"}}]";
CHECK(sp_conv_add(&conv, &am) == 0);
memset(&tm, 0, sizeof(tm));
tm.role = "tool";
tm.call_id = "toolu_02";
tm.content = "box\n";
CHECK(sp_conv_add(&conv, &tm) == 0);
memset(&tm, 0, sizeof(tm));
tm.role = "tool";
tm.call_id = "toolu_03";
tm.content = "error: no such file";
CHECK(sp_conv_add(&conv, &tm) == 0);
}
CHECK(sp_anthropic_request(&conv, "claude-x", &reg, 0, 0, &req) == 0);
CHECK(strstr(req.p, "\"tool_use_id\":\"toolu_02\"") != NULL);
CHECK(strstr(req.p, "\"tool_use_id\":\"toolu_03\"") != NULL);
CHECK(strstr(req.p, "\"is_error\":true") != NULL);
/* one user message carries both results: count the role markers */
{
const char *p = req.p;
int users = 0, assistants = 0;
while ((p = strstr(p, "\"role\":\"")) != NULL) {
if (strncmp(p + 8, "user\"", 5) == 0)
users++;
else if (strncmp(p + 8, "assistant\"", 10) == 0)
assistants++;
p += 8;
}
CHECK(users == 3); /* the question, then one per round */
CHECK(assistants == 2);
}
/* ---- reply: blocks become text and calls ---- */
CHECK(sp_anthropic_reply("{\"type\":\"message\",\"content\":["
"{\"type\":\"text\",\"text\":\"Let me look.\"},"
"{\"type\":\"tool_use\",\"id\":\"toolu_9\",\"name\":\"read\","
"\"input\":{\"path\":\"/etc/hosts\"}}],"
"\"usage\":{\"input_tokens\":11,\"output_tokens\":7}}",
&rep, &err) == 0);
CHECK(strcmp(rep.content, "Let me look.") == 0);
CHECK(rep.ncalls == 1);
CHECK(strcmp(rep.calls[0].id, "toolu_9") == 0);
CHECK(strcmp(rep.calls[0].name, "read") == 0);
CHECK(strcmp(rep.calls[0].args, "{\"path\":\"/etc/hosts\"}") == 0);
CHECK(strstr(rep.calls_json, "\"input\":{\"path\":\"/etc/hosts\"}") !=
NULL);
CHECK(rep.tokens_in == 11 && rep.tokens_out == 7);
sp_reply_free(&rep);
/*
* input_tokens excludes the cache reads and writes; the context is the
* sum, the cache share is the read part.
*/
CHECK(sp_anthropic_reply("{\"type\":\"message\",\"content\":["
"{\"type\":\"text\",\"text\":\"ok\"}],"
"\"usage\":{\"input_tokens\":10,\"cache_read_input_tokens\":100,"
"\"cache_creation_input_tokens\":20,\"output_tokens\":3}}",
&rep, &err) == 0);
CHECK(rep.tokens_in == 130 && rep.tokens_out == 3);
CHECK(rep.tokens_cached == 100);
sp_reply_free(&rep);
/* a provider error is quoted, not turned into an empty answer */
CHECK(sp_anthropic_reply("{\"type\":\"error\",\"error\":"
"{\"type\":\"rate_limit_error\",\"message\":\"slow down\"}}",
&rep, &err) != 0);
CHECK(strstr(err.p, "slow down") != NULL);
/* ---- stream: the same reply, one event at a time ---- */
sse = sp_as_sse_new();
CHECK(sse != NULL);
CHECK(sp_as_sse_line(sse, "event: message_start", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"message_start\","
"\"message\":{\"usage\":{\"input_tokens\":21,"
"\"cache_read_input_tokens\":100,"
"\"cache_creation_input_tokens\":20}}}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_start\","
"\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}",
&delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"Hel\"}}",
&delta, &err) == SP_STREAM_TEXT);
CHECK(strcmp(delta.p, "Hel") == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"lo\"}}",
&delta, &err) == SP_STREAM_TEXT);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_start\","
"\"index\":1,\"content_block\":{\"type\":\"tool_use\","
"\"id\":\"toolu_1\",\"name\":\"read\",\"input\":{}}}",
&delta, &err) == SP_STREAM_TOOL);
CHECK(strcmp(delta.p, "read") == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":1,\"delta\":{\"type\":\"input_json_delta\","
"\"partial_json\":\"{\\\"path\\\":\"}}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":1,\"delta\":{\"type\":\"input_json_delta\","
"\"partial_json\":\"\\\"/tmp\\\"}\"}}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_stop\","
"\"index\":1}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"message_delta\","
"\"delta\":{\"stop_reason\":\"tool_use\"},"
"\"usage\":{\"output_tokens\":9}}", &delta, &err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"message_stop\"}", &delta,
&err) == 0);
CHECK(sp_as_sse_done(sse));
CHECK(sp_as_sse_finish(sse, &rep, &err) == 0);
CHECK(strcmp(rep.content, "Hello") == 0);
CHECK(rep.ncalls == 1);
CHECK(strcmp(rep.calls[0].args, "{\"path\":\"/tmp\"}") == 0);
CHECK(rep.tokens_in == 141 && rep.tokens_out == 9);
CHECK(rep.tokens_cached == 100);
sp_reply_free(&rep);
sp_as_sse_free(sse);
/* reasoning arrives as its own kind: shown, never used as the answer */
sse = sp_as_sse_new();
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_start\","
"\"index\":0,\"content_block\":{\"type\":\"thinking\"}}", &delta,
&err) == 0);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":0,\"delta\":{\"type\":\"thinking_delta\","
"\"thinking\":\"hmm\"}}", &delta, &err) == SP_STREAM_THINK);
CHECK(sp_as_sse_finish(sse, &rep, &err) == 0);
CHECK(strcmp(rep.content, "") == 0);
sp_reply_free(&rep);
sp_as_sse_free(sse);
/*
* a tool_use at block index 20 (after text and thinking blocks) is
* call 0, not "too many tool calls"
*/
sse = sp_as_sse_new();
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_start\","
"\"index\":20,\"content_block\":{\"type\":\"tool_use\","
"\"id\":\"toolu_9\",\"name\":\"read\",\"input\":{}}}",
&delta, &err) == SP_STREAM_TOOL);
CHECK(sp_as_sse_line(sse, "data: {\"type\":\"content_block_delta\","
"\"index\":20,\"delta\":{\"type\":\"input_json_delta\","
"\"partial_json\":\"{}\"}}", &delta, &err) == 0);
CHECK(sp_as_sse_finish(sse, &rep, &err) == 0);
CHECK(rep.ncalls == 1 && strcmp(rep.calls[0].id, "toolu_9") == 0 &&
strcmp(rep.calls[0].args, "{}") == 0);
sp_reply_free(&rep);
sp_as_sse_free(sse);
/* two user turns in a row (the first one failed): one message, both blocks */
{
struct sp_conv c2;
struct sp_buf r2;
sp_conv_init(&c2);
CHECK(sp_buf_init(&r2) == 0);
CHECK(sp_conv_push(&c2, "user", NULL, "first") == 0);
CHECK(sp_conv_push(&c2, "user", NULL, "again") == 0);
CHECK(sp_conv_push(&c2, "assistant", NULL, "ok") == 0);
CHECK(sp_anthropic_request(&c2, "claude-x", &reg, 0, 0, &r2) == 0);
CHECK(strstr(r2.p, "\"messages\":[{\"role\":\"user\",\"content\":["
"{\"type\":\"text\",\"text\":\"first\"},{\"type\":\"text\","
"\"text\":\"again\"}]},{\"role\":\"assistant\"") != NULL);
/* no block may stand outside a message */
CHECK(strstr(r2.p, "]},{\"type\"") == NULL);
sp_buf_free(&r2);
sp_conv_free(&c2);
}
sp_buf_free(&req);
sp_buf_free(&err);
sp_buf_free(&delta);
sp_conv_free(&conv);
sp_registry_free(&reg);
if (failures == 0) {
printf("test_anthropic: ok\n");
return 0;
}
return 1;
}