#!/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" < "$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" < "$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