Files
splinter/tests/test_focus.c
T
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

333 lines
9.8 KiB
C

/*
* splinter -- focus plugin tests.
*
* The rule under test: a reminder every N turns, inserted before the newest
* user message, never twice in the same turn, and nothing at all when no goal
* is configured. The turn counter is the host's, driven here directly.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "i18n.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static int
count_focus(const struct sp_conv *c)
{
size_t i;
int n = 0;
for (i = 0; i < c->n; i++)
if (c->v[i].content != NULL &&
strstr(c->v[i].content, "[focus]") != NULL)
n++;
return n;
}
int
main(void)
{
struct sp_buf ierr;
/* the plugin speaks through the catalogue; load the English one */
if (sp_buf_init(&ierr) == 0) {
sp_i18n_add_dir("share/locale");
(void)sp_i18n_load(&ierr, "en");
sp_buf_free(&ierr);
}
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err;
struct sp_conv conv;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
/* ---- no goal configured: the plugin stays quiet ---- */
unsetenv("SPLINTER_FOCUS_GOAL");
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, "hallo") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 0) == 0);
CHECK(count_focus(&conv) == 0);
sp_conv_free(&conv);
sp_host_free(host);
/* ---- with a goal: one reminder every two turns ---- */
setenv("SPLINTER_FOCUS_GOAL", "Ship the config file", 1);
setenv("SPLINTER_FOCUS_EVERY", "2", 1);
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "system", NULL, "du bist knapp") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "erster turn") == 0);
sp_host_turn(host);
/* the first request carries the goal itself, before the user's line */
CHECK(sp_host_compact(host, &conv, 0) == 1);
CHECK(count_focus(&conv) == 1);
CHECK(strstr(conv.v[conv.n - 1].content, "erster turn") != NULL);
CHECK(strstr(conv.v[conv.n - 2].content, "Ship the config file") != NULL);
CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL);
{
char msg[256];
msg[0] = '\0';
CHECK(sp_host_status_take(host, msg, sizeof(msg)) == 1);
CHECK(strstr(msg, "goal in the prompt") != NULL);
}
/* turn 2: the reminder appears, once, and says so */
CHECK(sp_conv_push(&conv, "user", NULL, "zweiter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 2);
{
char msg[256], snap[1024];
msg[0] = '\0';
CHECK(sp_host_status_take(host, msg, sizeof(msg)) == 1);
CHECK(strstr(msg, "recap applied") != NULL);
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "recap 1, every 2 turns") != NULL);
}
CHECK(strstr(conv.v[conv.n - 1].content, "zweiter turn") != NULL);
{
size_t i;
/* it sits before the newest user message, not after it */
for (i = 0; i < conv.n; i++)
if (conv.v[i].content != NULL &&
strstr(conv.v[i].content, "[focus]") != NULL)
CHECK(i < conv.n - 1);
}
CHECK(strstr(conv.v[1].content, "Ship the config file") != NULL ||
strstr(conv.v[2].content, "Ship the config file") != NULL);
/* compacting again in the same turn must not add a second one */
CHECK(sp_host_compact(host, &conv, 1) == 0);
CHECK(count_focus(&conv) == 2);
/* turn 3: silent */
CHECK(sp_conv_push(&conv, "user", NULL, "dritter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 0);
CHECK(count_focus(&conv) == 2);
/* turn 4: second reminder, with the completion rule in it */
CHECK(sp_conv_push(&conv, "user", NULL, "vierter turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 3);
CHECK(strstr(conv.v[conv.n - 2].content, "GOAL_COMPLETE") != NULL);
/* ---- /goal is the recap feature; /recap is not a command ---- */
{
struct sp_buf out;
size_t i;
int found = 0;
CHECK(sp_buf_init(&out) == 0);
sp_conv_free(&conv);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, "start") == 0);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "", &out) == 0);
CHECK(strstr(out.p, "Ship the config file") != NULL);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "neues ziel", &out) == 0);
CHECK(strstr(out.p, "neues ziel") != NULL);
/* the goal prompt starts a turn; no recap exists yet */
CHECK(sp_conv_push(&conv, "user", NULL,
"Goal: neues ziel. Write a short recap.") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 0);
/* the answer is the recap; the next turn injects it */
CHECK(sp_conv_push(&conv, "assistant", NULL,
"KURZ: neues ziel, erster Schritt") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "weiter") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 1);
for (i = 0; i < conv.n; i++)
if (conv.v[i].content != NULL &&
strstr(conv.v[i].content, "[focus]") != NULL) {
CHECK(strstr(conv.v[i].content, "KURZ") != NULL);
found = 1;
}
CHECK(found);
/* /recap is gone; /goal carries the recap now */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "recap", "", &out) == -1);
CHECK(strstr(out.p, "unknown command") != NULL);
/* an unknown command is refused and says so */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "gibtsnicht", "", &out) == -1);
CHECK(strstr(out.p, "unknown command") != NULL);
/*
* The goal ends with GOAL_COMPLETE. The next compaction sees the
* newest assistant answer, marks the goal succeeded and stops
* reminding; /goal and the sidebar say so.
*/
CHECK(sp_conv_push(&conv, "assistant", NULL,
"alles erledigt\nGOAL_COMPLETE") == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "und jetzt?") == 0);
sp_host_turn(host);
{
int before = count_focus(&conv);
char snap[1024];
CHECK(sp_host_compact(host, &conv, 1) == 0);
CHECK(count_focus(&conv) == before);
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "[done]") != NULL);
CHECK(strstr(snap, "neues ziel") != NULL);
}
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "", &out) == 0);
CHECK(strstr(out.p, "succeeded") != NULL);
/* succeeded is final for this goal: no more reminders */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "", &out) == 0);
CHECK(strstr(out.p, "succeeded") != NULL);
CHECK(sp_conv_push(&conv, "user", NULL, "noch ein turn") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 0);
/* a new goal opens a new one, and is announced again */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "goal", "zweites ziel", &out) == 0);
CHECK(sp_conv_push(&conv, "user", NULL, "los") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &conv, 1) == 1);
{
char snap[1024];
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "[done]") == NULL);
CHECK(strstr(snap, "zweites ziel") != NULL);
}
sp_buf_free(&out);
}
sp_conv_free(&conv);
sp_host_free(host);
/*
* ---- the tick reads the answer from the session log ----
*
* The compactor sees the conversation; a turn that ends has no next
* compactor call. The tick watches the session file, so the goal turns
* "[done]" without waiting for another turn.
*/
{
static struct sp_session_info si;
char dir[] = "/tmp/opencode/focus-XXXXXX";
char snap[1024], st[16];
struct sp_conv c2;
CHECK(mkdtemp(dir) != NULL);
snprintf(si.dir, sizeof(si.dir), "%s", dir);
snprintf(si.id, sizeof(si.id), "sess-goaltest");
CHECK(sp_session_append(&si, "user", "start", &err) == 0);
CHECK(sp_session_set_goal(dir, si.id, "tick goal", &err) == 0);
host = sp_host_new(&reg, &ag, &si);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
sp_conv_init(&c2);
CHECK(sp_conv_push(&c2, "user", NULL, "erste anfrage") == 0);
sp_host_turn(host);
CHECK(sp_host_compact(host, &c2, 0) == 1); /* announced, armed */
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "tick goal") != NULL);
CHECK(strstr(snap, "[done]") == NULL);
/* the model answers after the goal went into the prompt */
{
struct timespec ts = { 1, 100 * 1000 * 1000L };
nanosleep(&ts, NULL);
}
CHECK(sp_session_append(&si, "assistant",
"fertig\nGOAL_COMPLETE", &err) == 0);
{
struct timespec ts = { 1, 600 * 1000 * 1000L };
nanosleep(&ts, NULL); /* let the tick run */
}
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "[done]") != NULL);
CHECK(sp_session_get_goal_state(dir, si.id, st, sizeof(st)) == 0);
CHECK(strcmp(st, "succeeded") == 0);
/* and the state survives a fresh host: the meta is the truth */
sp_conv_free(&c2);
sp_host_free(host);
host = sp_host_new(&reg, &ag, &si);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/focus/focus.so", &err) == 0);
{
struct sp_buf out;
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_host_command(host, "goal", "", &out) == 0);
CHECK(strstr(out.p, "succeeded") != NULL);
sp_buf_free(&out);
}
sp_host_free(host);
{
char path[700];
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, si.id);
(void)unlink(path);
snprintf(path, sizeof(path), "%s/%s.meta", dir, si.id);
(void)unlink(path);
}
(void)rmdir(dir);
}
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_focus: ok\n");
return failures == 0 ? 0 : 1;
}