Files
Hermes 879eac5241 tests: Startrennen und Hintergrundproben unter Last entschärfen
Die sporadischen Ausfälle hatten zwei gemeinsame Ursachen: feste sleeps
statt wait_port für frisch gestartete Stubs, und die Hintergrundprobe,
die einen Endpunkt nach einem einzigen fehlgeschlagenen curl für
offline_hold_s (20 s) aus dem Verkehr nahm. Unter Last traf beides
gelegentlich einen Request: 503 offline statt text-only, 503 statt des
Retry-After-Datums, oder ein nicht erreichbarer Stub.

- probe_thread versucht eine fehlgeschlagene Probe nach 200 ms erneut;
  erst zwei Fehlschläge nehmen den Endpunkt offline.
- retry-after.sh, ctx-vision.sh und flight.sh warten mit wait_port auf
  ihre Stubs, statt 0,3–0,8 s zu schlafen; probe_interval 0 schaltet die
  Hintergrundprobe in diesen deterministischen Abschnitten ab.
- cluster.sh: probe_interval 0, größere Startfristen, der Peer-Cache
  wird gepollt statt nach 0,4 s einmal geprüft, und der Silent-Peer-Test
  erlaubt 5 s statt 2 s (er prüft, dass nichts hängt, nicht die CPU-Zeit).
- mcp_concurrency.py: probe_interval 0; zerrissene Antworten werden mit
  Status und Rumpf ausgegeben, damit ein Wiederauftreten diagnostizierbar
  ist.

Nachweis: zwei vollständige Läufe ohne nice -n -10 unter Last 6,8 bzw.
9,3, jeweils 484 OK / 0 FAIL.
2026-11-18 08:47:15 +00:00

