Files
splinter/src/backend/agents.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

503 lines
12 KiB
C

/* splinter -- agent profiles; see agents.h. */
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "agents.h"
#include "log.h"
#define MAX_AGENTS 32
static char *
trim(char *s)
{
char *e;
while (*s == ' ' || *s == '\t')
s++;
e = s + strlen(s);
while (e > s && (e[-1] == ' ' || e[-1] == '\t' || e[-1] == '\r' ||
e[-1] == '\n'))
*--e = '\0';
return s;
}
/* "[a, b]" or a, "quoted": comma list without brackets or quotes */
static void
list_value(const char *v, char *out, size_t cap)
{
size_t n = 0;
for (; *v != '\0' && n + 1 < cap; v++) {
if (*v == '[' || *v == ']' || *v == '"' || *v == '\'')
continue;
if (*v == ' ' && (n == 0 || out[n - 1] == ','))
continue;
out[n++] = *v;
}
while (n > 0 && (out[n - 1] == ' ' || out[n - 1] == ','))
n--;
out[n] = '\0';
}
static int
truthy(const char *v)
{
return !strcmp(v, "true") || !strcmp(v, "yes") || !strcmp(v, "on") ||
!strcmp(v, "1");
}
static int
add_def(struct sp_agents *a, const struct sp_agent_def *d)
{
size_t i;
for (i = 0; i < a->n; i++)
if (strcmp(a->v[i].name, d->name) == 0) {
/* the later source replaces the earlier one */
free(a->v[i].prompt);
a->v[i] = *d;
return 0;
}
if (a->n == MAX_AGENTS) {
free(d->prompt);
return -1;
}
if (a->v == NULL) {
a->v = calloc(MAX_AGENTS, sizeof(*a->v));
if (a->v == NULL) {
free(d->prompt);
return -1;
}
}
a->v[a->n++] = *d;
return 0;
}
/* is 'name' an entry of the comma list 'list'? */
static int
in_list(const char *list, const char *name, size_t nlen)
{
const char *p = list;
while (*p != '\0') {
const char *e = strchr(p, ',');
size_t n = e != NULL ? (size_t)(e - p) : strlen(p);
if (n == nlen && strncmp(p, name, n) == 0)
return 1;
if (e == NULL)
break;
p = e + 1;
}
return 0;
}
/*
* A profile from a project directory comes with whatever repository was
* cloned, so it must not give itself more than the user agreed to: it never
* turns on autonomous mode, and when it replaces a profile (the built-in plan,
* or one of the user's) it keeps that profile's limits: tools no wider, allow
* only what was allowed before, deny everything denied before.
*/
static void
narrow_untrusted(const struct sp_agents *a, struct sp_agent_def *d)
{
const struct sp_agent_def *o = NULL;
char allow[sizeof(d->allow)];
const char *p;
size_t i, n = 0;
if (d->autonomous > 0) {
sp_logf("agents", "%s: autonomous ignored in a project "
"profile; set it in ~/.splinter/agents or the config",
d->source);
d->autonomous = -1;
}
for (i = 0; i < a->n; i++)
if (strcmp(a->v[i].name, d->name) == 0)
o = &a->v[i];
if (o == NULL)
return;
if (o->autonomous == 0)
d->autonomous = 0;
if (o->max_auth >= 0 && (d->max_auth < 0 || d->max_auth > o->max_auth)) {
sp_logf("agents", "%s: tools no wider than %s allows",
d->source, o->source);
d->max_auth = o->max_auth;
}
/* allow: only names the replaced profile allowed too */
allow[0] = '\0';
for (p = d->allow; *p != '\0'; ) {
const char *e = strchr(p, ',');
size_t len = e != NULL ? (size_t)(e - p) : strlen(p);
if (len > 0 && in_list(o->allow, p, len) &&
n + len + 2 < sizeof(allow)) {
if (n > 0)
allow[n++] = ',';
memcpy(allow + n, p, len);
n += len;
allow[n] = '\0';
}
if (e == NULL)
break;
p = e + 1;
}
if (strcmp(allow, d->allow) != 0)
sp_logf("agents", "%s: allow narrowed to what %s allows",
d->source, o->source);
snprintf(d->allow, sizeof(d->allow), "%s", allow);
/* deny: the old list plus the new one */
for (p = o->deny; *p != '\0'; ) {
const char *e = strchr(p, ',');
size_t len = e != NULL ? (size_t)(e - p) : strlen(p);
size_t dl = strlen(d->deny);
if (len > 0 && !in_list(d->deny, p, len) &&
dl + len + 2 < sizeof(d->deny)) {
if (dl > 0)
d->deny[dl++] = ',';
memcpy(d->deny + dl, p, len);
d->deny[dl + len] = '\0';
}
if (e == NULL)
break;
p = e + 1;
}
}
/* one markdown file into a definition; 1 added, 0 skipped */
static int
read_file(struct sp_agents *a, const char *path, const char *name,
int trusted)
{
FILE *f = fopen(path, "r");
char line[2048];
struct sp_agent_def d;
struct sp_buf body;
int in_front = 0, seen_front = 0, subagent = 0, first = 1;
if (f == NULL)
return 0;
memset(&d, 0, sizeof(d));
d.max_auth = -1;
d.autonomous = -1;
snprintf(d.name, sizeof(d.name), "%s", name);
snprintf(d.source, sizeof(d.source), "%s", path);
if (sp_buf_init(&body) != 0) {
fclose(f);
return 0;
}
while (fgets(line, sizeof(line), f) != NULL) {
char *l = line;
if (first && strncmp(l, "---", 3) == 0 && trim(l + 3)[0] == '\0') {
in_front = 1;
seen_front = 1;
first = 0;
continue;
}
first = 0;
if (in_front) {
char *colon, *k, *v;
if (strncmp(l, "---", 3) == 0 && trim(l + 3)[0] == '\0') {
in_front = 0;
continue;
}
if (*l == ' ' || *l == '\t' || *l == '#')
continue; /* nested yaml: not ours */
colon = strchr(l, ':');
if (colon == NULL)
continue;
*colon = '\0';
k = trim(l);
v = trim(colon + 1);
if (!strcmp(k, "description")) {
size_t n;
if (*v == '"' || *v == '\'')
v++;
snprintf(d.description, sizeof(d.description), "%s", v);
n = strlen(d.description);
if (n > 0 && (d.description[n - 1] == '"' ||
d.description[n - 1] == '\''))
d.description[n - 1] = '\0';
}
else if (!strcmp(k, "model"))
list_value(v, d.model, sizeof(d.model));
else if (!strcmp(k, "tools")) {
char t[32];
list_value(v, t, sizeof(t));
if (!strcmp(t, "read"))
d.max_auth = 0;
else if (!strcmp(t, "mutate"))
d.max_auth = 1;
else if (!strcmp(t, "all"))
d.max_auth = -1;
} else if (!strcmp(k, "allow"))
list_value(v, d.allow, sizeof(d.allow));
else if (!strcmp(k, "deny"))
list_value(v, d.deny, sizeof(d.deny));
else if (!strcmp(k, "autonomous"))
d.autonomous = truthy(v);
else if (!strcmp(k, "mode")) {
char t[32];
list_value(v, t, sizeof(t));
subagent = !strcmp(t, "subagent");
}
continue;
}
sp_buf_puts(&body, l);
}
fclose(f);
(void)seen_front;
if (subagent) {
sp_buf_free(&body);
return 0;
}
{
char *p = body.p != NULL ? trim(body.p) : (char *)"";
d.prompt = strdup(p);
}
sp_buf_free(&body);
if (d.prompt == NULL)
return 0;
if (!trusted)
narrow_untrusted(a, &d);
return add_def(a, &d) == 0;
}
static void
read_dir(struct sp_agents *a, const char *dir, int trusted)
{
DIR *dp = opendir(dir);
struct dirent *e;
char *names[64];
size_t n = 0, i;
if (dp == NULL)
return;
while ((e = readdir(dp)) != NULL && n < 64) {
size_t len = strlen(e->d_name);
if (len < 4 || strcmp(e->d_name + len - 3, ".md") != 0 ||
e->d_name[0] == '.')
continue;
if (!strcasecmp(e->d_name, "README.md") ||
!strcasecmp(e->d_name, "AGENTS.md"))
continue;
names[n] = strdup(e->d_name);
if (names[n] != NULL)
n++;
}
closedir(dp);
for (i = 0; i < n; i++) {
char path[900], name[48];
size_t len = strlen(names[i]) - 3;
if (len >= sizeof(name))
len = sizeof(name) - 1;
memcpy(name, names[i], len);
name[len] = '\0';
snprintf(path, sizeof(path), "%.700s/%.150s", dir, names[i]);
free(names[i]);
(void)read_file(a, path, name, trusted);
}
}
static int
cmp_def(const void *x, const void *y)
{
return strcmp(((const struct sp_agent_def *)x)->name,
((const struct sp_agent_def *)y)->name);
}
int
sp_agents_load(struct sp_agents *a, const char *cwd, const char *home)
{
struct sp_agent_def b, p;
char dirs[17][700];
int trusted[17];
size_t nd = 0, i;
char cur[700];
memset(a, 0, sizeof(*a));
memset(&b, 0, sizeof(b));
snprintf(b.name, sizeof(b.name), "build");
snprintf(b.description, sizeof(b.description),
"full access: reads, changes, builds, tests");
b.max_auth = -1;
b.autonomous = -1;
b.prompt = strdup("");
snprintf(b.source, sizeof(b.source), "(built in)");
memset(&p, 0, sizeof(p));
snprintf(p.name, sizeof(p.name), "plan");
snprintf(p.description, sizeof(p.description),
"read-only: analyse, weigh, plan; changes nothing");
p.max_auth = 0;
p.autonomous = -1;
snprintf(p.allow, sizeof(p.allow), "delegate*,plan_task_*");
p.prompt = strdup("You are in PLAN mode. You may read, search and "
"analyse, but you change nothing: no edits, no commands that "
"modify state. Work out what is wrong or what to do, weigh the "
"options and end with a concrete plan: steps, files, risks. Ask "
"the user where a decision is theirs. For analysis and decisions "
"you may hand the question to another model with the delegate "
"tool. The user switches to build mode (Tab) to have it done.");
snprintf(p.source, sizeof(p.source), "(built in)");
if (b.prompt == NULL || p.prompt == NULL) {
free(b.prompt);
free(p.prompt);
return -1;
}
add_def(a, &b);
add_def(a, &p);
/*
* The user's, then the project's from far to near: the nearest wins.
* Only what lives in the home directory is the user's own; the rest
* came with a checkout and is narrowed (narrow_untrusted).
*/
if (home != NULL && *home != '\0') {
trusted[nd] = 1;
snprintf(dirs[nd++], sizeof(dirs[0]), "%s/.splinter/agents", home);
}
if (cwd != NULL && *cwd != '\0') {
char chain[8][700];
size_t nc = 0, k;
snprintf(cur, sizeof(cur), "%s", cwd);
while (nc < 8) {
snprintf(chain[nc++], sizeof(chain[0]), "%s", cur);
if (home != NULL && strcmp(cur, home) == 0)
break;
{
char *slash = strrchr(cur, '/');
if (slash == NULL || slash == cur)
break;
*slash = '\0';
}
}
/* chain[0] is the nearest; 8 levels always fit, far first */
for (k = nc; k > 0 && nd + 2 <= 17; k--) {
int t = home != NULL && *home != '\0' &&
strcmp(chain[k - 1], home) == 0;
trusted[nd] = t;
snprintf(dirs[nd++], sizeof(dirs[0]), "%s/.agents",
chain[k - 1]);
trusted[nd] = t;
snprintf(dirs[nd++], sizeof(dirs[0]), "%s/.splinter/agents",
chain[k - 1]);
}
}
for (i = 0; i < nd; i++) {
read_dir(a, dirs[i], trusted[i]);
}
/* build and plan stay first; the rest by name */
if (a->n > 2)
qsort(a->v + 2, a->n - 2, sizeof(*a->v), cmp_def);
return 0;
}
void
sp_agents_free(struct sp_agents *a)
{
size_t i;
for (i = 0; i < a->n; i++)
free(a->v[i].prompt);
free(a->v);
memset(a, 0, sizeof(*a));
}
int
sp_agents_find(const struct sp_agents *a, const char *name)
{
size_t i;
for (i = 0; name != NULL && i < a->n; i++)
if (strcmp(a->v[i].name, name) == 0)
return (int)i;
return -1;
}
void
sp_agent_policy(const struct sp_agent_def *d, struct sp_tool_policy *p)
{
memset(p, 0, sizeof(*p));
snprintf(p->name, sizeof(p->name), "%s", d->name);
p->max_auth = d->max_auth;
snprintf(p->allow, sizeof(p->allow), "%s", d->allow);
snprintf(p->deny, sizeof(p->deny), "%s", d->deny);
}
int
sp_agent_prompt(const struct sp_agent_def *d, const char *base,
struct sp_buf *out)
{
if (sp_buf_puts(out, base != NULL ? base : "") != 0)
return -1;
if (d->prompt != NULL && d->prompt[0] != '\0')
return sp_buf_printf(out, "\n\n[agent: %s]\n%s", d->name, d->prompt);
return 0;
}
char *
sp_agents_base(const struct sp_conv *conv)
{
if (conv != NULL && conv->n > 0 && conv->v[0].role != NULL &&
strcmp(conv->v[0].role, "system") == 0 && conv->v[0].content != NULL)
return strdup(conv->v[0].content);
return strdup("");
}
int
sp_agents_activate(struct sp_agent *ag, const struct sp_agent_def *d,
const char *base, struct sp_tool_policy *pol)
{
struct sp_buf b;
int rc = 0;
sp_agent_policy(d, pol);
ag->policy = pol;
if (ag->conv == NULL)
return 0;
if (sp_buf_init(&b) != 0)
return -1;
if (sp_agent_prompt(d, base, &b) != 0) {
sp_buf_free(&b);
return -1;
}
if (ag->conv->n > 0 && ag->conv->v[0].role != NULL &&
strcmp(ag->conv->v[0].role, "system") == 0) {
char *nc = strdup(b.p != NULL ? b.p : "");
if (nc == NULL)
rc = -1;
else {
free(ag->conv->v[0].content);
ag->conv->v[0].content = nc;
}
} else if (b.len > 0) {
struct sp_msg m;
memset(&m, 0, sizeof(m));
m.role = "system";
m.content = b.p;
rc = sp_conv_insert(ag->conv, 0, &m);
}
sp_buf_free(&b);
return rc;
}