Files
splinter/src/conv.c
T
Konrad b8295ea474 splinter: compaction as plugins (lcm, cps)
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.
2026-11-07 13:44:43 +00:00

149 lines
3.2 KiB
C

/*
* splinter -- the conversation.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* The conversation is the harness's memory inside one turn and across turns.
* It is a plain array of owned messages: no sharing, no copy-on-write, no
* pointers into other messages. A compaction step therefore has to produce a
* new array, which is exactly what makes it reviewable.
*/
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
void
sp_conv_init(struct sp_conv *c)
{
memset(c, 0, sizeof(*c));
}
void
sp_conv_free(struct sp_conv *c)
{
size_t i;
if (c == NULL)
return;
for (i = 0; i < c->n; i++) {
free(c->v[i].role);
free(c->v[i].name);
free(c->v[i].call_id);
free(c->v[i].content);
free(c->v[i].calls);
}
free(c->v);
memset(c, 0, sizeof(*c));
}
static char *
dup_or_empty(const char *s)
{
if (s == NULL)
s = "";
return strdup(s);
}
int
sp_conv_add(struct sp_conv *c, const struct sp_msg *in)
{
struct sp_msg *m;
if (c == NULL || in == NULL || in->role == NULL)
return -1;
if (c->n == c->cap) {
size_t ncap = c->cap == 0 ? 8 : c->cap * 2;
struct sp_msg *nv = realloc(c->v, ncap * sizeof(*nv));
if (nv == NULL)
return -1;
c->v = nv;
c->cap = ncap;
}
m = &c->v[c->n];
m->role = dup_or_empty(in->role);
m->name = in->name != NULL ? dup_or_empty(in->name) : NULL;
m->call_id = in->call_id != NULL ? dup_or_empty(in->call_id) : NULL;
m->content = dup_or_empty(in->content);
m->calls = in->calls != NULL ? dup_or_empty(in->calls) : NULL;
if (m->role == NULL || m->content == NULL ||
(in->name != NULL && m->name == NULL) ||
(in->call_id != NULL && m->call_id == NULL) ||
(in->calls != NULL && m->calls == NULL)) {
free(m->role);
free(m->name);
free(m->call_id);
free(m->content);
free(m->calls);
return -1;
}
c->n++;
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)
{
struct sp_msg m;
memset(&m, 0, sizeof(m));
m.role = (char *)role;
m.name = (char *)name;
m.content = (char *)content;
return sp_conv_add(c, &m);
}
size_t
sp_conv_bytes(const struct sp_conv *c)
{
size_t i, total = 0;
for (i = 0; i < c->n; i++) {
total += c->v[i].role != NULL ? strlen(c->v[i].role) : 0;
total += c->v[i].name != NULL ? strlen(c->v[i].name) : 0;
total += c->v[i].content != NULL ? strlen(c->v[i].content) : 0;
total += c->v[i].calls != NULL ? strlen(c->v[i].calls) : 0;
total += 16; /* JSON scaffolding, estimated */
}
return total;
}
int
sp_conv_trim(struct sp_conv *c, size_t keep_last)
{
size_t drop, i;
if (c == NULL || keep_last >= c->n)
return 0;
drop = c->n - keep_last;
for (i = 0; i < drop; i++) {
free(c->v[i].role);
free(c->v[i].name);
free(c->v[i].call_id);
free(c->v[i].content);
free(c->v[i].calls);
}
memmove(c->v, c->v + drop, (c->n - drop) * sizeof(*c->v));
c->n -= drop;
return 0;
}