Compaction is not a flag in the loop; it is a compactor registered by a plugin and run on the main thread before every request, with the whole conversation in hand. Returning 1 means changed, 0 nothing to do, -1 failed - and a failure is counted, not fatal. lcm: deterministic lossy compression. Over budget, it keeps the leading system prompt and the last six messages and inserts one system message that says how many messages went, of which roles, and quotes the head of the oldest dropped turn. No second model, so a run stays reproducible and costs nothing extra. cps: continuous parallel compression. The worker thread decides what would go from an index it got under a mutex (role, size, 64 bytes of head per message) and never touches the conversation; the main thread splices the prepared plan in one pass when the budget is hit. The sidebar line shows applied/prepared counts or a ready plan, so the claim is visible. The limit is in both files: they can decide which messages go, not summarise their text, because the text is not the worker's to read. Ten test binaries green; test_compactor drives both against their real .so files, including that lcm leaves the system prompt first, inserts the [lcm] record at index 1, keeps the tail verbatim, and that cps needs two calls (prepare, then apply) to change anything.
117 lines
3.2 KiB
C
117 lines
3.2 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#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;
|
|
}
|