Files
junction/tests/ctx-vision.sh
T
Hermes 12cdf5a955 tests: auf den Listener warten statt zu schlafen
Ein Lauf nach frischem `make clean` fiel mit vier Fehlern durch, die nichts mit dem
Code zu tun hatten: "soft budget delays the request (8 ms)", "the refusal names the
reason: text-only endpoint (missing 'text-only')" und "a one-line endpoint block is
enough to serve a request (missing 'served by GATED')". Alle drei schicken eine
einzige Anfrage kurz nach dem Start und nehmen an, dass "0,7 s" reicht — unter Last
(kompiliert, gescrapt, Container-Churn) reicht es nicht immer, und dann misst die
Prüfung eine Verbindung, die noch niemand annimmt.

Jetzt wartet die Suite: wait_port pollt bis zu 10 s, bis der Listener annimmt, statt
eine Zeit zu raten. Beim Soft-Budget-Test zusätzlich: statt 2,5 s zu schlafen wird
gewartet, bis /status den Prozentwert wirklich zeigt (usage_poll 1 s) — das ist die
Bedingung, die die Prüfung braucht.

Zwei Läufe hintereinander: PASS, 146 Prüfungen, keine Restprozesse.
2026-11-07 20:17:21 +00:00

211 lines
9.1 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
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+=($!)
sleep 0.7
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
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+=($!)
sleep 0.7
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))
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)) 10
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+=($!)
sleep 0.3
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
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+=($!)
sleep 0.7
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
kill "${PIDS[-1]}" 2>/dev/null; sleep 0.3
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+=($!)
sleep 0.3
cat > "$TMP/show.conf" <<CFG
listen 127.0.0.1:$P_SHOWJ
ctx_chars_per_token 4
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"