Files
splinter/include/splinter_plugin.h
T
lotherkandClaude Opus 5.5 489c5da26a fixes from the review, second round: mcp client, kids rules, plugin life, ui
Each change has a test that fails against the code before it.

Also note: 8900d7f ("telegram: refuse to start without a token") swept in
work of this round that its message does not describe - an OLLAMA_KEY no longer
overwrites the key of the operator's last exit (config.c), parallel delegates
are killed and reaped after a timeout (delegate.c), lcm/cps never leave a tool
result without its call, cps keeps working past 256 messages, Telegram ids are
parsed as 64 bit, and 'run' also hides SPLINTER_OLLAMA_KEY_* and
SPLINTER_MCP_TOKEN.

MCP client (plugins/mcp)
- fgets into char[16384] split every longer reply into unparseable pieces; a
  big tools/list or file read never arrived. A reply sharing one write with a
  notification sat in stdio's buffer, invisible to poll(). Lines are now
  buffered on the fd (up to 16 MiB). An error is the top-level "error" member,
  not the text "error" anywhere in the line. test_mcpclient.

kids
- Rules were matched against the raw JSON, so "run ls*" never matched. They
  now see the command, path or url. A 'run' command that can chain, substitute
  or redirect is never whitelisted. The glob is linear instead of exponential.
  test_kids, which also covers the approval-command quoting and the domain
  checks from the first round.

Plugin life (src/plugin.c, ABI 2)
- An init() that failed kept what it had registered (tools, gate, widget
  threads); it is now rolled back. shutdown() was never called; the host calls
  it, newest plugin first, after the widget threads have stopped.
- A widget slot is filled before it is counted, and the count is read under the
  lock, so tick threads never see a half-made slot.
- SP_PLUGIN_ABI is 2: struct sp_gate grew block_turn while the ABI stayed 1.
  test_lifecycle, with tests/plugin_lifecycle.so.

UI
- Ctrl-C and Ctrl-D cancel under the lock and wake a turn waiting for approval
  (it waited forever); Ctrl-D cancelled nothing before the join.
- A steer the turn already took is not sent again as the next message.
- KEY_RESIZE re-cuts the windows; Ctrl-L repaints instead of sending the
  half-typed line; key codes above 255 are not typed into the input.
- Markdown attributes went to stdscr while the text went to ui_hist: bold and
  code never showed. They now build on the line's role colour.
  Checked in tmux: UTF-8 input, resize to 70x20, Ctrl-L, Ctrl-D, bold.

Telegram
- Replies are split on UTF-8 boundaries; a cut umlaut made Telegram refuse the
  chunk and the rest of the answer was lost.

Build
- Seven plugins linked with an undefined $(PLUGIN_LDFLAGS): no rpath, no LIBS.
  All eleven now carry $ORIGIN/../..; objects depend on the interface headers.

Seen, not changed: sp_session_append fsyncs every line; under disk load an
answer appears up to 15 s late. Not a deadlock, and the same before this round.

make check: 26 test binaries, green, also under ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 20:50:33 +00:00

189 lines
7.4 KiB
C

