if [ "$UB" != "skip" ]; then
  export ${PFX}_UBATCH="$UB"
  # a batch below the ubatch is silently clamped; keep batch >= ubatch
  export ${PFX}_BATCH="$(( UB < 4096 ? 4096 : UB ))"
fi
PROBE="$PROBE" BASE="http://$HOSTBASE:$PORT" python3 - <<'PY' 2>&1 | tee -a "$RES"
import os, sys, json, random, urllib.request
target = int(os.environ["PROBE"]); BASE = os.environ["BASE"]
rng = random.Random(20260920); CODE = "AMBER-3172-WILLOW"
T = ["drainage of the upper meadow", "insulation of the pump house", "seasoning of the oak planks"]
def line(i):
    return (f"ENTRY {i:05d}. the {rng.choice(T)} was recorded by the reeve; reserve "
            f"{rng.randint(3,400)} units against {rng.randint(3,400)}.")
def body(n):
    ls = [line(i) for i in range(1, n + 1)]
    ls[n // 2] = f"ENTRY {n//2:05d}. SEALED REFERENCE for this ledger is {CODE}. Quote it in full."
    return "\n".join(ls)          # varied prose: repeated characters tokenize pathologically
def ntok(t):
    r = urllib.request.Request(BASE + "/tokenize", data=json.dumps({"content": t}).encode(),
                               headers={"Content-Type": "application/json"})
    return len(json.load(urllib.request.urlopen(r, timeout=900))["tokens"])
per = ntok(body(200)) / 200
n = max(50, int(target / per))
for _ in range(3):
    t = ntok(body(n))
    if abs(t - target) / target < 0.02:
        break
    n = max(50, int(n * target / t))
q = body(n) + "\n\nQUESTION: quote the sealed reference exactly. Answer with the code only."
# max_tokens 900, NOT 64: several of these models think by default and a small budget is spent
# entirely on reasoning, returning an EMPTY answer that looks exactly like a corruption bug.
req = {"messages": [{"role": "user", "content": q}], "max_tokens": 900,
       "temperature": 0.0, "stream": False}
r = urllib.request.Request(BASE + "/v1/chat/completions", data=json.dumps(req).encode(),
                           headers={"Content-Type": "application/json"})
try:
    d = json.load(urllib.request.urlopen(r, timeout=7200))
except Exception as e:
    print(json.dumps({"error": f"{type(e).__name__}: {e}",
                      "note": "a dropped connection here usually means the server died mid-read "
                              ": the compute buffer did not fit. Try a smaller ubatch."}))
    sys.exit(0)
m = d["choices"][0]["message"]
c = (m.get("content") or "").strip()
rsn = (m.get("reasoning_content") or "").strip()
ti = d.get("timings", {})
print(json.dumps({"prompt_n": ti.get("prompt_n"),
                  "prefill_tps": round(ti.get("prompt_per_second", 0) or 0, 1),
                  "decode_tps": round(ti.get("predicted_per_second", 0) or 0, 2),
                  "needle_found": CODE in (c + " " + rsn),
                  # 2026-09-21: the code in the ANSWER itself, and what the answer actually says :
                  # a corrupted reply can still quote the code inside its reasoning.
                  "needle_in_answer": CODE in c, "answer_head": c[:60],
                  "answer_len": len(c), "reasoning_len": len(rsn)}))
PY