env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
5.6 KiB
C
187 lines
5.6 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 <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);
|
|
}
|
|
|
|
if (failures == 0)
|
|
printf("test_delegate: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|