/*
* splinter -- plugin ABI.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* A plugin is a shared object loaded with dlopen(). It links against
* libsplinter.so, never against the binary, so there is exactly one copy of
* every symbol in the process.
*
* What a plugin may do, and where:
*
* init() called once, on the main thread, before any turn. Register
* tools and widgets here. May fail; a failed plugin is unloaded
* from the set and reported, never half-used.
* run() called on the main thread while a turn is being handled.
* tick() called on the widget's own thread, so it may block, read
* files, wait on sockets. It must NOT touch curses and must NOT
* assume it is the only thread.
* draw() called on the UI thread only, must be fast and non-blocking.
* A widget without draw() is rendered from its published text.
*
* Text that should appear in the sidebar is published with
* sp_host_widget_text(); the host copies it under a mutex, so the string does
* not have to outlive the call.
*/
#ifndef SPLINTER_PLUGIN_H
#define SPLINTER_PLUGIN_H
#include <stdint.h>
#include "splinter.h"
/*
* Raise this whenever a struct a plugin hands to the host changes shape. It
* stayed 1 while struct sp_gate gained block_turn: a gate built against the old
* header would have had the host read past its struct and call what it found.
* 2: sp_gate.block_turn, sp_task_ops, sp_status, commands and transports.
*/
#define SP_PLUGIN_ABI 2u
struct sp_host;
struct sp_widget {
const char *name; /* unique in the sidebar */
int (*tick)(struct sp_host *, const char *name, void *ctx);
void (*draw)(struct sp_host *, const char *name, void *ctx);
void *ctx;
unsigned interval_ms; /* 0: tick once, then stop */
};
/*
* A compactor runs on the main thread before every request, with the whole
* conversation in hand. It may rewrite it. Returning 1 means "changed", 0
* "nothing to do", -1 "failed" - and a failure is reported, never ignored.
*/
struct sp_compactor {
const char *name;
int (*compact)(struct sp_host *, struct sp_conv *,
size_t budget_bytes, unsigned turn);
};
/*
* A command is what the operator types with a leading slash: /goal <text>.
* The UI does not know any command by name; it asks the host, and the host asks
* the plugins. A plugin that wants one registers it here.
*
* Return 0 and put the reply in `out`; return -1 to say "not mine".
*/
typedef int (*sp_cmd_fn)(struct sp_host *, const char *args, struct sp_buf *out,
void *ctx);
/*
* A gate decides whether a tool may run, before the operator is asked and before
* the authorization class is applied. It exists so a plugin can hold a policy the
* core does not know: kid mode, a sandbox, a host-level allow list.
*
* Return 1 to allow (skips the operator prompt), 0 to deny, -1 to say "no
* opinion" - then the normal rules apply.
*/
struct sp_gate {
const char *name;
int (*check)(struct sp_host *, const char *tool,
int auth, const char *args, char *reason,
size_t reason_len);
/*
* Optional: refuse the whole turn before the model is called. Used by kid
* mode when the day's minutes are gone - a message the child reads, not a
* hang, and not a model that has to be trusted to obey.
*/
int (*block_turn)(struct sp_host *, char *msg, size_t len);
};
/*
* What the status line is allowed to know. The harness owns the numbers - a
* plugin must not count tokens twice or guess a price.
*/
struct sp_meter {
size_t ctx_used; /* tokens the last request carried */
size_t ctx_max; /* the window, 0 = unknown */
size_t tok_in; /* this session */
size_t tok_out;
size_t cached; /* tokens the provider served from cache */
long tps; /* output tokens per second, last turn */
double cost; /* session cost, filled from the price config */
unsigned tasks_running;
unsigned tasks_max;
};
/* A status segment: a name and a renderer, drawn in the order registered. */
struct sp_status {
const char *name;
void (*render)(struct sp_host *, char *buf, size_t len);
};
/*
* Tasks owned by someone else. The delegate runs a model on another host and the
* tasks plugin shows it; neither knows the other, so the harness passes an entry
* through this little interface: begin gives an id, end closes it with a status
* and whatever was spent.
*/
struct sp_task_ops {
int (*begin)(struct sp_host *, const char *label, const char *detail);
void (*end)(struct sp_host *, int id, int status, size_t tokens);
};
struct sp_plugin {
uint32_t abi; /* must equal SP_PLUGIN_ABI */
const char *name;
const char *version;
int (*init)(struct sp_host *);
int (*run)(struct sp_host *, const char *input,
struct sp_buf *out);
void (*shutdown)(struct sp_host *);
};
/* Every plugin exports exactly this. */
const struct sp_plugin *sp_plugin_describe(void);
/* ---- host API, implemented by splinter, called by plugins ---- */
uint32_t sp_host_abi(struct sp_host *);
int sp_host_add_tool(struct sp_host *, const struct sp_tool *);
int sp_host_add_widget(struct sp_host *, const struct sp_widget *);
int sp_host_widget_text(struct sp_host *, const char *widget,
const char *text);
int sp_host_note(struct sp_host *, const char *text);
int sp_host_add_compactor(struct sp_host *, const struct sp_compactor *);
int sp_host_add_gate(struct sp_host *, const struct sp_gate *);
int sp_host_add_status(struct sp_host *, const struct sp_status *);
const struct sp_meter *sp_host_meter(struct sp_host *);
void sp_host_set_meter(struct sp_host *, const struct sp_meter *);
size_t sp_host_status_text(struct sp_host *, char *buf, size_t len);
void sp_host_set_task_ops(struct sp_host *, const struct sp_task_ops *);
int sp_host_task_begin(struct sp_host *, const char *label, const char *detail);
void sp_host_task_end(struct sp_host *, int id, int status, size_t tokens);
/* Ask every gate. Returns 1 allowed, 0 denied (reason filled), -1 no opinion. */
int sp_host_gate(struct sp_host *, const char *tool, int auth,
const char *args, char *reason, size_t reason_len);
int sp_host_turn_blocked(struct sp_host *, char *msg, size_t len);
int sp_host_add_command(struct sp_host *, const char *name, sp_cmd_fn,
void *ctx);
int sp_host_command(struct sp_host *, const char *name, const char *args,
struct sp_buf *out);
size_t sp_host_command_help(struct sp_host *, struct sp_buf *out);
int sp_host_add_transport(struct sp_host *, const char *name,
struct sp_transport);
struct sp_transport *sp_host_transport(struct sp_host *, const char *name);
int sp_host_compact(struct sp_host *, struct sp_conv *, size_t budget);
size_t sp_host_compaction_stats(struct sp_host *, size_t *compactions,
size_t *failed, unsigned *turns);
void sp_host_turn(struct sp_host *);
/* The loaded configuration, so plugins (the exit ring) can read it. */
const void *sp_host_config(struct sp_host *);
void sp_host_set_config(struct sp_host *, const void *);
struct sp_registry *sp_host_registry(struct sp_host *);
/* ---- host side, implemented by splinter, called by the harness ---- */
struct sp_host *sp_host_new(struct sp_registry *, struct sp_agent *,
struct sp_session_info *);
void sp_host_free(struct sp_host *);
int sp_plugin_load(struct sp_host *, const char *path,
struct sp_buf *err);
size_t sp_host_widget_snapshot(struct sp_host *, char *buf, size_t len);
size_t sp_host_widget_count(struct sp_host *);
#endif /* SPLINTER_PLUGIN_H */