Files
splinter/tests/test_corecompact.c
lotherk f7120882fa sessions: a resumed session is the whole transcript, not a chat log
A session file was written as if it were a chat: role and text, nothing
else. Reading it back kept "user" and "assistant" and dropped everything
else - and nearly every line of an agent transcript is an observation.
Measured on three real sessions: 21 of 1507 messages came back, 8 of 1356,
3 of 317. --resume then sent 1% of the conversation and the model had no
idea what it was continuing.

Three writers had to change with the loader:

  - A turn that asked for tools wrote no line at all (agent.c only wrote
    when ncalls == 0). It now goes out with its tool_calls, and each result
    with the name of its tool and its call id; a provider rejects a tool
    message without them.
  - {"role":"user","text":""} came from the steer path and showed the model
    a blank human turn it answered. sp_session_append_rec writes nothing
    when text and calls are both empty.
  - A record from the old format has no name and no call id. It is paired
    with the next unanswered call of its pending assistant turn, or given a
    synthetic one (recovered-N). Dropping it is what emptied every resume;
    keeping it unpaired is what a provider answers with 400.

The same pass gives the core its own compactor (compact.c): a thread digest
- one line per dropped message, OPERATOR: 800 chars, assistant: 300, tool
result 160 - so the model keeps the story, not a count. lcm stays a plugin
that runs after it; sp_core_compactor() is the fallback only when no plugin
registered one, because running both eats the conversation before lcm sees
work to do.

