/* * 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 #include #include #include #include #include #include #include #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 #define CACHE_BACKOFF_S 60 /* the longest wait after failed probes */ 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]; /* why the last probe failed; under lock */ int down; /* the last probe failed */ unsigned fails; /* failed probes in a row */ time_t next_probe; /* not before this */ } c = { NULL, PTHREAD_MUTEX_INITIALIZER, { 0 }, "", "", 0, 0, 0 }; /* * A probe that failed: say why, mark the numbers as stale (they are kept, with * their age, but no longer shown as current) and wait longer before the next * try - 3, 6, 12 ... up to 60 s. Every probe blocks the widget thread for up * to 4 s; a junction that is down must not cost that on every tick. */ static void probe_failed(const char *fmt, const char *what) { unsigned delay = CACHE_MS / 1000; unsigned k; pthread_mutex_lock(&c.lock); snprintf(c.dbg, sizeof(c.dbg), fmt, what); c.down = 1; if (c.fails < 16) c.fails++; for (k = 1; k < c.fails && delay < CACHE_BACKOFF_S; k++) delay *= 2; if (delay > CACHE_BACKOFF_S) delay = CACHE_BACKOFF_S; c.next_probe = time(NULL) + (time_t)delay; pthread_mutex_unlock(&c.lock); } /* "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); 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 || body[0] == '\0') { free(body); probe_failed("no body from %s", url); return; } v = mcp_parse(body); if (v == NULL) { probe_failed("unparseable: %.120s", body); free(body); return; } cache = mcp_arg(v, "cache"); if (cache == NULL) { probe_failed("no cache object: %.100s", body); free(body); mcp_json_free(v); return; } free(body); 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; c.down = 0; c.fails = 0; c.dbg[0] = '\0'; c.next_probe = j.at + CACHE_PROBE_MS / 1000; 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, down; (void)ctx; c.host = h; sp_host_meter_get(h, &m); pthread_mutex_lock(&c.lock); if (now >= c.next_probe) probe = 1; pthread_mutex_unlock(&c.lock); if (probe) probe_junction(); pthread_mutex_lock(&c.lock); j = c.j; down = c.down; 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 && down) { size_t off = strlen(text); /* the old numbers are not the cache now: say how old they are */ snprintf(text + off, sizeof(text) - off, "\njunction %s | stats down\n" "junction last stats %lds ago", sp_host_cache_mode(h)[0] != '\0' ? sp_host_cache_mode(h) : "unknown", (long)(now - j.at)); } else 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; char dbg[sizeof(c.dbg)]; int lpct = 0, spct = 0, down; (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; down = c.down; snprintf(dbg, sizeof(dbg), "%s", c.dbg); 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); if (down) sp_buf_printf(out, " junction: stale, from %ld s ago; " "the last probe failed\n", (long)(time(NULL) - j.at)); } else sp_buf_puts(out, " junction: no /status cache stats\n"); if (dbg[0] != '\0') sp_buf_printf(out, " probe: %s\n", 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; }