First run against a real model (Qwen3.6-35B-A3B on kontron, strata-poly)
exposed two things a stub never would:
- our tool schema sent "properties":{}, which tells the model that no
arguments exist. Tools now carry a real JSON schema (echo/command/clock), and
the loop completes a tool round against the 35B: request 1 -> tool_call,
request 2 -> "Das Ergebnis ist: ping".
- an answer longer than the screen was not drawn at all: the entry did not
'fit', so the viewport skipped it and the chat looked empty. draw_history is
now a real viewport (total height, bottom-anchored, entries taller than the
screen start at the offset that puts their end on screen).
Steering: a turn runs on its own thread while the UI keeps reading keys. Enter
queues text the loop picks up at the next step boundary; Ctrl-C cancels; in a
single-step turn the text becomes the next message instead of disappearing.
Curses remains UI-thread-only. A dangling ag->cfg after the turn made a steer
run twice - the caller's config is now restored, and the session log confirms
one user line per steer.
Transports are plugins now (sp_host_add_transport / --transport). plugins/cli
makes any program the model: SPLINTER_CLI_CMD, run through /bin/sh, prompt on
stdin, stdout as the answer, 4 MiB cap, non-zero exit with the program's stderr,
empty output is an error. Verified with sed, with claude -p (2.9 s, answer OK)
and with cat.
Eleven test binaries green, test_cli included.
221 lines
6.6 KiB
C
221 lines
6.6 KiB
C
/*
|
|
* splinter -- a small C99/POSIX agent harness.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Design rules this header encodes:
|
|
* - no fixed-size buffers anywhere; every growing thing is a struct sp_buf
|
|
* with geometric growth and overflow checks;
|
|
* - a tool declares its authorization class; the harness decides whether it
|
|
* may run, never the model and never a prompt;
|
|
* - a session is append-only JSONL, so a crash can only truncate its tail;
|
|
* - the transport is a vtable, so the whole loop is testable with no network.
|
|
*/
|
|
#ifndef SPLINTER_H
|
|
#define SPLINTER_H
|
|
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#define SP_VERSION "0.1.0"
|
|
|
|
/* ------------------------------------------------------------------ buffer */
|
|
|
|
struct sp_buf {
|
|
char *p;
|
|
size_t len; /* bytes used, excluding the NUL we keep at p[len] */
|
|
size_t cap; /* allocated bytes, always > len */
|
|
};
|
|
|
|
int sp_buf_init(struct sp_buf *);
|
|
int sp_buf_add(struct sp_buf *, const char *, size_t);
|
|
int sp_buf_puts(struct sp_buf *, const char *);
|
|
int sp_buf_printf(struct sp_buf *, const char *, ...);
|
|
int sp_buf_reserve(struct sp_buf *, size_t);
|
|
void sp_buf_clear(struct sp_buf *);
|
|
void sp_buf_free(struct sp_buf *);
|
|
|
|
/* --------------------------------------------------------------- escaping */
|
|
|
|
/* Append s as a JSON string body (no surrounding quotes). */
|
|
int sp_json_escape(struct sp_buf *, const char *);
|
|
|
|
/* --------------------------------------------------------------- tools */
|
|
|
|
enum sp_auth {
|
|
SP_AUTH_READ = 0, /* may run without asking */
|
|
SP_AUTH_MUTATE, /* changes files: needs operator consent */
|
|
SP_AUTH_DESTRUCTIVE /* runs commands: needs operator consent */
|
|
};
|
|
|
|
struct sp_tool;
|
|
|
|
/* Return 0 on success. The observation is written into out. */
|
|
typedef int (*sp_tool_run)(const char *args_json, struct sp_buf *out,
|
|
void *ctx);
|
|
|
|
struct sp_tool {
|
|
const char *name;
|
|
const char *description;
|
|
const char *params; /* JSON schema of the arguments, may be NULL */
|
|
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 *);
|
|
|
|
/* ---------------------------------------------------------- conversation */
|
|
|
|
struct sp_msg {
|
|
char *role; /* system | user | assistant | tool */
|
|
char *name; /* tool name when role is "tool" */
|
|
char *call_id; /* tool_call id, when role is "tool" */
|
|
char *content;
|
|
char *calls; /* raw tool_calls JSON of an assistant turn */
|
|
};
|
|
|
|
struct sp_conv {
|
|
struct sp_msg *v;
|
|
size_t n;
|
|
size_t cap;
|
|
};
|
|
|
|
void sp_conv_init(struct sp_conv *);
|
|
void sp_conv_free(struct sp_conv *);
|
|
int sp_conv_add(struct sp_conv *, const struct sp_msg *);
|
|
int sp_conv_insert(struct sp_conv *, size_t at, const struct sp_msg *);
|
|
int sp_conv_push(struct sp_conv *, const char *role, const char *name,
|
|
const char *content);
|
|
size_t sp_conv_bytes(const struct sp_conv *);
|
|
int sp_conv_trim(struct sp_conv *, size_t keep_last);
|
|
|
|
/* ------------------------------------------------------------- transport */
|
|
|
|
struct sp_toolcall {
|
|
char *id;
|
|
char *name;
|
|
char *args; /* the arguments as a JSON text */
|
|
};
|
|
|
|
struct sp_reply {
|
|
long status;
|
|
char *content; /* assistant text, may be "" */
|
|
struct sp_toolcall *calls;
|
|
char *calls_json; /* the provider's own tool_calls array */
|
|
size_t ncalls;
|
|
};
|
|
|
|
/*
|
|
* A transport turns a whole conversation into one request and one reply. The
|
|
* seam is the conversation, not a raw string: a provider adapter cannot invent
|
|
* history, and the loop cannot depend on a vendor's JSON.
|
|
*/
|
|
struct sp_transport {
|
|
int (*send)(void *ctx, const struct sp_conv *, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
void *ctx;
|
|
};
|
|
|
|
void sp_reply_free(struct sp_reply *);
|
|
|
|
/* 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;
|
|
|
|
/*
|
|
* Steering: while a turn runs, the operator may type. The UI cannot
|
|
* interrupt a request that is already on the wire, so the text is picked
|
|
* up at the next step boundary -- stated here, not discovered later.
|
|
* `cancel` is read before every request; `steer` is asked for text.
|
|
*/
|
|
volatile int *cancel;
|
|
int (*steer)(void *ctx, struct sp_buf *out);
|
|
void *steer_ctx;
|
|
};
|
|
|
|
struct sp_agent_stats {
|
|
size_t steps;
|
|
size_t compactions;
|
|
size_t tool_calls;
|
|
size_t denied;
|
|
size_t bytes_sent;
|
|
};
|
|
|
|
struct sp_host; /* plugin host, see splinter_plugin.h */
|
|
|
|
struct sp_agent {
|
|
struct sp_registry *reg;
|
|
struct sp_host *host; /* optional: compactors live there */
|
|
struct sp_transport tr;
|
|
const struct sp_agent_config *cfg;
|
|
struct sp_session_info *sess;
|
|
struct sp_conv *conv; /* optional: history lives here */
|
|
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_openai.c -- OpenAI-compatible chat completions, via curl(1) */
|
|
struct sp_transport sp_openai_transport(const char *url, const char *model,
|
|
const char *api_key, const struct sp_registry *, long timeout_s);
|
|
|
|
/* openai.c -- pure JSON shaping, testable without a network */
|
|
int sp_openai_request(const struct sp_conv *, const char *model,
|
|
const struct sp_registry *, struct sp_buf *out);
|
|
int sp_openai_reply(const char *json, struct sp_reply *out,
|
|
struct sp_buf *err);
|
|
|
|
#endif /* SPLINTER_H */
|