/* * splinter -- the reminder. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * Every tenth turn the rules go back into the conversation, as a system message * right before the newest user message: * * SPLINTER_REMINDER_EVERY turns between reminders, default 10 * * Why not in the system prompt? Because it is read once and then buried under * forty thousand tokens of conversation. A rule that matters every turn has to * come back, or it stops existing. * * The rules are short and factual. A reminder that lectures is a reminder the * model learns to skip. */ #include #include #include #include "splinter.h" #include "splinter_plugin.h" #include "i18n.h" #define REMINDER_MAX 1024 static unsigned every = 10; static unsigned last_reminder = (unsigned)-1; static unsigned sent; static const char *const rules[] = { "Read the documentation before you build: README, AGENTS, man pages, " "the source itself. A hack that works against the documented way is a " "bug with good timing.", "Check what already exists before writing something new.", "Evidence over claims: run it, read the output, say what is verified.", "Never invent output, paths or results.", "Stop and ask before anything destructive or irreversible.", NULL }; static int reminder_compact(struct sp_host *h, struct sp_conv *conv, size_t budget, unsigned turn) { char text[REMINDER_MAX]; struct sp_msg m; size_t at, i, used = 0; (void)budget; if (conv == NULL || conv->n == 0 || every == 0) return 0; if (turn % every != 0 || turn == last_reminder) return 0; last_reminder = turn; used += (size_t)snprintf(text, sizeof(text) - used, "[reminder]\n"); for (i = 0; rules[i] != NULL && used + 4 < sizeof(text); i++) used += (size_t)snprintf(text + used, sizeof(text) - used, "- %s\n", rules[i]); /* before the newest user message, never after it */ 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 = text; if (sp_conv_add(conv, &m) != 0) { (void)sp_host_note(h, "reminder: cannot add the message"); return -1; } /* sp_conv_add appends; move it in front of the user message */ 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; } sent++; return 1; } static const struct sp_compactor compactor = { "reminder", reminder_compact }; static int reminder_init(struct sp_host *h) { const char *env = getenv("SPLINTER_REMINDER_EVERY"); if (env != NULL && *env != '\0') every = (unsigned)strtoul(env, NULL, 10); if (sp_host_add_compactor(h, &compactor) != 0) { (void)sp_host_note(h, "reminder: cannot register (add_compactor)"); return -1; } return 0; } static const struct sp_plugin plugin = { SP_PLUGIN_ABI, "reminder", "1.0", reminder_init, NULL, NULL }; const struct sp_plugin * sp_plugin_describe(void) { return &plugin; }