splinter: exit ring, config next to the sessions, drop-ins

The config moved to ~/.splinter/splinter.conf - next to the sessions, where the
state of this thing already lives - and every *.conf in the directory named after
it is read after it, in name order. A later file overrides an earlier one, an
unknown key is still fatal, and the error now names the file it came from.

plugins/ring is the exit ring: several ways to reach a model, walked in order
when one is rate limited.

  exit = go    | OpenCode Go    | endpoint=... | model=...
  exit = groq  | Groq free tier | endpoint=... | model=... | key_env=GROQ_KEY
  exit = local | kontron, lokal | endpoint=... | model=...

Only pressure moves the ring - 429, 5xx, 'rate limit', 'quota'. A dead host or a
bad key stops the turn, because a ring that treats every failure as 'try the
next one' hides real breakage behind a random provider.

Two things had to be fixed underneath: sp_openai_transport kept its context in a
static variable, so building several transports pointed them all at the last one
(now one heap context per transport), and plugins had no way to read the config
or the registry (sp_host_config, sp_host_registry).

Measured against two fake providers: the first answers 429, the ring moves on and
the second answers 'Antwort vom zweiten Ausgang.'; a dead port does not move it
and the error stands.
This commit is contained in:
Konrad committed 2026-11-08 12:55:42 +00:00
1 parent 781e7eb514
commit 04420d9f8d
15 files changed
+566 -14

No files matched your search

