splinter: /goal as a command, and widgets that are actually alive

The goal is not configuration: it belongs to the conversation in front of you.
So it moved out of the config file and became a command.

- commands are an ABI now: sp_host_add_command / sp_host_command, so the UI
  never needs to know a plugin by name. A leading '/' is never sent to the
  model; an unknown command lists what exists.
- works in both modes: '/goal <text>' in the TUI and
  'splinter -p "/goal ..."' (exit 0 on success, 2 on an unknown command).
- focus registers /goal (set, show when empty of arguments) and /recap (force
  a reminder into the next turn, regardless of the five-turn cadence).

Fixed while testing it: the UI only repainted on a keypress, so a plugin widget
that ticks was frozen on screen - the clock showed the time from when the window
opened. The input read now times out after 500 ms and that timeout repaints the
sidebar and status line: clock, focus and cps are alive while you do nothing.

Fifteen test binaries green; test_focus covers the command path too: set, show,
force a recap outside the cadence, refuse an unknown command.
This commit is contained in:
Konrad committed 2026-11-07 20:50:58 +00:00
1 parent 5453b0a85a
commit 8fa3e9d7bb
15 files changed
+276 -17

No files matched your search

+8 -1
View File
@@ -146,7 +146,14 @@ instead of being dropped. Curses is only ever touched by the UI thread.
`focus` keeps the session's goal in front of the model every five turns, the
way Claude Code's recap does — one inserted system message, never twice in a
turn, nothing when no goal is configured.
turn, nothing until a goal is set. `/goal <text>` sets it, `/recap` forces a
reminder, both in the TUI and with `-p`.
## Slash commands
A leading `/` never reaches the model: the UI asks the host, the host asks the
plugins. A plugin registers what it handles (`/goal`, `/recap` today); an
unknown command lists what exists instead of being sent as a prompt.
Compaction is a plugin, not a switch in the loop. Two ship here: `lcm`
(deterministic front-trim with an in-band system message saying what went) and
+6 -1
View File
@@ -14,6 +14,12 @@ Format: one `key = value` per line, `#` starts a comment, blank lines are
ignored. Not JSON, on purpose — this file gets edited by hand, and a missing
comma must not be the reason the harness will not start.
## Not in the file: the goal
The session goal is set at runtime with `/goal <text>` — in the TUI, or in a
one-shot run (`splinter -p "/goal ship the config"`). It belongs to the
conversation in front of you, not to a file that outlives it.
## Precedence
Command line, then the file, then the built-in default. An unknown key is an
@@ -36,7 +42,6 @@ lies about what it does.
| `max_bytes` | conversation budget in bytes, `0` = no limit |
| `session_dir` | where session logs go (default `~/.splinter`) |
| `system_prompt` | sent once at the start of a conversation |
| `goal` | what this session is for; exported as `SPLINTER_FOCUS_GOAL`, repeated by the `focus` plugin every five turns |
## Example: local model
+5 -2
View File
@@ -170,10 +170,13 @@ reason the session exists is somewhere behind it. `focus` puts the goal back on
the table.
```
goal = splinter zu einem agent-harness machen, das hermes ersetzt
/goal splinter zu einem agent-harness machen, das hermes ersetzt
```
The config key `goal` is exported to plugins as `SPLINTER_FOCUS_GOAL`;
`/goal <text>` sets it, `/goal` shows it, `/recap` forces a reminder into the
next turn. That works in the TUI and in a one-shot run
(`splinter -p "/goal ..."`); it is deliberately not a config key, because a goal
belongs to the conversation in front of you and not to a file that outlives it.
`SPLINTER_FOCUS_EVERY` sets the interval (default 5 turns).
It registers as a compactor and, on every fifth turn, inserts one system
+15
View File
@@ -54,6 +54,16 @@ struct sp_compactor {
size_t budget_bytes, unsigned turn);
};
/*
* A command is what the operator types with a leading slash: /goal <text>.
* The UI does not know any command by name; it asks the host, and the host asks
* the plugins. A plugin that wants one registers it here.
*
* Return 0 and put the reply in `out`; return -1 to say "not mine".
*/
typedef int (*sp_cmd_fn)(struct sp_host *, const char *args, struct sp_buf *out,
void *ctx);
struct sp_plugin {
uint32_t abi; /* must equal SP_PLUGIN_ABI */
const char *name;
@@ -76,6 +86,11 @@ int sp_host_widget_text(struct sp_host *, const char *widget,
const char *text);
int sp_host_note(struct sp_host *, const char *text);
int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *);
int sp_host_add_command(struct sp_host *, const char *name, sp_cmd_fn,
void *ctx);
int sp_host_command(struct sp_host *, const char *name, const char *args,
struct sp_buf *out);
size_t sp_host_command_help(struct sp_host *, struct sp_buf *out);
int sp_host_add_transport(struct sp_host *, const char *name,
struct sp_transport);
struct sp_transport *sp_host_transport(struct sp_host *, const char *name);
BIN
View File
Binary file not shown.
+57 -4
View File
@@ -34,6 +34,7 @@
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];
@@ -49,7 +50,7 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
(void)h;
if (goal[0] == '\0' || conv == NULL || conv->n == 0)
return 0;
if (turn == 0 || (turn % every) != 0)
if (!force && (turn == 0 || (turn % every) != 0))
return 0;
if (turn == last_reminder)
return 0; /* one reminder per turn, always */
@@ -71,12 +72,60 @@ focus_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
return -1;
last_reminder = turn;
force = 0;
reminders++;
snprintf(widget_text, sizeof(widget_text), "%u reminders, every %u turns",
reminders, every);
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)
{
(void)ctx;
if (args == NULL || *args == '\0') {
if (goal[0] == '\0')
return sp_buf_puts(out, "kein Goal gesetzt "
"(/goal <text>)");
return sp_buf_printf(out, "goal: %s (%u Erinnerungen, alle %u "
"Turns)", goal, reminders, 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;
/* publish now, so the sidebar does not wait for the next tick */
{
char line[192];
snprintf(line, sizeof(line), "%.60s%s | 0 reminders", goal,
strlen(goal) > 60 ? "..." : "");
(void)sp_host_widget_text(h, "focus", line);
}
return sp_buf_printf(out, "goal gesetzt: %s", goal);
}
static int
recap_command(struct sp_host *h, const char *args, struct sp_buf *out,
void *ctx)
{
(void)h;
(void)args;
(void)ctx;
if (goal[0] == '\0')
return sp_buf_puts(out, "kein Goal gesetzt (/goal <text>)");
force = 1; /* the next turn gets a reminder */
return sp_buf_puts(out, "Erinnerung fuer den naechsten Turn eingeplant");
}
static int
focus_tick(struct sp_host *h, const char *name, void *ctx)
{
@@ -84,10 +133,10 @@ focus_tick(struct sp_host *h, const char *name, void *ctx)
(void)ctx;
if (goal[0] == '\0')
snprintf(line, sizeof(line), "no goal (set SPLINTER_FOCUS_GOAL)");
snprintf(line, sizeof(line), "no goal (/goal <text>)");
else
snprintf(line, sizeof(line), "%.60s%s | %u reminders",
goal, strlen(goal) > 60 ? "..." : "", reminders);
snprintf(line, sizeof(line), "%.44s%s | %u rem",
goal, strlen(goal) > 44 ? "..." : "", reminders);
return sp_host_widget_text(h, name, line);
}
@@ -111,6 +160,10 @@ focus_init(struct sp_host *h)
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;
Binary file not shown.
-6
View File
@@ -114,8 +114,6 @@ sp_config_load(struct sp_config *c, const char *path, struct sp_buf *err)
set_str(c->system_prompt, sizeof(c->system_prompt), val);
else if (!strcmp(key, "session_dir"))
set_str(c->session_dir, sizeof(c->session_dir), val);
else if (!strcmp(key, "goal"))
set_str(c->goal, sizeof(c->goal), val);
else if (!strcmp(key, "cli_cmd"))
set_str(c->cli_cmd, sizeof(c->cli_cmd), val);
else if (!strcmp(key, "steps"))
@@ -207,10 +205,6 @@ sp_config_write_example(const char *path, struct sp_buf *err)
"# Where session logs go (default ~/.splinter).\n"
"#session_dir = ~/.splinter\n"
"\n"
"# What this session is for. Plugins see it as SPLINTER_FOCUS_GOAL;\n"
"# the focus plugin repeats it every five turns.\n"
"#goal = \n"
"\n"
"# Sent once at the start of a conversation.\n"
"#system_prompt = Du bist knapp. Keine Floskeln.\n");
if (ferror(f)) {
-1
View File
@@ -32,7 +32,6 @@ struct sp_config {
char system_prompt[1024];
char session_dir[512];
char cli_cmd[512];
char goal[1024]; /* exported to plugins as SPLINTER_FOCUS_GOAL */
char plugins[SP_CONFIG_MAX_PLUGINS][512];
size_t nplugins;
size_t steps; /* 0: not set */
+35 -2
View File
@@ -287,8 +287,6 @@ main(int argc, char **argv)
/* the cli transport reads its command from the environment */
if (conf.cli_cmd[0] != '\0')
setenv("SPLINTER_CLI_CMD", conf.cli_cmd, 0);
if (conf.goal[0] != '\0')
setenv("SPLINTER_FOCUS_GOAL", conf.goal, 0);
/* a config means "this is my setup": start the chat */
if (prompt == NULL)
chatmode = 1;
@@ -429,6 +427,41 @@ main(int argc, char **argv)
return rc;
}
/* a leading slash is a harness command, also without a TUI */
if (prompt != NULL && prompt[0] == '/' && host != NULL) {
char name[64], args[1536];
struct sp_buf out;
const char *sp;
sp = strchr(prompt, ' ');
if (sp == NULL) {
snprintf(name, sizeof(name), "%.63s", prompt + 1);
args[0] = '\0';
} else {
size_t nlen = (size_t)(sp - (prompt + 1));
if (nlen >= sizeof(name))
nlen = sizeof(name) - 1;
memcpy(name, prompt + 1, nlen);
name[nlen] = '\0';
snprintf(args, sizeof(args), "%s", sp + 1);
}
if (sp_buf_init(&out) != 0)
return 3;
if (sp_host_command(host, name, args, &out) != 0) {
sp_buf_clear(&out);
sp_buf_printf(&out, "unbekanntes Kommando: /%s\n"
" verfuegbar: ", name);
(void)sp_host_command_help(host, &out);
printf("%s\n", out.p != NULL ? out.p : "");
sp_buf_free(&out);
return 2;
}
printf("%s\n", out.p != NULL ? out.p : "");
sp_buf_free(&out);
return 0;
}
cfg.stream = stdout_stream;
cfg.stream_ctx = NULL;
rc = sp_agent_run(&ag, prompt, &answer, &err);
+56
View File
@@ -28,6 +28,7 @@
#define MAX_WIDGETS 32
#define MAX_COMPACTORS 8
#define MAX_TRANSPORTS 8
#define MAX_COMMANDS 16
#define MAX_NOTE 2048
struct widget {
@@ -57,6 +58,12 @@ struct sp_host {
struct sp_transport tr;
} transports[MAX_TRANSPORTS];
size_t ntransports;
struct {
char name[32];
sp_cmd_fn fn;
void *ctx;
} commands[MAX_COMMANDS];
size_t ncommands;
pthread_mutex_t lock; /* guards note and the registry */
struct sp_session_info *sess;
struct sp_agent *agent;
@@ -199,6 +206,55 @@ sp_host_transport(struct sp_host *h, const char *name)
return NULL;
}
int
sp_host_add_command(struct sp_host *h, const char *name, sp_cmd_fn fn,
void *ctx)
{
if (h == NULL || name == NULL || fn == NULL)
return -1;
if (h->ncommands == MAX_COMMANDS)
return -1;
snprintf(h->commands[h->ncommands].name,
sizeof(h->commands[h->ncommands].name), "%s", name);
h->commands[h->ncommands].fn = fn;
h->commands[h->ncommands].ctx = ctx;
h->ncommands++;
return 0;
}
int
sp_host_command(struct sp_host *h, const char *name, const char *args,
struct sp_buf *out)
{
size_t i;
int rc = -1;
if (h == NULL || name == NULL)
return -1;
for (i = 0; i < h->ncommands; i++) {
if (strcmp(h->commands[i].name, name) != 0)
continue;
rc = h->commands[i].fn(h, args != NULL ? args : "",
out, h->commands[i].ctx);
if (rc == 0)
return 0;
}
sp_buf_printf(out, "unknown command: /%s", name);
return -1;
}
size_t
sp_host_command_help(struct sp_host *h, struct sp_buf *out)
{
size_t i;
if (h == NULL)
return 0;
for (i = 0; i < h->ncommands; i++)
(void)sp_buf_printf(out, "/%s ", h->commands[i].name);
return h->ncommands;
}
int
sp_host_compact(struct sp_host *h, struct sp_conv *conv, size_t budget)
{
+56
View File
@@ -603,8 +603,25 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
wmove(ui_input, 0, 2);
wrefresh(ui_input);
/*
* A widget that ticks is useless if the screen only refreshes
* when a key is pressed. The input read times out every 500 ms
* and that timeout repaints the sidebar and the status line --
* cheap, and it makes clock/focus/cps actually alive.
*/
wtimeout(ui_input, 500);
for (;;) {
ch = wgetch(ui_input);
if (ch == ERR) {
draw_sidebar(&s, ag->sess != NULL ?
ag->sess->id : NULL,
ag->sess != NULL ? ag->sess->dir : NULL,
host);
draw_status(&s);
wnoutrefresh(ui_input);
doupdate();
continue;
}
if (ch == '\n' || ch == '\r' || ch == KEY_ENTER)
break;
if (ch == 4) {
@@ -647,6 +664,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
}
}
input[len] = '\0';
wtimeout(ui_input, -1);
}
if (s.quit)
@@ -654,6 +672,44 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
if (input[0] == '\0')
continue;
/*
* A leading slash is a command for the harness, not a prompt: it
* never reaches the model, and its reply is shown as the harness
* speaking. Unknown commands list what exists.
*/
if (input[0] == '/' && host != NULL) {
char name[64], args[1536], outbuf[2048];
struct sp_buf out;
const char *sp;
sp = strchr(input, ' ');
if (sp == NULL) {
snprintf(name, sizeof(name), "%.63s", input + 1);
args[0] = '\0';
} else {
size_t nlen = (size_t)(sp - (input + 1));
if (nlen >= sizeof(name))
nlen = sizeof(name) - 1;
memcpy(name, input + 1, nlen);
name[nlen] = '\0';
snprintf(args, sizeof(args), "%s", sp + 1);
}
if (sp_buf_init(&out) != 0)
continue;
if (sp_host_command(host, name, args, &out) != 0) {
sp_buf_clear(&out);
sp_buf_printf(&out, "unbekanntes Kommando: /%s\n"
" verfuegbar: ", name);
(void)sp_host_command_help(host, &out);
}
snprintf(outbuf, sizeof(outbuf), "%s",
out.p != NULL ? out.p : "");
sp_buf_free(&out);
add_entry(&s, "splinter", outbuf);
continue;
}
add_entry(&s, "you", input);
draw_history(&s);
draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL,
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+38
View File
@@ -109,6 +109,44 @@ main(void)
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == 2);
/* ---- /goal sets it at runtime, /recap forces the next reminder ---- */
{
struct sp_buf out;
CHECK(sp_buf_init(&out) == 0);
sp_conv_free(&conv);
sp_conv_init(&conv);
CHECK(sp_conv_push(&conv, "user", NULL, "turn fuenf") == 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);
/* a forced recap lands on the next turn regardless of the counter */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "recap", "", &out) == 0);
sp_host_turn(host);
{
int before = count_focus(&conv);
CHECK(sp_host_compact(host, &conv, 1) == 1);
CHECK(count_focus(&conv) == before + 1);
CHECK(strstr(conv.v[conv.n - 1].content, "turn fuenf") != 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);
sp_buf_free(&out);
}
sp_conv_free(&conv);
sp_host_free(host);
sp_buf_free(&err);