Pressing Ctrl-C killed the harness. cbreak() leaves ISIG on, so the terminal driver turned the key into SIGINT and the process died before wgetch() could return 3 - the 'Ctrl-C cancels' line was a claim, and one keystroke disproved it. raw() hands every byte to the program. Cancelling a turn also only took effect between steps, so a request in flight kept running to completion. The transport used popen(), which returns a stream and not a process id, so there was nothing to kill. It now forks and execs, keeps the pid, and both the read loop and the streaming loop poll with a 200 ms timeout and SIGKILL the child when the turn is cancelled. Measured: Ctrl-C during a streaming request returns to the prompt in 510 ms and the UI survives. Temp files no longer use a fixed /tmp path: $TMPDIR, then /var/tmp. Three sites, including the cli plugin, and they carry the conversation, so the path is now a 512-byte buffer built at runtime instead of a literal.
209 lines
5.2 KiB
C
209 lines
5.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)
|
|
#define BODY_CAP (1u << 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;
|
|
}
|
|
}
|
|
|
|
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];
|
|
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 */
|
|
sp_buf_printf(&cmd, "timeout %s %s < %s", c->timeout, c->cmd, path);
|
|
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);
|
|
|
|
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,
|
|
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;
|
|
if (body.len > BODY_CAP)
|
|
(void)0; /* capped above; kept explicit */
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&cmd);
|
|
sp_buf_free(&raw);
|
|
return 0;
|
|
|
|
fail:
|
|
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;
|
|
}
|