junction is a pass-through: no token estimation, no invented max_tokens
'need' now reads only max_tokens/max_completion_tokens as the client wrote them. chars_per_token and default_max_tokens are gone (and rejected as unknown keywords, so an old config fails loudly instead of steering nothing). A request that declares no budget is not sized at all — the guessing is what made a 2048-context embedding endpoint reject requests that carried no token budget in the first place. tests: 'need' proven from a declared max_tokens, and a request without one proven to route by model and path alone.
This commit is contained in:
1 parent
a768f1061a
commit
a68ed88b84
7 files changed
+40
-71
No files matched your search
@@ -17,8 +17,10 @@ serves it — and what to do when none of them wants to.
|
||||
- **Unreachable means offline, immediately.** A connect failure is not a retry case.
|
||||
- **Honest timeouts.** Two per endpoint: *idle* ("nothing for N seconds", logged as
|
||||
`still_working`) and *total*. A thinking model never looks like a hang.
|
||||
- **Context-aware.** A request needing more context than an endpoint offers skips it
|
||||
instead of being silently truncated.
|
||||
- **Context-aware, without guessing.** `need` reads `max_tokens` / `max_completion_tokens`
|
||||
from the request and nothing else — no character counting, no assumed default. An
|
||||
endpoint whose `context` is smaller than a *declared* budget is skipped; a request
|
||||
that declares nothing is passed through unsized.
|
||||
- **Failover with cooldown**, and a `503` that names why each endpoint was skipped.
|
||||
- **TLS both ways.** `https://` upstreams (SNI, optional chain + hostname
|
||||
verification, custom CA) and an optional TLS listener — no stunnel needed.
|
||||
@@ -112,8 +114,9 @@ req [for <model-glob>] [path <glob>] [need >N | <N | N]
|
||||
```
|
||||
|
||||
- `for` — model glob (`fnmatch`) against the model in the request body.
|
||||
- `need` — estimated tokens (`chars/4 + max_tokens`); `>64000` means "needs more
|
||||
than 64k".
|
||||
- `need` — the output budget the client declared (`max_tokens`/`max_completion_tokens`);
|
||||
`>64000` matches a declared budget above 64k. A request without one never matches a
|
||||
`need` rule: junction is a pass-through and does not invent sizes.
|
||||
- `check hooks|none` — override the endpoint's hook list for this rule.
|
||||
- `retry <s>` — override `hook_retry` ("wait up to N s for the box").
|
||||
- `pass a, b` — try `a`, then `b` on failure.
|
||||
@@ -188,7 +191,8 @@ hook — building a magic packet should not need an interpreter either.
|
||||
processes and does not survive `SIGHUP`.
|
||||
- **TLS client is verified** against the system store or `tls_ca`; there is no
|
||||
certificate pinning and no client-certificate auth.
|
||||
- **Token estimate is `chars/4`** — good enough to choose an endpoint, not to bill.
|
||||
- **junction does not tokenise.** It sees only the numbers the client writes, so a
|
||||
request that declares no budget is routed by its model and path alone.
|
||||
|
||||
## Tests
|
||||
|
||||
|
||||
+3
-3
@@ -6,8 +6,6 @@ listen 0.0.0.0:8090
|
||||
# ... or TLS (https clients without a proxy in front):
|
||||
# listen 0.0.0.0:8443 tls /etc/junction/tls/cert.pem /etc/junction/tls/key.pem
|
||||
|
||||
chars_per_token 4
|
||||
default_max_tokens 4096
|
||||
connect_timeout_ms 1500
|
||||
offline_hold_s 20
|
||||
max_body_bytes 67108864
|
||||
@@ -76,7 +74,9 @@ endpoint cloud {
|
||||
# check hooks|none retry <seconds>
|
||||
# pass <endpoint>[,<endpoint>...] [as <model>] | block
|
||||
|
||||
# Requests that need more context than the peers have go straight to the big box.
|
||||
# A client that DECLARES more output than the peers can hold goes to the big box.
|
||||
# 'need' reads max_tokens from the request; there is no character counting and no
|
||||
# default, so a request that states nothing is not sized by guesswork.
|
||||
req need >64000 pass workstation, cloud
|
||||
|
||||
# Coding work: prefer the workstation, wake it, wait up to 5 minutes for the hooks,
|
||||
|
||||
@@ -15,8 +15,6 @@ listen 0.0.0.0:8090
|
||||
|
||||
log_prefix olivia
|
||||
|
||||
chars_per_token 4 # rough, only used to pick an endpoint
|
||||
default_max_tokens 4096 # assumed when the request does not say
|
||||
connect_timeout_ms 1200 # a box that cannot answer a SYN in 1.2 s is asleep
|
||||
offline_hold_s 20 # stop retrying a dead box for this long
|
||||
max_body_bytes 67108864 # 64 MiB: a long conversation, not a file upload
|
||||
@@ -128,8 +126,10 @@ req path "/v1/models" check none pass peer-a, peer-b, cloud
|
||||
# in a request to a third party, and the local box has the model.
|
||||
req for "*embed*" check none pass kitt-embed
|
||||
|
||||
# Nothing needs more than the peers' 64k? Then the peers are not a candidate at
|
||||
# all — pick the only boxes that can hold the request.
|
||||
# If the CLIENT declares an output budget the peers' 64k cannot hold, they are not a
|
||||
# candidate. "need" only ever reads max_tokens/max_completion_tokens from the request:
|
||||
# no character counting, no assumed default — a request that states no budget matches
|
||||
# this rule not at all, and the router does not pretend to know its size.
|
||||
req need >64000 pass desk, cloud
|
||||
|
||||
# Coding work wants the big context local box; the workstation gets woken for it,
|
||||
|
||||
@@ -73,8 +73,6 @@ void j_config_defaults(j_config *cfg)
|
||||
memset(cfg, 0, sizeof *cfg);
|
||||
snprintf(cfg->listen_host, sizeof cfg->listen_host, "0.0.0.0");
|
||||
cfg->listen_port = 8090;
|
||||
cfg->chars_per_token = 4;
|
||||
cfg->default_max_tokens = 4096;
|
||||
cfg->connect_timeout_ms = 1500;
|
||||
cfg->offline_hold_s = 20;
|
||||
cfg->max_body_bytes = 64 * 1024 * 1024;
|
||||
@@ -160,8 +158,6 @@ static int global_kw(j_config *cfg, char **w, int n, int lineno, char *err, size
|
||||
if (n != 2 || parse_listen(cfg, w[1])) goto bad;
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(k, "chars_per_token")) { if (n != 2 || as_int(w[1], &cfg->chars_per_token)) goto bad; return 0; }
|
||||
if (!strcmp(k, "default_max_tokens")) { if (n != 2 || as_long(w[1], &cfg->default_max_tokens)) goto bad; return 0; }
|
||||
if (!strcmp(k, "connect_timeout_ms")) { if (n != 2 || as_double(w[1], &cfg->connect_timeout_ms)) goto bad; return 0; }
|
||||
if (!strcmp(k, "offline_hold_s")) { if (n != 2 || as_double(w[1], &cfg->offline_hold_s)) goto bad; return 0; }
|
||||
if (!strcmp(k, "max_body_bytes")) { if (n != 2 || as_int(w[1], &cfg->max_body_bytes)) goto bad; return 0; }
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
* Grammar (one directive per line, '#' starts a comment, double quotes group words):
|
||||
*
|
||||
* listen 0.0.0.0:8090
|
||||
* chars_per_token 4
|
||||
* default_max_tokens 4096
|
||||
* connect_timeout_ms 1500
|
||||
* offline_hold_s 20
|
||||
* max_body_bytes 67108864
|
||||
@@ -107,8 +105,6 @@ typedef struct {
|
||||
typedef struct {
|
||||
char listen_host[J_NAME_MAX];
|
||||
int listen_port;
|
||||
int chars_per_token;
|
||||
long default_max_tokens;
|
||||
double connect_timeout_ms;
|
||||
double offline_hold_s;
|
||||
int max_body_bytes;
|
||||
|
||||
+14
-43
@@ -68,27 +68,6 @@ static void json_escape(const char *in, char *out, size_t outlen)
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------- token sizing */
|
||||
static size_t count_value_chars(const char *body, const char *key)
|
||||
{
|
||||
char pat[64];
|
||||
snprintf(pat, sizeof pat, "\"%s\"", key);
|
||||
size_t total = 0;
|
||||
for (const char *p = body; (p = strstr(p, pat)) != NULL; ) {
|
||||
p += strlen(pat);
|
||||
while (*p && *p != ':') p++;
|
||||
if (*p == ':') p++;
|
||||
while (*p && isspace((unsigned char)*p)) p++;
|
||||
if (*p != '"') continue;
|
||||
p++;
|
||||
while (*p) {
|
||||
if (*p == '\\' && p[1]) { p += 2; total++; continue; }
|
||||
if (*p == '"') break;
|
||||
p++; total++;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
static long json_int_value(const char *body, const char *key)
|
||||
{
|
||||
char pat[64];
|
||||
@@ -132,29 +111,21 @@ static void json_str_value(const char *body, const char *key, char *out, size_t
|
||||
out[i] = '\0';
|
||||
}
|
||||
|
||||
/* How much context does this request need?
|
||||
/* How much output did the CLIENT ask for? Nothing else.
|
||||
*
|
||||
* Input is estimated from the text; the output side has to be guessed. For a
|
||||
* completion the default output size applies, but an embeddings request produces a
|
||||
* vector, not tokens — charging it `default_max_tokens` made every embedding request
|
||||
* "too large" for a small-context embedding endpoint. */
|
||||
static long need_tokens(const char *body, const char *path)
|
||||
* junction is a pass-through: it does not tokenise, does not estimate from character
|
||||
* counts, and never invents a size for a request that did not state one. So `need` is
|
||||
* either the max_tokens/max_completion_tokens the client wrote, or unknown (-1) — and
|
||||
* an endpoint's `context` is only compared against a number the client actually sent.
|
||||
* Guessing here is how a 2048-context embedding endpoint ends up refusing requests
|
||||
* that carry no token budget at all.
|
||||
*/
|
||||
static long declared_max_tokens(const char *body)
|
||||
{
|
||||
size_t chars = 0;
|
||||
if (body) {
|
||||
chars += count_value_chars(body, "content");
|
||||
chars += count_value_chars(body, "prompt");
|
||||
chars += count_value_chars(body, "input");
|
||||
}
|
||||
long mt = body ? json_int_value(body, "max_tokens") : -1;
|
||||
if (mt < 0 && body) mt = json_int_value(body, "max_completion_tokens");
|
||||
|
||||
int embeddings = (path && strstr(path, "embedding")) ||
|
||||
(body && strstr(body, "\"input\"") && !strstr(body, "\"messages\""));
|
||||
if (mt < 0) mt = embeddings ? 0 : CFG.default_max_tokens;
|
||||
|
||||
long cpt = CFG.chars_per_token > 0 ? CFG.chars_per_token : 4;
|
||||
return (long)(chars / (size_t)cpt) + mt;
|
||||
if (!body) return -1;
|
||||
long mt = json_int_value(body, "max_tokens");
|
||||
if (mt < 0) mt = json_int_value(body, "max_completion_tokens");
|
||||
return mt;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------- request */
|
||||
@@ -894,7 +865,7 @@ static void handle_client(j_conn *client)
|
||||
if (!strcmp(rq.path, "/status")) { status_page(client); goto done; }
|
||||
if (!strncmp(rq.path, "/admin/", 7)) { admin(client, rq.path); goto done; }
|
||||
|
||||
long need = need_tokens(rq.body, rq.path);
|
||||
long need = declared_max_tokens(rq.body);
|
||||
char model[J_GLOB_MAX];
|
||||
json_str_value(rq.body, "model", model, sizeof model);
|
||||
|
||||
|
||||
+10
-8
@@ -51,8 +51,6 @@ PIDS+=($!)
|
||||
|
||||
cat > "$TMP/junction.conf" <<CFG
|
||||
listen 127.0.0.1:$PORT
|
||||
chars_per_token 4
|
||||
default_max_tokens 64
|
||||
offline_hold_s 15
|
||||
|
||||
endpoint dead {
|
||||
@@ -130,7 +128,6 @@ curl -s -m 5 -X POST "http://127.0.0.1:$PORT/admin/resume/gated" >/dev/null
|
||||
echo "== 4. rules: block, catch-all, context threshold =="
|
||||
cat > "$TMP/rules.conf" <<CFG
|
||||
listen 127.0.0.1:$((PORT+1))
|
||||
default_max_tokens 64
|
||||
|
||||
endpoint small {
|
||||
base http://127.0.0.1:$P_GATED/v1
|
||||
@@ -164,10 +161,19 @@ R4b=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
|
||||
"http://127.0.0.1:$((PORT+1))/v1/chat/completions")
|
||||
contains "$R4b" "served by GATED" "catch-all rule serves through the small endpoint"
|
||||
|
||||
# "need" only ever uses a number the client wrote — no character counting, no default
|
||||
R4c=$(curl -s -m 40 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"anything","messages":[{"role":"user","content":"hi"}],"max_tokens":120000}' \
|
||||
"http://127.0.0.1:$((PORT+1))/v1/chat/completions")
|
||||
contains "$R4c" "served by SLOW" "declared max_tokens above the threshold picks the big endpoint"
|
||||
R4d=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
|
||||
-d '{"model":"anything","messages":[{"role":"user","content":"a long prompt with no token budget declared at all"}]}' \
|
||||
"http://127.0.0.1:$((PORT+1))/v1/chat/completions")
|
||||
contains "$R4d" "served by GATED" "a request without max_tokens is not sized by guesswork"
|
||||
|
||||
echo "== 5. model rewrite ('pass <endpoint> as <model>') =="
|
||||
cat > "$TMP/rewrite.conf" <<CFG
|
||||
listen 127.0.0.1:$((PORT+2))
|
||||
default_max_tokens 8
|
||||
|
||||
endpoint echo {
|
||||
base http://127.0.0.1:$P_GATED/v1
|
||||
@@ -220,7 +226,6 @@ echo "== 8-10. features: chunked bodies, caching, header passthrough =="
|
||||
FEAT=$((PORT+4))
|
||||
cat > "$TMP/features.conf" <<CFG
|
||||
listen 127.0.0.1:$FEAT
|
||||
default_max_tokens 8
|
||||
cache_ttl 60
|
||||
cache_entries 32
|
||||
cache_bytes 1048576
|
||||
@@ -284,7 +289,6 @@ if command -v openssl >/dev/null 2>&1 && [ -f /usr/include/openssl/ssl.h ]; then
|
||||
TLSP=$((PORT+5))
|
||||
cat > "$TMP/tls.conf" <<CFG
|
||||
listen 127.0.0.1:$TLSP tls $TMP/cert.pem $TMP/key.pem
|
||||
default_max_tokens 8
|
||||
endpoint gated {
|
||||
base http://127.0.0.1:$P_GATED/v1
|
||||
context 64000
|
||||
@@ -308,7 +312,6 @@ CFG
|
||||
sleep 0.8
|
||||
cat > "$TMP/tlsup.conf" <<CFG
|
||||
listen 127.0.0.1:$((PORT+6))
|
||||
default_max_tokens 8
|
||||
endpoint secure {
|
||||
base https://127.0.0.1:9443/v1
|
||||
tls_verify off
|
||||
@@ -330,7 +333,6 @@ CFG
|
||||
# verification must actually be enforced: the same self-signed peer, tls_verify on
|
||||
cat > "$TMP/tlsverify.conf" <<CFG
|
||||
listen 127.0.0.1:$((PORT+7))
|
||||
default_max_tokens 8
|
||||
endpoint secure {
|
||||
base https://127.0.0.1:9443/v1
|
||||
context 64000
|
||||
|
||||
Reference in new issue
Block a user