Files
splinter/plugins/cps/cps.c
T
Konrad 554b4c97b8 splinter: tool calls with arguments, newline-aware rows, complete i18n
Four things, three of them reported by the operator.

1. Rows are counted from the real line structure. The old arithmetic
   (strlen/cols) assumed one long line; a model's reasoning is full of '\n', so
   the true height was far larger than the estimate and the text painted over
   the answer below it - 'it just goes anywhere on the screen'. text_rows() and
   row_offset() split on newlines first, then wrap. entry_lines() now measures
   the string that is actually drawn ('role: text'), which also fixes the last
   character of an entry being cut off (a truncated code fence).

2. Tool calls are visible: the agent's tool_begin hook fires before each call,
   the UI drains the list under its lock and writes 'werkzeug: run
   {"command":"echo a"}' into the transcript; the one at the top of the live
   area shows the call while it runs. The reasoning entry is inserted before
   those lines, so the transcript reads du, denkt, werkzeug, modell.

3. The chat pane has a black background, and the status line sits between the
   pane and the prompt, the way Hermes shows it.

4. i18n is complete: every remaining literal in ui.c, main.c and the plugins
   goes through the catalogue, including widget texts and format strings.
   sp_td() carries a compiled-in English default, so a missing catalogue can
   never drop a printf placeholder. tools/check_i18n.py runs in make check and
   fails when a key used in the code is missing from any catalogue, or when a
   catalogue key is used nowhere.

Seventeen test binaries green.
2026-11-08 08:11:09 +00:00

209 lines
5.1 KiB
C

/*
* 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 <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "i18n.h"
#define SNAP_MAX 256
#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;
struct snap_msg m[SNAP_MAX];
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;
static void
copy_snapshot(const struct sp_conv *conv)
{
size_t i, n = conv->n > SNAP_MAX ? SNAP_MAX : conv->n;
pthread_mutex_lock(&cps.lock);
cps.n = n;
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 = m->content != NULL ? strlen(m->content) : 0;
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);
if (cps.ready && cps.n == conv->n &&
(budget == 0 || sp_conv_bytes(conv) > budget)) {
apply = 1;
} else {
pthread_mutex_unlock(&cps.lock);
copy_snapshot(conv);
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);
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 + 16;
tail_from = cps.n > KEEP_TAIL ? cps.n - KEEP_TAIL : 1;
cut = tail_from;
if (cut <= 1)
cut = 1;
snprintf(cps.summary, sizeof(cps.summary),
"[cps] %zu messages cut from the front to stay under budget; "
"verbatim tail of %zu messages follows. Oldest cut turn began with: "
"\"%.60s\"", cut - 1, cps.n - cut,
cps.n > 1 ? cps.m[1].head : "");
cps.before = total;
cps.after = 0;
for (i = cut; i < cps.n; i++)
cps.after += cps.m[i].len + 16;
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;
}