/* * 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); } } 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(®, 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); /* * ---- 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(®); if (failures == 0) printf("test_compactor: ok\n"); return failures == 0 ? 0 : 1; }