diff --git a/README.md b/README.md index 2990ba9..c4945f6 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,22 @@ allow lines — an explicit `models deny "*"` would win and hide the allow list and a compressed response cannot slip past the policy; every other request keeps the client's encoding header. +### Context fallback + +A request that is too large for every target of its matching rule would normally +end in `503 no_endpoint_available` with `attempts: context context ...`. One +global pool can catch those: + +``` +ctx_fallback ds41flash +ctx_fallback ds41flash as "deepseek-v4.1-flash" # optional default model +``` + +If no target can hold `ctx + max_tokens`, junction retries the members of that +pool. Each member's own `as` wins; the model after `as` on the directive is the +fallback for members without one. `/v1/models` does not list the fallback pool — +it is a route, not an advertised model. + ## Images and context limits **An image is recognised inside `messages`, not anywhere in the body.** A request diff --git a/config.example b/config.example index 857356b..74c1db0 100644 --- a/config.example +++ b/config.example @@ -183,6 +183,12 @@ endpoint opencode-go { # models allow "foo" "bar" "ma/bla" "opencode-go0/*" # models deny "*-free" +# If no target of the selected rule can hold ctx + max_tokens, retry this pool. +# Pool members should name the model they expect with "as"; the optional model +# after "as" on the directive is the default for members without their own. +# ctx_fallback ds41flash +# ctx_fallback ds41flash as "deepseek-v4.1-flash" + # ------------------------------------------------------------------------- rules # First matching rule wins. Rules may use: # for need >N | diff --git a/examples/full-reference.conf b/examples/full-reference.conf index 363c9ab..7f1e2e1 100644 --- a/examples/full-reference.conf +++ b/examples/full-reference.conf @@ -531,6 +531,9 @@ pool alles opencode-go, lokal # the request's model; endpoint/model#max replaces it. A rule's "as" still applies # to members that do not name a model. # pool ds41flash opencode-go/deepseek-v4.1-flash#max, ollama0/deepseek-v4.1-flash#max +# +# When no target of the selected rule can hold ctx + max_tokens, retry this pool: +# ctx_fallback ds41flash # An endpoint may also fit on one line when it is small: # endpoint kurz { base http://127.0.0.1:8080/v1; timeout 60 } diff --git a/src/config.c b/src/config.c index bccc92c..8a13295 100644 --- a/src/config.c +++ b/src/config.c @@ -566,6 +566,15 @@ static int global_kw(j_config *cfg, char **w, int n, int lineno, char *err, size } return 0; } + if (!strcmp(k, "ctx_fallback")) { + /* ctx_fallback [as ] */ + if (n != 2 && n != 4) goto bad; + if (n == 4 && strcmp(w[2], "as")) goto bad; + snprintf(cfg->ctx_fallback, sizeof cfg->ctx_fallback, "%s", w[1]); + if (n == 4) + snprintf(cfg->ctx_fallback_model, sizeof cfg->ctx_fallback_model, "%s", w[3]); + return 0; + } if (!strcmp(k, "spend_log")) { if (n != 2) goto bad; snprintf(cfg->spend_log, sizeof cfg->spend_log, "%s", w[1]); @@ -1278,6 +1287,15 @@ int j_config_load(const char *path, j_config *cfg, char *err, size_t errlen) } if (!ep->kind[0]) snprintf(ep->kind, sizeof ep->kind, "remote"); } + if (cfg->ctx_fallback[0]) { + int found = 0; + for (int i = 0; i < cfg->pool_count; i++) + if (!strcmp(cfg->pools[i].name, cfg->ctx_fallback)) { found = 1; break; } + if (!found) { + snprintf(err, errlen, "ctx_fallback: no pool named '%s'", cfg->ctx_fallback); + 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 diff --git a/src/config.h b/src/config.h index 520b361..7813f08 100644 --- a/src/config.h +++ b/src/config.h @@ -396,6 +396,11 @@ typedef struct { j_pool pools[J_MAX_POOLS]; int pool_count; + /* Wenn keine Ziel-Regel dieser Anfrage den Kontext fassen kann, noch einmal + * über diesen Pool versuchen. Das Modell wird dabei auf ctx_fallback_model + * umgeschrieben, sofern der Pool-Member kein eigenes "as" trägt. */ + char ctx_fallback[J_NAME_MAX]; + char ctx_fallback_model[J_GLOB_MAX]; j_mcp mcps[J_MAX_MCP]; int mcp_count; diff --git a/src/junction.c b/src/junction.c index 863a6e6..0e41bb0 100644 --- a/src/junction.c +++ b/src/junction.c @@ -1546,6 +1546,9 @@ void j_config_swap(j_config *fresh) memcpy(CFG.vkeys, fresh->vkeys, sizeof CFG.vkeys); CFG.pool_count = fresh->pool_count; memcpy(CFG.pools, fresh->pools, sizeof CFG.pools); + snprintf(CFG.ctx_fallback, sizeof CFG.ctx_fallback, "%s", fresh->ctx_fallback); + snprintf(CFG.ctx_fallback_model, sizeof CFG.ctx_fallback_model, "%s", + fresh->ctx_fallback_model); CFG.balance = fresh->balance; snprintf(CFG.spend_log, sizeof CFG.spend_log, "%s", fresh->spend_log); snprintf(CFG.guard_cmd, sizeof CFG.guard_cmd, "%s", fresh->guard_cmd); @@ -1843,6 +1846,14 @@ static int first_matching_rule(const char *path, const char *model, long need, return found; } +static const j_pool *find_pool(const char *name) +{ + if (!name || !name[0]) return NULL; + for (int i = 0; i < CFG.pool_count; i++) + if (!strcmp(CFG.pools[i].name, name)) return &CFG.pools[i]; + return NULL; +} + static char *rewrite_model(const char *body, const char *new_model) { if (!body) return NULL; @@ -3385,6 +3396,52 @@ static void handle_client(j_conn *client) goto done; } + /* If no target of this rule can hold ctx + need, try the configured fallback + * pool instead of returning 503. The fallback pool's members carry their own + * "as" (usually deepseek-v4.1-flash); ctx_fallback_model is the fallback for + * members without one. */ + if (CFG.ctx_fallback[0] && ctx >= 0) { + int fits = 0; + for (int t = 0; t < rule_copy.target_count && !fits; t++) { + j_endpoint *e = NULL; + for (int i = 0; i < CFG.endpoint_count; i++) + if (!strcmp(CFG.endpoints[i].name, rule_copy.targets[t])) { + e = &CFG.endpoints[i]; + break; + } + if (!e) continue; + const char *em = rule_copy.target_as[t][0] ? rule_copy.target_as[t] : model; + long mctx = j_models_limit(e, em, 0); + long mout = j_models_limit(e, em, 1); + long ep_ctx = mctx > 0 ? mctx : e->context; + long budget = need >= 0 ? need : 0; + if (mout > 0 && budget > mout) budget = mout; + int ep_compresses = (e->compress >= 0) ? e->compress : (CFG.compress != J_COMPRESS_OFF); + long ctx_here = (ep_compresses && ctx_short >= 0) ? ctx_short : ctx; + long total = ctx_here + budget; + if (ep_ctx <= 0 || total <= ep_ctx) fits = 1; + } + if (!fits) { + const j_pool *fb = find_pool(CFG.ctx_fallback); + if (fb && fb->count > 0) { + rule_copy.target_count = fb->count; + rule_copy.check = J_CHECK_NONE; + rule_copy.balance = J_BALANCE_FIRST; + rule_copy.strip[0] = '\0'; + for (int c = 0; c < fb->count; c++) { + snprintf(rule_copy.targets[c], sizeof rule_copy.targets[c], + "%s", fb->targets[c]); + const char *m = fb->target_as[c][0] ? fb->target_as[c] + : CFG.ctx_fallback_model; + snprintf(rule_copy.target_as[c], sizeof rule_copy.target_as[c], + "%s", m); + } + jlog("ctx_fallback", "rule_line=%d ctx=%ld need=%ld pool=%s", + rule_copy.line, ctx, need, CFG.ctx_fallback); + } + } + } + /* The body goes out unchanged; a target that needs a different model name does * that rewrite itself (forward_step), so a rule may mix targets with and * without a rewrite. */ diff --git a/tests/ctx-fallback.sh b/tests/ctx-fallback.sh new file mode 100644 index 0000000..3d43b6c --- /dev/null +++ b/tests/ctx-fallback.sh @@ -0,0 +1,59 @@ +#!/bin/bash +# Wird von tests/run.sh eingebunden: ctx-Fallback, wenn keine Regel-Ziel den +# Kontext fassen kann. + +echo "== 42. ctx-Fallback auf den ds41flash-Pool ==" + +CF=$((PORT+260)) +CFS=$((PORT+261)) +CFB=$((PORT+262)) + +STUB_CTX=4096 STUB_OUT=1000 tests/stub $CFS "SMALL" & PIDS+=($!) +STUB_CTX=200000 STUB_OUT=1000 tests/stub $CFB "BIG" & PIDS+=($!) +sleep 0.3 + +cat > "$TMP/ctx-fallback.conf" < "$TMP/ctx-fallback.log" 2>&1 & +PIDS+=($!) +wait_port $CF 10 + +# Small request: the small endpoint fits, no fallback. +R=$(curl -s -m 10 -D- -o /dev/null -X POST -H 'Content-Type: application/json' \ + -d '{"model":"tiny","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \ + "http://127.0.0.1:$CF/v1/chat/completions") +contains "$R" "X-Junction-Endpoint: small" "a fitting request stays on its rule target" + +# Large request: ctx + max_tokens exceeds 4096; the fallback pool rewrites the model. +python3 - "$TMP/ctx-large.json" <<'PY' +import json, sys +body = { + "model": "tiny", + "messages": [{"role": "user", "content": "x" * 30000}], + "max_tokens": 1000, +} +open(sys.argv[1], "w").write(json.dumps(body)) +PY +R=$(curl -s -m 20 -D- -o /dev/null -X POST -H 'Content-Type: application/json' \ + --data-binary "@$TMP/ctx-large.json" \ + "http://127.0.0.1:$CF/v1/chat/completions") +contains "$R" "X-Junction-Endpoint: big" "an oversized request falls back to the ctx pool" +contains "$R" "X-Stub-Model: deepseek-v4.1-flash" "the ctx fallback rewrites the model" +contains "$(cat "$TMP/ctx-fallback.log")" "ctx_fallback" "the fallback is logged" diff --git a/tests/run.sh b/tests/run.sh index 35250ed..ed3f3b0 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -53,7 +53,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 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8165 8166 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 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 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 8165 8166 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 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/^/ /' @@ -680,6 +680,10 @@ contains "$OUT" "bad arguments" "an impossible offpeak window is rejected" # shellcheck source=tests/effort.sh . "$(dirname "$0")/effort.sh" +# Wenn kein Ziel den Kontext fasst: Fallback auf den ds41flash-Pool. +# shellcheck source=tests/ctx-fallback.sh +. "$(dirname "$0")/ctx-fallback.sh" + # Gewichtung, Balance pro Regel und Zeitlimits. # shellcheck source=tests/balancing.sh . "$(dirname "$0")/balancing.sh"