/* * 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. * * Every response header is kept, not only the identity one: the model route * adds the routing statistics (X-Junction-Endpoint, X-Junction-Ms, * X-Junction-Cache), /status adds the ordinary HTTP headers. The widget shows * the junction ones as one compact statistics line, /junction prints every * header of every probe grouped by source. * * The numbers come from /status (uptime, the MCP servers it aggregates, the * queue, request counts, endpoints) and /v1/models (how many models it * serves). junction accepts /status, /v1/status, /queue and /v1/queue as the * same route; the plugin remembers which one answered, so a changed alias does * not cost a probe on every tick. * * Configuration, in the plugin's own block or the MCP server it already has: * * plugin junction { * url = http://127.0.0.1:8090/mcp # the MCP endpoint * header = "Authorization: Bearer ${JUNCTION_TOKEN}" * interval = 5000 # ms between MCP/status checks, default 5000 * models_interval = 60000 # ms between /v1/models probes, default 60000 * status_path = /status # first alias to try * } * * 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 #include #include #include #include #include #include #include #include "splinter.h" #include "splinter_plugin.h" #include "config.h" #include "curl.h" #include "mcp.h" #include "usage.h" #define J_URL_MAX 512 #define J_HDR_MAX 512 #define J_DEFAULT_MS 5000 #define J_MODELS_MS 60000 #define J_TIMEOUT_S "4" #define J_MAX_HEADERS 24 #define J_STAT_MAX 768 #define J_ALIASES 4 struct jhead { char name[80]; char value[200]; }; struct jset { struct jhead v[J_MAX_HEADERS]; size_t n; long http; }; struct jep { char name[32]; int probe_ok, ok, fail, exhausted; long last_ms; }; /* Everything the tick thread writes and the UI thread reads lives here. */ static struct { struct sp_host *host; pthread_mutex_t lock; char url[J_URL_MAX]; char header[J_HDR_MAX]; unsigned interval_ms; unsigned models_ms; char status_path[24]; /* the alias that answered last */ char status_cfg[24]; /* status_path from the config */ int status_alias; /* index into aliases, -1 unknown, J_ALIASES the configured one */ char widget[1024]; /* the sidebar block, no colon in line 2+ */ char stats[J_STAT_MAX]; /* the X-Junction-* statistics */ 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 the status route */ long http_models; /* HTTP status of /v1/models */ unsigned long uptime_s; unsigned mcp_ok, mcp_total; unsigned queue_inflight, queue_waiting, queue_max; unsigned req_ok, req_fail; unsigned endpoints, exhausted; long p95_ms; unsigned models; char model[256]; /* the model this run is on */ char served[128]; /* which target answered last */ long served_ms; time_t served_at; int have_prev; struct { char name[32]; int ok; } prev[40]; size_t nprev; char route[256]; /* the rule targets, space separated */ int route_blocked; /* the matching rule blocks the model */ char route_down[160]; /* targets whose probe failed */ unsigned route_up, route_n; struct jep ep[40]; size_t nep; struct jset h_mcp, h_status, h_models; time_t next_models; time_t checked; } j = { .lock = PTHREAD_MUTEX_INITIALIZER, .status_alias = -1 }; static const char *const status_aliases[J_ALIASES] = { "/status", "/v1/status", "/queue", "/v1/queue" }; /* "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: the other routes live beside 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; } static void set_clear(struct jset *s) { s->n = 0; s->http = 0; } /* * The whole response header block into a set: the status line is not a header, * a folded line (continuation) is joined to the header before it. The block is * "Name: value" lines; every one of them is kept, not only X-Junction-*. */ static void set_parse(const char *block, struct jset *s) { const char *p = block; set_clear(s); s->http = header_status(block); while (p != NULL && *p != '\0' && s->n < J_MAX_HEADERS) { const char *nl = strchr(p, '\n'); size_t len = nl != NULL ? (size_t)(nl - p) : strlen(p); const char *colon; char *name, *value; size_t nl2, vl; while (len > 0 && (p[len - 1] == '\r' || p[len - 1] == ' ' || p[len - 1] == '\t')) len--; colon = memchr(p, ':', len); if (colon != NULL && (size_t)(colon - p) < sizeof(s->v[0].name)) { nl2 = (size_t)(colon - p); name = s->v[s->n].name; memcpy(name, p, nl2); name[nl2] = '\0'; value = s->v[s->n].value; while ((size_t)(colon + 1 - p) < len && (colon[1] == ' ' || colon[1] == '\t')) colon++; vl = len - (size_t)(colon + 1 - p); if (vl >= sizeof(s->v[0].value)) vl = sizeof(s->v[0].value) - 1; memcpy(value, colon + 1, vl); value[vl] = '\0'; s->n++; } p = nl != NULL ? nl + 1 : NULL; } } /* the value of the first header with this name in a set */ static int set_value(const struct jset *s, const char *name, char *out, size_t cap) { size_t i; for (i = 0; i < s->n; i++) { if (strcasecmp(s->v[i].name, name) == 0) { snprintf(out, cap, "%s", s->v[i].value); return 1; } } return 0; } /* * One request. The output is the header block and, unless headers_only, the * body; curl's -w appends the status code on its own last line. The two parts * are split at the empty line, so callers get both without a second request - * the routing headers are the reason this exists. */ static char * junction_get(const char *url, struct jset *set, char **bodyp) { static const char *const args[] = { "-s", "-S", "-m", J_TIMEOUT_S, "-D", "-", "-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 *raw, *body = NULL, *sep; size_t hlen; *bodyp = NULL; set_clear(set); 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(args, 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; } sp_buf_free(&err); raw = out.p; /* the header block ends at the first empty line */ hlen = 0; sep = strstr(raw, "\r\n\r\n"); if (sep != NULL) { hlen = (size_t)(sep - raw); body = sep + 4; } else if ((sep = strstr(raw, "\n\n")) != NULL) { hlen = (size_t)(sep - raw); body = sep + 2; } { char block[16384]; if (hlen >= sizeof(block)) hlen = sizeof(block) - 1; memcpy(block, raw, hlen); block[hlen] = '\0'; set_parse(block, set); } if (body != NULL) { char *nl = strrchr(body, '\n'); if (nl != NULL) { set->http = strtol(nl + 1, NULL, 10); *nl = '\0'; } *bodyp = body; } else set->http = header_status(raw); return raw; } /* * junction's model patterns: '*' and '?' only, everything else literal. * Iterative Thompson-style matcher — avoids stack overflow on long patterns. */ static int glob_match(const char *pat, const char *s) { const char *star = NULL; const char *s_backup = NULL; while (*s != '\0') { if (*pat == '*' || *pat == '?') { if (*pat == '*') { star = pat++; s_backup = s; } else { pat++; } continue; } if (*pat != *s) { if (star != NULL) { pat = star + 1; s_backup++; s = s_backup; } else { return 0; } } else { pat++; s++; } } while (*pat == '*') pat++; return *pat == '\0'; } /* * Which rule routes this model? The specific rule beats the glob, the glob * beats the catch-all, so "qwen3.6-35b" wins over "*" even when the catch-all * is listed first. Rules with a path describe another route (/v1/models) and * are skipped: they say nothing about a chat request. */ static void route_for_model(const mcp_json *rules, const char *model, char *out, size_t cap, char *blocked, size_t bcap) { size_t i, n = mcp_array_len(rules); int best = 0; const char *best_targets = NULL; out[0] = '\0'; blocked[0] = '\0'; for (i = 0; i < n; i++) { const mcp_json *r = mcp_array_at(rules, i); const char *mp, *path, *targets; int score = 0; if (r == NULL) continue; mp = mcp_arg_string(r, "model"); path = mcp_arg_string(r, "path"); targets = mcp_arg_string(r, "targets"); if (path != NULL && *path != '\0') continue; /* a different route */ if (targets == NULL || *targets == '\0') continue; if (mp == NULL || *mp == '\0') score = 1; /* catch-all */ else if (strcmp(mp, model) == 0) score = 3; /* exact */ else if (glob_match(mp, model)) score = 2; /* glob */ if (score > best) { best = score; best_targets = targets; snprintf(blocked, bcap, "%s", mcp_arg_bool(r, "block", 0) ? "yes" : ""); } } if (best_targets == NULL) return; /* "kontron as /models/x.gguf, kompute0 as ..." -> "kontron kompute0" */ { const char *p = best_targets; size_t off = 0; out[0] = '\0'; while (*p != '\0' && off + 2 < cap) { char item[256]; size_t len = 0; const char *as; while (*p == ' ' || *p == '\t') p++; while (*p != '\0' && *p != ',' && len + 1 < sizeof(item)) item[len++] = *p++; item[len] = '\0'; if (*p == ',') p++; as = strstr(item, " as "); if (as != NULL) *((char *)as) = '\0'; /* the target's own name only, not its model option */ { char *sp = strchr(item, ' '); if (sp != NULL) *sp = '\0'; } if (item[0] == '\0') continue; off += (size_t)snprintf(out + off, cap - off, "%s%s", off > 0 ? " " : "", item); } } } /* the endpoint table entry of a target name, or NULL */ static const struct jep * route_ep(const char *name) { size_t i; for (i = 0; i < j.nep; i++) if (strcmp(j.ep[i].name, name) == 0) return &j.ep[i]; return NULL; } /* route_up/route_down from the endpoint table; lock held */ static void route_health(void) { char down[sizeof(j.route_down)], copy[sizeof(j.route)]; size_t off = 0; char *p, *save = NULL; j.route_up = j.route_n = 0; down[0] = '\0'; snprintf(copy, sizeof(copy), "%s", j.route); for (p = strtok_r(copy, " ", &save); p != NULL; p = strtok_r(NULL, " ", &save)) { const struct jep *e = route_ep(p); j.route_n++; if (e != NULL && e->probe_ok == 1) { j.route_up++; continue; } if (off < sizeof(down) - 1) off += (size_t)snprintf(down + off, sizeof(down) - off, "%s%s", off > 0 ? " " : "", p); } snprintf(j.route_down, sizeof(j.route_down), "%s", down); } /* 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, exhausted = 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 = j.exhausted = 0; j.mcp_ok = j.mcp_total = 0; j.queue_inflight = j.queue_waiting = j.queue_max = 0; j.p95_ms = 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_waiting = (unsigned)mcp_arg_int(m, "waiting", 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.nep = 0; for (i = 0; i < j.endpoints; i++) { const mcp_json *e = mcp_array_at(m, i); const char *name; long p95; if (e == NULL) continue; if (mcp_arg_int(e, "exhausted", 0) == 1) exhausted++; p95 = (long)mcp_arg_int(e, "p95_ms", 0); if (p95 > j.p95_ms) j.p95_ms = p95; name = mcp_arg_string(e, "name"); if (name != NULL && j.nep < 40) { snprintf(j.ep[j.nep].name, sizeof(j.ep[j.nep].name), "%s", name); j.ep[j.nep].probe_ok = mcp_arg_int(e, "probe_ok", 0) == 1; j.ep[j.nep].ok = mcp_arg_int(e, "ok", 0); j.ep[j.nep].fail = mcp_arg_int(e, "fail", 0); j.ep[j.nep].exhausted = mcp_arg_int(e, "exhausted", 0) == 1; j.ep[j.nep].last_ms = (long)mcp_arg_int(e, "last_ms", -1); j.nep++; } } } m = mcp_arg(root, "rules"); j.route_blocked = 0; if (m != NULL) { char blocked[8]; route_for_model(m, j.model, j.route, sizeof(j.route), blocked, sizeof(blocked)); /* * A status, not a target list: j.route is split on spaces as * target names, so the text must not end up in it. */ if (blocked[0] != '\0') { j.route_blocked = 1; j.route[0] = '\0'; } } route_health(); /* * Streaming answers carry no X-Junction-Endpoint (only non-stream and * /v1/models do), so the interface counter is the next best witness: * the route target whose "ok" grew since the last sample answered. * Our own /v1/models probe hits another endpoint and is ignored. */ if (j.route[0] != '\0') { char copy[sizeof(j.route)], *tok, *save = NULL; snprintf(copy, sizeof(copy), "%s", j.route); for (tok = strtok_r(copy, " ", &save); tok != NULL; tok = strtok_r(NULL, " ", &save)) { size_t k, e; for (e = 0; e < j.nep; e++) if (strcmp(j.ep[e].name, tok) == 0) break; if (e == j.nep) continue; for (k = 0; k < j.nprev; k++) if (strcmp(j.prev[k].name, tok) == 0) { if (j.have_prev && j.ep[e].ok > j.prev[k].ok) { snprintf(j.served, sizeof(j.served), "%s", tok); j.served_ms = j.ep[e].last_ms; j.served_at = time(NULL); } break; } } } j.nprev = 0; { size_t e; for (e = 0; e < j.nep && j.nprev < 40; e++) { snprintf(j.prev[j.nprev].name, sizeof(j.prev[0].name), "%s", j.ep[e].name); j.prev[j.nprev].ok = j.ep[e].ok; j.nprev++; } } j.have_prev = 1; j.mcp_ok = ok; j.mcp_total = total; j.exhausted = exhausted; j.body_ok = 1; pthread_mutex_unlock(&j.lock); mcp_json_free(root); } /* how many models the router serves; the list itself is /model's business */ static void read_models(const char *body) { mcp_json *root = mcp_parse(body); const mcp_json *m; if (root == NULL) return; m = mcp_arg(root, "data"); pthread_mutex_lock(&j.lock); j.models = m != NULL ? (unsigned)mcp_array_len(m) : 0; pthread_mutex_unlock(&j.lock); mcp_json_free(root); } /* "X-Junction-Mcp: 1" -> "mcp=1", all sets, first one per name wins */ static void compose_stats(void) { static const struct jset *sets[3]; struct jhead seen[J_MAX_HEADERS]; size_t nseen = 0, off = 0, s, i, k; char buf[J_STAT_MAX]; sets[0] = &j.h_models; sets[1] = &j.h_status; sets[2] = &j.h_mcp; buf[0] = '\0'; for (s = 0; s < 3 && off < sizeof(buf) - 1; s++) { const struct jset *set = sets[s]; for (i = 0; i < set->n && off < sizeof(buf) - 1; i++) { const char *name = set->v[i].name; char key[40]; size_t kl; if (strncasecmp(name, "X-Junction-", 11) != 0) continue; for (k = 0; k < nseen; k++) if (strcasecmp(seen[k].name, name) == 0) break; if (k < nseen) continue; /* same header from another route */ if (nseen < J_MAX_HEADERS) snprintf(seen[nseen++].name, sizeof(seen[0].name), "%s", name); snprintf(key, sizeof(key), "%s", name + 11); for (kl = 0; key[kl] != '\0'; kl++) if (key[kl] >= 'A' && key[kl] <= 'Z') key[kl] = (char)(key[kl] - 'A' + 'a'); /* * The endpoint header belongs to the probe's own route * (/v1/models -> opencode-go), not to the conversation; * printing it as "endpoint" was what made the widget lie. * The real route comes from /status' rules. */ if (strcmp(key, "endpoint") == 0) continue; if (strcmp(key, "ms") == 0) snprintf(key, sizeof(key), "probe-ms"); else if (strcmp(key, "cache") == 0) snprintf(key, sizeof(key), "probe-cache"); off += (size_t)snprintf(buf + off, sizeof(buf) - off, "%s%s=%s", off > 0 ? " " : "", key, set->v[i].value); /* truncated: clamp, and both loops stop on the guard */ if (off >= sizeof(buf) - 1) off = sizeof(buf) - 1; } } snprintf(j.stats, sizeof(j.stats), "%s", buf); } /* 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)); { const char *header = sp_host_served_by(j.host); time_t now = time(NULL); if (j.served[0] == '\0' || j.served_at == 0 || now - j.served_at > 120) { if (header != NULL && *header != '\0') { snprintf(j.served, sizeof(j.served), "%s", header); j.served_ms = 0; j.served_at = now; } } } compose_stats(); 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 models http %ld\n" "not junction, or a proxy in front of it", j.http_mcp, j.http_status, j.http_models); snprintf(j.status, sizeof(j.status), "junction not recognised"); return; } if (!j.body_ok) { snprintf(j.widget, sizeof(j.widget), "identified, but the status route did not answer\n" "mcp http %ld status http %ld (%s)", j.http_mcp, j.http_status, j.status_path); snprintf(j.status, sizeof(j.status), "junction up, status http %ld", j.http_status); return; } { /* * Only what belongs to the model this run is on: its name and the * route the matching rule gives it, one target per row. A "!" * marks a target whose own health probe is failing. The server * counters (MCP servers, queue, requests, probe headers) are not * about the conversation and live in /junction. */ char copy[sizeof(j.route)]; char *tok, *save = NULL; size_t off; int first = 1; char sbuf[160]; if (j.served[0] == '\0') snprintf(sbuf, sizeof(sbuf), "(no request yet)"); else if (j.served_ms > 0 && j.served_ms < 600000) snprintf(sbuf, sizeof(sbuf), "%s %.1fs", j.served, (double)j.served_ms / 1000.0); else snprintf(sbuf, sizeof(sbuf), "%s", j.served); off = (size_t)snprintf(j.widget, sizeof(j.widget), "%s\nserving %s\n", j.model[0] != '\0' ? j.model : "(model unknown)", sbuf); { struct sp_judge_next jn; if (sp_host_judge_next(j.host, &jn) && jn.valid) { if (jn.in > 0) off += (size_t)snprintf(j.widget + off, sizeof(j.widget) - off, "judge next in %d %s via %s " "%d/%d cache %s\n", jn.in, jn.model[0] != '\0' ? jn.model : "?", jn.target[0] != '\0' ? jn.target : "?", jn.step, jn.every, jn.cache[0] != '\0' ? jn.cache : "?"); else off += (size_t)snprintf(j.widget + off, sizeof(j.widget) - off, "judge next now %s via %s " "%d/%d cache %s\n", jn.model[0] != '\0' ? jn.model : "?", jn.target[0] != '\0' ? jn.target : "?", jn.step, jn.every, jn.cache[0] != '\0' ? jn.cache : "?"); } } if (off < sizeof(j.widget)) off += (size_t)snprintf(j.widget + off, sizeof(j.widget) - off, "route "); snprintf(copy, sizeof(copy), "%s", j.route); for (tok = strtok_r(copy, " ", &save); tok != NULL; tok = strtok_r(NULL, " ", &save)) { const struct jep *ep = route_ep(tok); int is_down = (ep != NULL && ep->probe_ok != 1); if (off < sizeof(j.widget)) off += (size_t)snprintf(j.widget + off, sizeof(j.widget) - off, "%s%s%s", first ? "" : "\n", tok, is_down ? "!" : ""); first = 0; } if (first) off = (size_t)snprintf(j.widget, sizeof(j.widget), "%s\nserving %s\nroute %s", j.model[0] != '\0' ? j.model : "(model unknown)", sbuf, j.route_blocked ? "blocked by a rule" : "(no rule matched)"); if (off < sizeof(j.widget)) snprintf(j.widget + off, sizeof(j.widget) - off, "\n%u/%u up", j.route_up, j.route_n); } 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); } /* GET one alias of the status route; 1 when it answered JSON */ static int probe_status(const char *base, const char *path, struct jset *set) { char url[J_URL_MAX + 40]; char *body = NULL; char *raw; int ok = 0; snprintf(url, sizeof(url), "%s%s", base, path); raw = junction_get(url, set, &body); if (raw != NULL) { if (body != NULL && set->http == 200 && body[0] == '{') { read_status(body); ok = 1; } free(raw); } return ok; } /* * The model this run is on: the active provider's model, else the main one. * /status' rules are matched against it, so the plugin shows the route the * conversation takes, not the route of its own /v1/models probe. * * j.host is set by junction_init() before the tick thread starts; it is * never changed, so no lock is needed here. */ static void current_model(char *buf, size_t cap) { const struct sp_config *cfg = sp_host_config(j.host); const char *act = sp_usage_active(); size_t i; buf[0] = '\0'; { const char *live = sp_host_model(j.host); if (live != NULL && *live != '\0') { snprintf(buf, cap, "%s", live); return; } } if (cfg == NULL) return; for (i = 0; act != NULL && *act != '\0' && i < cfg->ndelegates; i++) if (strcmp(cfg->delegate[i].id, act) == 0) { snprintf(buf, cap, "%s", cfg->delegate[i].model); return; } snprintf(buf, cap, "%s", cfg->model); } static void probe(void) { char base[J_URL_MAX], url[J_URL_MAX + 40]; char hval[64] = "", path[24]; char *body = NULL, *raw; long http_mcp = 0, http_models = 0; int identity = 0, body_ok = 0, i; struct jset hmcp, hstatus, hmodels; time_t now = time(NULL); char model[256]; current_model(model, sizeof(model)); pthread_mutex_lock(&j.lock); snprintf(j.model, sizeof(j.model), "%s", model); pthread_mutex_unlock(&j.lock); set_clear(&hmcp); set_clear(&hstatus); set_clear(&hmodels); 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); raw = junction_get(url, &hmcp, &body); if (raw != NULL) { http_mcp = hmcp.http; if (set_value(&hmcp, "X-Junction-Mcp", hval, sizeof(hval))) identity = 1; free(raw); } /* * /status, /v1/status, /queue and /v1/queue are the same route on * junction. Remember the one that answered; try the whole list from * the start only when it stops answering. */ if (j.status_alias >= 0 && j.status_alias < J_ALIASES) { snprintf(path, sizeof(path), "%s", status_aliases[j.status_alias]); body_ok = probe_status(base, path, &hstatus); } /* the configured status_path is tried first, and remembered */ if (!body_ok && j.status_cfg[0] != '\0') { snprintf(path, sizeof(path), "%s", j.status_cfg); body_ok = probe_status(base, path, &hstatus); j.status_alias = body_ok ? J_ALIASES : -1; } for (i = 0; !body_ok && i < J_ALIASES; i++) { if (j.status_alias == i || strcmp(status_aliases[i], j.status_cfg) == 0) continue; snprintf(path, sizeof(path), "%s", status_aliases[i]); body_ok = probe_status(base, path, &hstatus); if (body_ok) { j.status_alias = i; break; } } if (!body_ok) { j.status_alias = -1; snprintf(path, sizeof(path), "%s", status_aliases[0]); } /* the model route carries the routing headers; less often than the rest */ if (now >= j.next_models) { snprintf(url, sizeof(url), "%s/v1/models", base); raw = junction_get(url, &hmodels, &body); if (raw != NULL) { http_models = hmodels.http; if (body != NULL && hmodels.http == 200 && body[0] == '{') read_models(body); free(raw); } j.next_models = now + (time_t)(j.models_ms / 1000); } pthread_mutex_lock(&j.lock); snprintf(j.status_path, sizeof(j.status_path), "%s", path); j.h_mcp = hmcp; j.h_status = hstatus; if (http_models != 0) j.h_models = hmodels; j.http_mcp = http_mcp; j.http_status = hstatus.http; j.http_models = http_models != 0 ? http_models : j.h_models.http; 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], status_path[24]; int identity, body_ok; struct jset hm, hs, hmod; size_t src, i; pthread_mutex_lock(&j.lock); snprintf(url, sizeof(url), "%s", j.url); snprintf(status_path, sizeof(status_path), "%s", j.status_path); identity = j.identity; body_ok = j.body_ok; snprintf(hdr, sizeof(hdr), "%s", j.header[0] != '\0' ? "yes" : "no"); hm = j.h_mcp; hs = j.h_status; hmod = j.h_models; human_duration(j.uptime_s, d, sizeof(d)); 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, %s %ld, /v1/models %ld\n", hm.http, status_path, hs.http, hmod.http); sp_buf_printf(out, " uptime: %s\n", d); sp_buf_printf(out, " mcp: %u/%u answering\n", j.mcp_ok, j.mcp_total); sp_buf_printf(out, " queue: %u/%u, %u waiting\n", j.queue_inflight, j.queue_max, j.queue_waiting); sp_buf_printf(out, " requests: %u ok, %u fail\n", j.req_ok, j.req_fail); sp_buf_printf(out, " endpoints: %u, %u exhausted, p95 %ldms\n", j.endpoints, j.exhausted, j.p95_ms); sp_buf_printf(out, " models: %u\n", j.models); sp_buf_printf(out, " model: %s\n", j.model[0] != '\0' ? j.model : "(unknown)"); if (strstr(j.model, "judged") != NULL) sp_buf_puts(out, " judged: yes (judge calls are upstream; the " "provider's usage carries only the final answer)\n"); sp_buf_printf(out, " serving: %s\n", j.served[0] != '\0' ? j.served : "(no request yet)"); { struct sp_judge_next jn; if (sp_host_judge_next(j.host, &jn) && jn.valid) sp_buf_printf(out, " judge next: in %d calls, " "step %d/%d, %s via %s, cache %s (counter %d)\n", jn.in, jn.step, jn.every, jn.model[0] != '\0' ? jn.model : "?", jn.target[0] != '\0' ? jn.target : "?", jn.cache[0] != '\0' ? jn.cache : "?", jn.next); else sp_buf_puts(out, " judge next: no headers\n"); } sp_buf_printf(out, " route: %s\n", j.route[0] != '\0' ? j.route : j.route_blocked ? "blocked by a rule" : "(no rule matched)"); sp_buf_printf(out, " route health: %u/%u up%s%s\n", j.route_up, j.route_n, j.route_down[0] != '\0' ? ", down: " : "", j.route_down); sp_buf_printf(out, " statistics: %s\n", j.stats[0] != '\0' ? j.stats : "(no X-Junction headers)"); for (src = 0; src < 3; src++) { const struct jset *s = src == 0 ? &hm : (src == 1 ? &hs : &hmod); const char *label = src == 0 ? "/mcp" : (src == 1 ? status_path : "/v1/models"); sp_buf_printf(out, " headers %s (http %ld, %zu)%s:\n", label, s->http, s->n, src == 2 ? " - this is the probe's route, not the session's" : ""); for (i = 0; i < s->n; i++) sp_buf_printf(out, " %s: %s\n", s->v[i].name, s->v[i].value); } pthread_mutex_unlock(&j.lock); } 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; v = sp_host_plugin_config(h, "junction", "models_interval"); if (v != NULL && atoi(v) > 0) j.models_ms = (unsigned)atoi(v); if (j.models_ms < 10000) j.models_ms = J_MODELS_MS; v = sp_host_plugin_config(h, "junction", "status_path"); if (v != NULL && *v == '/') { snprintf(j.status_cfg, sizeof(j.status_cfg), "%s", v); snprintf(j.status_path, sizeof(j.status_path), "%s", v); } else { j.status_cfg[0] = '\0'; snprintf(j.status_path, sizeof(j.status_path), "%s", status_aliases[0]); } } 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, every response header, uptime, how many MCP " "servers answer, the queue, request counts, endpoints and the " "number of models it serves.", "{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, junction_tool, NULL, NULL }; j.host = h; j.interval_ms = J_DEFAULT_MS; j.models_ms = J_MODELS_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.2.0", junction_init, NULL, NULL }; const struct sp_plugin * sp_plugin_describe(void) { return &junction_plugin; }