From 10e59f23ac3d59400dae9e4de190c023fe25dace Mon Sep 17 00:00:00 2001 From: Konrad Date: Sat, 7 Nov 2026 08:06:34 +0000 Subject: [PATCH] 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. --- .gitignore | 7 ++ Makefile | 79 +++++++++++++++ README.md | 62 ++++++++++++ docs/splinter.1 | 125 ++++++++++++++++++++++++ include/splinter.h | 164 +++++++++++++++++++++++++++++++ src/agent.c | 201 ++++++++++++++++++++++++++++++++++++++ src/buf.c | 142 +++++++++++++++++++++++++++ src/builtin.c | 84 ++++++++++++++++ src/main.c | 212 +++++++++++++++++++++++++++++++++++++++++ src/session.c | 178 ++++++++++++++++++++++++++++++++++ src/tool.c | 62 ++++++++++++ src/transport.c | 111 +++++++++++++++++++++ src/transport_curl.c | 122 ++++++++++++++++++++++++ src/www/index.html | 82 ++++++++++++++++ tests/test_agent.c | 197 ++++++++++++++++++++++++++++++++++++++ tests/test_buf.c | 64 +++++++++++++ tests/test_session.c | 83 ++++++++++++++++ tests/test_tool.c | 84 ++++++++++++++++ tests/test_transport.c | 68 +++++++++++++ 19 files changed, 2127 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 docs/splinter.1 create mode 100644 include/splinter.h create mode 100644 src/agent.c create mode 100644 src/buf.c create mode 100644 src/builtin.c create mode 100644 src/main.c create mode 100644 src/session.c create mode 100644 src/tool.c create mode 100644 src/transport.c create mode 100644 src/transport_curl.c create mode 100644 src/www/index.html create mode 100644 tests/test_agent.c create mode 100644 tests/test_buf.c create mode 100644 tests/test_session.c create mode 100644 tests/test_tool.c create mode 100644 tests/test_transport.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e76a67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o +splinter +tests/test_buf +tests/test_tool +tests/test_session +tests/test_transport +tests/test_agent diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9767652 --- /dev/null +++ b/Makefile @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..b35fb67 --- /dev/null +++ b/README.md @@ -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/.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 + request observe + reply answer + reply tool + +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. diff --git a/docs/splinter.1 b/docs/splinter.1 new file mode 100644 index 0000000..72f6fa7 --- /dev/null +++ b/docs/splinter.1 @@ -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-