Files
splinter/src/agent.c
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

202 lines
5.2 KiB
C

/*
* 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;
}