Files
splinter/tests/test_compactor.c
T
lotherk 80154055f1 lcm: do not grow the conversation when the cut adds as much as it removes
/resume compacted N messages to N+1 every time. With keep_tail 6 and a short
history, the cut removed one message, then added the summary and the pinned
question: net +1. The context never shrank and the next /resume repeated it.

A message rewrite now happens only when it removes more messages than it adds
(drop > 1 + pinned). If the cut is that small, the old tool-output trim is kept
and the message list is left alone. test_compactor covers the 8-message case
(no change, no [lcm] row) and the 10-message case (10 -> 9).

make check: all tests passed.
2026-11-18 12:47:30 +00:00

425 lines
12 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 <stdlib.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
/* the status table is the plugin host's, not the compactor's: many segments */
static void
dummy_status(struct sp_host *h, char *buf, size_t len)
{
(void)h;
snprintf(buf, len, "x");
}
#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);
}
}
static int
echo_tool(const char *args, struct sp_buf *out, void *ctx)
{
(void)ctx;
return sp_buf_puts(out, args);
}
/*
* Drive the real turn loop with lcm loaded and a byte budget. The mock echoes a
* fixed payload, so the conversation grows by a known amount per tool round;
* the only way the turn can finish is that the compactor kept every request
* under 'budget'. Returns the agent's rc, fills *stats.
*/
static int
run_budget(size_t budget, size_t calls, size_t payload,
struct sp_agent_stats *stats)
{
struct sp_registry r;
struct sp_tool t;
struct sp_agent ag;
struct sp_agent_config cfg;
struct sp_mock m;
struct sp_host *host = NULL;
struct sp_buf answer, err, reply, chunk;
size_t i;
int have_mock = 0, rc = -1;
memset(&r, 0, sizeof(r));
memset(&t, 0, sizeof(t));
t.name = "echo";
t.description = "echo the argument";
t.auth = SP_AUTH_READ;
t.run = echo_tool;
(void)sp_registry_add(&r, &t);
memset(&ag, 0, sizeof(ag));
memset(&cfg, 0, sizeof(cfg));
cfg.max_steps = calls + 2;
cfg.max_bytes = budget;
cfg.autonomous = 1;
ag.reg = &r;
ag.cfg = &cfg;
memset(&answer, 0, sizeof(answer));
memset(&err, 0, sizeof(err));
memset(&reply, 0, sizeof(reply));
memset(&chunk, 0, sizeof(chunk));
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0 ||
sp_buf_init(&reply) != 0 || sp_buf_init(&chunk) != 0)
goto out;
host = sp_host_new(&r, &ag, NULL);
if (host == NULL || sp_plugin_load(host, "plugins/lcm/lcm.so", &err) != 0)
goto out;
ag.host = host;
if (sp_mock_init(&m) != 0)
goto out;
have_mock = 1;
for (i = 0; i < payload; i++)
if (sp_buf_puts(&chunk, "x") != 0)
goto out;
for (i = 0; i < calls; i++) {
sp_buf_clear(&reply);
if (sp_buf_printf(&reply, "tool echo %s", chunk.p) != 0 ||
sp_mock_push(&m, 200, reply.p) != 0)
goto out;
}
if (sp_mock_push(&m, 200, "answer done") != 0)
goto out;
ag.tr = sp_mock_transport(&m);
rc = sp_agent_run(&ag, "start", &answer, &err);
if (stats != NULL)
*stats = ag.stats;
if (rc == 0) {
CHECK(answer.p != NULL && strcmp(answer.p, "done") == 0);
} else {
fprintf(stderr, "FAIL budget %zu: rc %d: %s\n", budget, rc,
err.p != NULL ? err.p : "(no error)");
failures++;
}
out:
if (have_mock)
sp_mock_free(&m);
sp_host_free(host);
sp_buf_free(&answer);
sp_buf_free(&err);
sp_buf_free(&reply);
sp_buf_free(&chunk);
sp_registry_free(&r);
return rc;
}
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);
/*
* An old tool output is cut before any message is dropped: head and
* tail survive, the middle is still in the session log for recall.
*/
{
char *big = malloc(20001);
size_t i;
CHECK(big != NULL);
memset(big, 'x', 20000);
big[20000] = '\0';
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "run it") == 0);
CHECK(sp_conv_push(&conv, "tool", NULL, big) == 0);
for (i = 0; i < 8; i++)
CHECK(sp_conv_push(&conv, i % 2 ? "assistant" : "user",
NULL, "message number x") == 0);
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host, &conv, before - 10000) == 1);
CHECK(conv.n == 11); /* nothing dropped */
CHECK(strstr(conv.v[2].content, "[lcm]") != NULL);
CHECK(strstr(conv.v[2].content, "recall") != NULL);
CHECK(sp_conv_bytes(&conv) < before);
free(big);
sp_conv_free(&conv);
}
/*
* The newest user message is the task: a long tool loop must not push it
* out of the tail. It comes back right after the summary.
*/
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
CHECK(sp_conv_push(&conv, "user", NULL,
"the question that matters") == 0);
{
size_t i;
for (i = 0; i < 12; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
"message number x") == 0);
}
CHECK(sp_host_compact(host, &conv, 300) == 1);
CHECK(strcmp(conv.v[0].role, "system") == 0);
CHECK(strcmp(conv.v[1].role, "system") == 0);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
CHECK(strstr(conv.v[1].content, "current question was kept") != NULL);
CHECK(strcmp(conv.v[2].role, "user") == 0);
CHECK(strstr(conv.v[2].content, "the question that matters") != 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);
/*
* The status table used to be capped with the compactors' eight slots.
* With status(7) + lcm(1) the ninth segment (junction) failed and the
* host skipped that plugin entirely. Many segments must fit.
*/
{
static const struct sp_status d = { "dummy", dummy_status };
int i;
for (i = 0; i < 9; i++)
CHECK(sp_host_add_status(host, &d) == 0);
}
/*
* A cut that removes one message and adds a summary plus the pinned
* question would grow the conversation. /resume did that every time
* (N -> N+1). It must leave the conversation alone instead.
*/
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "the question") == 0);
{
size_t i;
for (i = 0; i < 6; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
"message number x") == 0);
}
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host, &conv, before - 100) == 0);
CHECK(conv.n == 8);
CHECK(strstr(conv.v[1].content, "[lcm]") == NULL);
sp_conv_free(&conv);
/* one more old turn makes the cut a real reduction again */
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "the question") == 0);
{
size_t i;
for (i = 0; i < 8; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
"message number x") == 0);
}
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host, &conv, before - 100) == 1);
CHECK(conv.n == 9);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
sp_conv_free(&conv);
/*
* The sidebar shows those numbers, not an estimate: the widget is the
* compactor's own counter, and the status line carries one segment.
*/
nap_ms(2200); /* the widget ticks every two seconds */
{
char snap[2048];
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
CHECK(strstr(snap, "lcm: ctx ") != NULL);
CHECK(strstr(snap, "compacted ") != NULL);
CHECK(strstr(snap, "cut ") != NULL);
CHECK(strstr(snap, "tools trimmed ") != NULL);
snap[0] = '\0';
CHECK(sp_host_status_text(host, snap, sizeof(snap)) > 0);
CHECK(strstr(snap, "compact ") != NULL);
}
/*
* ---- lcm never leaves a tool result without its call ----
* system, user, then [assistant+tool_calls, tool, tool] x 4, then a user:
* fifteen messages, and the verbatim tail of six would begin with a tool
* result whose assistant turn is cut away. A provider answers that with
* a 400.
*/
{
struct sp_conv tc;
struct sp_msg m;
char big[300];
size_t k;
int orphan = 0;
sp_conv_init(&tc);
memset(big, 'x', sizeof(big) - 1);
big[sizeof(big) - 1] = '\0';
sp_conv_push(&tc, "system", NULL, "you are a harness");
sp_conv_push(&tc, "user", NULL, big);
for (k = 0; k < 4; k++) {
memset(&m, 0, sizeof(m));
m.role = "assistant";
m.content = "";
m.calls = "[{\"id\":\"c\",\"type\":\"function\"}]";
CHECK(sp_conv_add(&tc, &m) == 0);
sp_conv_push(&tc, "tool", "read", big);
sp_conv_push(&tc, "tool", "read", big);
}
sp_conv_push(&tc, "user", NULL, "and now?");
CHECK(tc.n == 15);
CHECK(strcmp(tc.v[tc.n - 6].role, "tool") == 0);
CHECK(sp_host_compact(host, &tc, 400) == 1);
for (k = 1; k < tc.n; k++)
if (strcmp(tc.v[k].role, "tool") == 0 &&
strcmp(tc.v[k - 1].role, "assistant") != 0 &&
strcmp(tc.v[k - 1].role, "tool") != 0)
orphan = 1;
CHECK(!orphan);
CHECK(strcmp(tc.v[0].role, "system") == 0);
CHECK(strstr(tc.v[1].content, "[lcm]") != NULL);
CHECK(strcmp(tc.v[tc.n - 1].content, "and now?") == 0);
sp_conv_free(&tc);
}
/* ---- a plugin that is not a compactor cannot register one ---- */
CHECK(sp_host_add_compactor(host, NULL) == -1);
/*
* ---- the loop compacts at a budget, not only the plugin in isolation ----
* 8 KiB and 32 KiB (a rough 8k-token window). Each request has to fit
* after compaction or the turn fails; the counter is the measurement.
*/
{
struct sp_agent_stats st;
memset(&st, 0, sizeof(st));
CHECK(run_budget(8192, 40, 400, &st) == 0);
CHECK(st.compactions >= 1);
CHECK(st.steps == 41);
memset(&st, 0, sizeof(st));
CHECK(run_budget(32768, 120, 400, &st) == 0);
CHECK(st.compactions >= 1);
CHECK(st.steps == 121);
}
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;
}