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").
236 lines
9.3 KiB
C
236 lines
9.3 KiB
C
/*
|
|
* splinter -- the engine every front end talks to.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The engine is the whole harness minus the screen: it owns the session, the
|
|
* conversation, the tool registry, the plugins, the transport and the agent,
|
|
* runs one turn at a time on its own thread, and reports what happened as a
|
|
* queue of events.
|
|
*
|
|
* Why a queue and not callbacks: a turn runs on a thread of the engine's
|
|
* making, and a front end (curses, a socket, a GUI) must not be called from
|
|
* it -- it would have to be thread-safe in every drawing routine, and a slow
|
|
* front end would stall the model. The engine only appends to a queue under a
|
|
* mutex; the front end drains it whenever it is ready. Nothing here ever calls
|
|
* into a front end.
|
|
*
|
|
* The contract, in one paragraph: sp_engine_open() builds the engine from the
|
|
* command line and the config; sp_engine_input() turns a typed line into
|
|
* either a command reply or a prompt; sp_engine_submit() starts the turn;
|
|
* sp_engine_poll() delivers its events; sp_engine_approve(), sp_engine_steer()
|
|
* and sp_engine_cancel() are what a front end may do while it runs.
|
|
*
|
|
* This is the C boundary. The same engine is served over a line-oriented JSON
|
|
* socket by `splinter --serve`, so a GUI may be written in any language; see
|
|
* docs/PROTOCOL.md. The two front ends use exactly the calls below, which is
|
|
* what keeps them from drifting apart.
|
|
*/
|
|
#ifndef SPLINTER_ENGINE_H
|
|
#define SPLINTER_ENGINE_H
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "splinter.h"
|
|
|
|
struct sp_config;
|
|
struct sp_host;
|
|
struct sp_agent;
|
|
struct sp_conv;
|
|
struct sp_engine;
|
|
struct swarm;
|
|
|
|
#define SP_ENGINE_PLUGINS 32
|
|
|
|
/* ---------------------------------------------------------------- options */
|
|
|
|
/*
|
|
* What sp_engine_open() needs. Strings are copied; `conf` and `base` are read
|
|
* and not owned (main keeps them alive for the whole run), but the engine does
|
|
* change conf the way the old binary did: /model writes a drop-in, /connect
|
|
* adds a provider, a pool failover marks one benched.
|
|
*/
|
|
struct sp_engine_opts {
|
|
struct sp_config *conf;
|
|
const struct sp_agent_config *base; /* steps, byte budget, prompt */
|
|
const char *endpoint;
|
|
const char *model;
|
|
const char *key_env; /* variable holding the API key */
|
|
const char *transport; /* plugin transport ("cli"), or NULL */
|
|
const char *kind; /* "anthropic", or NULL */
|
|
const char *script; /* mock replies, one per line */
|
|
const char *agent; /* profile to start in */
|
|
const char *session_dir;
|
|
const char *session_id;
|
|
const char *plugins[SP_ENGINE_PLUGINS];
|
|
size_t nplugins;
|
|
int resume; /* load the session's history */
|
|
int continue_last; /* -c: newest session in this cwd */
|
|
int no_transport; /* tools only (MCP server, delegate) */
|
|
int swarm; /* join the local fleet */
|
|
};
|
|
|
|
/* ----------------------------------------------------------------- events */
|
|
|
|
enum sp_ev_kind {
|
|
SP_EV_NONE = 0,
|
|
SP_EV_BUSY, /* n: 1 the turn started, 0 it ended */
|
|
SP_EV_STREAM, /* n: SP_STREAM_TEXT|SP_STREAM_THINK, a: chunk */
|
|
SP_EV_STEP, /* a: the step's reasoning, b: its answer */
|
|
SP_EV_TOOL, /* a: name, b: arguments as sent */
|
|
SP_EV_TOOL_RESULT, /* a: name, b: observation, n: 1 joins the call */
|
|
SP_EV_NOTE, /* a: the prompt's one line, "" clears it */
|
|
SP_EV_ENTRY, /* a: role, b: text */
|
|
SP_EV_APPROVAL, /* a: name, b: args, n: authorization class */
|
|
SP_EV_TURN_END, /* n: rc, a: answer, b: error */
|
|
SP_EV_STATE, /* the model, tokens or profile changed */
|
|
SP_EV_CLEAR, /* /new: the transcript is the old conversation */
|
|
SP_EV_QUIT /* /quit was typed; the front end may close */
|
|
};
|
|
|
|
struct sp_event {
|
|
int kind;
|
|
long n;
|
|
char *a;
|
|
char *b;
|
|
};
|
|
|
|
/* 1 when an event was taken, 0 when the queue is empty. Free with
|
|
* sp_engine_event_free(). */
|
|
int sp_engine_poll(struct sp_engine *, struct sp_event *);
|
|
void sp_engine_event_free(struct sp_event *);
|
|
|
|
/* --------------------------------------------------------------- snapshot */
|
|
|
|
struct sp_engine_state {
|
|
char model[256];
|
|
char endpoint[512];
|
|
char agent[48];
|
|
char session[64];
|
|
char dir[512];
|
|
char theme[48]; /* set by the front end, for /status */
|
|
size_t tokens_in, tokens_out;
|
|
size_t steps, calls, compactions; /* the session's counters */
|
|
size_t last_ms, last_steps, last_calls; /* the last turn */
|
|
int busy;
|
|
int quit; /* /quit was typed */
|
|
int waiting; /* the turn waits for an approval */
|
|
int agents_n;
|
|
int agent_cur;
|
|
};
|
|
|
|
int sp_engine_state(struct sp_engine *, struct sp_engine_state *);
|
|
|
|
/* -------------------------------------------------------------- lifecycle */
|
|
|
|
int sp_engine_open(struct sp_engine **, const struct sp_engine_opts *,
|
|
struct sp_buf *err);
|
|
void sp_engine_close(struct sp_engine *);
|
|
|
|
/* The pieces a front end may look at. The pointers stay valid until close. */
|
|
struct sp_agent *sp_engine_agent(struct sp_engine *);
|
|
struct sp_host *sp_engine_host(struct sp_engine *);
|
|
struct sp_config *sp_engine_config(struct sp_engine *);
|
|
struct sp_registry *sp_engine_registry(struct sp_engine *);
|
|
struct swarm *sp_engine_swarm(struct sp_engine *);
|
|
const struct sp_conv *sp_engine_conv(struct sp_engine *);
|
|
void sp_engine_set_theme(struct sp_engine *, const char *);
|
|
/* The default stream callback for async turns (the gateway has nobody to
|
|
* stream to and installs a sink; -p passes its own to sp_engine_prompt). */
|
|
void sp_engine_set_stream(struct sp_engine *, sp_stream_cb, void *ctx);
|
|
const char *sp_engine_state_dir(struct sp_engine *);
|
|
const char *sp_engine_session_id(struct sp_engine *);
|
|
|
|
/* ------------------------------------------------------------------ turns */
|
|
|
|
/*
|
|
* Start a turn. Returns 0, or -1 when one is already running: the engine runs
|
|
* exactly one turn at a time, because there is one conversation and one model.
|
|
*/
|
|
int sp_engine_submit(struct sp_engine *, const char *text);
|
|
int sp_engine_busy(struct sp_engine *);
|
|
int sp_engine_quit(struct sp_engine *);
|
|
/* Join the finished turn. Call it once sp_engine_busy() said 0, before the
|
|
* next submit; closing does it too. */
|
|
int sp_engine_wait(struct sp_engine *);
|
|
|
|
/* Ask the model once without the queue: what -p and the slash prompt use. */
|
|
int sp_engine_prompt(struct sp_engine *, const char *text, sp_stream_cb,
|
|
void *stream_ctx, struct sp_buf *answer, struct sp_buf *err);
|
|
|
|
/* ------------------------------------------------------- during one turn */
|
|
|
|
int sp_engine_cancel(struct sp_engine *);
|
|
/* Text typed while the turn runs: the loop picks it up at a step boundary.
|
|
* -1 when no turn is running. */
|
|
int sp_engine_steer(struct sp_engine *, const char *text);
|
|
/*
|
|
* A steer that arrived after the turn's last step boundary: the worker has
|
|
* finished, the text is still pending. A front end takes it and runs it as the
|
|
* next prompt, so typing into the last second is not lost. 0 while a turn is
|
|
* still running (the loop will take it) or when nothing is pending.
|
|
*/
|
|
int sp_engine_take_steer(struct sp_engine *, struct sp_buf *out);
|
|
/* 1 while a tool waits for the operator, with the call in the buffers. */
|
|
int sp_engine_approval(struct sp_engine *, char *name, size_t namelen,
|
|
int *auth, char *args, size_t argslen);
|
|
/* Answer it. 0 when nothing was pending. */
|
|
int sp_engine_approve(struct sp_engine *, int allow);
|
|
|
|
/* ------------------------------------------------------------------ input */
|
|
|
|
/*
|
|
* One line from the operator. Returns
|
|
*
|
|
* SP_INPUT_PROMPT a prompt for the model; start the turn with it
|
|
* SP_INPUT_HANDLED a command; `out` is the reply to show
|
|
* SP_INPUT_SUBMIT a command that replaced the line; `out` is the prompt
|
|
* to start a turn with (a program in commands/)
|
|
*
|
|
* The engine owns /model, /agent, /new, /connect, /usage, /status, /widgets,
|
|
* /swarm, /resume, /quit, !shell and every command a plugin registered, so a
|
|
* GUI gets the same set as the curses front end without re-implementing one.
|
|
*
|
|
* Call it while the engine is idle. A command changes the conversation, the
|
|
* transport and the session; doing that under a running turn is a front-end
|
|
* bug. sp_engine_conv() is a read-only view for the same reason: the messages
|
|
* are the worker's while a turn runs.
|
|
*/
|
|
#define SP_INPUT_PROMPT 0
|
|
#define SP_INPUT_HANDLED 1
|
|
#define SP_INPUT_SUBMIT 2
|
|
|
|
int sp_engine_input(struct sp_engine *, const char *line, struct sp_buf *out);
|
|
/* The names Tab completes, without the slash, sorted. Returns the count. */
|
|
int sp_engine_command_names(struct sp_engine *, char (*names)[64], int max);
|
|
|
|
/* Agent profiles: Tab's three calls. */
|
|
int sp_engine_agents(struct sp_engine *);
|
|
const char *sp_engine_agent_name(struct sp_engine *, int idx);
|
|
int sp_engine_agent_cur(struct sp_engine *);
|
|
int sp_engine_agent_switch(struct sp_engine *, int idx);
|
|
|
|
/* ------------------------------------------------------------- formatting */
|
|
|
|
/*
|
|
* How a tool call and its observation read, shared by every front end:
|
|
*
|
|
* Run du -sh ../xsrc
|
|
* Read src/frontend/curses.c
|
|
*
|
|
* Arguments are unescaped, the one that matters (command, path, ...) is
|
|
* picked, and the rest is left out. SP_OBS_JOIN says the observation is only
|
|
* " [exit status 0]" and belongs on the call's row; SP_OBS_NONE says there is
|
|
* nothing to show; SP_OBS_SHOWN means `out` is a block for the transcript.
|
|
*/
|
|
#define SP_OBS_NONE 0
|
|
#define SP_OBS_SHOWN 1
|
|
#define SP_OBS_JOIN 2
|
|
|
|
void sp_call_summary(const char *name, const char *args, char *out,
|
|
size_t cap);
|
|
int sp_obs_short(const char *obs, struct sp_buf *out);
|
|
|
|
#endif /* SPLINTER_ENGINE_H */
|