rules.set: Pools expandieren und Ziele wie im Dateilader validieren
Der Live-Pfad rules.set speicherte Regeln unverändert: ein Pool-Ziel blieb als Endpoint-Name stehen und landete über ctx_fallback auf dem falschen Modell, unbekannte Ziele und absteigende Judge-Ketten wurden akzeptiert. Die Pool-Expansion und die Regelvalidierung aus j_config_load sind jetzt in j_config_expand_rule zusammengefasst; der Dateilader und j_rule_apply rufen dieselbe Funktion unter g_rules_rw auf. Der Control-Test prüft Pool-Expansion, unbekannten Endpoint und absteigende Judge-Kette. Tests: 430 OK, 0 FAIL.
This commit is contained in:
1 parent
091f95093f
commit
0461179967
4 files changed
+197
-150
No files matched your search
+152
-148
@@ -1045,6 +1045,155 @@ static void auto_endpoints(j_config *cfg)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Expand pool names in one rule and validate it against cfg. The file loader and
|
||||
* the live rules.set path must behave identically: a rule that passes to a pool
|
||||
* means the pool's endpoints, not an endpoint literally named like the pool. */
|
||||
int j_config_expand_rule(j_rule *r, const j_config *cfg, char *err, size_t errlen)
|
||||
{
|
||||
/* A pool may name another pool (all → local → box3, box6). Expand until
|
||||
* nothing changes; the pass limit stops a cycle and lets validation report
|
||||
* the remaining pool name as an unknown endpoint. */
|
||||
for (int pass = 0; pass < 8; pass++) {
|
||||
int changed = 0;
|
||||
for (int t = 0; t < r->target_count; t++) {
|
||||
for (int p = 0; p < cfg->pool_count; p++) {
|
||||
if (strcmp(cfg->pools[p].name, r->targets[t])) continue;
|
||||
const j_pool *pl = &cfg->pools[p];
|
||||
if (r->target_count - 1 + pl->count > J_MAX_TARGETS) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: pool %s does not fit into rule (more than %d targets)",
|
||||
r->line, pl->name, J_MAX_TARGETS);
|
||||
return -1;
|
||||
}
|
||||
/* A one-member pool needs no shift. The old loop copied the
|
||||
* target onto itself (snprintf with overlapping source and
|
||||
* destination), which is undefined behaviour. */
|
||||
if (pl->count > 1) {
|
||||
for (int k = r->target_count - 1; k > t; k--) {
|
||||
snprintf(r->targets[k + pl->count - 1], sizeof r->targets[0],
|
||||
"%s", r->targets[k]);
|
||||
snprintf(r->target_as[k + pl->count - 1], sizeof r->target_as[0],
|
||||
"%s", r->target_as[k]);
|
||||
}
|
||||
}
|
||||
char rule_as[J_GLOB_MAX];
|
||||
snprintf(rule_as, sizeof rule_as, "%s", r->target_as[t]);
|
||||
for (int c = 0; c < pl->count; c++) {
|
||||
snprintf(r->targets[t + c], sizeof r->targets[0], "%s", pl->targets[c]);
|
||||
/* A pool member's own model wins; otherwise the rule's "as"
|
||||
* stays in force for every expanded target. */
|
||||
snprintf(r->target_as[t + c], sizeof r->target_as[0], "%s",
|
||||
pl->target_as[c][0] ? pl->target_as[c] : rule_as);
|
||||
}
|
||||
r->target_count += pl->count - 1;
|
||||
t += pl->count - 1;
|
||||
changed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!changed) break;
|
||||
}
|
||||
|
||||
/* A ctx condition without the scale factor can never be evaluated. */
|
||||
if ((r->ctx_min >= 0 || r->ctx_max >= 0) && cfg->ctx_chars_per_token <= 0) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: rule uses 'ctx' but 'ctx_chars_per_token' is not set",
|
||||
r->line);
|
||||
return -1;
|
||||
}
|
||||
for (int t = 0; t < r->target_count; t++) {
|
||||
int found = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++)
|
||||
if (!strcmp(cfg->endpoints[e].name, r->targets[t])) { found = 1; break; }
|
||||
if (!found) {
|
||||
snprintf(err, errlen, "line %d: rule passes to unknown endpoint '%s'",
|
||||
r->line, r->targets[t]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (r->review_count > 0 && r->block) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: review on a block rule has no target to review", r->line);
|
||||
return -1;
|
||||
}
|
||||
if (r->judge_count > 0) {
|
||||
int max_judge_slots = 0;
|
||||
for (int j = 0; j < r->judge_count; j++) {
|
||||
if (j > 0 && r->judges[j].every <= r->judges[j - 1].every) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge steps must be ascending (%d after %d)",
|
||||
r->line, r->judges[j].every, r->judges[j - 1].every);
|
||||
return -1;
|
||||
}
|
||||
const char *target = r->judges[j].target;
|
||||
if (!target[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs 'every N endpoint|pool [as \"model\"]'",
|
||||
r->line);
|
||||
return -1;
|
||||
}
|
||||
int found_judge = 0;
|
||||
int judge_slots = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++) {
|
||||
if (strcmp(cfg->endpoints[e].name, target)) continue;
|
||||
found_judge = 1;
|
||||
judge_slots = 1;
|
||||
if (!r->judges[j].as[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs 'every N endpoint as \"model\"'",
|
||||
r->line);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!found_judge) {
|
||||
for (int p = 0; p < cfg->pool_count; p++) {
|
||||
if (strcmp(cfg->pools[p].name, target)) continue;
|
||||
found_judge = 1;
|
||||
judge_slots = cfg->pools[p].count;
|
||||
for (int c = 0; c < cfg->pools[p].count; c++) {
|
||||
if (!cfg->pools[p].target_as[c][0] && !r->judges[j].as[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge pool member %s needs a model",
|
||||
r->line, cfg->pools[p].targets[c]);
|
||||
return -1;
|
||||
}
|
||||
int member = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++)
|
||||
if (!strcmp(cfg->endpoints[e].name, cfg->pools[p].targets[c])) {
|
||||
member = 1;
|
||||
break;
|
||||
}
|
||||
if (!member) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge pool member passes to unknown endpoint '%s'",
|
||||
r->line, cfg->pools[p].targets[c]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found_judge) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge passes to unknown endpoint or pool '%s'",
|
||||
r->line, target);
|
||||
return -1;
|
||||
}
|
||||
if (judge_slots > max_judge_slots) max_judge_slots = judge_slots;
|
||||
}
|
||||
/* One judge step is inserted in front of the targets at runtime. */
|
||||
if (r->target_count + max_judge_slots > J_MAX_TARGETS) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs %d more target slots (max %d)",
|
||||
r->line, max_judge_slots, J_MAX_TARGETS);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen)
|
||||
{
|
||||
FILE *fh = fopen(path, "r");
|
||||
@@ -1335,156 +1484,11 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
/* Pool-Namen expandieren. Das passiert nach dem Einlesen, damit ein Pool auch
|
||||
* unterhalb der Regel stehen darf, die ihn benutzt. */
|
||||
/* Ein Pool darf auf einen anderen verweisen (alles → lokal → box3, box6). Deshalb
|
||||
* wird so lange expandiert, bis sich nichts mehr ändert. Die Obergrenze verhindert,
|
||||
* dass ein Ringschluss die Schleife endlos dreht. */
|
||||
for (int pass = 0; pass < 8; pass++) {
|
||||
int changed = 0;
|
||||
/* Expand pools in every rule and run the same validation the live
|
||||
* rules.set path uses. */
|
||||
for (int i = 0; i < cfg->rule_count; i++) {
|
||||
j_rule *r = &cfg->rules[i];
|
||||
for (int t = 0; t < r->target_count; t++) {
|
||||
for (int p = 0; p < cfg->pool_count; p++) {
|
||||
if (strcmp(cfg->pools[p].name, r->targets[t])) continue;
|
||||
j_pool *pl = &cfg->pools[p];
|
||||
/* Platz für die Ziele des Pools schaffen (ein Ziel wird zu count Zielen). */
|
||||
if (r->target_count - 1 + pl->count > J_MAX_TARGETS) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: pool %s does not fit into rule (more than %d targets)",
|
||||
r->line, pl->name, J_MAX_TARGETS);
|
||||
return -1;
|
||||
}
|
||||
/* A one-member pool needs no shift. The old loop copied the target
|
||||
* onto itself (snprintf with overlapping source and destination),
|
||||
* which is undefined behaviour. */
|
||||
if (pl->count > 1) {
|
||||
for (int k = r->target_count - 1; k > t; k--) {
|
||||
snprintf(r->targets[k + pl->count - 1], sizeof r->targets[0],
|
||||
"%s", r->targets[k]);
|
||||
snprintf(r->target_as[k + pl->count - 1], sizeof r->target_as[0],
|
||||
"%s", r->target_as[k]);
|
||||
}
|
||||
}
|
||||
char rule_as[J_GLOB_MAX];
|
||||
snprintf(rule_as, sizeof rule_as, "%s", r->target_as[t]);
|
||||
for (int c = 0; c < pl->count; c++) {
|
||||
snprintf(r->targets[t + c], sizeof r->targets[0], "%s", pl->targets[c]);
|
||||
/* A pool member's own model wins; otherwise the rule's "as"
|
||||
* stays in force for every expanded target. */
|
||||
snprintf(r->target_as[t + c], sizeof r->target_as[0], "%s",
|
||||
pl->target_as[c][0] ? pl->target_as[c] : rule_as);
|
||||
}
|
||||
r->target_count += pl->count - 1;
|
||||
t += pl->count - 1;
|
||||
changed = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!changed) break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < cfg->rule_count; i++) {
|
||||
j_rule *r = &cfg->rules[i];
|
||||
/* Eine ctx-Bedingung ohne Skalierungsfaktor kann nie erfüllen, was sie
|
||||
* verspricht. Das ist ein Konfigurationsfehler und keine stille Regel, die
|
||||
* nie greift — der Unterschied kostet sonst Stunden Fehlersuche. */
|
||||
if ((r->ctx_min >= 0 || r->ctx_max >= 0) && cfg->ctx_chars_per_token <= 0) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: rule uses 'ctx' but 'ctx_chars_per_token' is not set",
|
||||
r->line);
|
||||
if (j_config_expand_rule(&cfg->rules[i], cfg, err, errlen) != 0)
|
||||
return -1;
|
||||
}
|
||||
for (int t = 0; t < r->target_count; t++) {
|
||||
int found = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++)
|
||||
if (!strcmp(cfg->endpoints[e].name, r->targets[t])) { found = 1; break; }
|
||||
if (!found) {
|
||||
snprintf(err, errlen, "line %d: rule passes to unknown endpoint '%s'",
|
||||
r->line, r->targets[t]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (r->review_count > 0 && r->block) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: review on a block rule has no target to review", r->line);
|
||||
return -1;
|
||||
}
|
||||
if (r->judge_count > 0) {
|
||||
int max_judge_slots = 0;
|
||||
for (int j = 0; j < r->judge_count; j++) {
|
||||
if (j > 0 && r->judges[j].every <= r->judges[j - 1].every) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge steps must be ascending (%d after %d)",
|
||||
r->line, r->judges[j].every, r->judges[j - 1].every);
|
||||
return -1;
|
||||
}
|
||||
const char *target = r->judges[j].target;
|
||||
if (!target[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs 'every N endpoint|pool [as \"model\"]'",
|
||||
r->line);
|
||||
return -1;
|
||||
}
|
||||
int found_judge = 0;
|
||||
int judge_slots = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++) {
|
||||
if (strcmp(cfg->endpoints[e].name, target)) continue;
|
||||
found_judge = 1;
|
||||
judge_slots = 1;
|
||||
if (!r->judges[j].as[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs 'every N endpoint as \"model\"'",
|
||||
r->line);
|
||||
return -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!found_judge) {
|
||||
for (int p = 0; p < cfg->pool_count; p++) {
|
||||
if (strcmp(cfg->pools[p].name, target)) continue;
|
||||
found_judge = 1;
|
||||
judge_slots = cfg->pools[p].count;
|
||||
for (int c = 0; c < cfg->pools[p].count; c++) {
|
||||
if (!cfg->pools[p].target_as[c][0] && !r->judges[j].as[0]) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge pool member %s needs a model",
|
||||
r->line, cfg->pools[p].targets[c]);
|
||||
return -1;
|
||||
}
|
||||
int member = 0;
|
||||
for (int e = 0; e < cfg->endpoint_count; e++)
|
||||
if (!strcmp(cfg->endpoints[e].name, cfg->pools[p].targets[c])) {
|
||||
member = 1;
|
||||
break;
|
||||
}
|
||||
if (!member) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge pool member passes to unknown endpoint '%s'",
|
||||
r->line, cfg->pools[p].targets[c]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found_judge) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge passes to unknown endpoint or pool '%s'",
|
||||
r->line, target);
|
||||
return -1;
|
||||
}
|
||||
if (judge_slots > max_judge_slots) max_judge_slots = judge_slots;
|
||||
}
|
||||
/* One judge step is inserted in front of the targets at runtime. */
|
||||
if (r->target_count + max_judge_slots > J_MAX_TARGETS) {
|
||||
snprintf(err, errlen,
|
||||
"line %d: judge needs %d more target slots (max %d)",
|
||||
r->line, max_judge_slots, J_MAX_TARGETS);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -446,5 +446,8 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen);
|
||||
* setzt. Dieselbe Funktion, die auch die Datei liest. */
|
||||
int j_config_apply_text(const char *text, char *err, size_t errlen);
|
||||
int j_config_rule_from_line(const char *text, j_rule *out, char *err, size_t errlen);
|
||||
/* Expand pools in one rule and validate it against cfg. Shared by the file
|
||||
* loader and the live rules.set path. */
|
||||
int j_config_expand_rule(j_rule *r, const j_config *cfg, char *err, size_t errlen);
|
||||
|
||||
#endif /* JUNCTION_CONFIG_H */
|
||||
+19
-2
@@ -1713,7 +1713,6 @@ int j_rule_apply(const char *text, char *err, size_t errlen)
|
||||
if (!strcmp(CFG.rules[i].model_glob, fresh.model_glob) &&
|
||||
!strcmp(CFG.rules[i].path_glob, fresh.path_glob)) {
|
||||
fresh.line = CFG.rules[i].line;
|
||||
CFG.rules[i] = fresh;
|
||||
replaced = 1;
|
||||
break;
|
||||
}
|
||||
@@ -1724,8 +1723,26 @@ int j_rule_apply(const char *text, char *err, size_t errlen)
|
||||
snprintf(err, errlen, "more than %d rules", J_MAX_RULES);
|
||||
return -1;
|
||||
}
|
||||
fresh.line = CFG.rule_count + 1;
|
||||
}
|
||||
/* A live rule must mean exactly what the same line in the file means:
|
||||
* expand pools and reject unknown endpoints, bad judge chains and review
|
||||
* on a block rule. The lock also keeps CFG.pools/endpoints stable while
|
||||
* expanding. */
|
||||
if (j_config_expand_rule(&fresh, &CFG, err, errlen) != 0) {
|
||||
pthread_rwlock_unlock(&g_rules_rw);
|
||||
return -1;
|
||||
}
|
||||
if (replaced) {
|
||||
for (int i = 0; i < CFG.rule_count; i++) {
|
||||
if (!strcmp(CFG.rules[i].model_glob, fresh.model_glob) &&
|
||||
!strcmp(CFG.rules[i].path_glob, fresh.path_glob)) {
|
||||
CFG.rules[i] = fresh;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
CFG.rules[CFG.rule_count] = fresh;
|
||||
CFG.rules[CFG.rule_count].line = CFG.rule_count + 1;
|
||||
CFG.rule_count++;
|
||||
}
|
||||
count = CFG.rule_count;
|
||||
|
||||
@@ -119,6 +119,8 @@ endpoint b {{
|
||||
priority 20
|
||||
}}
|
||||
|
||||
pool both a, b
|
||||
|
||||
req for "*" pass a
|
||||
"""
|
||||
with open(conf, "w") as f:
|
||||
@@ -203,6 +205,27 @@ req for "*" pass a
|
||||
if chat() != "a":
|
||||
failures.append("nach dem Neusetzen greift die Regel nicht")
|
||||
|
||||
# 2b. Live-Regeln müssen Pools expandieren und Ziele validieren.
|
||||
ctrl({"cmd": "rules.delete", "index": 0})
|
||||
r = ctrl({"cmd": "rules.set", "text": 'req for "pool-target" pass both'})
|
||||
if not r.get("ok"):
|
||||
failures.append(f"rules.set mit Pool: {r}")
|
||||
rules = ctrl({"cmd": "rules.list"}).get("rules") or []
|
||||
targets = rules[0].get("targets", "") if rules else ""
|
||||
if "a" not in targets or "b" not in targets or "both" in targets:
|
||||
failures.append(f"rules.set hat den Pool nicht expandiert: {targets!r}")
|
||||
if chat("pool-target") not in ("a", "b"):
|
||||
failures.append("die Pool-Regel hat keinen Endpoint bedient")
|
||||
r = ctrl({"cmd": "rules.set", "text": 'req for "bad" pass missing'})
|
||||
if r.get("ok"):
|
||||
failures.append("rules.set akzeptierte einen unbekannten Endpoint")
|
||||
r = ctrl({"cmd": "rules.set",
|
||||
"text": 'req for "bad-chain" judge every 10 a as "m" '
|
||||
'judge every 5 b as "m" pass a'})
|
||||
if r.get("ok"):
|
||||
failures.append("rules.set akzeptierte eine absteigende Judge-Kette")
|
||||
ctrl({"cmd": "rules.set", "text": 'req for "*" pass a'})
|
||||
|
||||
# 4. Endpunkt pausieren und aufnehmen
|
||||
r = ctrl({"cmd": "endpoint.pause", "name": "a"})
|
||||
if not r.get("ok"):
|
||||
|
||||
Reference in new issue
Block a user