config: max_context, one hard ceiling for every model
Caps the model metadata (including #variant base matching in j_models_limit), the effective rule limits, endpoint context fallbacks and the limit values published in /v1/models. A request whose estimated context exceeds the ceiling no longer finds an endpoint. Used on n95: max_context 131072.
This commit is contained in:
1 parent
299fd600ac
commit
24515eed3e
3 files changed
+82
-7
No files matched your search
@@ -241,6 +241,13 @@ static int global_kw(j_config *cfg, char **w, int n, int lineno, char *err, size
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(k, "max_context")) {
|
||||
if (n != 2 || as_long(w[1], &cfg->max_context) || cfg->max_context < 16) {
|
||||
snprintf(err, errlen, "line %d: max_context wants a token count >= 16", lineno);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(k, "listen")) {
|
||||
if (n == 5 && !strcmp(w[2], "tls")) {
|
||||
if (parse_listen(cfg, w[1])) goto bad;
|
||||
@@ -1561,6 +1568,11 @@ 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");
|
||||
}
|
||||
/* max_context is a hard ceiling for every model, even one that advertises more. */
|
||||
if (cfg->max_context > 0)
|
||||
for (int i = 0; i < cfg->endpoint_count; i++)
|
||||
if (cfg->endpoints[i].context > cfg->max_context)
|
||||
cfg->endpoints[i].context = cfg->max_context;
|
||||
if (cfg->ctx_fallback[0]) {
|
||||
int found = 0;
|
||||
for (int i = 0; i < cfg->pool_count; i++)
|
||||
|
||||
@@ -370,6 +370,10 @@ typedef struct {
|
||||
* Messung — deshalb steht er in der Konfiguration und nicht im Code. */
|
||||
double ctx_chars_per_token;
|
||||
|
||||
/* Harte Obergrenze für alle Modelle, 0 = aus. Auch ein Modell, das mehr
|
||||
* veröffentlicht, wird auf diesen Wert gekappt. */
|
||||
long max_context;
|
||||
|
||||
/* Modell-Politik. Sobald eine `models allow`-Zeile existiert, ist die Vorgabe
|
||||
* "alles verboten": nur ein passendes Muster darf gelistet und angefragt werden.
|
||||
* `models deny` gewinnt immer. Muster sind fnmatch-Globs auf die Modell-ID, wie
|
||||
|
||||
+66
-7
@@ -155,14 +155,28 @@ int j_models_parse(j_endpoint *ep, const char *json, size_t len)
|
||||
return n;
|
||||
}
|
||||
|
||||
/* CFG.max_context is a hard ceiling: even a model that publishes more is
|
||||
* treated as this small. 0 = no ceiling. */
|
||||
static long cap_context(long lim)
|
||||
{
|
||||
if (lim > 0 && CFG.max_context > 0 && lim > CFG.max_context)
|
||||
return CFG.max_context;
|
||||
return lim;
|
||||
}
|
||||
|
||||
long j_models_limit(const j_endpoint *ep, const char *model, int which)
|
||||
{
|
||||
if (!model || !*model) return 0;
|
||||
long found = 0;
|
||||
char base[J_GLOB_MAX];
|
||||
snprintf(base, sizeof base, "%s", model);
|
||||
char *hash = strchr(base, '#');
|
||||
if (hash) *hash = '\0';
|
||||
pthread_mutex_lock(&g_models_lock);
|
||||
if (ep->models && ep->model_count > 0) {
|
||||
for (int i = 0; i < ep->model_count; i++)
|
||||
if (!strcmp(ep->models[i].id, model)) {
|
||||
if (!strcmp(ep->models[i].id, model) ||
|
||||
(hash && !strcmp(ep->models[i].id, base))) {
|
||||
found = which ? ep->models[i].out : ep->models[i].ctx;
|
||||
goto done;
|
||||
}
|
||||
@@ -170,7 +184,8 @@ long j_models_limit(const j_endpoint *ep, const char *model, int which)
|
||||
* ("deepseek/deepseek-v4.1-flash"); ein "as" nennt oft nur den kurzen Teil. */
|
||||
for (int i = 0; i < ep->model_count; i++) {
|
||||
const char *slash = strrchr(ep->models[i].id, '/');
|
||||
if (slash && !strcmp(slash + 1, model)) {
|
||||
if (slash && (!strcmp(slash + 1, model) ||
|
||||
(hash && !strcmp(slash + 1, base)))) {
|
||||
found = which ? ep->models[i].out : ep->models[i].ctx;
|
||||
break;
|
||||
}
|
||||
@@ -178,7 +193,7 @@ long j_models_limit(const j_endpoint *ep, const char *model, int which)
|
||||
}
|
||||
done:
|
||||
pthread_mutex_unlock(&g_models_lock);
|
||||
return found;
|
||||
return cap_context(found);
|
||||
}
|
||||
|
||||
/* Ein Muster gegen die volle ID und, falls vorhanden, gegen die ID ohne
|
||||
@@ -362,7 +377,7 @@ static long effective_limit_for_rule(const j_rule *r, const char *model, int whi
|
||||
}
|
||||
}
|
||||
}
|
||||
return best;
|
||||
return cap_context(best);
|
||||
}
|
||||
|
||||
long j_models_rule_limit(const j_rule *r, int which)
|
||||
@@ -494,12 +509,55 @@ int j_models_filter_list(const char *json, size_t len, char **out, size_t *out_l
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* CFG.max_context also applies to what /v1/models publishes: a model that
|
||||
* advertises more is listed with the ceiling. Returns a malloc'd copy when a
|
||||
* value changed, NULL otherwise. */
|
||||
static char *cap_limits_json(const char *json, size_t len, size_t *out_len)
|
||||
{
|
||||
if (CFG.max_context <= 0) return NULL;
|
||||
char *out = malloc(len + 1);
|
||||
if (!out) return NULL;
|
||||
size_t i = 0, o = 0;
|
||||
int changed = 0;
|
||||
static const char *const keys[] = { "\"context\":", "\"output\":" };
|
||||
while (i < len) {
|
||||
size_t klen = 0;
|
||||
int hit = -1;
|
||||
if (json[i] == '"') {
|
||||
for (int k = 0; k < 2; k++) {
|
||||
klen = strlen(keys[k]);
|
||||
if (i + klen <= len && !memcmp(json + i, keys[k], klen)) { hit = k; break; }
|
||||
}
|
||||
}
|
||||
if (hit < 0) { out[o++] = json[i++]; continue; }
|
||||
memcpy(out + o, json + i, klen); o += klen; i += klen;
|
||||
while (i < len && (json[i] == ' ' || json[i] == '\t')) out[o++] = json[i++];
|
||||
size_t start = i;
|
||||
long v = 0;
|
||||
while (i < len && json[i] >= '0' && json[i] <= '9') { v = v * 10 + (json[i] - '0'); i++; }
|
||||
if (i == start) { continue; }
|
||||
if (v > CFG.max_context) {
|
||||
int n = snprintf(out + o, len + 1 - o, "%ld", CFG.max_context);
|
||||
if (n > 0) o += (size_t)n;
|
||||
changed = 1;
|
||||
} else {
|
||||
while (start < i) out[o++] = json[start++];
|
||||
}
|
||||
}
|
||||
out[o] = '\0';
|
||||
if (!changed) { free(out); return NULL; }
|
||||
*out_len = o;
|
||||
return out;
|
||||
}
|
||||
|
||||
int j_models_patch_list(const char *json, size_t len, char **out, size_t *out_len)
|
||||
{
|
||||
*out = NULL; *out_len = 0;
|
||||
if (!json || len == 0) return -1;
|
||||
char *capped = cap_limits_json(json, len, &len);
|
||||
if (capped) json = capped;
|
||||
const char *arr = NULL, *endarr = NULL;
|
||||
if (find_data_array(json, len, &arr, &endarr) != 0) return -1;
|
||||
if (find_data_array(json, len, &arr, &endarr) != 0) { free(capped); return -1; }
|
||||
|
||||
/* Sammle virtuelle Ketten-Modelle */
|
||||
char extra[4096]; size_t eo = 0; extra[0] = '\0';
|
||||
@@ -543,17 +601,18 @@ int j_models_patch_list(const char *json, size_t len, char **out, size_t *out_le
|
||||
n = snprintf(extra + eo, sizeof extra - eo, "}"); if (n>0) eo+=(size_t)n;
|
||||
if (eo >= sizeof extra - 64) break;
|
||||
}
|
||||
if (eo == 0) return -1;
|
||||
if (eo == 0) { free(capped); return -1; }
|
||||
|
||||
size_t newlen = len + eo;
|
||||
char *nb = malloc(newlen + 1);
|
||||
if (!nb) return -1;
|
||||
if (!nb) { free(capped); return -1; }
|
||||
size_t pre = (size_t)(endarr - json);
|
||||
memcpy(nb, json, pre);
|
||||
memcpy(nb + pre, extra, eo);
|
||||
memcpy(nb + pre + eo, endarr, len - pre);
|
||||
nb[newlen] = '\0';
|
||||
*out = nb; *out_len = newlen;
|
||||
free(capped);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user