219 lines
9.6 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: Prüfungen für ctx-Filter und Vision-Routing.
# Erwartet: $TMP, $PORT, $P_GATED (Text), $P_SLOW, ok/bad/contains, ./junction
echo "== 17. ctx filters and vision routing =="
cat > "$TMP/ctx.conf" <<CFG
listen 127.0.0.1:$((PORT+15))
ctx_chars_per_token 4
probe_interval 0
endpoint textbox {
base http://127.0.0.1:$P_GATED/v1
context 64000
timeout 10
idle_timeout 5
vision off
}
endpoint eyebox {
base http://127.0.0.1:$P_SLOW/v1
context 200000
timeout 30
idle_timeout 2
vision on
}
# Reihenfolge zählt: das engste Fenster zuerst.
req vision pass eyebox
req ctx <200 pass textbox
req ctx <2000 pass eyebox
req for "*" pass textbox
CFG
./junction --config "$TMP/ctx.conf" > "$TMP/ctx.log" 2>&1 &
PIDS+=($!)
wait_port $((PORT+15)) 30
BASE="http://127.0.0.1:$((PORT+15))/v1/chat/completions"
# 1. Kleiner Text -> textbox
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"kurz"}],"max_tokens":8}' "$BASE")
contains "$R" "served by GATED" "a small prompt goes to the small endpoint"
# 2. Großer Text (über 200 Token bei 4 Zeichen/Token = über 800 Zeichen) -> eyebox
BIG=$(python3 -c "print('wort ' * 400)")
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"m\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIG\"}],\"max_tokens\":8}" "$BASE")
contains "$R" "served by SLOW" "a prompt above the threshold goes to the large endpoint"
# 3. Bild -> eyebox, obwohl die ctx-Regel zuerst textbox wählen würde
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"was ist das"},{"type":"image_url","image_url":{"url":"data:image/png;base64,iVBORw0KGgo="}}]}],"max_tokens":8}' "$BASE")
contains "$R" "served by SLOW" "an image request is routed to the vision endpoint"
# 4. Der Bildanteil darf die Schätzung nicht aufblasen: ein winziges Base64-Bild
# in einem kurzen Text bleibt unter der Schwelle.
IMG=$(python3 -c "print('A'*4000)")
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"m\",\"messages\":[{\"role\":\"user\",\"content\":[{\"type\":\"text\",\"text\":\"kurz\"},{\"type\":\"image_url\",\"image_url\":{\"url\":\"data:image/png;base64,$IMG\"}}]}],\"max_tokens\":8}" "$BASE")
contains "$R" "served by SLOW" "base64 payload does not inflate the estimate into the wrong rule"
# 4b. Ein Werkzeug-Schema, das einen Parameter `image_url` BESCHREIBT, ist kein Bild.
# Hermes schickt bei jeder Anfrage das Schema von vision_analyze mit; ein Scan über
# den ganzen Rumpf machte daraus eine Bildanfrage, und die landete in der Bildkette.
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"kurz"}],"tools":[{"type":"function","function":{"name":"vision_analyze","parameters":{"type":"object","properties":{"image_url":{"type":"string","description":"Image URL"}},"required":["image_url"]}}}],"max_tokens":8}' "$BASE")
contains "$R" "served by GATED" "a tool schema naming image_url is not an image request"
# 4c. Dasselbe für eine Werkzeugausgabe, die den Bild-Teil als Text zitiert (escapt).
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":"kurz"},{"role":"tool","content":"{\"type\":\"image_url\",\"image_url\":{\"url\":\"x\"}}"}],"max_tokens":8}' "$BASE")
contains "$R" "served by GATED" "an escaped image part inside a tool result stays text"
# 4d. Und mit Werkzeugen im Rumpf geht ein echtes Bild weiterhin an die Bildkette.
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":[{"type":"text","text":"was ist das"},{"type":"image_url","image_url":{"url":"data:image/png;base64,iVBORw0KGgo="}}]}],"tools":[{"type":"function","function":{"name":"vision_analyze","parameters":{"type":"object","properties":{"image_url":{"type":"string"}}}}}],"max_tokens":8}' "$BASE")
contains "$R" "served by SLOW" "a real image part still wins over the tool schema"
# 5. Ein Text-Endpunkt wird für ein Bild gar nicht erst versucht
cat > "$TMP/ctx2.conf" <<CFG
listen 127.0.0.1:$((PORT+16))
ctx_chars_per_token 4
probe_interval 0
endpoint textonly {
base http://127.0.0.1:$P_GATED/v1
context 64000
timeout 10
vision off
}
endpoint any {
base http://127.0.0.1:$P_SLOW/v1
context 200000
timeout 30
vision on
}
req for "*" pass textonly, any
CFG
./junction --config "$TMP/ctx2.conf" > "$TMP/ctx2.log" 2>&1 &
PIDS+=($!)
wait_port $((PORT+16)) 30
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"http://example.org/a.png"}}]}],"max_tokens":8}' \
"http://127.0.0.1:$((PORT+16))/v1/chat/completions")
contains "$R" "served by SLOW" "a text-only endpoint is skipped for an image request"
# Der Grund stand bisher nur im Log, wenn ALLE Endpunkte ausfallen. Also genau diesen
# Fall prüfen: nur ein Text-Endpunkt, Bild-Anfrage -> 503, das den Grund nennt.
cat > "$TMP/ctx3.conf" <<CFG
listen 127.0.0.1:$((PORT+17))
probe_interval 0
endpoint textonly {
base http://127.0.0.1:$P_GATED/v1
context 64000
timeout 10
vision off
}
req for "*" pass textonly
CFG
./junction --config "$TMP/ctx3.conf" > "$TMP/ctx3.log" 2>&1 &
PIDS+=($!)
wait_port $((PORT+17)) 30
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"m","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"http://example.org/a.png"}}]}],"max_tokens":8}' \
"http://127.0.0.1:$((PORT+17))/v1/chat/completions")
contains "$R" "text-only" "the refusal names the reason: text-only endpoint, image request"
# 6. ctx ohne Skalierungsfaktor ist ein Konfigurationsfehler, keine stille Regel
printf 'listen 127.0.0.1:9995\nendpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq ctx <500 pass e\n' > "$TMP/noscal.conf"
OUT=$(./junction --config "$TMP/noscal.conf" 2>&1)
contains "$OUT" "ctx_chars_per_token" "a ctx rule without ctx_chars_per_token is rejected"
# 7. Der Schätzwert taucht im Status auf, damit man ihn nachlesen kann
ST=$(curl -s -m 10 "http://127.0.0.1:$((PORT+15))/status")
contains "$ST" '"ctx_max":199' "the compiled ctx bound is visible in /status"
contains "$ST" '"vision":1' "the vision setting is visible in /status"
# 8. Die Grenze des *Modells* schlägt die Zahl des Endpunkts.
# Der Stub veröffentlicht limit.context=1000000, im Endpunkt-Block steht 1000.
# Ein Prompt mit ~5000 Token muss bedient werden — vorher wurde er mit
# "context" übersprungen und der Client bekam 503 no_endpoint.
P_META=$((PORT+25))
P_PLAIN=$((PORT+26))
STUB_CTX=1000000 tests/stub $P_META "BIGCTX" & PIDS+=($!)
tests/stub $P_PLAIN "NOMETA" & PIDS+=($!)
wait_port "$P_META" 30 || bad "the published-limit stub did not come up"
wait_port "$P_PLAIN" 30 || bad "the plain-limit stub did not come up"
for pair in "$P_META:meta" "$P_PLAIN:plain"; do
port=${pair%%:*}; tag=${pair##*:}
cat > "$TMP/$tag.conf" <<CFG
listen 127.0.0.1:$((PORT+27))
ctx_chars_per_token 4
probe_interval 0
endpoint box {
base http://127.0.0.1:$port/v1
context 1000
timeout 15
idle_timeout 5
}
req for "*" pass box
CFG
./junction --config "$TMP/$tag.conf" > "$TMP/$tag.log" 2>&1 &
PIDS+=($!)
wait_port $((PORT+27)) 30
BIGP=$(python3 -c "print('wort ' * 4000)")
# Der Client fragt das Modell, das der Endpunkt veröffentlicht -- nur dann
# greift dessen Grenze.
REQMODEL=NOMETA
[ "$tag" = meta ] && REQMODEL=BIGCTX
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"$REQMODEL\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGP\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$((PORT+27))/v1/chat/completions")
if [ "$tag" = meta ]; then
contains "$R" "served by BIGCTX" "a published model limit overrides the endpoint's smaller number"
else
contains "$R" "no_endpoint" "without published limits the endpoint number still applies"
fi
# junction pro Durchgang beenden, der Port wird wiederverwendet. SIGTERM laesst
# den Listener noch restart_grace_s offen; erst danach darf der naechste binden.
kill "${PIDS[-1]}" 2>/dev/null
wait_port_free $((PORT+27)) 10 || bad "port $((PORT+27)) blieb nach SIGTERM belegt"
done
# 9. Zweite Quelle: die eigene API des Endpunkts.
# Die OpenAI-Liste schweigt (kein limit), aber /api/show nennt 1.000.000 —
# ollama.com macht das so. Der Endpunkt-Block sagt 1000. Der Prompt hat ~5000.
P_SHOW=$((PORT+28))
P_SHOWJ=$((PORT+29))
STUB_SHOW_CTX=1000000 tests/stub $P_SHOW "SHOWCTX" & PIDS+=($!)
wait_port "$P_SHOW" 30 || bad "the /api/show stub did not come up"
cat > "$TMP/show.conf" <<CFG
listen 127.0.0.1:$P_SHOWJ
ctx_chars_per_token 4
probe_interval 0
endpoint box {
base http://127.0.0.1:$P_SHOW/v1
context 1000
timeout 15
idle_timeout 5
}
req for "*" pass box
CFG
./junction --config "$TMP/show.conf" > "$TMP/show.log" 2>&1 & PIDS+=($!)
# Die Grenze kommt aus einem zweiten Abruf; kurz warten, statt zu raten.
for i in $(seq 1 40); do
grep -q "Grenzen aus /api/show" "$TMP/show.log" && break
sleep 0.1
done
BIGP=$(python3 -c "print('wort ' * 4000)")
R=$(curl -s -m 30 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"SHOWCTX\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGP\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$P_SHOWJ/v1/chat/completions")
contains "$R" "served by SHOWCTX" "a limit published only in the endpoint's own API counts too"