Files
splinter/plugins/junction/junction.c
T
lotherk 614988aa68 junction plugin, light/dark themes, goal completion
- plugins/junction: evaluates the X-Junction-Mcp header and /status; the
  sidebar widget, the "junction" status segment, junction_status and
  /junction show uptime, MCP-server health, queue and request counts.
  The url/header come from the plugin block, the mcp junction server or
  SPLINTER_JUNCTION_URL/TOKEN; the key is never printed.
- theme: two flat themes. light is a white page with black text, dark the
  same inverted; chat, sidebar and input share the page, the status line is
  the one separate surface. Each CDE palette keeps its own rules.
- curses: the sidebar uses the theme's own text on its own background
  (white/black) instead of white on a darkened surface; the one column
  between chat and sidebar is painted, so a white theme has no black seam.
- focus: the goal goes into the first prompt (config, resume, planner), and
  every goal-carrying text asks the model to end with GOAL_COMPLETE. The
  marker is read from the conversation and the session log and recorded as
  goal_state=succeeded in the session meta; /goal and the sidebar say
  [done], reminders stop, a new goal opens a new one.
- tests: test_junction (fake server, header identity, no colon in widget
  continuation lines, no key in the output), test_focus (announcement,
  completion in the compactor and from the tick, persisted state),
  test_theme (flat themes, list order).

make check: all tests passed.
2026-11-17 10:10:47 +00:00

550 lines
15 KiB
C

