Files
splinter/tests/test_engine.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

493 lines
14 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"
#include "mcp.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");
/* a second turn whose tool fails: /self-check must name the failure */
fprintf(f, "tool read {\"path\":\"/nonexistent-splinter-test\"}\n");
fprintf(f, "answer done\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);
/*
* One provider and one pool, so the picker's list is not empty: the
* front end asks the engine, never the config file it may not have.
*/
snprintf(conf.endpoint, sizeof(conf.endpoint),
"http://127.0.0.1:1/v1");
snprintf(conf.model, sizeof(conf.model), "test-model");
snprintf(conf.default_id, sizeof(conf.default_id), "main");
conf.npools = 1;
snprintf(conf.pool[0].id, sizeof(conf.pool[0].id), "testpool");
snprintf(conf.pool[0].strategy, sizeof(conf.pool[0].strategy),
"balance");
snprintf(conf.pool[0].member[0], sizeof(conf.pool[0].member[0]),
"main");
conf.pool[0].nmembers = 1;
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);
/*
* Every tool schema must be a valid JSON schema object. A judge model
* (Claude) validates them and rejects the whole request when one has
* "parameters": {} without "type": "object" - delegate_due and
* delegate_jobs did, and three sessions ended in an empty stream.
*/
{
size_t n = 0, i;
const struct sp_tool *tools = sp_registry_tools(
sp_engine_registry(eng), &n);
CHECK(n > 0);
for (i = 0; i < n; i++) {
mcp_json *v;
const char *type;
if (tools[i].params == NULL)
continue;
v = mcp_parse(tools[i].params);
CHECK(v != NULL);
if (v == NULL) {
fprintf(stderr, "tool %s: unparseable schema\n",
tools[i].name);
continue;
}
type = mcp_arg_string(v, "type");
if (type == NULL || strcmp(type, "object") != 0)
fprintf(stderr, "tool %s: parameters.type is %s\n",
tools[i].name, type != NULL ? type : "(missing)");
CHECK(type != NULL && strcmp(type, "object") == 0);
mcp_json_free(v);
}
}
/*
* mcp_parse_exact: a tool call is one object, not the first of several.
* mcp_parse() stops at the closing brace, so "{a}{b}" parsed as {a} and
* the second command vanished; the exact form is what the agent uses.
*/
{
mcp_json *v;
v = mcp_parse("{\"a\":1}{\"b\":2}");
CHECK(v != NULL);
mcp_json_free(v);
CHECK(mcp_parse_exact("{\"a\":1}{\"b\":2}") == NULL);
CHECK(mcp_parse_exact("{\"a\":1") == NULL);
v = mcp_parse_exact(" {\"command\":\"ls\"} \n");
CHECK(v != NULL);
CHECK(contains(mcp_arg_string(v, "command"), "ls"));
mcp_json_free(v);
}
/* the /model picker's list: providers first, then pools */
{
struct sp_model_choice mv[SP_MODEL_CHOICES];
size_t mn = sp_engine_models(eng, mv,
SP_MODEL_CHOICES);
CHECK(mn == 2);
CHECK(strcmp(mv[0].sel, "local/test-model") == 0);
CHECK(contains(mv[0].label, "test-model"));
CHECK(strcmp(mv[1].sel, "testpool") == 0);
CHECK(contains(mv[1].label, "pool"));
}
kind = sp_engine_input(eng, "/cost", &out);
CHECK(kind == SP_INPUT_HANDLED);
CHECK(contains(out.p, "cost:"));
CHECK(contains(out.p, "judged:"));
sp_buf_clear(&out);
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);
/*
* /self-check hands the model its own tail: the turns it ran and the
* observations that failed, with the framing that tells it to debug
* itself. Both spellings are commands; the hyphen is the real one.
*/
{
struct sp_event ev;
int guard = 0;
/* the second scripted turn reads a path that does not exist */
CHECK(sp_engine_submit(eng, "read it") == 0);
while (guard++ < 500) {
while (sp_engine_poll(eng, &ev))
sp_engine_event_free(&ev);
if (!sp_engine_busy(eng))
break;
{
struct timespec ts = { 0, 10000000L };
nanosleep(&ts, NULL);
}
}
(void)sp_engine_wait(eng);
}
kind = sp_engine_input(eng, "/self-check", &out);
CHECK(kind == SP_INPUT_PROMPT);
CHECK(contains(out.p, "check yourself"));
CHECK(contains(out.p, "sag hallo"));
CHECK(contains(out.p, "the last turns"));
CHECK(contains(out.p, "failures in those turns"));
CHECK(contains(out.p, "nonexistent-splinter-test"));
sp_buf_clear(&out);
kind = sp_engine_input(eng, "/selfcheck", &out);
CHECK(kind == SP_INPUT_PROMPT);
CHECK(contains(out.p, "check yourself"));
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 turn outside the engine (the daemon's Telegram loop) holds the
* agent: the engine reports busy, refuses a submit, a profile switch
* and a second holder, and lets go when the turn ends.
*/
{
struct sp_agent *ag = sp_engine_agent(eng);
struct sp_event ev;
CHECK(sp_engine_agent_turn_begin(ag) == 0);
CHECK(sp_engine_busy(eng) == 1);
CHECK(sp_engine_submit(eng, "hi") == -1);
CHECK(sp_engine_agent_switch(eng, 0) == -1);
CHECK(sp_engine_turn_begin(eng) == -1);
sp_engine_agent_turn_end(ag);
CHECK(sp_engine_busy(eng) == 0);
CHECK(sp_engine_hold(eng) == 0);
CHECK(sp_engine_busy(eng) == 0);
CHECK(sp_engine_turn_begin(eng) == -1);
sp_engine_release(eng);
/* an agent no engine owns is nobody's business */
CHECK(sp_engine_agent_turn_begin(NULL) == 0);
while (sp_engine_poll(eng, &ev))
sp_engine_event_free(&ev);
}
/*
* /model belongs to the session: the selector is written to the
* session's sidecar and comes back when the session is reopened.
*/
kind = sp_engine_input(eng, "/model test-model-2", &out);
CHECK(kind == SP_INPUT_HANDLED);
CHECK(contains(out.p, "test-model-2"));
{
char mp[800], line[320];
FILE *mf;
snprintf(mp, sizeof(mp), "%s/%s.model", path,
sp_engine_session_id(eng));
mf = fopen(mp, "r");
CHECK(mf != NULL);
if (mf != NULL) {
CHECK(fgets(line, sizeof(line), mf) != NULL);
CHECK(contains(line, "test-model-2"));
fclose(mf);
}
}
sp_buf_clear(&out);
/* the endpoint answers nothing here; the command still answers */
kind = sp_engine_input(eng, "/model probe", &out);
CHECK(kind == SP_INPUT_HANDLED);
CHECK(out.p != NULL && out.p[0] != '\0');
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);
{
struct sp_engine_state st;
CHECK(sp_engine_state(eng2, &st) == 0);
CHECK(strcmp(st.model, "test-model-2") == 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;
}