Files
splinter/plugins/ring/ring.c
lotherkandClaude Opus 5.5 75d8beeaa4 delegates and cli_env: hardcoded environment per cli program; swarm, provider, planner, scheduler, tmux
env=NAME=VALUE on a delegate line (repeatable) is set in the child right
before exec and beats what the parent exported, so two delegates can run
the same cli with two accounts. Remotely it goes in front as
`env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the
same for the main cli transport. A bad name is an error with the line.

Also in this commit, the work in progress from the tree: swarm and
provider state, the provider/scheduler/planner/tmux plugins, the ui test,
whose tmux session name is now per run so parallel runs do not collide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 19:15:48 +00:00

310 lines
7.4 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 pressure moves the ring: a rate limit, an exhausted quota, or a provider
* that is down (500/502/503/504). 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.
*
* And once an exit has streamed text to the screen, its failure is the turn's
* failure: asking the next exit would show the operator a second answer after
* half of the first one.
*/
#include <pthread.h>
#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];
/* last and used are written by the turn thread, read by the widget */
pthread_mutex_t lock;
};
static struct ring ring = { .lock = PTHREAD_MUTEX_INITIALIZER };
/* Pressure - a limit or a provider that is down - is 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);
{
const char *hto = getenv("SPLINTER_HTTP_TIMEOUT");
long hto_s = hto != NULL && *hto != '\0' ?
strtol(hto, NULL, 10) : 0;
if (hto_s <= 0)
hto_s = 900;
ring.tr[ring.n] = sp_openai_transport(e->endpoint, e->model,
key, sp_host_registry(ring.host), hto_s);
}
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) {
pthread_mutex_lock(&r->lock);
r->last = (int)i;
r->used[i]++;
pthread_mutex_unlock(&r->lock);
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;
}
/* passes the stream through and remembers whether anything was shown */
struct seen {
sp_stream_cb cb;
void *ctx;
int shown;
};
static void
seen_cb(void *ctx, int kind, const char *text)
{
struct seen *s = ctx;
if (text != NULL && *text != '\0')
s->shown = 1;
if (s->cb != NULL)
s->cb(s->ctx, kind, text);
}
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];
struct seen seen;
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);
seen.cb = cb;
seen.ctx = cbctx;
seen.shown = 0;
rc = r->tr[i].send_stream(r->tr[i].ctx, conv, out, &e, seen_cb,
&seen);
status = out->status;
if (rc == 0) {
pthread_mutex_lock(&r->lock);
r->last = (int)i;
r->used[i]++;
pthread_mutex_unlock(&r->lock);
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) || seen.shown) {
if (err != NULL) {
(void)sp_buf_puts(err, e.p != NULL ? e.p :
"ring: exit failed");
if (seen.shown && is_pressure(rc, status, &e))
(void)sp_buf_puts(err, " (after part of "
"the answer was shown; not retried)");
}
sp_buf_free(&e);
return rc != 0 ? rc : -1;
}
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;
}
pthread_mutex_lock(&r->lock);
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);
}
pthread_mutex_unlock(&r->lock);
(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));
(void)pthread_mutex_init(&ring.lock, NULL);
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;
}