Files
splinter/plugins/cache/cache.c
T
lotherk 7f8493c949 ui: multiline prompt, shift+enter, mouse; cache plugin; judge headers
Prompt box
- The input wraps at the window width and grows from one to five rows;
  beyond five it keeps the line with the cursor visible and scrolls inside.
  The conversation and sidebar shrink instead of the prompt being cut.
- Shift+Enter inserts a newline. It accepts the CSI-u and xterm modifyOtherKeys
  sequences and Alt+Enter; modifyOtherKeys is enabled at start and disabled on
  exit/signal. Enter still sends.
- History entries are folded to one line so the line-based history file stays
  readable.
- Mouse wheel scrolls the transcript (3 rows) in the prompt and during a turn.

Cache plugin
- New plugins/cache/cache.so: sidebar block and /cache command with the
  provider prompt-cache hit rate (last and session), cumulative cached tokens,
  and the junction cache from X-Junction-Cache plus its /status cache object
  (entries, hits, misses, bytes, ttl, joined).
- sp_meter/agent/engine now carry cached_total; the UI fills it.
- plugin cache { } added to the user config.

Junction judge schedule
- The transport parses X-Junction-Judge-Next/-In/-Next-Step/Every/Target/
  Model/Cache from every chat response and publishes the last one on the host.
- The junction widget and /junction show "judge next in N ... via ... cache
  ..." when the provider sends it; no stale schedule after a plain model.
- X-Junction-Cache is parsed even when no judge headers are present.

Self plugin
- Human labels and units: memory, threads, open files, children, bytes read/
  written; "children" instead of "kids".

Errors
- An in-stream error event now reads "provider error: ..." instead of "sse:",
  because it is the provider/router talking, not the stream parser.

make check: all tests passed.
2026-11-18 14:17:36 +00:00

348 lines
8.8 KiB
C

