Files
splinter/tests/test_compactor.c
T
Konrad b8295ea474 splinter: compaction as plugins (lcm, cps)
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.
2026-11-07 13:44:43 +00:00

140 lines
3.7 KiB
C

/*
* splinter -- compactor plugin tests.
*
* lcm is deterministic: give it a conversation over budget and it must cut the
* front, keep the tail, and leave a system message that says what went.
* cps is a pipeline: the first call prepares a plan on its worker, a later call
* applies it. Both are exercised against the real .so files.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
nap_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
static void
fill(struct sp_conv *c, int n)
{
int i;
sp_conv_init(c);
sp_conv_push(c, "system", NULL, "you are a harness");
for (i = 0; i < n; i++) {
char text[128];
snprintf(text, sizeof(text), "message number %d with some padding "
"so the conversation grows past a small budget", i);
sp_conv_push(c, (i % 2) ? "assistant" : "user", NULL, text);
}
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host, *host2;
struct sp_buf err;
struct sp_conv conv;
size_t before, after, compactions, failed;
unsigned turns;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
/* ---- lcm ---- */
sp_buf_clear(&err);
CHECK(sp_plugin_load(host, "plugins/lcm/lcm.so", &err) == 0);
/* under budget: untouched */
fill(&conv, 4);
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host, &conv, 1000000) == 0);
CHECK(sp_conv_bytes(&conv) == before);
CHECK(conv.n == 5);
sp_conv_free(&conv);
/* over budget: cut from the front, system prompt stays, summary inserted */
fill(&conv, 20);
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host, &conv, 400) == 1);
after = sp_conv_bytes(&conv);
CHECK(after < before);
CHECK(strcmp(conv.v[0].role, "system") == 0);
CHECK(strcmp(conv.v[0].content, "you are a harness") == 0);
CHECK(strcmp(conv.v[1].role, "system") == 0);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
CHECK(strstr(conv.v[1].content, "compacted out") != NULL);
/* the tail survived verbatim */
CHECK(strstr(conv.v[conv.n - 1].content, "message number") != NULL);
sp_conv_free(&conv);
/* ---- cps: prepare on the worker, apply on a later call ---- */
/* its own host: loaded next to lcm, lcm would do the work first */
host2 = sp_host_new(&reg, &ag, NULL);
CHECK(host2 != NULL);
sp_buf_clear(&err);
CHECK(sp_plugin_load(host2, "plugins/cps/cps.so", &err) == 0);
fill(&conv, 20);
before = sp_conv_bytes(&conv);
/* first call only hands the index over */
CHECK(sp_host_compact(host2, &conv, 400) == 0);
CHECK(sp_conv_bytes(&conv) == before);
nap_ms(350); /* its worker ticks every 100 ms */
/* the plan is ready now: the next call applies it */
{
int rc = sp_host_compact(host2, &conv, 400);
CHECK(rc == 1);
if (rc == 1) {
CHECK(sp_conv_bytes(&conv) < before);
CHECK(strstr(conv.v[0].content, "you are a harness") != NULL);
CHECK(strstr(conv.v[1].content, "[cps]") != NULL);
}
}
sp_conv_free(&conv);
/* ---- stats are counted, not claimed ---- */
sp_host_compaction_stats(host, &compactions, &failed, &turns);
CHECK(compactions >= 1);
CHECK(failed == 0);
/* ---- a plugin that is not a compactor cannot register one ---- */
CHECK(sp_host_add_compactor(host, NULL) == -1);
sp_host_free(host2);
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_compactor: ok\n");
return failures == 0 ? 0 : 1;
}