Files
splinter/tests/test_focus.c
T
lotherk f7120882fa sessions: a resumed session is the whole transcript, not a chat log
A session file was written as if it were a chat: role and text, nothing
else. Reading it back kept "user" and "assistant" and dropped everything
else - and nearly every line of an agent transcript is an observation.
Measured on three real sessions: 21 of 1507 messages came back, 8 of 1356,
3 of 317. --resume then sent 1% of the conversation and the model had no
idea what it was continuing.

Three writers had to change with the loader:

  - A turn that asked for tools wrote no line at all (agent.c only wrote
    when ncalls == 0). It now goes out with its tool_calls, and each result
    with the name of its tool and its call id; a provider rejects a tool
    message without them.
  - {"role":"user","text":""} came from the steer path and showed the model
    a blank human turn it answered. sp_session_append_rec writes nothing
    when text and calls are both empty.
  - A record from the old format has no name and no call id. It is paired
    with the next unanswered call of its pending assistant turn, or given a
    synthetic one (recovered-N). Dropping it is what emptied every resume;
    keeping it unpaired is what a provider answers with 400.

The same pass gives the core its own compactor (compact.c): a thread digest
- one line per dropped message, OPERATOR: 800 chars, assistant: 300, tool
result 160 - so the model keeps the story, not a count. lcm stays a plugin
that runs after it; sp_core_compactor() is the fallback only when no plugin
registered one, because running both eats the conversation before lcm sees
work to do.

Two bugs fell out of the same area:

  - The plugin host was only built when a plugin was configured, so a
    plugin-less run had no compactor at all: over max_bytes the turn was
    refused instead of compacted (90 KB session, budget 8000, "nothing
    sent").
  - lcm walked *forward* past tool results at the tail boundary, which
    silently shrank the tail: a turn with two results kept four messages
    where keep_tail says six. Walking back to the assistant that carries
    the call keeps six and no orphan.

And the .meta sidecar is shared now: the core rewrote it with only the keys
it knew, so anything a plugin put there was gone on the next open. It
carries unknown lines through (extra[8][160]), which is what lets a plugin
mark a session in the core's own file.

Tests: tests/test_corecompact.c (10 checks, no plugin), the resume cases in
tests/test_session.c, and the tail-size check in tests/test_compactor.c.
Each was seen red on the old code before it was green on the new.
2026-11-21 21:25:51 +00:00

342 lines
10 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)
/*
* The focus plugin ignores the budget, but the core compactor does not: it
* trims and digests whenever the conversation does not fit. This file tests
* reminders, so it hands over a budget nothing can exceed. The old "1" meant
* "one byte" the moment the core started reading it, and the core then ate the
* reminders the test was looking for.
*/
#define NO_PRESSURE ((size_t)-1)
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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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, NO_PRESSURE) == 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;
}