Files
lotherk 36a66ab4f0 self monitor, MCP session tools, junction serving endpoint
- plugins/self: what the session costs the machine - RSS, CPU, threads, fds
  and io of the process itself from /proc/self/*, and the same (count, RSS,
  CPU) for every process whose parent chain reaches it: run calls, MCP
  children, delegates. Widget, /self and the session_usage tool; interval in
  the plugin block (default 2 s).
- MCP server: the session tools. splinter_sessions, splinter_state,
  splinter_models, splinter_agents, splinter_history, splinter_say,
  splinter_open, splinter_model, splinter_agent, splinter_steer,
  splinter_cancel, splinter_approve. Say starts a real turn and returns the
  answer; a turn parked on an approval returns the question instead of
  failing. Mutating tools obey --autonomous. `--daemon --mcp-server-listen`
  runs the MCP server on a thread beside the Telegram daemon.
- the MCP server asks the engine for its registry on every call (a session
  switch replaces it) and copies the tool name out of the parsed JSON before
  freeing it (the first version compared a dangling pointer and answered
  every session call with empty text).
- junction: "serving <endpoint>" - streaming answers carry no
  X-Junction-Endpoint, so the plugin compares the route targets' ok counters
  from /status between samples and names the one that grew, with its
  last_ms. The header is still used when present (non-streaming). The
  X-Junction-Endpoint of the plugin's own /v1/models probe remains a probe
  fact in /junction only.
- transport: X-Junction-Endpoint is parsed from the response headers in both
  the streaming and the non-streaming path and published through
  sp_host_served_by(); sp_reply carries served_by.
- the curl rate guard (--speed-limit/--speed-time) is gone: it cut slow
  router streams with "Less than 1 bytes/sec" even while keepalives arrived.
  The overall -m timeout stays.
- focus: the sidebar shows "recap N, every M turns", and an applied recap
  writes "recap applied (turn N, every M turns)" into the transcript (host
  status messages are now drained during a turn; protocol clients get them
  as entry events too). The initial goal announcement says so as well.
- tests: test_self, test_mcpsession.sh (real socket), test_focus status
  messages, test_junction serving placeholder.

make check: all tests passed.
2026-11-17 19:13:59 +00:00

157 lines
4.5 KiB
C

/*
* splinter -- junction plugin tests.
*
* The plugin is loaded for real and talks HTTP to tests/fake_junction.py. What
* is proven: the X-Junction-Mcp header is what makes the endpoint "junction",
* the counters come from /status (not from the plugin counting), the sidebar
* text carries no colon in a continuation line (the sidebar would read it as a
* new widget), and neither the tool nor /junction prints the key.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
nap_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
/* the sidebar reads "name: text"; line 2 and later must not look like a name */
static int
continuation_has_colon(const char *text)
{
const char *p = strchr(text, '\n');
while (p != NULL && *p != '\0') {
const char *start = p + 1;
const char *nl = strchr(start, '\n');
p = nl;
if (strchr(start, ':') != NULL &&
(nl == NULL || strchr(start, ':') < nl))
return 1;
}
return 0;
}
int
main(int argc, char **argv)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err, out;
char snap[4096], url[256];
if (argc < 2) {
fprintf(stderr, "usage: %s PORT\n", argv[0]);
return 1;
}
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0)
return 1;
snprintf(url, sizeof(url), "http://127.0.0.1:%s/mcp", argv[1]);
setenv("SPLINTER_JUNCTION_URL", url, 1);
unsetenv("SPLINTER_JUNCTION_TOKEN");
unsetenv("JUNCTION_TOKEN");
host = sp_host_new(&reg, &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 */
/* the first tick runs immediately, the HTTP round trips take a moment */
nap_ms(2500);
snap[0] = '\0';
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
CHECK(strstr(snap, "junction: ") != NULL);
CHECK(strstr(snap, "up") != NULL);
/*
* The widget is only the model and its route: a is up, b is not (hence
* "b!"), and the counters are not repeated here - they are /junction's
* business.
*/
CHECK(strstr(snap, "m\nserving (no request yet)\nroute a\nb!\n1/2 up") != NULL);
CHECK(strstr(snap, "mcp 4/5") == NULL);
CHECK(strstr(snap, "models 3") == NULL);
CHECK(strstr(snap, "endpoint=") == NULL);
CHECK(!continuation_has_colon(snap));
/* the status segment the status line and /status show */
{
char buf[160];
buf[0] = '\0';
(void)sp_host_status_text(host, buf, sizeof(buf));
CHECK(strstr(buf, "junction") != NULL);
CHECK(strstr(buf, "up") != NULL);
}
/* /junction: every field, no key */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "junction", "", &out) == 0);
CHECK(strstr(out.p, "junction: up") != NULL);
CHECK(strstr(out.p, "mcp: 4/5 answering") != NULL);
CHECK(strstr(out.p, "header: no") != NULL); /* no token was set */
CHECK(strstr(out.p, "Authorization") == NULL);
/* every header of every probe, and the status alias that answered */
CHECK(strstr(out.p, "/v1/status 200") != NULL);
CHECK(strstr(out.p, "headers /mcp") != NULL);
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, "statistics: endpoint=") == NULL);
CHECK(strstr(out.p, "probe-ms=12") != NULL);
CHECK(strstr(out.p, "model: m") != NULL);
CHECK(strstr(out.p, "serving: (no request yet)") != 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 */
CHECK(sp_registry_find(&reg, "junction_status") != NULL);
sp_host_free(host);
sp_buf_free(&err);
sp_buf_free(&out);
sp_registry_free(&reg);
if (failures == 0)
printf("test_junction: ok\n");
return failures == 0 ? 0 : 1;
}