A review found the tool classes - the core of the harness - bypassable in
several places where model output was pasted into a shell or trusted as an id.
Each fix below has a test that reproduces the original failure.
No shell for model data
- webfetch put the URL between '...' and ran it through popen: a quote in the
URL ran any command, from a tool that needed no consent. curl is now exec'd
with an argv, http(s) only, HTML stripped in C, stderr off the curses screen.
- webfetch is mutate: with read free, "read a key, fetch evil/?k=..." left the
machine without anybody being asked. websearch stays read (fixed host).
- kids: the parent-approval command got the model's arguments inside '...'
via system(); they are now "$1". The domain check reads the URL with the
same parser as the tool (duplicate keys) and refuses user@host URLs.
- delegate: the two copies of the child argv are one (exec_delegate). The ssh
path exec'd splinter with "ssh" as argv[0]; it now runs ssh and quotes every
word for the remote shell. A single delegate to a host now really runs there.
Approvals (ack)
- The id was the pid: a late yes stayed on disk and approved the next request
unseen. Ids are random, requests 0600 with O_EXCL, deleted when answered or
expired; an answer is refused after expiry; the first answer wins (link(2)).
- An ack dir owned by someone else or writable by others is refused.
- --ack accepted every word but "no" as yes ("nein" approved). Now strict.
- The terminal thread blocked in fgets after a phone answer; it is stoppable.
Secrets
- API key and Telegram token were in curl's argv, readable via ps. src/curl.c
passes URL and auth headers as a -K config on stdin (transport, telegram,
tg-ask.sh; its dead eval fallback is gone).
- 'run' and task_start no longer inherit splinter's keys ("run env"). 'run' got
its own process group, stdin /dev/null and SPLINTER_RUN_TIMEOUT (300 s).
MCP server
- Beyond 127.x it refuses to start without SPLINTER_MCP_TOKEN; connections
must begin with "AUTH <token>". SIGPIPE ignored, idle connections closed.
Crashes
- task_kill sent kill(-0) - SIGTERM to splinter's own process group - because
popen gave no pid. Tasks now have a pid and a process group; integer ids work.
- ui: a 3- or 4-byte UTF-8 character overflowed char[2] on every redraw
(now sp_ui_ascii_char, tested); the scroll offset was taken from one buffer
and applied to another, reading past its end.
Repo
- The test binaries are no longer tracked (.gitignore already said so).
make check: 23 test binaries, green, also under ASan/UBSan.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
232 lines
8.9 KiB
Markdown
232 lines
8.9 KiB
Markdown
# Plugins
|
|
|
|
A splinter plugin is a shared object that links against `libsplinter.so` and
|
|
exports one symbol:
|
|
|
|
```c
|
|
const struct sp_plugin *sp_plugin_describe(void);
|
|
```
|
|
|
|
The struct, the ABI number and the host functions are in
|
|
`include/splinter_plugin.h`. Nothing else is stable. `SP_PLUGIN_ABI` changes
|
|
whenever that header changes in a way that breaks an old plugin, and a plugin
|
|
with the wrong number is refused at load time with the two numbers in the
|
|
message.
|
|
|
|
## Build
|
|
|
|
```sh
|
|
cc -std=c99 -shared -fPIC -o myplugin.so myplugin.c -L. -lsplinter \
|
|
-Wl,-rpath,'$ORIGIN'
|
|
```
|
|
|
|
`splinter --plugin ./myplugin.so --chat` loads it before the first turn. A
|
|
plugin that fails to load is reported on stderr and skipped; the harness keeps
|
|
working.
|
|
|
|
## What a plugin may do
|
|
|
|
| hook | thread | may block | may touch curses |
|
|
|------------|---------------|-----------|------------------|
|
|
| `init()` | main | no | no |
|
|
| `run()` | main | yes | no |
|
|
| `tick()` | its own | yes | no |
|
|
| `draw()` | main (UI) | no | no |
|
|
|
|
Streaming is a transport concern, not a plugin one: `sp_transport.send_stream`
|
|
is optional, and a transport that cannot stream leaves it NULL — the loop then
|
|
falls back to the one-shot call.
|
|
|
|
`tick()` runs on a thread the host creates and can stop. It may read files,
|
|
wait on sockets, sleep. It must not assume it is the only thread.
|
|
|
|
Anything that should appear in the sidebar is published:
|
|
|
|
```c
|
|
sp_host_widget_text(host, "mywidget", "42 items");
|
|
```
|
|
|
|
The host copies the string under a mutex, so a stack buffer is fine and the
|
|
worker never hands the UI a pointer it could free.
|
|
|
|
## Widgets
|
|
|
|
```c
|
|
static const struct sp_widget w = {
|
|
.name = "clock",
|
|
.tick = clock_tick, /* worker thread, publishes text */
|
|
.draw = NULL, /* NULL: render the published text */
|
|
.ctx = NULL,
|
|
.interval_ms = 1000, /* 0: tick once */
|
|
};
|
|
sp_host_add_widget(host, &w);
|
|
```
|
|
|
|
The sidebar lists one line per widget, `name: text`. `draw()` exists for
|
|
widgets that need more than one line; it runs on the UI thread and must be
|
|
fast, because it holds up every other pane while it runs.
|
|
|
|
## Threads, honestly
|
|
|
|
- One thread per widget with a `tick`. A widget without `tick` gets no thread.
|
|
- The host joins every thread in `sp_host_free()`, setting a stop flag first.
|
|
A `tick` that never returns therefore hangs the exit; that is a bug in the
|
|
plugin, not something the host can fix from outside.
|
|
- No plugin is `dlclose()`d. Unmapping code that a thread might still be
|
|
inside is how a harness dies in a way nobody can debug.
|
|
- The host pointer is thread-safe only for `sp_host_widget_text()` and
|
|
`sp_host_note()`. Everything else belongs to the main thread. That is a
|
|
rule, not an accident.
|
|
|
|
## Tools
|
|
|
|
A plugin registers tools with the same authorization classes the builtins use:
|
|
|
|
```c
|
|
struct sp_tool t = { 0 };
|
|
t.name = "clock";
|
|
t.description = "Report the local date and time.";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = clock_tool;
|
|
sp_host_add_tool(host, &t);
|
|
```
|
|
|
|
`SP_AUTH_READ` runs. `SP_AUTH_MUTATE` and `SP_AUTH_DESTRUCTIVE` ask the
|
|
operator unless the harness was started with `--autonomous`. The loop decides
|
|
this, not the plugin.
|
|
|
|
## Compaction
|
|
|
|
A compactor runs on the main thread before every request, with the whole
|
|
conversation in hand, and may rewrite it:
|
|
|
|
```c
|
|
struct sp_compactor {
|
|
.const char *name;
|
|
int (*compact)(struct sp_host *, struct sp_conv *, size_t budget_bytes,
|
|
unsigned turn);
|
|
};
|
|
```
|
|
|
|
Return 1 when it changed something, 0 when there was nothing to do, -1 on
|
|
failure. A failure is counted and the turn continues: a broken compactor must
|
|
not take the conversation with it.
|
|
|
|
Two come with the tree.
|
|
|
|
**lcm** — deterministic, lossy, honest. If the conversation does not fit the
|
|
budget, it keeps the leading system message and the last six messages, and puts
|
|
one system message in between that says how many messages went, of which roles,
|
|
and quotes the head of the oldest dropped turn. No second model is called, so a
|
|
run stays reproducible and costs nothing extra.
|
|
|
|
**cps** — continuous parallel compression. The expensive part (deciding what
|
|
would go) is computed on the widget's worker thread from an *index* of the
|
|
conversation — role, size, and 64 bytes of head per message, copied under a
|
|
mutex. The worker never touches the conversation itself. When the budget is
|
|
hit, the main thread splices an already-prepared plan in one pass. Its sidebar
|
|
line shows `idle (applied N, prepared M)` or `plan ready: cut X/Y, A -> B B`,
|
|
so "continuous" is visible rather than claimed.
|
|
|
|
The limit is stated in both files: they can decide *which* messages go, not
|
|
summarise their text, because summarising needs the text and the text is not
|
|
the worker's to read.
|
|
|
|
## Transports
|
|
|
|
A transport turns a conversation into an answer. The built-in one speaks
|
|
OpenAI's `chat/completions`; a plugin can provide others:
|
|
|
|
```c
|
|
struct sp_transport tr = { my_send, my_ctx };
|
|
sp_host_add_transport(host, "cli", tr);
|
|
```
|
|
|
|
then `splinter --plugin ... --transport cli`.
|
|
|
|
**cli** ships with the tree and makes *any program* the model — `claude -p`,
|
|
`opencode run`, `ollama run`, a shell script. Each brings its own keys, its own
|
|
quota, its own free tier:
|
|
|
|
```sh
|
|
SPLINTER_CLI_CMD="claude -p" ./splinter -p "hi" --plugin plugins/cli/cli.so --transport cli
|
|
SPLINTER_CLI_CMD="opencode run" ./splinter -p "hi" --plugin plugins/cli/cli.so --transport cli
|
|
```
|
|
|
|
- `SPLINTER_CLI_CMD` — the program, default `claude -p`
|
|
- `SPLINTER_CLI_FULL=1` — send the whole transcript instead of the last user
|
|
message (for programs that keep no history of their own)
|
|
- `SPLINTER_CLI_TIMEOUT` — seconds, default 600
|
|
|
|
The command comes from the operator's environment and runs through `/bin/sh -c`;
|
|
it is never built from model output. Output is capped at 4 MiB, a non-zero exit
|
|
returns the program's own stderr, and a program that prints nothing is an error
|
|
— an empty answer must never look like a real one.
|
|
|
|
## focus
|
|
|
|
Long conversations drift: ten turns in, the model is deep in a detail and the
|
|
reason the session exists is somewhere behind it. `focus` puts the goal back on
|
|
the table.
|
|
|
|
```
|
|
/goal splinter zu einem agent-harness machen, das hermes ersetzt
|
|
```
|
|
|
|
`/goal <text>` sets it, `/goal` shows it, `/recap` forces a reminder into the
|
|
next turn. That works in the TUI and in a one-shot run
|
|
(`splinter -p "/goal ..."`); it is deliberately not a config key, because a goal
|
|
belongs to the conversation in front of you and not to a file that outlives it.
|
|
`SPLINTER_FOCUS_EVERY` sets the interval (default 5 turns).
|
|
|
|
It registers as a compactor and, on every fifth turn, inserts one system
|
|
message — *before* the newest user message, so the model reads goal then request
|
|
and answers the request. Never twice in one turn, and nothing at all when no
|
|
goal is set. The sidebar shows the goal and how many reminders went out.
|
|
|
|
It is a reminder, not an instruction: the user can change direction at any
|
|
time, and the message says so on its face.
|
|
|
|
## Zustimmung (ack)
|
|
|
|
Ein Werkzeug, das etwas aendert, braucht ein Ja von einem Menschen. Der Mensch
|
|
kann am Terminal sitzen, am Telefon, oder hinter einem Skript - es koennen
|
|
mehrere Kanaele sein, und die erste Antwort gewinnt.
|
|
|
|
Der Mechanismus ist zwei Dateien, damit nicht zwei Prozesse um dieselben
|
|
Telegram-Updates streiten:
|
|
|
|
~/.splinter/ack/pending/<id> die Anfrage (0600)
|
|
~/.splinter/ack/pending/<id>.answer die Antwort (0600)
|
|
|
|
Der blockierte Prozess wartet nur auf die Datei. Kanaele sind Melder: sie stossen
|
|
die Anfrage nach draussen (ein Kommando pro Kanal), und wer die Antwort liest -
|
|
das Gateway, ein Telefon, ein Skript - legt die Antwortdatei ab.
|
|
|
|
SPLINTER_ACK_CHANNELS="telegram-ask,mastodon-ask" # Kommandos, Komma-getrennt
|
|
SPLINTER_ACK_TTL=120 # Sekunden, Default 120
|
|
SPLINTER_ACK_DIR=~/.splinter/ack # optional
|
|
SPLINTER_ACK_BY=konrad # wer antwortet (beim --ack)
|
|
|
|
Antworten, aus jedem Prozess:
|
|
|
|
splinter --ack 482915307 yes # yes, y, ok, ja
|
|
splinter --ack 482915307 no # no, n, nein - jedes andere Wort ist ein Fehler
|
|
splinter --ack-list
|
|
|
|
Ein Timeout ist ein Nein. Stille ist keine Zustimmung.
|
|
|
|
Regeln, die der Code durchsetzt:
|
|
|
|
- Die Id ist zufaellig (6-9 Ziffern) und wird nie wiederverwendet; sie ist nicht
|
|
die pid. Eine Antwort gilt genau einer Anfrage.
|
|
- Nur die erste Antwort zaehlt; eine zweite wird abgewiesen.
|
|
- Ist die Anfrage beantwortet oder abgelaufen, wird sie geloescht. Eine spaete
|
|
Antwort scheitert mit "no open request", statt auf die naechste Anfrage zu
|
|
warten.
|
|
- Ein ack-Verzeichnis, das nicht dem Benutzer gehoert oder fuer andere
|
|
schreibbar ist, wird abgelehnt: dort koennte jeder ein Ja hinlegen.
|
|
- Grenze: Jeder Prozess unter derselben uid kann antworten. Wer das Kind am
|
|
selben Konto vom Zustimmen abhalten will, braucht eine eigene uid fuer splinter
|
|
(oder einen Kanal, dessen Antwort der lokale Benutzer nicht schreiben kann).
|