splinter: one ack mechanism, any channel, any process answers

Approval was a prompt on stdin: the terminal could answer, a phone could not, and
in the TUI the whole path was dead code that failed closed.

Now a request is two files:

  ~/.splinter/ack/pending/<id>   the request (tool, args, why, expiry)
  ~/.splinter/ack/<id>.answer    the answer (yes/no plus who)

The blocked process waits on the file and nothing else. Channels are notifiers -
a command per channel pushes the request out - and whoever reads the reply drops
the answer: the gateway, a phone, a script, another terminal. No second poller,
no lost Telegram updates, no special case per channel.

  SPLINTER_ACK_CHANNELS=...   comma separated commands, each gets the request
  SPLINTER_ACK_TTL=120          seconds; a timeout is a no
  splinter --ack <id> yes|no    answer from anywhere
  splinter --ack-list           what is waiting

The terminal is one more channel, not a special case: a thread reads stdin and
drops the answer, while the main path waits on the file like everybody else. Kid
mode uses the same mechanism instead of its own command.

Verified: tests/test_ack.c, 7 checks - request, yes, no, who answered, timeout is
a no, answering a request that does not exist fails, and the pending list shows
the arguments. make check runs it with the other 21.
This commit is contained in:
Konrad committed 2026-11-08 16:55:27 +00:00
1 parent 2e776611f3
commit 8682f97759
8 files changed
+552 -3

No files matched your search