Two bugs fell out of the same area:

  - The plugin host was only built when a plugin was configured, so a
    plugin-less run had no compactor at all: over max_bytes the turn was
    refused instead of compacted (90 KB session, budget 8000, "nothing
    sent").
  - lcm walked *forward* past tool results at the tail boundary, which
    silently shrank the tail: a turn with two results kept four messages
    where keep_tail says six. Walking back to the assistant that carries
    the call keeps six and no orphan.

And the .meta sidecar is shared now: the core rewrote it with only the keys
it knew, so anything a plugin put there was gone on the next open. It
carries unknown lines through (extra[8][160]), which is what lets a plugin
mark a session in the core's own file.

Tests: tests/test_corecompact.c (10 checks, no plugin), the resume cases in
tests/test_session.c, and the tail-size check in tests/test_compactor.c.
Each was seen red on the old code before it was green on the new.
2026-11-21 21:25:51 +00:00

431 lines
11 KiB
C

/*
* splinter -- the core compactor, without any plugin.
*
* The failure this guards against was silent: compact_threshold and
* compact_target were read, a budget was computed from them, and the compactor
* list was empty unless `plugin lcm` had been loaded. The conversation then grew
* until the provider cut it. This test registers NOTHING but the host, so every
* assertion below fails on the old code.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "config.h"
#include "embed.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
fill(struct sp_conv *c, int n, size_t pad)
{
int i;
char *big = malloc(pad + 64);
if (big == NULL)
return;
memset(big, 'x', pad);
big[pad] = '\0';
sp_conv_init(c);
sp_conv_push(c, "system", NULL, "you are a harness");
for (i = 0; i < n; i++) {
char text[256];
if (i % 5 == 4)
sp_conv_push(c, "tool", "run", big);
else {
snprintf(text, sizeof(text), "message %d %s", i, big);
sp_conv_push(c, (i % 2) ? "assistant" : "user", NULL,
text);
}
}
free(big);
}
/* --- 1. a conversation that fits is not touched ------------------------- */
static void
test_fits(void)
{
struct sp_conv c;
size_t before, n;
fill(&c, 4, 32);
before = sp_conv_bytes(&c);
n = c.n;
CHECK(sp_host_compact(NULL, &c, 1000000) == 0);
CHECK(sp_conv_bytes(&c) == before);
CHECK(c.n == n);
sp_conv_free(&c);
printf("ok 1 - a conversation under budget is untouched\n");
}
/* --- 2. the host carries a compactor with no plugin loaded -------------- */
static void
test_core_registered(void)
{
struct sp_host *h;
struct sp_conv c;
size_t before;
h = sp_host_new(NULL, NULL, NULL);
CHECK(h != NULL);
if (h == NULL)
return;
fill(&c, 40, 400);
before = sp_conv_bytes(&c);
CHECK(before > 4000);
CHECK(sp_host_compact(h, &c, 4000) == 1);
CHECK(sp_conv_bytes(&c) < before);
sp_conv_free(&c);
sp_host_free(h);
printf("ok 2 - the core compactor runs with no plugin loaded\n");
}
/* --- 3. the tail survives, the front goes ------------------------------- */
static void
test_tail_kept(void)
{
struct sp_host *h;
struct sp_conv c;
size_t i;
int found_last = 0;
h = sp_host_new(NULL, NULL, NULL);
fill(&c, 40, 400);
sp_conv_push(&c, "user", NULL, "THE QUESTION THAT MUST SURVIVE");
(void)sp_host_compact(h, &c, 4000);
for (i = 0; i < c.n; i++)
if (c.v[i].content != NULL &&
strstr(c.v[i].content, "MUST SURVIVE") != NULL)
found_last = 1;
CHECK(found_last);
sp_conv_free(&c);
sp_host_free(h);
printf("ok 3 - the newest question survives compaction\n");
}
/* --- 4. the cut is announced, never silent ------------------------------ */
static void
test_summary_present(void)
{
struct sp_host *h;
struct sp_conv c;
size_t i;
int note = 0;
h = sp_host_new(NULL, NULL, NULL);
fill(&c, 40, 400);
(void)sp_host_compact(h, &c, 4000);
for (i = 0; i < c.n; i++)
if (c.v[i].role != NULL && strcmp(c.v[i].role, "system") == 0 &&
c.v[i].content != NULL &&
strstr(c.v[i].content, "[thread so far - compacted]") != NULL)
note = 1;
CHECK(note);
CHECK(c.v[0].content != NULL &&
strcmp(c.v[0].content, "you are a harness") == 0);
sp_conv_free(&c);
sp_host_free(h);
printf("ok 4 - a system note says what was removed, original prompt stays\n");
}
/* --- 5. the tail never starts on a tool result -------------------------- */
static void
test_no_orphan_tool(void)
{
struct sp_host *h;
struct sp_conv c;
size_t i;
h = sp_host_new(NULL, NULL, NULL);
fill(&c, 40, 400);
(void)sp_host_compact(h, &c, 3000);
/*
* Walk from the note forward: the first message after the compaction
* seam must not be a tool result whose assistant turn is gone.
*/
for (i = 1; i < c.n; i++) {
if (c.v[i].role != NULL && strcmp(c.v[i].role, "tool") == 0) {
size_t j = i;
while (j > 0) {
j--;
if (c.v[j].role == NULL)
break;
if (strcmp(c.v[j].role, "assistant") == 0)
break;
if (strcmp(c.v[j].role, "system") == 0 &&
c.v[j].content != NULL &&
strstr(c.v[j].content,
"[thread so far - compacted]") != NULL)
break;
}
CHECK(j > 0 && c.v[j].role != NULL &&
strcmp(c.v[j].role, "assistant") == 0);
}
}
sp_conv_free(&c);
sp_host_free(h);
printf("ok 5 - no tool result is orphaned from its assistant turn\n");
}
/* --- 6. compaction actually shrinks, and is idempotent ------------------ */
static void
test_shrinks_and_settles(void)
{
struct sp_host *h;
struct sp_conv c;
size_t a, b, c2;
int r2;
h = sp_host_new(NULL, NULL, NULL);
fill(&c, 60, 500);
a = sp_conv_bytes(&c);
CHECK(sp_host_compact(h, &c, 6000) == 1);
b = sp_conv_bytes(&c);
CHECK(b < a);
r2 = sp_host_compact(h, &c, 6000);
c2 = sp_conv_bytes(&c);
if (r2 == 1)
CHECK(c2 < b); /* a second cut may happen, but shrinks */
else
CHECK(c2 == b);
sp_conv_free(&c);
sp_host_free(h);
printf("ok 6 - compaction shrinks the conversation (%zu -> %zu)\n", a, b);
}
/* --- 7. a budget of 0 means "no compaction", not "compact to nothing" --- */
static void
test_zero_budget(void)
{
struct sp_conv c;
size_t before;
fill(&c, 20, 200);
before = sp_conv_bytes(&c);
CHECK(sp_host_compact(NULL, &c, 0) == 0);
CHECK(sp_conv_bytes(&c) == before);
sp_conv_free(&c);
printf("ok 7 - a zero budget compacts nothing\n");
}
/* --- 8. trimming is preferred over deleting ----------------------------- */
static void
test_trim_before_drop(void)
{
struct sp_host *h;
struct sp_conv c;
size_t before_n, trimmed_found = 0, i;
h = sp_host_new(NULL, NULL, NULL);
sp_conv_init(&c);
sp_conv_push(&c, "system", NULL, "sys");
for (i = 0; i < 30; i++) {
char *big = malloc(8192);
if (big == NULL)
break;
memset(big, 'y', 8191);
big[8191] = '\0';
sp_conv_push(&c, (i % 2) ? "assistant" : "user", NULL, "short");
sp_conv_push(&c, "tool", "run", big);
free(big);
}
before_n = c.n;
CHECK(sp_host_compact(h, &c, 12000) == 1);
for (i = 0; i < c.n; i++)
if (c.v[i].content != NULL &&
strstr(c.v[i].content, "trimmed by compaction") != NULL)
trimmed_found++;
CHECK(trimmed_found > 0);
CHECK(c.n <= before_n + 2);
sp_conv_free(&c);
sp_host_free(h);
printf("ok 8 - long tool output is trimmed rather than deleted (%zu bodies)\n",
trimmed_found);
}
/* --- 9. the thread survives: who asked what, in order -------------------- */
static void
test_thread_survives(void)
{
struct sp_host *h;
struct sp_conv c;
size_t i;
int first_task = 0, correction = 0, order_ok = 0;
size_t pos_task = 0, pos_fix = 0;
h = sp_host_new(NULL, NULL, NULL);
sp_conv_init(&c);
sp_conv_push(&c, "system", NULL, "you are a harness");
sp_conv_push(&c, "user", NULL,
"BAUE EINE MCP-BRUECKE fuer den ESP32, Ziel: deauth erkennen");
sp_conv_push(&c, "assistant", NULL, "Ich lese zuerst das Repo.");
sp_conv_push(&c, "user", NULL,
"nein, nimm das ANDERE repo, nicht ARTEX sondern apex-twin");
sp_conv_push(&c, "assistant", NULL, "Verstanden, apex-twin.");
for (i = 0; i < 20; i++) {
char t[300];
snprintf(t, sizeof(t), "fuell nachricht %zu ......", i);
sp_conv_push(&c, (i % 2) ? "assistant" : "user", NULL, t);
}
sp_conv_push(&c, "user", NULL, "DIE AKTUELLE FRAGE");
/* 0 is fine: at this budget it may already settle after the trim. What
* matters is the thread below, not the return code. */
(void)sp_host_compact(h, &c, 2200);
for (i = 0; i < c.n; i++) {
if (c.v[i].content == NULL)
continue;
if (strstr(c.v[i].content, "BAUE EINE MCP-BRUECKE") != NULL) {
first_task = 1;
pos_task = i;
}
if (strstr(c.v[i].content, "das ANDERE repo") != NULL) {
correction = 1;
pos_fix = i;
}
}
CHECK(first_task);
CHECK(correction);
/*
* The correction came after the task, so it must stay after it in the
* digest. This is the property lcm broke: the model forgot that the
* operator had changed direction and redid the first plan.
*/
order_ok = first_task && correction && pos_fix >= pos_task;
CHECK(order_ok);
printf("ok 9 - the thread survives in order (task at %zu, correction at %zu)\n",
pos_task, pos_fix);
sp_conv_free(&c);
sp_host_free(h);
}
/* --- 10. a bigger budget keeps more of the thread, not less ------------- */
static void
test_budget_scales_thread(void)
{
struct sp_host *h;
struct sp_conv c1, c2;
size_t i;
size_t small, large;
h = sp_host_new(NULL, NULL, NULL);
for (i = 0; i < 2; i++) {
struct sp_conv *c = i == 0 ? &c1 : &c2;
size_t k;
sp_conv_init(c);
sp_conv_push(c, "system", NULL, "sys");
for (k = 0; k < 40; k++) {
char t[600];
snprintf(t, sizeof(t), "message %zu %s", k,
"padding padding padding padding padding padding "
"padding padding padding padding padding padding "
"padding padding padding padding padding padding");
sp_conv_push(c, (k % 2) ? "assistant" : "user", NULL, t);
}
}
(void)sp_host_compact(h, &c1, 4000);
(void)sp_host_compact(h, &c2, 12000);
small = sp_conv_bytes(&c1);
large = sp_conv_bytes(&c2);
CHECK(large >= small);
printf("ok 10 - a larger budget keeps more (%zu vs %zu bytes)\n", small,
large);
sp_conv_free(&c1);
sp_conv_free(&c2);
sp_host_free(h);
}
/* --- 11. the digest points at history_search, but only if it exists ------ */
/*
* The digest is a lossy summary. If the index is off, the model must not be
* told about a tool that is not registered - a prompt naming a missing tool
* teaches it to call nothing. Both halves are checked against the real
* registry, because "embed = off" is a supported configuration.
*/
static void
test_search_hint(void)
{
struct sp_registry reg;
struct sp_config conf;
struct sp_embed_ctx *emb;
struct sp_host *h;
struct sp_conv c;
size_t i;
int hinted = 0;
memset(&reg, 0, sizeof(reg));
sp_config_init(&conf);
conf.embed = SP_EMBED_OFF; /* no index: no tool */
h = sp_host_new(&reg, NULL, NULL);
fill(&c, 40, 400);
(void)sp_host_compact(h, &c, 4000);
for (i = 0; i < c.n; i++)
if (c.v[i].content != NULL &&
strstr(c.v[i].content, "history_search") != NULL)
hinted = 1;
CHECK(!hinted);
sp_conv_free(&c);
sp_host_free(h);
/* now with the tool really registered, as the engine does it */
memset(&reg, 0, sizeof(reg));
sp_config_init(&conf);
conf.embed = SP_EMBED_LOCAL;
emb = sp_embed_new(&conf);
CHECK(emb != NULL);
h = sp_host_new(&reg, NULL, NULL);
CHECK(h != NULL);
CHECK(sp_embed_register(sp_host_registry(h), emb) == 0);
fill(&c, 40, 400);
(void)sp_host_compact(h, &c, 4000);
hinted = 0;
for (i = 0; i < c.n; i++)
if (c.v[i].content != NULL &&
strstr(c.v[i].content, "history_search") != NULL)
hinted = 1;
CHECK(hinted);
sp_conv_free(&c);
sp_host_free(h);
sp_embed_free(emb);
sp_registry_free(&reg);
printf("ok 11 - the digest names history_search only when it exists\n");
}
int
main(void)
{
test_fits();
test_core_registered();
test_tail_kept();
test_summary_present();
test_no_orphan_tool();
test_shrinks_and_settles();
test_zero_budget();
test_trim_before_drop();
test_thread_survives();
test_budget_scales_thread();
test_search_hint();
if (failures > 0) {
fprintf(stderr, "%d check(s) failed\n", failures);
return 1;
}
printf("all compact checks passed\n");
return 0;
}