Files
splinter/tests/test_cli.c
T
lotherkandClaude Opus 5.5 808039cd58 cli: the main transport falls back at the limit, too
cli_fallback = claude1, claude0 names cli delegates whose cmd and env=
take over when cli_cmd answers with its limit. The main command is
benched as "cli", a fallback under its own id, on the same bench the
delegation uses (sp_limit_parse, sp_cooldown_until, sp_cooldown_set are
now exported from libsplinter). The session key stays, so claude
resumes the same conversation under the next account. With everyone
benched the turn fails with the first reset, as WAIT_QUOTA ready_at=.

A fallback's env= goes through a 0600 file the shell sources, not a
command line. A delegate's child is benched under the delegate's id
(SPLINTER_CLI_ID), never as "cli".

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

370 lines
11 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"
#include "config.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);
/*
* ---- the main cli at its limit falls back to cli delegates ----
* acct answers with the token it was given, or with claude's limit
* when that token is listed as spent.
*/
{
struct sp_config conf;
struct sp_host *hf;
char cool[300], sp[400];
write_exec("acct",
"#!/bin/sh\n"
"D=$(dirname \"$0\")\n"
"echo \"$TOK\" >> \"$D/acctlog\"\n"
"if grep -qx \"$TOK\" \"$D/spent\" 2>/dev/null; then\n"
" echo \"You've hit your weekly limit · resets 3am"
" (Europe/Lisbon)\" >&2; exit 1\n"
"fi\n"
"cat > /dev/null; echo \"ok $TOK\"\n");
snprintf(cool, sizeof(cool), "%s/cool", tdir);
setenv("SPLINTER_COOLDOWN_DIR", cool, 1);
setenv("TOK", "zero", 1);
setenv("SPLINTER_CLI_SESSION", "", 1);
snprintf(sp, sizeof(sp), "%s/spent", tdir);
sp_config_init(&conf);
memset(&conf.delegate[0], 0, sizeof(conf.delegate[0]));
snprintf(conf.delegate[0].id, 32, "acc1");
snprintf(conf.delegate[0].kind, 16, "cli");
snprintf(conf.delegate[0].cmd, 256, "acct");
snprintf(conf.delegate[0].env[0], SP_CONFIG_ENV_LEN,
"TOK=one 'quoted'");
conf.delegate[0].nenv = 1;
conf.delegate[1] = conf.delegate[0];
snprintf(conf.delegate[1].id, 32, "acc2");
snprintf(conf.delegate[1].env[0], SP_CONFIG_ENV_LEN,
"TOK=two");
conf.ndelegates = 2;
snprintf(conf.cli_fallback, sizeof(conf.cli_fallback),
"acc1, acc2");
setenv("SPLINTER_CLI_CMD", "acct", 1);
hf = sp_host_new(&reg, &ag, NULL);
CHECK(hf != NULL);
sp_host_set_config(hf, &conf);
CHECK(sp_plugin_load(hf, "plugins/cli/cli.so", &err) == 0);
/* the main account is spent: acc1 answers, its token arrives */
write_exec("spent", "zero\n");
memset(&rep, 0, sizeof(rep));
sp_buf_clear(&err);
CHECK(one_turn(hf, "a", &rep, &err) == 0);
CHECK(rep.content != NULL &&
strncmp(rep.content, "ok one 'quoted'", 15) == 0);
sp_reply_free(&rep);
snprintf(path, sizeof(path), "%s/cli", cool);
CHECK(access(path, F_OK) == 0);
/* benched: the next turn does not ask the main account */
snprintf(path, sizeof(path), "%s/acctlog", tdir);
(void)unlink(path);
memset(&rep, 0, sizeof(rep));
CHECK(one_turn(hf, "b", &rep, &err) == 0);
sp_reply_free(&rep);
{
FILE *f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
CHECK(fgets(line, sizeof(line), f) != NULL &&
strcmp(line, "one 'quoted'\n") == 0);
CHECK(fgets(line, sizeof(line), f) == NULL);
fclose(f);
}
}
/* everyone spent: an error that says so and when */
write_exec("spent", "zero\none 'quoted'\ntwo\n");
memset(&rep, 0, sizeof(rep));
sp_buf_clear(&err);
CHECK(one_turn(hf, "c", &rep, &err) == -1);
CHECK(strstr(err.p, "every account at its usage limit") != NULL);
CHECK(strstr(err.p, "ready_at=") != NULL);
snprintf(path, sizeof(path), "%s/acc2", cool);
CHECK(access(path, F_OK) == 0);
sp_reply_free(&rep);
/* no env file is left behind */
snprintf(cmd, sizeof(cmd), "ls /var/tmp/splinter-cli-*.env "
"2>/dev/null | grep -q .");
CHECK(system(cmd) != 0);
sp_host_free(hf);
unsetenv("SPLINTER_COOLDOWN_DIR");
}
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;
}