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.
This commit is contained in:
1 parent
f574313ad5
commit
10e59f23ac
19 files changed
+2127
No files matched your search
@@ -0,0 +1,7 @@
|
||||
*.o
|
||||
splinter
|
||||
tests/test_buf
|
||||
tests/test_tool
|
||||
tests/test_session
|
||||
tests/test_transport
|
||||
tests/test_agent
|
||||
@@ -0,0 +1,79 @@
|
||||
# splinter -- POSIX make, no GNU extensions, no shell helpers.
|
||||
#
|
||||
# `make` builds the binary
|
||||
# `make check` builds and runs every test binary
|
||||
# `make clean` removes generated files only
|
||||
#
|
||||
# Generated files never mix with handwritten ones, and nothing here needs a
|
||||
# configure step: the dependency set is libc, plus curl(1) at runtime if you
|
||||
# use --endpoint.
|
||||
|
||||
CC ?= cc
|
||||
CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Werror -D_POSIX_C_SOURCE=200809L -Iinclude
|
||||
LDFLAGS ?=
|
||||
LIBS ?=
|
||||
|
||||
BIN = splinter
|
||||
OBJ = src/buf.o src/tool.o src/session.o src/transport.o src/transport_curl.o \
|
||||
src/builtin.o src/agent.o
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_transport \
|
||||
tests/test_agent
|
||||
|
||||
all: $(BIN)
|
||||
|
||||
$(BIN): $(OBJ) src/main.o
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) src/main.o $(LIBS)
|
||||
|
||||
src/buf.o: src/buf.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/buf.c
|
||||
|
||||
src/tool.o: src/tool.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/tool.c
|
||||
|
||||
src/session.o: src/session.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/session.c
|
||||
|
||||
src/transport.o: src/transport.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/transport.c
|
||||
|
||||
src/transport_curl.o: src/transport_curl.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/transport_curl.c
|
||||
|
||||
src/builtin.o: src/builtin.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/builtin.c
|
||||
|
||||
src/agent.o: src/agent.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/agent.c
|
||||
|
||||
src/main.o: src/main.c include/splinter.h
|
||||
$(CC) $(CFLAGS) -c -o $@ src/main.c
|
||||
|
||||
tests/test_buf: tests/test_buf.c $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_buf.c $(OBJ) $(LIBS)
|
||||
|
||||
tests/test_tool: tests/test_tool.c $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_tool.c $(OBJ) $(LIBS)
|
||||
|
||||
tests/test_session: tests/test_session.c $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_session.c $(OBJ) $(LIBS)
|
||||
|
||||
tests/test_transport: tests/test_transport.c $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_transport.c $(OBJ) $(LIBS)
|
||||
|
||||
tests/test_agent: tests/test_agent.c $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_agent.c $(OBJ) $(LIBS)
|
||||
|
||||
check: $(BIN) $(TESTS)
|
||||
@set -e; for t in $(TESTS); do echo "== $$t"; ./$$t; done; \
|
||||
echo "all tests passed"
|
||||
|
||||
install: $(BIN)
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
||||
cp $(BIN) $(DESTDIR)$(PREFIX)/bin/
|
||||
|
||||
PREFIX ?= /usr/local
|
||||
|
||||
clean:
|
||||
rm -f $(BIN) $(OBJ) src/main.o $(TESTS)
|
||||
|
||||
.PHONY: all check install clean
|
||||
@@ -0,0 +1,62 @@
|
||||
# splinter
|
||||
|
||||
A small C99/POSIX agent harness: one turn, a tool registry, explicit
|
||||
authorization classes, an append-only session log.
|
||||
|
||||
make
|
||||
make check
|
||||
|
||||
## What it does
|
||||
|
||||
`splinter -p "list the C files" --endpoint http://127.0.0.1:8080/turn` sends a
|
||||
prompt, runs the tools the transport asks for, and prints the answer. Every turn
|
||||
is appended to `~/.splinter/<id>.jsonl` as one JSON object per line.
|
||||
|
||||
## What it refuses to do
|
||||
|
||||
- Send a request that exceeds the configured byte budget (nothing is sent; the
|
||||
turn fails instead of letting an endpoint truncate silently).
|
||||
- Run a tool that is not in the registry (the model is told, not guessed at).
|
||||
- Run a mutating or destructive tool without operator consent, unless
|
||||
`--autonomous` was given.
|
||||
- Skip a denial silently: the denial becomes the observation the model sees.
|
||||
|
||||
## Protocol
|
||||
|
||||
The harness speaks a line protocol with its transport, not a vendor API:
|
||||
|
||||
request user <text>
|
||||
request observe <tool> <output>
|
||||
reply answer <text>
|
||||
reply tool <name> <arguments>
|
||||
|
||||
A provider adapter translates an OpenAI-compatible endpoint into this shape and
|
||||
back. That is why `make check` needs no network, no key and no model: the mock
|
||||
transport replays replies.
|
||||
|
||||
## Layout
|
||||
|
||||
include/splinter.h the whole interface
|
||||
src/buf.c growable buffer, JSON escaping
|
||||
src/tool.c registry
|
||||
src/session.c append-only JSONL session
|
||||
src/transport.c deterministic mock transport
|
||||
src/transport_curl.c HTTP via curl(1), request body through a temp file
|
||||
src/builtin.c echo (read) and run (destructive)
|
||||
src/agent.c the loop
|
||||
src/main.c CLI
|
||||
tests/ five test binaries, run by `make check`
|
||||
docs/splinter.1 manual page
|
||||
src/www/index.html HTML 4.01 static page
|
||||
|
||||
## Limits, stated
|
||||
|
||||
- Tool arguments reach the tool as one string; each tool validates its own.
|
||||
- The loop has no JSON tool-call parser yet. Wire formats live in adapters, so
|
||||
the loop stays testable; an OpenAI adapter is the next piece.
|
||||
- No compaction, no retrieval tiers. When a turn does not fit the budget it
|
||||
fails loudly, which is the honest behaviour until compaction exists.
|
||||
|
||||
## License
|
||||
|
||||
BSD-3-Clause.
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
.Dd November 7, 2026
|
||||
.Dt SPLINTER 1
|
||||
.Os
|
||||
.Sh NAME
|
||||
.Nm splinter
|
||||
.Nd a small C99/POSIX agent harness
|
||||
.Sh SYNOPSIS
|
||||
.Nm
|
||||
.Fl p Ar prompt
|
||||
.Fl -endpoint Ar url
|
||||
.Op Fl -autonomous
|
||||
.Op Fl -steps Ar n
|
||||
.Op Fl -session Ar dir
|
||||
.Op Fl -session-id Ar id
|
||||
.Nm
|
||||
.Fl p Ar prompt
|
||||
.Fl -script Ar file
|
||||
.Op Ar options
|
||||
.Nm
|
||||
.Fl V
|
||||
.Sh DESCRIPTION
|
||||
.Nm
|
||||
runs one turn of an agent loop against a transport.
|
||||
It sends a prompt, executes the tools the transport asks for, and prints the
|
||||
final answer.
|
||||
Every turn is appended to a session log as JSON Lines.
|
||||
.Pp
|
||||
The loop enforces, in this order, before any tool runs:
|
||||
.Bl -enum -compact
|
||||
.It
|
||||
the request fits the configured byte budget; otherwise nothing is sent;
|
||||
.It
|
||||
the tool exists in the registry; otherwise the model is told it does not;
|
||||
.It
|
||||
the tool's authorization class allows the call; a mutating or destructive
|
||||
tool asks the operator unless
|
||||
.Fl -autonomous
|
||||
was given.
|
||||
.El
|
||||
.Pp
|
||||
A denied tool is not skipped silently: the denial is the observation the model
|
||||
receives.
|
||||
.Sh OPTIONS
|
||||
.Bl -tag -width Ds
|
||||
.It Fl p , -prompt Ar TEXT
|
||||
The user message for this turn.
|
||||
Required.
|
||||
.It Fl -endpoint Ar URL
|
||||
Transport: POST the line protocol to
|
||||
.Ar URL
|
||||
using
|
||||
.Xr curl 1 .
|
||||
.It Fl -script Ar FILE
|
||||
Transport: replay one reply per line from
|
||||
.Ar FILE .
|
||||
This needs no network and is how the test suite drives the loop.
|
||||
.It Fl -autonomous
|
||||
Run mutating tools without asking.
|
||||
Read-only tools never ask.
|
||||
.It Fl -steps Ar N
|
||||
Maximum tool rounds per turn (default 8).
|
||||
.It Fl -max-bytes Ar N
|
||||
Request budget in bytes; 0 means no limit.
|
||||
.It Fl -session Ar DIR
|
||||
Session directory (default
|
||||
.Pa ~/.splinter ) .
|
||||
.It Fl -session-id Ar ID
|
||||
Session id (default
|
||||
.Pa sess-<time>-<pid> ) .
|
||||
.It Fl V , -version
|
||||
Print the version and exit.
|
||||
.El
|
||||
.Sh PROTOCOL
|
||||
.Nm
|
||||
speaks a line protocol with its transport, not a vendor API:
|
||||
.Bd -literal -offset indent
|
||||
request user <text>
|
||||
request observe <tool> <output>
|
||||
reply answer <text>
|
||||
reply tool <name> <arguments>
|
||||
.Ed
|
||||
.Pp
|
||||
A provider adapter translates an OpenAI-compatible endpoint into this shape
|
||||
and back.
|
||||
The harness itself never parses vendor JSON, which is why the loop is testable
|
||||
with no network at all.
|
||||
.Sh EXIT STATUS
|
||||
.Bl -tag -width Ds -compact
|
||||
.It 0
|
||||
The turn produced an answer.
|
||||
.It 1
|
||||
The turn failed.
|
||||
The reason is printed on standard error.
|
||||
.It 2
|
||||
Usage or configuration error.
|
||||
.It 3
|
||||
The environment could not be set up, for example the session directory.
|
||||
.El
|
||||
.Sh FILES
|
||||
.Bl -tag -width Ds
|
||||
.It Pa ~/.splinter/<id>.jsonl
|
||||
Append-only session log, one JSON object per line.
|
||||
A crash can only truncate the tail.
|
||||
.El
|
||||
.Sh EXAMPLES
|
||||
Run a scripted turn with no network:
|
||||
.Bd -literal -offset indent
|
||||
$ printf 'answer hello\\n' > /tmp/r.txt
|
||||
$ splinter -p "say hello" --script /tmp/r.txt
|
||||
hello
|
||||
.Ed
|
||||
.Pp
|
||||
Talk to an adapter:
|
||||
.Bd -literal -offset indent
|
||||
$ splinter -p "list the C files" --endpoint http://127.0.0.1:8080/turn
|
||||
.Ed
|
||||
.Sh SEE ALSO
|
||||
.Xr curl 1
|
||||
.Sh CAVEATS
|
||||
The transport is responsible for anything the model says; the harness trusts
|
||||
only the registry and the authorization classes.
|
||||
Tool arguments are passed as a single string and are not validated beyond the
|
||||
tool's own checks.
|
||||
.Sh AUTHORS
|
||||
.An Konrad Lother
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* 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 */
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
/*
|
||||
* splinter -- the turn loop.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Wire protocol between harness and transport (deliberately tiny, so it can be
|
||||
* tested with no network and no JSON parser; a provider adapter translates
|
||||
* OpenAI's shape into this one and back):
|
||||
*
|
||||
* request "user <text>\n" first round of a turn
|
||||
* "observe <tool> <output>\n" after a tool ran
|
||||
* reply "answer <text>" the turn is done
|
||||
* "tool <name> <arguments>" run this tool, then continue
|
||||
*
|
||||
* Invariants the loop enforces, in this order, before any tool runs:
|
||||
* 1. the request fits the configured budget, otherwise the turn fails and
|
||||
* nothing is sent (never rely on the endpoint to truncate);
|
||||
* 2. the tool exists in the registry, otherwise the model is told so;
|
||||
* 3. the tool's authorization class is honoured; a denied tool is reported
|
||||
* back as the observation, not silently skipped.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int
|
||||
starts_with(const char *s, const char *prefix)
|
||||
{
|
||||
return strncmp(s, prefix, strlen(prefix)) == 0;
|
||||
}
|
||||
|
||||
static const char *
|
||||
after_prefix(const char *s, const char *prefix)
|
||||
{
|
||||
return s + strlen(prefix);
|
||||
}
|
||||
|
||||
static int
|
||||
ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args)
|
||||
{
|
||||
char line[64];
|
||||
|
||||
if (a->cfg->approval_out != NULL) {
|
||||
fprintf(a->cfg->approval_out, "run %s (%s) %s? [y/N] ", t->name,
|
||||
t->auth == SP_AUTH_MUTATE ? "mutate" : "destructive", args);
|
||||
fflush(a->cfg->approval_out);
|
||||
}
|
||||
if (a->cfg->approval_in == NULL)
|
||||
return 0; /* fail closed */
|
||||
if (fgets(line, sizeof(line), a->cfg->approval_in) == NULL)
|
||||
return 0;
|
||||
return (line[0] == 'y' || line[0] == 'Y');
|
||||
}
|
||||
|
||||
int
|
||||
sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf payload, obs;
|
||||
size_t step;
|
||||
int rc = -1;
|
||||
|
||||
if (a == NULL || a->reg == NULL || a->cfg == NULL)
|
||||
return -1;
|
||||
if (a->cfg->max_steps == 0)
|
||||
return -1;
|
||||
|
||||
sp_buf_clear(answer);
|
||||
if (sp_buf_init(&payload) != 0 || sp_buf_init(&obs) != 0) {
|
||||
sp_buf_puts(err, "agent: out of memory");
|
||||
sp_buf_free(&payload);
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_puts(&payload, "user ") != 0 ||
|
||||
sp_buf_puts(&payload, user_text != NULL ? user_text : "") != 0 ||
|
||||
sp_buf_puts(&payload, "\n") != 0)
|
||||
goto oom;
|
||||
if (a->sess != NULL &&
|
||||
sp_session_append(a->sess, "user", user_text, err) != 0)
|
||||
goto out;
|
||||
|
||||
for (step = 0; step < a->cfg->max_steps; step++) {
|
||||
struct sp_reply reply = { 0, NULL };
|
||||
const char *rest, *sp;
|
||||
const struct sp_tool *t;
|
||||
char name[128];
|
||||
const char *args;
|
||||
size_t nlen;
|
||||
|
||||
if (a->cfg->max_bytes > 0 && payload.len > a->cfg->max_bytes) {
|
||||
sp_buf_printf(err, "agent: request budget exceeded "
|
||||
"(%zu > %zu bytes); nothing sent", payload.len,
|
||||
a->cfg->max_bytes);
|
||||
goto out;
|
||||
}
|
||||
a->stats.bytes_sent += payload.len;
|
||||
a->stats.steps++;
|
||||
|
||||
if (a->tr.send(a->tr.ctx, payload.p, &reply, err) != 0) {
|
||||
free(reply.body);
|
||||
goto out;
|
||||
}
|
||||
if (reply.status < 200 || reply.status > 299) {
|
||||
sp_buf_printf(err, "agent: transport status %ld",
|
||||
reply.status);
|
||||
free(reply.body);
|
||||
goto out;
|
||||
}
|
||||
if (reply.body == NULL) {
|
||||
sp_buf_puts(err, "agent: empty reply");
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (starts_with(reply.body, "answer ")) {
|
||||
rest = after_prefix(reply.body, "answer ");
|
||||
if (sp_buf_puts(answer, rest) != 0) {
|
||||
free(reply.body);
|
||||
goto oom;
|
||||
}
|
||||
if (a->sess != NULL &&
|
||||
sp_session_append(a->sess, "assistant", rest,
|
||||
err) != 0) {
|
||||
free(reply.body);
|
||||
goto out;
|
||||
}
|
||||
free(reply.body);
|
||||
rc = 0;
|
||||
goto out;
|
||||
}
|
||||
if (!starts_with(reply.body, "tool ")) {
|
||||
sp_buf_puts(err, "agent: reply is neither answer nor "
|
||||
"tool");
|
||||
free(reply.body);
|
||||
goto out;
|
||||
}
|
||||
|
||||
rest = after_prefix(reply.body, "tool ");
|
||||
sp = strchr(rest, ' ');
|
||||
if (sp == NULL) {
|
||||
nlen = strlen(rest);
|
||||
args = "";
|
||||
} else {
|
||||
nlen = (size_t)(sp - rest);
|
||||
args = sp + 1;
|
||||
}
|
||||
if (nlen >= sizeof(name)) {
|
||||
sp_buf_puts(err, "agent: tool name too long");
|
||||
free(reply.body);
|
||||
goto out;
|
||||
}
|
||||
memcpy(name, rest, nlen);
|
||||
name[nlen] = '\0';
|
||||
|
||||
a->stats.tool_calls++;
|
||||
t = sp_registry_find(a->reg, name);
|
||||
sp_buf_clear(&obs);
|
||||
if (t == NULL) {
|
||||
if (sp_buf_printf(&obs, "error: unknown tool '%s'",
|
||||
name) != 0) {
|
||||
free(reply.body);
|
||||
goto oom;
|
||||
}
|
||||
} else if (t->auth != SP_AUTH_READ && !a->cfg->autonomous &&
|
||||
!ask_operator(a, t, args)) {
|
||||
a->stats.denied++;
|
||||
if (sp_buf_puts(&obs, "error: denied by operator") != 0) {
|
||||
free(reply.body);
|
||||
goto oom;
|
||||
}
|
||||
} else if (t->run(args, &obs, t->ctx) != 0) {
|
||||
if (sp_buf_printf(&obs, "error: tool '%s' failed",
|
||||
name) != 0) {
|
||||
free(reply.body);
|
||||
goto oom;
|
||||
}
|
||||
}
|
||||
free(reply.body);
|
||||
|
||||
if (a->sess != NULL &&
|
||||
sp_session_append(a->sess, "observation", obs.p, err) != 0)
|
||||
goto out;
|
||||
|
||||
sp_buf_clear(&payload);
|
||||
if (sp_buf_printf(&payload, "observe %s %s\n", name, obs.p) != 0)
|
||||
goto oom;
|
||||
}
|
||||
|
||||
sp_buf_printf(err, "agent: step limit (%zu) reached without an answer",
|
||||
a->cfg->max_steps);
|
||||
goto out;
|
||||
|
||||
oom:
|
||||
sp_buf_puts(err, "agent: out of memory");
|
||||
out:
|
||||
sp_buf_free(&payload);
|
||||
sp_buf_free(&obs);
|
||||
return rc;
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* splinter -- growable buffers, JSON escaping.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
#define SP_MIN_CAP 64
|
||||
|
||||
int
|
||||
sp_buf_reserve(struct sp_buf *b, size_t want)
|
||||
{
|
||||
size_t ncap;
|
||||
char *np;
|
||||
|
||||
if (want <= b->cap)
|
||||
return 0;
|
||||
ncap = b->cap ? b->cap : SP_MIN_CAP;
|
||||
while (ncap < want) {
|
||||
if (ncap > (size_t)-1 / 2) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
ncap *= 2;
|
||||
}
|
||||
np = realloc(b->p, ncap);
|
||||
if (np == NULL)
|
||||
return -1;
|
||||
b->p = np;
|
||||
b->cap = ncap;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_buf_init(struct sp_buf *b)
|
||||
{
|
||||
memset(b, 0, sizeof(*b));
|
||||
if (sp_buf_reserve(b, SP_MIN_CAP) != 0)
|
||||
return -1;
|
||||
b->p[0] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_buf_add(struct sp_buf *b, const char *data, size_t n)
|
||||
{
|
||||
if (n == 0)
|
||||
return 0;
|
||||
/* keep one spare byte for the NUL we always maintain */
|
||||
if (b->len + n + 1 < b->len) { /* overflow of the sum */
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_reserve(b, b->len + n + 1) != 0)
|
||||
return -1;
|
||||
memcpy(b->p + b->len, data, n);
|
||||
b->len += n;
|
||||
b->p[b->len] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_buf_puts(struct sp_buf *b, const char *s)
|
||||
{
|
||||
return sp_buf_add(b, s, strlen(s));
|
||||
}
|
||||
|
||||
int
|
||||
sp_buf_printf(struct sp_buf *b, const char *fmt, ...)
|
||||
{
|
||||
va_list ap, ap2;
|
||||
int need;
|
||||
|
||||
va_start(ap, fmt);
|
||||
va_copy(ap2, ap);
|
||||
need = vsnprintf(NULL, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
if (need < 0) {
|
||||
va_end(ap2);
|
||||
return -1;
|
||||
}
|
||||
if (sp_buf_reserve(b, b->len + (size_t)need + 1) != 0) {
|
||||
va_end(ap2);
|
||||
return -1;
|
||||
}
|
||||
vsnprintf(b->p + b->len, b->cap - b->len, fmt, ap2);
|
||||
va_end(ap2);
|
||||
b->len += (size_t)need;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
sp_buf_clear(struct sp_buf *b)
|
||||
{
|
||||
b->len = 0;
|
||||
if (b->p != NULL)
|
||||
b->p[0] = '\0';
|
||||
}
|
||||
|
||||
void
|
||||
sp_buf_free(struct sp_buf *b)
|
||||
{
|
||||
free(b->p);
|
||||
memset(b, 0, sizeof(*b));
|
||||
}
|
||||
|
||||
int
|
||||
sp_json_escape(struct sp_buf *b, const char *s)
|
||||
{
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
unsigned char c;
|
||||
|
||||
for (; *s != '\0'; s++) {
|
||||
c = (unsigned char)*s;
|
||||
switch (c) {
|
||||
case '"': if (sp_buf_puts(b, "\\\"") != 0) return -1; break;
|
||||
case '\\': if (sp_buf_puts(b, "\\\\") != 0) return -1; break;
|
||||
case '\n': if (sp_buf_puts(b, "\\n") != 0) return -1; break;
|
||||
case '\r': if (sp_buf_puts(b, "\\r") != 0) return -1; break;
|
||||
case '\t': if (sp_buf_puts(b, "\\t") != 0) return -1; break;
|
||||
default:
|
||||
if (c < 0x20) {
|
||||
char esc[7] = { '\\', 'u', '0', '0',
|
||||
hex[c >> 4], hex[c & 0xf], '\0' };
|
||||
if (sp_buf_puts(b, esc) != 0)
|
||||
return -1;
|
||||
} else {
|
||||
if (sp_buf_add(b, (const char *)&c, 1) != 0)
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* splinter -- builtin tools.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Two tools, on purpose: one that cannot change anything, one that can. Every
|
||||
* new tool has to declare which class it belongs to, and the loop enforces it.
|
||||
* Shell execution goes through popen("/bin/sh -c") and its exit status is
|
||||
* reported; a command that fails is an observation, not an exception.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int
|
||||
tool_echo(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return sp_buf_puts(out, args != NULL ? args : "");
|
||||
}
|
||||
|
||||
static int
|
||||
tool_run(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
FILE *p;
|
||||
char chunk[4096];
|
||||
size_t got, total = 0;
|
||||
int status;
|
||||
|
||||
(void)ctx;
|
||||
if (args == NULL || *args == '\0') {
|
||||
return sp_buf_puts(out, "error: empty command");
|
||||
}
|
||||
p = popen(args, "r");
|
||||
if (p == NULL) {
|
||||
return sp_buf_printf(out, "error: popen: %s", strerror(errno));
|
||||
}
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
|
||||
if (total > 1u << 20) { /* 1 MiB cap, stated not silent */
|
||||
sp_buf_puts(out, "\n[output truncated at 1 MiB]");
|
||||
break;
|
||||
}
|
||||
if (sp_buf_add(out, chunk, got) != 0) {
|
||||
pclose(p);
|
||||
return -1;
|
||||
}
|
||||
total += got;
|
||||
}
|
||||
status = pclose(p);
|
||||
if (status < 0) {
|
||||
return sp_buf_printf(out, "\n[exit status unknown: %s]",
|
||||
strerror(errno));
|
||||
}
|
||||
if (WIFEXITED(status))
|
||||
return sp_buf_printf(out, "\n[exit status %d]",
|
||||
WEXITSTATUS(status));
|
||||
return sp_buf_printf(out, "\n[killed by signal %d]", WTERMSIG(status));
|
||||
}
|
||||
|
||||
int
|
||||
sp_builtin_register(struct sp_registry *r)
|
||||
{
|
||||
struct sp_tool t;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "echo";
|
||||
t.description = "Return the argument unchanged.";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = tool_echo;
|
||||
if (sp_registry_add(r, &t) != 0)
|
||||
return -1;
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "run";
|
||||
t.description = "Run a shell command and return its combined output.";
|
||||
t.auth = SP_AUTH_DESTRUCTIVE;
|
||||
t.run = tool_run;
|
||||
return sp_registry_add(r, &t);
|
||||
}
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
/*
|
||||
* splinter -- command line entry point.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Exit statuses: 0 the turn produced an answer, 1 the turn failed, 2 usage or
|
||||
* configuration error, 3 the environment could not be set up (session dir).
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static void
|
||||
usage(FILE *f)
|
||||
{
|
||||
fprintf(f,
|
||||
"usage: splinter [options]\n\n"
|
||||
" -p, --prompt TEXT run one turn with TEXT and exit\n"
|
||||
" --endpoint URL transport: POST the line protocol to URL\n"
|
||||
" --script FILE transport: replay replies from FILE, one\n"
|
||||
" per line (no network; for tests and demos)\n"
|
||||
" --autonomous run mutating tools without asking\n"
|
||||
" --steps N tool rounds per turn (default 8)\n"
|
||||
" --session DIR session directory (default ~/.splinter)\n"
|
||||
" --session-id ID session id (default sess-<time>-<pid>)\n"
|
||||
" --max-bytes N request budget in bytes (0 = no limit)\n"
|
||||
" -V, --version print the version\n"
|
||||
" -h, --help this text\n\n"
|
||||
"Exactly one transport is required. With none, splinter refuses to\n"
|
||||
"start rather than pretend it can talk to a model.\n");
|
||||
}
|
||||
|
||||
static int
|
||||
need_value(int argc, int i, const char *flag)
|
||||
{
|
||||
if (i + 1 >= argc) {
|
||||
fprintf(stderr, "splinter: %s needs a value\n", flag);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct script_ctx {
|
||||
FILE *f;
|
||||
int empty;
|
||||
};
|
||||
|
||||
static int
|
||||
script_send(void *ctx, const char *req, struct sp_reply *out, struct sp_buf *err)
|
||||
{
|
||||
struct script_ctx *s = ctx;
|
||||
char line[8192];
|
||||
|
||||
(void)req;
|
||||
if (fgets(line, sizeof(line), s->f) == NULL) {
|
||||
s->empty = 1;
|
||||
sp_buf_puts(err, "script: no reply left");
|
||||
return -1;
|
||||
}
|
||||
line[strcspn(line, "\n")] = '\0';
|
||||
out->status = 200;
|
||||
out->body = strdup(line);
|
||||
if (out->body == NULL) {
|
||||
sp_buf_puts(err, "script: out of memory");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
struct sp_registry reg;
|
||||
struct sp_agent ag;
|
||||
struct sp_agent_config cfg;
|
||||
struct sp_session_info sess;
|
||||
struct sp_buf answer = { 0 }, err = { 0 };
|
||||
const char *prompt = NULL, *endpoint = NULL, *script = NULL;
|
||||
const char *sessdir = NULL, *sessid = NULL;
|
||||
char default_id[64];
|
||||
struct script_ctx sctx = { NULL, 0 };
|
||||
int i, rc;
|
||||
|
||||
memset(®, 0, sizeof(reg));
|
||||
memset(&ag, 0, sizeof(ag));
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.max_steps = 8;
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
|
||||
usage(stdout);
|
||||
return 0;
|
||||
} else if (!strcmp(argv[i], "-V") ||
|
||||
!strcmp(argv[i], "--version")) {
|
||||
printf("splinter %s\n", SP_VERSION);
|
||||
return 0;
|
||||
} else if (!strcmp(argv[i], "-p") ||
|
||||
!strcmp(argv[i], "--prompt")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
prompt = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--endpoint")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
endpoint = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--script")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
script = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--autonomous")) {
|
||||
cfg.autonomous = 1;
|
||||
} else if (!strcmp(argv[i], "--steps")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
cfg.max_steps = (size_t)strtoul(argv[++i], NULL, 10);
|
||||
} else if (!strcmp(argv[i], "--max-bytes")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
cfg.max_bytes = (size_t)strtoul(argv[++i], NULL, 10);
|
||||
} else if (!strcmp(argv[i], "--session")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
sessdir = argv[++i];
|
||||
} else if (!strcmp(argv[i], "--session-id")) {
|
||||
if (!need_value(argc, i, argv[i]))
|
||||
return 2;
|
||||
sessid = argv[++i];
|
||||
} else {
|
||||
fprintf(stderr, "splinter: unknown option %s\n", argv[i]);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
if (endpoint != NULL && script != NULL) {
|
||||
fprintf(stderr, "splinter: choose one transport, not two\n");
|
||||
return 2;
|
||||
}
|
||||
if (endpoint == NULL && script == NULL) {
|
||||
fprintf(stderr, "splinter: no transport (-e/--endpoint or "
|
||||
"--script)\n");
|
||||
return 2;
|
||||
}
|
||||
if (prompt == NULL) {
|
||||
fprintf(stderr, "splinter: -p/--prompt is required in %s\n",
|
||||
SP_VERSION);
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0) {
|
||||
fprintf(stderr, "splinter: out of memory\n");
|
||||
return 3;
|
||||
}
|
||||
if (sp_builtin_register(®) != 0) {
|
||||
fprintf(stderr, "splinter: cannot register builtins\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (sessdir == NULL) {
|
||||
const char *home = getenv("HOME");
|
||||
static char buf[512];
|
||||
|
||||
snprintf(buf, sizeof(buf), "%s/.splinter", home ? home : ".");
|
||||
sessdir = buf;
|
||||
}
|
||||
if (sessid == NULL) {
|
||||
snprintf(default_id, sizeof(default_id), "sess-%ld-%d",
|
||||
(long)time(NULL), (int)getpid());
|
||||
sessid = default_id;
|
||||
}
|
||||
if (sp_session_open(&sess, sessdir, sessid, &err) != 0) {
|
||||
fprintf(stderr, "splinter: %s\n", err.p ? err.p : "session");
|
||||
return 3;
|
||||
}
|
||||
ag.sess = &sess;
|
||||
|
||||
if (script != NULL) {
|
||||
sctx.f = fopen(script, "r");
|
||||
if (sctx.f == NULL) {
|
||||
fprintf(stderr, "splinter: %s: %s\n", script,
|
||||
strerror(errno));
|
||||
return 3;
|
||||
}
|
||||
ag.tr.send = script_send;
|
||||
ag.tr.ctx = &sctx;
|
||||
} else {
|
||||
ag.tr = sp_curl_transport(endpoint, 120);
|
||||
}
|
||||
|
||||
cfg.approval_in = stdin;
|
||||
cfg.approval_out = stderr;
|
||||
ag.reg = ®
|
||||
ag.cfg = &cfg;
|
||||
|
||||
rc = sp_agent_run(&ag, prompt, &answer, &err);
|
||||
if (rc == 0) {
|
||||
printf("%s\n", answer.p);
|
||||
} else {
|
||||
fprintf(stderr, "splinter: %s\n", err.p ? err.p : "turn failed");
|
||||
}
|
||||
if (sctx.f != NULL)
|
||||
fclose(sctx.f);
|
||||
sp_buf_free(&answer);
|
||||
sp_buf_free(&err);
|
||||
sp_registry_free(®);
|
||||
return rc == 0 ? 0 : 1;
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* splinter -- append-only session log.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* One JSON object per line, opened O_APPEND. A crash can only truncate the
|
||||
* tail, so recovery is "replay complete lines, drop one partial line". Nothing
|
||||
* here ever rewrites an earlier record: history that can be edited is history
|
||||
* you cannot trust after an incident.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int
|
||||
mkdir_p(const char *path)
|
||||
{
|
||||
char buf[512];
|
||||
size_t i, n;
|
||||
|
||||
n = strlen(path);
|
||||
if (n == 0 || n >= sizeof(buf)) {
|
||||
errno = ENAMETOOLONG;
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, path, n + 1);
|
||||
for (i = 1; i < n; i++) {
|
||||
if (buf[i] != '/')
|
||||
continue;
|
||||
buf[i] = '\0';
|
||||
if (mkdir(buf, 0755) != 0 && errno != EEXIST)
|
||||
return -1;
|
||||
buf[i] = '/';
|
||||
}
|
||||
if (mkdir(buf, 0755) != 0 && errno != EEXIST)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_open(struct sp_session_info *info, const char *dir,
|
||||
const char *id, struct sp_buf *err)
|
||||
{
|
||||
char path[640];
|
||||
int fd;
|
||||
|
||||
if (dir == NULL || id == NULL || info == NULL) {
|
||||
sp_buf_puts(err, "session: dir and id are required");
|
||||
return -1;
|
||||
}
|
||||
if (strlen(id) >= sizeof(info->id) || strlen(dir) >= sizeof(info->dir)) {
|
||||
sp_buf_puts(err, "session: id or dir too long");
|
||||
return -1;
|
||||
}
|
||||
snprintf(info->id, sizeof(info->id), "%s", id);
|
||||
snprintf(info->dir, sizeof(info->dir), "%s", dir);
|
||||
|
||||
if (mkdir_p(dir) != 0) {
|
||||
sp_buf_printf(err, "session: cannot create %s: %s", dir,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, info->id);
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
||||
if (fd < 0) {
|
||||
sp_buf_printf(err, "session: cannot open %s: %s", path,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
write_all(int fd, const char *p, size_t n)
|
||||
{
|
||||
ssize_t w;
|
||||
|
||||
while (n > 0) {
|
||||
w = write(fd, p, n);
|
||||
if (w < 0) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
return -1;
|
||||
}
|
||||
p += (size_t)w;
|
||||
n -= (size_t)w;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_append(const struct sp_session_info *info, const char *role,
|
||||
const char *text, struct sp_buf *err)
|
||||
{
|
||||
struct sp_buf line;
|
||||
char path[640];
|
||||
int fd, rc;
|
||||
|
||||
if (sp_buf_init(&line) != 0) {
|
||||
sp_buf_puts(err, "session: out of memory");
|
||||
return -1;
|
||||
}
|
||||
rc = sp_buf_puts(&line, "{\"role\":\"");
|
||||
if (rc == 0)
|
||||
rc = sp_json_escape(&line, role);
|
||||
if (rc == 0)
|
||||
rc = sp_buf_puts(&line, "\",\"text\":\"");
|
||||
if (rc == 0)
|
||||
rc = sp_json_escape(&line, text);
|
||||
if (rc == 0)
|
||||
rc = sp_buf_puts(&line, "\"}\n");
|
||||
if (rc != 0) {
|
||||
sp_buf_puts(err, "session: out of memory");
|
||||
sp_buf_free(&line);
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", info->dir, info->id);
|
||||
fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
||||
if (fd < 0) {
|
||||
sp_buf_printf(err, "session: cannot open %s: %s", path,
|
||||
strerror(errno));
|
||||
sp_buf_free(&line);
|
||||
return -1;
|
||||
}
|
||||
rc = write_all(fd, line.p, line.len);
|
||||
if (rc == 0 && fsync(fd) != 0)
|
||||
rc = -1;
|
||||
if (rc != 0)
|
||||
sp_buf_printf(err, "session: write failed: %s", strerror(errno));
|
||||
close(fd);
|
||||
sp_buf_free(&line);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
sp_session_count(const struct sp_session_info *info, size_t *n,
|
||||
struct sp_buf *err)
|
||||
{
|
||||
char path[640], chunk[4096];
|
||||
FILE *f;
|
||||
size_t count = 0, i, got;
|
||||
|
||||
snprintf(path, sizeof(path), "%s/%s.jsonl", info->dir, info->id);
|
||||
*n = 0;
|
||||
f = fopen(path, "r");
|
||||
if (f == NULL) {
|
||||
if (errno == ENOENT)
|
||||
return 0;
|
||||
sp_buf_printf(err, "session: cannot read %s: %s", path,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) {
|
||||
for (i = 0; i < got; i++)
|
||||
if (chunk[i] == '\n')
|
||||
count++;
|
||||
}
|
||||
if (ferror(f)) {
|
||||
sp_buf_printf(err, "session: read error on %s", path);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
/*
|
||||
* Only complete lines (terminated by a newline) are records: a torn tail
|
||||
* from a crash is skipped, not repaired and not an error.
|
||||
*/
|
||||
*n = count;
|
||||
return 0;
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* splinter -- tool registry.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* The registry is authoritative: a tool the model names but this registry does
|
||||
* not hold is an unknown tool, and the harness says so instead of guessing.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
int
|
||||
sp_registry_add(struct sp_registry *r, const struct sp_tool *t)
|
||||
{
|
||||
size_t ncap;
|
||||
void *np;
|
||||
|
||||
if (r->len == r->cap) {
|
||||
if (r->cap > (size_t)-1 / 2 / sizeof(*r->tools)) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
ncap = r->cap ? r->cap * 2 : 8;
|
||||
np = realloc(r->tools, ncap * sizeof(*r->tools));
|
||||
if (np == NULL)
|
||||
return -1;
|
||||
r->tools = np;
|
||||
r->cap = ncap;
|
||||
}
|
||||
r->tools[r->len++] = *t;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct sp_tool *
|
||||
sp_registry_find(const struct sp_registry *r, const char *name)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (name == NULL)
|
||||
return NULL;
|
||||
for (i = 0; i < r->len; i++)
|
||||
if (strcmp(r->tools[i].name, name) == 0)
|
||||
return &r->tools[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
sp_registry_count(const struct sp_registry *r)
|
||||
{
|
||||
return r->len;
|
||||
}
|
||||
|
||||
void
|
||||
sp_registry_free(struct sp_registry *r)
|
||||
{
|
||||
free(r->tools);
|
||||
memset(r, 0, sizeof(*r));
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* splinter -- deterministic in-process transport.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* The mock is what makes the loop testable without a network, a key or a paid
|
||||
* endpoint. Replies are queued FIFO and handed out in order; the last request
|
||||
* is kept so a test can assert on what the harness actually sent.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
int
|
||||
sp_mock_init(struct sp_mock *m)
|
||||
{
|
||||
memset(m, 0, sizeof(*m));
|
||||
return sp_buf_init(&m->last_request);
|
||||
}
|
||||
|
||||
int
|
||||
sp_mock_push(struct sp_mock *m, long status, const char *body)
|
||||
{
|
||||
size_t ncap;
|
||||
void *np;
|
||||
char *copy;
|
||||
|
||||
if (m->len == m->cap) {
|
||||
ncap = m->cap ? m->cap * 2 : 4;
|
||||
np = realloc(m->queue, ncap * sizeof(*m->queue));
|
||||
if (np == NULL)
|
||||
return -1;
|
||||
m->queue = np;
|
||||
m->cap = ncap;
|
||||
}
|
||||
copy = strdup(body != NULL ? body : "");
|
||||
if (copy == NULL)
|
||||
return -1;
|
||||
m->queue[m->len].status = status;
|
||||
m->queue[m->len].body = copy;
|
||||
m->len++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
mock_send(void *ctx, const char *req, struct sp_reply *out, struct sp_buf *err)
|
||||
{
|
||||
struct sp_mock *m = ctx;
|
||||
|
||||
sp_buf_clear(&m->last_request);
|
||||
if (sp_buf_puts(&m->last_request, req) != 0) {
|
||||
sp_buf_puts(err, "mock: out of memory");
|
||||
return -1;
|
||||
}
|
||||
m->requests++;
|
||||
if (m->head >= m->len) {
|
||||
out->status = 0;
|
||||
out->body = strdup("");
|
||||
if (out->body == NULL) {
|
||||
sp_buf_puts(err, "mock: out of memory");
|
||||
return -1;
|
||||
}
|
||||
sp_buf_printf(err, "mock: no queued reply for request %zu",
|
||||
m->requests);
|
||||
return -1;
|
||||
}
|
||||
out->status = m->queue[m->head].status;
|
||||
out->body = strdup(m->queue[m->head].body);
|
||||
if (out->body == NULL) {
|
||||
sp_buf_puts(err, "mock: out of memory");
|
||||
return -1;
|
||||
}
|
||||
m->head++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct sp_transport
|
||||
sp_mock_transport(struct sp_mock *m)
|
||||
{
|
||||
struct sp_transport t;
|
||||
|
||||
t.send = mock_send;
|
||||
t.ctx = m;
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
sp_mock_free(struct sp_mock *m)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < m->len; i++)
|
||||
free(m->queue[i].body);
|
||||
free(m->queue);
|
||||
sp_buf_free(&m->last_request);
|
||||
memset(m, 0, sizeof(*m));
|
||||
}
|
||||
|
||||
void
|
||||
sp_reply_free(struct sp_reply *r);
|
||||
|
||||
void
|
||||
sp_reply_free(struct sp_reply *r)
|
||||
{
|
||||
free(r->body);
|
||||
r->body = NULL;
|
||||
r->status = 0;
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* splinter -- HTTP transport through the curl binary.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* C99/POSIX gives no portable HTTPS client. Rather than link a TLS stack into
|
||||
* the harness, this adapter runs curl(1) and speaks the same line protocol as
|
||||
* the mock. Consequences, stated rather than discovered later:
|
||||
* - the request body travels through a temp file, never through the shell
|
||||
* command line, so quoting cannot corrupt it;
|
||||
* - the HTTP status is taken from curl's own -w output;
|
||||
* - a failed transfer is an error, never an empty success.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
struct curl_ctx {
|
||||
char url[512];
|
||||
long timeout_s;
|
||||
};
|
||||
|
||||
static int
|
||||
read_all(FILE *f, struct sp_buf *out)
|
||||
{
|
||||
char chunk[4096];
|
||||
size_t got;
|
||||
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) {
|
||||
if (sp_buf_add(out, chunk, got) != 0)
|
||||
return -1;
|
||||
}
|
||||
return ferror(f) ? -1 : 0;
|
||||
}
|
||||
|
||||
static int
|
||||
curl_send(void *ctx, const char *req, struct sp_reply *out, struct sp_buf *err)
|
||||
{
|
||||
struct curl_ctx *c = ctx;
|
||||
struct sp_buf cmd, raw;
|
||||
char path[] = "/tmp/splinter-req-XXXXXX";
|
||||
int fd, rc = -1;
|
||||
FILE *p;
|
||||
ssize_t w;
|
||||
size_t n;
|
||||
char *nl;
|
||||
long status;
|
||||
|
||||
fd = mkstemp(path);
|
||||
if (fd < 0) {
|
||||
sp_buf_printf(err, "curl: mkstemp: %s", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
n = strlen(req);
|
||||
w = write(fd, req, n);
|
||||
close(fd);
|
||||
if (w < 0 || (size_t)w != n) {
|
||||
sp_buf_printf(err, "curl: short write: %s", strerror(errno));
|
||||
unlink(path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sp_buf_init(&cmd) != 0 || sp_buf_init(&raw) != 0) {
|
||||
sp_buf_puts(err, "curl: out of memory");
|
||||
unlink(path);
|
||||
return -1;
|
||||
}
|
||||
sp_buf_printf(&cmd, "curl -sS -m %ld -X POST -H 'Content-Type: "
|
||||
"text/plain' --data-binary @%s -w '\\n%%{http_code}' '%s'",
|
||||
c->timeout_s, path, c->url);
|
||||
|
||||
p = popen(cmd.p, "r");
|
||||
if (p == NULL) {
|
||||
sp_buf_printf(err, "curl: popen: %s", strerror(errno));
|
||||
goto out;
|
||||
}
|
||||
if (read_all(p, &raw) != 0) {
|
||||
sp_buf_puts(err, "curl: read failed");
|
||||
pclose(p);
|
||||
goto out;
|
||||
}
|
||||
if (pclose(p) != 0) {
|
||||
sp_buf_printf(err, "curl: transfer failed for %s", c->url);
|
||||
goto out;
|
||||
}
|
||||
if (raw.len < 2 || (nl = strrchr(raw.p, '\n')) == NULL) {
|
||||
sp_buf_puts(err, "curl: unparseable reply (no status line)");
|
||||
goto out;
|
||||
}
|
||||
status = strtol(nl + 1, NULL, 10);
|
||||
*nl = '\0';
|
||||
out->status = status;
|
||||
out->body = strdup(raw.p);
|
||||
if (out->body == NULL) {
|
||||
sp_buf_puts(err, "curl: out of memory");
|
||||
goto out;
|
||||
}
|
||||
rc = 0;
|
||||
out:
|
||||
sp_buf_free(&cmd);
|
||||
sp_buf_free(&raw);
|
||||
unlink(path);
|
||||
return rc;
|
||||
}
|
||||
|
||||
struct sp_transport
|
||||
sp_curl_transport(const char *url, long timeout_s)
|
||||
{
|
||||
static struct curl_ctx ctx;
|
||||
struct sp_transport t;
|
||||
|
||||
snprintf(ctx.url, sizeof(ctx.url), "%s", url);
|
||||
ctx.timeout_s = timeout_s > 0 ? timeout_s : 120;
|
||||
t.send = curl_send;
|
||||
t.ctx = &ctx;
|
||||
return t;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||
"http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
<title>splinter — a small C99/POSIX agent harness</title>
|
||||
<meta name="description" content="splinter runs one turn of an agent loop against a transport, with a tool registry and explicit authorization classes.">
|
||||
<style type="text/css">
|
||||
body { background: #0d0d0d; color: #d8d8d8; font-family: "DejaVu Sans Mono", monospace;
|
||||
max-width: 46em; margin: 4em auto; padding: 0 1em; line-height: 1.55; }
|
||||
h1 { color: #f0f0f0; font-size: 1.6em; border-bottom: 1px solid #333; padding-bottom: .4em; }
|
||||
h2 { color: #b9b9b9; font-size: 1.1em; margin-top: 2.2em; }
|
||||
a { color: #7fb2ff; }
|
||||
pre { background: #151515; border-left: 3px solid #333; padding: .8em; overflow-x: auto; }
|
||||
code { color: #e6c07b; }
|
||||
.dim { color: #777; }
|
||||
ul { padding-left: 1.2em; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>splinter</h1>
|
||||
|
||||
<p class="dim">a small C99/POSIX agent harness — no runtime, no SDK, no database</p>
|
||||
|
||||
<p>
|
||||
splinter runs one turn of an agent loop against a transport. It sends a prompt,
|
||||
executes the tools the transport asks for, and prints the final answer. Every
|
||||
turn is appended to a session log as JSON Lines.
|
||||
</p>
|
||||
|
||||
<h2>Why it exists</h2>
|
||||
<p>
|
||||
Because a harness that hides its state cannot be debugged. splinter keeps three
|
||||
things visible at all times: the request it sent, the tools it ran, and the
|
||||
records it wrote. It does not own a model, a UI, or a network stack — those are
|
||||
transports.
|
||||
</p>
|
||||
|
||||
<h2>Rules it enforces before any tool runs</h2>
|
||||
<ul>
|
||||
<li>the request fits the byte budget, otherwise nothing is sent at all;</li>
|
||||
<li>the tool exists in the registry, otherwise the model is told it does not;</li>
|
||||
<li>the tool's authorization class allows the call — mutating and destructive
|
||||
tools ask the operator unless autonomy was granted;</li>
|
||||
<li>a denial is the observation the model receives, never a silent skip.</li>
|
||||
</ul>
|
||||
|
||||
<h2>Build</h2>
|
||||
<pre><code>$ make
|
||||
$ make check</code></pre>
|
||||
<p>Five test binaries drive the loop through a deterministic mock transport:
|
||||
no network, no key, no model.</p>
|
||||
|
||||
<h2>Use</h2>
|
||||
<pre><code>$ printf 'tool echo ping\nanswer finished\n' > /tmp/r.txt
|
||||
$ splinter -p "test run" --script /tmp/r.txt
|
||||
finished
|
||||
|
||||
$ splinter -p "list the C files" --endpoint http://127.0.0.1:8080/turn</code></pre>
|
||||
|
||||
<h2>Protocol</h2>
|
||||
<pre><code>request user <text>
|
||||
request observe <tool> <output>
|
||||
reply answer <text>
|
||||
reply tool <name> <arguments></code></pre>
|
||||
<p>
|
||||
A provider adapter translates an OpenAI-compatible endpoint into this shape and
|
||||
back. The harness never parses vendor JSON — which is exactly why the loop can
|
||||
be tested with no network at all.
|
||||
</p>
|
||||
|
||||
<h2>What it is not</h2>
|
||||
<p>
|
||||
Not a chat UI. Not a model. Not a plugin platform. It is the part in the middle
|
||||
that decides whether a tool may run, and writes down what happened.
|
||||
</p>
|
||||
|
||||
<p class="dim">Manual page: <code>man ./docs/splinter.1</code> · Source: <code>src/</code> · License: BSD-3-Clause</p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* splinter -- turn loop tests.
|
||||
*
|
||||
* Every case drives the real loop through the mock transport: no network, no
|
||||
* model, no key. What is asserted is what the loop actually did - the answer,
|
||||
* the request it sent, the observations it produced, the counters.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static int
|
||||
echo_tool(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)ctx;
|
||||
return sp_buf_puts(out, args);
|
||||
}
|
||||
|
||||
static int
|
||||
failing_tool(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)args;
|
||||
(void)out;
|
||||
(void)ctx;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
setup(struct sp_registry *r)
|
||||
{
|
||||
struct sp_tool t;
|
||||
|
||||
memset(r, 0, sizeof(*r));
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "echo";
|
||||
t.description = "echo the argument";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = echo_tool;
|
||||
(void)sp_registry_add(r, &t);
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "write";
|
||||
t.description = "pretend to mutate";
|
||||
t.auth = SP_AUTH_MUTATE;
|
||||
t.run = echo_tool;
|
||||
(void)sp_registry_add(r, &t);
|
||||
|
||||
memset(&t, 0, sizeof(t));
|
||||
t.name = "boom";
|
||||
t.description = "always fails";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = failing_tool;
|
||||
(void)sp_registry_add(r, &t);
|
||||
}
|
||||
|
||||
static int
|
||||
run_turn(struct sp_mock *m, struct sp_registry *r, const char *prompt,
|
||||
int autonomous, size_t steps, size_t max_bytes, struct sp_buf *answer,
|
||||
struct sp_buf *err, struct sp_agent_stats *stats)
|
||||
{
|
||||
struct sp_agent a;
|
||||
struct sp_agent_config cfg;
|
||||
|
||||
memset(&a, 0, sizeof(a));
|
||||
memset(&cfg, 0, sizeof(cfg));
|
||||
cfg.max_steps = steps ? steps : 8;
|
||||
cfg.max_bytes = max_bytes;
|
||||
cfg.autonomous = autonomous;
|
||||
a.reg = r;
|
||||
a.cfg = &cfg;
|
||||
a.tr = sp_mock_transport(m);
|
||||
{
|
||||
int rc = sp_agent_run(&a, prompt, answer, err);
|
||||
|
||||
if (stats != NULL)
|
||||
*stats = a.stats;
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_registry r;
|
||||
struct sp_buf answer, err;
|
||||
struct sp_agent_stats st;
|
||||
struct sp_mock m;
|
||||
|
||||
setup(&r);
|
||||
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
|
||||
/* 1. a plain answer needs no tool and sends exactly one request */
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer hello") == 0);
|
||||
CHECK(run_turn(&m, &r, "hi", 0, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(strcmp(answer.p, "hello") == 0);
|
||||
CHECK(st.steps == 1 && st.tool_calls == 0);
|
||||
CHECK(strcmp(m.last_request.p, "user hi\n") == 0);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 2. a read tool runs and its observation goes back to the transport */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool echo ping") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer done") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(strcmp(answer.p, "done") == 0);
|
||||
CHECK(st.tool_calls == 1 && st.denied == 0);
|
||||
CHECK(strstr(m.last_request.p, "observe echo ping") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 3. an unknown tool is reported, not guessed at */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool nope x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(strstr(m.last_request.p, "unknown tool 'nope'") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 4. a mutating tool without consent is denied, and the denial is the
|
||||
* observation the model sees (fail closed: no approval input) */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 0, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(st.denied == 1);
|
||||
CHECK(strstr(m.last_request.p, "denied by operator") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 5. the same tool runs when the operator allowed autonomy */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool write x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(st.denied == 0);
|
||||
CHECK(strstr(m.last_request.p, "observe write x") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 6. a failing tool becomes an observation, not a crash */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool boom x") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer ok") == 0);
|
||||
CHECK(run_turn(&m, &r, "do it", 1, 8, 0, &answer, &err, &st) == 0);
|
||||
CHECK(strstr(m.last_request.p, "tool 'boom' failed") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 7. an endless tool loop stops at the step limit */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool echo a") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool echo b") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "tool echo c") == 0);
|
||||
CHECK(run_turn(&m, &r, "loop", 1, 2, 0, &answer, &err, &st) != 0);
|
||||
CHECK(strstr(err.p, "step limit") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 8. an over-budget request is refused before anything is sent */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(run_turn(&m, &r, "this prompt is far longer than ten bytes",
|
||||
1, 8, 10, &answer, &err, &st) != 0);
|
||||
CHECK(strstr(err.p, "budget exceeded") != NULL);
|
||||
CHECK(m.requests == 0);
|
||||
sp_mock_free(&m);
|
||||
|
||||
/* 9. a non-2xx reply fails the turn instead of being treated as text */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
CHECK(sp_mock_push(&m, 500, "boom") == 0);
|
||||
CHECK(run_turn(&m, &r, "hi", 1, 8, 0, &answer, &err, &st) != 0);
|
||||
CHECK(strstr(err.p, "status 500") != NULL);
|
||||
sp_mock_free(&m);
|
||||
|
||||
sp_buf_free(&answer);
|
||||
sp_buf_free(&err);
|
||||
sp_registry_free(&r);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_agent: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/* splinter -- buffer and escaping tests. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_buf b;
|
||||
|
||||
CHECK(sp_buf_init(&b) == 0);
|
||||
CHECK(b.len == 0);
|
||||
CHECK(b.p != NULL);
|
||||
CHECK(strcmp(b.p, "") == 0);
|
||||
|
||||
CHECK(sp_buf_puts(&b, "hello") == 0);
|
||||
CHECK(b.len == 5);
|
||||
CHECK(strcmp(b.p, "hello") == 0);
|
||||
|
||||
CHECK(sp_buf_add(&b, " world", 6) == 0);
|
||||
CHECK(b.len == 11);
|
||||
CHECK(strcmp(b.p, "hello world") == 0);
|
||||
|
||||
CHECK(sp_buf_printf(&b, " %d", 42) == 0);
|
||||
CHECK(strcmp(b.p, "hello world 42") == 0);
|
||||
|
||||
/* growth: 10k bytes must land, and the NUL must always be there */
|
||||
{
|
||||
size_t i;
|
||||
|
||||
sp_buf_clear(&b);
|
||||
for (i = 0; i < 10000; i++)
|
||||
CHECK(sp_buf_puts(&b, "x") == 0);
|
||||
CHECK(b.len == 10000);
|
||||
CHECK(b.p[b.len] == '\0');
|
||||
CHECK(b.cap > b.len);
|
||||
}
|
||||
|
||||
sp_buf_clear(&b);
|
||||
CHECK(sp_json_escape(&b, "a\"b\\c\nd\te") == 0);
|
||||
CHECK(strcmp(b.p, "a\\\"b\\\\c\\nd\\te") == 0);
|
||||
|
||||
sp_buf_clear(&b);
|
||||
CHECK(sp_json_escape(&b, "\x01") == 0);
|
||||
CHECK(strcmp(b.p, "\\u0001") == 0);
|
||||
|
||||
sp_buf_free(&b);
|
||||
CHECK(b.p == NULL);
|
||||
CHECK(b.len == 0);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_buf: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/* splinter -- append-only session tests. */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_session_info s;
|
||||
struct sp_buf err, line;
|
||||
char tmpl[] = "/tmp/splinter-sess-XXXXXX";
|
||||
char *dir;
|
||||
char path[640];
|
||||
size_t n;
|
||||
FILE *f;
|
||||
|
||||
dir = mkdtemp(tmpl);
|
||||
if (dir == NULL) {
|
||||
perror("mkdtemp");
|
||||
return 1;
|
||||
}
|
||||
if (sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
|
||||
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
||||
CHECK(strcmp(s.id, "s1") == 0);
|
||||
|
||||
CHECK(sp_session_count(&s, &n, &err) == 0);
|
||||
CHECK(n == 0);
|
||||
|
||||
CHECK(sp_session_append(&s, "user", "hi \"there\"", &err) == 0);
|
||||
CHECK(sp_session_append(&s, "assistant", "hello", &err) == 0);
|
||||
CHECK(sp_session_count(&s, &n, &err) == 0);
|
||||
CHECK(n == 2);
|
||||
|
||||
/* the record is valid JSONL with the text escaped */
|
||||
snprintf(path, sizeof(path), "%s/s1.jsonl", dir);
|
||||
f = fopen(path, "r");
|
||||
CHECK(f != NULL);
|
||||
if (f != NULL) {
|
||||
if (sp_buf_init(&line) == 0) {
|
||||
char buf[512];
|
||||
size_t got = fread(buf, 1, sizeof(buf) - 1, f);
|
||||
|
||||
buf[got] = '\0';
|
||||
CHECK(strstr(buf, "\"role\":\"user\"") != NULL);
|
||||
CHECK(strstr(buf, "hi \\\"there\\\"") != NULL);
|
||||
CHECK(buf[got - 1] == '\n');
|
||||
sp_buf_free(&line);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
/* a torn tail must not be counted as a record and must not be an error */
|
||||
f = fopen(path, "a");
|
||||
CHECK(f != NULL);
|
||||
if (f != NULL) {
|
||||
fputs("{\"role\":\"user\",\"text\":\"torn", f);
|
||||
fclose(f);
|
||||
}
|
||||
CHECK(sp_session_count(&s, &n, &err) == 0);
|
||||
CHECK(n == 2);
|
||||
|
||||
/* reopening an existing session appends, it never truncates */
|
||||
CHECK(sp_session_open(&s, dir, "s1", &err) == 0);
|
||||
CHECK(sp_session_append(&s, "user", "again", &err) == 0);
|
||||
|
||||
sp_buf_free(&err);
|
||||
printf("test_session: ok (%s)\n", dir);
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/* splinter -- registry tests. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static int
|
||||
noop(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)args;
|
||||
(void)ctx;
|
||||
return sp_buf_puts(out, "noop");
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_registry r;
|
||||
struct sp_tool t;
|
||||
const struct sp_tool *found;
|
||||
|
||||
memset(&r, 0, sizeof(r));
|
||||
memset(&t, 0, sizeof(t));
|
||||
|
||||
CHECK(sp_registry_count(&r) == 0);
|
||||
CHECK(sp_registry_find(&r, "x") == NULL);
|
||||
|
||||
t.name = "alpha";
|
||||
t.description = "first";
|
||||
t.auth = SP_AUTH_READ;
|
||||
t.run = noop;
|
||||
CHECK(sp_registry_add(&r, &t) == 0);
|
||||
|
||||
t.name = "beta";
|
||||
t.description = "second";
|
||||
t.auth = SP_AUTH_MUTATE;
|
||||
t.run = noop;
|
||||
CHECK(sp_registry_add(&r, &t) == 0);
|
||||
|
||||
CHECK(sp_registry_count(&r) == 2);
|
||||
|
||||
found = sp_registry_find(&r, "alpha");
|
||||
CHECK(found != NULL);
|
||||
CHECK(found != NULL && found->auth == SP_AUTH_READ);
|
||||
CHECK(found != NULL && strcmp(found->description, "first") == 0);
|
||||
|
||||
found = sp_registry_find(&r, "beta");
|
||||
CHECK(found != NULL);
|
||||
CHECK(found != NULL && found->auth == SP_AUTH_MUTATE);
|
||||
|
||||
CHECK(sp_registry_find(&r, "gamma") == NULL);
|
||||
CHECK(sp_registry_find(&r, NULL) == NULL);
|
||||
|
||||
/* growth beyond the initial eight slots */
|
||||
{
|
||||
int i;
|
||||
char name[16];
|
||||
|
||||
for (i = 0; i < 40; i++) {
|
||||
snprintf(name, sizeof(name), "t%d", i);
|
||||
t.name = name;
|
||||
CHECK(sp_registry_add(&r, &t) == 0);
|
||||
}
|
||||
CHECK(sp_registry_count(&r) == 42);
|
||||
CHECK(sp_registry_find(&r, "t39") != NULL);
|
||||
}
|
||||
|
||||
sp_registry_free(&r);
|
||||
CHECK(sp_registry_count(&r) == 0);
|
||||
CHECK(r.tools == NULL);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_tool: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/* splinter -- mock transport tests. */
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "splinter.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
struct sp_mock m;
|
||||
struct sp_transport t;
|
||||
struct sp_reply r;
|
||||
struct sp_buf err;
|
||||
|
||||
CHECK(sp_mock_init(&m) == 0);
|
||||
if (sp_buf_init(&err) != 0)
|
||||
return 1;
|
||||
t = sp_mock_transport(&m);
|
||||
|
||||
/* an empty queue is an error, never an empty success */
|
||||
memset(&r, 0, sizeof(r));
|
||||
CHECK(t.send(t.ctx, "user hi\n", &r, &err) != 0);
|
||||
CHECK(err.len > 0);
|
||||
sp_buf_clear(&err);
|
||||
sp_reply_free(&r);
|
||||
|
||||
CHECK(sp_mock_push(&m, 200, "answer first") == 0);
|
||||
CHECK(sp_mock_push(&m, 200, "answer second") == 0);
|
||||
|
||||
memset(&r, 0, sizeof(r));
|
||||
CHECK(t.send(t.ctx, "user one\n", &r, &err) == 0);
|
||||
CHECK(r.status == 200);
|
||||
CHECK(r.body != NULL && strcmp(r.body, "answer first") == 0);
|
||||
CHECK(strcmp(m.last_request.p, "user one\n") == 0);
|
||||
sp_reply_free(&r);
|
||||
|
||||
memset(&r, 0, sizeof(r));
|
||||
CHECK(t.send(t.ctx, "user two\n", &r, &err) == 0);
|
||||
CHECK(r.body != NULL && strcmp(r.body, "answer second") == 0);
|
||||
CHECK(strcmp(m.last_request.p, "user two\n") == 0);
|
||||
sp_reply_free(&r);
|
||||
|
||||
CHECK(m.requests == 3);
|
||||
|
||||
/* a non-2xx status is passed through untouched */
|
||||
CHECK(sp_mock_push(&m, 429, "slow down") == 0);
|
||||
memset(&r, 0, sizeof(r));
|
||||
CHECK(t.send(t.ctx, "user three\n", &r, &err) == 0);
|
||||
CHECK(r.status == 429);
|
||||
sp_reply_free(&r);
|
||||
|
||||
sp_buf_free(&err);
|
||||
sp_mock_free(&m);
|
||||
CHECK(m.queue == NULL);
|
||||
|
||||
if (failures == 0)
|
||||
printf("test_transport: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user