The harness was two halves that could not see each other: main.c built the
agent, ui.c ran it -- with the curses thread model, the approval condition
variable, the slash commands and the provider switching inside the drawing
code. A second front end had to copy all of it.
Now:
src/backend/ engine.c owns the session, the turn thread, the event queue
and every command; agent, transports, tools, session, config,
providers, plugins and the protocol servers move here.
src/frontend/ curses.c draws and reads keys through ui_backend.h;
ui_local.c is the engine in this process, ui_remote.c is a
served engine behind a socket. protocol.c serves the engine
as JSON lines (protocol 2: history, approvals, steering);
client.c is splinterctl, the reference client.
splinter --serve / --attach: the same screen runs as a client of a served
engine, so the socket carries everything the front end needs. docs/PROTOCOL.md
and docs/FRONTENDS.md describe the contract; docs/proto-example.py is a
60-line client. tests/test_engine, test_protocol and test_attach cover the
event order, the socket and the attach mode.
Review fixes in the same tree: dynamic line buffers (9 KB prompts and 8 KB
events were truncated), a turn event no longer ends the busy window (steer
leftovers), the screen finalises every turn, /new keeps the profile, reopens
the log and gets unique ids, the profile is remembered in <session>.agent so
--resume comes back in it, agent statistics are a locked snapshot (no data
race), !shell keeps 64 KiB with a marker, the socket probes for a live server,
compares its inode and sets CLOEXEC/SNDTIMEO, exit 2 means configuration,
BSD peer checks use getpeereid, and the i18n checker scans the new
subdirectories again.
Includes the parallel session's SSE fix: the parser reports what a line
produced, so a tool name no longer lands in the answer ("readread").
296 lines
7.8 KiB
C
296 lines
7.8 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);
|
|
|
|
/*
|
|
* /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;
|
|
}
|