Files
junction/tests/virtual-models.sh
Hermes 42c4f4baef models: virtuellen Modellnamen nur einmal in /v1/models listen
Zwei Regeln mit demselben exakten Modellnamen (z.B. eine virtuelle
Judge-Regel und eine explizite ctx-Überlauf-Regel) erzeugten bisher zwei
Einträge: json_has_model_id prüfte nur die Upstream-Liste, nicht die
bereits angehängten Einträge. Jetzt wird auch der extra-Puffer geprüft.

Test: vm-chain hat eine zweite virtuelle Regel (ctx >100000, eigenes
"as"); /v1/models enthält den Namen genau einmal.

Tests: 519 OK, 0 FAIL.
2026-11-18 13:40:26 +00:00

147 lines
6.9 KiB
Bash

#!/bin/bash
# Wird von tests/run.sh eingebunden: /v1/models für virtuelle Ketten.
#
# Eine virtuelle Regel (Pool, Alias, Judge, Review) verspricht die kleinste
# ctx-Größe ihrer ganzen Kette. Ein Request darüber darf nicht still beim Judge
# oder über ctx_fallback landen, sondern muss über eine eigene ctx-Regel gehen.
echo
echo "== 44. Virtuelle Modelle: Ketten-Grenzen und ctx-Regeln =="
VW=$((PORT+244)) # Worker-Stub, ctx 4000 / out 1000
VJ=$((PORT+245)) # Judge-Stub, ctx 2000 / out 500
VB=$((PORT+246)) # Big-Stub, ctx 100000 / out 8000
VF=$((PORT+247)) # Fallback-Stub, ctx 200000 / out 8000
V1=$((PORT+248)) # Junction mit expliziter ctx-Regel
V2=$((PORT+249)) # Junction mit globalem ctx_fallback
STUB_STATE="$TMP/vm_worker.state" STUB_ANSWERS="worker answer" \
STUB_MODELS="wm" STUB_CTX=4000 STUB_OUT=1000 tests/stub $VW "WORKER" \
> "$TMP/vm_worker_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/vm_judge.state" STUB_ANSWERS="judge answer" \
STUB_MODELS="jm" STUB_CTX=2000 STUB_OUT=500 tests/stub $VJ "JUDGE" \
> "$TMP/vm_judge_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/vm_big.state" STUB_ANSWERS="big answer" \
STUB_MODELS="bm" STUB_CTX=100000 STUB_OUT=8000 tests/stub $VB "BIG" \
> "$TMP/vm_big_stub.log" 2>&1 &
PIDS+=($!)
STUB_STATE="$TMP/vm_fb.state" STUB_ANSWERS="fallback answer" \
STUB_MODELS="fm" STUB_CTX=200000 STUB_OUT=8000 tests/stub $VF "FALLBACK" \
> "$TMP/vm_fb_stub.log" 2>&1 &
PIDS+=($!)
wait_port "$VW" 30 || bad "the worker stub did not come up"
wait_port "$VJ" 30 || bad "the judge stub did not come up"
wait_port "$VB" 30 || bad "the big stub did not come up"
wait_port "$VF" 30 || bad "the fallback stub did not come up"
# Die virtuelle Regel steht vor der ctx-Regel: first match wins. Erst das
# Ketten-Minimum (2000) macht die große Anfrage zu einer Sache der ctx-Regel.
cat > "$TMP/vm1.conf" <<CFG
listen 127.0.0.1:$V1
log_prefix vm1
ctx_chars_per_token 1
probe_interval 0
retry_num 0
endpoint worker { base http://127.0.0.1:$VW/v1; context 4000; timeout 10; idle_timeout 5 }
endpoint judge { base http://127.0.0.1:$VJ/v1; context 2000; timeout 10; idle_timeout 5 }
endpoint big { base http://127.0.0.1:$VB/v1; context 100000; timeout 10; idle_timeout 5 }
req path "/v1/models" check none pass worker
req for "vm-chain" judge every 1 judge as "jm" cache off pass worker as "wm"
req for "vm-chain" ctx >2000 pass big
# Zweite virtuelle Regel mit demselben Namen: /v1/models darf den Eintrag nur
# einmal enthalten (ein Alias mit eigenem Ziel).
req for "vm-chain" ctx >100000 pass big as "bm"
req for "*" pass big
CFG
./junction --config "$TMP/vm1.conf" > "$TMP/vm1.log" 2>&1 &
PIDS+=($!)
wait_port "$V1" 30 || bad "the virtual-model instance did not come up"
# Die Modellisten werden im Hintergrund geholt. Erst wenn beide da sind, sind
# die output-Grenzen vollständig; sonst fiele output auf den Kontext zurück.
for i in $(seq 1 100); do
grep -q 'models: worker 1 Modelle' "$TMP/vm1.log" 2>/dev/null &&
grep -q 'models: judge 1 Modelle' "$TMP/vm1.log" 2>/dev/null && break
sleep 0.05
done
VM=$(curl -s -m 10 "http://127.0.0.1:$V1/v1/models")
VMCOUNT=$(printf '%s' "$VM" | grep -o '"id":"vm-chain"' | wc -l)
[ "$VMCOUNT" = "1" ] && ok "a duplicate rule name produces one /v1/models entry" \
|| bad "vm-chain is listed $VMCOUNT times"
if python3 - "$VM" <<'PY'
import json, sys
d = json.loads(sys.argv[1])
m = [x for x in d.get("data", []) if x.get("id") == "vm-chain"]
if not m:
raise SystemExit("vm-chain is not listed")
m = m[0]
if m.get("limit") != {"context": 2000, "output": 500}:
raise SystemExit("limit is %r" % m.get("limit"))
if m.get("name") != "vm-chain":
raise SystemExit("name is %r" % m.get("name"))
PY
then ok "the virtual model exposes name and the smallest limit of worker and judge"
else bad "the virtual model limit/name is wrong: $VM"
fi
SMALL=$(python3 -c "print('a' * 1000)")
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$SMALL\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$V1/v1/chat/completions")
contains "$R" "judge answer" "a request inside the chain minimum reaches the judge"
JC1=$(tr -d '\n' < "$TMP/vm_judge.state")
[ "$JC1" = "1" ] && ok "the judge was called once for the small request" \
|| bad "the judge call count is '$JC1' instead of 1"
BIGPROMPT=$(python3 -c "print('a' * 3000)")
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGPROMPT\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$V1/v1/chat/completions" -w '\nHTTP=%{http_code}')
contains "$R" "big answer" "ctx overflow goes to the explicit ctx rule"
contains "$R" "HTTP=200" "the explicit ctx rule serves the overflow"
JC2=$(tr -d '\n' < "$TMP/vm_judge.state")
[ "$JC2" = "1" ] && ok "the judge is not used for the ctx overflow" \
|| bad "the judge was called '$JC2' times after the overflow"
log_has "$TMP/vm1.log" "virtual_ctx_skip model=vm-chain" \
"the skipped virtual rule is logged"
log_has "$TMP/vm1.log" "limit=2000" "the skip log names the chain limit"
# Ohne eigene ctx-Regel darf der globale ctx_fallback nicht für das virtuelle
# Modell einspringen. Eine gewöhnliche Regel benutzt ihn weiterhin.
cat > "$TMP/vm2.conf" <<CFG
listen 127.0.0.1:$V2
log_prefix vm2
ctx_chars_per_token 1
probe_interval 0
retry_num 0
ctx_fallback fb
endpoint worker { base http://127.0.0.1:$VW/v1; context 4000; timeout 10; idle_timeout 5 }
endpoint judge { base http://127.0.0.1:$VJ/v1; context 2000; timeout 10; idle_timeout 5 }
endpoint fallback { base http://127.0.0.1:$VF/v1; context 200000; timeout 10; idle_timeout 5 }
pool fb fallback
req for "vm-chain" judge every 1 judge as "jm" cache off pass worker as "wm"
req for "plain" check none pass worker
CFG
./junction --config "$TMP/vm2.conf" > "$TMP/vm2.log" 2>&1 &
PIDS+=($!)
wait_port "$V2" 30 || bad "the ctx_fallback instance did not come up"
FB1=0
[ -f "$TMP/vm_fb.state" ] && FB1=$(tr -d '\n' < "$TMP/vm_fb.state")
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"vm-chain\",\"messages\":[{\"role\":\"user\",\"content\":\"$BIGPROMPT\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$V2/v1/chat/completions" -w '\nHTTP=%{http_code}')
contains "$R" "no_matching_rule" "a virtual ctx overflow without its own rule is refused"
FB2=0
[ -f "$TMP/vm_fb.state" ] && FB2=$(tr -d '\n' < "$TMP/vm_fb.state")
[ "$FB1" = "$FB2" ] && ok "ctx_fallback did not silently serve the virtual model" \
|| bad "ctx_fallback served the virtual model ($FB1 -> $FB2)"
PLAIN=$(python3 -c "print('b' * 5000)")
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
-d "{\"model\":\"plain\",\"messages\":[{\"role\":\"user\",\"content\":\"$PLAIN\"}],\"max_tokens\":8}" \
"http://127.0.0.1:$V2/v1/chat/completions")
contains "$R" "fallback answer" "a normal rule still uses ctx_fallback"