The goal lived only in the focus plugin's memory: a resume lost it and
there was no way to start the next turn from it. It is now stored in the
session's .meta (one line, 1000 bytes) and read back when the plugin sees a
new session id, so /open and a resumed process find their own goal again.
/continue reads the same sidecar and starts a turn with "Continue working
toward this goal: ...". /goal still sets it and starts the recap turn.
The sidebar was a patchwork of pairs and widgets could inherit the wrong
background. It now uses one PAIR_SIDE (white on a darkened theme
background) for every heading, label, usage row and widget; the window
gets that pair as its background, so no line paints its own surface.
New plugins/sysinfo: a widget thread samples /proc/meminfo, /proc/stat and
statvfs("/"), and publishes RAM, CPU, swap and disk with five-cell
[|||| ] gauges every two seconds. It is added to the shipped plugin list
and to the user's config.
agent.c keeps the last prompt/cache counters when a tool-call step reports
no usage. A zero there used to reset the ctx gauge in the status bar after
every tool call.
Tests: session goal survives reopen, /continue returns the prompt,
make check: all tests passed.
304 lines
8.1 KiB
C
304 lines
8.1 KiB
C
/*
|
|
* splinter -- the engine a front end talks to: one scripted turn, no screen.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* What it guards: the event queue carries a whole turn in order (busy, the
|
|
* streamed text, the step, the call, its observation, the answer, idle); the
|
|
* commands answer through sp_engine_input without touching an agent; a shell
|
|
* line becomes an entry; the shared call/observation shaping reads the way the
|
|
* transcript does; and closing the engine frees the turn it ran.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_engine.h"
|
|
#include "config.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)
|
|
|
|
static int
|
|
contains(const char *hay, const char *needle)
|
|
{
|
|
return hay != NULL && strstr(hay, needle) != NULL;
|
|
}
|
|
|
|
/* the whole turn, by draining the queue until the engine says idle */
|
|
static void
|
|
run_turn(struct sp_engine *eng)
|
|
{
|
|
struct sp_event ev;
|
|
int saw_busy_on = 0, saw_stream = 0, saw_step = 0;
|
|
int saw_tool = 0, saw_result = 0, saw_turn = 0;
|
|
int saw_busy_off = 0;
|
|
int rc = -1;
|
|
char answer[256] = "";
|
|
char tool[64] = "";
|
|
int guard = 0;
|
|
|
|
CHECK(sp_engine_submit(eng, "sag hallo") == 0);
|
|
while (guard++ < 500) {
|
|
while (sp_engine_poll(eng, &ev)) {
|
|
switch (ev.kind) {
|
|
case SP_EV_BUSY:
|
|
if (ev.n != 0)
|
|
saw_busy_on = 1;
|
|
else
|
|
saw_busy_off = 1;
|
|
break;
|
|
case SP_EV_STREAM:
|
|
if (ev.n == SP_STREAM_TEXT)
|
|
saw_stream = 1;
|
|
break;
|
|
case SP_EV_STEP:
|
|
saw_step = 1;
|
|
break;
|
|
case SP_EV_TOOL:
|
|
saw_tool = 1;
|
|
snprintf(tool, sizeof(tool), "%s",
|
|
ev.a != NULL ? ev.a : "");
|
|
break;
|
|
case SP_EV_TOOL_RESULT:
|
|
saw_result = 1;
|
|
break;
|
|
case SP_EV_TURN_END:
|
|
saw_turn = 1;
|
|
rc = (int)ev.n;
|
|
snprintf(answer, sizeof(answer), "%s",
|
|
ev.a != NULL ? ev.a : "");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
sp_engine_event_free(&ev);
|
|
}
|
|
if (!sp_engine_busy(eng) && saw_busy_off)
|
|
break;
|
|
{
|
|
struct timespec ts = { 0, 10000000L };
|
|
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
}
|
|
(void)sp_engine_wait(eng);
|
|
CHECK(saw_busy_on);
|
|
/* the mock transport cannot stream; it is not the engine's job to fake it */
|
|
(void)saw_stream;
|
|
CHECK(saw_step);
|
|
CHECK(saw_tool);
|
|
CHECK(strcmp(tool, "echo") == 0);
|
|
CHECK(saw_result);
|
|
CHECK(saw_turn);
|
|
CHECK(rc == 0);
|
|
CHECK(strcmp(answer, "fertig") == 0);
|
|
CHECK(saw_busy_off);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_config conf;
|
|
struct sp_agent_config base;
|
|
struct sp_engine_opts opts;
|
|
struct sp_engine *eng = NULL;
|
|
struct sp_buf err, out;
|
|
char tmpl[512], script[600], path[700], logpath[760];
|
|
char *dir;
|
|
const char *tmp = getenv("TMPDIR");
|
|
FILE *f;
|
|
char call[256];
|
|
int kind;
|
|
|
|
if (tmp == NULL || *tmp == '\0')
|
|
tmp = "/tmp";
|
|
snprintf(tmpl, sizeof(tmpl), "%s/splinter-engine-XXXXXX", tmp);
|
|
dir = mkdtemp(tmpl);
|
|
if (dir == NULL) {
|
|
perror("mkdtemp");
|
|
return 1;
|
|
}
|
|
(void)snprintf(script, sizeof(script), "%s/script.txt", dir);
|
|
snprintf(path, sizeof(path), "%s/.splinter", dir);
|
|
(void)mkdir(path, 0700);
|
|
f = fopen(script, "w");
|
|
if (f == NULL) {
|
|
perror("script");
|
|
return 1;
|
|
}
|
|
/* one call, then the answer; the mock replays one line per request */
|
|
fprintf(f, "tool echo {\"text\":\"hallo-welt\"}\n");
|
|
fprintf(f, "answer fertig\n");
|
|
fclose(f);
|
|
(void)setenv("HOME", dir, 1);
|
|
(void)sp_i18n_init("en");
|
|
|
|
if (sp_buf_init(&err) != 0 || sp_buf_init(&out) != 0)
|
|
return 1;
|
|
sp_config_init(&conf);
|
|
memset(&base, 0, sizeof(base));
|
|
base.max_steps = 10;
|
|
base.system_prompt = "test";
|
|
memset(&opts, 0, sizeof(opts));
|
|
opts.conf = &conf;
|
|
opts.base = &base;
|
|
opts.script = script;
|
|
opts.session_dir = path;
|
|
opts.session_id = "sess-engine";
|
|
opts.no_transport = 0;
|
|
if (sp_engine_open(&eng, &opts, &err) != 0) {
|
|
fprintf(stderr, "engine: %s\n", err.p != NULL ? err.p : "?");
|
|
return 1;
|
|
}
|
|
run_turn(eng);
|
|
|
|
kind = sp_engine_input(eng, "/status", &out);
|
|
CHECK(kind == SP_INPUT_HANDLED);
|
|
CHECK(contains(out.p, "session sess-engine"));
|
|
CHECK(contains(out.p, "agent build"));
|
|
sp_buf_clear(&out);
|
|
|
|
kind = sp_engine_input(eng, "/no-such-command", &out);
|
|
CHECK(kind == SP_INPUT_HANDLED);
|
|
CHECK(contains(out.p, "/no-such-command"));
|
|
sp_buf_clear(&out);
|
|
|
|
/* the goal is in the session sidecar; /continue turns it into a prompt */
|
|
CHECK(sp_session_set_goal(path, "sess-engine",
|
|
"Ship the config file", &err) == 0);
|
|
kind = sp_engine_input(eng, "/continue", &out);
|
|
CHECK(kind == SP_INPUT_PROMPT);
|
|
CHECK(contains(out.p, "Ship the config file"));
|
|
sp_buf_clear(&out);
|
|
|
|
/*
|
|
* /new drops the conversation, not the way of working: the active
|
|
* profile's prompt goes back on top, and the new session gets an id of
|
|
* its own even when two /new happen in the same second.
|
|
*/
|
|
{
|
|
int i, n = sp_engine_agents(eng), plan = -1;
|
|
char id1[64] = "", id2[64] = "";
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (strcmp(sp_engine_agent_name(eng, i), "plan") == 0)
|
|
plan = i;
|
|
CHECK(plan >= 0);
|
|
CHECK(sp_engine_agent_switch(eng, plan) == 0);
|
|
CHECK(sp_engine_input(eng, "/new", &out) == SP_INPUT_HANDLED);
|
|
CHECK(contains(out.p, "sess-"));
|
|
snprintf(id1, sizeof(id1), "%s", out.p != NULL ? out.p : "");
|
|
/* "[agent: plan]" is the profile tag, in every language */
|
|
CHECK(sp_engine_conv(eng)->n > 0 &&
|
|
contains(sp_engine_conv(eng)->v[0].content,
|
|
"[agent: plan]"));
|
|
sp_buf_clear(&out);
|
|
CHECK(sp_engine_input(eng, "/new", &out) == SP_INPUT_HANDLED);
|
|
snprintf(id2, sizeof(id2), "%s", out.p != NULL ? out.p : "");
|
|
CHECK(strcmp(id1, id2) != 0);
|
|
CHECK(sp_engine_conv(eng)->n > 0 &&
|
|
contains(sp_engine_conv(eng)->v[0].content,
|
|
"[agent: plan]"));
|
|
sp_buf_clear(&out);
|
|
}
|
|
|
|
/* a shell line is an entry event, not a reply */
|
|
kind = sp_engine_input(eng, "!echo shell-out", &out);
|
|
CHECK(kind == SP_INPUT_HANDLED);
|
|
{
|
|
int got = 0;
|
|
struct sp_event ev;
|
|
|
|
while (sp_engine_poll(eng, &ev)) {
|
|
if (ev.kind == SP_EV_ENTRY && ev.a != NULL &&
|
|
strcmp(ev.a, "shell") == 0 &&
|
|
contains(ev.b, "shell-out"))
|
|
got = 1;
|
|
sp_engine_event_free(&ev);
|
|
}
|
|
CHECK(got);
|
|
}
|
|
sp_buf_clear(&out);
|
|
|
|
/* the session log is on disk, one JSON object per line */
|
|
snprintf(logpath, sizeof(logpath), "%s/sess-engine.jsonl",
|
|
opts.session_dir);
|
|
CHECK(access(logpath, R_OK) == 0);
|
|
|
|
/*
|
|
* The session remembers the profile: a new engine on the same session
|
|
* starts in plan again, and the profile prompt appears once, not twice.
|
|
*/
|
|
{
|
|
char sess[64], sdir[512];
|
|
struct sp_engine *eng2 = NULL;
|
|
int plan2 = -1, i, n2, marks = 0;
|
|
const char *sys;
|
|
|
|
snprintf(sess, sizeof(sess), "%s", sp_engine_session_id(eng));
|
|
snprintf(sdir, sizeof(sdir), "%s", sp_engine_state_dir(eng));
|
|
sp_engine_close(eng);
|
|
eng = NULL;
|
|
opts.session_id = sess;
|
|
opts.session_dir = sdir;
|
|
opts.resume = 1;
|
|
opts.agent = NULL;
|
|
CHECK(sp_engine_open(&eng2, &opts, &err) == 0);
|
|
n2 = sp_engine_agents(eng2);
|
|
for (i = 0; i < n2; i++)
|
|
if (strcmp(sp_engine_agent_name(eng2, i), "plan") == 0)
|
|
plan2 = i;
|
|
CHECK(plan2 >= 0);
|
|
CHECK(sp_engine_agent_cur(eng2) == plan2);
|
|
sys = sp_engine_conv(eng2)->n > 0 ?
|
|
sp_engine_conv(eng2)->v[0].content : "";
|
|
for (i = 0; sys != NULL && (sys = strstr(sys, "[agent: plan]"))
|
|
!= NULL; i++, sys++)
|
|
marks = i + 1;
|
|
CHECK(marks == 1);
|
|
CHECK(sp_engine_take_steer(eng2, &out) == 0);
|
|
sp_buf_clear(&out);
|
|
sp_engine_close(eng2);
|
|
eng = eng2;
|
|
}
|
|
|
|
|
|
|
|
/* the shaping both front ends print */
|
|
sp_call_summary("run", "{\"command\":\"ls -l\"}", call, sizeof(call));
|
|
CHECK(strcmp(call, "Run ls -l") == 0);
|
|
sp_call_summary("read", "{\"path\":\"src/x.c\",\"limit\":10}", call,
|
|
sizeof(call));
|
|
CHECK(strcmp(call, "Read src/x.c") == 0);
|
|
sp_buf_clear(&out);
|
|
CHECK(sp_obs_short("[exit status 0]", &out) == SP_OBS_JOIN);
|
|
CHECK(strcmp(out.p, "[exit status 0]") == 0);
|
|
sp_buf_clear(&out);
|
|
CHECK(sp_obs_short("", &out) == SP_OBS_NONE);
|
|
sp_buf_free(&out);
|
|
sp_buf_free(&err);
|
|
(void)unlink(script);
|
|
(void)rmdir(path);
|
|
(void)rmdir(dir);
|
|
|
|
if (failures == 0) {
|
|
printf("test_engine: ok\n");
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|