/* * 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" #include "i18n.h" #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; /* messages in the snapshot: all of them */ size_t cap; /* allocated entries in m */ size_t total; /* messages in the conversation then */ size_t bytes; /* and its size: a stale plan is refused */ size_t budget; /* the budget the plan is made for */ struct snap_msg *m; 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; /* one message as sp_conv_bytes() counts it */ static size_t msg_bytes(const struct sp_msg *m) { return (m->role != NULL ? strlen(m->role) : 0) + (m->name != NULL ? strlen(m->name) : 0) + (m->content != NULL ? strlen(m->content) : 0) + (m->calls != NULL ? strlen(m->calls) : 0) + 16; } /* * The index covers the whole conversation: a plan made from the first few * hundred messages cut against a conversation of thousands. */ static void copy_snapshot(const struct sp_conv *conv, size_t budget) { size_t i, n = conv->n; pthread_mutex_lock(&cps.lock); if (n > cps.cap) { struct snap_msg *nm; size_t ncap = cps.cap ? cps.cap : 64; while (ncap < n) ncap *= 2; nm = realloc(cps.m, ncap * sizeof(*nm)); if (nm == NULL) { /* no index, no plan: lcm or the host trim still work */ cps.requested = 0; cps.ready = 0; pthread_mutex_unlock(&cps.lock); return; } cps.m = nm; cps.cap = ncap; } cps.n = n; cps.total = conv->n; cps.bytes = sp_conv_bytes(conv); cps.budget = budget; 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 = msg_bytes(m); 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); /* * The plan fits this conversation only if it is the one it was made for: * same length, same size, same budget. */ if (budget == 0) { pthread_mutex_unlock(&cps.lock); return 0; /* no byte budget: nothing to do */ } if (cps.ready && cps.total == conv->n && cps.budget == budget && cps.bytes == sp_conv_bytes(conv) && sp_conv_bytes(conv) > budget) { apply = 1; } else { pthread_mutex_unlock(&cps.lock); if (sp_conv_bytes(conv) <= budget) return 0; /* fits: no plan needed */ copy_snapshot(conv, budget); /* * The worker is making a plan for exactly this conversation. The * core compactor must not cut it in the meantime, or the plan is * invalid before it lands and every round throws it away. */ sp_host_compactor_hold(h); 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); /* a tool result goes with the call it answers, never alone */ while (cut < conv->n && conv->v[cut].role != NULL && strcmp(conv->v[cut].role, "tool") == 0) cut++; 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), sp_td("cps.idle", "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; tail_from = cps.n > KEEP_TAIL ? cps.n - KEEP_TAIL : 1; if (tail_from < 1) tail_from = 1; /* * Cut from the front only as far as the budget needs (room left for the * summary), never into the verbatim tail, and never so that the first * kept message is a tool result without its call. */ cut = 1; { size_t left = total; while (cut < tail_from && left + SUMMARY_MAX > cps.budget) { left -= cps.m[cut].len; cut++; } } while (cut < cps.n && strcmp(cps.m[cut].role, "tool") == 0) cut++; snprintf(cps.summary, sizeof(cps.summary), "[cps] %zu messages cut from the front to fit %zu bytes; " "verbatim tail of %zu messages follows. Oldest cut turn began with: " "\"%.60s\"", cut - 1, cps.budget, cps.n - cut, cps.n > 1 ? cps.m[1].head : ""); cps.before = total; cps.after = cps.n > 0 ? cps.m[0].len : 0; for (i = cut; i < cps.n; i++) cps.after += cps.m[i].len; if (cut <= 1 || cut >= cps.n) { /* nothing to cut that would leave a valid tail */ cps.ready = 0; snprintf(line, sizeof(line), sp_td("cps.idle", "idle (applied %lu, prepared %lu)"), cps.applied, cps.prepared); pthread_mutex_unlock(&cps.lock); return sp_host_widget_text(h, name, line); } cps.cut = cut; cps.ready = 1; cps.prepared++; did = 1; snprintf(line, sizeof(line), sp_td("cps.plan", "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; }