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>
314 lines
11 KiB
C
314 lines
11 KiB
C
/*
|
|
* splinter -- a small C99/POSIX agent harness.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Design rules this header encodes:
|
|
* - no fixed-size buffers anywhere; every growing thing is a struct sp_buf
|
|
* with geometric growth and overflow checks;
|
|
* - a tool declares its authorization class; the harness decides whether it
|
|
* may run, never the model and never a prompt;
|
|
* - a session is append-only JSONL, so a crash can only truncate its tail;
|
|
* - the transport is a vtable, so the whole loop is testable with no network.
|
|
*/
|
|
#ifndef SPLINTER_H
|
|
#define SPLINTER_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#define SP_VERSION "0.1.0"
|
|
|
|
/* ------------------------------------------------------------------ buffer */
|
|
|
|
struct sp_buf {
|
|
char *p;
|
|
size_t len; /* bytes used, excluding the NUL we keep at p[len] */
|
|
size_t cap; /* allocated bytes, always > len */
|
|
};
|
|
|
|
int sp_buf_init(struct sp_buf *);
|
|
int sp_buf_add(struct sp_buf *, const char *, size_t);
|
|
int sp_buf_puts(struct sp_buf *, const char *);
|
|
int sp_buf_printf(struct sp_buf *, const char *, ...);
|
|
int sp_buf_reserve(struct sp_buf *, size_t);
|
|
void sp_buf_clear(struct sp_buf *);
|
|
void sp_buf_free(struct sp_buf *);
|
|
|
|
/* --------------------------------------------------------------- escaping */
|
|
|
|
/* Append s as a JSON string body (no surrounding quotes). */
|
|
int sp_json_escape(struct sp_buf *, const char *);
|
|
|
|
/* --------------------------------------------------------------- tools */
|
|
|
|
enum sp_auth {
|
|
SP_AUTH_READ = 0, /* may run without asking */
|
|
SP_AUTH_MUTATE, /* changes files or sends data out: consent */
|
|
SP_AUTH_DESTRUCTIVE /* runs commands: needs operator consent */
|
|
};
|
|
|
|
struct sp_tool;
|
|
|
|
/* Return 0 on success. The observation is written into out. */
|
|
typedef int (*sp_tool_run)(const char *args_json, struct sp_buf *out,
|
|
void *ctx);
|
|
|
|
struct sp_tool {
|
|
const char *name;
|
|
const char *description;
|
|
const char *params; /* JSON schema of the arguments, may be NULL */
|
|
enum sp_auth auth;
|
|
sp_tool_run run;
|
|
void *ctx;
|
|
};
|
|
|
|
struct sp_registry {
|
|
struct sp_tool *tools;
|
|
size_t len;
|
|
size_t cap;
|
|
void *ctx; /* the builtin that owns the tools, if any */
|
|
};
|
|
|
|
int sp_registry_add(struct sp_registry *, const struct sp_tool *);
|
|
const struct sp_tool *sp_registry_find(const struct sp_registry *,
|
|
const char *);
|
|
size_t sp_registry_count(const struct sp_registry *);
|
|
void sp_registry_free(struct sp_registry *);
|
|
|
|
/* ---------------------------------------------------------- conversation */
|
|
|
|
struct sp_msg {
|
|
char *role; /* system | user | assistant | tool */
|
|
char *name; /* tool name when role is "tool" */
|
|
char *call_id; /* tool_call id, when role is "tool" */
|
|
char *content;
|
|
char *calls; /* raw tool_calls JSON of an assistant turn */
|
|
};
|
|
|
|
struct sp_conv {
|
|
struct sp_msg *v;
|
|
size_t n;
|
|
size_t cap;
|
|
};
|
|
|
|
void sp_conv_init(struct sp_conv *);
|
|
void sp_conv_free(struct sp_conv *);
|
|
int sp_conv_add(struct sp_conv *, const struct sp_msg *);
|
|
int sp_conv_insert(struct sp_conv *, size_t at, const struct sp_msg *);
|
|
int sp_conv_push(struct sp_conv *, const char *role, const char *name,
|
|
const char *content);
|
|
size_t sp_conv_bytes(const struct sp_conv *);
|
|
int sp_conv_trim(struct sp_conv *, size_t keep_last);
|
|
|
|
/* ------------------------------------------------------------- transport */
|
|
|
|
struct sp_toolcall {
|
|
char *id;
|
|
char *name;
|
|
char *args; /* the arguments as a JSON text */
|
|
};
|
|
|
|
struct sp_reply {
|
|
long status;
|
|
long tokens_in; /* provider-reported, 0 when unknown */
|
|
long tokens_out;
|
|
char *content; /* assistant text, may be "" */
|
|
struct sp_toolcall *calls;
|
|
char *calls_json; /* the provider's own tool_calls array */
|
|
size_t ncalls;
|
|
};
|
|
|
|
/*
|
|
* A transport turns a whole conversation into one request and one reply. The
|
|
* seam is the conversation, not a raw string: a provider adapter cannot invent
|
|
* history, and the loop cannot depend on a vendor's JSON.
|
|
*/
|
|
/*
|
|
* Streaming: a transport that can deliver a turn incrementally reports text
|
|
* deltas and tool-call starts through this callback while it reads. A transport
|
|
* that cannot simply leaves send_stream NULL and the loop uses send().
|
|
*/
|
|
enum {
|
|
SP_STREAM_TEXT = 1, /* a chunk of the assistant's answer */
|
|
SP_STREAM_TOOL, /* a tool call has begun: text is its name */
|
|
SP_STREAM_THINK /* the model is reasoning: show it, do not use it */
|
|
};
|
|
|
|
typedef void (*sp_stream_cb)(void *ctx, int kind, const char *text);
|
|
|
|
struct sp_transport {
|
|
int (*send)(void *ctx, const struct sp_conv *, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
int (*send_stream)(void *ctx, const struct sp_conv *,
|
|
struct sp_reply *out, struct sp_buf *err,
|
|
sp_stream_cb, void *cbctx);
|
|
void *ctx;
|
|
};
|
|
|
|
void sp_reply_free(struct sp_reply *);
|
|
|
|
/* A deterministic in-process transport: replies are queued FIFO. */
|
|
struct sp_mock {
|
|
struct sp_reply *queue;
|
|
size_t head;
|
|
size_t len;
|
|
size_t cap;
|
|
struct sp_buf last_request;
|
|
size_t requests;
|
|
};
|
|
|
|
int sp_mock_init(struct sp_mock *);
|
|
int sp_mock_push(struct sp_mock *, long status, const char *body);
|
|
void sp_mock_free(struct sp_mock *);
|
|
struct sp_transport sp_mock_transport(struct sp_mock *);
|
|
|
|
/* --------------------------------------------------------------- session */
|
|
|
|
struct sp_session_info {
|
|
char id[64];
|
|
char dir[512];
|
|
};
|
|
|
|
int sp_session_open(struct sp_session_info *, const char *dir,
|
|
const char *id, struct sp_buf *err);
|
|
int sp_session_append(const struct sp_session_info *, const char *role,
|
|
const char *text, struct sp_buf *err);
|
|
int sp_session_count(const struct sp_session_info *, size_t *n,
|
|
struct sp_buf *err);
|
|
int sp_session_latest(const char *dir, char *id, size_t cap);
|
|
/* The newest session started in this directory - what '-c' means. */
|
|
int sp_session_last_for(const char *dir, const char *cwd, char *id,
|
|
size_t cap);
|
|
/* What happened in a directory: sessions, turns, last question. */
|
|
int sp_session_folder_history(const char *dir, const char *cwd,
|
|
unsigned limit, struct sp_buf *out);
|
|
int sp_session_load(const struct sp_session_info *, struct sp_conv *conv,
|
|
size_t *loaded, struct sp_buf *err);
|
|
|
|
/* ----------------------------------------------------------------- agent */
|
|
|
|
struct sp_agent_config {
|
|
size_t max_steps; /* tool rounds per turn */
|
|
size_t max_bytes; /* request budget */
|
|
const char *system_prompt;
|
|
int autonomous; /* 1: run mutate tools without asking */
|
|
FILE *approval_in; /* NULL with !autonomous means deny */
|
|
FILE *approval_out;
|
|
/*
|
|
* UI path for approval: when set, it is asked instead of the streams, so a
|
|
* curses program can put the question on the screen instead of reading
|
|
* stdin behind its own back. Returns 1 to allow, 0 to deny.
|
|
*/
|
|
int (*approve)(void *ctx, const char *name, int auth,
|
|
const char *args);
|
|
void *approve_ctx;
|
|
|
|
/*
|
|
* Steering: while a turn runs, the operator may type. The UI cannot
|
|
* interrupt a request that is already on the wire, so the text is picked
|
|
* up at the next step boundary -- stated here, not discovered later.
|
|
* `cancel` is read before every request; `steer` is asked for text.
|
|
*/
|
|
volatile int *cancel;
|
|
int (*steer)(void *ctx, struct sp_buf *out);
|
|
void *steer_ctx;
|
|
|
|
/* Called before a tool runs, with its name and its arguments as sent. */
|
|
void (*tool_begin)(void *ctx, const char *name,
|
|
const char *args);
|
|
void *tool_begin_ctx;
|
|
/* Called after a tool ran, with its observation. */
|
|
void (*tool_end)(void *ctx, const char *name,
|
|
const char *observation);
|
|
void *tool_end_ctx;
|
|
|
|
/* incremental output, if the transport and the caller both support it */
|
|
sp_stream_cb stream;
|
|
void *stream_ctx;
|
|
};
|
|
|
|
struct sp_agent_stats {
|
|
size_t steps;
|
|
size_t tokens_in;
|
|
size_t tokens_out;
|
|
size_t compactions;
|
|
size_t tool_calls;
|
|
size_t denied;
|
|
size_t bytes_sent;
|
|
};
|
|
|
|
struct sp_host; /* plugin host, see splinter_plugin.h */
|
|
|
|
struct sp_agent {
|
|
struct sp_registry *reg;
|
|
struct sp_host *host; /* optional: compactors live there */
|
|
struct sp_transport tr;
|
|
const struct sp_agent_config *cfg;
|
|
struct sp_session_info *sess;
|
|
struct sp_conv *conv; /* optional: history lives here */
|
|
struct sp_agent_stats stats;
|
|
};
|
|
|
|
int sp_agent_run(struct sp_agent *, const char *user_text,
|
|
struct sp_buf *answer, struct sp_buf *err);
|
|
|
|
/* builtin.c */
|
|
int sp_builtin_register(struct sp_registry *);
|
|
/*
|
|
* Environment names the 'run' tool's shell does not inherit (API keys,
|
|
* tokens). A trailing '*' makes a prefix. sp_secret_env_is takes "NAME=value".
|
|
*/
|
|
int sp_secret_env_add(const char *name);
|
|
int sp_secret_env_is(const char *entry);
|
|
struct sp_config;
|
|
int sp_delegate_register(struct sp_registry *, const struct sp_config *,
|
|
struct sp_host *);
|
|
/* Gateway mode: answers on Telegram until SIGINT/SIGTERM. */
|
|
struct sp_host;
|
|
int sp_telegram_run(struct sp_agent *, struct sp_host *host,
|
|
const char *token, const char *allow, struct sp_buf *err);
|
|
int sp_telegram_token(char *buf, size_t len, const char *path);
|
|
const char *sp_memory_user(void);
|
|
/*
|
|
* Slash commands that are programs: <session dir>/commands/<name>. The program
|
|
* gets the rest of the line, and its output becomes context for the model.
|
|
*/
|
|
int sp_slash_script(const char *dir, const char *name, const char *args,
|
|
struct sp_buf *out, struct sp_buf *err);
|
|
int sp_delegate_prio(const struct sp_config *, const char *id);
|
|
int sp_delegate_choose(const struct sp_config *, const char *task,
|
|
const char *tags, char *id, size_t cap, char *why, size_t whylen,
|
|
int *score);
|
|
|
|
/* transport_openai.c -- OpenAI-compatible chat completions, via curl(1) */
|
|
/* The turn's cancellation flag; set by the UI before a turn, cleared after. */
|
|
void sp_openai_set_cancel(const volatile int *flag);
|
|
|
|
struct sp_transport sp_openai_transport(const char *url, const char *model,
|
|
const char *api_key, const struct sp_registry *, long timeout_s);
|
|
|
|
/* openai.c -- pure JSON shaping, testable without a network */
|
|
int sp_openai_request(const struct sp_conv *, const char *model,
|
|
const struct sp_registry *, int streaming, struct sp_buf *out);
|
|
int sp_openai_reply(const char *json, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
|
|
/* SSE accumulation, one line at a time, so it can be tested without a socket */
|
|
struct sp_sse;
|
|
struct sp_sse *sp_sse_new(void);
|
|
void sp_sse_free(struct sp_sse *);
|
|
/* Feed one line (with or without its newline). Returns 1 when a delta was
|
|
* produced (written to out, may be empty), 0 for lines that carry nothing,
|
|
* -1 on a parse error with the line quoted. */
|
|
int sp_sse_line(struct sp_sse *, const char *line, struct sp_buf *out,
|
|
struct sp_buf *err);
|
|
/* Turn the accumulated state into a reply. Returns 0 or -1. */
|
|
int sp_sse_finish(struct sp_sse *, struct sp_reply *out, struct sp_buf *err);
|
|
int sp_sse_done(const struct sp_sse *);
|
|
int sp_sse_usage(const struct sp_sse *, long *in, long *out);
|
|
const char *sp_sse_tool_start(const struct sp_sse *, size_t index);
|
|
|
|
#endif /* SPLINTER_H */
|