/*
* splinter -- junction: is the fleet router there, and what does it say?
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* junction is the one endpoint that holds the fleet's tools (tasksd, skilld,
* memoryd, wikid, koder) and routes the models. This plugin keeps its health
* on screen: a sidebar widget, a status segment, a tool and /junction.
*
* The header is the identity. junction answers with
*
* X-Junction-Mcp: 1
*
* on /mcp, and that is how this plugin tells junction from a proxy that happens
* to return JSON on the same port. A 200 with no such header is reported as
* "not junction", not as "up" - a health check that trusts any answer is a
* health check that is wrong exactly when it matters.
*
* The numbers come from /status (uptime, the MCP servers it aggregates, the
* queue, request counts, endpoints). They are junction's own counters; this
* plugin does not count requests a second time.
*
* Configuration, in the plugin's own block or the MCP server it already has:
*
* plugin junction {
* url = http://127.0.0.1:8090/mcp
* header = "Authorization: Bearer ${JUNCTION_TOKEN}"
* interval = 5000 # ms, default 5000
* }
*
* Without a block the `mcp junction { url = ... header = ... }` server is used,
* so there is one place for the address and the key. Without either,
* SPLINTER_JUNCTION_URL / SPLINTER_JUNCTION_TOKEN are read. The header is never
* printed: it carries the key.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 J_URL_MAX 512
#define J_HDR_MAX 512
#define J_DEFAULT_MS 5000
#define J_TIMEOUT_S "4"
/* Everything the tick thread writes and the UI thread reads lives here. */
static struct {
pthread_mutex_t lock;
char url[J_URL_MAX];
char header[J_HDR_MAX];
unsigned interval_ms;
char widget[1024]; /* the sidebar block, no colon in line 2+ */
char status[160]; /* the status segment */
int identity; /* X-Junction-Mcp was seen */
int body_ok; /* /status answered JSON */
long http_mcp; /* HTTP status of GET /mcp */
long http_status; /* HTTP status of GET /status */
unsigned long uptime_s;
unsigned mcp_ok, mcp_total;
unsigned queue_inflight, queue_max;
unsigned req_ok, req_fail;
unsigned endpoints;
time_t checked;
} j = { .lock = PTHREAD_MUTEX_INITIALIZER };
/* "1d4h", "2h07m", "20m", "41s" */
static void
human_duration(unsigned long s, char *buf, size_t len)
{
if (s >= 86400)
snprintf(buf, len, "%lud%02luh", s / 86400,
(s % 86400) / 3600);
else if (s >= 3600)
snprintf(buf, len, "%luh%02lum", s / 3600, (s % 3600) / 60);
else if (s >= 60)
snprintf(buf, len, "%lum", s / 60);
else
snprintf(buf, len, "%lus", s);
}
/* the address without its /mcp tail: /status and /mcp are two routes on it */
static void
base_url(char *buf, size_t len)
{
size_t n;
pthread_mutex_lock(&j.lock);
snprintf(buf, len, "%s", j.url);
pthread_mutex_unlock(&j.lock);
n = strlen(buf);
while (n > 0 && buf[n - 1] == '/')
buf[--n] = '\0';
if (n > 4 && strcmp(buf + n - 4, "/mcp") == 0)
buf[n - 4] = '\0';
}
/* "HTTP/1.1 405 Method Not Allowed" -> 405 */
static long
header_status(const char *headers)
{
const char *sp;
if (headers == NULL || strncmp(headers, "HTTP/", 5) != 0)
return 0;
sp = strchr(headers, ' ');
return sp != NULL ? strtol(sp + 1, NULL, 10) : 0;
}
/* one response header, case-blind; 1 when it is there */
static int
header_value(const char *headers, const char *name, char *out, size_t cap)
{
const char *p = headers;
size_t n = strlen(name);
while (p != NULL && *p != '\0') {
const char *nl = strchr(p, '\n');
size_t len = nl != NULL ? (size_t)(nl - p) : strlen(p);
if (len > n && strncasecmp(p, name, n) == 0 && p[n] == ':') {
const char *v = p + n + 1;
size_t vl;
while (*v == ' ' || *v == '\t')
v++;
vl = len - (size_t)(v - p);
while (vl > 0 && (v[vl - 1] == '\r' || v[vl - 1] == ' ' ||
v[vl - 1] == '\t'))
vl--;
snprintf(out, cap, "%.*s", (int)vl, v);
return 1;
}
p = nl != NULL ? nl + 1 : NULL;
}
return 0;
}
/*
* One GET. headers_only returns the response headers instead of the body;
* otherwise the body is returned and the HTTP status is appended by curl's -w
* on its own last line, the same shape the MCP client uses.
*/
static char *
junction_get(const char *url, int headers_only, long *http)
{
static const char *const args_headers[] = {
"-s", "-S", "-m", J_TIMEOUT_S, "-o", "/dev/null", "-D", "-",
NULL
};
static const char *const args_body[] = {
"-s", "-S", "-m", J_TIMEOUT_S, "-w", "\n%{http_code}", NULL
};
const char *hdrs[2];
char hdr[J_HDR_MAX];
struct sp_buf out, err;
char chunk[4096];
ssize_t got;
pid_t pid;
int fd, st = 0;
char *ret;
*http = 0;
if (sp_buf_init(&out) != 0)
return NULL;
(void)sp_buf_init(&err);
pthread_mutex_lock(&j.lock);
snprintf(hdr, sizeof(hdr), "%s", j.header);
pthread_mutex_unlock(&j.lock);
hdrs[0] = hdr[0] != '\0' ? hdr : NULL;
hdrs[1] = NULL;
fd = sp_curl_start(headers_only ? args_headers : args_body, url, hdrs,
1, &pid, &err);
if (fd < 0) {
sp_buf_free(&err);
sp_buf_free(&out);
return NULL;
}
while ((got = read(fd, chunk, sizeof(chunk))) > 0) {
if (sp_buf_add(&out, chunk, (size_t)got) != 0)
break;
}
close(fd);
(void)waitpid(pid, &st, 0);
if (!WIFEXITED(st) || WEXITSTATUS(st) != 0 || out.p == NULL) {
sp_buf_free(&err);
sp_buf_free(&out);
return NULL;
}
if (headers_only) {
*http = header_status(out.p);
} else {
char *nl = strrchr(out.p, '\n');
if (nl != NULL) {
*http = strtol(nl + 1, NULL, 10);
*nl = '\0';
out.len = (size_t)(nl - out.p);
}
}
sp_buf_free(&err);
ret = out.p != NULL ? out.p : calloc(1, 1);
return ret;
}
/* junction's /status: the counters it publishes, read once per tick */
static void
read_status(const char *body)
{
mcp_json *root = mcp_parse(body);
const mcp_json *m;
unsigned ok = 0, total = 0;
size_t i;
if (root == NULL)
return;
pthread_mutex_lock(&j.lock);
j.uptime_s = (unsigned long)mcp_arg_int(root, "uptime_s", 0);
j.req_ok = j.req_fail = 0;
j.endpoints = 0;
j.mcp_ok = j.mcp_total = 0;
j.queue_inflight = j.queue_max = 0;
m = mcp_arg(root, "requests");
if (m != NULL) {
j.req_ok = (unsigned)mcp_arg_int(m, "ok", 0);
j.req_fail = (unsigned)mcp_arg_int(m, "fail", 0);
}
m = mcp_arg(root, "queue");
if (m != NULL) {
j.queue_inflight = (unsigned)mcp_arg_int(m, "inflight", 0);
j.queue_max = (unsigned)mcp_arg_int(m, "max_inflight", 0);
}
m = mcp_arg(root, "mcp");
if (m != NULL) {
total = (unsigned)mcp_array_len(m);
for (i = 0; i < total; i++) {
const mcp_json *e = mcp_array_at(m, i);
if (e != NULL && mcp_arg_int(e, "ok", 0) == 1)
ok++;
}
}
m = mcp_arg(root, "endpoints");
if (m != NULL)
j.endpoints = (unsigned)mcp_array_len(m);
j.mcp_ok = ok;
j.mcp_total = total;
j.body_ok = 1;
pthread_mutex_unlock(&j.lock);
mcp_json_free(root);
}
/* the text the sidebar, the status segment and /junction show; lock held */
static void
compose(void)
{
char d[32];
human_duration(j.uptime_s, d, sizeof(d));
if (!j.identity) {
/*
* Continuation lines carry no colon: the sidebar reads a line
* with one as a new widget, not as part of this block.
*/
snprintf(j.widget, sizeof(j.widget),
"no X-Junction-Mcp header\n"
"mcp http %ld status http %ld\n"
"not junction, or a proxy in front of it",
j.http_mcp, j.http_status);
snprintf(j.status, sizeof(j.status), "junction not recognised");
return;
}
if (!j.body_ok) {
snprintf(j.widget, sizeof(j.widget),
"identified, but /status did not answer\n"
"mcp http %ld status http %ld",
j.http_mcp, j.http_status);
snprintf(j.status, sizeof(j.status), "junction up, status http %ld",
j.http_status);
return;
}
snprintf(j.widget, sizeof(j.widget),
"%s up mcp %u/%u\n"
"q %u/%u req %u ok %u fail ep %u\n"
"hdr X-Junction-Mcp %s",
d, j.mcp_ok, j.mcp_total, j.queue_inflight, j.queue_max,
j.req_ok, j.req_fail, j.endpoints, j.identity ? "seen" : "missing");
if (j.mcp_total > 0 && j.mcp_ok < j.mcp_total)
snprintf(j.status, sizeof(j.status), "junction %s up, mcp %u/%u",
d, j.mcp_ok, j.mcp_total);
else
snprintf(j.status, sizeof(j.status), "junction %s up", d);
}
static void
probe(void)
{
char base[J_URL_MAX], url[J_URL_MAX + 32];
char hval[64] = "";
char *headers = NULL, *body = NULL;
long http_mcp = 0, http_status = 0;
int identity = 0, body_ok = 0;
base_url(base, sizeof(base));
if (base[0] == '\0') {
pthread_mutex_lock(&j.lock);
snprintf(j.widget, sizeof(j.widget),
"no url configured\n"
"plugin junction { url = ... } or mcp junction { }");
snprintf(j.status, sizeof(j.status), "junction not configured");
j.identity = 0;
j.body_ok = 0;
pthread_mutex_unlock(&j.lock);
return;
}
snprintf(url, sizeof(url), "%s/mcp", base);
headers = junction_get(url, 1, &http_mcp);
if (headers != NULL) {
if (header_value(headers, "X-Junction-Mcp", hval, sizeof(hval)))
identity = 1;
free(headers);
}
snprintf(url, sizeof(url), "%s/status", base);
body = junction_get(url, 0, &http_status);
if (body != NULL) {
read_status(body);
body_ok = 1;
free(body);
}
pthread_mutex_lock(&j.lock);
j.http_mcp = http_mcp;
j.http_status = http_status;
j.identity = identity;
j.body_ok = body_ok;
j.checked = time(NULL);
compose();
pthread_mutex_unlock(&j.lock);
}
/* a text for the tool and the command: every field, the URL, never the key */
static void
snapshot_text(struct sp_buf *out)
{
char d[32], url[J_URL_MAX], hdr[8];
int identity, body_ok;
long hm, hs;
unsigned ok, total, inflight, qmax, rok, rfail, eps;
pthread_mutex_lock(&j.lock);
snprintf(url, sizeof(url), "%s", j.url);
identity = j.identity;
body_ok = j.body_ok;
hm = j.http_mcp;
hs = j.http_status;
ok = j.mcp_ok;
total = j.mcp_total;
inflight = j.queue_inflight;
qmax = j.queue_max;
rok = j.req_ok;
rfail = j.req_fail;
eps = j.endpoints;
snprintf(hdr, sizeof(hdr), "%s", j.header[0] != '\0' ? "yes" : "no");
human_duration(j.uptime_s, d, sizeof(d));
pthread_mutex_unlock(&j.lock);
sp_buf_printf(out, "junction: %s\n", identity && body_ok ? "up" :
identity ? "identified, status unreadable" : "not recognised");
sp_buf_printf(out, " url: %s\n", url[0] != '\0' ? url : "(none)");
sp_buf_printf(out, " header: %s (X-Junction-Mcp %s)\n", hdr,
identity ? "seen" : "missing");
sp_buf_printf(out, " http: /mcp %ld, /status %ld\n", hm, hs);
sp_buf_printf(out, " uptime: %s\n", d);
sp_buf_printf(out, " mcp: %u/%u answering\n", ok, total);
sp_buf_printf(out, " queue: %u/%u\n", inflight, qmax);
sp_buf_printf(out, " requests: %u ok, %u fail endpoints: %u\n",
rok, rfail, eps);
}
static void
junction_status(struct sp_host *h, char *buf, size_t len)
{
(void)h;
pthread_mutex_lock(&j.lock);
snprintf(buf, len, "%s", j.status[0] != '\0' ? j.status : "junction ?");
pthread_mutex_unlock(&j.lock);
}
static int
junction_tick(struct sp_host *h, const char *name, void *ctx)
{
char text[sizeof(j.widget)];
(void)ctx;
probe();
pthread_mutex_lock(&j.lock);
snprintf(text, sizeof(text), "%s", j.widget);
pthread_mutex_unlock(&j.lock);
return sp_host_widget_text(h, name, text);
}
static int
junction_tool(const char *args_json, struct sp_buf *out, void *ctx)
{
(void)args_json;
(void)ctx;
snapshot_text(out);
return 0;
}
static int
junction_command(struct sp_host *h, const char *args, struct sp_buf *out,
void *ctx)
{
(void)h;
(void)ctx;
if (args != NULL && *args != '\0')
return sp_buf_puts(out, "usage: /junction");
snapshot_text(out);
return 0;
}
/*
* The address and the key: the plugin's own block first, then the MCP server
* called junction (one place for both), then the environment. The key is never
* copied anywhere it could be printed.
*/
static void
resolve_config(struct sp_host *h)
{
const struct sp_config *cfg = sp_host_config(h);
const char *v;
const char *tok;
size_t i;
v = sp_host_plugin_config(h, "junction", "url");
if (v != NULL && *v != '\0') {
pthread_mutex_lock(&j.lock);
snprintf(j.url, sizeof(j.url), "%s", v);
pthread_mutex_unlock(&j.lock);
}
v = sp_host_plugin_config(h, "junction", "header");
if (v != NULL && *v != '\0') {
pthread_mutex_lock(&j.lock);
snprintf(j.header, sizeof(j.header), "%s", v);
pthread_mutex_unlock(&j.lock);
}
if (cfg != NULL && cfg->nmcp > 0) {
for (i = 0; i < cfg->nmcp; i++) {
if (strcmp(cfg->mcp[i].id, "junction") != 0)
continue;
pthread_mutex_lock(&j.lock);
if (j.url[0] == '\0' && cfg->mcp[i].url[0] != '\0')
snprintf(j.url, sizeof(j.url), "%s",
cfg->mcp[i].url);
if (j.header[0] == '\0' &&
cfg->mcp[i].header[0] != '\0')
snprintf(j.header, sizeof(j.header), "%s",
cfg->mcp[i].header);
pthread_mutex_unlock(&j.lock);
break;
}
}
if (j.url[0] == '\0') {
v = getenv("SPLINTER_JUNCTION_URL");
if (v != NULL && *v != '\0') {
pthread_mutex_lock(&j.lock);
snprintf(j.url, sizeof(j.url), "%s", v);
pthread_mutex_unlock(&j.lock);
}
}
if (j.header[0] == '\0') {
tok = getenv("SPLINTER_JUNCTION_TOKEN");
if (tok == NULL || *tok == '\0')
tok = getenv("JUNCTION_TOKEN");
if (tok != NULL && *tok != '\0') {
pthread_mutex_lock(&j.lock);
snprintf(j.header, sizeof(j.header),
"Authorization: Bearer %s", tok);
pthread_mutex_unlock(&j.lock);
}
}
v = sp_host_plugin_config(h, "junction", "interval");
if (v != NULL && atoi(v) > 0)
j.interval_ms = (unsigned)atoi(v);
else if ((v = getenv("SPLINTER_JUNCTION_INTERVAL")) != NULL &&
atoi(v) > 0)
j.interval_ms = (unsigned)atoi(v);
if (j.interval_ms < 1000)
j.interval_ms = 1000;
if (j.interval_ms > 600000)
j.interval_ms = 600000;
}
static int
junction_init(struct sp_host *h)
{
struct sp_widget w;
static const struct sp_status seg = { "junction", junction_status };
static const struct sp_tool tool = {
"junction_status",
"The fleet router's health: whether it answers, the X-Junction-Mcp "
"identity header, uptime, how many MCP servers answer, the queue "
"and the request counts from junction's own /status.",
"{\"type\":\"object\",\"properties\":{}}",
SP_AUTH_READ,
junction_tool,
NULL
};
j.interval_ms = J_DEFAULT_MS;
resolve_config(h);
memset(&w, 0, sizeof(w));
w.name = "junction";
w.tick = junction_tick;
w.interval_ms = j.interval_ms;
if (sp_host_add_widget(h, &w) != 0)
return -1;
if (sp_host_add_status(h, &seg) != 0)
return -1;
if (sp_host_add_tool(h, &tool) != 0)
return -1;
if (sp_host_add_command(h, "junction", junction_command, NULL) != 0)
return -1;
return 0;
}
static const struct sp_plugin junction_plugin = {
SP_PLUGIN_ABI, "junction", "1.0.0", junction_init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &junction_plugin;
}