Konrad, 24.11.: "v1 geht durch ohne irgendwas. ich will auch keine approvals generell. ich will nur das der user mit ask nach sachen gefragt werden kann." Removed the whole approval apparatus, not just the prompt: - agent.c: the decision point (sp_host_gate/approve callbacks), the pause/resume path (has_pending, pending_from, sp_agent_resume), ack_approval, ask_operator and the ack stdin thread are gone. A tool runs; its exit status is the observation. sp_tool_policy (allow/deny per profile) stays - that is the operator's own configuration, not a question to the operator. - engine.c: SP_EV_APPROVAL, sp_engine_approve, the approve_pending condition variable and the `approval`/`approve` protocol commands are gone. - openai_server.c: the v1 endpoint runs gw_* tools without a gate; the autonomous flag is no longer part of sp_gateway_opts. - a2aserver.c: INPUT_REQUIRED and the resume-by-taskId path are gone; CancelTask stays. An A2A caller has no operator sitting in front of a prompt. - mcpserver.c: --autonomous no longer gates mutating tools; the flag and the config key are accepted for compatibility and documented as changing nothing. - telegram.c: tg_approve and the approval callback are gone; ask is the only dialog, and /cancel inside it still ends the turn. - front ends: protocol.c, curses.c, ui_local.c, ui_remote.c, client.c lose the approval event and the waiting flag. - ack.c stays: it is a plugin API (plugins/kids asks its parent through it), not splinter's own gate. --ack is documented as such. Tests: test_pause.c guarded the pause and now guards the opposite (a mutate and a destructive tool run, nothing is asked). test_a2a_input.sh, test_protocol.sh, test_ui.sh, test_agents_ui.sh, test_gateway.sh, test_gateway_openai.sh, test_agent.c and test_mcpsession.sh were rewritten the same way; splinter_approve must now be absent from the MCP tool list. Every rewritten test was run red against the old behaviour where that was possible. make check: all tests passed (428 s) make check-asan: all 47 test binaries clean make check-tsan: all 47 test binaries free of data races
333 lines
14 KiB
C
333 lines
14 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_answer(), 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 */
|
|
/*
|
|
* --claude/--opencode/--hermes: import the newest session that tool
|
|
* ran in this directory and continue that instead of a splinter one.
|
|
* from_foreign_id names one session instead of "the newest" - that is
|
|
* how the /sessions picker continues what the operator pointed at.
|
|
*/
|
|
const char *from_foreign;
|
|
const char *from_foreign_id;
|
|
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 */
|
|
/*
|
|
* The model asked the operator a question (the `ask` tool). n: the
|
|
* number of options, a: the text. The options are read with
|
|
* sp_engine_ask() when the front end draws the dialog; the answer goes
|
|
* back through sp_engine_answer().
|
|
*/
|
|
SP_EV_ASK,
|
|
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 */
|
|
/* the turn waits for an answer to the `ask` tool */
|
|
int asking;
|
|
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 *);
|
|
|
|
/*
|
|
* What the /sessions picker offers: the sources that have sessions at all,
|
|
* its own first. "splinter" is always there (this folder's session directory
|
|
* is what the run is); the other three only when their store answers with at
|
|
* least one session, so the picker never offers an empty source.
|
|
*/
|
|
struct sp_session_source {
|
|
char name[16]; /* "splinter", "claude", "hermes", "opencode" */
|
|
int count; /* 0 when the source could not be listed */
|
|
};
|
|
int sp_engine_sources(struct sp_engine *,
|
|
struct sp_session_source *v, size_t max);
|
|
|
|
/*
|
|
* The sessions of one source. For "splinter" that is the session directory;
|
|
* for the others the foreign plugin's list, and a row's `id` is then
|
|
* "claude:<id>" - what sp_engine_switch() takes to import and continue it.
|
|
* `title` is a label for the screen, not something to parse.
|
|
*/
|
|
int sp_engine_source_sessions(struct sp_engine *, const char *source,
|
|
void (*cb)(void *cbctx, const char *id, const char *title,
|
|
int current, long mtime), void *cbctx);
|
|
|
|
/* ------------------------------------------------------------------ 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);
|
|
/*
|
|
* The model asked the operator something (the `ask` tool). The turn thread
|
|
* parks on the engine's condition variable and the front end wakes it, so
|
|
* cancel and Ctrl-C behave the same; the answer is text: an option the
|
|
* operator picked, or words they typed.
|
|
*
|
|
* 1 while a question is pending; `text` is what was asked and up to `max`
|
|
* options are copied into `opts` (nopts may be NULL). Options are hints for a
|
|
* dialog; a front end that has no dialog asks the same question as a line and
|
|
* the answer is still valid text.
|
|
*/
|
|
int sp_engine_ask(struct sp_engine *, char *text, size_t textlen,
|
|
char (*opts)[SP_ASK_OPT_LEN], size_t max, size_t *nopts);
|
|
/* Answer it with free text or a picked option. 0 when nothing was pending. */
|
|
int sp_engine_answer(struct sp_engine *, const char *text);
|
|
|
|
/* ------------------------------------------------------------------ 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 */
|