/* * 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; }