- lcm: the sidebar widget and /status segment show the context, the number of
compactions, messages and bytes of the last cut, and how many old tool
outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
sse/openai, the agent and the engine. /cost reports it with the cache share
and splits judged from plain turns; the notes say that an upstream judge is
not visible in the provider's usage. The status plugin and junction prefer
the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
bar. Notes and errors are transcript rows below the cause, never text in the
input line or the status bar; a failed backend request is a status row and is
retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
once, then asks the endpoint the session is on what it serves and adds those
models, so a router's whole menu is selectable. provider/model selectors now
apply the right provider instead of becoming a model name. The choice is
written to <session>.model and restored when that session is reopened; /new
carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
is still there before the window is extended. A long build that keeps
reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
restore and /model probe, cli idle timeout with a talkative and a silent
command, run output reset, and the UI status/retry rows.
make check: all tests passed.
438 lines
13 KiB
C
438 lines
13 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 <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.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];
|
|
|
|
/* the operator pressing Ctrl-C: set the turn's flag a moment after the start */
|
|
static volatile int cancel_now;
|
|
|
|
static void *
|
|
set_cancel_soon(void *ctx)
|
|
{
|
|
struct timespec ts;
|
|
|
|
(void)ctx;
|
|
ts.tv_sec = 0;
|
|
ts.tv_nsec = 300000000L;
|
|
(void)nanosleep(&ts, NULL);
|
|
cancel_now = 1;
|
|
return NULL;
|
|
}
|
|
|
|
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, *host4, *host5;
|
|
struct sp_buf err;
|
|
struct sp_reply rep;
|
|
|
|
memset(®, 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(®, &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(®, &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(®, &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);
|
|
|
|
/*
|
|
* The timeout is an idle window: a program that keeps printing is
|
|
* working, and the window moves with every line. One that says nothing
|
|
* for the whole window is still killed and reported.
|
|
*/
|
|
setenv("SPLINTER_CLI_TIMEOUT", "2", 1);
|
|
host4 = host_for(®, &ag, "sh -c 'for i in 1 2 3 4 5 6; do "
|
|
"echo tick; sleep 0.5; done; echo fertig'", &err);
|
|
CHECK(host4 != NULL);
|
|
sp_buf_clear(&err);
|
|
memset(&rep, 0, sizeof(rep));
|
|
CHECK(one_turn(host4, "hallo", &rep, &err) == 0);
|
|
CHECK(rep.content != NULL && strstr(rep.content, "fertig") != NULL);
|
|
sp_reply_free(&rep);
|
|
|
|
host5 = host_for(®, &ag, "sleep 10", &err);
|
|
CHECK(host5 != NULL);
|
|
sp_buf_clear(&err);
|
|
memset(&rep, 0, sizeof(rep));
|
|
CHECK(one_turn(host5, "hallo", &rep, &err) == -1);
|
|
CHECK(strstr(err.p, "timed out") != NULL);
|
|
sp_reply_free(&rep);
|
|
unsetenv("SPLINTER_CLI_TIMEOUT");
|
|
|
|
/*
|
|
* Ctrl-C while the program runs. This is the freeze: the transport used
|
|
* popen() and read the pipe with no way to stop it, so a `claude -p` turn
|
|
* held the UI's join for as long as the program kept talking.
|
|
*/
|
|
{
|
|
struct sp_host *hk;
|
|
pthread_t th;
|
|
time_t t0;
|
|
|
|
hk = host_for(®, &ag, "sleep 60", &err);
|
|
sp_buf_clear(&err);
|
|
memset(&rep, 0, sizeof(rep));
|
|
cancel_now = 0;
|
|
sp_openai_set_cancel(&cancel_now);
|
|
CHECK(pthread_create(&th, NULL, set_cancel_soon, NULL) == 0);
|
|
t0 = time(NULL);
|
|
CHECK(one_turn(hk, "hallo", &rep, &err) == -1);
|
|
CHECK(time(NULL) - t0 < 10);
|
|
CHECK(strstr(err.p, "cancelled") != NULL);
|
|
CHECK(pthread_join(th, NULL) == 0);
|
|
sp_openai_set_cancel(NULL);
|
|
sp_reply_free(&rep);
|
|
sp_host_free(hk);
|
|
}
|
|
|
|
/* ---- 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(®, &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(®, &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(®, &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(®, &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(®);
|
|
|
|
if (failures == 0)
|
|
printf("test_cli: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|