Files
Hermes 83a56eb11d tests: wait for a port instead of sleeping before it
Run 13 failed the whole cache section with six checks and no listener: the
router was started with "sleep 0.8" and curl asked too early. The same fixed
sleep sat in front of a dozen other routers and stubs. Every one now waits for
its port (wait_port), and the two checks that need a listener to be *absent*
(mesh without a secret, roll3 without SO_REUSEPORT) wait for their log line
(wait_log) instead of hoping the message is there after 0.6 s.

Same checks, no fixed windows left before a listener; the suite is also faster
because it no longer sleeps through them. Full run: 537 OK, 0 FAIL, PASS.
2026-11-19 06:59:55 +00:00

175 lines
7.0 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: Kürzen von Werkzeugausgaben.
echo "== 21. Werkzeugausgaben kürzen =="
CPORT=$((PORT+41))
# Der Stub spiegelt den Rumpf nicht zurück, deshalb wird hier ein winziger
# Echo-Endpunkt gebraucht: nur so lässt sich prüfen, was tatsächlich ankam.
cat > "$TMP/echo.py" <<'PY'
import http.server, json, sys
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, *a): pass
def do_POST(self):
n = int(self.headers.get("Content-Length", "0"))
body = self.rfile.read(n)
out = json.dumps({"echo": body.decode("utf-8", "replace")}).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
http.server.HTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
PY
python3 "$TMP/echo.py" $((CPORT+1)) & PIDS+=($!)
wait_port $((CPORT + 1)) 10 || bad "the compress echo stub did not come up"
cat > "$TMP/comp.conf" <<CFG
listen 127.0.0.1:$CPORT
compress tool
compress_keep 2
compress_min_bytes 1024
endpoint echo { base http://127.0.0.1:$((CPORT+1))/v1 }
req for "*" pass echo
CFG
./junction --config "$TMP/comp.conf" > "$TMP/comp.log" 2>&1 &
PIDS+=($!)
wait_port "$CPORT" 30 || bad "the compress instance did not come up"
# Rumpf bauen: eine sehr lange Werkzeugausgabe, danach zwei kurze Nachrichten.
python3 - "$TMP/req.json" <<'PY'
import json, sys
big = "\n".join("zeile %d einer langen werkzeugausgabe" % i for i in range(900))
body = {"model": "x", "messages": [
{"role": "system", "content": "systemtext"},
{"role": "user", "content": "mach was"},
{"role": "tool", "tool_call_id": "c1", "content": big},
{"role": "assistant", "content": "erledigt"},
{"role": "user", "content": "und nun"},
], "max_tokens": 7}
json.dump(body, open(sys.argv[1], "w"))
PY
ASK=$(cat "$TMP/req.json")
# 1. Gekürzt, gültig, Rollen respektiert
OUT=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' -d "$ASK" \
"http://127.0.0.1:$CPORT/v1/chat/completions")
echo "$OUT" > "$TMP/out.json"
python3 - "$TMP/out.json" <<'PY' > "$TMP/check.txt" 2>&1
import json, sys
outer = json.load(open(sys.argv[1]))
inner = json.loads(outer["echo"])
m = inner["messages"]
print("messages", len(m))
print("tool_len", len(m[2]["content"]))
print("system_same", m[0]["content"] == "systemtext")
print("user_same", m[1]["content"] == "mach was")
print("last_same", m[4]["content"] == "und nun")
print("max_tokens", inner["max_tokens"])
print("note", "gekuerzt" in m[2]["content"])
PY
if [ "$(grep -c '_same True' "$TMP/check.txt")" -ge 3 ]; then
ok "compression leaves system, user and the last messages untouched"
else
bad "compression touched a message it must not ($(tr '\n' ' ' < "$TMP/check.txt"))"
fi
if grep -q 'note True' "$TMP/check.txt"; then
ok "the shortened tool output carries a note about what was dropped"
else
bad "the shortened tool output has no note"
fi
TOKLEN=$(sed -n 's/^tool_len //p' "$TMP/check.txt")
if [ "${TOKLEN:-0}" -lt 4000 ]; then
ok "a 33 KB tool output came through as $TOKLEN characters"
else
bad "tool output was not shortened ($TOKLEN characters)"
fi
# 2. Der Kunde kann es pro Anfrage abschalten
OUT2=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' \
-H 'X-Junction-Compress: 0' -d "$ASK" "http://127.0.0.1:$CPORT/v1/chat/completions")
L2=$(printf '%s' "$OUT2" | python3 -c 'import json,sys; print(len(json.loads(json.load(sys.stdin)["echo"])["messages"][2]["content"]))')
if [ "${L2:-0}" -gt 20000 ]; then
ok "X-Junction-Compress: 0 sends the body untouched ($L2 characters)"
else
bad "the client could not turn compression off ($L2 characters)"
fi
# 3. Ungültiges JSON wird nicht angefasst — lieber gar nicht als halb kürzen
BAD='{"messages":[{"role":"tool","content":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
OUT3=$(curl -s -m 10 -X POST -H 'Content-Type: application/json' -d "$BAD" \
"http://127.0.0.1:$CPORT/v1/chat/completions")
case "$OUT3" in
*"$BAD"*|*"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"*) ok "broken JSON passes through unchanged" ;;
*) bad "broken JSON was rewritten" ;;
esac
# 4. Der Zähler steht im Status und zählt nur echte Kürzungen
ST=$(curl -s -m 5 "http://127.0.0.1:$CPORT/status")
case "$ST" in
*'"compress":"tool"'*) ok "/status reports the compression mode" ;;
*) bad "/status does not report the compression mode" ;;
esac
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+=($!)
wait_port $((CPORT + 2)) 10 || bad "the second echo stub did not come up"
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+=($!)
wait_port $((CPORT + 3)) 30 || bad "the split instance did not come up"
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