From a68ed88b8413ec53bd9109770a97d7fb26c00f72 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Thu, 29 Oct 2026 14:46:24 +0000 Subject: [PATCH] junction is a pass-through: no token estimation, no invented max_tokens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit '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. --- README.md | 14 +++++++---- config.example | 6 ++--- examples/olivia.conf | 8 +++---- src/config.c | 4 ---- src/config.h | 4 ---- src/junction.c | 57 +++++++++++--------------------------------- tests/run.sh | 18 +++++++------- 7 files changed, 40 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 250f2e3..5ba4e85 100644 --- a/README.md +++ b/README.md @@ -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 ] [path ] [need >N | 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 ` — 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 diff --git a/config.example b/config.example index 0fedbc3..49e9586 100644 --- a/config.example +++ b/config.example @@ -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 # pass [,...] [as ] | 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, diff --git a/examples/olivia.conf b/examples/olivia.conf index 03785e7..648c0bc 100644 --- a/examples/olivia.conf +++ b/examples/olivia.conf @@ -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, diff --git a/src/config.c b/src/config.c index 0415a27..d280cf5 100644 --- a/src/config.c +++ b/src/config.c @@ -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; } diff --git a/src/config.h b/src/config.h index 64840f7..dd27263 100644 --- a/src/config.h +++ b/src/config.h @@ -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; diff --git a/src/junction.c b/src/junction.c index 95e8ee7..875052d 100644 --- a/src/junction.c +++ b/src/junction.c @@ -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); diff --git a/tests/run.sh b/tests/run.sh index 9ac842a..5d79bad 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -51,8 +51,6 @@ PIDS+=($!) cat > "$TMP/junction.conf" </dev/null echo "== 4. rules: block, catch-all, context threshold ==" cat > "$TMP/rules.conf" < as ') ==" cat > "$TMP/rewrite.conf" < "$TMP/features.conf" </dev/null 2>&1 && [ -f /usr/include/openssl/ssl.h ]; then TLSP=$((PORT+5)) cat > "$TMP/tls.conf" < "$TMP/tlsup.conf" < "$TMP/tlsverify.conf" <