Files
splinter/include/splinter.h
T
Konrad 10e59f23ac splinter 0.1.0: loop, registry, session, transports, tests
Rebuilt from scratch after the previous attempt left no code behind: what
exists here is what compiles and what the tests prove, and nothing else is
claimed.

What is in it:
- a turn loop that refuses an over-budget request before sending it, reports
  unknown tools instead of guessing, and honours authorization classes
  (read runs, mutate/destructive ask unless --autonomous);
- a registry that is authoritative - the model names a tool, the registry
  decides whether it exists;
- an append-only JSONL session (O_APPEND, fsync per record), where a crash can
  only truncate the tail and the tail is skipped, not repaired;
- two transports: a deterministic mock (queue of replies, last request kept)
  and HTTP via curl(1) with the request body in a temp file;
- two builtins that exist to prove the classes: echo (read) and run
  (destructive, reports its exit status as an observation).

Five test binaries, no external framework, no network: mock driver covers the
answer path, a tool round, an unknown tool, a denial, autonomy, a failing tool,
the step limit, the byte budget (asserting that nothing was sent) and a
non-2xx reply.

The wire format is splinter's own line protocol (user/observe out,
answer/tool in); provider adapters translate. That is why the loop is testable
without a model, and why no vendor JSON is parsed in the core.
2026-11-07 08:06:34 +00:00

165 lines
4.6 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: needs operator 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;
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 *);
/* ------------------------------------------------------------- transport */
struct sp_reply {
long status;
char *body; /* malloc'd, NUL-terminated, may be "" */
};
/*
* A transport sends one request body and returns one reply. It never parses
* JSON or SSE: that keeps a mock, a curl adapter and any future provider on
* the same seam.
*/
struct sp_transport {
int (*send)(void *ctx, const char *req, struct sp_reply *out,
struct sp_buf *err);
void *ctx;
};
/* 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);
/* ----------------------------------------------------------------- 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;
};
struct sp_agent_stats {
size_t steps;
size_t tool_calls;
size_t denied;
size_t bytes_sent;
};
struct sp_agent {
struct sp_registry *reg;
struct sp_transport tr;
const struct sp_agent_config *cfg;
struct sp_session_info *sess;
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 *);
/* transport_curl.c */
struct sp_transport sp_curl_transport(const char *url, long timeout_s);
/* transport.c */
void sp_reply_free(struct sp_reply *);
#endif /* SPLINTER_H */