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>
282 lines
6.9 KiB
C
282 lines
6.9 KiB
C
/*
|
|
* splinter -- memory, one bucket per user.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The model may remember things. It may not remember them across users.
|
|
*
|
|
* user_id := the account this process runs as ($USER, else the passwd entry)
|
|
*
|
|
* Every request carries that id, so a memory written by /root is invisible to
|
|
* lotherk and the other way round. The id is not configurable on purpose: a
|
|
* config key is a thing to forget, and the process already knows who it is.
|
|
*
|
|
* Talks mem0's REST API with curl:
|
|
* POST <url>/v1/memories/ {messages, user_id}
|
|
* POST <url>/v1/memories/search/ {query, user_id}
|
|
*/
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <pwd.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
#include "config.h"
|
|
#include "memory.h"
|
|
#include "curl.h"
|
|
|
|
static const struct sp_config *mem_cfg;
|
|
|
|
/* Who is asking. Not a setting: a fact about the process. */
|
|
const char *
|
|
sp_memory_user(void)
|
|
{
|
|
static char name[64];
|
|
const char *env = getenv("USER");
|
|
struct passwd *pw;
|
|
|
|
if (name[0] != '\0')
|
|
return name;
|
|
if (env != NULL && *env != '\0') {
|
|
snprintf(name, sizeof(name), "%s", env);
|
|
return name;
|
|
}
|
|
pw = getpwuid(getuid());
|
|
if (pw != NULL && pw->pw_name != NULL) {
|
|
snprintf(name, sizeof(name), "%s", pw->pw_name);
|
|
return name;
|
|
}
|
|
snprintf(name, sizeof(name), "uid-%ld", (long)getuid());
|
|
return name;
|
|
}
|
|
|
|
/*
|
|
* One POST, body from a buffer, answer captured.
|
|
*
|
|
* memory_key_env used to be put into the child's environment as
|
|
* SPLINTER_MEM_KEY, which curl never reads: no request carried a key, and a
|
|
* mem0 that wanted one refused every call. It is now a header, on curl's stdin
|
|
* (sp_curl_start), in mem0's "Authorization: Token <key>" form.
|
|
*/
|
|
static int
|
|
mem_post(const char *url, const char *body, struct sp_buf *out,
|
|
struct sp_buf *err)
|
|
{
|
|
const char *args[] = { "-sS", "-m", "30", "-X", "POST", "-H",
|
|
"Content-Type: application/json", "--data-binary",
|
|
body, NULL };
|
|
const char *hdrs[2] = { NULL, NULL };
|
|
char auth[600];
|
|
pid_t child;
|
|
int fd;
|
|
|
|
if (mem_cfg != NULL && mem_cfg->memory_key_env[0] != '\0') {
|
|
const char *key = getenv(mem_cfg->memory_key_env);
|
|
|
|
if (key != NULL && *key != '\0') {
|
|
snprintf(auth, sizeof(auth), "Authorization: Token %s", key);
|
|
hdrs[0] = auth;
|
|
}
|
|
}
|
|
fd = sp_curl_start(args, url, hdrs, 1, &child, err);
|
|
memset(auth, 0, sizeof(auth));
|
|
if (fd < 0)
|
|
return -1;
|
|
{
|
|
struct pollfd pfd;
|
|
char chunk[4096];
|
|
|
|
for (;;) {
|
|
ssize_t got;
|
|
|
|
pfd.fd = fd;
|
|
pfd.events = POLLIN;
|
|
if (poll(&pfd, 1, 35000) <= 0) {
|
|
(void)kill(child, SIGKILL);
|
|
break;
|
|
}
|
|
got = read(fd, chunk, sizeof(chunk));
|
|
if (got <= 0)
|
|
break;
|
|
if (sp_buf_add(out, chunk, (size_t)got) != 0)
|
|
break;
|
|
}
|
|
}
|
|
close(fd);
|
|
{
|
|
int st = 0;
|
|
|
|
(void)waitpid(child, &st, 0);
|
|
if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) {
|
|
sp_buf_printf(err, "memory: curl failed");
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
have_url(struct sp_buf *out)
|
|
{
|
|
if (mem_cfg == NULL || mem_cfg->memory_url[0] == '\0') {
|
|
sp_buf_puts(out, "error: no memory configured (memory_url in the "
|
|
"config)\n");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int
|
|
tool_memory_add(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf body, reply, err;
|
|
const char *text = NULL;
|
|
mcp_json *v;
|
|
|
|
(void)ctx;
|
|
if (!have_url(out))
|
|
return 0;
|
|
if (sp_buf_init(&body) != 0 || sp_buf_init(&reply) != 0 ||
|
|
sp_buf_init(&err) != 0)
|
|
return -1;
|
|
if (args != NULL && args[0] == '{') {
|
|
v = mcp_parse(args);
|
|
if (v != NULL) {
|
|
text = mcp_arg_string(v, "text");
|
|
if (text != NULL)
|
|
(void)sp_buf_puts(&body, text);
|
|
mcp_json_free(v);
|
|
}
|
|
}
|
|
text = body.p;
|
|
if (text == NULL || *text == '\0') {
|
|
sp_buf_puts(out, "error: 'text' is required\n");
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&err);
|
|
return 0;
|
|
}
|
|
{
|
|
struct sp_buf req;
|
|
|
|
if (sp_buf_init(&req) != 0)
|
|
return -1;
|
|
sp_buf_printf(&req, "{\"messages\":[{\"role\":\"user\",\"content\":\"");
|
|
(void)sp_json_escape(&req, text);
|
|
sp_buf_printf(&req, "\"}],\"user_id\":\"");
|
|
(void)sp_json_escape(&req, sp_memory_user());
|
|
sp_buf_puts(&req, "\"}");
|
|
{
|
|
struct sp_buf url;
|
|
|
|
if (sp_buf_init(&url) != 0) {
|
|
sp_buf_free(&req);
|
|
return -1;
|
|
}
|
|
sp_buf_printf(&url, "%s/v1/memories/", mem_cfg->memory_url);
|
|
if (mem_post(url.p, req.p, &reply, &err) != 0)
|
|
sp_buf_printf(out, "error: %s\n",
|
|
err.p != NULL ? err.p : "memory add failed");
|
|
else
|
|
sp_buf_printf(out, "remembered for '%s'\n",
|
|
sp_memory_user());
|
|
sp_buf_free(&url);
|
|
}
|
|
sp_buf_free(&req);
|
|
}
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&err);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_memory_search(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf body, reply, err, req, url;
|
|
const char *query = NULL;
|
|
mcp_json *v;
|
|
|
|
(void)ctx;
|
|
if (!have_url(out))
|
|
return 0;
|
|
if (sp_buf_init(&body) != 0 || sp_buf_init(&reply) != 0 ||
|
|
sp_buf_init(&err) != 0 || sp_buf_init(&req) != 0 ||
|
|
sp_buf_init(&url) != 0)
|
|
return -1;
|
|
if (args != NULL && args[0] == '{') {
|
|
v = mcp_parse(args);
|
|
if (v != NULL) {
|
|
const char *q = mcp_arg_string(v, "query");
|
|
|
|
if (q != NULL)
|
|
(void)sp_buf_puts(&body, q);
|
|
mcp_json_free(v);
|
|
}
|
|
}
|
|
query = body.p;
|
|
if (query == NULL || *query == '\0') {
|
|
sp_buf_puts(out, "error: 'query' is required\n");
|
|
goto done;
|
|
}
|
|
sp_buf_puts(&req, "{\"query\":\"");
|
|
(void)sp_json_escape(&req, query);
|
|
sp_buf_puts(&req, "\",\"user_id\":\"");
|
|
(void)sp_json_escape(&req, sp_memory_user());
|
|
sp_buf_puts(&req, "\"}");
|
|
sp_buf_printf(&url, "%s/v1/memories/search/", mem_cfg->memory_url);
|
|
if (mem_post(url.p, req.p, &reply, &err) != 0) {
|
|
sp_buf_printf(out, "error: %s\n",
|
|
err.p != NULL ? err.p : "memory search failed");
|
|
goto done;
|
|
}
|
|
if (reply.p == NULL || strstr(reply.p, "memory") == NULL) {
|
|
sp_buf_puts(out, "(nothing remembered)\n");
|
|
goto done;
|
|
}
|
|
sp_buf_printf(out, "%s\n", reply.p);
|
|
done:
|
|
sp_buf_free(&body);
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&req);
|
|
sp_buf_free(&url);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_memory_register(struct sp_registry *r, const struct sp_config *cfg)
|
|
{
|
|
struct sp_tool t;
|
|
|
|
mem_cfg = cfg;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "memory_search";
|
|
t.description = "Search your own memories (scoped to the user this "
|
|
"process runs as).";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"query\":"
|
|
"{\"type\":\"string\"}},\"required\":[\"query\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_memory_search;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "memory_add";
|
|
t.description = "Remember one fact for this user.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"text\":"
|
|
"{\"type\":\"string\"}},\"required\":[\"text\"]}";
|
|
t.auth = SP_AUTH_MUTATE;
|
|
t.run = tool_memory_add;
|
|
return sp_registry_add(r, &t);
|
|
}
|