Files
splinter/tests/test_delegate.c
T
lotherkandClaude Opus 5.5 ca8cdb6d13 delegate_parallel: a task at the limit moves to the next delegate
A parallel child that answers with its limit benches its delegate, like
the single call, and its task is spawned again in the same slot on the
next delegate that fits - the request's tags plus the benched one's -
while the other tasks keep running. A fallback already at its
max_parallel among the running jobs is not taken. The task header says
what happened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 20:35:02 +00:00

319 lines
10 KiB
C

/*
* splinter -- delegate selection.
*
* The choice is code, so it is tested like code: given a task and a set of
* delegates, the same one must win every time, and the reason must name what
* decided it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
add(struct sp_config *c, const char *id, const char *about, const char *tags)
{
struct sp_delegate *d = &c->delegate[c->ndelegates++];
memset(d, 0, sizeof(*d));
snprintf(d->id, sizeof(d->id), "%s", id);
snprintf(d->about, sizeof(d->about), "%s", about);
snprintf(d->tags, sizeof(d->tags), "%s", tags);
snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1");
snprintf(d->model, sizeof(d->model), "m");
}
int
main(void)
{
struct sp_config c;
char id[64], why[192];
int score;
memset(&c, 0, sizeof(c));
add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review");
add(&c, "quick", "kurze Antworten, Chat", "chat,cheap,summary");
/* a tag in the task wins by tag */
CHECK(sp_delegate_choose(&c, "pruefe den code auf fehler", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
CHECK(score >= 3);
CHECK(strstr(why, "code") != NULL);
CHECK(sp_delegate_choose(&c, "mach eine kurze summary", NULL, id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
/* a requested tag outweighs the wording */
CHECK(sp_delegate_choose(&c, "irgendwas mit code", "chat", id, sizeof(id),
why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "quick") == 0);
CHECK(strstr(why, "requested") != NULL);
/* a description word is weaker than a tag but still counts */
CHECK(sp_delegate_choose(&c, "kennt jemand den quellbaum", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "coder") == 0);
/* nothing matches: no pick rather than a random one */
CHECK(sp_delegate_choose(&c, "xyzzy", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/* prio decides a tie: same score, higher prio wins */
memset(&c, 0, sizeof(c));
add(&c, "low", "gleich gut", "x");
add(&c, "high", "gleich gut", "x");
c.delegate[1].prio = 9;
CHECK(sp_delegate_choose(&c, "x", NULL, id, sizeof(id), why, sizeof(why),
&score) == 0);
CHECK(strcmp(id, "high") == 0);
CHECK(sp_delegate_prio(&c, "high") == 9);
/* max_parallel defaults to one and never to zero */
memset(&c, 0, sizeof(c));
add(&c, "d", "x", "x");
CHECK(c.delegate[0].max_parallel == 0); /* add() does not set it */
/* an empty set is not a match either */
c.ndelegates = 0;
CHECK(sp_delegate_choose(&c, "code", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/*
* ---- a remote delegate: the task reaches the far side as one word ----
* ssh joins its arguments into a command line for the remote shell. A
* fake ssh in PATH plays that shell: it re-splits the line with eval,
* which runs anything an unquoted task would smuggle in, and records the
* words it got.
*/
{
char tdir[] = "/var/tmp/splinter-del-XXXXXX";
char path[600], mark[600], words[600], fake[1600];
char task[700], args[1200], line[800];
const char *oldpath = getenv("PATH");
struct sp_registry r;
struct sp_buf out;
struct stat st;
FILE *f;
int n = 0, task_ok = 0, env_ok = 0;
CHECK(mkdtemp(tdir) != NULL);
snprintf(mark, sizeof(mark), "%s/pwned", tdir);
snprintf(words, sizeof(words), "%s/words", tdir);
snprintf(path, sizeof(path), "%s/ssh", tdir);
snprintf(fake, sizeof(fake), "#!/bin/sh\n"
"[ \"$3\" = -- ] && [ \"$4\" = farhost ] || exit 3\n"
"eval \"set -- $5\"\n"
"printf '%%s\\n' \"$@\" > '%s'\n"
"echo fertig\n", words);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs(fake, f);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
memset(&c, 0, sizeof(c));
add(&c, "far", "remote", "far");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "farhost");
c.delegate[0].max_parallel = 1;
/* env= travels in front as `env 'NAME=VALUE'`, quoted like the rest */
snprintf(c.delegate[0].env[0], sizeof(c.delegate[0].env[0]),
"TOK=a b'c");
c.delegate[0].nenv = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
snprintf(task, sizeof(task), "it's x'; touch %s; echo '", mark);
snprintf(args, sizeof(args), "{\"task\":\"%s\",\"tags\":\"far\"}",
task);
CHECK(sp_buf_init(&out) == 0);
{
const struct sp_tool *t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, &out, t->ctx);
}
CHECK(stat(mark, &st) != 0);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "delegate said: %s\n", out.p);
f = fopen(words, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(line, sizeof(line), f) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (n == 0 && strcmp(line, "env") == 0)
env_ok = 1;
if (n == 1 && strcmp(line, "TOK=a b'c") == 0)
env_ok++;
if (n == 5 && strcmp(line, task) == 0)
task_ok = 1;
n++;
}
fclose(f);
}
CHECK(task_ok);
CHECK(env_ok == 2);
sp_buf_free(&out);
sp_registry_free(&r);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
/*
* ---- a delegate at its limit is benched and the task goes on ----
* Fake ssh: host "spent" answers like claude with an empty account, host
* "fresh" answers. Two delegates fit the same task; the better one is the
* spent one.
*/
{
struct sp_config c;
struct sp_registry r;
struct sp_buf out;
char tdir[64] = "/tmp/splinter-cool-XXXXXX";
char path[400], line[400], cool[100];
const char *oldpath = getenv("PATH");
const struct sp_tool *t;
FILE *f;
long until = 0;
CHECK(mkdtemp(tdir) != NULL);
snprintf(path, sizeof(path), "%s/ssh", tdir);
f = fopen(path, "w");
CHECK(f != NULL);
if (f != NULL) {
fprintf(f, "#!/bin/sh\n"
"echo \"$4\" >> '%s/hosts'\n"
"if [ \"$4\" = spent ]; then\n"
" echo \"You've hit your weekly limit · resets 3am"
" (Europe/Lisbon)\"; exit 1\n"
"fi\n"
"echo 'antwort von fresh'\n", tdir);
fclose(f);
}
(void)chmod(path, 0755);
snprintf(line, sizeof(line), "%s:%s", tdir,
oldpath != NULL ? oldpath : "/usr/bin:/bin");
(void)setenv("PATH", line, 1);
snprintf(cool, sizeof(cool), "%s/cool", tdir);
(void)setenv("SPLINTER_COOLDOWN_DIR", cool, 1);
memset(&c, 0, sizeof(c));
add(&c, "claude0", "first account", "big");
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "spent");
c.delegate[0].prio = 9;
c.delegate[0].max_parallel = 1;
add(&c, "claude1", "second account", "big");
snprintf(c.delegate[1].host, sizeof(c.delegate[1].host), "fresh");
c.delegate[1].prio = 5;
c.delegate[1].max_parallel = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
CHECK(sp_buf_init(&out) == 0);
/* the spent one is tried, benched, and the task still gets done */
if (t != NULL)
(void)t->run("{\"task\":\"x\",\"tags\":\"big\"}", &out,
t->ctx);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "fallback said: %s\n", out.p);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
CHECK(out.p != NULL && strstr(out.p, "falling back to claude1") != NULL);
snprintf(path, sizeof(path), "%s/claude0", cool);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
CHECK(fscanf(f, "%ld", &until) == 1);
fclose(f);
}
/* until the next 3am in Lisbon: in the future, within a day */
CHECK(until > (long)time(NULL));
CHECK(until <= (long)time(NULL) + 86400 + 3600);
/* benched: the next task does not knock on the spent door */
snprintf(path, sizeof(path), "%s/hosts", tdir);
(void)unlink(path);
sp_buf_clear(&out);
if (t != NULL)
(void)t->run("{\"task\":\"y\",\"tags\":\"big\"}", &out,
t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
/* not even when it is asked for by name */
sp_buf_clear(&out);
if (t != NULL)
(void)t->run("{\"task\":\"z\",\"delegate\":\"claude0\"}",
&out, t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort von fresh") != NULL);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(line, sizeof(line), f) != NULL)
CHECK(strncmp(line, "spent", 5) != 0);
fclose(f);
}
/*
* parallel: the spent one fails mid-run, the task moves to the
* next one in the same slot, the other task is not disturbed
*/
snprintf(line, sizeof(line), "rm -rf '%s'", cool);
(void)system(line);
sp_buf_free(&out);
sp_registry_free(&r);
add(&c, "other", "unrelated", "small");
snprintf(c.delegate[2].host, sizeof(c.delegate[2].host), "fresh");
c.delegate[2].max_parallel = 1;
memset(&r, 0, sizeof(r));
CHECK(sp_delegate_register(&r, &c, NULL) == 0);
t = sp_registry_find(&r, "delegate_parallel");
CHECK(t != NULL);
CHECK(sp_buf_init(&out) == 0);
if (t != NULL)
(void)t->run("{\"tasks\":[{\"task\":\"p1\",\"tags\":\"big\"},"
"{\"task\":\"p2\",\"tags\":\"small\"}]}", &out, t->ctx);
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "parallel said: %s\n", out.p);
CHECK(out.p != NULL && strstr(out.p, "[claude0 at its limit") != NULL);
CHECK(out.p != NULL && strstr(out.p, "[falling back to claude1]") != NULL);
CHECK(out.p != NULL && strstr(out.p, "model claude1") != NULL);
CHECK(out.p != NULL && strstr(out.p, "model other") != NULL);
CHECK(out.p != NULL && strstr(out.p, "exit 1") == NULL);
snprintf(path, sizeof(path), "%s/claude0", cool);
CHECK(access(path, F_OK) == 0);
sp_buf_free(&out);
sp_registry_free(&r);
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
(void)unsetenv("SPLINTER_COOLDOWN_DIR");
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
(void)system(line);
}
if (failures == 0)
printf("test_delegate: ok\n");
return failures == 0 ? 0 : 1;
}