Files
splinter/plugins/ring/ring.c
T
lotherkandClaude Opus 5.5 25a616a87f review fixes, third round: the gateway asks in its chat, approvals say what
Telegram gateway
- A tool call of the gateway's own turn that needed consent was either denied
  (a daemon has no approval stream) or, with ack channels set, waited for an
  "ok <id>" that only the blocked gateway could have read. It now asks in the
  chat the turn came from, keeps reading while it waits, resolves answers
  (also other processes' and 'splinter --ack'), ignores strangers and tells
  everything else to wait. The update offset is shared, so nothing is handled
  twice. New cfg flag approve_is_channel: such an approve is asked directly.
- send_message wrote its error through err, and half the callers pass NULL: the
  first refused message (too long, bad UTF-8) crashed the gateway. It also
  missed '"ok": true' by substring; the reply is parsed now.
- tests/test_gateway.sh drives the real daemon against a fake Bot API and a
  fake model (tests/fake_telegram.py), with "ok" and with "no". In make check;
  .gitignore no longer hides tests/test_*.sh.

Approvals
- A channel was told "Zustimmung <id>" and nothing else: a phone said yes to a
  number. The message now names the tool and its arguments (cut at 300 bytes)
  and how to answer, and goes to the channel as "$1", never parsed by sh.

Smaller
- ring: an exit that already streamed text is not retried on the next one
  (the operator saw a second answer after half of the first); the header now
  says what moves the ring - limits and providers that are down.
- cli: the program's stderr went over the curses screen and the error quoted
  stdout. stderr goes to a file and into the error; temp paths are quoted;
  the BODY_CAP no-op is gone. (That is the "kaputt" make check used to print.)
- memory: memory_key_env became an env var curl never reads, so no request had
  a key. It is "Authorization: Token <key>" now, on curl's stdin. Tested.
- docs/CONFIG.md: the real default path, and the seven keys it did not know
  (telegram_allow, memory_url, memory_key_env, skills_dir, env_delegates,
  exit, delegate) with the line formats, checked against the parser.
- The no-token message named a config key that does not exist.

Left as it is, on purpose: 'read' stays unrestricted (decision); the fsync per
session line stays (durability over latency).

make check: 26 test binaries and the gateway script, green, also under
ASan/UBSan.

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

292 lines
6.8 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 <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;
/* 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);
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;
}
/* 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) {
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) || 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;
}
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;
}