+7 -2
View File
@@ -23,7 +23,7 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \
LIB = libsplinter.so
LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \
src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o
BIN = splinter
BINOBJ = src/main.o src/ui.o
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
@@ -32,7 +32,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
plugins/kids/kids.so
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
tests/test_transport tests/test_agent tests/test_openai \
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor
RPATH = -Wl,-rpath,'$$ORIGIN'
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
@@ -112,6 +112,7 @@ src/telegram.o: include/splinter.h
src/log.o: src/log.h
src/cmdscript.o: src/cmdscript.h include/splinter.h
src/folder.o: src/folder.h include/splinter.h
src/ack.o: src/ack.h include/splinter.h
src/interview.o: src/interview.h include/splinter.h
src/mcpserver.o: src/mcpserver.h include/splinter.h
src/skills.o: src/skills.h src/config.h include/splinter.h
@@ -150,6 +151,9 @@ tests/test_delegate: tests/test_delegate.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_delegate.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_builtin: tests/test_builtin.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_builtin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_ack: tests/test_ack.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_ack.c -L. -lsplinter \
-Iinclude -Isrc $(PLUGIN_LDFLAGS)
tests/test_i18n: tests/test_i18n.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_i18n.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_ascii: tests/test_ascii.c $(LIB) $(BINOBJ)
@@ -169,6 +173,7 @@ tests/test_plugin: tests/test_plugin.c $(LIB) $(PLUGIN)
check: $(LIB) $(BIN) $(PLUGIN) $(TESTS)
@set -e; python3 tools/check_i18n.py; python3 tools/gen_www.py --check; \
export LD_LIBRARY_PATH=.; \
for t in $(TESTS); do echo "== $$t"; ./$$t; done; \
echo "all tests passed"
+29
View File
@@ -186,3 +186,32 @@ goal is set. The sidebar shows the goal and how many reminders went out.
It is a reminder, not an instruction: the user can change direction at any
time, and the message says so on its face.
## Zustimmung (ack)
Ein Werkzeug, das etwas aendert, braucht ein Ja von einem Menschen. Der Mensch
kann am Terminal sitzen, am Telefon, oder hinter einem Skript - es koennen
mehrere Kanaele sein, und die erste Antwort gewinnt.
Der Mechanismus ist zwei Dateien, damit nicht zwei Prozesse um dieselben
Telegram-Updates streiten:
~/.splinter/ack/pending/<id> die Anfrage
~/.splinter/ack/<id>.answer die Antwort
Der blockierte Prozess wartet nur auf die Datei. Kanaele sind Melder: sie stossen
die Anfrage nach draussen (ein Kommando pro Kanal), und wer die Antwort liest -
das Gateway, ein Telefon, ein Skript - legt die Antwortdatei ab.
SPLINTER_ACK_CHANNELS="telegram-ask,mastodon-ask" # Kommandos, Komma-getrennt
SPLINTER_ACK_TTL=120 # Sekunden, Default 120
SPLINTER_ACK_DIR=~/.splinter/ack # optional
SPLINTER_ACK_BY=konrad # wer antwortet (beim --ack)
Antworten, aus jedem Prozess:
splinter --ack 4711 yes
splinter --ack 4711 no
splinter --ack-list
Ein Timeout ist ein Nein. Stille ist keine Zustimmung.
+28
View File
@@ -28,6 +28,7 @@
#include "splinter.h"
#include "splinter_plugin.h"
#include "ack.h"
#define KIDS_RULES 64
#define KIDS_RULE_LEN 256
@@ -213,6 +214,33 @@ ask_parent(const char *tool, const char *args, char *reason, size_t reason_len)
char cmd[1024];
int rc;
/*
* The general ack mechanism first: a request file plus whichever channels are
* configured. Then any process may answer - the gateway, the parent's phone,
* or 'splinter --ack <id> yes'. Only without it does the older single command
* get used.
*/
if (getenv("SPLINTER_ACK_CHANNELS") != NULL ||
getenv("SPLINTER_ACK_DIR") != NULL) {
char who[64] = "";
int id = sp_ack_request(tool, args, "kind will etwas aendern",
(int)timeout_s);
if (id < 0) {
snprintf(reason, reason_len, "Zustimmung nicht moeglich");
return 0;
}
(void)sp_ack_notify(id, NULL);
if (sp_ack_wait(id, (int)timeout_s, who, sizeof(who))) {
snprintf(reason, reason_len, "zugestimmt von %s",
who[0] != '\0' ? who : "einem Erwachsenen");
return 1;
}
snprintf(reason, reason_len, "ein Erwachsener hat nicht "
"zugestimmt (Zeit abgelaufen)");
return 0;
}
snprintf(cmd, sizeof(cmd), "%s 'Anfrage: %s %s'",
approve_cmd, tool, args != NULL ? args : "");
rc = system(cmd);
+252
View File
@@ -0,0 +1,252 @@
/*
* 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 "splinter.h"
#include "ack.h"
static char ack_root[512];
static int
ensure_dir(const char *path)
{
if (mkdir(path, 0700) == 0)
return 0;
return errno == EEXIST ? 0 : -1;
}
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);
(void)ensure_dir(root);
(void)ensure_dir(dir);
snprintf(buf, len, "%s/%d%s", dir, id, suffix);
return 0;
}
/*
* Write the request. The id is the pid and a counter, so two processes asking at
* the same time cannot take each other's answer.
*/
int
sp_ack_request(const char *tool, const char *args, const char *why, int ttl)
{
char path[700];
FILE *f;
int id = (int)getpid();
time_t now = time(NULL);
if (ack_id_path(path, sizeof(path), id, "") != 0)
return -1;
f = fopen(path, "w");
if (f == NULL)
return -1;
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));
fclose(f);
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.
*/
int
sp_ack_notify(int id, struct sp_buf *log)
{
const char *chans = getenv("SPLINTER_ACK_CHANNELS");
char list[512], *save = NULL, *tok;
int fired = 0;
if (chans == NULL || *chans == '\0')
return 0;
snprintf(list, sizeof(list), "%s", chans);
for (tok = strtok_r(list, ",", &save); tok != NULL;
tok = strtok_r(NULL, ",", &save)) {
char cmd[1400];
int rc;
while (*tok == ' ')
tok++;
if (*tok == '\0')
continue;
snprintf(cmd, sizeof(cmd), "%s 'Zustimmung %d'", tok, id);
rc = system(cmd);
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];
time_t end = time(NULL) + (ttl > 0 ? ttl : 120);
for (;;) {
FILE *f;
char line[256];
if (ack_id_path(path, sizeof(path), id, ".answer") == 0 &&
(f = fopen(path, "r")) != NULL) {
int ok = 0;
if (fgets(line, sizeof(line), f) != NULL) {
line[strcspn(line, "\r\n")] = '\0';
ok = strncmp(line, "yes", 3) == 0;
if (who != NULL) {
char *sp = strchr(line, ' ');
snprintf(who, who_len, "%s",
sp != NULL ? sp + 1 : "unknown");
}
}
fclose(f);
(void)unlink(path);
return ok;
}
if (time(NULL) >= end)
return 0; /* silence is not consent */
{
struct timespec ts = { 0, 200 * 1000 * 1000 };
(void)nanosleep(&ts, NULL);
}
}
}
/* Any process may answer: the gateway, a helper, a script. */
int
sp_ack_answer(int id, int yes, const char *who)
{
char path[700];
FILE *f;
if (ack_id_path(path, sizeof(path), id, "") != 0)
return -1;
{
FILE *req = fopen(path, "r");
if (req == NULL)
return -1; /* no such request */
fclose(req);
}
if (ack_id_path(path, sizeof(path), id, ".answer") != 0)
return -1;
f = fopen(path, "w");
if (f == NULL)
return -1;
fprintf(f, "%s %s\n", yes ? "yes" : "no",
who != NULL && *who != '\0' ? who : "unknown");
fclose(f);
return 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_puts(out, "keine offenen Anfragen\n");
return 0;
}
for (i = 0; i < n; i++) {
char path[1024], line[256];
FILE *f;
if (ents[i]->d_name[0] == '.') {
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;
}
+22
View File
@@ -0,0 +1,22 @@
/*
* splinter -- approvals that do not care who answers.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef SPLINTER_ACK_H
#define SPLINTER_ACK_H
#include <stddef.h>
#include "splinter.h"
const char *sp_ack_dir(void);
int sp_ack_request(const char *tool, const char *args,
const char *why, int ttl);
int sp_ack_notify(int id, struct sp_buf *log);
int sp_ack_wait(int id, int ttl, char *who, size_t who_len);
int sp_ack_answer(int id, int yes, const char *who);
int sp_ack_list(struct sp_buf *out);
#endif /* SPLINTER_ACK_H */
+94
View File
@@ -22,13 +22,107 @@
#include "splinter.h"
#include "log.h"
#include <pthread.h>
#include "ack.h"
#include "splinter_plugin.h"
#include <pthread.h>
#include "ack.h"
#include "splinter_plugin.h"
struct ack_thread {
int id;
FILE *in;
};
static void *
ack_stdin_thread(void *arg)
{
struct ack_thread *at = arg;
char line[64];
if (fgets(line, sizeof(line), at->in) == NULL) {
free(at);
return NULL;
}
(void)sp_ack_answer(at->id, line[0] == 'y' || line[0] == 'Y', "terminal");
free(at);
return NULL;
}
/*
* Approval through the ack mechanism: the request becomes a file, the channels are
* fired, and whoever answers first wins - the person at this terminal, a phone, or
* a script. Without any channel configured this falls back to the plain prompt, so
* nothing starts hanging that used to be synchronous.
*/
static int
ack_approval(struct sp_agent *a, const struct sp_tool *t, const char *args)
{
const char *chans = getenv("SPLINTER_ACK_CHANNELS");
const char *ttl_s = getenv("SPLINTER_ACK_TTL");
struct sp_buf log;
char who[64] = "";
int id, ttl, yes;
pthread_t th;
int th_started = 0;
if (chans == NULL || *chans == '\0')
return -1;
ttl = ttl_s != NULL && *ttl_s != '\0' ? atoi(ttl_s) : 120;
if (ttl <= 0)
ttl = 120;
id = sp_ack_request(t->name, args,
t->auth == SP_AUTH_MUTATE ? "mutate" : "destructive", ttl);
if (id < 0)
return 0;
if (sp_buf_init(&log) == 0) {
(void)sp_ack_notify(id, &log);
if (log.p != NULL && *log.p != '\0')
fprintf(stderr, "%s", log.p);
sp_buf_free(&log);
}
if (a->cfg->approval_out != NULL)
fprintf(a->cfg->approval_out, "Zustimmung %d: %s %s "
"(%ds) - 'splinter --ack %d yes' oder hier y/N: ", id, t->name,
args, ttl, id);
else
fprintf(stderr, "Zustimmung %d: %s %s (%ds)\n", id, t->name, args,
ttl);
if (a->cfg->approval_out != NULL)
fflush(a->cfg->approval_out);
/* the terminal is one more channel, not a special case */
if (a->cfg->approval_in != NULL) {
struct ack_thread *at = calloc(1, sizeof(*at));
if (at != NULL) {
at->id = id;
at->in = a->cfg->approval_in;
if (pthread_create(&th, NULL, ack_stdin_thread, at) == 0)
th_started = 1;
else
free(at);
}
}
yes = sp_ack_wait(id, ttl, who, sizeof(who));
if (th_started)
(void)pthread_join(th, NULL);
if (yes && who[0] != '\0')
fprintf(stderr, "splinter: zugestimmt von %s\n", who);
return yes;
}
static int
ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args)
{
char line[64];
int verdict;
verdict = ack_approval(a, t, args);
if (verdict >= 0)
return verdict;
if (a->cfg->approve != NULL)
return a->cfg->approve(a->cfg->approve_ctx, t->name, (int)t->auth,
+30 -1
View File
@@ -25,6 +25,7 @@
#include "folder.h"
#include "mcpserver.h"
#include "interview.h"
#include "ack.h"
#include "splinter_plugin.h"
#include "theme.h"
#include "ui.h"
@@ -206,7 +207,7 @@ main(int argc, char **argv)
int have_mock = 0;
int i, rc, chatmode = 0, resume = 0;
int daemon_mode = 0, no_stdin = 0, continue_here = 0;
int interview = 0;
int interview = 0, acklist = 0, ack_id = 0, ack_yes = 0;
const char *mcp_ip = NULL;
int mcp_port = 8098;
struct sp_config conf;
@@ -281,6 +282,11 @@ main(int argc, char **argv)
choose_text = argv[++i];
} else if (!strcmp(argv[i], "--daemon")) {
daemon_mode = 1;
} else if (!strcmp(argv[i], "--ack") && i + 2 < argc) {
ack_id = atoi(argv[++i]);
ack_yes = strcmp(argv[++i], "no") != 0;
} else if (!strcmp(argv[i], "--ack-list")) {
acklist = 1;
} else if (!strcmp(argv[i], "--interview")) {
interview = 1;
} else if (!strcmp(argv[i], "--mcp-server-listen")) {
@@ -580,6 +586,29 @@ main(int argc, char **argv)
}
ag.sess = &sess;
(void)sp_log_open(sess.dir, sess.id);
/*
* Answering is not running an agent: any process may drop the answer, which
* is the whole point of the file behind it.
*/
if (acklist) {
struct sp_buf out;
if (sp_buf_init(&out) != 0)
return 1;
(void)sp_ack_list(&out);
printf("%s", out.p != NULL ? out.p : "");
sp_buf_free(&out);
return 0;
}
if (ack_id != 0) {
if (sp_ack_answer(ack_id, ack_yes, getenv("SPLINTER_ACK_BY")) != 0) {
fprintf(stderr, "splinter: no such request %d\n", ack_id);
return 1;
}
printf("%s %d\n", ack_yes ? "yes" : "no", ack_id);
return 0;
}
if (interview) {
rc = sp_interview_run(sess.dir, &err);
if (rc != 0)
+90
View File
@@ -0,0 +1,90 @@
/*
* The ack mechanism: a request is a file, an answer is a file, and any process
* may write it. Silence is not consent.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "ack.h"
#include "splinter.h"
static int checks, fails;
#define CHECK(cond, what) do { \
checks++; \
if (!(cond)) { \
fails++; \
printf("FAIL %s (line %d)\n", what, __LINE__); \
} else { \
printf("ok %s\n", what); \
} \
} while (0)
static int answer_id;
static int answer_yes;
static void *
answerer(void *arg)
{
(void)arg;
{ struct timespec ts = { 0, 300000000L }; (void)nanosleep(&ts, NULL); }
(void)sp_ack_answer(answer_id, answer_yes, "test-phone");
return NULL;
}
int
main(void)
{
char out[2048], who[64];
struct sp_buf log;
int id, yes;
pthread_t th;
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack", 1);
(void)system("rm -rf /var/tmp/splinter_test_ack");
id = sp_ack_request("run", "{\"command\":\"ls\"}", "mutate", 5);
CHECK(id > 0, "request is written and returns an id");
/* the answer comes from another thread, like another process would */
answer_id = id;
answer_yes = 1;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 1, "an answer of yes is read back");
CHECK(strcmp(who, "test-phone") == 0, "the answer says who gave it");
id = sp_ack_request("run", "{\"command\":\"rm -rf /\"}", "destructive", 1);
answer_id = id;
answer_yes = 0;
(void)pthread_create(&th, NULL, answerer, NULL);
yes = sp_ack_wait(id, 5, who, sizeof(who));
(void)pthread_join(th, NULL);
CHECK(yes == 0, "an answer of no is read back as no");
id = sp_ack_request("run", "slow", "mutate", 1);
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
"a timeout is a no, not a yes");
CHECK(sp_ack_answer(999999, 1, "nobody") != 0,
"answering a request that does not exist fails");
id = sp_ack_request("edit", "{\"path\":\"/etc/passwd\"}", "mutate", 30);
if (sp_buf_init(&log) == 0) {
(void)sp_ack_list(&log);
snprintf(out, sizeof(out), "%s", log.p != NULL ? log.p : "");
sp_buf_free(&log);
CHECK(strstr(out, "\"path\":\"/etc/passwd\"") != NULL,
"the pending list shows what is being asked");
}
(void)sp_ack_answer(id, 0, "test");
printf("\n%d checks, %d failures\n", checks, fails);
(void)system("rm -rf /var/tmp/splinter_test_ack");
return fails == 0 ? 0 : 1;
}