Files
Hermes 299fd600ac review: max 12 rounds in the config, and stop when the answer is stable
The operator pays with local GPU time, not per token: the config may now ask for
up to 12 rounds (review 13 is a config error), matching the per-request header.
A round that returns the answer verbatim is a confirmation, not a new candidate:
count the round, keep its JSON as the last good answer, log converged=1 and
stop. Without this, twelve rounds on a correct answer only gave the reviewer
twelve chances to rewrite it.

tests/review.sh: a stub with twelve distinct answers proves seven rounds run; a
stub that repeats the answer stops after two rounds (three calls instead of
four) and the response says X-Junction-Review-Rounds: 2. review 13 is rejected,
review 12 is accepted. tests/run.sh: 555 OK, PASS.
2026-11-19 12:04:17 +00:00

558 lines
28 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: review N lässt dasselbe Modell die eigene
# Antwort N-mal prüfen. Der Client sieht nur die letzte Fassung; Tool-Calls gehen
# ungeprüft durch. Die Stub-Antworten wechseln pro Aufruf, damit die Runden
# nachweisbar sind.
echo
echo "== 42. Self-Review: N versteckte Runden, nur die letzte Antwort =="
RW=$((PORT+280))
RJ=$((PORT+281))
RT=$((PORT+282))
RJT=$((PORT+283))
RB=$((PORT+284))
RBJ=$((PORT+285))
RA=$((PORT+286))
RAJ=$((PORT+287))
RS=$((PORT+288))
RSJ=$((PORT+289))
RU=$((PORT+290))
RUJ=$((PORT+291))
RHP=$((PORT+292))
RHPJ=$((PORT+293))
RBT=$((PORT+294))
RNT=$((PORT+295))
RNTJ=$((PORT+296))
RCV=$((PORT+297))
RCVJ=$((PORT+298))
STUB_STATE="$TMP/review.state" STUB_ANSWERS="first answer|second answer|third answer|fourth answer|fifth answer|sixth answer|seventh answer|eighth answer|ninth answer|tenth answer|eleventh answer|twelfth answer" \
tests/stub $RW "REVIEWER" > "$TMP/review_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_tools.state" STUB_ANSWERS="tool answer" STUB_TOOLS=3 \
tests/stub $RT "TOOLER" > "$TMP/review_tools_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_bad.state" STUB_ANSWERS="unused" tests/stub $RB "BADREQUEST" --status 400 \
> "$TMP/review_bad_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_array.state" STUB_ANSWERS="array answer" STUB_CONTENT_ARRAY=1 \
tests/stub $RA "ARRAYER" > "$TMP/review_array_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_so.state" STUB_ANSWERS="so answer|so reviewed" \
STUB_REJECT_STREAM_OPTIONS=1 tests/stub $RS "STRICT" > "$TMP/review_so_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_usage.state" STUB_ANSWERS="usage one|usage two|usage three" \
STUB_USAGE=1 tests/stub $RU "USAGE" > "$TMP/review_usage_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_converge.state" STUB_ANSWERS="first answer|same answer|same answer" \
tests/stub $RCV "CONVERGE" > "$TMP/review_converge_stub.log" 2>&1 &
PIDS+=($!)
STUB_MANY_HEADERS=110 STUB_BAD_HEADER=1 tests/stub $RHP "HEADERS" \
> "$TMP/review_headers_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/review_null_tools.state" STUB_ANSWERS="null tools answer" \
STUB_NULL_TOOLS=1 tests/stub $RNT "NULLTOOLS" > "$TMP/review_null_tools_stub.log" 2>&1 &
PIDS+=($!)
wait_port $RW 10 || bad "the review stub did not come up"
wait_port $RT 10 || bad "the tool-review stub did not come up"
wait_port $RB 10 || bad "the 400 stub did not come up"
wait_port $RA 10 || bad "the content-array stub did not come up"
wait_port $RS 10 || bad "the strict stub did not come up"
wait_port $RU 10 || bad "the usage stub did not come up"
wait_port $RHP 10 || bad "the header stub did not come up"
wait_port $RNT 10 || bad "the null-tools stub did not come up"
cat > "$TMP/review.conf" <<CFG
listen 127.0.0.1:$RJ
queue_timeout 10
models allow "vm-reviewed" "REVIEWER"
endpoint worker { base http://127.0.0.1:$RW/v1; context 8000; timeout 10 }
req path "/v1/models" pass worker
req for "vm-reviewed" review 2 pass worker as "worker-model"
CFG
J_DUMP_REQ="$TMP/review-dump.txt" ./junction --config "$TMP/review.conf" \
> "$TMP/review.log" 2>&1 &
PIDS+=($!)
wait_port "$RJ" 10 || bad "the review instance did not come up"
REVIEW_BODY='{"model":"vm-reviewed","messages":[{"role":"system","content":"Be terse."},{"role":"user","content":"Say OK."}],"max_tokens":8}'
# Non-streaming: drei Aufrufe (Original + 2 Reviews), nur "third answer" kommt an.
R=$(curl -s -D "$TMP/review_h.txt" -m 30 -X POST -H 'Content-Type: application/json' \
-d "$REVIEW_BODY" "http://127.0.0.1:$RJ/v1/chat/completions")
contains "$R" "third answer" "the client gets the last reviewed answer"
case "$R" in *"first answer"*|*"second answer"*)
bad "an intermediate answer leaked to the client" ;;
*) ok "no intermediate answer leaked to the client" ;;
esac
CALLS=$(tr -d '\n' < "$TMP/review.state")
[ "$CALLS" = "3" ] && ok "review 2 means three model calls" \
|| bad "expected 3 model calls, got '$CALLS'"
contains "$(cat "$TMP/review.log")" "round=1 endpoint=worker ok=1" \
"the first review round is logged"
contains "$(cat "$TMP/review.log")" "round=2 endpoint=worker ok=1" \
"the second review round is logged"
contains "$(cat "$TMP/review-dump.txt")" "Review your previous answer" \
"the review prompt reached the model"
contains "$(cat "$TMP/review-dump.txt")" "The user asked:" \
"each review round carries the user's question"
contains "$(cat "$TMP/review-dump.txt")" "Say OK." \
"the review prompt carries the original user text"
contains "$(cat "$TMP/review-dump.txt")" "Your answer so far:" \
"each review round names the answer it is reviewing"
contains "$(cat "$TMP/review-dump.txt")" "not a spelling or style check" \
"the review prompt says it is not a spelling check"
contains "$(cat "$TMP/review-dump.txt")" "first answer" \
"the first answer was fed back into the first review"
contains "$(cat "$TMP/review-dump.txt")" "second answer" \
"the second answer was fed back into the second review"
contains "$(cat "$TMP/review-dump.txt")" '"max_tokens":4096' \
"the review rounds get a usable output budget"
RH=$(tr -d '\r' < "$TMP/review_h.txt")
contains "$RH" "X-Junction-Review: 2" "the response header names the review count"
contains "$RH" "X-Junction-Review-Rounds: 2" "the response header names the completed rounds"
contains "$RH" "X-Junction-Review-Model: worker-model" "the response header names the review model"
contains "$RH" "X-Junction-Endpoint: worker" "the response header names the endpoint"
# Streaming: dieselben Runden, aber der Client bekommt eine finale SSE-Antwort.
rm -f "$TMP/review.state"
SSE_BODY='{"model":"vm-reviewed","stream":true,"messages":[{"role":"system","content":"Be terse."},{"role":"user","content":"Say OK."}],"max_tokens":8}'
SSE=$(curl -s -N -D "$TMP/review_sh.txt" -m 30 -X POST -H 'Content-Type: application/json' \
-d "$SSE_BODY" "http://127.0.0.1:$RJ/v1/chat/completions")
contains "$(tr -d '\r' < "$TMP/review_sh.txt")" "X-Junction-Review: 2" \
"the stream head names the review count"
contains "$SSE" '"junction":{"review_rounds":2' \
"the final stream chunk carries the review outcome"
contains "$SSE" 'data: [DONE]' "the review stream ends with [DONE]"
case "$SSE" in
*'"usage"'*) bad "a stream without provider usage must not invent one" ;;
*) ok "a stream without provider usage carries no usage frame" ;;
esac
contains "$SSE" "third answer" "the stream carries the last reviewed answer"
case "$SSE" in *"first answer"*|*"second answer"*)
bad "an intermediate answer leaked into the stream" ;;
*) ok "no intermediate answer leaked into the stream" ;;
esac
CALLS=$(tr -d '\n' < "$TMP/review.state")
[ "$CALLS" = "3" ] && ok "the streaming path also runs three calls" \
|| bad "the streaming path ran '$CALLS' calls instead of 3"
# Tool-Call-Antworten werden nicht als Text geprüft, sondern durchgereicht.
cat > "$TMP/review_tools.conf" <<CFG
listen 127.0.0.1:$RJT
queue_timeout 10
models allow "vm-tools" "TOOLER"
endpoint tools { base http://127.0.0.1:$RT/v1; context 8000; timeout 10 }
req for "vm-tools" review 2 pass tools as "tool-model"
CFG
./junction --config "$TMP/review_tools.conf" > "$TMP/review_tools.log" 2>&1 &
PIDS+=($!)
wait_port "$RJT" 10 || bad "the tool-review instance did not come up"
RTC=$(curl -s -D "$TMP/review_tools_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-tools","messages":[{"role":"user","content":"do it"}],"max_tokens":8,"tools":[{"type":"function","function":{"name":"doit","parameters":{"type":"object"}}}]}' \
"http://127.0.0.1:$RJT/v1/chat/completions")
contains "$RTC" '"tool_calls"' "a tool-call answer passes through unreviewed"
contains "$(cat "$TMP/review_tools.log")" "skipped=tool_calls" \
"the skipped review is logged"
RTH=$(tr -d '\r' < "$TMP/review_tools_h.txt")
contains "$RTH" "X-Junction-Review-Rounds: 0" "the tool turn reports zero review rounds"
contains "$RTH" "X-Junction-Review-Skipped: tool_calls" \
"the tool turn names why the review was skipped"
TCALLS=$(tr -d '\n' < "$TMP/review_tools.state")
[ "$TCALLS" = "1" ] && ok "no review call was made for the tool-call answer" \
|| bad "the tool-call answer caused '$TCALLS' model calls"
# Ein Streaming-Client bekommt die Calls mit ihren Indizes zurueck. Ein
# nicht-streamendes tool_calls-Array hat keine; schiebt ein Client sie zusammen,
# sieht er einen Call, dessen Argument mehrere JSON-Objekte sind
# ("{}{...}{...}") -- genau das liess splinter an vm-tools scheitern.
curl -s -N -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-tools","stream":true,"messages":[{"role":"user","content":"do it"}],"max_tokens":8,"tools":[{"type":"function","function":{"name":"doit","parameters":{"type":"object"}}}]}' \
"http://127.0.0.1:$RJT/v1/chat/completions" > "$TMP/review_tools_sse.txt"
if python3 - "$TMP/review_tools_sse.txt" <<'PY'
import json, sys
calls = {}
done = False
for line in open(sys.argv[1], errors="replace"):
line = line.strip()
if not line.startswith("data: "):
continue
payload = line[6:]
if payload == "[DONE]":
done = True
continue
try:
event = json.loads(payload)
except Exception:
continue
for choice in event.get("choices", []):
for call in (choice.get("delta") or {}).get("tool_calls") or []:
index = call.get("index")
if index is None:
raise SystemExit("tool call without index: %r" % (call,))
fn = call.get("function") or {}
calls[index] = (fn.get("name"), fn.get("arguments"))
want = {0: ("doit1", "{}"), 1: ("doit2", "{}"), 2: ("doit3", "{}")}
if not done:
raise SystemExit("stream did not end with [DONE]")
if calls != want:
raise SystemExit("tool calls merged or renamed: %r" % (calls,))
PY
then ok "streamed tool calls keep their index and their own names"
else bad "streamed tool calls were merged or lost their index"
fi
# Per-request review depth: the rule stays the default, the header overrides it
# for one request. 0 turns the review off, N raises it (0..12), effort names map
# to 4/7/9/12.
: > "$TMP/review.state"
D0=$(curl -s -D "$TMP/depth0.h" -m 30 -X POST -H 'Content-Type: application/json' \
-H 'X-Junction-Review-Rounds: 0' -d "$REVIEW_BODY" \
"http://127.0.0.1:$RJ/v1/chat/completions")
[ "$(tr -d '\n' < "$TMP/review.state")" = "1" ] \
&& ok "review rounds 0: only the worker call" \
|| bad "review rounds 0 made '$(tr -d '\n' < "$TMP/review.state")' calls"
case "$(tr -d '\r' < "$TMP/depth0.h")" in
*"X-Junction-Review"*) bad "review rounds 0 still announced a review" ;;
*) ok "review rounds 0 sends no review header" ;;
esac
: > "$TMP/review.state"
D3=$(curl -s -D "$TMP/depth3.h" -m 30 -X POST -H 'Content-Type: application/json' \
-H 'X-Junction-Review-Rounds: 3' -d "$REVIEW_BODY" \
"http://127.0.0.1:$RJ/v1/chat/completions")
[ "$(tr -d '\n' < "$TMP/review.state")" = "4" ] \
&& ok "review rounds 3: worker plus three reviews" \
|| bad "review rounds 3 made '$(tr -d '\n' < "$TMP/review.state")' calls instead of 4"
contains "$(tr -d '\r' < "$TMP/depth3.h")" "X-Junction-Review: 3" \
"review rounds 3 is named in the response"
: > "$TMP/review.state"
DE=$(curl -s -D "$TMP/effort.h" -m 60 -X POST -H 'Content-Type: application/json' \
-H 'X-Junction-Effort: medium' -d "$REVIEW_BODY" \
"http://127.0.0.1:$RJ/v1/chat/completions")
[ "$(tr -d '\n' < "$TMP/review.state")" = "8" ] \
&& ok "effort medium: worker plus seven reviews" \
|| bad "effort medium made '$(tr -d '\n' < "$TMP/review.state")' calls instead of 8"
contains "$(tr -d '\r' < "$TMP/effort.h")" "X-Junction-Review: 7" \
"effort medium reports seven rounds"
: > "$TMP/review.state"
D99=$(curl -s -D "$TMP/depth99.h" -m 30 -X POST -H 'Content-Type: application/json' \
-H 'X-Junction-Review-Rounds: 99' -d "$REVIEW_BODY" \
"http://127.0.0.1:$RJ/v1/chat/completions")
[ "$(tr -d '\n' < "$TMP/review.state")" = "3" ] \
&& ok "an out-of-range depth falls back to the rule" \
|| bad "an out-of-range depth made '$(tr -d '\n' < "$TMP/review.state")' calls instead of 3"
# /v1/models und /status kennen das virtuelle Modell und die Rundenzahl.
M=$(curl -s -m 10 "http://127.0.0.1:$RJ/v1/models")
contains "$M" '"id":"vm-reviewed"' "the reviewed virtual model is listed"
ST=$(curl -s -m 10 "http://127.0.0.1:$RJ/status")
contains "$ST" '"review":2' "the status names the review count"
# Ein Provider-400 ist ein Client-Fehler und muss als 400 ankommen, nicht als
# generisches 503 no_endpoint. Die Review-Runden dürfen dafür nicht laufen.
cat > "$TMP/review_bad.conf" <<CFG
listen 127.0.0.1:$RBJ
queue_timeout 10
retry_num 0
models allow "vm-bad" "BADREQUEST"
endpoint bad { base http://127.0.0.1:$RB/v1; context 8000; timeout 10 }
req for "vm-bad" review 2 pass bad as "bad-model"
CFG
./junction --config "$TMP/review_bad.conf" > "$TMP/review_bad.log" 2>&1 &
PIDS+=($!)
wait_port "$RBJ" 10 || bad "the 400-review instance did not come up"
BADCODE=$(curl -s -D "$TMP/review_bad_h.txt" -o "$TMP/review_bad_b.json" -m 20 \
-X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-bad","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RBJ/v1/chat/completions" -w '%{http_code}')
[ "$BADCODE" = "400" ] && ok "a provider 400 reaches the review client as 400" \
|| bad "provider 400 became HTTP $BADCODE"
contains "$(cat "$TMP/review_bad_b.json")" "bad request from stub" \
"the provider's 400 body is preserved"
BCALLS=$(tr -d '\n' < "$TMP/review_bad.state")
[ "$BCALLS" = "1" ] && ok "the 400 stopped before any review round" \
|| bad "the 400 path made '$BCALLS' provider calls"
# Derselbe Fehler bei einem Streaming-Client: der Kopf ist schon 200, der
# Provider-Fehler muss als SSE-Event mit Code 400 ankommen.
rm -f "$TMP/review_bad.state"
BSSE=$(curl -s -N -D /dev/null -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-bad","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RBJ/v1/chat/completions")
contains "$BSSE" "400" "the streaming review error carries the 400"
BCALLS=$(tr -d '\n' < "$TMP/review_bad.state")
[ "$BCALLS" = "1" ] && ok "the streaming 400 also stopped before reviews" \
|| bad "the streaming 400 path made '$BCALLS' provider calls"
# n>1: mehrere Choices sind keine eine Antwort, die geprüft werden könnte.
# Sie müssen unverändert ankommen, und keine Review-Runde darf laufen.
rm -f "$TMP/review.state"
N2=$(curl -s -D "$TMP/review_n2_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-reviewed","n":2,"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RJ/v1/chat/completions")
contains "$N2" '"index":1' "n>1: both choices reach the client"
N2H=$(tr -d '\r' < "$TMP/review_n2_h.txt")
contains "$N2H" "X-Junction-Review-Skipped: n>1" "n>1 names the skip reason"
contains "$N2H" "X-Junction-Review-Rounds: 0" "n>1 runs no review round"
NCALLS=$(tr -d '\n' < "$TMP/review.state")
[ "$NCALLS" = "1" ] && ok "n>1 makes only the original model call" \
|| bad "n>1 made '$NCALLS' model calls"
contains "$(cat "$TMP/review.log")" "review_skipped endpoint=worker reason=n>1" \
"n>1 is logged with its reason"
# Content-Arrays: OpenAI erlaubt content als Liste von Teilen. Ein Review würde
# die Struktur neu zusammensetzen; stattdessen bleibt die Antwort unverändert.
cat > "$TMP/review_array.conf" <<CFG
listen 127.0.0.1:$RAJ
queue_timeout 10
models allow "vm-array" "ARRAYER"
endpoint arr { base http://127.0.0.1:$RA/v1; context 8000; timeout 10 }
req for "vm-array" review 1 pass arr as "array-model"
CFG
./junction --config "$TMP/review_array.conf" > "$TMP/review_array.log" 2>&1 &
PIDS+=($!)
wait_port "$RAJ" 10 || bad "the content-array instance did not come up"
ARR=$(curl -s -D "$TMP/review_array_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-array","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RAJ/v1/chat/completions")
contains "$ARR" '"content":[{"type":"text","text":"array answer"}]' \
"a content-array answer reaches the client unchanged"
ARRH=$(tr -d '\r' < "$TMP/review_array_h.txt")
contains "$ARRH" "X-Junction-Review-Skipped: content_array" \
"the content-array skip names its reason"
contains "$ARRH" "X-Junction-Review-Rounds: 0" "the content-array answer is not reviewed"
ACALLS=$(tr -d '\n' < "$TMP/review_array.state")
[ "$ACALLS" = "1" ] && ok "the content-array answer makes only the original call" \
|| bad "the content-array answer made '$ACALLS' model calls"
# stream_options gehört zu einer Streaming-Anfrage. Der Review-Pfad erzwingt
# stream:false und muss das Feld entfernen, sonst lehnen strenge Provider ab.
cat > "$TMP/review_so.conf" <<CFG
listen 127.0.0.1:$RSJ
queue_timeout 10
models allow "vm-so" "STRICT"
endpoint strict { base http://127.0.0.1:$RS/v1; context 8000; timeout 10 }
req for "vm-so" review 1 pass strict as "strict-model"
CFG
J_DUMP_REQ="$TMP/review_so_dump.txt" ./junction --config "$TMP/review_so.conf" \
> "$TMP/review_so.log" 2>&1 &
PIDS+=($!)
wait_port "$RSJ" 10 || bad "the stream_options instance did not come up"
SO=$(curl -s -D "$TMP/review_so_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-so","stream_options":{"include_usage":true},"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RSJ/v1/chat/completions")
contains "$SO" "so reviewed" "the review call reaches a strict provider without stream_options"
SOH=$(tr -d '\r' < "$TMP/review_so_h.txt")
contains "$SOH" "X-Junction-Review-Rounds: 1" "the strict provider allowed the review round"
SOCALLS=$(tr -d '\n' < "$TMP/review_so.state")
[ "$SOCALLS" = "2" ] && ok "the strict provider saw the original and one review call" \
|| bad "the strict provider saw '$SOCALLS' calls instead of 2"
if python3 - "$TMP/review_so_dump.txt" <<'PY'
import json, sys
raw = open(sys.argv[1]).read()
bodies = 0
for chunk in raw.split("--- ")[1:]:
marker = "=== BODY ===\n"
if marker not in chunk:
continue
json.loads(chunk.split(marker, 1)[1])
bodies += 1
if bodies != 2:
raise SystemExit("expected 2 dumped bodies, got %d" % bodies)
PY
then ok "both upstream bodies stay valid JSON after removing stream_options"
else bad "removing stream_options produced invalid JSON"
fi
# Die Usage des Clients beschreibt nur den letzten Call. Der junction-Block
# summiert Original und alle Review-Runden; der Preis kommt aus der Tabelle.
cat > "$TMP/review_usage_prices.conf" <<'CFG'
# test/review in cached out
test/review 1.0 0.0 2.0
CFG
cat > "$TMP/review_usage.conf" <<CFG
listen 127.0.0.1:$RUJ
queue_timeout 10
prices $TMP/review_usage_prices.conf
models allow "vm-usage" "USAGE"
endpoint u { base http://127.0.0.1:$RU/v1; context 8000; timeout 10 }
req for "vm-usage" review 2 pass u as "test/review"
CFG
./junction --config "$TMP/review_usage.conf" > "$TMP/review_usage.log" 2>&1 &
PIDS+=($!)
wait_port "$RUJ" 10 || bad "the usage-review instance did not come up"
UOUT=$(curl -s -D "$TMP/review_usage_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-usage","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RUJ/v1/chat/completions")
contains "$UOUT" "usage three" "the usage test gets the last reviewed answer"
contains "$UOUT" '"junction":{"review_rounds":2' "the final JSON carries the junction object"
contains "$UOUT" '"usage":{"prompt_tokens":3600,"completion_tokens":900,"cached_tokens":0,"total_tokens":4500}' \
"the junction object sums the usage of all three calls"
contains "$UOUT" '"cost_usd":0.005400' "the junction object sums the cost of all three calls"
UH=$(tr -d '\r' < "$TMP/review_usage_h.txt")
contains "$UH" "X-Junction-Review-Tokens-In: 3600" "the header carries the summed input tokens"
contains "$UH" "X-Junction-Review-Tokens-Out: 900" "the header carries the summed output tokens"
contains "$UH" "X-Junction-Review-Cost-Usd: 0.005400" "the header carries the summed cost"
contains "$UH" "X-Junction-Cost-Usd: 0.005400" "the general cost header carries the review total too"
contains "$UH" "X-Junction-Ms: " "the request time covers the whole review"
# Streaming: derselbe Block steckt im finalen SSE-Chunk, nicht nur im JSON-Pfad.
rm -f "$TMP/review_usage.state"
USSE=$(curl -s -N -D /dev/null -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-usage","stream":true,"messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RUJ/v1/chat/completions")
contains "$USSE" '"usage":{"prompt_tokens":3600,"completion_tokens":900,"cached_tokens":0,"total_tokens":4500}' \
"the final stream chunk sums the usage of all three calls"
contains "$USSE" '"cost_usd":0.005400' "the final stream chunk sums the cost"
contains "$USSE" '"review_rounds":2' "the final stream chunk names the rounds"
# Header-Werte werden entschärft und eine volle Headerliste wird sichtbar
# verworfen: ein eingebettetes Steuerzeichen darf keine zweite Header-Zeile
# erzeugen, und >96 Header dürfen nicht still verschwinden.
cat > "$TMP/review_headers.conf" <<CFG
listen 127.0.0.1:$RHPJ
queue_timeout 10
models allow "vm-headers" "HEADERS"
endpoint h { base http://127.0.0.1:$RHP/v1; context 8000; timeout 10 }
req for "vm-headers" check none pass h
CFG
./junction --config "$TMP/review_headers.conf" > "$TMP/review_headers.log" 2>&1 &
PIDS+=($!)
wait_port "$RHPJ" 10 || bad "the header instance did not come up"
HH=$(curl -s -D "$TMP/review_headers_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-headers","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RHPJ/v1/chat/completions")
contains "$HH" "served by HEADERS" "the many-header response is still served"
tr -d '\r' < "$TMP/review_headers_h.txt" | grep -qi '^X-Stub-Bad: a b$' \
&& ok "a control character in a header value becomes a space" \
|| bad "the control character was not neutralised"
if LC_ALL=C grep -q $'\001' "$TMP/review_headers_h.txt"; then
bad "the raw control character reached the client"
else
ok "the raw control character does not reach the client"
fi
contains "$(cat "$TMP/review_headers.log")" "header_control_chars name=X-Stub-Bad" \
"the sanitised header is logged"
contains "$(cat "$TMP/review_headers.log")" "headers_dropped name=X-Stub-Many-" \
"a full header list logs what was dropped"
contains "$(cat "$TMP/review_headers.log")" "max=96" "the drop log names the capacity"
# Ein nicht-numerisches max_tokens ersetzte bisher nur dann, wenn strtol eine
# Zahl fand; sonst wurde ein zweites Feld angehängt und der Rumpf ungültig.
cat > "$TMP/review_bad_tokens.conf" <<CFG
listen 127.0.0.1:$RBT
queue_timeout 10
models allow "vm-bad-tokens" "REVIEWER"
endpoint worker { base http://127.0.0.1:$RW/v1; context 8000; timeout 10 }
req for "vm-bad-tokens" review 1 pass worker as "worker-model"
CFG
J_DUMP_REQ="$TMP/review_bad_tokens_dump.txt" ./junction --config "$TMP/review_bad_tokens.conf" \
> "$TMP/review_bad_tokens.log" 2>&1 &
PIDS+=($!)
wait_port "$RBT" 10 || bad "the bad-max_tokens instance did not come up"
BT=$(curl -s -D "$TMP/review_bad_tokens_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-bad-tokens","messages":[{"role":"user","content":"hi"}],"max_tokens":"lots"}' \
"http://127.0.0.1:$RBT/v1/chat/completions")
contains "$BT" "third answer" "a malformed max_tokens does not break the review"
contains "$(tr -d '\r' < "$TMP/review_bad_tokens_h.txt")" "X-Junction-Review-Rounds: 1" \
"the review still ran with a malformed max_tokens"
if python3 - "$TMP/review_bad_tokens_dump.txt" <<'PY'
import json, sys
raw = open(sys.argv[1]).read()
bodies = reviews = 0
for chunk in raw.split("--- ")[1:]:
marker = "=== BODY ===\n"
if marker not in chunk:
continue
rawbody = chunk.split(marker, 1)[1]
json.loads(rawbody)
bodies += 1
# The worker call keeps the client's budget exactly. Only the body the
# review path builds gets the 4096 floor, and it must carry one key, not two.
if "Review your previous answer" not in rawbody:
continue
if rawbody.count('"max_tokens"') != 1:
raise SystemExit("duplicate max_tokens key in the review body")
if json.loads(rawbody).get("max_tokens") != 4096:
raise SystemExit("review max_tokens was not raised")
reviews += 1
if bodies != 2 or reviews != 1:
raise SystemExit("expected 2 bodies/1 review, got %d/%d" % (bodies, reviews))
PY
then ok "the review body stays valid JSON with exactly one max_tokens=4096"
else bad "the malformed max_tokens produced an invalid or duplicated review body"
fi
# "tool_calls":null ist kein Tool-Call. Die Antwort ist Text und darf geprüft
# werden; nur ein nicht-leeres Array muss ungeprüft durch.
cat > "$TMP/review_null_tools.conf" <<CFG
listen 127.0.0.1:$RNTJ
queue_timeout 10
models allow "vm-null-tools" "NULLTOOLS"
endpoint t { base http://127.0.0.1:$RNT/v1; context 8000; timeout 10 }
req for "vm-null-tools" review 1 pass t as "null-model"
CFG
./junction --config "$TMP/review_null_tools.conf" > "$TMP/review_null_tools.log" 2>&1 &
PIDS+=($!)
wait_port "$RNTJ" 10 || bad "the null-tools instance did not come up"
NT=$(curl -s -D "$TMP/review_null_tools_h.txt" -m 20 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-null-tools","messages":[{"role":"user","content":"hi"}],"max_tokens":8}' \
"http://127.0.0.1:$RNTJ/v1/chat/completions")
contains "$NT" "null tools answer" "the null-tools answer reaches the client"
NTH=$(tr -d '\r' < "$TMP/review_null_tools_h.txt")
contains "$NTH" "X-Junction-Review-Rounds: 1" "tool_calls:null is reviewed as text"
case "$NTH" in
*"X-Junction-Review-Skipped"*) bad "tool_calls:null was treated as a skipped tool call" ;;
*) ok "tool_calls:null sets no skip reason" ;;
esac
NTCALLS=$(tr -d '\n' < "$TMP/review_null_tools.state")
[ "$NTCALLS" = "2" ] && ok "tool_calls:null makes the original and one review call" \
|| bad "tool_calls:null made '$NTCALLS' calls instead of 2"
# Die usage des letzten Calls gehört im SSE-Pfad in einen eigenen Chunk.
contains "$USSE" '"choices":[],"usage":{"prompt_tokens":1200,"completion_tokens":300' \
"the stream ends with a choices-empty usage chunk"
# Ein Reviewer, der die Antwort wortgleich wiederholt, hat sie bestaetigt:
# die restlichen Runden bringen nichts und koennen sie nur wieder umschreiben.
cat > "$TMP/review_conv.conf" <<CFG
listen 127.0.0.1:$RCVJ
queue_timeout 10
models allow "vm-conv" "CONVERGE"
endpoint conv { base http://127.0.0.1:$RCV/v1; context 8000; timeout 10 }
req for "vm-conv" review 3 pass conv as "conv-model"
CFG
./junction --config "$TMP/review_conv.conf" > "$TMP/review_conv.log" 2>&1 &
PIDS+=($!)
wait_port "$RCVJ" 10 || bad "the convergence instance did not come up"
CV=$(curl -s -D "$TMP/conv.h" -m 30 -X POST -H 'Content-Type: application/json' \
-d '{"model":"vm-conv","messages":[{"role":"user","content":"say it"}]}' \
"http://127.0.0.1:$RCVJ/v1/chat/completions")
contains "$CV" 'same answer' "the stable answer is delivered"
[ "$(tr -d '\n' < "$TMP/review_converge.state")" = "3" ] \
&& ok "a repeated answer stops after two review rounds" \
|| bad "convergence made '$(tr -d '\n' < "$TMP/review_converge.state")' calls instead of 3"
contains "$(cat "$TMP/review_conv.log")" "converged=1" "the convergence is logged"
contains "$(tr -d '\r' < "$TMP/conv.h")" "X-Junction-Review-Rounds: 2" \
"the response names the two completed rounds"
# Konfigurationsfehler müssen laut sein.
printf 'endpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq for "vm" review 13 pass e\n' \
> "$TMP/review_bad_count.conf"
OUT=$(./junction --config "$TMP/review_bad_count.conf" 2>&1)
contains "$OUT" "bad arguments" "review 13 is rejected"
printf 'endpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq for "vm" review 12 pass e\n' \
> "$TMP/review_max.conf"
OUT=$(./junction --config "$TMP/review_max.conf" --check 2>&1)
[ $? -eq 0 ] && ok "review 12 is accepted" || bad "review 12 was rejected: $OUT"
printf 'endpoint e {\n base http://127.0.0.1:9995/v1\n}\nreq for "vm" review 2 block\n' \
> "$TMP/review_block.conf"
OUT=$(./junction --config "$TMP/review_block.conf" 2>&1)
contains "$OUT" "review on a block rule" "review on a block rule is rejected"