diff --git a/Makefile b/Makefile index ad34e77..c5062f7 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ CLIENT = splinterctl CLIENTOBJ = src/frontend/client.o PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so \ - plugins/ring/ring.so plugins/reminder/reminder.so \ + plugins/ring/ring.so plugins/reminder/reminder.so plugins/recap/recap.so \ plugins/kids/kids.so plugins/status/status.so plugins/processes/processes.so \ plugins/provider/provider.so \ plugins/scheduler/scheduler.so \ @@ -67,7 +67,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ plugins/foreign/foreign.so TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \ tests/test_transport tests/test_agent tests/test_openai \ - tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_corecompact tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self tests/test_fuzz \ + tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_recap tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_corecompact tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self tests/test_fuzz \ tests/test_sessiontools tests/test_embed tests/test_foreign \ tests/test_foreignopen tests/test_remote_list @@ -154,6 +154,9 @@ plugins/status/status.so: plugins/status/status.c $(LIB) plugins/reminder/reminder.so: plugins/reminder/reminder.c $(LIB) $(CC) $(CFLAGS) -shared -o $@ plugins/reminder/reminder.c $(SPLLIB) \ $(PLUGIN_RPATH) $(LIBS) +plugins/recap/recap.so: plugins/recap/recap.c $(LIB) + $(CC) $(CFLAGS) -shared -o $@ plugins/recap/recap.c $(SPLLIB) \ + $(PLUGIN_RPATH) $(LIBS) plugins/mcp/mcp.so: plugins/mcp/mcp.c $(LIB) $(CC) $(CFLAGS) -shared -o $@ plugins/mcp/mcp.c $(SPLLIB) \ $(PLUGIN_RPATH) $(LIBS) @@ -251,6 +254,8 @@ tests/test_openai: tests/test_openai.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_openai.c $(SPLLIB) $(TEST_RPATH) $(LIBS) tests/test_theme: tests/test_theme.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_theme.c $(SPLLIB) $(TEST_RPATH) $(LIBS) +tests/test_recap: tests/test_recap.c $(LIB) $(PLUGIN) + $(CC) $(CFLAGS) -o $@ tests/test_recap.c $(SPLLIB) $(TEST_RPATH) $(LIBS) tests/test_focus: tests/test_focus.c $(LIB) $(PLUGIN) $(CC) $(CFLAGS) -o $@ tests/test_focus.c $(SPLLIB) $(TEST_RPATH) $(LIBS) tests/test_skills: tests/test_skills.c $(LIB) diff --git a/docs/CONFIG.md b/docs/CONFIG.md index 13fe57d..18d5389 100644 --- a/docs/CONFIG.md +++ b/docs/CONFIG.md @@ -32,6 +32,7 @@ core { } plugin lcm { } # one block per plugin, see "Plugins" +plugin recap { every = 5 } # /recap , repeated every 5 turns plugin cli { } telegram { allow = [123456789] } diff --git a/docs/PLUGINS.md b/docs/PLUGINS.md index 7f14f80..cd23008 100644 --- a/docs/PLUGINS.md +++ b/docs/PLUGINS.md @@ -279,6 +279,26 @@ never printed twice in one turn. It is a reminder, not an instruction: the user can change direction at any time, and the message says so on its face. +## recap + +`/recap ` sets a note that the plugin puts back into the conversation: +on the next request, then every N turns (default 5), as a real system message +right before the newest user message. It stays in the history, so a long +session keeps meeting it. + +``` +/recap set it (up to 1500 bytes) +/recap show it, the interval and how often it was sent +/recap every N change the interval; 0 = only once, no repeats +/recap off forget it +``` + +The interval is `plugin recap { every = 5 }`, `SPLINTER_RECAP_EVERY`, or +`/recap every N` for the running process. The text is stored per session in +`/.recap` and comes back on resume. It is not `/goal`: a goal +asks the model for its own recap and can end; a recap is the operator's words, +verbatim, until `off`. + ## junction The fleet router, evaluated from the outside. junction answers `/mcp` with diff --git a/pkgsrc/PLIST b/pkgsrc/PLIST index da4c12a..f54672e 100644 --- a/pkgsrc/PLIST +++ b/pkgsrc/PLIST @@ -15,6 +15,7 @@ lib/splinter/plugins/mcp.so lib/splinter/plugins/processes.so lib/splinter/plugins/provider.so lib/splinter/plugins/reminder.so +lib/splinter/plugins/recap.so lib/splinter/plugins/ring.so lib/splinter/plugins/scheduler.so lib/splinter/plugins/self.so diff --git a/plugins/focus/focus.c b/plugins/focus/focus.c index a43e2c7..6c9c623 100644 --- a/plugins/focus/focus.c +++ b/plugins/focus/focus.c @@ -12,8 +12,8 @@ * * `/goal ` sets the goal. The engine starts a turn with it, asking the * model for a short recap; the plugin remembers the answer and injects that - * recap every seventh turn. There is no separate /recap command: recapping is - * what a goal does. + * recap every seventh turn. /recap (plugins/recap) is separate: it repeats the + * operator's own text, not the model's recap of a goal. * * The goal is also the first thing the prompt carries. A session that starts * with a goal (config, resume, planner) gets one system message on its first diff --git a/plugins/recap/recap.c b/plugins/recap/recap.c new file mode 100644 index 0000000..d2ec44c --- /dev/null +++ b/plugins/recap/recap.c @@ -0,0 +1,228 @@ +/* + * splinter -- the recap. + * + * Copyright (c) 2026 Konrad Lother + * SPDX-License-Identifier: BSD-3-Clause + * + * /recap set the recap; it goes into the conversation on the + * next request and then every N turns + * /recap show it + * /recap every N change the interval (0 = never repeat) + * /recap off forget it + * + * The recap is the operator's own text, put into the conversation as a system + * message right before the newest user message. It is a real message: it stays + * in the history, so a long session keeps meeting it and not only the + * system prompt that sank under the conversation. + * + * Interval: plugin recap { every = 5 }, SPLINTER_RECAP_EVERY, or /recap every. + * Default 5. The recap is kept per session in /.recap and + * comes back on resume. + */ +#include +#include +#include +#include + +#include "splinter.h" +#include "splinter_plugin.h" + +#define RECAP_MAX 1500 +#define EVERY_MAX 1000 + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static char text[RECAP_MAX + 1]; +static unsigned every = 5; +static unsigned last_turn = (unsigned)-1; +static int fresh; /* set, and not yet in the conversation */ +static unsigned sent; +static char session_id[64]; + +static int +recap_path(struct sp_host *h, char *path, size_t n) +{ + const char *dir = sp_host_session_dir(h); + const char *id = sp_host_session_id(h); + + if (dir == NULL || id == NULL || *id == '\0' || strchr(id, '/') != NULL) + return -1; + return snprintf(path, n, "%s/%s.recap", dir, id) < (int)n ? 0 : -1; +} + +static void +recap_save(struct sp_host *h) +{ + char path[1024]; + FILE *f; + + if (recap_path(h, path, sizeof(path)) != 0) + return; + if (text[0] == '\0') { + (void)remove(path); + return; + } + if ((f = fopen(path, "w")) == NULL) + return; + (void)fputs(text, f); + (void)fclose(f); +} + +/* The statics serve every session; reload when the session changes. */ +static void +recap_sync(struct sp_host *h) +{ + const char *id = sp_host_session_id(h); + char path[1024]; + FILE *f; + size_t n; + + if (id == NULL || *id == '\0' || strcmp(session_id, id) == 0) + return; + snprintf(session_id, sizeof(session_id), "%s", id); + text[0] = '\0'; + fresh = 0; + last_turn = (unsigned)-1; + if (recap_path(h, path, sizeof(path)) != 0 || + (f = fopen(path, "r")) == NULL) + return; + n = fread(text, 1, RECAP_MAX, f); + text[n] = '\0'; + (void)fclose(f); +} + +static int +recap_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, + unsigned turn) +{ + char msg[RECAP_MAX + 64]; + struct sp_msg m; + size_t at, i; + int due; + + (void)budget; + pthread_mutex_lock(&lock); + recap_sync(h); + if (conv == NULL || conv->n == 0 || text[0] == '\0') { + pthread_mutex_unlock(&lock); + return 0; + } + due = fresh || (every != 0 && turn != 0 && turn % every == 0); + if (!due || turn == last_turn) { + pthread_mutex_unlock(&lock); + return 0; + } + last_turn = turn; + fresh = 0; + snprintf(msg, sizeof(msg), "[recap]\n%s\n", text); + pthread_mutex_unlock(&lock); + + at = conv->n; + for (i = conv->n; i > 0; i--) { + if (conv->v[i - 1].role != NULL && + strcmp(conv->v[i - 1].role, "user") == 0) { + at = i - 1; + break; + } + } + memset(&m, 0, sizeof(m)); + m.role = "system"; + m.content = msg; + if (sp_conv_add(conv, &m) != 0) { + (void)sp_host_note(h, "recap: cannot add the message"); + return -1; + } + if (at < conv->n - 1) { + struct sp_msg moved = conv->v[conv->n - 1]; + + memmove(&conv->v[at + 1], &conv->v[at], + sizeof(conv->v[0]) * (conv->n - 1 - at)); + conv->v[at] = moved; + } + pthread_mutex_lock(&lock); + sent++; + pthread_mutex_unlock(&lock); + return 1; +} + +static int +recap_command(struct sp_host *h, const char *args, struct sp_buf *out, + void *ctx) +{ + int rc; + + (void)ctx; + pthread_mutex_lock(&lock); + recap_sync(h); + while (args != NULL && (*args == ' ' || *args == '\t')) + args++; + if (args == NULL || *args == '\0') { + if (text[0] == '\0') + rc = sp_buf_puts(out, "no recap set (/recap )"); + else + rc = sp_buf_printf(out, + "recap: %s (every %u turns, %u sent)", text, every, + sent); + } else if (strcmp(args, "off") == 0) { + text[0] = '\0'; + fresh = 0; + recap_save(h); + rc = sp_buf_puts(out, "recap removed"); + } else if (strncmp(args, "every ", 6) == 0) { + char *end; + unsigned long v = strtoul(args + 6, &end, 10); + + while (*end == ' ') + end++; + if (end == args + 6 || *end != '\0' || v > EVERY_MAX) + rc = sp_buf_printf(out, "every: 0..%d", EVERY_MAX); + else { + every = (unsigned)v; + rc = sp_buf_printf(out, "recap every %u turns", every); + } + } else { + snprintf(text, sizeof(text), "%.*s", RECAP_MAX, args); + fresh = 1; + sent = 0; + recap_save(h); + rc = sp_buf_printf(out, "recap set (every %u turns)", every); + } + pthread_mutex_unlock(&lock); + return rc; +} + +static const struct sp_compactor compactor = { "recap", recap_compact }; + +static int +recap_init(struct sp_host *h) +{ + const char *v = sp_host_plugin_config(h, "recap", "every"); + const char *env = getenv("SPLINTER_RECAP_EVERY"); + + if ((v == NULL || *v == '\0') && env != NULL) + v = env; + text[0] = '\0'; + session_id[0] = '\0'; + every = 5; + if (v != NULL && *v != '\0') { + unsigned long n = strtoul(v, NULL, 10); + + if (n <= EVERY_MAX) + every = (unsigned)n; + } + if (sp_host_add_compactor(h, &compactor) != 0 || + sp_host_add_command(h, "recap", recap_command, NULL) != 0) { + (void)sp_host_note(h, "recap: cannot register"); + return -1; + } + return 0; +} + +static const struct sp_plugin plugin = { + SP_PLUGIN_ABI, "recap", "1.0", recap_init, NULL, NULL +}; + +const struct sp_plugin * +sp_plugin_describe(void) +{ + return &plugin; +} diff --git a/tests/test_recap.c b/tests/test_recap.c new file mode 100644 index 0000000..99b61f0 --- /dev/null +++ b/tests/test_recap.c @@ -0,0 +1,134 @@ +/* + * splinter -- recap plugin tests. + * + * /recap sets a recap; it enters the conversation on the next request + * and then every N turns, before the newest user message, once per turn. + */ +#include +#include +#include + +#include "splinter.h" +#include "splinter_plugin.h" + +static int failures; + +#define CHECK(cond) do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \ + failures++; \ + } \ +} while (0) + +#define NO_PRESSURE ((size_t)-1) + +static int +count(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, "[recap]") != NULL) + n++; + return n; +} + +static int +turn(struct sp_host *h, struct sp_conv *c, const char *user) +{ + CHECK(sp_conv_push(c, "user", NULL, user) == 0); + sp_host_turn(h); + return sp_host_compact(h, c, NO_PRESSURE); +} + +static int +cmd(struct sp_host *h, const char *args, char *reply, size_t n) +{ + struct sp_buf b; + int rc; + + if (sp_buf_init(&b) != 0) + return -1; + rc = sp_host_command(h, "recap", args, &b); + snprintf(reply, n, "%s", b.p != NULL ? b.p : ""); + sp_buf_free(&b); + return rc; +} + +int +main(void) +{ + struct sp_registry reg; + struct sp_agent ag; + struct sp_host *host; + struct sp_buf err; + struct sp_conv conv; + char reply[512]; + int i; + + memset(®, 0, sizeof(reg)); + memset(&ag, 0, sizeof(ag)); + if (sp_buf_init(&err) != 0) + return 1; + setenv("SPLINTER_RECAP_EVERY", "3", 1); + host = sp_host_new(®, &ag, NULL); + CHECK(host != NULL); + CHECK(sp_plugin_load(host, "plugins/recap/recap.so", &err) == 0); + + /* nothing set: quiet */ + sp_conv_init(&conv); + CHECK(turn(host, &conv, "eins") == 0); + CHECK(count(&conv) == 0); + CHECK(cmd(host, "", reply, sizeof(reply)) == 0); + CHECK(strstr(reply, "no recap") != NULL); + + /* set: next request carries it, before the user's line */ + CHECK(cmd(host, "Ship the parser, nothing else", reply, sizeof(reply)) == 0); + CHECK(strstr(reply, "every 3") != NULL); + CHECK(turn(host, &conv, "zwei") == 1); + CHECK(count(&conv) == 1); + CHECK(strstr(conv.v[conv.n - 1].content, "zwei") != NULL); + CHECK(strstr(conv.v[conv.n - 2].content, "Ship the parser") != NULL); + CHECK(strcmp(conv.v[conv.n - 2].role, "system") == 0); + /* same turn again: no second copy */ + CHECK(sp_host_compact(host, &conv, NO_PRESSURE) == 0); + CHECK(count(&conv) == 1); + + /* the next turns until the interval; the host counts from its own turn */ + for (i = 0; i < 12; i++) + (void)turn(host, &conv, "weiter"); + CHECK(count(&conv) >= 4 && count(&conv) <= 6); + + /* interval 0: no repeats after the first */ + CHECK(cmd(host, "every 0", reply, sizeof(reply)) == 0); + CHECK(cmd(host, "Neu", reply, sizeof(reply)) == 0); + { + int before = count(&conv); + + (void)turn(host, &conv, "a"); + CHECK(count(&conv) == before + 1); + for (i = 0; i < 9; i++) + (void)turn(host, &conv, "b"); + CHECK(count(&conv) == before + 1); + } + CHECK(cmd(host, "every x", reply, sizeof(reply)) == 0); + CHECK(strstr(reply, "every:") != NULL); + + /* off */ + CHECK(cmd(host, "off", reply, sizeof(reply)) == 0); + { + int before = count(&conv); + + for (i = 0; i < 6; i++) + (void)turn(host, &conv, "c"); + CHECK(count(&conv) == before); + } + sp_conv_free(&conv); + sp_host_free(host); + sp_buf_free(&err); + if (failures == 0) + puts("recap: ok"); + return failures != 0; +}