diff --git a/README.md b/README.md index e297340..1ba12bc 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,9 @@ serves it — and what to do when none of them wants to. headers can be added per endpoint, and `X-Forwarded-For`/`Proto` are appended. Every response carries `X-Junction-Endpoint`, `X-Junction-Ms`, `X-Junction-Cache`. A sampled judge adds `X-Junction-Judge`, `X-Junction-Judge-Step`, - `X-Junction-Judge-Every`, `X-Junction-Judge-Model`, `X-Junction-Judge-Served`. + `X-Junction-Judge-Every`, `X-Junction-Judge-Model`, `X-Junction-Judge-Served`, + and on every turn of a judged rule `X-Junction-Judge-Next/-In` plus the + `X-Junction-Judge-Next-*` fields naming the next judge. A self-review adds `X-Junction-Review`, `X-Junction-Review-Rounds`, `X-Junction-Review-Model`, `X-Junction-Review-Skipped`; when a review ran, a non-streaming answer also carries `X-Junction-Review-Tokens-In/-Out` and @@ -222,6 +224,13 @@ fallback gets the original body, without the judge's cache marker. `/v1/models` the virtual model with the smaller context of worker and judge, because the worker still has to fit ordinary turns. +Every response of a rule with a judge announces the **next** judge, including worker +turns and the judge turn itself (meaning the one after it): `X-Junction-Judge-Next` +is the counter value at which it fires, `X-Junction-Judge-In` says how many calls +are left, and `X-Junction-Judge-Next-Target/-Model/-Step/-Every/-Cache` say which +judge it will be. Rules without a judge send none of these headers. A failed judge +still counts as its turn; the next headers then describe the next scheduled judge. + ### Self-review — the worker checks itself `review N` keeps the worker, but hides N review rounds before the answer is released: @@ -348,6 +357,13 @@ request falls back to an unreviewed forward and only the header/log names it. | `X-Junction-Judge-Every` | the N of this step | | `X-Junction-Judge-Model` | the step's `as` model; `pool` when the pool members carry their own | | `X-Junction-Judge-Served` | `1` only if the judge slot delivered the 2xx; `0` when the worker fallback served the turn | +| `X-Junction-Judge-Next` | counter value at which the next judge runs (e.g. `5`); present on every turn of a judged rule | +| `X-Junction-Judge-In` | calls remaining until that judge (`1` = the next call) | +| `X-Junction-Judge-Next-Step` | zero-based index of the next step in the chain | +| `X-Junction-Judge-Next-Every` | the N of the next step | +| `X-Junction-Judge-Next-Target` | endpoint or pool that will judge next | +| `X-Junction-Judge-Next-Model` | its `as` model, or `pool` when the pool members carry their own | +| `X-Junction-Judge-Next-Cache` | `off`, `5m` or `1h` for the next judge | | `X-Junction-Review` | the configured N | | `X-Junction-Review-Rounds` | review rounds that actually completed | | `X-Junction-Review-Model` | the model the worker/review calls were sent as | diff --git a/config.example b/config.example index 2356d89..c5cc676 100644 --- a/config.example +++ b/config.example @@ -222,6 +222,10 @@ req path "/v1/models" check none pass peer1, peer2, opencode-go # The judge clauses form a chain: the 5th call goes to the DeepSeek pool, the # 10th to Claude, then the counter resets to 0 (15th/20th next). Counts must be # ascending. A pool is allowed as the judge; its members keep their own "as". +# Every response of a judged rule carries X-Junction-Judge-Next (the counter +# value), X-Junction-Judge-In (calls remaining) and X-Junction-Judge-Next-Target/ +# -Model/-Step/-Every/-Cache, also on worker turns — so a client can see when +# and where the next escalation happens. # "cache" is for Anthropic-style cache_control; DeepSeek/OpenAI-compatible # providers cache prefixes automatically, so leave it off there. # diff --git a/src/config.c b/src/config.c index b1d59fa..48f45e7 100644 --- a/src/config.c +++ b/src/config.c @@ -1086,6 +1086,18 @@ 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_rule_is_virtual_chain(const j_rule *r) +{ + if (!r || r->block || r->target_count <= 0) return 0; + if (!r->model_glob[0] || strchr(r->model_glob, '*') || strchr(r->model_glob, '?')) + return 0; + if (r->path_glob[0] && strcmp(r->path_glob, "*") != 0) return 0; + if (r->target_count > 1 || r->judge_count > 0 || r->review_count > 0) return 1; + for (int t = 0; t < r->target_count; t++) + if (r->target_as[t][0]) return 1; + return 0; +} + 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 diff --git a/src/config.h b/src/config.h index fd570c2..d53d342 100644 --- a/src/config.h +++ b/src/config.h @@ -450,4 +450,9 @@ int j_config_rule_from_line(const char *text, j_rule *out, char *err, size_t err * loader and the live rules.set path. */ int j_config_expand_rule(j_rule *r, const j_config *cfg, char *err, size_t errlen); +/* A rule that names exactly one model and has a chain behind it: a pool or + * fallback list, an "as" rewrite, a judge or a review. These are the virtual + * models junction adds to /v1/models. */ +int j_rule_is_virtual_chain(const j_rule *r); + #endif /* JUNCTION_CONFIG_H */ diff --git a/src/judge.c b/src/judge.c index f78f57b..85f77c7 100644 --- a/src/judge.c +++ b/src/judge.c @@ -68,8 +68,10 @@ uint64_t j_judge_rule_id(const char *model_glob, const char *path_glob) return h ? h : 1; } -int j_judge_tick(uint64_t conversation, uint64_t rule_id, const int *steps, int nsteps) +int j_judge_tick(uint64_t conversation, uint64_t rule_id, + const int *steps, int nsteps, int *count_out) { + if (count_out) *count_out = 0; if (nsteps <= 0) return -1; uint64_t key = conversation ? conversation : 1; @@ -93,7 +95,9 @@ int j_judge_tick(uint64_t conversation, uint64_t rule_id, const int *steps, int /* After the last step the chain is complete; the next request starts * at 0 and the first step is due again N calls later. */ if (step == nsteps - 1) s->count = 0; + int count = (int)s->count; pthread_mutex_unlock(&g_slots_mu); + if (count_out) *count_out = count; return step; } if (s->key == 0 || now - s->last > JUDGE_IDLE_S) { @@ -130,7 +134,9 @@ int j_judge_tick(uint64_t conversation, uint64_t rule_id, const int *steps, int s->last = now; int step = step_for_count(1, steps, nsteps); if (step == nsteps - 1) s->count = 0; + int count = (int)s->count; pthread_mutex_unlock(&g_slots_mu); + if (count_out) *count_out = count; return step; } diff --git a/src/judge.h b/src/judge.h index 6672b7c..094240c 100644 --- a/src/judge.h +++ b/src/judge.h @@ -25,9 +25,11 @@ uint64_t j_judge_rule_id(const char *model_glob, const char *path_glob); * `steps` are the cumulative counts inside the chain (strictly ascending), e.g. * {5, 10}: the 5th request is step 0, the 10th is step 1, then the counter * starts again at 0. `conversation` comes from j_judge_conversation(); - * `rule_id` comes from j_judge_rule_id(). */ + * `rule_id` comes from j_judge_rule_id(). When `count_out` is not NULL it gets + * the counter after this request (0 after the last step), which the caller uses + * to tell the client when the next judge is due. */ int j_judge_tick(uint64_t conversation, uint64_t rule_id, - const int *steps, int nsteps); + const int *steps, int nsteps, int *count_out); /* Stable conversation id: session header if present, else the first 4 KiB of the * body (the part that does not change when a turn is appended), plus the vkey. diff --git a/src/junction.c b/src/junction.c index 8185206..04b3e13 100644 --- a/src/junction.c +++ b/src/junction.c @@ -88,6 +88,17 @@ static __thread int t_judge_step; static __thread int t_judge_every; static __thread char t_judge_target[J_NAME_MAX]; static __thread char t_judge_model[J_GLOB_MAX]; +/* The judge after this request: the counter value it fires at, how many calls + * are left, and which step/target/model/cache it uses. Valid on worker turns too, + * so a client can see when and where the next escalation happens. */ +static __thread int t_judge_next_valid; +static __thread int t_judge_next; +static __thread int t_judge_next_in; +static __thread int t_judge_next_step; +static __thread int t_judge_next_every; +static __thread int t_judge_next_cache; +static __thread char t_judge_next_target[J_NAME_MAX]; +static __thread char t_judge_next_model[J_GLOB_MAX]; static __thread int t_review_count; static __thread int t_review_done; static __thread char t_review_model[J_GLOB_MAX]; @@ -1964,11 +1975,22 @@ static int first_matching_rule(const char *path, const char *model, long need, pthread_rwlock_rdlock(&g_rules_rw); for (int i = 0; i < CFG.rule_count; i++) { - if (rule_matches(&CFG.rules[i], path, model, need, ctx, has_image)) { - *out = CFG.rules[i]; - found = 1; - break; + if (!rule_matches(&CFG.rules[i], path, model, need, ctx, has_image)) + continue; + /* A virtual model advertises the smallest ctx of its whole chain. A + * request beyond that must be handled by another rule (usually a ctx + * rule further down), not silently by a larger judge or by ctx_fallback. */ + if (j_rule_is_virtual_chain(&CFG.rules[i]) && ctx >= 0) { + long lim = j_models_rule_limit(&CFG.rules[i], 0); + if (lim > 0 && ctx > lim) { + jlog("virtual_ctx_skip", "model=%s ctx=%ld limit=%ld rule_line=%d", + model, ctx, lim, CFG.rules[i].line); + continue; + } } + *out = CFG.rules[i]; + found = 1; + break; } pthread_rwlock_unlock(&g_rules_rw); return found; @@ -1996,6 +2018,21 @@ static void add_route_meta_headers(j_hdrs *h) j_hdrs_set(h, "X-Junction-Judge-Model", t_judge_model); j_hdrs_set(h, "X-Junction-Judge-Served", t_judge_served ? "1" : "0"); } + if (t_judge_next_valid) { + snprintf(v, sizeof v, "%d", t_judge_next); + j_hdrs_set(h, "X-Junction-Judge-Next", v); + snprintf(v, sizeof v, "%d", t_judge_next_in); + j_hdrs_set(h, "X-Junction-Judge-In", v); + snprintf(v, sizeof v, "%d", t_judge_next_step); + j_hdrs_set(h, "X-Junction-Judge-Next-Step", v); + snprintf(v, sizeof v, "%d", t_judge_next_every); + j_hdrs_set(h, "X-Junction-Judge-Next-Every", v); + j_hdrs_set(h, "X-Junction-Judge-Next-Target", t_judge_next_target); + j_hdrs_set(h, "X-Junction-Judge-Next-Model", t_judge_next_model); + j_hdrs_set(h, "X-Junction-Judge-Next-Cache", + t_judge_next_cache == 2 ? "1h" : + t_judge_next_cache == 1 ? "5m" : "off"); + } if (t_review_count > 0) { snprintf(v, sizeof v, "%d", t_review_count); j_hdrs_set(h, "X-Junction-Review", v); @@ -3783,6 +3820,14 @@ static void handle_client_inner(j_conn *client) t_judge_every = 0; t_judge_target[0] = '\0'; t_judge_model[0] = '\0'; + t_judge_next_valid = 0; + t_judge_next = 0; + t_judge_next_in = 0; + t_judge_next_step = 0; + t_judge_next_every = 0; + t_judge_next_cache = 0; + t_judge_next_target[0] = '\0'; + t_judge_next_model[0] = '\0'; t_review_count = 0; t_review_done = 0; t_review_model[0] = '\0'; @@ -4000,7 +4045,11 @@ static void handle_client_inner(j_conn *client) * "as" (usually deepseek-v4.1-flash); ctx_fallback_model is the fallback for * members without one. */ int ctx_fallback_used = 0; - if (CFG.ctx_fallback[0] && ctx >= 0) { + /* Der globale ctx_fallback ist für gewöhnliche Regeln gedacht. Ein virtuelles + * Modell verspricht die kleinste ctx-Größe seiner Kette; wer mehr braucht, + * schreibt dafür eine eigene ctx-Regel — sonst würde der Judge oder die + * Cloud still einspringen und das Versprechen wäre falsch. */ + if (CFG.ctx_fallback[0] && ctx >= 0 && !j_rule_is_virtual_chain(rule)) { int fits = 0; for (int t = 0; t < rule_copy.target_count && !fits; t++) { j_endpoint *e = NULL; @@ -4056,7 +4105,28 @@ static void handle_client_inner(j_conn *client) const char *session = request_session(&rq.hdrs, sess, sizeof sess); uint64_t conversation = j_judge_conversation(rq.body, vk ? vk->name : "", session); uint64_t rule_id = j_judge_rule_id(rule->model_glob, rule->path_glob); - int step = j_judge_tick(conversation, rule_id, steps, rule->judge_count); + int judge_count = 0; + int step = j_judge_tick(conversation, rule_id, steps, rule->judge_count, + &judge_count); + /* The next judge for the response headers. Compute it from the counter + * after this request: after the last step it was reset to 0, so the + * chain starts again with its first step. */ + { + int ns = 0; + for (int j = 0; j < rule->judge_count; j++) + if (rule->judges[j].every > judge_count) { ns = j; break; } + t_judge_next_valid = 1; + t_judge_next = rule->judges[ns].every; + t_judge_next_in = rule->judges[ns].every - judge_count; + if (t_judge_next_in < 1) t_judge_next_in = 1; + t_judge_next_step = ns; + t_judge_next_every = rule->judges[ns].every; + t_judge_next_cache = rule->judges[ns].cache; + snprintf(t_judge_next_target, sizeof t_judge_next_target, "%s", + rule->judges[ns].target); + snprintf(t_judge_next_model, sizeof t_judge_next_model, "%s", + rule->judges[ns].as[0] ? rule->judges[ns].as : "pool"); + } if (step >= 0) { const char *target = rule->judges[step].target; const char *judge_as = rule->judges[step].as; diff --git a/src/models.c b/src/models.c index 266b734..4f69385 100644 --- a/src/models.c +++ b/src/models.c @@ -313,8 +313,59 @@ static const j_pool *find_pool_cfg(const char *name) return NULL; } -/* Ist das ein "einfacher" Modellname (kein Globjoker), der als virtuelle/alias - * Kette in Regeln auftaucht? Wir nutzen Regeln mit exakt passendem model_glob. */ +/* Grenze einer einzelnen virtuellen Regel: alle Ziele plus die Judge-Kette, + * das tatsächlich angesprochene Modell je Ziel. Für context fällt ein unbekannter + * Modellwert auf die Endpoint-Zahl zurück; für output wird nichts aus dem Kontext + * geraten — dort zählt nur, was ein Modell wirklich als Ausgabe nennt. */ +static long effective_limit_for_rule(const j_rule *r, const char *model, int which) +{ + long best = 0; + + for (int t = 0; t < r->target_count; t++) { + j_endpoint *ep = find_ep(r->targets[t]); + if (!ep) continue; + const char *em = r->target_as[t][0] ? r->target_as[t] : model; + long lim = j_models_limit(ep, em, which); + if (which == 0 && lim <= 0) lim = ep->context; + if (lim > 0 && (best <= 0 || lim < best)) best = lim; + } + for (int j = 0; j < r->judge_count; j++) { + const char *target = r->judges[j].target; + const char *judge_as = r->judges[j].as; + if (!target[0]) continue; + const j_pool *jp = find_pool_cfg(target); + if (jp) { + for (int c = 0; c < jp->count; c++) { + j_endpoint *ep = find_ep(jp->targets[c]); + if (!ep) continue; + const char *em = jp->target_as[c][0] ? jp->target_as[c] + : (judge_as[0] ? judge_as : model); + long lim = j_models_limit(ep, em, which); + if (which == 0 && lim <= 0) lim = ep->context; + if (lim > 0 && (best <= 0 || lim < best)) best = lim; + } + } else { + j_endpoint *ep = find_ep(target); + if (ep) { + const char *em = judge_as[0] ? judge_as : model; + long lim = j_models_limit(ep, em, which); + if (which == 0 && lim <= 0) lim = ep->context; + if (lim > 0 && (best <= 0 || lim < best)) best = lim; + } + } + } + return best; +} + +long j_models_rule_limit(const j_rule *r, int which) +{ + if (!r || !j_rule_is_virtual_chain(r)) return 0; + return effective_limit_for_rule(r, r->model_glob, which); +} + +/* Effektive Grenze für einen virtuellen Modellnamen. Gibt es mehrere Regeln mit + * demselben exakten Namen, gewinnt die kleinste Grenze — die erste Regel matcht + * im Betrieb, aber die Anzeige soll nicht mehr versprechen als irgendeine davon. */ long j_models_effective_limit(const char *model, int which) { if (!model || !*model) return 0; @@ -323,56 +374,11 @@ long j_models_effective_limit(const char *model, int which) for (int ri = 0; ri < CFG.rule_count; ri++) { const j_rule *r = &CFG.rules[ri]; - if (r->block || r->target_count <= 0) continue; - /* Nur explizite Modellregeln ohne Joker als virtuelle Alias-Namen betrachten, - * und nur wenn es eine Kette/Pool (mehrere Ziele) oder ein "as" ist. */ if (strcmp(r->model_glob, model) != 0) continue; - /* Pfadspezifische Regeln (z.B. /v1/models) sind keine Modellalias-Ketten. */ - if (r->path_glob[0] && strcmp(r->path_glob, "*") != 0) continue; - int is_chain = (r->target_count > 1) || r->judge_count > 0 || r->review_count > 0; - for (int t = 0; !is_chain && t < r->target_count; t++) - if (r->target_as[t][0]) is_chain = 1; - if (!is_chain) continue; - + if (!j_rule_is_virtual_chain(r)) continue; found_rule = 1; - for (int t = 0; t < r->target_count; t++) { - j_endpoint *ep = find_ep(r->targets[t]); - if (!ep) continue; - const char *em = r->target_as[t][0] ? r->target_as[t] : model; - long lim = j_models_limit(ep, em, which); - if (lim <= 0) lim = ep->context; /* Rückfall wie im Routing-Kontext */ - if (which == 1 && lim <= 0) lim = 0; /* output: kein guter Rückfall außer Modellwert */ - if (lim > 0) { - if (best <= 0 || lim < best) best = lim; - } - } - for (int j = 0; j < r->judge_count; j++) { - const char *target = r->judges[j].target; - const char *judge_as = r->judges[j].as; - if (!target[0]) continue; - const j_pool *jp = find_pool_cfg(target); - if (jp) { - for (int c = 0; c < jp->count; c++) { - j_endpoint *ep = find_ep(jp->targets[c]); - if (!ep) continue; - const char *em = jp->target_as[c][0] ? jp->target_as[c] - : (judge_as[0] ? judge_as : model); - long lim = j_models_limit(ep, em, which); - if (lim <= 0) lim = ep->context; - if (which == 1 && lim <= 0) lim = 0; - if (lim > 0 && (best <= 0 || lim < best)) best = lim; - } - } else { - j_endpoint *ep = find_ep(target); - if (ep) { - const char *em = judge_as[0] ? judge_as : model; - long lim = j_models_limit(ep, em, which); - if (lim <= 0) lim = ep->context; - if (which == 1 && lim <= 0) lim = 0; - if (lim > 0 && (best <= 0 || lim < best)) best = lim; - } - } - } + long lim = effective_limit_for_rule(r, model, which); + if (lim > 0 && (best <= 0 || lim < best)) best = lim; } return found_rule ? best : 0; } @@ -491,17 +497,16 @@ int j_models_patch_list(const char *json, size_t len, char **out, size_t *out_le char extra[4096]; size_t eo = 0; extra[0] = '\0'; for (int ri = 0; ri < CFG.rule_count; ri++) { const j_rule *r = &CFG.rules[ri]; - if (r->block || r->target_count <= 0) continue; - if (!r->model_glob[0] || strchr(r->model_glob, '*') || strchr(r->model_glob, '?')) continue; - if (r->path_glob[0] && strcmp(r->path_glob, "*") != 0) continue; - int is_chain = (r->target_count > 1) || r->judge_count > 0 || r->review_count > 0; - for (int t = 0; !is_chain && t < r->target_count; t++) if (r->target_as[t][0]) is_chain = 1; - if (!is_chain) continue; + if (!j_rule_is_virtual_chain(r)) continue; const char *name = r->model_glob; if (!j_model_allowed(name)) continue; if (json_has_model_id(json, len, name)) continue; long ctx = j_models_effective_limit(name, 0); long outlim = j_models_effective_limit(name, 1); + /* Kein Modell nennt eine Ausgabegrenze? Dann ist der effektive Kontext die + * sichere Obergrenze — nicht die Endpoint-Zahl eines einzelnen Ziels. */ + if (outlim <= 0) outlim = ctx; + if (ctx > 0 && outlim > ctx) outlim = ctx; /* nur ergänzen, wenn wir wenigstens eine Grenze kennen */ if (ctx <= 0 && outlim <= 0) continue; int need_comma = 0; @@ -511,8 +516,9 @@ int j_models_patch_list(const char *json, size_t len, char **out, size_t *out_le int first_extra = (eo == 0); int prefix_comma = (!first_extra) || need_comma; int n = snprintf(extra + eo, sizeof extra - eo, - "%s{\"id\":\"%s\",\"object\":\"model\",\"owned_by\":\"junction\"", - prefix_comma ? "," : "", name); + "%s{\"id\":\"%s\",\"name\":\"%s\",\"object\":\"model\"," + "\"owned_by\":\"junction\"", + prefix_comma ? "," : "", name, name); if (n < 0 || (size_t)n >= sizeof extra - eo) break; eo += (size_t)n; if (ctx > 0 || outlim > 0) { diff --git a/src/models.h b/src/models.h index b5bb9e2..d4577af 100644 --- a/src/models.h +++ b/src/models.h @@ -24,6 +24,10 @@ long j_models_limit(const j_endpoint *ep, const char *model, int which); * which: 0 = context, 1 = output. 0 = keine auswertbare Grenze gefunden. */ long j_models_effective_limit(const char *model, int which); +/* Dasselbe für eine konkrete Regel (ohne Namenssuche). Wird vom Request-Pfad + * benutzt, um eine virtuelle Regel bei ctx-Überlauf zu überspringen. */ +long j_models_rule_limit(const j_rule *r, int which); + /* Ergänzt/patcht eine OpenAI-/v1/models-JSON-Antwort um virtuelle Fallback-/Pool- * Alias-Modelle (kleinste ctx/output über die Kette). Gibt 0 bei Erfolg zurück und * liefert ggf. neuen Buffer in *out / *out_len (Caller freed). */ diff --git a/tests/judge.sh b/tests/judge.sh index c679c58..8c1a000 100644 --- a/tests/judge.sh +++ b/tests/judge.sh @@ -82,6 +82,13 @@ judge_ask() { # judge_ask JJEVERY=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Every: //p') JJMODEL=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Model: //p') JJSERVED=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Served: //p') + JJNEXT=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next: //p') + JJIN=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-In: //p') + JJNSTEP=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next-Step: //p') + JJNEVERY=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next-Every: //p') + JJNTARGET=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next-Target: //p') + JJNMODEL=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next-Model: //p') + JJNCACHE=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Judge-Next-Cache: //p') JENDPOINT=$(tr -d '\r' < "$TMP/judge_h.txt" | sed -n 's/^X-Junction-Endpoint: //p') } @@ -124,6 +131,12 @@ for i in 1 2 3 4 5 6; do [ "$JENDPOINT" = "worker" ] && ok "turn $i names the worker endpoint" \ || bad "turn $i: endpoint '$JENDPOINT'" fi + NEXTIN=$(( i % 3 == 1 ? 2 : (i % 3 == 2 ? 1 : 3) )) + [ "$JJNEXT" = "3" ] && [ "$JJIN" = "$NEXTIN" ] && [ "$JJNSTEP" = "0" ] \ + && [ "$JJNEVERY" = "3" ] && [ "$JJNTARGET" = "judge" ] \ + && [ "$JJNMODEL" = "judge-model" ] && [ "$JJNCACHE" = "1h" ] \ + && ok "turn $i announces the next judge at 3 (in $NEXTIN)" \ + || bad "turn $i next judge: next=$JJNEXT in=$JJIN step=$JJNSTEP every=$JJNEVERY target=$JJNTARGET model=$JJNMODEL cache=$JJNCACHE" done # Unterhaltung B beginnt bei 1, obwohl A schon beim Judge war. @@ -139,6 +152,13 @@ judge_ask "$JP" "qwen-judged" "A different system prompt for B." "conversation B [ "$JJUDGE" = "judge" ] && [ "$JJSERVED" = "1" ] && ok "conversation B's judge is visible in the headers" \ || bad "conversation B's judge headers are wrong: '$JJUDGE'/'$JJSERVED'" +# Eine Regel ohne judge darf keine Next-Judge-Header erfinden. +MJ=$(curl -s -D - -o /dev/null -m 10 "http://127.0.0.1:$JP/v1/models") +case "$(printf '%s' "$MJ" | tr -d '\r')" in + *"X-Junction-Judge-Next:"*) bad "a rule without a judge announces a next judge" ;; + *) ok "a rule without a judge has no next-judge headers" ;; +esac + contains "$(grep '\[junction\] judge ' "$TMP/judge.log" | head -1)" "cache=1h" \ "the judge log names the cache TTL" contains "$(grep '\[junction\] judge ' "$TMP/judge.log" | head -1)" "conv=" \ @@ -174,6 +194,11 @@ judge_ask "$JP2" "qwen-judged" "$SYS" "fallback turn" || bad "the fallback judge served header is '$JJSERVED'" [ "$JENDPOINT" = "worker" ] && ok "the fallback header names the worker" \ || bad "the fallback endpoint header is '$JENDPOINT'" +[ "$JJNEXT" = "1" ] && [ "$JJIN" = "1" ] && [ "$JJNSTEP" = "0" ] \ + && [ "$JJNEVERY" = "1" ] && [ "$JJNTARGET" = "badjudge" ] \ + && [ "$JJNMODEL" = "judge-model" ] && [ "$JJNCACHE" = "1h" ] \ + && ok "a failed judge still announces the next one at 1" \ + || bad "fallback next judge: next=$JJNEXT in=$JJIN target=$JJNTARGET model=$JJNMODEL" contains "$(cat "$TMP/judge_fb.log")" "upstream_error endpoint=badjudge" \ "the judge's error body is logged" contains "$(cat "$TMP/judge_fb.log")" "served_by endpoint=worker" \ @@ -234,6 +259,11 @@ judge_ask "$JP3" "vm-pool" "$SYS" "pool judge turn" || bad "the pool judge model header is '$JJMODEL'" [ "$JJSERVED" = "1" ] && ok "the pool judge header says it served" \ || bad "the pool judge served header is '$JJSERVED'" +[ "$JJNEXT" = "1" ] && [ "$JJIN" = "1" ] && [ "$JJNSTEP" = "0" ] \ + && [ "$JJNEVERY" = "1" ] && [ "$JJNTARGET" = "judgepool" ] \ + && [ "$JJNMODEL" = "pool" ] && [ "$JJNCACHE" = "off" ] \ + && ok "a pool judge announces the next judge with the pool name" \ + || bad "pool next judge: next=$JJNEXT in=$JJIN target=$JJNTARGET model=$JJNMODEL" contains "$(cat "$TMP/judge_pool.log")" "target=judgepool slots=2" \ "the judge log names the pool and its slot count" contains "$(cat "$TMP/judge_pool.log")" "failed endpoint=badjudge" \ @@ -257,6 +287,12 @@ CFG PIDS+=($!) wait_port "$JP4" 10 || bad "the judge-chain instance did not come up" CHAIN=("W" "W" "A" "W" "B" "W" "W" "A" "W" "B") +NEXTA=(3 3 5 5 3 3 3 5 5 3) +NEXTI=(2 1 2 1 3 2 1 2 1 3) +NEXTS=(0 0 1 1 0 0 0 1 1 0) +NEXTT=(judgea judgea judge2 judge2 judgea judgea judgea judge2 judge2 judgea) +NEXTM=(pool pool judge-b-model judge-b-model pool pool pool judge-b-model judge-b-model pool) +NEXTC=(off off 1h 1h off off off 1h 1h off) for i in 1 2 3 4 5 6 7 8 9 10; do judge_ask "$JP4" "vm-chain" "$SYS" "chain turn $i" want=${CHAIN[$((i-1))]} @@ -284,6 +320,12 @@ for i in 1 2 3 4 5 6 7 8 9 10; do && [ "$JJSERVED" = "1" ] && ok "chain call $i names the claude judge" \ || bad "chain call $i judge headers are wrong" ;; esac + [ "$JJNEXT" = "${NEXTA[$((i-1))]}" ] && [ "$JJIN" = "${NEXTI[$((i-1))]}" ] \ + && [ "$JJNSTEP" = "${NEXTS[$((i-1))]}" ] && [ "$JJNEVERY" = "${NEXTA[$((i-1))]}" ] \ + && [ "$JJNTARGET" = "${NEXTT[$((i-1))]}" ] && [ "$JJNMODEL" = "${NEXTM[$((i-1))]}" ] \ + && [ "$JJNCACHE" = "${NEXTC[$((i-1))]}" ] \ + && ok "chain call $i announces the next step ${NEXTS[$((i-1))]}" \ + || bad "chain call $i next judge: next=$JJNEXT in=$JJIN step=$JJNSTEP every=$JJNEVERY target=$JJNTARGET model=$JJNMODEL cache=$JJNCACHE" done contains "$(grep 'step=0 every=3 target=judgea' "$TMP/judge_chain.log" | head -1)" \ "step=0" "the chain log names the deepseek step" diff --git a/tests/run.sh b/tests/run.sh index 7b34e16..35e2585 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -71,7 +71,7 @@ log_has() { # answer requests with somebody else's config), so fail loudly and early. # 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem # Test benutzt, also darf er die Suite nicht aufhalten. -for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8208 9443 9990 9991 9992 9993 9994 9995 9996; do +for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8208 9443 9990 9991 9992 9993 9994 9995 9996; do if ss -tln 2>/dev/null | grep -q ":$port "; then echo "port $port is busy — a leftover junction/stub is still running:" ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /' @@ -711,6 +711,10 @@ contains "$OUT" "bad arguments" "an impossible offpeak window is rejected" # shellcheck source=tests/model-policy.sh . "$(dirname "$0")/model-policy.sh" +# Virtuelle Modelle: Ketten-Grenzen in /v1/models und ctx-Überlauf über Regeln. +# shellcheck source=tests/virtual-models.sh +. "$(dirname "$0")/virtual-models.sh" + # Effort: Anthropic output_config, OpenAI-kompatible unverändert. # shellcheck source=tests/effort.sh . "$(dirname "$0")/effort.sh" diff --git a/tests/stub.c b/tests/stub.c index 74d334b..a6c679e 100644 --- a/tests/stub.c +++ b/tests/stub.c @@ -422,12 +422,20 @@ int main(int argc, char **argv) bo += (size_t)snprintf(b + bo, sizeof b - bo, "{\"object\":\"list\",\"data\":["); const char *p = list; - while (*p && bo + 64 < sizeof b) { + while (*p && bo + 128 < sizeof b) { const char *comma = strchr(p, ','); size_t l = comma ? (size_t)(comma - p) : strlen(p); - bo += (size_t)snprintf(b + bo, sizeof b - bo, - "%s{\"id\":\"%.*s\"}", - first ? "" : ",", (int)l, p); + if (ectx && *ectx) + bo += (size_t)snprintf(b + bo, sizeof b - bo, + "%s{\"id\":\"%.*s\",\"limit\":" + "{\"context\":%ld,\"output\":%ld}}", + first ? "" : ",", (int)l, p, + strtol(ectx, NULL, 10), + eout ? strtol(eout, NULL, 10) : 0L); + else + bo += (size_t)snprintf(b + bo, sizeof b - bo, + "%s{\"id\":\"%.*s\"}", + first ? "" : ",", (int)l, p); first = 0; if (!comma) break; p = comma + 1; diff --git a/tests/virtual-models.sh b/tests/virtual-models.sh new file mode 100644 index 0000000..5ee4937 --- /dev/null +++ b/tests/virtual-models.sh @@ -0,0 +1,140 @@ +#!/bin/bash +# Wird von tests/run.sh eingebunden: /v1/models für virtuelle Ketten. +# +# Eine virtuelle Regel (Pool, Alias, Judge, Review) verspricht die kleinste +# ctx-Größe ihrer ganzen Kette. Ein Request darüber darf nicht still beim Judge +# oder über ctx_fallback landen, sondern muss über eine eigene ctx-Regel gehen. + +echo +echo "== 44. Virtuelle Modelle: Ketten-Grenzen und ctx-Regeln ==" + +VW=$((PORT+244)) # Worker-Stub, ctx 4000 / out 1000 +VJ=$((PORT+245)) # Judge-Stub, ctx 2000 / out 500 +VB=$((PORT+246)) # Big-Stub, ctx 100000 / out 8000 +VF=$((PORT+247)) # Fallback-Stub, ctx 200000 / out 8000 +V1=$((PORT+248)) # Junction mit expliziter ctx-Regel +V2=$((PORT+249)) # Junction mit globalem ctx_fallback + +STUB_STATE="$TMP/vm_worker.state" STUB_ANSWERS="worker answer" \ + STUB_MODELS="wm" STUB_CTX=4000 STUB_OUT=1000 tests/stub $VW "WORKER" \ + > "$TMP/vm_worker_stub.log" 2>&1 & +PIDS+=($!) +STUB_STATE="$TMP/vm_judge.state" STUB_ANSWERS="judge answer" \ + STUB_MODELS="jm" STUB_CTX=2000 STUB_OUT=500 tests/stub $VJ "JUDGE" \ + > "$TMP/vm_judge_stub.log" 2>&1 & +PIDS+=($!) +STUB_STATE="$TMP/vm_big.state" STUB_ANSWERS="big answer" \ + STUB_MODELS="bm" STUB_CTX=100000 STUB_OUT=8000 tests/stub $VB "BIG" \ + > "$TMP/vm_big_stub.log" 2>&1 & +PIDS+=($!) +STUB_STATE="$TMP/vm_fb.state" STUB_ANSWERS="fallback answer" \ + STUB_MODELS="fm" STUB_CTX=200000 STUB_OUT=8000 tests/stub $VF "FALLBACK" \ + > "$TMP/vm_fb_stub.log" 2>&1 & +PIDS+=($!) +wait_port "$VW" 30 || bad "the worker stub did not come up" +wait_port "$VJ" 30 || bad "the judge stub did not come up" +wait_port "$VB" 30 || bad "the big stub did not come up" +wait_port "$VF" 30 || bad "the fallback stub did not come up" + +# Die virtuelle Regel steht vor der ctx-Regel: first match wins. Erst das +# Ketten-Minimum (2000) macht die große Anfrage zu einer Sache der ctx-Regel. +cat > "$TMP/vm1.conf" <2000 pass big +req for "*" pass big +CFG +./junction --config "$TMP/vm1.conf" > "$TMP/vm1.log" 2>&1 & +PIDS+=($!) +wait_port "$V1" 30 || bad "the virtual-model instance did not come up" +# Die Modellisten werden im Hintergrund geholt. Erst wenn beide da sind, sind +# die output-Grenzen vollständig; sonst fiele output auf den Kontext zurück. +for i in $(seq 1 100); do + grep -q 'models: worker 1 Modelle' "$TMP/vm1.log" 2>/dev/null && + grep -q 'models: judge 1 Modelle' "$TMP/vm1.log" 2>/dev/null && break + sleep 0.05 +done + +VM=$(curl -s -m 10 "http://127.0.0.1:$V1/v1/models") +if python3 - "$VM" <<'PY' +import json, sys +d = json.loads(sys.argv[1]) +m = [x for x in d.get("data", []) if x.get("id") == "vm-chain"] +if not m: + raise SystemExit("vm-chain is not listed") +m = m[0] +if m.get("limit") != {"context": 2000, "output": 500}: + raise SystemExit("limit is %r" % m.get("limit")) +if m.get("name") != "vm-chain": + raise SystemExit("name is %r" % m.get("name")) +PY +then ok "the virtual model exposes name and the smallest limit of worker and judge" +else bad "the virtual model limit/name is wrong: $VM" +fi + +SMALL=$(python3 -c "print('a' * 1000)") +R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \ + -d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$SMALL\"}],\"max_tokens\":8}" \ + "http://127.0.0.1:$V1/v1/chat/completions") +contains "$R" "judge answer" "a request inside the chain minimum reaches the judge" +JC1=$(tr -d '\n' < "$TMP/vm_judge.state") +[ "$JC1" = "1" ] && ok "the judge was called once for the small request" \ + || bad "the judge call count is '$JC1' instead of 1" + +BIGPROMPT=$(python3 -c "print('a' * 3000)") +R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \ + -d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGPROMPT\"}],\"max_tokens\":8}" \ + "http://127.0.0.1:$V1/v1/chat/completions" -w '\nHTTP=%{http_code}') +contains "$R" "big answer" "ctx overflow goes to the explicit ctx rule" +contains "$R" "HTTP=200" "the explicit ctx rule serves the overflow" +JC2=$(tr -d '\n' < "$TMP/vm_judge.state") +[ "$JC2" = "1" ] && ok "the judge is not used for the ctx overflow" \ + || bad "the judge was called '$JC2' times after the overflow" +log_has "$TMP/vm1.log" "virtual_ctx_skip model=vm-chain" \ + "the skipped virtual rule is logged" +log_has "$TMP/vm1.log" "limit=2000" "the skip log names the chain limit" + +# Ohne eigene ctx-Regel darf der globale ctx_fallback nicht für das virtuelle +# Modell einspringen. Eine gewöhnliche Regel benutzt ihn weiterhin. +cat > "$TMP/vm2.conf" < "$TMP/vm2.log" 2>&1 & +PIDS+=($!) +wait_port "$V2" 30 || bad "the ctx_fallback instance did not come up" + +FB1=0 +[ -f "$TMP/vm_fb.state" ] && FB1=$(tr -d '\n' < "$TMP/vm_fb.state") +R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \ + -d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGPROMPT\"}],\"max_tokens\":8}" \ + "http://127.0.0.1:$V2/v1/chat/completions" -w '\nHTTP=%{http_code}') +contains "$R" "no_matching_rule" "a virtual ctx overflow without its own rule is refused" +FB2=0 +[ -f "$TMP/vm_fb.state" ] && FB2=$(tr -d '\n' < "$TMP/vm_fb.state") +[ "$FB1" = "$FB2" ] && ok "ctx_fallback did not silently serve the virtual model" \ + || bad "ctx_fallback served the virtual model ($FB1 -> $FB2)" + +PLAIN=$(python3 -c "print('b' * 5000)") +R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \ + -d "{\"model\":\"plain\",\"messages\":[{\"role\":\"user\",\"content\":\"$PLAIN\"}],\"max_tokens\":8}" \ + "http://127.0.0.1:$V2/v1/chat/completions") +contains "$R" "fallback answer" "a normal rule still uses ctx_fallback"