/*
* splinter -- cache: what the prompt cache is doing, in the sidebar.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Three caches meet in one turn:
*
* provider the prompt tokens the model server served from its own cache
* (usage.prompt_tokens_details.cached_tokens), last call and the
* whole session;
* junction the router's cache mode from X-Junction-Cache (off/5m/1h) and,
* when its /status answers, entries/hits/misses/bytes;
* local the same provider number seen from this machine - there is no
* second cache between splinter and the endpoint, so it is shown
* once, not invented twice.
*
* The widget says the last hit rate and the session hit rate, not a single
* number that hides which one it is.
*/
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "config.h"
#include "curl.h"
#include "mcp.h"
#define CACHE_MS 3000
#define CACHE_PROBE_MS 15000
struct jcache {
int valid;
long ttl_s, entries, bytes, hits, misses, joined;
time_t at;
char url[600];
};
static struct {
struct sp_host *host;
pthread_mutex_t lock;
struct jcache j;
char widget[1024];
char dbg[256];
} c = { NULL, PTHREAD_MUTEX_INITIALIZER, { 0 }, "", "" };
/* "12.3k" for token counts; a cache line should fit one sidebar row */
static void
human(size_t n, char *buf, size_t cap)
{
if (n >= 1000000)
snprintf(buf, cap, "%.1fM", (double)n / 1e6);
else if (n >= 1000)
snprintf(buf, cap, "%.1fk", (double)n / 1e3);
else
snprintf(buf, cap, "%zu", n);
}
static void
human_bytes(long n, char *buf, size_t cap)
{
if (n >= 1024L * 1024L * 1024L)
snprintf(buf, cap, "%.1f GB", (double)n / (1024.0 * 1024.0 * 1024.0));
else if (n >= 1024L * 1024L)
snprintf(buf, cap, "%.1f MB", (double)n / (1024.0 * 1024.0));
else if (n >= 1024L)
snprintf(buf, cap, "%.0f kB", (double)n / 1024.0);
else
snprintf(buf, cap, "%ld B", n);
}
/* the junction status URL and header from the mcp server the config names */
static void
resolve_junction(const struct sp_config *cfg, char *url, size_t urlcap,
char *hdr, size_t hdrcap)
{
size_t i;
url[0] = '\0';
hdr[0] = '\0';
if (cfg == NULL)
return;
for (i = 0; i < cfg->nmcp; i++) {
const struct sp_mcpserver *m = &cfg->mcp[i];
if (strcmp(m->id, "junction") != 0)
continue;
if (m->url[0] == '\0')
return;
snprintf(url, urlcap, "%s", m->url);
snprintf(hdr, hdrcap, "%s", m->header);
break;
}
if (url[0] == '\0')
return;
{
size_t n = strlen(url);
if (n >= 4 && strcmp(url + n - 4, "/mcp") == 0) {
n -= 4;
url[n] = '\0';
}
while (n > 0 && url[n - 1] == '/')
url[--n] = '\0';
snprintf(url + n, urlcap - n, "/status");
}
}
/* one GET, body only; the widget thread may block for the 4 s timeout */
static char *
get_body(const char *url, const char *header)
{
static const char *const args[] = { "-s", "-m", "4", NULL };
const char *hdrs[2];
char chunk[4096];
struct sp_buf err, body;
pid_t pid;
int fd, st = 0;
ssize_t got;
hdrs[0] = header != NULL && *header != '\0' ? header : NULL;
hdrs[1] = NULL;
if (sp_buf_init(&err) != 0)
return NULL;
fd = sp_curl_start(args, url, hdrs, 1, &pid, &err);
if (fd < 0) {
sp_buf_free(&err);
return NULL;
}
if (sp_buf_init(&body) != 0) {
close(fd);
(void)waitpid(pid, &st, 0);
sp_buf_free(&err);
return NULL;
}
while ((got = read(fd, chunk, sizeof(chunk))) > 0)
if (sp_buf_add(&body, chunk, (size_t)got) != 0)
break;
close(fd);
while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
;
sp_buf_free(&err);
if (body.p == NULL) {
sp_buf_free(&body);
return NULL;
}
return body.p;
}
static void
probe_junction(void)
{
const struct sp_config *cfg;
char url[600], hdr[600], *body;
mcp_json *v;
const mcp_json *cache;
struct jcache j;
cfg = sp_host_config(c.host);
resolve_junction(cfg, url, sizeof(url), hdr, sizeof(hdr));
if (url[0] == '\0')
return;
body = get_body(url, hdr);
if (body == NULL) {
snprintf(c.dbg, sizeof(c.dbg), "no body from %s", url);
return;
}
v = mcp_parse(body);
if (v == NULL) {
snprintf(c.dbg, sizeof(c.dbg), "unparseable: %.120s", body);
free(body);
return;
}
cache = mcp_arg(v, "cache");
if (cache == NULL) {
snprintf(c.dbg, sizeof(c.dbg), "no cache object: %.100s", body);
free(body);
mcp_json_free(v);
return;
}
free(body);
if (cache != NULL) {
memset(&j, 0, sizeof(j));
j.valid = 1;
j.ttl_s = (long)mcp_arg_int(cache, "ttl_s", 0);
j.entries = (long)mcp_arg_int(cache, "entries", 0);
j.bytes = (long)mcp_arg_int(cache, "bytes", 0);
j.hits = (long)mcp_arg_int(cache, "hits", 0);
j.misses = (long)mcp_arg_int(cache, "misses", 0);
j.joined = (long)mcp_arg_int(cache, "joined", 0);
j.at = time(NULL);
snprintf(j.url, sizeof(j.url), "%s", url);
pthread_mutex_lock(&c.lock);
c.j = j;
pthread_mutex_unlock(&c.lock);
}
mcp_json_free(v);
}
static int
cache_tick(struct sp_host *h, const char *name, void *ctx)
{
struct sp_meter m;
struct jcache j;
char text[1024], last[24], ses[24], l[24], s[24];
int lpct = 0, spct = 0;
time_t now = time(NULL);
int probe = 0;
(void)ctx;
c.host = h;
sp_host_meter_get(h, &m);
pthread_mutex_lock(&c.lock);
j = c.j;
if (!j.valid || now - j.at >= CACHE_PROBE_MS / 1000)
probe = 1;
pthread_mutex_unlock(&c.lock);
if (probe)
probe_junction();
pthread_mutex_lock(&c.lock);
j = c.j;
pthread_mutex_unlock(&c.lock);
human(m.ctx_used, last, sizeof(last));
human(m.tok_in, ses, sizeof(ses));
if (m.ctx_used > 0)
lpct = (int)((m.cached * 100 + m.ctx_used / 2) / m.ctx_used);
if (m.tok_in > 0)
spct = (int)((m.cached_total * 100 + m.tok_in / 2) / m.tok_in);
snprintf(l, sizeof(l), "%zu", m.cached);
snprintf(s, sizeof(s), "%zu", m.cached_total);
if (m.ctx_used == 0 && m.tok_in == 0)
snprintf(text, sizeof(text),
"prompt no request yet\n"
"junction %s", sp_host_cache_mode(h)[0] != '\0' ?
sp_host_cache_mode(h) : "unknown");
else
snprintf(text, sizeof(text),
"prompt %d%% last | %d%% session\n"
"cached %s of %s last\n"
"session %s of %s tokens",
lpct, spct, l, last, s, ses);
if (j.valid) {
size_t off = strlen(text);
char b[24];
human_bytes(j.bytes, b, sizeof(b));
snprintf(text + off, sizeof(text) - off,
"\njunction %s | %ld entries\n"
"junction %ld hits | %ld misses | %s",
sp_host_cache_mode(h)[0] != '\0' ?
sp_host_cache_mode(h) : "unknown",
j.entries, j.hits, j.misses, b);
} else {
size_t off = strlen(text);
snprintf(text + off, sizeof(text) - off,
"\njunction %s | no stats",
sp_host_cache_mode(h)[0] != '\0' ?
sp_host_cache_mode(h) : "unknown");
}
(void)sp_host_widget_text(h, name, text);
return 0;
}
static int
cache_command(struct sp_host *h, const char *args, struct sp_buf *out,
void *ctx)
{
struct sp_meter m;
struct jcache j;
char l[24], s[24], b[24];
char curl[600], chdr[600];
const struct sp_config *cfg;
int lpct = 0, spct = 0;
(void)args;
(void)ctx;
c.host = h;
cfg = sp_host_config(h);
resolve_junction(cfg, curl, sizeof(curl), chdr, sizeof(chdr));
sp_host_meter_get(h, &m);
pthread_mutex_lock(&c.lock);
j = c.j;
pthread_mutex_unlock(&c.lock);
if (m.ctx_used > 0)
lpct = (int)((m.cached * 100 + m.ctx_used / 2) / m.ctx_used);
if (m.tok_in > 0)
spct = (int)((m.cached_total * 100 + m.tok_in / 2) / m.tok_in);
human(m.ctx_used, l, sizeof(l));
human(m.tok_in, s, sizeof(s));
sp_buf_printf(out, "cache:\n");
sp_buf_printf(out, " provider last: %zu of %s tokens (%d%%)\n",
m.cached, l, lpct);
sp_buf_printf(out, " provider session: %zu of %s tokens (%d%%)\n",
m.cached_total, s, spct);
sp_buf_printf(out, " junction mode: %s\n",
sp_host_cache_mode(h)[0] != '\0' ? sp_host_cache_mode(h) : "?");
sp_buf_printf(out, " junction url: %s (mcp %zu)\n",
curl[0] != '\0' ? curl : "(not configured)",
cfg != NULL ? cfg->nmcp : 0);
if (j.valid) {
human_bytes(j.bytes, b, sizeof(b));
sp_buf_printf(out, " junction: %ld entries, %ld hits, "
"%ld misses, %s, ttl %ld s, %ld joined\n", j.entries,
j.hits, j.misses, b, j.ttl_s, j.joined);
sp_buf_printf(out, " junction status: %s\n", j.url);
} else {
sp_buf_puts(out, " junction: no /status cache stats\n");
if (c.dbg[0] != '\0')
sp_buf_printf(out, " probe: %s\n", c.dbg);
}
return 0;
}
static const struct sp_widget w = {
"cache", cache_tick, NULL, NULL, CACHE_MS
};
static int
cache_init(struct sp_host *h)
{
c.host = h;
if (sp_host_add_widget(h, &w) != 0)
return -1;
if (sp_host_add_command(h, "cache", cache_command, NULL) != 0)
return -1;
return 0;
}
static const struct sp_plugin cache_plugin = {
SP_PLUGIN_ABI, "cache", "1.0.0", cache_init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &cache_plugin;
}