Files
splinter/tests/test_ack.c
T
lotherkandClaude Opus 5.5 25a616a87f review fixes, third round: the gateway asks in its chat, approvals say what
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>
2026-11-09 06:13:09 +00:00

171 lines
5.4 KiB
C

/*
* 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 <sys/stat.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");
/*
* ---- an answer belongs to one request, once, in time ----
* The id used to be the pid: a "yes" that came too late stayed on disk
* and approved the next request of the same process unseen.
*/
{
int a, b;
a = sp_ack_request("run", "first", "destructive", 1);
b = sp_ack_request("run", "second", "destructive", 1);
CHECK(a > 0 && b > 0 && a != b,
"two requests of one process get two ids");
CHECK(a != (int)getpid(), "the id is not the pid");
(void)sp_ack_answer(b, 0, "test");
(void)sp_ack_wait(b, 1, who, sizeof(who));
CHECK(sp_ack_wait(a, 1, who, sizeof(who)) == 0,
"an unanswered request times out as no");
CHECK(sp_ack_answer(a, 1, "late") != 0,
"an answer after the timeout is refused");
id = sp_ack_request("run", "third", "destructive", 1);
CHECK(sp_ack_wait(id, 1, who, sizeof(who)) == 0,
"a late yes does not approve the next request");
id = sp_ack_request("run", "fourth", "destructive", 5);
CHECK(sp_ack_answer(id, 0, "first") == 0, "the first answer counts");
CHECK(sp_ack_answer(id, 1, "second") != 0,
"a second answer to the same request is refused");
CHECK(sp_ack_wait(id, 5, who, sizeof(who)) == 0 &&
strcmp(who, "first") == 0, "the first answer wins");
}
/*
* ---- the channel message says what is approved, and is only data ----
* It used to be "Zustimmung <id>" alone, so a phone said yes to a
* number.
*/
{
const char *chan = "/var/tmp/splinter_test_ack/chan.sh";
const char *got = "/var/tmp/splinter_test_ack/chan.out";
const char *mark = "/var/tmp/splinter_test_ack/pwned";
FILE *f = fopen(chan, "w");
char msg[1024] = "";
if (f != NULL) {
fprintf(f, "#!/bin/sh\nprintf '%%s' \"$1\" > %s\n", got);
fclose(f);
}
(void)chmod(chan, 0700);
(void)setenv("SPLINTER_ACK_CHANNELS", chan, 1);
id = sp_ack_request("run",
"{\"command\":\"x'; touch /var/tmp/splinter_test_ack/pwned; '\"}",
"destructive", 5);
CHECK(sp_ack_notify(id, NULL) == 1, "the channel was fired");
f = fopen(got, "r");
if (f != NULL) {
size_t n = fread(msg, 1, sizeof(msg) - 1, f);
msg[n] = '\0';
fclose(f);
}
CHECK(strstr(msg, "run") != NULL && strstr(msg, "touch") != NULL,
"the message names the tool and its arguments");
CHECK(access(mark, F_OK) != 0,
"a quote in the arguments runs nothing");
(void)sp_ack_answer(id, 0, "test");
(void)sp_ack_wait(id, 1, who, sizeof(who));
(void)unsetenv("SPLINTER_ACK_CHANNELS");
}
/* ---- a directory others may write into is not trusted with a yes ---- */
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
(void)system("mkdir -p /var/tmp/splinter_test_ack_open && "
"chmod 0777 /var/tmp/splinter_test_ack_open");
(void)setenv("SPLINTER_ACK_DIR", "/var/tmp/splinter_test_ack_open", 1);
CHECK(sp_ack_request("run", "x", "destructive", 1) < 0,
"a world-writable ack directory is refused");
(void)system("rm -rf /var/tmp/splinter_test_ack_open");
printf("\n%d checks, %d failures\n", checks, fails);
(void)system("rm -rf /var/tmp/splinter_test_ack");
return fails == 0 ? 0 : 1;
}