Kürzen pro Endpunkt statt global

Ein großer Cloud-Endpunkt hat Platz genug; eine lokale Box mit 65k Kontext nicht. Also
entscheidet jetzt das Ziel:

  endpoint box3 { base http://...; compress tool }
  endpoint cloud { base http://...; compress off }

Damit das zusammengeht, wird der Rumpf zweimal geschätzt — einmal voll, einmal gekürzt.
Welche Zahl gilt, entscheidet das Ziel: eines mit Kürzen wird gegen den kurzen Text
gemessen, sonst würde eine kleine Box abgelehnt, obwohl sie den Text fasst.

Der Zähler zählt erst beim Senden, nicht beim Vorbereiten — vorher meldete er zwei
Kürzungen, obwohl nur eine rausging.

Ausserdem: /status zeigt "compress" pro Endpunkt.

Live: box3 und box6 kürzen, global aus, keep 2. Gemessen 68.488 auf 2.036 Bytes.
This commit is contained in:
Hermes committed 2026-11-04 23:40:47 +00:00
1 parent 3929cc5907
commit d33b7a1d10
4 files changed
+95 -9

No files matched your search

+8
View File
@@ -108,6 +108,7 @@ static void endpoint_defaults(j_endpoint *ep)
ep->cost_weight = 1.0;
ep->connect_timeout_ms = 0; /* 0 = global erben */
ep->weight = 1;
ep->compress = -1; /* global erben */
ep->vision = -1; /* nicht angegeben: keine Pruefung */
}
@@ -352,6 +353,13 @@ static int endpoint_kw(j_endpoint *ep, char **w, int n, int lineno, char *err, s
if (n != 2 || as_double(w[1], &ep->connect_timeout_ms)) goto bad;
return 0;
}
if (!strcmp(k, "compress")) {
if (n != 2) goto bad;
if (!strcmp(w[1], "off")) ep->compress = 0;
else if (!strcmp(w[1], "tool")) ep->compress = 1;
else goto bad;
return 0;
}
if (!strcmp(k, "weight")) {
if (n != 2 || as_int(w[1], &ep->weight) || ep->weight < 1 || ep->weight > 100) {
snprintf(err, errlen, "line %d: weight must be 1..100", lineno);
+3
View File
@@ -75,6 +75,9 @@ typedef struct {
double connect_timeout_ms;
/* Gewicht für die Auswahl: höher = häufiger bzw. bevorzugt. 1 ist neutral. */
int weight;
/* -1 = global erben, 0 = nie kürzen, 1 = kürzen. Ein großer Endpunkt will den vollen
* Text, eine kleine lokale Box freut sich über jede gesparte Zeile. */
int compress;
double cooldown_s;
double hook_retry_s;
double hook_backoff_s;
+28 -9
View File
@@ -446,7 +446,7 @@ static void status_page(j_conn *c)
"\"usage_percent\":%.2f,\"usage_used_usd\":%.2f,\"usage_budget_usd\":%.2f,"
"\"usage_ok\":%d,\"exhausted\":%d,\"budget_soft\":%.0f,\"budget_hard\":%.0f,"
"\"offpeak\":%d,\"cost\":%.2f,\"hooks\":%d,"
"\"wakeable\":%s,\"tls\":%s,\"cache\":%d,\"vision\":%d,"
"\"wakeable\":%s,\"tls\":%s,\"cache\":%d,\"vision\":%d,\"compress\":%d,"
"\"last_error\":\"%s\",\"last_hook_reason\":\"%s\"}",
i ? "," : "", ep->name, ep->base, ep->kind, ep->context, ep->priority,
ep->paused ? "true" : "false", ep->inflight, ep->ok_count, ep->fail_count,
@@ -458,7 +458,8 @@ static void status_page(j_conn *c)
ep->offpeak_active ? 1 : 0, ep->cost_weight,
ep->hook_count, ep->wake[0] ? "true" : "false",
!strncmp(ep->base, "https://", 8) ? "true" : "false",
ep->cache_on, ep->vision, errs, hook);
ep->cache_on, ep->vision,
(ep->compress >= 0) ? ep->compress : (CFG.compress != J_COMPRESS_OFF), errs, hook);
STATE_UNLOCK();
}
size_t entries = 0, bytes = 0;
@@ -1539,6 +1540,8 @@ static void handle_client(j_conn *client)
jcomp_stats cstat;
memset(&cstat, 0, sizeof cstat);
int want_compress = (CFG.compress != J_COMPRESS_OFF);
for (int i = 0; i < CFG.endpoint_count; i++)
if (CFG.endpoints[i].compress == 1) want_compress = 1;
{
char hdr[16];
if (!j_hdrs_find(&rq.hdrs, "X-Junction-Compress", hdr, sizeof hdr) &&
@@ -1552,16 +1555,15 @@ static void handle_client(j_conn *client)
jlog("compressed", "in=%zu out=%zu messages=%d replaced=%d keep=%d",
cstat.bytes_in, cstat.bytes_out, cstat.messages, cstat.replaced,
CFG.compress_keep);
g_compress_reqs++;
g_compress_bytes_in += cstat.bytes_in;
g_compress_bytes_out += cstat.bytes_out;
rq.body = comp_body; /* ab hier gilt der gekürzte Rumpf */
rq.body_len = comp_len;
/* Der Originalrumpf bleibt stehen: welches Ziel was bekommt, entscheidet
* sich erst bei der Auswahl. */
}
}
long need = declared_max_tokens(rq.body);
long ctx = estimate_ctx_tokens(rq.body, rq.body_len, CFG.ctx_chars_per_token);
long ctx_short = (comp_body && CFG.ctx_chars_per_token > 0)
? estimate_ctx_tokens(comp_body, comp_len, CFG.ctx_chars_per_token) : -1;
int has_image = body_has_image(rq.body);
char model[J_GLOB_MAX];
json_str_value(rq.body, "model", model, sizeof model);
@@ -1699,8 +1701,12 @@ static void handle_client(j_conn *client)
/* Was der Endpunkt an Kontext braucht: die geschätzte Prompt-Größe plus das
* angeforderte Ausgabebudget. Vorher wurde nur das Ausgabebudget geprüft —
* ein 9k-Prompt mit 4k Ausgabe passte damit in einen 8k-Endpunkt. */
/* Ein Ziel, das kürzt, muss auch mit dem gekürzten Rumpf gemessen werden — sonst
* wird eine kleine Box abgelehnt, obwohl sie den Text problemlos fasst. */
int ep_compresses = (ep->compress >= 0) ? ep->compress : (CFG.compress != J_COMPRESS_OFF);
long ctx_here = (ep_compresses && ctx_short >= 0) ? ctx_short : ctx;
long need_total = need;
if (ctx >= 0) need_total = (need >= 0) ? ctx + need : ctx;
if (ctx_here >= 0) need_total = (need >= 0) ? ctx_here + need : ctx_here;
int skip_context = (need_total >= 0 && ep->context < need_total);
int skip_vision = (ep->vision == 0 && has_image);
int skip_offline = ep->offline_until > now_s();
@@ -1751,7 +1757,19 @@ static void handle_client(j_conn *client)
strncat(attempts, " bridge", sizeof attempts - strlen(attempts) - 1);
continue;
}
int rc = forward_step(client, ep, &rq, out_body, out_len, rule->target_as[t], model,
/* Welcher Rumpf geht raus? Ein Ziel mit eigenem Kürzen bekommt den kurzen,
* ein großes den vollen. Beide Schätzungen oben gehören dazu. */
const char *send_body = (ep_compresses && comp_body) ? comp_body : out_body;
size_t send_len = (ep_compresses && comp_body) ? comp_len : out_len;
if (ep_compresses && comp_body) {
/* Erst hier zählen: gekürzt wird zwar für jede Anfrage vorbereitet, aber nur
* ein Ziel, das es will, bekommt den kurzen Text. Ein Zähler am Vorbereiten
* wäre eine Lüge über die Ersparnis. */
g_compress_reqs++;
g_compress_bytes_in += cstat.bytes_in;
g_compress_bytes_out += cstat.bytes_out;
}
int rc = forward_step(client, ep, &rq, send_body, send_len, rule->target_as[t], model,
may_cache ? "miss" : "off", started, head_sent, err, sizeof err);
STATE_LOCK();
ep->inflight--;
@@ -1805,6 +1823,7 @@ static void handle_client(j_conn *client)
}
done:
free(comp_body);
free(rq.body);
j_buf_free(&buf);
}
+56
View File
@@ -116,3 +116,59 @@ case "$ST" in
*'"compress_requests":1'*) ok "the counter counts one compression, not three" ;;
*) bad "the compression counter is wrong: $(printf '%s' "$ST" | head -c 160)" ;;
esac
# 5. Pro Endpunkt: einer kürzt, der andere nicht — derselbe Rumpf, zwei Rümpfe
python3 "$TMP/echo.py" $((CPORT+2)) & PIDS+=($!)
sleep 0.6
cat > "$TMP/split.conf" <<CFG
listen 127.0.0.1:$((CPORT+3))
compress off
compress_keep 2
compress_min_bytes 1024
endpoint klein { base http://127.0.0.1:$((CPORT+1))/v1; compress tool }
endpoint gross { base http://127.0.0.1:$((CPORT+2))/v1; compress off }
req for "klein*" pass klein
req for "gross*" pass gross
CFG
./junction --config "$TMP/split.conf" > "$TMP/split.log" 2>&1 &
PIDS+=($!)
sleep 0.8
SEND=$(python3 -c '
import json
big = "\n".join("zeile %d" % i for i in range(900))
print(json.dumps({"model": MODEL, "messages": [
{"role": "system", "content": "s"}, {"role": "user", "content": "u"},
{"role": "tool", "tool_call_id": "c", "content": big},
{"role": "assistant", "content": "a"}, {"role": "user", "content": "z"}]}))
' 2>/dev/null)
mk() {
python3 -c "
import json,sys
big='\n'.join('zeile %d' % i for i in range(900))
print(json.dumps({'model':'$1','messages':[
{'role':'system','content':'s'},{'role':'user','content':'u'},
{'role':'tool','tool_call_id':'c','content':big},
{'role':'assistant','content':'a'},{'role':'user','content':'z'}]}))
"
}
OUTK=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' -d "$(mk klein-1)" \
"http://127.0.0.1:$((CPORT+3))/v1/chat/completions")
OUTG=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' -d "$(mk gross-1)" \
"http://127.0.0.1:$((CPORT+3))/v1/chat/completions")
LK=$(printf '%s' "$OUTK" | python3 -c 'import json,sys; print(len(json.loads(json.load(sys.stdin)["echo"])["messages"][2]["content"]))' 2>/dev/null)
LG=$(printf '%s' "$OUTG" | python3 -c 'import json,sys; print(len(json.loads(json.load(sys.stdin)["echo"])["messages"][2]["content"]))' 2>/dev/null)
# Kein fester Schwellwert: der gekürzte Rumpf muss deutlich kleiner sein als der volle.
if [ "${LK:-0}" -gt 0 ] && [ "${LG:-0}" -gt $(( ${LK:-0} * 3 )) ] && [ "${LK:-0}" -lt 4000 ]; then
ok "one endpoint gets the short body ($LK), the other the full one ($LG)"
else
bad "per-endpoint compression failed (short=$LK full=$LG)"
fi
SPLITST=$(curl -s -m 5 "http://127.0.0.1:$((CPORT+3))/status")
case "$SPLITST" in
*'"compress_requests":1'*) ok "only the endpoint that compressed counts towards the saving" ;;
*) bad "the counter counted a request that was not compressed" ;;
esac