Compaction is not a flag in the loop; it is a compactor registered by a plugin and run on the main thread before every request, with the whole conversation in hand. Returning 1 means changed, 0 nothing to do, -1 failed - and a failure is counted, not fatal. lcm: deterministic lossy compression. Over budget, it keeps the leading system prompt and the last six messages and inserts one system message that says how many messages went, of which roles, and quotes the head of the oldest dropped turn. No second model, so a run stays reproducible and costs nothing extra. cps: continuous parallel compression. The worker thread decides what would go from an index it got under a mutex (role, size, 64 bytes of head per message) and never touches the conversation; the main thread splices the prepared plan in one pass when the budget is hit. The sidebar line shows applied/prepared counts or a ready plan, so the claim is visible. The limit is in both files: they can decide which messages go, not summarise their text, because the text is not the worker's to read. Ten test binaries green; test_compactor drives both against their real .so files, including that lcm leaves the system prompt first, inserts the [lcm] record at index 1, keeps the tail verbatim, and that cps needs two calls (prepare, then apply) to change anything.
208 lines
5.1 KiB
C
208 lines
5.1 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"
|
|
|
|
#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;
|
|
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;
|
|
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);
|
|
if (cps.ready && cps.n == conv->n &&
|
|
(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);
|
|
|
|
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), "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), "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;
|
|
}
|