From 9bbca6365b801e153de9c9d2b870258eb8450d38 Mon Sep 17 00:00:00 2001 From: Hermes Date: Wed, 18 Nov 2026 08:49:32 +0000 Subject: [PATCH] =?UTF-8?q?docs:=20Judge/Review-Grenzen=20und=20Header=20a?= =?UTF-8?q?n=20einer=20Stelle=20b=C3=BCndeln?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit README hat jetzt einen Abschnitt "Judge/review limits and headers" mit allem, was sonst über judge.c, review.c und review_serve verstreut war: - Judge: max. 4 Schritte, aufsteigende kumulative Zählung, Reset nach dem letzten Schritt, stabile Regel-Identität, 4096 Slots im RAM mit LRU und 1 h Idle-Ablauf, Neustart-Reset, Fallback ohne Cache-Marker. - Review: 0–4 Runden, N+1 Calls, 1024-Token-Floor nur für Review-Calls, Pufferung mit stream:false und ohne stream_options, gleicher Endpoint und gleiches Modell, nur /v1/chat/completions. - Skip-Tabelle mit n>1, tool_calls, content_array, no_text, budget, error, body, protocol und wann der Grund im Header, im SSE-junction- Objekt oder nur im Log steht. - 4xx-Verhalten: Originalfehler geht mit Status und Rumpf an den Client, Review-Runden behalten die letzte gute Antwort. - Usage/Kosten über alle Runden (junction-Objekt, SSE-usage-Chunk, X-Junction-Review-Tokens-In/-Out/-Cost-Usd) und die Kosten eines Disconnects bei Non-Streaming. - Header-Tabelle für X-Junction-Judge-* und X-Junction-Review-* plus Sanitisierung und 96-Header-Grenze. config.example verweist an der judge/review-Stelle auf dieselben Grenzen. Tests: unverändert 484 OK / 0 FAIL (Doku-Änderung). --- README.md | 116 ++++++++++++++++++++++++++++++++++++++++++++++++- config.example | 11 +++++ 2 files changed, 125 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4e14b5f..e297340 100644 --- a/README.md +++ b/README.md @@ -61,9 +61,11 @@ serves it — and what to do when none of them wants to. A sampled judge adds `X-Junction-Judge`, `X-Junction-Judge-Step`, `X-Junction-Judge-Every`, `X-Junction-Judge-Model`, `X-Junction-Judge-Served`. A self-review adds `X-Junction-Review`, `X-Junction-Review-Rounds`, - `X-Junction-Review-Model`, `X-Junction-Review-Skipped`. A streaming response + `X-Junction-Review-Model`, `X-Junction-Review-Skipped`; when a review ran, a + non-streaming answer also carries `X-Junction-Review-Tokens-In/-Out` and + `X-Junction-Review-Cost-Usd` summed over all its calls. A streaming response carries the planned judge/review in the head; a reviewed stream puts the actual - outcome in a `junction` object in its final chunk. + outcome and summed usage in a `junction` object in its final chunk. A worked, annotated configuration for a mixed local/cloud setup is in `examples/olivia.conf`; `config.example` is the same grammar, reduced to the bones. @@ -249,6 +251,116 @@ the same endpoint and hold the same slot, so a local box sees its own prompt cac and the review instruction are new. DeepSeek/OpenAI-compatible providers reuse the prefix automatically; Anthropic endpoints need their explicit marker (see `cache`). +### Judge/review limits and headers — one place + +The clauses are safe defaults, but they have hard edges. This is the complete +list; the code lives in `src/judge.c`, `src/review.c` and `review_serve` in +`src/junction.c`. + +**Judge** + +- At most **4** judge steps per rule; a fifth is a config error. Counts are + ascending and cumulative inside the chain. `every 5` then `every 10` means step + 0 at the 5th call and step 1 at the 10th; after step 1 the counter starts at 0 + again (15th/20th next). +- The counter is per conversation **and per rule identity**. The identity is the + rule's model and path globs, not its line: inserting a comment or another rule + above it keeps the counters. A conversation is the session header when present, + otherwise the first system plus first user message (see above). +- The table is in RAM: 4096 conversations, least-recently-used eviction, one hour + of idle expiry, reset on restart, and reset after the last chain step. These + resets change *when* a judge runs, never whether an answer is correct. +- A judge failure (429, 5xx, offline, timeout) offers the same request to the + rule's normal targets. The fallback body is the original, without the judge's + `cache_control` marker. If the marker cannot be added, the judge call goes out + without it rather than failing the turn. +- A judge turn is never answered from junction's response cache. + +**Review** + +- `review N` accepts **0–4**; `review 5` is rejected at config load and by + `rules.set`. 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 + sequentially. Each review request carries at least **1024** `max_tokens` (or + `max_completion_tokens`), even when the client asked for less; the first worker + call keeps exactly the client's budget. The review path buffers, so **every** + upstream call under a `review` rule (the worker call too) is sent with + `stream:false` and without `stream_options`; the client still gets SSE frames + built from the final answer if it asked for a stream. +- The review prompt is appended to the same conversation after the worker or + previous answer. Only the final answer is written to the client; intermediate + answers and the prompt never leak. +- 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`. + +**What skips the review (the answer is passed through)** + +| Reason (`X-Junction-Review-Skipped`) | When | +|---|---| +| `n>1` | the client asked for more than one choice; several answers cannot be reviewed as one | +| `tool_calls` | the answer carries a non-empty `tool_calls` array (`null`/`[]` is text) | +| `content_array` | the answer's `content` is an array of parts | +| `no_text` | the answer has no string content | +| `budget` | the vkey's monthly budget is spent before the next round | +| `error` | a review round failed; the last good answer is delivered | +| `body` | the review body could not be built; the original request is forwarded without review | +| `protocol` | review is not supported for this path or endpoint kind | + +The original call's own provider error is **not** hidden: a 4xx/5xx from the +first call reaches the client with its status and body (JSON, or an SSE error +event when the stream head is already open). A failed *review* round is +different: it is an internal extra call, so the last good answer is delivered +instead. `X-Junction-Review-Rounds` says how many rounds actually completed. + +`n>1` and `protocol` are decided before any review call. A non-streaming client +sees the reason in `X-Junction-Review-Skipped`; a streaming client's body is +forwarded untouched (no `junction` chunk), so that case is visible in the log +(`review_skipped ... reason=...`) and in the non-streaming header. The other +reasons are produced by `review_serve` and also appear as `review_skipped` in +the final SSE `junction` object; if the review body cannot be built (`body`), the +request falls back to an unreviewed forward and only the header/log names it. + +**Usage, cost and disconnects** + +- The top-level `usage` is the last provider call's, as the provider reported it. + The `junction` object in a non-streaming answer and in the final SSE chunk adds + `usage` (prompt/completion/cached/total) and `cost_usd` summed over the worker + and all completed review rounds; an unknown price is reported as + `cost_unknown` instead of zero. Non-streaming answers also carry + `X-Junction-Review-Tokens-In`, `-Out` and `-Cost-Usd`; `X-Junction-Ms` covers + the whole request. +- Streaming clients get a final `choices:[]` chunk with the last call's `usage` + before `[DONE]`, and only when the provider sent usage. The summed numbers are + in the `junction` object of the preceding chunk. +- A non-streaming client that disconnects while review rounds are running cannot + cancel the upstream calls that already started; they remain billed and appear + in `/spend`. Streaming clients see `: junction:` comments while rounds run; the + next failed comment write stops the wait. + +**Headers** + +| Header | Meaning | +|---|---| +| `X-Junction-Judge` | judge endpoint or pool name from the clause; absent when this turn is not a judge turn | +| `X-Junction-Judge-Step` | zero-based index of the step in the chain (0, 1, …) | +| `X-Junction-Judge-Every` | the N of this step | +| `X-Junction-Judge-Model` | the step's `as` model; `pool` when the pool members carry their own | +| `X-Junction-Judge-Served` | `1` only if the judge slot delivered the 2xx; `0` when the worker fallback served the turn | +| `X-Junction-Review` | the configured N | +| `X-Junction-Review-Rounds` | review rounds that actually completed | +| `X-Junction-Review-Model` | the model the worker/review calls were sent as | +| `X-Junction-Review-Skipped` | the reason from the table above; absent when no skip happened | +| `X-Junction-Review-Tokens-In/-Out` | summed prompt/completion tokens over the worker and completed rounds (non-streaming, only when a review ran) | +| `X-Junction-Review-Cost-Usd` | summed known cost over those calls (non-streaming, only when a review ran and the price is known) | + +Header values are sanitized: CR/LF and other control bytes become spaces, an +invalid header name is dropped, and an array full at 96 headers drops the rest. +Drops and sanitized values are logged (`headers_dropped`, +`header_control_chars`, `header_name_rejected`), so a missing header is never +silent. + ### Model policy — what `/v1/models` shows and serves ``` diff --git a/config.example b/config.example index 121a1ba..2356d89 100644 --- a/config.example +++ b/config.example @@ -232,6 +232,17 @@ req path "/v1/models" check none pass peer1, peer2, opencode-go # The worker's first answer plus a review instruction go back to the same model # N times; only the last answer reaches the client. Tool-call answers pass # 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 (review 5 is a config +# error). Review calls carry at least 1024 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 +# per conversation and per rule (model/path glob), live in RAM (4096 slots, LRU +# eviction, 1 h idle expiry, reset on restart) and reset after the last chain +# step. A failing judge falls back to the worker without its cache marker; a +# first-call provider 4xx reaches the client with its own status and body. # Anything unseen is refused rather than silently sent to the opencode-go. req for "*" block