review: Monatsbudget vor jeder Review-Runde prüfen

Der vkey wurde nur beim Eintreffen der Anfrage gegen sein Monatsbudget
geprüft. review_serve machte danach bis zu review_count weitere
Provider-Calls, ein hartes Budget konnte so um Faktor 5 überschritten
werden.

vkey_budget_open liest den Verbrauch unter STATE_LOCK und wird vor jeder
Review-Runde geprüft. Ist das Budget erschöpft, endet die Schleife, die
letzte gute Antwort (bzw. die Originalantwort) geht an den Client und
X-Junction-Review-Skipped nennt "budget".

Test in spend-vkey.sh: eigener vkey mit budget 0.0005 und review 2, ein
Call kostet 0.00072. Erwartet und geprüft: Originalantwort wird
ausgeliefert, Rounds: 0, genau eine Zeile im Spend-Log, Log nennt
skipped=budget.

Tests: 440 OK, 0 FAIL.
This commit is contained in:
Hermes committed 2026-11-18 07:29:23 +00:00
1 parent bcb70e2a66
commit 6d3547e794
3 files changed
+65 -1

No files matched your search

+26
View File
@@ -3360,6 +3360,26 @@ static int forward_with_retry(j_conn *client, j_endpoint *ep, const j_req *rq,
return rc;
}
/* Is the monthly budget of this vkey still open? handle_client checks it once when
* the request arrives, but every review round costs another provider call. A hard
* budget must stop the rounds, not be overdrawn by up to review_count calls. An
* unknown key or a key without a budget is open. */
static int vkey_budget_open(const char *name)
{
if (!name || !name[0]) return 1;
int open = 1;
STATE_LOCK();
for (int i = 0; i < CFG.vkey_count; i++) {
if (strcmp(CFG.vkeys[i].name, name)) continue;
if (CFG.vkeys[i].budget_usd > 0 &&
CFG.vkeys[i].spent_usd >= CFG.vkeys[i].budget_usd)
open = 0;
break;
}
STATE_UNLOCK();
return open;
}
/* Hidden self-review: the first answer is buffered, then review_count calls append
* that answer and the review instruction to the same conversation. Only the last
* answer is written to the client. A failed review is not fatal; the last good
@@ -3419,6 +3439,12 @@ static int review_serve(j_conn *client, j_endpoint *ep, const j_req *rq,
}
for (int round = 1; round <= review_count && current && !tools; round++) {
if (!vkey_budget_open(vkey_name)) {
snprintf(t_review_skipped, sizeof t_review_skipped, "budget");
jlog("review", "round=%d endpoint=%s skipped=budget vkey=%s",
round, ep->name, vkey_name);
break;
}
char *rb = NULL;
size_t rb_len = 0;
if (j_review_body(ns, current, &rb, &rb_len) != 0) {
+1 -1
View File
@@ -71,7 +71,7 @@ log_has() {
# answer requests with somebody else's config), so fail loudly and early.
# 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem
# Test benutzt, also darf er die Suite nicht aufhalten.
for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8419 8420 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 9443 9990 9991 9992 9993 9994 9995 9996; do
for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8415 8416 8417 8418 8419 8420 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8208 9443 9990 9991 9992 9993 9994 9995 9996; do
if ss -tln 2>/dev/null | grep -q ":$port "; then
echo "port $port is busy — a leftover junction/stub is still running:"
ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /'
+38
View File
@@ -142,6 +142,44 @@ else
fi
contains "$LM" 'junction_vkey_requests_total{vkey="hermes",outcome="ok"} 1' "/metrics counts the key's requests"
# --- 3b. Review-Runden halten das Monatsbudget ein ------------------------------------
# Der vkey prüft sein Budget beim Eintreffen der Anfrage. Danach kostet jede
# Review-Runde einen weiteren Provider-Call; ein hartes Budget darf nicht um
# bis zu review_count Calls überschritten werden.
P_RV=$((PORT+73))
cat > "$TMP/review-budget.conf" <<CFG
listen 127.0.0.1:$P_RV
prices $TMP/ledger-prices.conf
spend_log $TMP/review-budget.jsonl
vkey "rv" token_env VKEY_RV allow "*" budget 0.0005
endpoint used {
base http://127.0.0.1:$P_USE/v1
context 64000
timeout 20
idle_timeout 5
}
req for "*" review 2 check none pass used
CFG
VKEY_RV=secret-rv ./junction --config "$TMP/review-budget.conf" > "$TMP/review-budget.log" 2>&1 &
PIDS+=($!)
wait_port $P_RV 10
RV=$(curl -s -m 20 -D "$TMP/review-budget.h" -X POST -H 'Content-Type: application/json' \
-H 'Authorization: Bearer secret-rv' \
-d '{"model":"test/preis","messages":[{"role":"user","content":"hi"}],"max_tokens":50}' \
"http://127.0.0.1:$P_RV/v1/chat/completions")
contains "$RV" '"ok"' "the original answer is delivered when the review budget is spent"
tr -d '\r' < "$TMP/review-budget.h" | grep -qi '^X-Junction-Review-Skipped: budget' \
&& ok "the skipped review names the budget in its header" \
|| bad "the skipped review names the budget in its header"
tr -d '\r' < "$TMP/review-budget.h" | grep -qi '^X-Junction-Review-Rounds: 0' \
&& ok "no review round ran after the budget was spent" \
|| bad "no review round ran after the budget was spent"
[ "$(wc -l < "$TMP/review-budget.jsonl")" = "1" ] \
&& ok "the budget stopped the run after the one paid request" \
|| bad "the budget stopped the run after the one paid request ($(wc -l < "$TMP/review-budget.jsonl") calls)"
log_has "$TMP/review-budget.log" "review round=1 endpoint=used skipped=budget" \
"the skipped review round is logged with its reason"
# --- 4. Die Monatsauskunft --------------------------------------------------------------
# Dieselbe Datei, ausgewertet: was hat dieser Monat gekostet, je Schlüssel und Modell.
SP=$(curl -s -m 5 "http://127.0.0.1:$P_LED/spend")