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.
This commit is contained in:
Hermes committed 2026-11-19 12:04:17 +00:00
1 parent aaa289ab01
commit 299fd600ac
5 files changed
+67 -10

No files matched your search

+6 -2
View File
@@ -315,8 +315,8 @@ list; the code lives in `src/judge.c`, `src/review.c` and `review_serve` in
**Review**
- `review N` in the config accepts **0–4**; `review 5` is rejected at config
load and by `rules.set`. A per-request header can raise the depth to **12**
- `review N` in the config accepts **0–12**; `review 13` is rejected at config
load and by `rules.set`. A per-request header can raise or lower the depth
(see "Per request" above). N rounds mean **N+1** provider calls (the worker
answer plus N reviews), all billed and counted in `/spend` and the vkey budget.
- Review calls use the same endpoint and model as the worker and run
@@ -330,6 +330,10 @@ list; the code lives in `src/judge.c`, `src/review.c` and `review_serve` in
previous answer. It carries the user's request and the answer so far and says
to keep the same question, scope and language; only the final answer is
written to the client, intermediate answers and the prompt never leak.
- If a review round returns the answer **verbatim**, the answer is stable: the
round is counted, the remaining rounds are skipped, and the log says
`converged=1`. A reviewer that would otherwise keep rewriting a correct answer
does not get the chance.
- Review only applies to `POST /v1/chat/completions` on OpenAI-compatible
endpoints. `kind anthropic`/`kind opencode`, other paths and non-chat requests
are forwarded unreviewed with `X-Junction-Review-Skipped: protocol`.
+5 -4
View File
@@ -238,10 +238,11 @@ req path "/v1/models" check none pass peer1, peer2, opencode-go
# through unreviewed. "review 2" means three provider calls.
#
# Limits and failure behavior (full table in README, "Judge/review limits and
# headers"): at most 4 judge steps and 4 review rounds in the config (review 5 is
# a config error). A request may raise the review depth to 12 for itself with
# X-Junction-Review-Rounds: N, or X-Junction-Effort: off|low|medium|high|max
# (0/4/7/9/12). Review calls carry at least 4096 max_tokens; the first worker call
# headers"): at most 4 judge steps and 12 review rounds in the config (review 13
# is a config error). A request may set its own depth with X-Junction-Review-Rounds:
# N, or X-Junction-Effort: off|low|medium|high|max (0/4/7/9/12). A round that
# repeats the answer verbatim stops the loop (converged=1). Review calls carry at
# least 4096 max_tokens; the first worker call
# keeps the client's budget. n>1, content arrays, non-empty tool_calls and a
# spent vkey budget skip the remaining rounds and deliver the original or last
# good answer; X-Junction-Review-Skipped names the reason. Judge counters are
+1 -1
View File
@@ -55,7 +55,7 @@
#define J_MAX_MODEL_PATTERNS 32
#define J_MAX_POOLS 16
#define J_MAX_JUDGES 4
#define J_MAX_REVIEWS 4
#define J_MAX_REVIEWS 12
#define J_MAX_MCP 16
#define J_MAX_CACHE_PEERS 8
/* Endliche Latenzgrenzen in Sekunden; dazu kommt der +Inf-Eimer. */
+18
View File
@@ -3766,6 +3766,24 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq,
free(next);
break;
}
/*
* The reviewer repeated the answer verbatim: it is stable, and more
* rounds would only give the model another chance to rewrite it. Count
* the confirming round, keep its JSON as the last good answer, stop.
*/
if (strcmp(next, current) == 0) {
char *same = strdup(cap.data ? cap.data : "");
if (same != NULL) {
free(last_json);
last_json = same;
}
free(next);
t_review_done++;
jlog("review", "round=%d endpoint=%s converged=1 chars=%zu",
round, ep->name, strlen(current));
break;
}
free(current);
current = next;
char *fresh = strdup(cap.data ? cap.data : "");
+37 -3
View File
@@ -24,8 +24,10 @@ 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" \
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 \
@@ -43,6 +45,9 @@ 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+=($!)
@@ -396,6 +401,7 @@ contains "$UH" "X-Junction-Cost-Usd: 0.005400" "the general cost header carries
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")
@@ -511,11 +517,39 @@ NTCALLS=$(tr -d '\n' < "$TMP/review_null_tools.state")
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 5 pass e\n' \
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 5 is rejected"
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"