The gateway entry point accepted an empty token and then polled with it; the guard belongs there so a daemon that cannot listen says so instead of looking alive. Honest limit of this change: it is NOT the explanation for the running daemon. That one had a token (resolved from the token file) and failed with 'curl: (35) Recv failure: Connection reset by peer' thousands of times, and with the env variable removed it now fails with 'curl failed (-1)' and stops - a different error, not the missing token. The guard only catches the case where neither the environment nor the file provide one. What this commit does prove: with a config that has endpoint, model and telegram_allow, the daemon starts, reports 'gateway listening (allow: 8300935)', and stops on the failing poll instead of looping. What it does not prove: that a valid token makes it work - that needs a real message.
223 lines
5.8 KiB
C
223 lines
5.8 KiB
C
/*
|
|
* splinter -- cps: continuous parallel compression.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The idea: the expensive part of compaction is deciding what to cut. That can
|
|
* be done while nothing else happens, on a thread of its own, so the moment the
|
|
* budget is hit the main thread only has to splice an already-computed plan.
|
|
*
|
|
* The worker never sees the conversation. It sees an index: the role and the
|
|
* size of each message, copied under a mutex by the main thread. That is why
|
|
* this is safe without a lock around the conversation itself, and it is also
|
|
* its limit: it can decide *which* messages go, not summarise their text.
|
|
*
|
|
* The widget in the sidebar shows how often a plan was prepared and what the
|
|
* last one would cut - so "continuous" is visible, not claimed.
|
|
*/
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
#include "i18n.h"
|
|
|
|
#define SNAP_MAX 256
|
|
#define KEEP_TAIL 6
|
|
#define SUMMARY_MAX 512
|
|
|
|
struct snap_msg {
|
|
char role[16];
|
|
size_t len;
|
|
char head[64];
|
|
};
|
|
|
|
static struct {
|
|
pthread_mutex_t lock;
|
|
size_t n; /* messages in the snapshot, <= SNAP_MAX */
|
|
size_t total; /* messages in the conversation then */
|
|
size_t bytes; /* and its size: a stale plan is refused */
|
|
struct snap_msg m[SNAP_MAX];
|
|
int requested;
|
|
int ready;
|
|
size_t cut; /* drop [1, cut) */
|
|
char summary[SUMMARY_MAX];
|
|
size_t before;
|
|
size_t after;
|
|
unsigned long prepared;
|
|
unsigned long applied;
|
|
unsigned long idle;
|
|
char widget[256];
|
|
} cps;
|
|
|
|
static void
|
|
copy_snapshot(const struct sp_conv *conv)
|
|
{
|
|
size_t i, n = conv->n > SNAP_MAX ? SNAP_MAX : conv->n;
|
|
|
|
pthread_mutex_lock(&cps.lock);
|
|
cps.n = n;
|
|
cps.total = conv->n;
|
|
cps.bytes = sp_conv_bytes(conv);
|
|
for (i = 0; i < n; i++) {
|
|
const struct sp_msg *m = &conv->v[i];
|
|
|
|
snprintf(cps.m[i].role, sizeof(cps.m[i].role), "%s",
|
|
m->role != NULL ? m->role : "?");
|
|
cps.m[i].len = m->content != NULL ? strlen(m->content) : 0;
|
|
snprintf(cps.m[i].head, sizeof(cps.m[i].head), "%s",
|
|
m->content != NULL ? m->content : "");
|
|
}
|
|
cps.requested = 1;
|
|
cps.ready = 0;
|
|
pthread_mutex_unlock(&cps.lock);
|
|
}
|
|
|
|
static int
|
|
cps_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
|
unsigned turn)
|
|
{
|
|
size_t i, at;
|
|
int apply = 0;
|
|
|
|
(void)turn;
|
|
if (conv == NULL || conv->n <= KEEP_TAIL + 1)
|
|
return 0;
|
|
|
|
pthread_mutex_lock(&cps.lock);
|
|
/*
|
|
* The plan fits this conversation only if it is the one it was made for.
|
|
* Comparing against cps.n, which stops at SNAP_MAX, meant that past 256
|
|
* messages no plan ever applied - compaction just stopped.
|
|
*/
|
|
if (cps.ready && cps.total == conv->n &&
|
|
cps.bytes == sp_conv_bytes(conv) &&
|
|
(budget == 0 || sp_conv_bytes(conv) > budget)) {
|
|
apply = 1;
|
|
} else {
|
|
pthread_mutex_unlock(&cps.lock);
|
|
copy_snapshot(conv);
|
|
return 0; /* plan is being prepared */
|
|
}
|
|
|
|
if (apply) {
|
|
size_t cut = cps.cut;
|
|
char summary[SUMMARY_MAX];
|
|
struct sp_msg msg;
|
|
size_t inserted;
|
|
|
|
snprintf(summary, sizeof(summary), "%s", cps.summary);
|
|
cps.ready = 0;
|
|
cps.applied++;
|
|
pthread_mutex_unlock(&cps.lock);
|
|
|
|
/* a tool result goes with the call it answers, never alone */
|
|
while (cut < conv->n && conv->v[cut].role != NULL &&
|
|
strcmp(conv->v[cut].role, "tool") == 0)
|
|
cut++;
|
|
if (cut <= 1 || cut >= conv->n)
|
|
return 0;
|
|
for (i = 1; i < cut; i++) {
|
|
free(conv->v[i].role);
|
|
free(conv->v[i].name);
|
|
free(conv->v[i].call_id);
|
|
free(conv->v[i].content);
|
|
free(conv->v[i].calls);
|
|
}
|
|
memmove(&conv->v[1], &conv->v[cut],
|
|
(conv->n - cut) * sizeof(*conv->v));
|
|
conv->n = 1 + (conv->n - cut);
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
msg.role = "system";
|
|
msg.content = summary;
|
|
at = (conv->n > 0 && strcmp(conv->v[0].role, "system") == 0) ?
|
|
1 : 0;
|
|
if (sp_conv_insert(conv, at, &msg) != 0)
|
|
return -1;
|
|
inserted = at;
|
|
(void)inserted;
|
|
return 1;
|
|
}
|
|
pthread_mutex_unlock(&cps.lock);
|
|
(void)h;
|
|
return 0;
|
|
}
|
|
|
|
/* the worker side: called from the widget's own thread, never from the loop */
|
|
static int
|
|
cps_tick(struct sp_host *h, const char *name, void *ctx)
|
|
{
|
|
size_t i, total = 0, cut, tail_from;
|
|
char line[256];
|
|
int did = 0;
|
|
|
|
(void)ctx;
|
|
pthread_mutex_lock(&cps.lock);
|
|
if (!cps.requested) {
|
|
cps.idle++;
|
|
snprintf(line, sizeof(line), sp_td("cps.idle", "idle (applied %lu, prepared %lu)"),
|
|
cps.applied, cps.prepared);
|
|
pthread_mutex_unlock(&cps.lock);
|
|
return sp_host_widget_text(h, name, line);
|
|
}
|
|
cps.requested = 0;
|
|
|
|
for (i = 0; i < cps.n; i++)
|
|
total += cps.m[i].len + 16;
|
|
tail_from = cps.n > KEEP_TAIL ? cps.n - KEEP_TAIL : 1;
|
|
cut = tail_from;
|
|
if (cut <= 1)
|
|
cut = 1;
|
|
snprintf(cps.summary, sizeof(cps.summary),
|
|
"[cps] %zu messages cut from the front to stay under budget; "
|
|
"verbatim tail of %zu messages follows. Oldest cut turn began with: "
|
|
"\"%.60s\"", cut - 1, cps.n - cut,
|
|
cps.n > 1 ? cps.m[1].head : "");
|
|
cps.before = total;
|
|
cps.after = 0;
|
|
for (i = cut; i < cps.n; i++)
|
|
cps.after += cps.m[i].len + 16;
|
|
cps.cut = cut;
|
|
cps.ready = 1;
|
|
cps.prepared++;
|
|
did = 1;
|
|
snprintf(line, sizeof(line), sp_td("cps.plan", "plan ready: cut %zu/%zu, %zu -> %zu B"),
|
|
cut - 1, cps.n, cps.before, cps.after);
|
|
pthread_mutex_unlock(&cps.lock);
|
|
(void)did;
|
|
return sp_host_widget_text(h, name, line);
|
|
}
|
|
|
|
static const struct sp_compactor cps_compactor = { "cps", cps_compact };
|
|
|
|
static int
|
|
cps_init(struct sp_host *h)
|
|
{
|
|
static const struct sp_widget w = {
|
|
"cps", cps_tick, NULL, NULL, 100
|
|
};
|
|
|
|
pthread_mutex_init(&cps.lock, NULL);
|
|
cps.ready = 0;
|
|
cps.requested = 0;
|
|
if (sp_host_add_compactor(h, &cps_compactor) != 0)
|
|
return -1;
|
|
if (sp_host_add_widget(h, &w) != 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static const struct sp_plugin cps_plugin = {
|
|
SP_PLUGIN_ABI, "cps", "1.0.0", cps_init, NULL, NULL
|
|
};
|
|
|
|
const struct sp_plugin *
|
|
sp_plugin_describe(void)
|
|
{
|
|
return &cps_plugin;
|
|
}
|