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>
245 lines
6.2 KiB
C
245 lines
6.2 KiB
C
/*
|
|
* splinter -- cli: any command-line agent as the model.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Splinter does not need an HTTP endpoint. It needs something that answers.
|
|
* This transport runs a program and takes its stdout as the answer, so
|
|
* `claude -p`, `opencode run`, `ollama run`, `llm` or a shell script can all be
|
|
* the brains -- and each of them brings its own keys, its own quota, its own
|
|
* free tier.
|
|
*
|
|
* Configuration is by environment, deliberately: no config file to drift.
|
|
*
|
|
* SPLINTER_CLI_CMD the program, default "claude -p"
|
|
* SPLINTER_CLI_FULL=1 send the whole transcript instead of the last user
|
|
* message (the program keeps no history of its own)
|
|
* SPLINTER_CLI_TIMEOUT seconds, default 600
|
|
*
|
|
* Trust note, because it matters: the command runs through /bin/sh -c, so the
|
|
* string comes from the operator's environment, never from the model. Output is
|
|
* capped at 4 MiB and a non-zero exit is an error with the program's own
|
|
* stderr attached -- a silent empty answer is the one thing this must not do.
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
#include "i18n.h"
|
|
|
|
#define OUT_CAP (4u << 20)
|
|
|
|
struct cli_cfg {
|
|
char cmd[512];
|
|
char timeout[16];
|
|
int full;
|
|
};
|
|
|
|
static void
|
|
render(const struct sp_conv *conv, int full, struct sp_buf *out)
|
|
{
|
|
size_t i, from = 0;
|
|
|
|
sp_buf_clear(out);
|
|
if (!full) {
|
|
for (i = conv->n; i > 0; i--) {
|
|
if (conv->v[i - 1].role != NULL &&
|
|
strcmp(conv->v[i - 1].role, "user") == 0) {
|
|
from = i - 1;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
for (i = from; i < conv->n; i++) {
|
|
const struct sp_msg *m = &conv->v[i];
|
|
|
|
if (m->content == NULL || m->content[0] == '\0')
|
|
continue;
|
|
if (full)
|
|
sp_buf_printf(out, "%s: ", m->role != NULL ? m->role :
|
|
"?");
|
|
if (sp_buf_puts(out, m->content) != 0 ||
|
|
sp_buf_puts(out, "\n") != 0)
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* 's' as one single-quoted shell word */
|
|
static int
|
|
sh_word(struct sp_buf *b, const char *s)
|
|
{
|
|
if (sp_buf_puts(b, "'") != 0)
|
|
return -1;
|
|
for (; *s != '\0'; s++)
|
|
if (*s == '\'' ? sp_buf_puts(b, "'\\''") != 0 :
|
|
sp_buf_add(b, s, 1) != 0)
|
|
return -1;
|
|
return sp_buf_puts(b, "'");
|
|
}
|
|
|
|
static int
|
|
cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
|
|
struct sp_buf *err)
|
|
{
|
|
struct cli_cfg *c = ctx;
|
|
struct sp_buf body, cmd, raw;
|
|
char path[512], errpath[540] = "", errtext[512];
|
|
const char *tmpdir = getenv("TMPDIR");
|
|
int fd, status;
|
|
ssize_t w;
|
|
size_t n;
|
|
FILE *p;
|
|
|
|
if (c == NULL)
|
|
return -1;
|
|
if (sp_buf_init(&body) != 0 || sp_buf_init(&cmd) != 0 ||
|
|
sp_buf_init(&raw) != 0) {
|
|
sp_buf_puts(err, "cli: out of memory");
|
|
return -1;
|
|
}
|
|
render(conv, c->full, &body);
|
|
|
|
if (tmpdir == NULL || *tmpdir == '\0')
|
|
tmpdir = "/var/tmp";
|
|
snprintf(path, sizeof(path), "%s/splinter-cli-XXXXXX", tmpdir);
|
|
fd = mkstemp(path);
|
|
if (fd < 0) {
|
|
sp_buf_printf(err, "cli: mkstemp: %s", strerror(errno));
|
|
goto fail;
|
|
}
|
|
n = body.len;
|
|
w = write(fd, body.p, n);
|
|
close(fd);
|
|
if (w < 0 || (size_t)w != n) {
|
|
sp_buf_printf(err, "cli: short write: %s", strerror(errno));
|
|
unlink(path);
|
|
goto fail;
|
|
}
|
|
|
|
/*
|
|
* The program reads the prompt on stdin; timeout guards a hang. Its
|
|
* stderr goes to a file next to the prompt: on the terminal it landed in
|
|
* the middle of the curses screen, and the error message below promised
|
|
* "the program's own stderr" while it quoted stdout. The paths are quoted;
|
|
* TMPDIR is the operator's, but a space in it still broke the command.
|
|
*/
|
|
snprintf(errpath, sizeof(errpath), "%s.err", path);
|
|
sp_buf_printf(&cmd, "timeout %s %s < ", c->timeout, c->cmd);
|
|
if (sh_word(&cmd, path) != 0 || sp_buf_puts(&cmd, " 2> ") != 0 ||
|
|
sh_word(&cmd, errpath) != 0) {
|
|
unlink(path);
|
|
goto fail;
|
|
}
|
|
p = popen(cmd.p, "r");
|
|
if (p == NULL) {
|
|
sp_buf_printf(err, "cli: popen: %s", strerror(errno));
|
|
unlink(path);
|
|
goto fail;
|
|
}
|
|
{
|
|
char chunk[4096];
|
|
size_t got;
|
|
|
|
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
|
|
if (raw.len + got > OUT_CAP) {
|
|
sp_buf_puts(err, "cli: output over 4 MiB, aborted");
|
|
pclose(p);
|
|
unlink(path);
|
|
goto fail;
|
|
}
|
|
if (sp_buf_add(&raw, chunk, got) != 0) {
|
|
pclose(p);
|
|
unlink(path);
|
|
goto fail;
|
|
}
|
|
}
|
|
}
|
|
status = pclose(p);
|
|
unlink(path);
|
|
errtext[0] = '\0';
|
|
{
|
|
FILE *ef = fopen(errpath, "r");
|
|
size_t en = 0;
|
|
|
|
if (ef != NULL) {
|
|
en = fread(errtext, 1, sizeof(errtext) - 1, ef);
|
|
fclose(ef);
|
|
}
|
|
errtext[en] = '\0';
|
|
(void)unlink(errpath);
|
|
}
|
|
|
|
if (status < 0) {
|
|
sp_buf_printf(err, "cli: %s", strerror(errno));
|
|
goto fail;
|
|
}
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) == 124) {
|
|
sp_buf_printf(err, sp_td("cli.timeout", "cli: '%s' timed out after %s s"), c->cmd,
|
|
c->timeout);
|
|
goto fail;
|
|
}
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
|
sp_buf_printf(err, sp_td("cli.failed", "cli: '%s' failed (exit %d): %.400s"), c->cmd,
|
|
WIFEXITED(status) ? WEXITSTATUS(status) : -1,
|
|
errtext[0] != '\0' ? errtext : raw.p != NULL ? raw.p : "");
|
|
goto fail;
|
|
}
|
|
if (raw.len == 0) {
|
|
sp_buf_printf(err, sp_td("cli.no_output", "cli: '%s' produced no output"), c->cmd);
|
|
goto fail;
|
|
}
|
|
out->status = 200;
|
|
out->content = strdup(raw.p);
|
|
if (out->content == NULL)
|
|
goto fail;
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&cmd);
|
|
sp_buf_free(&raw);
|
|
return 0;
|
|
|
|
fail:
|
|
if (errpath[0] != '\0')
|
|
(void)unlink(errpath);
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&cmd);
|
|
sp_buf_free(&raw);
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
cli_init(struct sp_host *h)
|
|
{
|
|
static struct cli_cfg cfg;
|
|
static struct sp_transport tr;
|
|
const char *cmd = getenv("SPLINTER_CLI_CMD");
|
|
const char *to = getenv("SPLINTER_CLI_TIMEOUT");
|
|
const char *full = getenv("SPLINTER_CLI_FULL");
|
|
|
|
memset(&cfg, 0, sizeof(cfg));
|
|
snprintf(cfg.cmd, sizeof(cfg.cmd), "%s",
|
|
cmd != NULL && *cmd != '\0' ? cmd : "claude -p");
|
|
snprintf(cfg.timeout, sizeof(cfg.timeout), "%s",
|
|
to != NULL && *to != '\0' ? to : "600");
|
|
cfg.full = full != NULL && strcmp(full, "0") != 0;
|
|
|
|
tr.send = cli_send;
|
|
tr.ctx = &cfg;
|
|
return sp_host_add_transport(h, "cli", tr);
|
|
}
|
|
|
|
static const struct sp_plugin cli_plugin = {
|
|
SP_PLUGIN_ABI, "cli", "1.0.0", cli_init, NULL, NULL
|
|
};
|
|
|
|
const struct sp_plugin *
|
|
sp_plugin_describe(void)
|
|
{
|
|
return &cli_plugin;
|
|
}
|