The harness had echo, run and clock - a chat with a shell. A coding agent needs
to look at a file, change one, find something, list a directory:
read (read) numbered lines, offset/limit, refuses binary, says where it
stopped
search (read) literal search over a tree, capped at 100 hits
list (read) a directory
write (mutate) up to 4 MiB
edit (mutate) exactly one occurrence; a non-unique 'old' is an error
run (destructive, already there)
Approval was dead code in the chat: ask_operator() read stdin, which a curses
program cannot do without eating its own keystrokes, so with --autonomous off
every mutate tool was silently denied ('fail closed'). The agent now asks through
a callback; the UI puts the question in the prompt line and parks the turn
thread on a condition variable until y or n arrives.
Measured against kontron: prompt appeared 6 s into the turn, 'y' ran the tool and
the answer carried its output; 'n' left the target file absent and the model saw
'error: denied by operator'.
test_builtin covers the refusals, not just the happy path: ambiguous edit writes
nothing, binary read reports instead of spraying, missing arguments say what is
required. README no longer claims there is no tool-call parser and no compaction.
248 lines
6.3 KiB
C
248 lines
6.3 KiB
C
/*
|
|
* splinter -- the turn loop.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* One turn: append the user message, ask the transport, run the tools it asks
|
|
* for, repeat until it answers or the step limit is hit. The conversation
|
|
* lives in sp_conv and survives the turn, so splinter is not an amnesiac
|
|
* one-shot.
|
|
*
|
|
* Invariants, in this order, before any tool runs:
|
|
* 1. the conversation fits the byte 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 denial is reported back
|
|
* as the observation, not silently skipped.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
|
|
static int
|
|
ask_operator(struct sp_agent *a, const struct sp_tool *t, const char *args)
|
|
{
|
|
char line[64];
|
|
|
|
if (a->cfg->approve != NULL)
|
|
return a->cfg->approve(a->cfg->approve_ctx, t->name, (int)t->auth,
|
|
args);
|
|
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');
|
|
}
|
|
|
|
/* The observation is always written: an error is an observation too. */
|
|
static void
|
|
run_one_call(struct sp_agent *a, const struct sp_toolcall *call,
|
|
struct sp_buf *obs)
|
|
{
|
|
const struct sp_tool *t;
|
|
|
|
a->stats.tool_calls++;
|
|
t = sp_registry_find(a->reg, call->name);
|
|
if (t == NULL) {
|
|
sp_buf_printf(obs, "error: unknown tool '%s'", call->name);
|
|
return;
|
|
}
|
|
if (t->auth != SP_AUTH_READ && !a->cfg->autonomous &&
|
|
!ask_operator(a, t, call->args)) {
|
|
a->stats.denied++;
|
|
sp_buf_puts(obs, "error: denied by operator");
|
|
return;
|
|
}
|
|
if (t->run(call->args, obs, t->ctx) != 0)
|
|
sp_buf_printf(obs, "error: tool '%s' failed", call->name);
|
|
}
|
|
|
|
int
|
|
sp_agent_run(struct sp_agent *a, const char *user_text, struct sp_buf *answer,
|
|
struct sp_buf *err)
|
|
{
|
|
struct sp_conv local;
|
|
struct sp_conv *conv;
|
|
struct sp_buf obs;
|
|
size_t step, i;
|
|
int rc = -1;
|
|
|
|
if (a == NULL || a->reg == NULL || a->cfg == NULL)
|
|
return -1;
|
|
if (a->cfg->max_steps == 0)
|
|
return -1;
|
|
|
|
if (a->conv != NULL) {
|
|
conv = a->conv;
|
|
} else {
|
|
sp_conv_init(&local);
|
|
conv = &local;
|
|
}
|
|
|
|
sp_buf_clear(answer);
|
|
/* the turn counter is what a plugin like 'focus' schedules against */
|
|
if (a->host != NULL)
|
|
sp_host_turn(a->host);
|
|
if (sp_buf_init(&obs) != 0) {
|
|
sp_buf_puts(err, "agent: out of memory");
|
|
if (conv == &local)
|
|
sp_conv_free(&local);
|
|
return -1;
|
|
}
|
|
if (sp_conv_push(conv, "user", NULL, user_text) != 0) {
|
|
sp_buf_puts(err, "agent: out of memory");
|
|
goto out;
|
|
}
|
|
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;
|
|
size_t sent;
|
|
|
|
memset(&reply, 0, sizeof(reply));
|
|
if (a->cfg->cancel != NULL && *a->cfg->cancel) {
|
|
sp_buf_puts(err, "agent: cancelled by operator");
|
|
goto out;
|
|
}
|
|
if (a->cfg->steer != NULL) {
|
|
struct sp_buf steer;
|
|
|
|
if (sp_buf_init(&steer) != 0)
|
|
goto oom;
|
|
if (a->cfg->steer(a->cfg->steer_ctx, &steer) > 0 &&
|
|
steer.len > 0) {
|
|
if (sp_conv_push(conv, "user", NULL, steer.p) != 0) {
|
|
sp_buf_free(&steer);
|
|
goto oom;
|
|
}
|
|
if (a->sess != NULL)
|
|
(void)sp_session_append(a->sess, "user",
|
|
steer.p, err);
|
|
}
|
|
sp_buf_free(&steer);
|
|
}
|
|
/* compaction happens before the budget is judged, never after */
|
|
if (a->host != NULL &&
|
|
sp_host_compact(a->host, conv, a->cfg->max_bytes) > 0)
|
|
a->stats.compactions++;
|
|
sent = sp_conv_bytes(conv);
|
|
if (a->cfg->max_bytes > 0 && sent > a->cfg->max_bytes) {
|
|
sp_buf_printf(err, "agent: conversation budget exceeded "
|
|
"(~%zu > %zu bytes); nothing sent", sent,
|
|
a->cfg->max_bytes);
|
|
goto out;
|
|
}
|
|
a->stats.bytes_sent += sent;
|
|
a->stats.steps++;
|
|
|
|
{
|
|
int sent_rc;
|
|
|
|
if (a->cfg->stream != NULL &&
|
|
a->tr.send_stream != NULL)
|
|
sent_rc = a->tr.send_stream(a->tr.ctx, conv,
|
|
&reply, err, a->cfg->stream,
|
|
a->cfg->stream_ctx);
|
|
else
|
|
sent_rc = a->tr.send(a->tr.ctx, conv, &reply,
|
|
err);
|
|
if (sent_rc != 0) {
|
|
sp_reply_free(&reply);
|
|
goto out;
|
|
}
|
|
}
|
|
if (reply.status < 200 || reply.status > 299) {
|
|
sp_buf_printf(err, "agent: transport status %ld",
|
|
reply.status);
|
|
sp_reply_free(&reply);
|
|
goto out;
|
|
}
|
|
|
|
a->stats.tokens_in += (size_t)(reply.tokens_in > 0 ?
|
|
reply.tokens_in : 0);
|
|
a->stats.tokens_out += (size_t)(reply.tokens_out > 0 ?
|
|
reply.tokens_out : 0);
|
|
|
|
if (reply.ncalls == 0) {
|
|
const char *text = reply.content != NULL ?
|
|
reply.content : "";
|
|
|
|
if (sp_buf_puts(answer, text) != 0 ||
|
|
sp_conv_push(conv, "assistant", NULL, text) != 0) {
|
|
sp_reply_free(&reply);
|
|
goto oom;
|
|
}
|
|
if (a->sess != NULL &&
|
|
sp_session_append(a->sess, "assistant", text,
|
|
err) != 0) {
|
|
sp_reply_free(&reply);
|
|
goto out;
|
|
}
|
|
sp_reply_free(&reply);
|
|
rc = 0;
|
|
goto out;
|
|
}
|
|
|
|
{
|
|
struct sp_msg am;
|
|
|
|
memset(&am, 0, sizeof(am));
|
|
am.role = "assistant";
|
|
am.content = reply.content;
|
|
am.calls = reply.calls_json;
|
|
if (sp_conv_add(conv, &am) != 0) {
|
|
sp_reply_free(&reply);
|
|
goto oom;
|
|
}
|
|
}
|
|
for (i = 0; i < reply.ncalls; i++) {
|
|
struct sp_msg tm;
|
|
|
|
sp_buf_clear(&obs);
|
|
if (a->cfg->tool_begin != NULL)
|
|
a->cfg->tool_begin(a->cfg->tool_begin_ctx,
|
|
reply.calls[i].name, reply.calls[i].args);
|
|
run_one_call(a, &reply.calls[i], &obs);
|
|
memset(&tm, 0, sizeof(tm));
|
|
tm.role = "tool";
|
|
tm.name = reply.calls[i].name;
|
|
tm.call_id = reply.calls[i].id;
|
|
tm.content = obs.p;
|
|
if (sp_conv_add(conv, &tm) != 0) {
|
|
sp_reply_free(&reply);
|
|
goto oom;
|
|
}
|
|
if (a->sess != NULL &&
|
|
sp_session_append(a->sess, "observation", obs.p,
|
|
err) != 0) {
|
|
sp_reply_free(&reply);
|
|
goto out;
|
|
}
|
|
}
|
|
sp_reply_free(&reply);
|
|
}
|
|
|
|
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(&obs);
|
|
if (conv == &local)
|
|
sp_conv_free(&local);
|
|
return rc;
|
|
}
|