A delegate is a line in the config - id, what it is good at, tags, endpoint, model. No profiles: a profile is a directory of files, and this needs a line you can read at 2 a.m. The delegate tool takes a task and optional tags. The pick is code, not a guess: requested tag 5, tag in the task 3, description word 1, ties to the earlier entry. The observation opens with the choice and the reason, and --choose prints the same decision without running anything: coder 7 task mentions 'code' quick 5 task mentions 'chat' coder 3 task mentions 'refactor' The child is splinter itself in one-shot mode - same tools, own context, own endpoint and model. Classed destructive, because it runs a whole agent; the operator approves the delegation once. test_delegate pins the decisions, including the two cases that must not pick anything: no matching tag and no delegates at all.
348 lines
8.9 KiB
C
348 lines
8.9 KiB
C
/*
|
|
* splinter -- delegation to other models.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* The harness can hand work to another model. Which one is decided here, in
|
|
* code, from the tags and the description configured for each delegate - not by
|
|
* guessing and not by asking a model to pick itself a helper.
|
|
*
|
|
* delegate = coder | C99 und POSIX, kennt den Baum | tags=code,c99 |
|
|
* endpoint=http://kontron.local:8080/v1/chat/completions |
|
|
* model=Qwen3.6-35B
|
|
*
|
|
* The child is splinter itself in one-shot mode: same tools, same rules, own
|
|
* context. Its answer comes back as the observation, with the choice and the
|
|
* reason in front of it, so a wrong pick is visible instead of mysterious.
|
|
*/
|
|
#include <ctype.h>
|
|
#include <errno.h>
|
|
#include <poll.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
#include "config.h"
|
|
|
|
#define DELEGATE_TIMEOUT_MS 300000
|
|
#define DELEGATE_MAX_BYTES 65536
|
|
|
|
struct del_ctx {
|
|
const struct sp_config *cfg;
|
|
char self[512];
|
|
};
|
|
|
|
static void
|
|
lower(char *dst, size_t cap, const char *src)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; src[i] != '\0' && i + 1 < cap; i++)
|
|
dst[i] = (char)tolower((unsigned char)src[i]);
|
|
dst[i] = '\0';
|
|
}
|
|
|
|
/*
|
|
* Pick the delegate that fits the task.
|
|
*
|
|
* A requested tag outweighs a tag found in the task, which outweighs a word of
|
|
* the description. Ties go to the earlier entry: predictable beats clever when
|
|
* someone is reading the log at night.
|
|
*/
|
|
static const struct sp_delegate *
|
|
choose(const struct sp_config *cfg, const char *task, const char *wanted,
|
|
int *score_out, char *why, size_t whylen)
|
|
{
|
|
const struct sp_delegate *best = NULL;
|
|
char task_lc[1024], want_lc[256];
|
|
int best_score = 0;
|
|
size_t i;
|
|
|
|
lower(task_lc, sizeof(task_lc), task != NULL ? task : "");
|
|
lower(want_lc, sizeof(want_lc), wanted != NULL ? wanted : "");
|
|
|
|
for (i = 0; i < cfg->ndelegates; i++) {
|
|
const struct sp_delegate *d = &cfg->delegate[i];
|
|
char tags[160], about_lc[160], *t, *save = NULL;
|
|
int score = 0;
|
|
char reason[192] = "";
|
|
|
|
snprintf(tags, sizeof(tags), "%s", d->tags);
|
|
for (t = strtok_r(tags, ",", &save); t != NULL;
|
|
t = strtok_r(NULL, ",", &save)) {
|
|
char tag[64];
|
|
|
|
lower(tag, sizeof(tag), t);
|
|
while (*tag == ' ')
|
|
memmove(tag, tag + 1, strlen(tag));
|
|
if (tag[0] == '\0')
|
|
continue;
|
|
if (want_lc[0] != '\0' && strstr(want_lc, tag) != NULL) {
|
|
score += 5;
|
|
snprintf(reason, sizeof(reason),
|
|
"requested tag '%s'", tag);
|
|
} else if (strstr(task_lc, tag) != NULL) {
|
|
score += 3;
|
|
if (reason[0] == '\0')
|
|
snprintf(reason, sizeof(reason),
|
|
"task mentions '%s'", tag);
|
|
}
|
|
}
|
|
lower(about_lc, sizeof(about_lc), d->about);
|
|
{
|
|
char *w, *wsave = NULL;
|
|
|
|
for (w = strtok_r(about_lc, " ,.;", &wsave); w != NULL;
|
|
w = strtok_r(NULL, " ,.;", &wsave)) {
|
|
if (strlen(w) < 4)
|
|
continue;
|
|
if (strstr(task_lc, w) == NULL)
|
|
continue;
|
|
score += 1;
|
|
if (reason[0] == '\0')
|
|
snprintf(reason, sizeof(reason),
|
|
"description matches '%s'", w);
|
|
}
|
|
}
|
|
if (score > best_score) {
|
|
best_score = score;
|
|
best = d;
|
|
snprintf(why, whylen, "%s", reason);
|
|
}
|
|
}
|
|
if (score_out != NULL)
|
|
*score_out = best_score;
|
|
return best;
|
|
}
|
|
|
|
/* Run the child and collect its answer. */
|
|
static int
|
|
run_child(const struct del_ctx *c, const struct sp_delegate *d,
|
|
const char *task, struct sp_buf *out, struct sp_buf *err)
|
|
{
|
|
int fds[2];
|
|
pid_t child;
|
|
const char *key_env = d->key_env[0] != '\0' ? d->key_env : NULL;
|
|
char maxbuf[32];
|
|
|
|
if (pipe(fds) != 0) {
|
|
sp_buf_printf(err, "delegate: pipe: %s", strerror(errno));
|
|
return -1;
|
|
}
|
|
child = fork();
|
|
if (child < 0) {
|
|
sp_buf_printf(err, "delegate: fork: %s", strerror(errno));
|
|
close(fds[0]);
|
|
close(fds[1]);
|
|
return -1;
|
|
}
|
|
if (child == 0) {
|
|
char *argv[16];
|
|
int n = 0;
|
|
|
|
(void)dup2(fds[1], STDOUT_FILENO);
|
|
(void)dup2(fds[1], STDERR_FILENO);
|
|
close(fds[0]);
|
|
close(fds[1]);
|
|
snprintf(maxbuf, sizeof(maxbuf), "%d", DELEGATE_MAX_BYTES);
|
|
argv[n++] = (char *)c->self;
|
|
argv[n++] = "--no-config";
|
|
argv[n++] = "-p";
|
|
argv[n++] = (char *)task;
|
|
argv[n++] = "--endpoint";
|
|
argv[n++] = (char *)d->endpoint;
|
|
argv[n++] = "--model";
|
|
argv[n++] = (char *)d->model;
|
|
argv[n++] = "--max-bytes";
|
|
argv[n++] = maxbuf;
|
|
argv[n++] = "--autonomous";
|
|
if (key_env != NULL) {
|
|
argv[n++] = "--api-key-env";
|
|
argv[n++] = (char *)key_env;
|
|
}
|
|
argv[n] = NULL;
|
|
execv(c->self, argv);
|
|
_exit(127);
|
|
}
|
|
close(fds[1]);
|
|
{
|
|
struct pollfd pfd;
|
|
char chunk[4096];
|
|
ssize_t got;
|
|
int done = 0;
|
|
|
|
while (!done) {
|
|
pfd.fd = fds[0];
|
|
pfd.events = POLLIN;
|
|
if (poll(&pfd, 1, DELEGATE_TIMEOUT_MS) <= 0) {
|
|
(void)kill(child, SIGKILL);
|
|
(void)waitpid(child, NULL, 0);
|
|
close(fds[0]);
|
|
sp_buf_printf(err, "delegate: '%s' timed out",
|
|
d->id);
|
|
return -1;
|
|
}
|
|
got = read(fds[0], chunk, sizeof(chunk));
|
|
if (got <= 0)
|
|
break;
|
|
if (out->len + (size_t)got > DELEGATE_MAX_BYTES) {
|
|
size_t room = DELEGATE_MAX_BYTES - out->len;
|
|
|
|
if (room > 0)
|
|
(void)sp_buf_add(out, chunk, room);
|
|
sp_buf_puts(out, "\n[delegate output truncated]\n");
|
|
done = 1;
|
|
break;
|
|
}
|
|
if (sp_buf_add(out, chunk, (size_t)got) != 0)
|
|
break;
|
|
}
|
|
close(fds[0]);
|
|
{
|
|
int st = 0;
|
|
|
|
(void)waitpid(child, &st, 0);
|
|
if (done || (WIFEXITED(st) && WEXITSTATUS(st) != 0)) {
|
|
sp_buf_printf(err, "delegate: '%s' exited %d",
|
|
d->id, WIFEXITED(st) ? WEXITSTATUS(st) : -1);
|
|
return done ? 0 : -1;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_delegate(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct del_ctx *c = ctx;
|
|
struct sp_buf keep, tbuf, reply, err;
|
|
const char *task, *wanted;
|
|
const struct sp_delegate *d;
|
|
char why[192] = "";
|
|
int score = 0;
|
|
|
|
if (sp_buf_init(&keep) != 0 || sp_buf_init(&tbuf) != 0)
|
|
return -1;
|
|
task = NULL;
|
|
wanted = NULL;
|
|
if (args != NULL && args[0] == '{') {
|
|
mcp_json *v = mcp_parse(args);
|
|
|
|
if (v != NULL) {
|
|
const char *t = mcp_arg_string(v, "task");
|
|
const char *g = mcp_arg_string(v, "tags");
|
|
|
|
if (t != NULL)
|
|
(void)sp_buf_puts(&keep, t);
|
|
if (g != NULL)
|
|
(void)sp_buf_puts(&tbuf, g);
|
|
mcp_json_free(v);
|
|
task = keep.p;
|
|
wanted = tbuf.p;
|
|
}
|
|
} else
|
|
task = args;
|
|
|
|
if (task == NULL || *task == '\0') {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return sp_buf_puts(out, "error: 'task' is required");
|
|
}
|
|
if (c->cfg->ndelegates == 0) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return sp_buf_puts(out, "error: no delegate configured "
|
|
"(add a 'delegate = ...' line to the config)");
|
|
}
|
|
d = choose(c->cfg, task, wanted, &score, why, sizeof(why));
|
|
if (d == NULL) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return sp_buf_puts(out, "error: no delegate matches this task");
|
|
}
|
|
|
|
if (sp_buf_init(&reply) != 0 || sp_buf_init(&err) != 0) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return -1;
|
|
}
|
|
sp_buf_printf(out, "[delegate %s, score %d%s%s]\n", d->id, score,
|
|
why[0] != '\0' ? ": " : "", why);
|
|
if (run_child(c, d, task, &reply, &err) != 0) {
|
|
sp_buf_printf(out, "error: %s\n", err.p != NULL ? err.p : "failed");
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return 0;
|
|
}
|
|
sp_buf_printf(out, "%s", reply.p != NULL ? reply.p : "(no answer)");
|
|
sp_buf_free(&reply);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&tbuf);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* The decision on its own, so it can be tested and shown: which delegate fits a
|
|
* task, and why. A pick nobody can explain is a pick nobody can correct.
|
|
*/
|
|
int
|
|
sp_delegate_choose(const struct sp_config *cfg, const char *task,
|
|
const char *tags, char *id, size_t cap, char *why, size_t whylen,
|
|
int *score)
|
|
{
|
|
const struct sp_delegate *d;
|
|
|
|
if (id != NULL && cap > 0)
|
|
id[0] = '\0';
|
|
if (why != NULL && whylen > 0)
|
|
why[0] = '\0';
|
|
d = choose(cfg, task, tags, score, why, whylen);
|
|
if (d == NULL)
|
|
return -1;
|
|
if (id != NULL)
|
|
snprintf(id, cap, "%s", d->id);
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg)
|
|
{
|
|
static struct del_ctx ctx;
|
|
struct sp_tool t;
|
|
ssize_t n;
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
ctx.cfg = cfg;
|
|
n = readlink("/proc/self/exe", ctx.self, sizeof(ctx.self) - 1);
|
|
if (n <= 0)
|
|
snprintf(ctx.self, sizeof(ctx.self), "splinter");
|
|
else
|
|
ctx.self[n] = '\0';
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "delegate";
|
|
t.description = "Hand a task to another configured model and return its "
|
|
"answer. Use it for work another model does better or cheaper.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"task\":{\"type\":\"string\",\"description\":\"what to do, "
|
|
"self-contained\"},"
|
|
"\"tags\":{\"type\":\"string\",\"description\":\"what kind of work, "
|
|
"e.g. code or chat\"}},"
|
|
"\"required\":[\"task\"]}";
|
|
t.auth = SP_AUTH_DESTRUCTIVE; /* it runs a whole agent of its own */
|
|
t.run = tool_delegate;
|
|
t.ctx = &ctx;
|
|
return sp_registry_add(r, &t);
|
|
}
|