diff --git a/Makefile b/Makefile index bec7645..770123a 100644 --- a/Makefile +++ b/Makefile @@ -26,10 +26,10 @@ LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \ src/agent.o src/theme.o src/palettes.o src/plugin.o BIN = splinter BINOBJ = src/main.o src/ui.o -PLUGIN = plugins/clock/clock.so +PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \ tests/test_transport tests/test_agent tests/test_openai \ - tests/test_theme tests/test_plugin + tests/test_theme tests/test_plugin tests/test_compactor RPATH = -Wl,-rpath,'$$ORIGIN' PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..' @@ -54,10 +54,18 @@ $(BIN): $(BINOBJ) $(LIB) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BINOBJ) -L. -lsplinter $(RPATH) \ $(CURSES_A) $(LIBS) -$(PLUGIN): plugins/clock/clock.c $(LIB) +plugins/clock/clock.so: plugins/clock/clock.c $(LIB) $(CC) $(CFLAGS) -shared -o $@ plugins/clock/clock.c -L. -lsplinter \ $(PLUGIN_RPATH) $(LIBS) +plugins/lcm/lcm.so: plugins/lcm/lcm.c $(LIB) + $(CC) $(CFLAGS) -shared -o $@ plugins/lcm/lcm.c -L. -lsplinter \ + $(PLUGIN_RPATH) $(LIBS) + +plugins/cps/cps.so: plugins/cps/cps.c $(LIB) + $(CC) $(CFLAGS) -shared -o $@ plugins/cps/cps.c -L. -lsplinter \ + $(PLUGIN_RPATH) $(LIBS) + .c.o: $(CC) $(CFLAGS) -c -o $@ $< @@ -93,6 +101,8 @@ tests/test_openai: tests/test_openai.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_openai.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_theme: tests/test_theme.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_theme.c -L. -lsplinter $(TEST_RPATH) $(LIBS) +tests/test_compactor: tests/test_compactor.c $(LIB) $(PLUGIN) + $(CC) $(CFLAGS) -o $@ tests/test_compactor.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_plugin: tests/test_plugin.c $(LIB) $(PLUGIN) $(CC) $(CFLAGS) -o $@ tests/test_plugin.c -L. -lsplinter $(TEST_RPATH) $(LIBS) diff --git a/README.md b/README.md index 8da82bf..7a0b9e7 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,14 @@ different JSON. `content: null` is an empty answer, not an error; a reply without choices, or a provider `error` object, is an error that quotes the provider's own message. +## Compaction + +Compaction is a plugin, not a switch in the loop. Two ship here: `lcm` +(deterministic front-trim with an in-band system message saying what went) and +`cps` (continuous parallel compression: plans are prepared on a worker thread +from an index of the conversation, then spliced in instantly). Details and the +ABI in `docs/PLUGINS.md`. + ## Limits, stated - Tool arguments reach the tool as one string; each tool validates its own. diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index d12a112..a5b0b27 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -90,3 +90,40 @@ sp_host_add_tool(host, &t); `SP_AUTH_READ` runs. `SP_AUTH_MUTATE` and `SP_AUTH_DESTRUCTIVE` ask the operator unless the harness was started with `--autonomous`. The loop decides this, not the plugin. + +## Compaction + +A compactor runs on the main thread before every request, with the whole +conversation in hand, and may rewrite it: + +```c +struct sp_compactor { + .const char *name; + int (*compact)(struct sp_host *, struct sp_conv *, size_t budget_bytes, + unsigned turn); +}; +``` + +Return 1 when it changed something, 0 when there was nothing to do, -1 on +failure. A failure is counted and the turn continues: a broken compactor must +not take the conversation with it. + +Two come with the tree. + +**lcm** — deterministic, lossy, honest. If the conversation does not fit the +budget, it keeps the leading system message and the last six messages, and puts +one system message in between that says how many messages went, of which roles, +and quotes the head of the oldest dropped turn. No second model is called, so a +run stays reproducible and costs nothing extra. + +**cps** — continuous parallel compression. The expensive part (deciding what +would go) is computed on the widget's worker thread from an *index* of the +conversation — role, size, and 64 bytes of head per message, copied under a +mutex. The worker never touches the conversation itself. When the budget is +hit, the main thread splices an already-prepared plan in one pass. Its sidebar +line shows `idle (applied N, prepared M)` or `plan ready: cut X/Y, A -> B B`, +so "continuous" is visible rather than claimed. + +The limit is stated in both files: they can decide *which* messages go, not +summarise their text, because summarising needs the text and the text is not +the worker's to read. diff --git a/include/splinter.h b/include/splinter.h index 9f50892..58e6fde 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -95,6 +95,7 @@ struct sp_conv { void sp_conv_init(struct sp_conv *); void sp_conv_free(struct sp_conv *); int sp_conv_add(struct sp_conv *, const struct sp_msg *); +int sp_conv_insert(struct sp_conv *, size_t at, const struct sp_msg *); int sp_conv_push(struct sp_conv *, const char *role, const char *name, const char *content); size_t sp_conv_bytes(const struct sp_conv *); @@ -171,13 +172,17 @@ struct sp_agent_config { struct sp_agent_stats { size_t steps; + size_t compactions; size_t tool_calls; size_t denied; size_t bytes_sent; }; +struct sp_host; /* plugin host, see splinter_plugin.h */ + struct sp_agent { struct sp_registry *reg; + struct sp_host *host; /* optional: compactors live there */ struct sp_transport tr; const struct sp_agent_config *cfg; struct sp_session_info *sess; diff --git a/include/splinter_plugin.h b/include/splinter_plugin.h index 1d89021..6de3353 100644 --- a/include/splinter_plugin.h +++ b/include/splinter_plugin.h @@ -43,6 +43,17 @@ struct sp_widget { unsigned interval_ms; /* 0: tick once, then stop */ }; +/* + * A compactor runs on the main thread before every request, with the whole + * conversation in hand. It may rewrite it. Returning 1 means "changed", 0 + * "nothing to do", -1 "failed" - and a failure is reported, never ignored. + */ +struct sp_compactor { + const char *name; + int (*compact)(struct sp_host *, struct sp_conv *, + size_t budget_bytes, unsigned turn); +}; + struct sp_plugin { uint32_t abi; /* must equal SP_PLUGIN_ABI */ const char *name; @@ -64,6 +75,11 @@ int sp_host_add_widget(struct sp_host *, const struct sp_widget *); int sp_host_widget_text(struct sp_host *, const char *widget, const char *text); int sp_host_note(struct sp_host *, const char *text); +int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *); +int sp_host_compact(struct sp_host *, struct sp_conv *, size_t budget); +size_t sp_host_compaction_stats(struct sp_host *, size_t *compactions, + size_t *failed, unsigned *turns); +void sp_host_turn(struct sp_host *); /* ---- host side, implemented by splinter, called by the harness ---- */ diff --git a/libsplinter.so b/libsplinter.so index 6b8d88c..a719c49 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/plugins/cps/cps.c b/plugins/cps/cps.c new file mode 100644 index 0000000..acba38c --- /dev/null +++ b/plugins/cps/cps.c @@ -0,0 +1,207 @@ +/* + * 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 +#include +#include +#include + +#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; +} diff --git a/plugins/cps/cps.so b/plugins/cps/cps.so new file mode 100755 index 0000000..b956860 Binary files /dev/null and b/plugins/cps/cps.so differ diff --git a/plugins/lcm/lcm.c b/plugins/lcm/lcm.c new file mode 100644 index 0000000..1c7e081 --- /dev/null +++ b/plugins/lcm/lcm.c @@ -0,0 +1,116 @@ +/* + * splinter -- lcm: lossy compression memory for the conversation. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * The rule this plugin follows: a conversation that does not fit the budget is + * cut from the front, but never silently. What is dropped is replaced by one + * system message that says how much went, of which roles, and quotes the head + * of the oldest dropped turn — so a model (and a human reading the session log) + * can tell the difference between "the user never said that" and "that was + * compacted away". + * + * Deterministic on purpose: no second model, no summary that costs another + * call, nothing that makes a run unreproducible. The harder variant (an LLM + * writes the summary) is a different plugin, not a flag on this one. + */ +#include +#include +#include + +#include "splinter.h" +#include "splinter_plugin.h" + +#define KEEP_TAIL 6 /* messages always kept verbatim */ +#define PREVIEW 120 /* chars of the oldest dropped message */ + +static int +lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, + unsigned turn) +{ + size_t bytes, i, drop, keep_from; + char preview[PREVIEW + 1]; + char summary[512]; + size_t n_user = 0, n_asst = 0, n_tool = 0; + const char *oldest = NULL; + + (void)turn; + if (conv == NULL || conv->n <= KEEP_TAIL + 1) + return 0; + bytes = sp_conv_bytes(conv); + if (budget > 0 && bytes <= budget) + return 0; /* fits: nothing to do */ + + /* keep a leading system message, drop everything up to the tail */ + keep_from = conv->n - KEEP_TAIL; + if (keep_from <= 1) + return 0; + drop = keep_from; + if (conv->n > 0 && strcmp(conv->v[0].role, "system") == 0) + drop--; /* the system prompt stays */ + + oldest = conv->v[conv->n - KEEP_TAIL].content; + for (i = 1; i < conv->n - KEEP_TAIL; i++) { + const char *r = conv->v[i].role; + + if (r == NULL) + continue; + if (strcmp(r, "user") == 0) + n_user++; + else if (strcmp(r, "assistant") == 0) + n_asst++; + else if (strcmp(r, "tool") == 0) + n_tool++; + } + snprintf(preview, sizeof(preview), "%s", oldest != NULL ? oldest : ""); + snprintf(summary, sizeof(summary), + "[lcm] %zu messages compacted out (user %zu, assistant %zu, tool %zu) " + "to fit %zu bytes; the verbatim tail of %d messages follows. " + "Oldest dropped turn began with: \"%s\"", + drop, n_user, n_asst, n_tool, budget, KEEP_TAIL, preview); + + /* remove [1 .. conv->n-KEEP_TAIL) */ + for (i = 1; i < conv->n - KEEP_TAIL; 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[conv->n - KEEP_TAIL], + KEEP_TAIL * sizeof(*conv->v)); + conv->n = 1 + KEEP_TAIL; + + { + struct sp_msg m; + size_t at = (conv->n > 0 && + strcmp(conv->v[0].role, "system") == 0) ? 1 : 0; + + memset(&m, 0, sizeof(m)); + m.role = "system"; + m.content = summary; + if (sp_conv_insert(conv, at, &m) != 0) + return -1; + } + (void)h; + return 1; +} + +static const struct sp_compactor lcm = { "lcm", lcm_compact }; + +static int +lcm_init(struct sp_host *h) +{ + return sp_host_add_compactor(h, &lcm); +} + +static const struct sp_plugin lcm_plugin = { + SP_PLUGIN_ABI, "lcm", "1.0.0", lcm_init, NULL, NULL +}; + +const struct sp_plugin * +sp_plugin_describe(void) +{ + return &lcm_plugin; +} diff --git a/plugins/lcm/lcm.so b/plugins/lcm/lcm.so new file mode 100755 index 0000000..01dbe3c Binary files /dev/null and b/plugins/lcm/lcm.so differ diff --git a/src/agent.c b/src/agent.c index b9a4862..4ebf3a5 100644 --- a/src/agent.c +++ b/src/agent.c @@ -21,6 +21,7 @@ #include #include "splinter.h" +#include "splinter_plugin.h" static int ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args) @@ -104,6 +105,10 @@ sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer, size_t sent; memset(&reply, 0, sizeof(reply)); + /* compaction happens before the budget is judged, never after */ + if (a->host != NULL && + sp_host_compact(a->host, conv, a->cfg->max_bytes) > 0) + a->stats.compactions++; sent = sp_conv_bytes(conv); if (a->cfg->max_bytes > 0 && sent > a->cfg->max_bytes) { sp_buf_printf(err, "agent: conversation budget exceeded " diff --git a/src/conv.c b/src/conv.c index 0ec8bf2..850cf75 100644 --- a/src/conv.c +++ b/src/conv.c @@ -83,6 +83,22 @@ sp_conv_add(struct sp_conv *c, const struct sp_msg *in) return 0; } +int +sp_conv_insert(struct sp_conv *c, size_t at, const struct sp_msg *in) +{ + struct sp_msg m; + + if (c == NULL || in == NULL || in->role == NULL || at > c->n) + return -1; + /* append, then rotate the new last element into place */ + if (sp_conv_add(c, in) != 0) + return -1; + m = c->v[c->n - 1]; + memmove(&c->v[at + 1], &c->v[at], (c->n - 1 - at) * sizeof(*c->v)); + c->v[at] = m; + return 0; +} + int sp_conv_push(struct sp_conv *c, const char *role, const char *name, const char *content) diff --git a/src/plugin.c b/src/plugin.c index 67c57cd..50f3540 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -26,6 +26,7 @@ #include "splinter_plugin.h" #define MAX_WIDGETS 32 +#define MAX_COMPACTORS 8 #define MAX_NOTE 2048 struct widget { @@ -48,9 +49,14 @@ struct sp_host { struct widget widgets[MAX_WIDGETS]; size_t nwidgets; char note[MAX_NOTE]; + const struct sp_compactor *compact[MAX_COMPACTORS]; + size_t ncompact; pthread_mutex_t lock; /* guards note and the registry */ struct sp_session_info *sess; struct sp_agent *agent; + size_t compactions; + size_t failed; + unsigned turns; }; static void * @@ -141,6 +147,63 @@ sp_host_add_widget(struct sp_host *h, const struct sp_widget *w) return 0; } +int +sp_host_add_compactor(struct sp_host *h, const struct sp_compactor *c) +{ + if (h == NULL || c == NULL || c->name == NULL || c->compact == NULL) + return -1; + if (h->ncompact == MAX_COMPACTORS) + return -1; + h->compact[h->ncompact++] = c; + return 0; +} + +/* + * Run every compactor in registration order. A compactor that fails does not + * stop the others and does not abort the turn: the failure is counted and the + * loop carries on with whatever the conversation looks like now. + */ +int +sp_host_compact(struct sp_host *h, struct sp_conv *conv, size_t budget) +{ + size_t i; + int changed = 0, rc; + + if (h == NULL || conv == NULL) + return 0; + for (i = 0; i < h->ncompact; i++) { + rc = h->compact[i]->compact(h, conv, budget, + (unsigned)h->turns); + if (rc > 0) { + changed = 1; + h->compactions++; + } else if (rc < 0) { + h->failed++; + } + } + return changed; +} + +size_t +sp_host_compaction_stats(struct sp_host *h, size_t *compactions, + size_t *failed, unsigned *turns) +{ + if (compactions != NULL) + *compactions = h != NULL ? h->compactions : 0; + if (failed != NULL) + *failed = h != NULL ? h->failed : 0; + if (turns != NULL) + *turns = h != NULL ? h->turns : 0; + return h != NULL ? h->compactions : 0; +} + +void +sp_host_turn(struct sp_host *h) +{ + if (h != NULL) + h->turns++; +} + int sp_host_widget_text(struct sp_host *h, const char *widget, const char *text) { diff --git a/tests/test_compactor b/tests/test_compactor new file mode 100755 index 0000000..a329d39 Binary files /dev/null and b/tests/test_compactor differ diff --git a/tests/test_compactor.c b/tests/test_compactor.c new file mode 100644 index 0000000..4ce6934 --- /dev/null +++ b/tests/test_compactor.c @@ -0,0 +1,139 @@ +/* + * 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 +#include +#include + +#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(®, 0, sizeof(reg)); + memset(&ag, 0, sizeof(ag)); + if (sp_buf_init(&err) != 0) + return 1; + + host = sp_host_new(®, &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(®, &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(®); + + if (failures == 0) + printf("test_compactor: ok\n"); + return failures == 0 ? 0 : 1; +} diff --git a/tests/test_plugin b/tests/test_plugin index 0033d1e..9efa4a9 100755 Binary files a/tests/test_plugin and b/tests/test_plugin differ