The harness was two halves that could not see each other: main.c built the
agent, ui.c ran it -- with the curses thread model, the approval condition
variable, the slash commands and the provider switching inside the drawing
code. A second front end had to copy all of it.
Now:
src/backend/ engine.c owns the session, the turn thread, the event queue
and every command; agent, transports, tools, session, config,
providers, plugins and the protocol servers move here.
src/frontend/ curses.c draws and reads keys through ui_backend.h;
ui_local.c is the engine in this process, ui_remote.c is a
served engine behind a socket. protocol.c serves the engine
as JSON lines (protocol 2: history, approvals, steering);
client.c is splinterctl, the reference client.
splinter --serve / --attach: the same screen runs as a client of a served
engine, so the socket carries everything the front end needs. docs/PROTOCOL.md
and docs/FRONTENDS.md describe the contract; docs/proto-example.py is a
60-line client. tests/test_engine, test_protocol and test_attach cover the
event order, the socket and the attach mode.
Review fixes in the same tree: dynamic line buffers (9 KB prompts and 8 KB
events were truncated), a turn event no longer ends the busy window (steer
leftovers), the screen finalises every turn, /new keeps the profile, reopens
the log and gets unique ids, the profile is remembered in <session>.agent so
--resume comes back in it, agent statistics are a locked snapshot (no data
race), !shell keeps 64 KiB with a marker, the socket probes for a live server,
compares its inode and sets CLOEXEC/SNDTIMEO, exit 2 means configuration,
BSD peer checks use getpeereid, and the i18n checker scans the new
subdirectories again.
Includes the parallel session's SSE fix: the parser reports what a line
produced, so a tool name no longer lands in the answer ("readread").
427 lines
11 KiB
C
427 lines
11 KiB
C
/*
|
|
* splinter -- approvals that do not care who answers.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* A tool call that changes something needs a yes from a human. The human may be
|
|
* at the terminal, on Telegram, or behind a command that a script answers; there
|
|
* may be several of them and the first answer wins.
|
|
*
|
|
* The shape that makes that work without two processes fighting over the same
|
|
* Telegram updates:
|
|
*
|
|
* ~/.splinter/ack/pending/<id> the request, written by whoever is blocked
|
|
* ~/.splinter/ack/<id>.answer the answer, written by whoever knows it
|
|
*
|
|
* So the asking process only waits on a file. Channels are notifiers: they push
|
|
* the pending request out (a command per channel, e.g. a Telegram sendMessage) and
|
|
* whoever reads the reply - the gateway, a pager, a script - drops the answer file.
|
|
* No polling loop in two places, no lost updates, and the same mechanism works
|
|
* for the terminal, a phone and an ack button.
|
|
*
|
|
* A timeout is a no. Silence is not consent.
|
|
*/
|
|
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <sys/wait.h>
|
|
|
|
#include "splinter.h"
|
|
#include "i18n.h"
|
|
#include "ack.h"
|
|
|
|
static char ack_root[512];
|
|
|
|
static int
|
|
ensure_dir(const char *path)
|
|
{
|
|
struct stat st;
|
|
|
|
if (mkdir(path, 0700) != 0 && errno != EEXIST)
|
|
return -1;
|
|
/*
|
|
* An answer file is a yes. A directory that someone else owns, or that
|
|
* others may write into, lets them write that yes: refuse it instead of
|
|
* trusting it. lstat, so a symlink planted in its place is refused too.
|
|
*/
|
|
if (lstat(path, &st) != 0 || !S_ISDIR(st.st_mode) ||
|
|
st.st_uid != getuid() || (st.st_mode & 022) != 0) {
|
|
errno = EPERM;
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
const char *
|
|
sp_ack_dir(void)
|
|
{
|
|
const char *env = getenv("SPLINTER_ACK_DIR");
|
|
const char *home = getenv("HOME");
|
|
|
|
if (env != NULL && *env != '\0') {
|
|
snprintf(ack_root, sizeof(ack_root), "%s", env);
|
|
return ack_root;
|
|
}
|
|
if (home == NULL || *home == '\0')
|
|
return NULL;
|
|
snprintf(ack_root, sizeof(ack_root), "%s/.splinter/ack", home);
|
|
return ack_root;
|
|
}
|
|
|
|
static int
|
|
ack_id_path(char *buf, size_t len, int id, const char *suffix)
|
|
{
|
|
const char *root = sp_ack_dir();
|
|
char dir[600];
|
|
|
|
if (root == NULL)
|
|
return -1;
|
|
snprintf(dir, sizeof(dir), "%s/pending", root);
|
|
if (ensure_dir(root) != 0 || ensure_dir(dir) != 0)
|
|
return -1;
|
|
snprintf(buf, len, "%s/%d%s", dir, id, suffix);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* An id nobody can guess and nobody reuses. It used to be the pid, so the next
|
|
* request of the same process - or of a later process with a recycled pid -
|
|
* found the previous answer waiting and was approved by it. Nine digits at most,
|
|
* because a human types it into a phone.
|
|
*/
|
|
static int
|
|
new_id(void)
|
|
{
|
|
unsigned char r[4] = { 0, 0, 0, 0 };
|
|
unsigned long v;
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
if (read(fd, r, sizeof(r)) != (ssize_t)sizeof(r))
|
|
memset(r, 0, sizeof(r));
|
|
close(fd);
|
|
}
|
|
v = ((unsigned long)r[0] << 24) | ((unsigned long)r[1] << 16) |
|
|
((unsigned long)r[2] << 8) | (unsigned long)r[3];
|
|
if (v == 0) /* no randomness: refuse, do not guess */
|
|
return -1;
|
|
return (int)(100000 + v % 900000000UL);
|
|
}
|
|
|
|
/* the expiry recorded in a request file, or 0 when there is none */
|
|
static long
|
|
request_expires(const char *path)
|
|
{
|
|
FILE *f = fopen(path, "r");
|
|
char line[256];
|
|
long exp = 0;
|
|
|
|
if (f == NULL)
|
|
return -1;
|
|
while (fgets(line, sizeof(line), f) != NULL)
|
|
if (strncmp(line, "expires=", 8) == 0)
|
|
exp = atol(line + 8);
|
|
fclose(f);
|
|
return exp;
|
|
}
|
|
|
|
/* sh -c '<cmd>' sh '<arg>': the command is parsed, the argument is not */
|
|
static int
|
|
run_with_arg(const char *cmd, const char *arg)
|
|
{
|
|
pid_t child;
|
|
int status;
|
|
|
|
child = fork();
|
|
if (child < 0)
|
|
return -1;
|
|
if (child == 0) {
|
|
execl("/bin/sh", "sh", "-c", cmd, "sh", arg, (char *)NULL);
|
|
_exit(127);
|
|
}
|
|
while (waitpid(child, &status, 0) < 0)
|
|
if (errno != EINTR)
|
|
return -1;
|
|
return status;
|
|
}
|
|
|
|
/*
|
|
* Write the request under a fresh random id. O_EXCL, so two requests never share
|
|
* a file, and 0600, because the arguments may be what is being protected.
|
|
*/
|
|
int
|
|
sp_ack_request(const char *tool, const char *args, const char *why, int ttl)
|
|
{
|
|
char path[700];
|
|
FILE *f = NULL;
|
|
int id = -1, fd = -1, tries;
|
|
time_t now = time(NULL);
|
|
|
|
for (tries = 0; tries < 8 && fd < 0; tries++) {
|
|
id = new_id();
|
|
if (id < 0 || ack_id_path(path, sizeof(path), id, "") != 0)
|
|
return -1;
|
|
fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
if (fd < 0 && errno != EEXIST)
|
|
return -1;
|
|
}
|
|
if (fd < 0 || (f = fdopen(fd, "w")) == NULL) {
|
|
if (fd >= 0)
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
{
|
|
/*
|
|
* An answer that raced the end of an earlier request with this id
|
|
* may still lie here. It belongs to nobody; it must not become the
|
|
* answer to this one.
|
|
*/
|
|
char ans[700];
|
|
|
|
if (ack_id_path(ans, sizeof(ans), id, ".answer") == 0)
|
|
(void)unlink(ans);
|
|
}
|
|
fprintf(f, "id=%d\n", id);
|
|
fprintf(f, "tool=%s\n", tool != NULL ? tool : "");
|
|
fprintf(f, "args=%s\n", args != NULL ? args : "");
|
|
fprintf(f, "why=%s\n", why != NULL ? why : "");
|
|
fprintf(f, "pid=%d\n", (int)getpid());
|
|
fprintf(f, "expires=%ld\n", (long)now + (ttl > 0 ? ttl : 120));
|
|
if (fclose(f) != 0) {
|
|
(void)unlink(path);
|
|
return -1;
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/*
|
|
* Fire every configured channel. Each entry is a command; the request is appended
|
|
* as one argument. A channel that fails is not fatal - another one may reach a
|
|
* human - but the failure is named.
|
|
*/
|
|
/* one "key=" line of a request file, without the newline */
|
|
static void
|
|
request_field(int id, const char *key, char *buf, size_t len)
|
|
{
|
|
char path[700], line[1024];
|
|
size_t kl = strlen(key);
|
|
FILE *f;
|
|
|
|
buf[0] = '\0';
|
|
if (ack_id_path(path, sizeof(path), id, "") != 0 ||
|
|
(f = fopen(path, "r")) == NULL)
|
|
return;
|
|
while (fgets(line, sizeof(line), f) != NULL)
|
|
if (strncmp(line, key, kl) == 0 && line[kl] == '=') {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
snprintf(buf, len, "%s", line + kl + 1);
|
|
break;
|
|
}
|
|
fclose(f);
|
|
}
|
|
|
|
int
|
|
sp_ack_notify(int id, struct sp_buf *log)
|
|
{
|
|
const char *chans = getenv("SPLINTER_ACK_CHANNELS");
|
|
char list[512], *save = NULL, *tok;
|
|
char tool[64], args[400], msg[600];
|
|
int fired = 0;
|
|
|
|
if (chans == NULL || *chans == '\0')
|
|
return 0;
|
|
/*
|
|
* The message says what is being approved. It used to be "Zustimmung
|
|
* <id>" alone: a parent on a phone said yes to a number. Arguments are
|
|
* cut at 300 bytes - enough to see the command, not a whole file.
|
|
*/
|
|
request_field(id, "tool", tool, sizeof(tool));
|
|
request_field(id, "args", args, sizeof(args));
|
|
if (strlen(args) > 300)
|
|
memcpy(args + 297, "...", 4);
|
|
{
|
|
char head[480], how[100];
|
|
|
|
snprintf(head, sizeof(head), sp_td("ack.request",
|
|
"Approval %d: %s %s"), id, tool[0] != '\0' ? tool : "?", args);
|
|
snprintf(how, sizeof(how), sp_td("ack.how",
|
|
"Answer: ok %d / no %d"), id, id);
|
|
snprintf(msg, sizeof(msg), "%s\n%s", head, how);
|
|
}
|
|
snprintf(list, sizeof(list), "%s", chans);
|
|
for (tok = strtok_r(list, ",", &save); tok != NULL;
|
|
tok = strtok_r(NULL, ",", &save)) {
|
|
char cmd[600];
|
|
int rc;
|
|
|
|
while (*tok == ' ')
|
|
tok++;
|
|
if (*tok == '\0')
|
|
continue;
|
|
/*
|
|
* The channel command is the operator's; the message carries the
|
|
* model's arguments and therefore goes in as "$1", never parsed.
|
|
*/
|
|
snprintf(cmd, sizeof(cmd), "%s \"$1\"", tok);
|
|
rc = run_with_arg(cmd, msg);
|
|
if (rc == 0)
|
|
fired++;
|
|
else if (log != NULL)
|
|
sp_buf_printf(log, "ack: channel '%s' failed (%d)\n", tok,
|
|
rc);
|
|
}
|
|
return fired;
|
|
}
|
|
|
|
/*
|
|
* Wait for the answer. Returns 1 yes, 0 no, -1 nothing yet. 'who' says where the
|
|
* answer came from, which is worth recording when a child asks and an adult
|
|
* answers from a phone.
|
|
*/
|
|
int
|
|
sp_ack_wait(int id, int ttl, char *who, size_t who_len)
|
|
{
|
|
char path[700], req[700];
|
|
time_t end = time(NULL) + (ttl > 0 ? ttl : 120);
|
|
int ok = 0;
|
|
|
|
if (ack_id_path(path, sizeof(path), id, ".answer") != 0 ||
|
|
ack_id_path(req, sizeof(req), id, "") != 0)
|
|
return 0;
|
|
for (;;) {
|
|
FILE *f;
|
|
char line[256];
|
|
|
|
if ((f = fopen(path, "r")) != NULL) {
|
|
if (fgets(line, sizeof(line), f) != NULL) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
ok = strncmp(line, "yes ", 4) == 0;
|
|
if (who != NULL) {
|
|
char *sp = strchr(line, ' ');
|
|
|
|
snprintf(who, who_len, "%s",
|
|
sp != NULL ? sp + 1 : "unknown");
|
|
}
|
|
}
|
|
fclose(f);
|
|
break;
|
|
}
|
|
if (time(NULL) >= end)
|
|
break; /* silence is not consent */
|
|
{
|
|
struct timespec ts = { 0, 200 * 1000 * 1000 };
|
|
|
|
(void)nanosleep(&ts, NULL);
|
|
}
|
|
}
|
|
/*
|
|
* Answered or not, the request is over. Removing it is what makes a late
|
|
* answer fail ("no such request") instead of lying in wait for the next one.
|
|
*/
|
|
(void)unlink(req);
|
|
(void)unlink(path);
|
|
return ok;
|
|
}
|
|
|
|
/*
|
|
* Any process may answer, once, while the request is still open. The answer is
|
|
* written to a private temp file and then link(2)ed into place: link fails when
|
|
* the name exists, so the first answer wins and a waiter never reads half a
|
|
* file.
|
|
*/
|
|
int
|
|
sp_ack_answer(int id, int yes, const char *who)
|
|
{
|
|
char req[700], path[700], tmp[760];
|
|
long exp;
|
|
int fd, rc;
|
|
FILE *f;
|
|
|
|
if (id <= 0 || ack_id_path(req, sizeof(req), id, "") != 0 ||
|
|
ack_id_path(path, sizeof(path), id, ".answer") != 0)
|
|
return -1;
|
|
exp = request_expires(req);
|
|
if (exp < 0)
|
|
return -1; /* no such request */
|
|
if (exp > 0 && (long)time(NULL) >= exp)
|
|
return -1; /* too late: the request was a no already */
|
|
snprintf(tmp, sizeof(tmp), "%s.%ld", path, (long)getpid());
|
|
fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0600);
|
|
if (fd < 0 || (f = fdopen(fd, "w")) == NULL) {
|
|
if (fd >= 0) {
|
|
close(fd);
|
|
(void)unlink(tmp);
|
|
}
|
|
return -1;
|
|
}
|
|
fprintf(f, "%s %s\n", yes ? "yes" : "no",
|
|
who != NULL && *who != '\0' ? who : "unknown");
|
|
if (fclose(f) != 0) {
|
|
(void)unlink(tmp);
|
|
return -1;
|
|
}
|
|
rc = link(tmp, path);
|
|
(void)unlink(tmp);
|
|
return rc == 0 ? 0 : -1;
|
|
}
|
|
|
|
int
|
|
sp_ack_answered(int id)
|
|
{
|
|
char path[700];
|
|
|
|
return ack_id_path(path, sizeof(path), id, ".answer") == 0 &&
|
|
access(path, F_OK) == 0;
|
|
}
|
|
|
|
int
|
|
sp_ack_list(struct sp_buf *out)
|
|
{
|
|
const char *root = sp_ack_dir();
|
|
char dir[600];
|
|
struct dirent **ents;
|
|
int n, i;
|
|
|
|
if (root == NULL)
|
|
return -1;
|
|
snprintf(dir, sizeof(dir), "%s/pending", root);
|
|
n = scandir(dir, &ents, NULL, alphasort);
|
|
if (n < 0) {
|
|
sp_buf_printf(out, "%s\n", sp_td("ack.none",
|
|
"no open requests"));
|
|
return 0;
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
char path[1024], line[256];
|
|
FILE *f;
|
|
|
|
if (strchr(ents[i]->d_name, '.') != NULL) { /* answers, temp files */
|
|
free(ents[i]);
|
|
continue;
|
|
}
|
|
snprintf(path, sizeof(path), "%s/%s", dir, ents[i]->d_name);
|
|
sp_buf_printf(out, "%s ", ents[i]->d_name);
|
|
if ((f = fopen(path, "r")) != NULL) {
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
if (strncmp(line, "args=", 5) == 0 ||
|
|
strncmp(line, "expires=", 8) == 0) {
|
|
line[strcspn(line, "\r\n")] = '\0';
|
|
sp_buf_printf(out, "%s ", line);
|
|
}
|
|
}
|
|
fclose(f);
|
|
}
|
|
sp_buf_puts(out, "\n");
|
|
free(ents[i]);
|
|
}
|
|
free(ents);
|
|
return 0;
|
|
}
|