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.
261 lines
6.0 KiB
C
261 lines
6.0 KiB
C
/*
|
|
* 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 = ˚
|
|
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;
|
|
}
|