delegate: tag weighting by word, synonyms, private as a rule, default as fallback

Substring matching made 'cli' match 'client' and never matched 'gross' to 'big'.
Words now, with German/English synonyms; a private task only goes to a private
delegate; when no tag fits the config's default takes the task.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
lotherkandClaude Sonnet 5.5 committed 2026-11-11 22:34:42 +00:00
1 parent 0b7a91e3b7
commit 1f79ae29dc
3 files changed
+296 -29

No files matched your search

+26
View File
@@ -89,6 +89,32 @@ that fits a member goes to whichever member the pool picks, as long as that
member has every tag of the one that matched (a private task never leaves
the `private` provider). A cli member's pool mates become its fallback.
## How a task finds its provider
`delegate` (and `--choose TEXT`, which shows the verdict) scores every
provider that is not benched or used up:
| | points |
|---|---|
| the model asked for the tag (`tags` argument) | +6 |
| a word of the task is the tag | +3 |
| a word of the task starts with the tag, or is a synonym of it | +2 |
| a word of the provider's `about` is in the task (max +2) | +1 |
Words, not substrings: `client` is not `cli`. Synonyms are built in, German
and English: `big` is also gross, large, komplex, migration; `review` is
pruef, audit; `plan` is entwurf, architektur; `private` is privat, geheim,
secret; and so on for `code`, `small`, `fast`, `cheap`, `local`, `chat`,
`summary`, `research`, `test`, `doc`. A tag counts once.
Two things are rules, not scores. A task that is private (tag `private` or its
synonyms) only goes to a provider tagged `private`; with none available it
fails rather than leaving the house. And when no tag fits at all, `default`
takes the task (unless it is private) instead of an error.
Equal scores go to the provider with the most left of its allowance, then to
the higher `prio`. A pool then picks its member (see above).
## When everything is at its limit
splinter finds out when the limit lifts (from `Retry-After`, from the reset a
+221 -29
View File
@@ -220,26 +220,185 @@ lower(char *dst, size_t cap, const char *src)
* someone is reading the log at night.
*/
static const struct sp_delegate *find_delegate(const struct sp_config *cfg, const char *id);
/*
* How well a delegate fits a task.
*
* The first version asked "does the task contain the tag as text", which is
* wrong in both directions: "cli" is inside "client", and "gross" never
* matched the tag "big". Matching is by word now, with a small table of what
* people write for a tag (German and English), and a few rules that are not
* scores at all.
*
* requested tag (the model asked for it) +6
* a word of the task is the tag +3
* a word of the task starts with the tag +2 (codebase, reviewen)
* a word of the task is a synonym of the tag +2 (gross -> big)
* a word of the description is in the task +1 (at most +2, no filler)
*
* A tag counts once. Rules: a task that is private (privat, geheim, secret ...)
* only goes to a delegate tagged "private" -- a score must never be able to
* send it elsewhere. When nothing matches at all, the config's default takes
* the task, if there is one, instead of an error.
*/
#define FIT_WORDS 96
#define FIT_WORDLEN 40
struct words {
char w[FIT_WORDS][FIT_WORDLEN];
size_t n;
};
static void
split_words(struct words *ws, const char *text)
{
const unsigned char *p = (const unsigned char *)(text != NULL ? text : "");
ws->n = 0;
while (*p != '\0' && ws->n < FIT_WORDS) {
size_t k = 0;
while (*p != '\0' && !isalnum(*p) && *p < 0x80)
p++;
while (*p != '\0' && (isalnum(*p) || *p >= 0x80)) {
if (k + 1 < FIT_WORDLEN)
ws->w[ws->n][k++] = (char)tolower(*p);
p++;
}
if (k > 0) {
ws->w[ws->n][k] = '\0';
ws->n++;
}
}
}
/* what people write for a tag; a synonym matches as the start of a word */
static const struct { const char *tag, *syn; } synonyms[] = {
{ "code", "programm,funktion,function,bug,fehler,compil,refactor,implement,skript,script,klasse,patch" },
{ "review", "pruef,prüf,audit,kontroll,durchsicht,begutacht" },
{ "plan", "planung,entwurf,entwirf,architektur,design,roadmap,strategie" },
{ "big", "gross,groß,large,komplex,umfangreich,gesamt,monorepo,refactor,migrat" },
{ "small", "klein,kurz,short,tiny,winzig" },
{ "fast", "schnell,quick,eilig,zuegig" },
{ "cheap", "billig,guenstig,günstig,sparsam" },
{ "private", "privat,geheim,secret,vertraulich,confidential,intern" },
{ "local", "lokal,offline" },
{ "chat", "frage,plauder,erklaer,erkläre,explain" },
{ "summary", "zusammenfass,summar,tldr" },
{ "research", "recherch,investigat,untersuch" },
{ "test", "pytest,unittest,testfall" },
{ "doc", "dokumentation,documentation,readme,handbuch" },
};
static int
synonym_hit(const char *tag, const char *word)
{
size_t i;
for (i = 0; i < sizeof(synonyms) / sizeof(synonyms[0]); i++) {
char list[256], *s, *save = NULL;
if (strcmp(synonyms[i].tag, tag) != 0)
continue;
snprintf(list, sizeof(list), "%s", synonyms[i].syn);
for (s = strtok_r(list, ",", &save); s != NULL;
s = strtok_r(NULL, ",", &save))
if (strncmp(word, s, strlen(s)) == 0)
return 1;
}
return 0;
}
/* 3 exact, 2 prefix or synonym, 0 none */
static int
word_fit(const char *tag, const char *word)
{
size_t tl = strlen(tag);
if (strcmp(word, tag) == 0)
return 3;
if (tl >= 4 && strncmp(word, tag, tl) == 0)
return 2;
if (synonym_hit(tag, word))
return 2;
return 0;
}
static int
has_tag(const char *tags, const char *tag)
{
char buf[160], *t, *save = NULL;
snprintf(buf, sizeof(buf), "%s", tags);
for (t = strtok_r(buf, ", ", &save); t != NULL;
t = strtok_r(NULL, ", ", &save)) {
char l[64];
lower(l, sizeof(l), t);
if (strcmp(l, tag) == 0)
return 1;
}
return 0;
}
static int
mentions(const struct words *ws, const char *tag)
{
size_t i;
for (i = 0; i < ws->n; i++)
if (word_fit(tag, ws->w[i]) > 0)
return 1;
return 0;
}
static int
filler(const char *w)
{
static const char *const f[] = { "dieses", "diese", "dieser", "einen",
"einer", "eines", "wenn", "wenn", "fuer", "ohne", "oder", "aber",
"nicht", "kein", "keine", "mehr", "sehr", "gute", "guter", "gutes",
"anderes", "andere", "anderen", "konto", "gleiche", "account",
"with", "that", "this", "from", "have", "good", "best", "other",
NULL };
size_t i;
for (i = 0; f[i] != NULL; i++)
if (strcmp(w, f[i]) == 0)
return 1;
return 0;
}
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;
double best_frac = 2;
size_t i;
struct words *tw, *ww, *aw;
int best_score = 0, private_task;
double best_frac = 2;
size_t i;
lower(task_lc, sizeof(task_lc), task != NULL ? task : "");
lower(want_lc, sizeof(want_lc), wanted != NULL ? wanted : "");
if (score_out != NULL)
*score_out = 0;
if (whylen > 0)
why[0] = '\0';
tw = calloc(3, sizeof(*tw));
if (tw == NULL)
return NULL;
ww = tw + 1;
aw = tw + 2;
split_words(tw, task);
split_words(ww, wanted);
private_task = mentions(tw, "private") || mentions(ww, "private");
for (i = 0; i < cfg->ndelegates; i++) {
const struct sp_delegate *d = &cfg->delegate[i];
char tags[160], about_lc[160], *t, *save = NULL;
char tags[160], reason[192] = "", *t, *save = NULL;
int score = 0;
char reason[192] = "";
struct sp_usage u;
size_t k, extra = 0;
if (sp_cooldown_until(d->id) != 0)
continue; /* benched: at its limit until the reset */
@@ -247,44 +406,61 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted,
d->tok_limit, d->tok_window, &u);
if (u.frac >= 1.0)
continue; /* its own allowance is used up */
/* a private task is not a matter of score */
if (private_task && !has_tag(d->tags, "private"))
continue;
snprintf(tags, sizeof(tags), "%s", d->tags);
for (t = strtok_r(tags, ",", &save); t != NULL;
t = strtok_r(NULL, ",", &save)) {
char tag[64];
char tag[64];
int best_w = 0, req = 0, hit;
const char *how = "";
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;
for (k = 0; k < ww->n; k++)
if (word_fit(tag, ww->w[k]) > 0)
req = 1;
for (k = 0; k < tw->n; k++) {
hit = word_fit(tag, tw->w[k]);
if (hit > best_w)
best_w = hit;
}
if (req) {
score += 6;
snprintf(reason, sizeof(reason),
"requested tag '%s'", tag);
} else if (strstr(task_lc, tag) != NULL) {
score += 3;
} else if (best_w > 0) {
score += best_w;
how = best_w == 3 ? "task mentions" :
"task suggests";
if (reason[0] == '\0')
snprintf(reason, sizeof(reason),
"task mentions '%s'", tag);
"%s '%s'", how, tag);
}
}
lower(about_lc, sizeof(about_lc), d->about);
{
char *w, *wsave = NULL;
split_words(aw, d->about);
for (k = 0; k < aw->n && extra < 2; k++) {
size_t j;
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 (strlen(aw->w[k]) < 5 || filler(aw->w[k]))
continue;
for (j = 0; j < tw->n; j++)
if (strcmp(aw->w[k], tw->w[j]) == 0) {
extra++;
if (reason[0] == '\0')
snprintf(reason, sizeof(reason),
"description matches '%s'",
aw->w[k]);
break;
}
}
score += (int)extra;
if (score > best_score ||
(score == best_score && best != NULL &&
(u.frac + 0.05 < best_frac ||
@@ -301,6 +477,22 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted,
}
}
}
free(tw);
/* nothing fits by tag: the config's default takes it, if it may */
if (best == NULL && cfg->default_id[0] != '\0') {
const struct sp_delegate *dd = find_delegate(cfg, cfg->default_id);
if (dd != NULL && sp_cooldown_until(dd->id) == 0 &&
(!private_task || has_tag(dd->tags, "private"))) {
best = dd;
snprintf(why, whylen, "no tag fits, default '%s'",
cfg->default_id);
}
}
if (best == NULL && private_task && whylen > 0)
snprintf(why, whylen, "private task and no available delegate "
"is tagged private");
/*
* A member of a pool stands for the pool: the task fits the member's
* tags, but the one to use is whichever the pool's strategy picks now.
+49
View File
@@ -111,6 +111,9 @@ main(void)
char id[64], why[192];
int score;
/* scoring reads the usage and cooldown files: not the real ones */
setenv("SPLINTER_USAGE_DIR", "/var/tmp/splinter-nonexistent-usage", 1);
setenv("SPLINTER_COOLDOWN_DIR", "/var/tmp/splinter-nonexistent-cd", 1);
test_cli_plugin_path();
memset(&c, 0, sizeof(c));
add(&c, "coder", "C99, POSIX, kennt den Quellbaum", "code,c99,review");
@@ -152,6 +155,52 @@ main(void)
CHECK(strcmp(id, "high") == 0);
CHECK(sp_delegate_prio(&c, "high") == 9);
/*
* ---- tag weighting ----
* Words, not substrings: "client" is not "cli". Synonyms: "gross" is
* "big". A private task goes to a private delegate whatever the score.
*/
memset(&c, 0, sizeof(c));
add(&c, "cli", "terminal programs", "cli");
add(&c, "heavy", "grosse Aufgaben", "big,code");
add(&c, "home", "lokal und privat", "local,private,code");
add(&c, "cloud", "schnell", "cloud,fast,code");
CHECK(sp_delegate_choose(&c, "der client stuerzt ab", NULL, id,
sizeof(id), why, sizeof(why), &score) != 0 ||
strcmp(id, "cli") != 0);
CHECK(sp_delegate_choose(&c, "eine grosse migration", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "heavy") == 0);
CHECK(strstr(why, "big") != NULL);
/* a codebase is code (prefix), a synonym counts less than the tag itself */
CHECK(sp_delegate_choose(&c, "schau in die codebase", NULL, id,
sizeof(id), why, sizeof(why), &score) == 0);
CHECK(score >= 2);
/* private: only the private delegate may take it, even if others score more */
CHECK(sp_delegate_choose(&c, "privater code, grosse migration, schnell",
NULL, id, sizeof(id), why, sizeof(why), &score) == 0);
CHECK(strcmp(id, "home") == 0);
CHECK(sp_delegate_choose(&c, "x", "private", id, sizeof(id), why,
sizeof(why), &score) == 0);
CHECK(strcmp(id, "home") == 0);
/* nobody private is available: an error, not a leak to the cloud */
c.ndelegates = 1;
c.delegate[0].tags[0] = '\0';
snprintf(c.delegate[0].tags, sizeof(c.delegate[0].tags), "code");
CHECK(sp_delegate_choose(&c, "geheimer code", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
CHECK(strstr(why, "private") != NULL);
/* nothing fits: the default takes the task, unless it is private */
memset(&c, 0, sizeof(c));
add(&c, "gen", "allgemein", "general");
add(&c, "spec", "speziell", "special");
snprintf(c.default_id, sizeof(c.default_id), "gen");
CHECK(sp_delegate_choose(&c, "xyzzy plugh", NULL, id, sizeof(id), why,
sizeof(why), &score) == 0);
CHECK(strcmp(id, "gen") == 0 && strstr(why, "default") != NULL);
CHECK(sp_delegate_choose(&c, "xyzzy geheim", NULL, id, sizeof(id), why,
sizeof(why), &score) != 0);
/* max_parallel defaults to one and never to zero */
memset(&c, 0, sizeof(c));
add(&c, "d", "x", "x");