Files
splinter/tests/test_cli.c
T
lotherkandClaude Opus 5.5 60475016ae cli: one session per splinter session; delegates: bench at the limit, fall back
The cli transport keys the program's own session on SPLINTER_CLI_SESSION
(the splinter session id, plus ':<delegate>' for a delegate child):
claude gets a derived UUID, --session-id first and --resume after, with
one retry when claude disagrees; opencode gets --session ses_<derived>;
other programs can use {session} and {uuid}. Consecutive calls continue
one conversation and hit the provider cache (measured: 17.6k read, 80
written on the second call).

A delegate whose answer is a limit (429, rate/usage/weekly limit, quota)
is benched until the reset it names - claude's "resets 3am
(Europe/Lisbon)" is read through date(1) - in ~/.splinter/cooldown/<id>,
and the task goes to the next delegate that fits. choose() skips benched
delegates, also when one is asked for by name.

run_child now carries the tail of the child's output in its error and
treats a signal as failure; "exited 1" alone hid every limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 20:33:17 +00:00

277 lines
8.1 KiB
C

/*
* splinter -- the cli transport: any program as the model.
*
* Deterministic: the "model" here is /bin/sh running sed, so a red test means
* the transport is wrong, not an endpoint. What is checked: the prompt reaches
* the program's stdin, its stdout becomes the answer, a program that prints
* nothing is an error, and a non-zero exit carries the program's own words.
*
* Sessions: a fake claude that behaves like the real one about ids (refuses
* --session-id for a session it has, --resume for one it lacks) shows that
* consecutive calls land in one session, that a lost marker heals, and that
* another splinter session gets another id. A fake opencode shows the same
* for --session.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.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 char tdir[256];
static void
write_exec(const char *name, const char *body)
{
char path[400];
FILE *f;
snprintf(path, sizeof(path), "%s/%s", tdir, name);
f = fopen(path, "w");
CHECK(f != NULL);
if (f == NULL)
return;
fputs(body, f);
fclose(f);
CHECK(chmod(path, 0755) == 0);
}
/* the last line of the fake's argument log */
static void
last_log(char *buf, size_t len)
{
char path[400], line[512];
FILE *f;
buf[0] = '\0';
snprintf(path, sizeof(path), "%s/log", tdir);
f = fopen(path, "r");
if (f == NULL)
return;
while (fgets(line, sizeof(line), f) != NULL) {
line[strcspn(line, "\n")] = '\0';
snprintf(buf, len, "%s", line);
}
fclose(f);
}
static struct sp_host *
host_for(struct sp_registry *reg, struct sp_agent *ag, const char *cmd,
struct sp_buf *err)
{
struct sp_host *h;
setenv("SPLINTER_CLI_CMD", cmd, 1);
h = sp_host_new(reg, ag, NULL);
CHECK(h != NULL);
if (h != NULL)
CHECK(sp_plugin_load(h, "plugins/cli/cli.so", err) == 0);
return h;
}
static int
one_turn(struct sp_host *host, const char *text, struct sp_reply *out,
struct sp_buf *err)
{
struct sp_transport *tr;
struct sp_conv conv;
tr = sp_host_transport(host, "cli");
CHECK(tr != NULL);
if (tr == NULL)
return -1;
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, text) == 0);
{
int rc = tr->send(tr->ctx, &conv, out, err);
sp_conv_free(&conv);
return rc;
}
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host, *host2, *host3;
struct sp_buf err;
struct sp_reply rep;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
setenv("SPLINTER_CLI_CMD", "sed 's/^/X: /'", 1);
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/cli/cli.so", &err) == 0);
CHECK(sp_host_transport(host, "cli") != NULL);
CHECK(sp_host_transport(host, "nope") == NULL);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host, "hallo", &rep, &err) == 0);
CHECK(rep.status == 200);
CHECK(rep.content != NULL && strncmp(rep.content, "X: hallo", 8) == 0);
sp_reply_free(&rep);
/* a program that prints nothing must not look like an empty answer */
/* a fresh host: the transport name is taken once per host, on purpose */
setenv("SPLINTER_CLI_CMD", "true", 1);
host2 = sp_host_new(&reg, &ag, NULL);
CHECK(host2 != NULL);
CHECK(sp_plugin_load(host2, "plugins/cli/cli.so", &err) == 0);
sp_buf_clear(&err);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host2, "hallo", &rep, &err) == -1);
CHECK(strstr(err.p, "no output") != NULL);
sp_reply_free(&rep);
/* a failing program says so, with its own output attached */
setenv("SPLINTER_CLI_CMD", "sh -c 'echo kaputt >&2; exit 3'", 1);
host3 = sp_host_new(&reg, &ag, NULL);
CHECK(host3 != NULL);
CHECK(sp_plugin_load(host3, "plugins/cli/cli.so", &err) == 0);
sp_buf_clear(&err);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(host3, "hallo", &rep, &err) == -1);
CHECK(strstr(err.p, "exit 3") != NULL);
CHECK(strstr(err.p, "kaputt") != NULL);
sp_reply_free(&rep);
/* ---- sessions ---- */
{
struct sp_host *hc, *ho, *hp;
char line[512], first[512], path[1024], cmd[1100];
const char *oldpath = getenv("PATH");
char *u;
snprintf(tdir, sizeof(tdir), "/tmp/splinter-cli-test-XXXXXX");
CHECK(mkdtemp(tdir) != NULL);
write_exec("claude",
"#!/bin/sh\n"
"D=$(dirname \"$0\")\n"
"echo \"$*\" >> \"$D/log\"\n"
"id=none\n"
"while [ $# -gt 0 ]; do\n"
" case $1 in\n"
" --session-id) if [ -e \"$D/s.$2\" ]; then\n"
" echo \"Error: Session ID $2 is already in use.\" >&2;"
" exit 1; fi; : > \"$D/s.$2\"; id=$2; shift;;\n"
" --resume) [ -e \"$D/s.$2\" ] || { echo \"No conversation"
" found with session ID: $2\" >&2; exit 1; }; id=$2; shift;;\n"
" esac; shift\n"
"done\n"
"cat > /dev/null\n"
"echo \"ok $id\"\n");
write_exec("opencode",
"#!/bin/sh\n"
"echo \"$*\" >> \"$(dirname \"$0\")/log\"\n"
"cat > /dev/null; echo ok\n");
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
setenv("PATH", line, 1);
snprintf(path, sizeof(path), "%s/state", tdir);
setenv("SPLINTER_CLI_STATE", path, 1);
setenv("SPLINTER_CLI_SESSION", "sess-1:claude0", 1);
hc = host_for(&reg, &ag, "claude -p", &err);
/* first call starts the session */
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hc, "eins", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(first, sizeof(first));
CHECK(strncmp(first, "-p --session-id ", 16) == 0);
CHECK(strlen(first) == 16 + 36);
u = first + 16;
/* second call continues it */
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hc, "zwei", &rep, &err) == 0);
CHECK(rep.content != NULL && strstr(rep.content, u) != NULL);
sp_reply_free(&rep);
last_log(line, sizeof(line));
snprintf(cmd, sizeof(cmd), "-p --resume %s", u);
CHECK(strcmp(line, cmd) == 0);
/* the marker is gone, the session is not: one retry heals it */
snprintf(path, sizeof(path), "%s/state/%s", tdir, u);
CHECK(unlink(path) == 0);
memset(&rep, 0, sizeof(rep));
sp_buf_clear(&err);
CHECK(one_turn(hc, "drei", &rep, &err) == 0);
CHECK(rep.content != NULL && strstr(rep.content, u) != NULL);
sp_reply_free(&rep);
last_log(line, sizeof(line));
CHECK(strcmp(line, cmd) == 0);
CHECK(access(path, F_OK) == 0);
/* another session key, another id */
setenv("SPLINTER_CLI_SESSION", "sess-1:claude1", 1);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hc, "vier", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(line, sizeof(line));
CHECK(strncmp(line, "-p --session-id ", 16) == 0);
CHECK(strcmp(line + 16, u) != 0);
/* empty key: no session arguments at all */
setenv("SPLINTER_CLI_SESSION", "", 1);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hc, "fuenf", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(line, sizeof(line));
CHECK(strcmp(line, "-p") == 0);
/* a command that names its own session is left alone */
setenv("SPLINTER_CLI_SESSION", "sess-1:claude0", 1);
hp = host_for(&reg, &ag, "claude -p --resume mine", &err);
write_exec("s.mine", "");
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hp, "sechs", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(line, sizeof(line));
CHECK(strcmp(line, "-p --resume mine") == 0);
/* opencode: one ses_ id, the same on every call */
ho = host_for(&reg, &ag, "opencode run -m x/y", &err);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(ho, "a", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(first, sizeof(first));
CHECK(strncmp(first, "run -m x/y --session ses_", 25) == 0);
CHECK(strlen(first) == 21 + 30);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(ho, "b", &rep, &err) == 0);
sp_reply_free(&rep);
last_log(line, sizeof(line));
CHECK(strcmp(line, first) == 0);
sp_host_free(hp);
sp_host_free(ho);
sp_host_free(hc);
setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", tdir);
(void)system(cmd);
}
sp_host_free(host3);
sp_host_free(host2);
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_cli: ok\n");
return failures == 0 ? 0 : 1;
}