From b0c1da65a2d23fe48e7416a4927a13f227f92cf5 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 12:31:58 +0000 Subject: [PATCH] junction: the session's route, not the probe's endpoint The widget printed "endpoint=opencode-go" because that is the X-Junction-Endpoint header of the plugin's own /v1/models probe, which junction routes there by a path rule. It says nothing about where the conversation goes, and the operator's qwen3.6-35b is routed to kontron and kompute0..2 - so the line was wrong, not just imprecise. - read the active model from the config (sp_usage_active -> provider model) and match /status' rules against it: exact model > glob > catch-all, rules with a path are another route. The result is the route line: "route kontron! kompute0 kompute1 kompute2" ("!" = that target's own health probe is not ok). - /junction shows model, route and "route health: N/M up, down: ..."; the /v1/models header block is marked as the probe's route, not the session's. - the header stats label the probe values (probe-ms=, probe-cache=) and no longer print the endpoint header as if it were the route. - side_text(): wrap one line at a time and let a continuation use up to three rows; passing a multi-line string to one mvwaddnstr() double-counted rows and cut "route ... kompute1 kompute2" in half. - test_junction: fake /status gains rules and per-endpoint probe_ok; the test sets the run's model ("m") and expects "route a b!", "probe-ms=12", "route health: 1/2 up, down: b" and no "endpoint=" in the stats. make check: all tests passed. --- docs/PLUGINS.md | 31 ++-- plugins/junction/junction.c | 287 ++++++++++++++++++++++++++++++++++-- src/frontend/curses.c | 65 +++++--- tests/fake_junction.py | 14 +- tests/test_junction.c | 26 +++- 5 files changed, 376 insertions(+), 47 deletions(-) diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 91c57eb..9fee2bf 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -264,24 +264,31 @@ and that header is the identity: a 200 with the header is junction, a 200 without it is "not junction, or a proxy in front of it" — a health check that trusts any answer 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 stats line; `/junction` prints every header of -every probe, grouped by source. +Every response header is kept, not only the identity one. `/status` adds the +ordinary HTTP headers, the model route adds `X-Junction-Ms`/`X-Junction-Cache` +and its own `X-Junction-Endpoint`. That last one is **the probe's** route +(`/v1/models` is routed to `opencode-go` by junction's rules), not the route of +the conversation — so the widget does not print it as "endpoint". The real +route comes from `/status`' `rules`: the rule matching this run's model (the +active provider's model) gives the targets, and the endpoint table gives their +health. ``` junction 20m up mcp 5/5 models 9 -endpoint=opencode-go ms=449 cache=off mcp=1 -q 1/32 req 11 ok 0 fail ep 25 (3 ex) p95 500ms +route kontron! kompute0 +kompute1 kompute2 +q 1/32 req 11 ok 0 fail ep 25 (3 ex) +probe-ms=449 probe-cache=off mcp=1 ``` -The numbers come from the status route (uptime, MCP servers, queue, requests, -endpoints, p95) 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 and only tries the whole list when it stops. It -does not count a second time. +A `!` marks a route target whose own health probe is failing; `/junction` +spells out `route health: 3/4 up, down: kontron`. The numbers come from the +status route (uptime, MCP servers, queue, requests, endpoints, p95) and +`/v1/models` (how many models it serves, and the probe's headers). junction +accepts `/status`, `/v1/status`, `/queue` and `/v1/queue` as the same route; +the plugin remembers which one answered and only tries the whole list when it +stops. It does not count a second time. That is a widget, a status segment (`junction 20m up`), the tool `junction_status` and `/junction`. The address and the key come from the diff --git a/plugins/junction/junction.c b/plugins/junction/junction.c index 5b293ac..cc503f3 100644 --- a/plugins/junction/junction.c +++ b/plugins/junction/junction.c @@ -58,6 +58,7 @@ #include "config.h" #include "curl.h" #include "mcp.h" +#include "usage.h" #define J_URL_MAX 512 #define J_HDR_MAX 512 @@ -79,8 +80,14 @@ struct jset { long http; }; +struct jep { + char name[32]; + int probe_ok, ok, fail, exhausted; +}; + /* 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]; @@ -104,6 +111,12 @@ static struct { unsigned endpoints, exhausted; long p95_ms; unsigned models; + char model[256]; /* the model this run is on */ + char route[256]; /* the rule targets, space separated */ + 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; @@ -307,6 +320,147 @@ junction_get(const char *url, struct jset *set, char **bodyp) return raw; } +/* junction's model patterns: '*' and '?' only, everything else literal */ +static int +glob_match(const char *pat, const char *s) +{ + if (*pat == '\0') + return *s == '\0'; + if (*pat == '*') { + while (*pat == '*') + pat++; + if (*pat == '\0') + return 1; + for (; *s != '\0'; s++) + if (glob_match(pat, s)) + return 1; + return 0; + } + if (*pat == '?') + return *s != '\0' && glob_match(pat + 1, s + 1); + if (*pat != *s) + return 0; + return glob_match(pat + 1, s + 1); +} + +/* + * 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; + } + 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) @@ -350,8 +504,10 @@ read_status(const char *body) 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) @@ -361,8 +517,30 @@ read_status(const char *body) 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.nep++; + } } } + m = mcp_arg(root, "rules"); + if (m != NULL) { + char blocked[8]; + + route_for_model(m, j.model, j.route, sizeof(j.route), + blocked, sizeof(blocked)); + if (blocked[0] != '\0') + snprintf(j.route, sizeof(j.route), "blocked by a rule"); + } + route_health(); j.mcp_ok = ok; j.mcp_total = total; j.exhausted = exhausted; @@ -422,6 +600,18 @@ compose_stats(void) 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); @@ -462,14 +652,54 @@ compose(void) j.http_status); return; } - snprintf(j.widget, sizeof(j.widget), - "%s up mcp %u/%u models %u\n" - "%s\n" - "q %u/%u req %u ok %u fail ep %u (%u ex) p95 %ldms", - d, j.mcp_ok, j.mcp_total, j.models, - j.stats[0] != '\0' ? j.stats : "(no X-Junction headers)", - j.queue_inflight, j.queue_max, j.req_ok, j.req_fail, - j.endpoints, j.exhausted, j.p95_ms); + { + /* + * The route the conversation takes, from the rule that matches + * this run's model. A target whose own health probe fails gets + * a "!"; /junction spells the counts out. + */ + char marked[sizeof(j.route) + 64], copy[sizeof(j.route)]; + char *tok, *save = NULL; + size_t off = 0; + + marked[0] = '\0'; + snprintf(copy, sizeof(copy), "%s", j.route); + for (tok = strtok_r(copy, " ", &save); tok != NULL; + tok = strtok_r(NULL, " ", &save)) { + char down[sizeof(j.route_down)], *dt, *ds = NULL; + int is_down = 0; + + snprintf(down, sizeof(down), "%s", j.route_down); + for (dt = strtok_r(down, " ", &ds); dt != NULL; + dt = strtok_r(NULL, " ", &ds)) + if (strcmp(dt, tok) == 0) + is_down = 1; + off += (size_t)snprintf(marked + off, + sizeof(marked) - off, "%s%s%s", + off > 0 ? " " : "", tok, is_down ? "!" : ""); + } + if (marked[0] != '\0') + snprintf(j.widget, sizeof(j.widget), + "%s up mcp %u/%u models %u\n" + "route %s\n" + "q %u/%u req %u ok %u fail ep %u (%u ex)\n" + "%s", + d, j.mcp_ok, j.mcp_total, j.models, marked, + j.queue_inflight, j.queue_max, j.req_ok, + j.req_fail, j.endpoints, j.exhausted, + j.stats[0] != '\0' ? j.stats : + "(no X-Junction headers)"); + else + snprintf(j.widget, sizeof(j.widget), + "%s up mcp %u/%u models %u\n" + "q %u/%u req %u ok %u fail ep %u (%u ex)\n" + "%s", + d, j.mcp_ok, j.mcp_total, j.models, + j.queue_inflight, j.queue_max, j.req_ok, + j.req_fail, j.endpoints, j.exhausted, + j.stats[0] != '\0' ? j.stats : + "(no X-Junction headers)"); + } 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); @@ -498,6 +728,29 @@ probe_status(const char *base, const char *path, struct jset *set) 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. + */ +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'; + 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) { @@ -508,7 +761,12 @@ probe(void) 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); @@ -625,6 +883,13 @@ snapshot_text(struct sp_buf *out) 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)"); + sp_buf_printf(out, " route: %s%s\n", j.route[0] != '\0' ? j.route : + "(no rule matched)", j.route[0] != '\0' ? "" : ""); + 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++) { @@ -633,8 +898,9 @@ snapshot_text(struct sp_buf *out) const char *label = src == 0 ? "/mcp" : (src == 1 ? status_path : "/v1/models"); - sp_buf_printf(out, " headers %s (http %ld, %zu):\n", label, - s->http, s->n); + 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); @@ -785,6 +1051,7 @@ junction_init(struct sp_host *h) NULL }; + j.host = h; j.interval_ms = J_DEFAULT_MS; j.models_ms = J_MODELS_MS; resolve_config(h); diff --git a/src/frontend/curses.c b/src/frontend/curses.c index e65df1b..1b8dfd5 100644 --- a/src/frontend/curses.c +++ b/src/frontend/curses.c @@ -1050,28 +1050,51 @@ sidebar_heading(int *y, int cols, const char *text) static void side_text(int *y, int cols, const char *text, int maxrows, int bold_first) { - int rows = 0; + int rows = 0; + const char *p = text; - while (*text != '\0' && rows < maxrows && *y < getmaxy(ui_side)) { - size_t n = strlen(text), take = n; - int bold = bold_first && rows == 0; + /* + * Wrap one line at a time. Passing a multi-line string to a single + * mvwaddnstr() let curses move the cursor on '\n' while the loop also + * counted a row, so a long widget line overwrote the line below it. + * That is why "route kontron! kompute0 kompute1 kompute2" lost its + * tail: the route line and the line after it landed on one row. + */ + while (*p != '\0' && rows < maxrows) { + const char *nl = strchr(p, '\n'); + size_t len = nl != NULL ? (size_t)(nl - p) : strlen(p); + char line[1024]; + char *q; - if (take > (size_t)cols - 1) { - take = (size_t)cols - 1; - while (take > 0 && text[take] != ' ') - take--; - if (take == 0) + if (len >= sizeof(line)) + len = sizeof(line) - 1; + memcpy(line, p, len); + line[len] = '\0'; + q = line; + while (*q != '\0' && rows < maxrows && *y < getmaxy(ui_side)) { + size_t take = strlen(q); + int bold = bold_first && rows == 0; + + if (take > (size_t)cols - 1) { take = (size_t)cols - 1; + while (take > 0 && q[take] != ' ') + take--; + if (take == 0) + take = (size_t)cols - 1; + } + if (bold) + wattron(ui_side, A_BOLD); + mvwaddnstr(ui_side, (*y)++, 0, q, (int)take); + if (bold) + wattroff(ui_side, A_BOLD); + rows++; + q += take; + while (*q == ' ') + q++; } - if (bold) - wattron(ui_side, A_BOLD); - mvwaddnstr(ui_side, (*y)++, 0, text, (int)take); - if (bold) - wattroff(ui_side, A_BOLD); - rows++; - text += take; - while (*text == ' ') - text++; + if (nl == NULL) + break; + p = nl + 1; } } @@ -1211,12 +1234,14 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir, /* * A line without "name:" continues the widget above - * it. sysinfo uses that for one metric per row. + * it. It may still be longer than the column, so it + * wraps over a few rows - one row cut a route like + * "kontron kompute0 kompute1 kompute2" in half. */ if (colon == NULL) { if (cont_name[0] != '\0' && *line != '\0' && y < getmaxy(ui_side) - 2) - side_text(&y, cols, line, 1, 0); + side_text(&y, cols, line, 3, 0); continue; } nl = (size_t)(colon - line); diff --git a/tests/fake_junction.py b/tests/fake_junction.py index 5a77e08..485a26f 100644 --- a/tests/fake_junction.py +++ b/tests/fake_junction.py @@ -17,9 +17,17 @@ STATUS = { "requests": {"ok": 11, "fail": 0}, "queue": {"inflight": 1, "waiting": 0, "max_inflight": 32}, "endpoints": [ - {"name": "a", "exhausted": 0, "p95_ms": 500}, - {"name": "b", "exhausted": 1, "p95_ms": 900}, - {"name": "c", "exhausted": 0, "p95_ms": 300}, + {"name": "a", "probe_ok": 1, "exhausted": 0, "p95_ms": 500}, + {"name": "b", "probe_ok": 0, "exhausted": 1, "p95_ms": 900}, + {"name": "c", "probe_ok": 1, "exhausted": 0, "p95_ms": 300}, + ], + # the route of the test's model ("m"), and a catch-all behind it + "rules": [ + {"line": 1, "model": "m", "path": "", + "targets": "a as /models/x, b as /models/y", "block": False}, + {"line": 2, "model": "", "path": "/v1/models", + "targets": "c", "block": False}, + {"line": 3, "model": "", "path": "", "targets": "c", "block": False}, ], "mcp": [ {"name": "tasksd", "ok": 1, "fail": 0, "tools": 9}, diff --git a/tests/test_junction.c b/tests/test_junction.c index 756c1de..e2da059 100644 --- a/tests/test_junction.c +++ b/tests/test_junction.c @@ -14,6 +14,7 @@ #include "splinter.h" #include "splinter_plugin.h" +#include "config.h" static int failures; @@ -77,6 +78,18 @@ main(int argc, char **argv) host = sp_host_new(®, &ag, NULL); CHECK(host != NULL); + /* + * The route comes from the run's model: /status' rules are matched + * against it, so the plugin must read it from the config, exactly as + * the sidebar names it. + */ + { + static struct sp_config conf; + + sp_config_init(&conf); + snprintf(conf.model, sizeof(conf.model), "m"); + sp_host_set_config(host, &conf); + } CHECK(sp_plugin_load(host, "plugins/junction/junction.so", &err) == 0); /* the loader reports a failed init; a CHECK here would have caught it */ @@ -90,7 +103,11 @@ main(int argc, char **argv) CHECK(strstr(snap, "mcp 4/5") != NULL); /* the alias /v1/status answered, and the model route's headers are shown */ CHECK(strstr(snap, "models 3") != NULL); - CHECK(strstr(snap, "endpoint=fake") != NULL); + /* the session's route, not the probe's: a is up, b is not, hence "b!" */ + CHECK(strstr(snap, "route a b!") != NULL); + CHECK(strstr(snap, "probe-ms=12") != NULL); + /* the probe's endpoint header is not presented as the session's route */ + CHECK(strstr(snap, "endpoint=") == NULL); CHECK(strstr(snap, "1 ex") != NULL); CHECK(!continuation_has_colon(snap)); @@ -117,7 +134,12 @@ main(int argc, char **argv) CHECK(strstr(out.p, "headers /v1/models") != NULL); CHECK(strstr(out.p, "X-Junction-Endpoint: fake") != NULL); CHECK(strstr(out.p, "statistics: ") != NULL); - CHECK(strstr(out.p, "endpoint=fake") != NULL); + CHECK(strstr(out.p, "statistics: endpoint=") == NULL); + CHECK(strstr(out.p, "probe-ms=12") != NULL); + CHECK(strstr(out.p, "model: m") != NULL); + CHECK(strstr(out.p, "route: a b") != NULL); + CHECK(strstr(out.p, "route health: 1/2 up, down: b") != NULL); + CHECK(strstr(out.p, "not the session's") != NULL); CHECK(strstr(out.p, "models: 3") != NULL); /* the tool is registered and returns the same snapshot */