+5 -1
View File
@@ -27,7 +27,8 @@ LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \
BIN = splinter
BINOBJ = src/main.o src/ui.o
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so
plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so \
plugins/ring/ring.so
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
tests/test_transport tests/test_agent tests/test_openai \
tests/test_theme tests/test_plugin tests/test_delegate tests/test_builtin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
@@ -67,6 +68,9 @@ plugins/cli/cli.so: plugins/cli/cli.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/cli/cli.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/ring/ring.so: plugins/ring/ring.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/ring/ring.c -L. -lsplinter \
-Iinclude -Isrc -Ivendor/libmcp $(PLUGIN_LDFLAGS)
plugins/mcp/mcp.so: plugins/mcp/mcp.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/mcp/mcp.c -L. -lsplinter \
-Iinclude -Isrc -Ivendor/libmcp $(PLUGIN_LDFLAGS)
+17
View File
@@ -184,6 +184,21 @@ key that `en.msg` has.
Ships with `en` and `de`. Plugins translate their own strings with `sp_t()`.
## The exit ring
`plugins/ring/ring.so` holds several ways to reach a model and walks them in
order when one is rate limited:
exit = go | OpenCode Go | endpoint=... | model=...
exit = groq | Groq free tier | endpoint=... | model=... | key_env=GROQ_KEY
exit = local | kontron, lokal | endpoint=... | model=...
Run it with `--transport ring`. Only pressure moves the ring: 429, 5xx, "rate
limit", "quota". A dead host or a bad key stops the turn where it is - a ring
that treats every failure as "try the next" hides real breakage behind a random
provider. Proven against two fake providers: the first answered 429, the ring
moved on and the second answered; a dead port did not move it.
## Delegation
Work can be handed to another model. A delegate is one line in the config - no
@@ -270,6 +285,8 @@ ABI in `docs/PLUGINS.md`.
- Tool arguments reach the tool as one string; each tool validates its own.
- MCP tools are all `mutate`, so they ask before they run.
- The ring only retries on rate limits; a 500 from a broken endpoint also counts
as pressure, so a provider that fails hard gets skipped rather than fixed.
- No retrieval tiers over old sessions. Compaction is a plugin (lcm); it cuts the
conversation, it does not remember what it cut.
- A `tick()` that never returns hangs the exit: the host can stop a thread only
+14
View File
@@ -68,3 +68,17 @@ theme = Delphinium
With both `endpoint` and `transport` set, `transport` wins — it is the more
specific instruction.
## Drop-ins
Next to the config file, a directory with the same name plus `.d` is read after
it, every `*.conf` in name order:
```
~/.splinter/splinter.conf
~/.splinter/splinter.conf.d/10-local.conf
~/.splinter/splinter.conf.d/20-delegates.conf
```
A later file overrides an earlier one, and an unknown key is still an error -
now with the file it came from. Proven in `test_config`.
+4
View File
@@ -98,6 +98,10 @@ int sp_host_compact(struct sp_host *, struct sp_conv *, size_t budget);
size_t sp_host_compaction_stats(struct sp_host *, size_t *compactions,
size_t *failed, unsigned *turns);
void sp_host_turn(struct sp_host *);
/* The loaded configuration, so plugins (the exit ring) can read it. */
const void *sp_host_config(struct sp_host *);
void sp_host_set_config(struct sp_host *, const void *);
struct sp_registry *sp_host_registry(struct sp_host *);
/* ---- host side, implemented by splinter, called by the harness ---- */
BIN
View File
Binary file not shown.
+260
View File
@@ -0,0 +1,260 @@
/*
* splinter -- the exit ring.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* A turn should not die because one provider's free tier ran out at 03:14. The
* ring holds several ways to reach a model and walks them in order:
*
* exit = go | OpenCode Go | endpoint=... | model=... | key_env=OPENCODE_KEY
* exit = groq | Groq free tier | endpoint=... | model=... | key_env=GROQ_KEY
* exit = local | kontron, lokal | endpoint=... | model=...
*
* Only a rate limit moves the ring. A syntax error or a bad key stops the turn
* where it is - pretending every failure is "try the next one" would hide real
* breakage behind a random provider. Each step is reported, so the log says which
* exit served the turn.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "config.h"
#define MAX_EXITS SP_CONFIG_MAX_DELEGATES
struct ring {
struct sp_host *host;
const struct sp_config *cfg;
struct sp_transport tr[MAX_EXITS];
size_t n;
int last; /* which exit served the last turn */
int used[MAX_EXITS];
};
static struct ring ring;
/* A rate limit is the only thing worth another exit. */
static int
is_pressure(int rc, long status, const struct sp_buf *err)
{
if (status == 429 || status == 503 || status == 502 ||
status == 504 || status == 500)
return 1;
if (rc == 0)
return 0;
if (err != NULL && err->p != NULL) {
const char *p = err->p;
if (strstr(p, "429") != NULL || strstr(p, "rate limit") != NULL ||
strstr(p, "rate_limit") != NULL ||
strstr(p, "quota") != NULL ||
strstr(p, "limit reached") != NULL ||
strstr(p, "insufficient") != NULL ||
strstr(p, "overloaded") != NULL)
return 1;
}
return 0;
}
static void
publish(const char *text)
{
(void)sp_host_widget_text(ring.host, "ring", text);
(void)sp_host_note(ring.host, text);
}
static int
build(void)
{
size_t i;
if (ring.n > 0)
return 0;
if (ring.cfg == NULL || ring.cfg->nexits == 0) {
publish("ring: no exit configured");
return -1;
}
for (i = 0; i < ring.cfg->nexits && i < MAX_EXITS; i++) {
const struct sp_exit *e = &ring.cfg->exit[i];
const char *env = e->key_env[0] != '\0' ?
e->key_env : "SPLINTER_API_KEY";
const char *key = getenv(env);
ring.tr[ring.n] = sp_openai_transport(e->endpoint, e->model,
key, sp_host_registry(ring.host), 300);
if (ring.tr[ring.n].send == NULL) {
publish("ring: cannot build an exit");
return -1;
}
ring.n++;
}
return 0;
}
static int
ring_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err)
{
struct ring *r = ctx;
size_t i;
char line[128];
if (build() != 0)
return -1;
for (i = 0; i < r->n; i++) {
struct sp_buf e;
long status;
int rc;
if (sp_buf_init(&e) != 0)
return -1;
sp_reply_free(out);
rc = r->tr[i].send(r->tr[i].ctx, conv, out, &e);
status = out->status;
if (rc == 0) {
r->last = (int)i;
r->used[i]++;
snprintf(line, sizeof(line), "%s (%zu/%zu)",
r->cfg->exit[i].label, i + 1, r->n);
(void)sp_host_widget_text(r->host, "ring", line);
sp_buf_free(&e);
return 0;
}
if (!is_pressure(rc, status, &e)) {
/* not a limit: this is a real failure, keep it */
if (err != NULL)
(void)sp_buf_puts(err, e.p != NULL ? e.p :
"ring: exit failed");
sp_buf_free(&e);
return rc;
}
snprintf(line, sizeof(line), "ring: %s busy, next exit",
r->cfg->exit[i].label);
(void)sp_host_note(r->host, line);
sp_buf_free(&e);
}
if (err != NULL)
(void)sp_buf_puts(err, "ring: every exit is rate limited");
return -1;
}
static int
ring_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
struct sp_buf *err, sp_stream_cb cb, void *cbctx)
{
struct ring *r = ctx;
size_t i;
char line[128];
if (build() != 0)
return -1;
for (i = 0; i < r->n; i++) {
struct sp_buf e;
long status;
int rc;
if (sp_buf_init(&e) != 0)
return -1;
sp_reply_free(out);
rc = r->tr[i].send_stream(r->tr[i].ctx, conv, out, &e, cb,
cbctx);
status = out->status;
if (rc == 0) {
r->last = (int)i;
r->used[i]++;
snprintf(line, sizeof(line), "%s (%zu/%zu)",
r->cfg->exit[i].label, i + 1, r->n);
(void)sp_host_widget_text(r->host, "ring", line);
sp_buf_free(&e);
return 0;
}
if (!is_pressure(rc, status, &e)) {
if (err != NULL)
(void)sp_buf_puts(err, e.p != NULL ? e.p :
"ring: exit failed");
sp_buf_free(&e);
return rc;
}
snprintf(line, sizeof(line), "ring: %s busy, next exit",
r->cfg->exit[i].label);
(void)sp_host_note(r->host, line);
sp_buf_free(&e);
}
if (err != NULL)
(void)sp_buf_puts(err, "ring: every exit is rate limited");
return -1;
}
static int
ring_widget(struct sp_host *h, const char *name, void *ctx)
{
struct ring *r = ctx;
char line[160];
size_t i;
(void)h;
if (r->n == 0) {
(void)sp_host_widget_text(ring.host, name, "no exits");
return 0;
}
snprintf(line, sizeof(line), "%.16s |", r->cfg->exit[r->last].label);
for (i = 0; i < r->n; i++) {
char bit[16];
snprintf(bit, sizeof(bit), " %d", r->used[i]);
strncat(line, bit, sizeof(line) - strlen(line) - 1);
}
(void)sp_host_widget_text(ring.host, name, line);
return 0;
}
static const struct sp_widget widget = {
"ring",
ring_widget,
NULL,
&ring,
3000
};
static int
ring_init(struct sp_host *h)
{
struct sp_transport t;
memset(&ring, 0, sizeof(ring));
ring.host = h;
ring.cfg = sp_host_config(h);
if (ring.cfg == NULL || ring.cfg->nexits == 0) {
(void)sp_host_note(h, "ring: no exit configured");
return -1;
}
memset(&t, 0, sizeof(t));
t.send = ring_send;
t.send_stream = ring_stream;
t.ctx = &ring;
if (sp_host_add_transport(h, "ring", t) != 0) {
(void)sp_host_note(h, "ring: cannot register the transport");
return -1;
}
(void)sp_host_add_widget(h, &widget);
return 0;
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI,
"ring",
"1.0",
ring_init,
NULL,
NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}
BIN
View File
Binary file not shown.
+160 -5
View File
@@ -12,6 +12,8 @@
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "config.h"
void
@@ -33,13 +35,29 @@ sp_config_path(char *buf, size_t len)
snprintf(buf, len, "%s", p);
return 0;
}
if (xdg != NULL && *xdg != '\0') {
snprintf(buf, len, "%s/splinter/config", xdg);
return 0;
}
if (home == NULL || *home == '\0')
return -1;
snprintf(buf, len, "%s/.config/splinter/config", home);
/*
* The config lives next to the sessions: ~/.splinter is where this thing
* keeps its state, and a second directory for one file was a stray.
*/
snprintf(buf, len, "%s/.splinter/splinter.conf", home);
{
char newpath[512];
FILE *f;
snprintf(newpath, sizeof(newpath), "%s", buf);
f = fopen(newpath, "r");
if (f != NULL) {
fclose(f);
return 0;
}
/* still nothing there? an old install may have used XDG */
if (xdg != NULL && *xdg != '\0')
snprintf(buf, len, "%s/splinter/config", xdg);
else
snprintf(buf, len, "%s/.config/splinter/config", home);
}
return 0;
}
@@ -69,6 +87,65 @@ set_str(char *dst, size_t len, const char *src)
* The bar is that every field is named: a positional format with five slots is
* how a config file becomes a guessing game.
*/
/* exit = id | label | endpoint=... | model=... | key_env=... */
static int
parse_exit(struct sp_exit *e, const char *val, struct sp_buf *err,
const char *path, unsigned lineno)
{
char *copy, *field, *save = NULL;
int first = 1;
memset(e, 0, sizeof(*e));
copy = strdup(val);
if (copy == NULL)
return -1;
for (field = strtok_r(copy, "|", &save); field != NULL;
field = strtok_r(NULL, "|", &save)) {
char *s = field, *end;
while (*s == ' ' || *s == '\t')
s++;
end = s + strlen(s);
while (end > s && (end[-1] == ' ' || end[-1] == '\t'))
*--end = '\0';
if (*s == '\0')
continue;
if (first && strchr(s, '=') == NULL) {
snprintf(e->id, sizeof(e->id), "%s", s);
first = 0;
continue;
}
if (!strncmp(s, "endpoint=", 9))
snprintf(e->endpoint, sizeof(e->endpoint), "%s", s + 9);
else if (!strncmp(s, "model=", 6))
snprintf(e->model, sizeof(e->model), "%s", s + 6);
else if (!strncmp(s, "key_env=", 8))
snprintf(e->key_env, sizeof(e->key_env), "%s", s + 8);
else if (strchr(s, '=') == NULL)
snprintf(e->label, sizeof(e->label), "%s", s);
else {
sp_buf_printf(err, "config: %s:%u: unknown exit field "
"'%s'", path, lineno, s);
free(copy);
return -1;
}
}
free(copy);
if (e->id[0] == '\0' || e->endpoint[0] == '\0' ||
e->model[0] == '\0') {
sp_buf_printf(err, "config: %s:%u: exit needs id, endpoint and "
"model", path, lineno);
return -1;
}
if (e->label[0] == '\0') {
char tmp[sizeof(e->id)];
snprintf(tmp, sizeof(tmp), "%s", e->id);
snprintf(e->label, sizeof(e->label), "%s", tmp);
}
return 0;
}
static int
parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err,
const char *path, unsigned lineno)
@@ -129,8 +206,71 @@ parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err,
return 0;
}
/*
* Every file is parsed by the same function, and every error carries the file
* it came from: with drop-ins a line number without a name is a riddle.
*/
static int
load_one(struct sp_config *c, const char *path, struct sp_buf *err);
static int
cmp_name(const void *a, const void *b)
{
return strcmp(*(const char *const *)a, *(const char *const *)b);
}
/*
* Drop-ins: every .conf file in the directory named after the config file,
* read in name order, so 10-local beats 90-remote. A file that fails to parse
* names itself in the error.
*/
static int
load_dropins(struct sp_config *c, const char *mainpath, struct sp_buf *err)
{
char dir[600], *names[64];
size_t n = 0, i;
DIR *d;
struct dirent *e;
snprintf(dir, sizeof(dir), "%s.d", mainpath);
d = opendir(dir);
if (d == NULL)
return 0;
while ((e = readdir(d)) != NULL && n < 64) {
size_t len = strlen(e->d_name);
if (len < 6 || strcmp(e->d_name + len - 5, ".conf") != 0)
continue;
names[n] = strdup(e->d_name);
if (names[n] != NULL)
n++;
}
closedir(d);
qsort(names, n, sizeof(names[0]), cmp_name);
for (i = 0; i < n; i++) {
char full[700];
snprintf(full, sizeof(full), "%s/%s", dir, names[i]);
free(names[i]);
if (load_one(c, full, err) != 0) {
while (++i < n)
free(names[i]);
return -1;
}
}
return 0;
}
int
sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
{
if (load_one(c, path, err) != 0)
return -1;
return load_dropins(c, path, err);
}
static int
load_one(struct sp_config *c, const char *path, struct sp_buf *err)
{
FILE *f;
char line[2048];
@@ -191,6 +331,21 @@ sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
else if (!strcmp(key, "autonomous"))
c->autonomous = (!strcmp(val, "1") ||
!strcmp(val, "true") || !strcmp(val, "yes")) ? 1 : 0;
else if (!strcmp(key, "exit")) {
if (c->nexits == SP_CONFIG_MAX_DELEGATES) {
sp_buf_printf(err, "config: %s:%u: more than %d "
"exits", path, lineno,
SP_CONFIG_MAX_DELEGATES);
fclose(f);
return -1;
}
if (parse_exit(&c->exit[c->nexits], val, err, path,
lineno) != 0) {
fclose(f);
return -1;
}
c->nexits++;
}
else if (!strcmp(key, "delegate")) {
if (c->ndelegates == SP_CONFIG_MAX_DELEGATES) {
sp_buf_printf(err, "config: %s:%u: more than %d "
+15
View File
@@ -30,6 +30,19 @@
*/
#define SP_CONFIG_MAX_DELEGATES 8
/*
* An exit is one way to reach a model: an endpoint, a model name and where its
* key lives. The ring tries them in order, so a rate limit costs a step, not the
* turn.
*/
struct sp_exit {
char id[32];
char label[64]; /* what shows up in the sidebar */
char endpoint[512];
char model[256];
char key_env[64];
};
struct sp_delegate {
char id[32];
char about[128]; /* what it is good at, in words */
@@ -42,6 +55,8 @@ struct sp_delegate {
struct sp_config {
struct sp_delegate delegate[SP_CONFIG_MAX_DELEGATES];
size_t ndelegates;
struct sp_exit exit[SP_CONFIG_MAX_DELEGATES];
size_t nexits;
char endpoint[512];
char model[256];
char key_env[64];
+2
View File
@@ -389,6 +389,8 @@ main(int argc, char **argv)
size_t k;
host = sp_host_new(&reg, &ag, &sess);
if (host != NULL)
sp_host_set_config(host, &conf);
if (host == NULL) {
fprintf(stderr, "splinter: cannot create plugin host\n");
return 3;
+20
View File
@@ -48,6 +48,7 @@ struct widget {
struct sp_host {
struct sp_registry *reg;
const void *conf; /* the loaded config, for plugins */
struct widget widgets[MAX_WIDGETS];
size_t nwidgets;
char note[MAX_NOTE];
@@ -289,6 +290,25 @@ sp_host_compaction_stats(struct sp_host *h, size_t *compactions,
return h != NULL ? h->compactions : 0;
}
struct sp_registry *
sp_host_registry(struct sp_host *h)
{
return h != NULL ? h->reg : NULL;
}
const void *
sp_host_config(struct sp_host *h)
{
return h != NULL ? h->conf : NULL;
}
void
sp_host_set_config(struct sp_host *h, const void *conf)
{
if (h != NULL)
h->conf = conf;
}
void
sp_host_turn(struct sp_host *h)
{
+16 -8
View File
@@ -401,17 +401,25 @@ struct sp_transport
sp_openai_transport(const char *url, const char *model, const char *api_key,
const struct sp_registry *reg, long timeout_s)
{
static struct openai_ctx ctx;
struct openai_ctx *ctx;
struct sp_transport t;
memset(&ctx, 0, sizeof(ctx));
snprintf(ctx.url, sizeof(ctx.url), "%s", url != NULL ? url : "");
snprintf(ctx.model, sizeof(ctx.model), "%s", model != NULL ? model : "");
snprintf(ctx.key, sizeof(ctx.key), "%s", api_key != NULL ? api_key : "");
ctx.reg = reg;
ctx.timeout_s = timeout_s > 0 ? timeout_s : 180;
/*
* One context per transport, on the heap: with a static one every
* transport shared a single endpoint, so building several of them (the
* exit ring) silently pointed them all at the last one.
*/
ctx = malloc(sizeof(*ctx));
if (ctx == NULL)
return t; /* zeroed: send is NULL, caller reports */
memset(ctx, 0, sizeof(*ctx));
snprintf(ctx->url, sizeof(ctx->url), "%s", url != NULL ? url : "");
snprintf(ctx->model, sizeof(ctx->model), "%s", model != NULL ? model : "");
snprintf(ctx->key, sizeof(ctx->key), "%s", api_key != NULL ? api_key : "");
ctx->reg = reg;
ctx->timeout_s = timeout_s > 0 ? timeout_s : 180;
t.send = openai_send;
t.send_stream = openai_stream;
t.ctx = &ctx;
t.ctx = ctx;
return t;
}
BIN
View File
Binary file not shown.
+53
View File
@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
@@ -125,6 +126,58 @@ main(void)
unsetenv("SPLINTER_CONFIG");
sp_buf_free(&err);
/* ---- drop-ins: a directory next to the config, read in name order ---- */
{
char dir[512], main_c[600], drop[600];
FILE *f;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-conf-XXXXXX", tmp);
if (mkdtemp(dir) != NULL) {
struct sp_config c;
struct sp_buf e;
snprintf(main_c, sizeof(main_c), "%s/splinter.conf", dir);
write_file(main_c, "theme = Delphinium\n");
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
if (mkdir(drop, 0700) == 0) {
char one[700], two[700];
snprintf(one, sizeof(one), "%s/50-late.conf", drop);
snprintf(two, sizeof(two), "%s/10-early.conf", drop);
write_file(one, "theme = Spider\n");
write_file(two, "locale = de\n");
/* a file that is not .conf must be ignored */
{
char other[700];
snprintf(other, sizeof(other),
"%s/ignored.txt", drop);
write_file(other, "kaputt = 1\n");
}
}
sp_config_init(&c);
if (sp_buf_init(&e) == 0) {
CHECK(sp_config_load(&c, main_c, &e) == 0);
/* 50-late.conf is read after the main file */
CHECK(strcmp(c.theme, "Spider") == 0);
CHECK(strcmp(c.locale, "de") == 0);
sp_buf_free(&e);
}
f = NULL;
(void)f;
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed\n");
}
}
}
if (failures == 0)
printf("test_config: ok\n");
return failures == 0 ? 0 : 1;
Binary file not shown.