Files
splinter/plugins/cli/cli.c
T
Konrad 50bc6751b2 splinter: real-model fixes, steering, cli transport
First run against a real model (Qwen3.6-35B-A3B on kontron, strata-poly)
exposed two things a stub never would:

- our tool schema sent "properties":{}, which tells the model that no
  arguments exist. Tools now carry a real JSON schema (echo/command/clock), and
  the loop completes a tool round against the 35B: request 1 -> tool_call,
  request 2 -> "Das Ergebnis ist: ping".
- an answer longer than the screen was not drawn at all: the entry did not
  'fit', so the viewport skipped it and the chat looked empty. draw_history is
  now a real viewport (total height, bottom-anchored, entries taller than the
  screen start at the offset that puts their end on screen).

Steering: a turn runs on its own thread while the UI keeps reading keys. Enter
queues text the loop picks up at the next step boundary; Ctrl-C cancels; in a
single-step turn the text becomes the next message instead of disappearing.
Curses remains UI-thread-only. A dangling ag->cfg after the turn made a steer
run twice - the caller's config is now restored, and the session log confirms
one user line per steer.

Transports are plugins now (sp_host_add_transport / --transport). plugins/cli
makes any program the model: SPLINTER_CLI_CMD, run through /bin/sh, prompt on
stdin, stdout as the answer, 4 MiB cap, non-zero exit with the program's stderr,
empty output is an error. Verified with sed, with claude -p (2.9 s, answer OK)
and with cat.

Eleven test binaries green, test_cli included.
2026-11-07 15:43:47 +00:00

204 lines
5.0 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"
#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[] = "/tmp/splinter-cli-XXXXXX";
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);
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, "cli: '%s' timed out after %s s", c->cmd,
c->timeout);
goto fail;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
sp_buf_printf(err, "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, "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;
}