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.
285 lines
12 KiB
C
285 lines
12 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 interview; /* the model interviews the user */
|
|
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;
|
|
double cost; /* provider-reported, USD */
|
|
size_t ctx_used; /* prompt tokens of the last request */
|
|
size_t ctx_max; /* context window, 0 = unknown */
|
|
size_t cached; /* of ctx_used, served from the provider cache */
|
|
size_t cached_total; /* of tokens_in, the whole session */
|
|
size_t turn_tokens_out; /* output tokens of this/last turn */
|
|
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 *);
|
|
|
|
/*
|
|
* The choices /model takes - every provider, then every pool - with the model
|
|
* as the label and the current one marked. Configuration, not a network probe:
|
|
* a picker must open at once. Returns how many were written.
|
|
*/
|
|
size_t sp_engine_models(struct sp_engine *, struct sp_model_choice *,
|
|
size_t max);
|
|
/*
|
|
* The models the endpoint this session is on reports at /v1/models, as
|
|
* selectors /model accepts. This talks to the network; the picker asks for it
|
|
* after the configured list is already on screen.
|
|
*/
|
|
size_t sp_engine_served_models(struct sp_engine *, struct sp_model_choice *,
|
|
size_t max);
|
|
|
|
/* -------------------------------------------------------------- lifecycle */
|
|
|
|
int sp_engine_open(struct sp_engine **, const struct sp_engine_opts *,
|
|
struct sp_buf *err);
|
|
void sp_engine_close(struct sp_engine *);
|
|
/*
|
|
* Move an engine to another session: close the old engine, open the new one
|
|
* from the same options, replace *ep. `id` is a session id, "new" for a fresh
|
|
* one, or NULL/"last" for the newest. The old engine is closed first because
|
|
* plugins keep process globals; a failed open reopens the session the engine
|
|
* was on, so the pointer is only NULL when even that fails. Returns 0 or -1
|
|
* with `err` set.
|
|
*/
|
|
int sp_engine_switch(struct sp_engine **ep, const struct sp_engine_opts *,
|
|
const char *id, struct sp_buf *err);
|
|
|
|
/* 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 *);
|
|
|
|
/*
|
|
* Work on the engine's agent outside its turn thread. A daemon's Telegram
|
|
* turn runs sp_agent_run() on its own thread with the engine's agent:
|
|
* sp_engine_turn_begin() before it, sp_engine_turn_end() after; -1 means
|
|
* busy (a turn or a session tool owns the agent) and nothing was taken.
|
|
* While held, sp_engine_busy() says 1. The _agent_ forms find the engine
|
|
* that owns the agent (0 and a no-op for an agent no engine owns).
|
|
* sp_engine_hold()/release() is the short form for one tool call: exclusive
|
|
* like a turn, refused while one runs, not reported as busy.
|
|
*/
|
|
int sp_engine_turn_begin(struct sp_engine *);
|
|
void sp_engine_turn_end(struct sp_engine *);
|
|
int sp_engine_agent_turn_begin(struct sp_agent *);
|
|
void sp_engine_agent_turn_end(struct sp_agent *);
|
|
int sp_engine_hold(struct sp_engine *);
|
|
void sp_engine_release(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 */
|