env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
6.3 KiB
C
219 lines
6.3 KiB
C
/*
|
|
* splinter -- focus: keep the goal in front of the model.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Long conversations drift. Ten turns in, the model is deep in a detail and the
|
|
* thing the session was started for is somewhere behind it. This plugin puts
|
|
* the goal back on the table every five turns - not as a new instruction that
|
|
* overrides the user, but as one system message right before the turn, so it
|
|
* can be read as a reminder and dismissed if the user has changed direction.
|
|
*
|
|
* The goal comes from the operator, never from the model:
|
|
*
|
|
* SPLINTER_FOCUS_GOAL text, exported from the config key `goal`
|
|
* SPLINTER_FOCUS_EVERY turns between reminders, default 5
|
|
*
|
|
* A goal longer than 1000 characters is truncated with a marker: a reminder
|
|
* that eats the budget it is meant to protect would be self-defeating.
|
|
*
|
|
* Position matters. The reminder is inserted before the newest user message,
|
|
* not appended after it: the model then reads goal -> request and answers the
|
|
* request, instead of reading the request and treating the goal as a reply.
|
|
*/
|
|
#include <pthread.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
#include "i18n.h"
|
|
|
|
#define GOAL_MAX 1000
|
|
|
|
/*
|
|
* goal, force and reminders are shared by three threads: /goal and /recap run
|
|
* on the UI thread, the compactor on the turn thread, the tick on its own.
|
|
* One lock for all of them; the tick used to read the goal while /goal was
|
|
* rewriting it.
|
|
*/
|
|
static pthread_mutex_t focus_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
static char goal[GOAL_MAX + 1];
|
|
static unsigned every = 5;
|
|
static unsigned last_reminder;
|
|
static int force; /* /recap: remind on the next turn */
|
|
static unsigned reminders;
|
|
static char widget_text[128];
|
|
|
|
static int
|
|
focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
|
|
unsigned turn)
|
|
{
|
|
char text[GOAL_MAX + 512];
|
|
struct sp_msg m;
|
|
size_t at;
|
|
|
|
char g[GOAL_MAX + 1];
|
|
|
|
(void)budget;
|
|
(void)h;
|
|
if (conv == NULL || conv->n == 0)
|
|
return 0;
|
|
pthread_mutex_lock(&focus_lock);
|
|
if (goal[0] == '\0' || (!force && (turn == 0 || (turn % every) != 0)) ||
|
|
turn == last_reminder) { /* one reminder per turn, always */
|
|
pthread_mutex_unlock(&focus_lock);
|
|
return 0;
|
|
}
|
|
snprintf(g, sizeof(g), "%s", goal);
|
|
pthread_mutex_unlock(&focus_lock);
|
|
|
|
{
|
|
char rem[1024];
|
|
|
|
snprintf(rem, sizeof(rem), sp_td("focus.reminder",
|
|
"Before answering, check whether the last step moved closer "
|
|
"to the goal. If not, say so and name the next step. Goal: %s"), g);
|
|
snprintf(text, sizeof(text), "[focus] %s", rem);
|
|
}
|
|
|
|
memset(&m, 0, sizeof(m));
|
|
m.role = "system";
|
|
m.content = text;
|
|
|
|
/* before the newest message, so the goal is read first */
|
|
at = conv->n - 1;
|
|
while (at > 0 && conv->v[at].role != NULL &&
|
|
strcmp(conv->v[at].role, "user") != 0)
|
|
at--;
|
|
if (sp_conv_insert(conv, at, &m) != 0)
|
|
return -1;
|
|
|
|
pthread_mutex_lock(&focus_lock);
|
|
last_reminder = turn;
|
|
force = 0;
|
|
reminders++;
|
|
snprintf(widget_text, sizeof(widget_text), "%u reminders, every %u turns",
|
|
reminders, every);
|
|
pthread_mutex_unlock(&focus_lock);
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
* /goal <text> set the goal at runtime, effective from the next request
|
|
* /goal show what it is now
|
|
* /recap force the reminder into the next turn
|
|
*/
|
|
static int
|
|
focus_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
|
void *ctx)
|
|
{
|
|
char g[GOAL_MAX + 1];
|
|
unsigned n;
|
|
|
|
(void)ctx;
|
|
pthread_mutex_lock(&focus_lock);
|
|
if (args == NULL || *args == '\0') {
|
|
snprintf(g, sizeof(g), "%s", goal);
|
|
n = reminders;
|
|
pthread_mutex_unlock(&focus_lock);
|
|
if (g[0] == '\0')
|
|
return sp_buf_puts(out, sp_td("focus.none", "no goal set (/goal <text>)"));
|
|
return sp_buf_printf(out, sp_td("focus.status",
|
|
"goal: %s (%u reminders, every %u turns)"), g, n, every);
|
|
}
|
|
snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, args);
|
|
if (strlen(args) > GOAL_MAX)
|
|
snprintf(goal + GOAL_MAX - 24, 25, "...[gekuerzt]");
|
|
reminders = 0;
|
|
last_reminder = 0;
|
|
force = 0;
|
|
snprintf(g, sizeof(g), "%s", goal);
|
|
pthread_mutex_unlock(&focus_lock);
|
|
/* publish now, so the sidebar does not wait for the next tick */
|
|
{
|
|
char line[192];
|
|
|
|
snprintf(line, sizeof(line), "%.60s%s", g,
|
|
strlen(g) > 60 ? "..." : "");
|
|
(void)sp_host_widget_text(h, "focus", line);
|
|
}
|
|
return sp_buf_printf(out, sp_td("focus.set", "goal set: %s"), g);
|
|
}
|
|
|
|
static int
|
|
recap_command(struct sp_host *h, const char *args, struct sp_buf *out,
|
|
void *ctx)
|
|
{
|
|
(void)h;
|
|
(void)args;
|
|
(void)ctx;
|
|
pthread_mutex_lock(&focus_lock);
|
|
if (goal[0] == '\0') {
|
|
pthread_mutex_unlock(&focus_lock);
|
|
return sp_buf_puts(out, sp_td("focus.none", "no goal set (/goal <text>)"));
|
|
}
|
|
force = 1; /* the next turn gets a reminder */
|
|
pthread_mutex_unlock(&focus_lock);
|
|
return sp_buf_puts(out, sp_td("focus.recap",
|
|
"reminder scheduled for the next turn"));
|
|
}
|
|
|
|
static int
|
|
focus_tick(struct sp_host *h, const char *name, void *ctx)
|
|
{
|
|
char line[192];
|
|
|
|
(void)ctx;
|
|
pthread_mutex_lock(&focus_lock);
|
|
if (goal[0] == '\0')
|
|
snprintf(line, sizeof(line), "%s", sp_td("focus.widget_none",
|
|
"no goal (/goal <text>)"));
|
|
else
|
|
snprintf(line, sizeof(line), "%.44s%s | %u",
|
|
goal, strlen(goal) > 44 ? "..." : "", reminders);
|
|
pthread_mutex_unlock(&focus_lock);
|
|
return sp_host_widget_text(h, name, line);
|
|
}
|
|
|
|
static const struct sp_compactor focus = { "focus", focus_compact };
|
|
|
|
static int
|
|
focus_init(struct sp_host *h)
|
|
{
|
|
static const struct sp_widget w = { "focus", focus_tick, NULL, NULL, 1000 };
|
|
const char *g = getenv("SPLINTER_FOCUS_GOAL");
|
|
const char *e = getenv("SPLINTER_FOCUS_EVERY");
|
|
|
|
memset(goal, 0, sizeof(goal));
|
|
if (g != NULL) {
|
|
snprintf(goal, sizeof(goal), "%.*s", GOAL_MAX, g);
|
|
if (strlen(g) > GOAL_MAX)
|
|
snprintf(goal + GOAL_MAX - 24, 25, "...[gekuerzt]");
|
|
}
|
|
if (e != NULL && atoi(e) > 0 && atoi(e) < 1000)
|
|
every = (unsigned)atoi(e);
|
|
|
|
if (sp_host_add_compactor(h, &focus) != 0)
|
|
return -1;
|
|
if (sp_host_add_command(h, "goal", focus_command, NULL) != 0)
|
|
return -1;
|
|
if (sp_host_add_command(h, "recap", recap_command, NULL) != 0)
|
|
return -1;
|
|
if (sp_host_add_widget(h, &w) != 0)
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
static const struct sp_plugin focus_plugin = {
|
|
SP_PLUGIN_ABI, "focus", "1.0.0", focus_init, NULL, NULL
|
|
};
|
|
|
|
const struct sp_plugin *
|
|
sp_plugin_describe(void)
|
|
{
|
|
return &focus_plugin;
|
|
}
|