Files
splinter/tests/test_compactor.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

560 lines
16 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);
/*
* The harness's own continuation prompt is not the task. After /resume the
* newest user message is that prompt and the question it continues is
* older; pinning the prompt kept the keystroke and lost the question.
*/
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 < 30; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
"message number x") == 0);
}
CHECK(sp_conv_push(&conv, "user", NULL,
"Continue with the task you were working on. Do not repeat work "
"that is already done.") == 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, "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);
/*
* Past 256 messages the plan used to be made from a capped snapshot and
* cut at "snapshot - 6", deep inside the conversation. It covers all of
* it now, and cuts only what the budget needs.
*/
fill(&conv, 400);
before = sp_conv_bytes(&conv);
CHECK(sp_host_compact(host2, &conv, before / 2) == 0);
nap_ms(350);
{
int rc = sp_host_compact(host2, &conv, before / 2);
CHECK(rc == 1);
if (rc == 1) {
CHECK(sp_conv_bytes(&conv) <= before / 2);
/* about half went, not all but the last six */
CHECK(conv.n > 150);
CHECK(strstr(conv.v[1].content, "[cps]") != NULL);
CHECK(strstr(conv.v[conv.n - 1].content,
"message number 399") != 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);
/*
* Two more old turns, long enough that dropping them saves more bytes
* than the summary costs, make 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);
{
char long_turn[600];
size_t i;
memset(long_turn, 'y', sizeof(long_turn) - 1);
long_turn[sizeof(long_turn) - 1] = '\0';
for (i = 0; i < 8; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
i < 2 ? long_turn : "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);
CHECK(sp_conv_bytes(&conv) < before);
sp_conv_free(&conv);
/*
* The guard counts bytes, not messages: one huge old turn is cut even
* though the message count stays the same (it was never cut before, and
* the conversation stayed over budget forever).
*/
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);
{
char *huge = malloc(100001);
size_t i;
CHECK(huge != NULL);
if (huge != NULL) {
memset(huge, 'z', 100000);
huge[100000] = '\0';
CHECK(sp_conv_push(&conv, "assistant", NULL, huge) == 0);
free(huge);
}
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, 2000) == 1);
CHECK(conv.n == 9);
CHECK(sp_conv_bytes(&conv) <= 2000);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
CHECK(strcmp(conv.v[2].role, "user") == 0);
CHECK(strcmp(conv.v[2].content, "the question") == 0);
sp_conv_free(&conv);
/*
* No user message in the cut: nothing is pinned. The system prompt (or
* an assistant turn) must not come back as a "user" message, and the
* count of dropped user turns must not wrap.
*/
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "you are a harness") == 0);
{
char long_turn[600];
size_t i, k;
memset(long_turn, 'y', sizeof(long_turn) - 1);
long_turn[sizeof(long_turn) - 1] = '\0';
for (i = 0; i < 10; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
long_turn) == 0);
CHECK(sp_host_compact(host, &conv, 4000) == 1);
CHECK(strcmp(conv.v[0].content, "you are a harness") == 0);
CHECK(strstr(conv.v[1].content, "[lcm]") != NULL);
CHECK(strstr(conv.v[1].content, "(user 0,") != NULL);
CHECK(strstr(conv.v[1].content, "current question") == NULL);
for (k = 0; k < conv.n; k++)
CHECK(strcmp(conv.v[k].role, "user") != 0);
}
sp_conv_free(&conv);
/* the same without a system prompt: v[0] is an assistant turn */
sp_conv_init(&conv);
{
char long_turn[600];
size_t i, k;
memset(long_turn, 'y', sizeof(long_turn) - 1);
long_turn[sizeof(long_turn) - 1] = '\0';
for (i = 0; i < 10; i++)
CHECK(sp_conv_push(&conv, "assistant", NULL,
long_turn) == 0);
CHECK(sp_host_compact(host, &conv, 4000) == 1);
CHECK(strcmp(conv.v[0].role, "system") == 0);
CHECK(strstr(conv.v[0].content, "(user 0,") != NULL);
for (k = 0; k < conv.n; k++)
CHECK(strcmp(conv.v[k].role, "user") != 0);
}
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;
}