/* * 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 #include #include #include #include #include #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); /* * 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); /* 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, "main") == 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); /* * /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); } /* * /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; }