Skip to content

Official Codebase of 3DCodeVerse

Python

health: a preflight probe must look like the work it guards — the 5-token version gave a false green light

gaoypeng7c18d738b210
diff --git a/harness/codeverse/models/health.py b/harness/codeverse/models/health.py
index ae9fc847..6a602bd9 100644
--- a/harness/codeverse/models/health.py
+++ b/harness/codeverse/models/health.py
@@ -6,9 +6,15 @@ into it and each spent 56-87 minutes losing to 503s before recording a failure,
the judge model went down with it. The provider was unreachable within seconds of
the first call — the cost was entirely in not asking.
-``probe`` asks once per key, cheaply and in parallel, with a short timeout and no
-retries: the point is a fast verdict, not a successful call. Callers use it as a
-preflight gate before spending real money.
+``probe`` asks once per key, in parallel, with a short timeout and no retries: the
+point is a fast verdict, not a successful call. Callers use it as a preflight gate
+before spending real money.
+
+The probe prompt is deliberately **the size of real work**, not five tokens. Measured
+2026-08-24: during the flash degradation a trivial prompt answered while the 12-24 k
+token planner calls beside it were still 503-ing, so a tiny probe passed the preflight
+and the battery then lost two cells to the wall at 15 min each. A probe that does not
+resemble the workload does not measure the workload.
"""
from __future__ import annotations
@@ -20,11 +26,25 @@ from dataclasses import dataclass
log = logging.getLogger(__name__)
#: enough of the pool to tell an outage from one unlucky key, without a long probe
-DEFAULT_SAMPLE = 4
-#: a healthy model answers a 5-token prompt well inside this
-DEFAULT_TIMEOUT_S = 20.0
-#: below this share of answering keys, treat the model as down
-DEFAULT_MIN_OK = 0.5
+DEFAULT_SAMPLE = 6
+#: a healthy model answers a workload-sized prompt well inside this
+DEFAULT_TIMEOUT_S = 30.0
+#: prompt size the probe sends, in tokens. Sized like a real planner/generator call
+#: (docs/COST.md §20 measured the corpus at ~12 k input): a 5-token probe answers when
+#: a 12 k one does not, which is exactly the false green light this gate must not give.
+#: 4 probes x 8 k tokens is ~$0.003 on flash — irrelevant next to one wasted battery.
+PROBE_TOKENS = 8000
+#: below this share of answering calls, treat the model as down.
+#:
+#: NOT 0.5. A preflight guards a BATTERY, and one cell is dozens of model calls that
+#: must all land: at p per call a 20-call cell completes with p**20, so p = 0.5 is not
+#: "half healthy", it is zero. Retries soften it — roughly p >= 0.7 for a coin-flip
+#: chance at a cell — so the bar sits above that with margin. Measured 2026-08-24: the
+#: gate passed flash at 2/4 and the battery then lost both its first cells to the retry
+#: deadline at ~15 min each. The asymmetry decides the tie: refusing wrongly costs one
+#: 2-minute re-probe, starting wrongly costs hours. A judgement call, not a measured
+#: optimum.
+DEFAULT_MIN_OK = 0.75
@dataclass(frozen=True)
@@ -48,11 +68,13 @@ def probe(model: str = "gemini:gemini-3.7-flash", *, sample: int = DEFAULT_SAMPL
"""One tiny call per sampled key, in parallel, no retries. Never raises."""
from codeverse.contracts.chat import ChatMessage, ChatRequest
+ prompt = _probe_prompt()
+
def one(_: int) -> str | None:
try:
# built directly, not via get_chat_model: a probe must NOT retry or back off
m = _bare_model(model, timeout_s)
- m.generate(ChatRequest(messages=[ChatMessage.user("Reply with the single word: pong")],
+ m.generate(ChatRequest(messages=[ChatMessage.user(prompt)],
temperature=0.0, max_output_tokens=256, thinking="off", label="health"))
return None
except Exception as e: # noqa: BLE001 — a probe reports, it does not propagate
@@ -65,6 +87,17 @@ def probe(model: str = "gemini:gemini-3.7-flash", *, sample: int = DEFAULT_SAMPL
return Health(model=model, n_ok=sum(r is None for r in out), n_tried=n, reasons=reasons)
+def _probe_prompt(tokens: int = PROBE_TOKENS) -> str:
+ """A workload-sized prompt whose answer is still one word.
+
+ Filler carries the token weight; the instruction at the end keeps the RESPONSE
+ tiny, so the probe costs input tokens (cheap, and the thing under test) rather
+ than output tokens.
+ """
+ filler = "The quick brown fox jumps over the lazy dog. " * max(1, tokens // 9)
+ return f"{filler}\n\nIgnore the text above. Reply with the single word: pong"
+
+
def _bare_model(model_id: str, timeout_s: float):
"""The model with retries switched off — a probe that retries measures nothing."""
provider, _, name = model_id.partition(":")
diff --git a/harness/docs/COST.md b/harness/docs/COST.md
index c390b392..9025fbae 100644
--- a/harness/docs/COST.md
+++ b/harness/docs/COST.md
@@ -1088,3 +1088,37 @@ The real fix is a machine-wide limiter — a file-locked token bucket under
`~/.cache/codeverse/` that every process shares — so the quota is enforced where it
actually lives. Not built here: it needs crash/staleness handling and its own A/B, and
it should be measured against the operational rule above rather than assumed better.
+
+
+## 24. A preflight probe must look like the work it is guarding
+
+The §23 preflight passed `gemini-3.7-flash` and the battery immediately lost its first
+two cells — `infra_failed`, 15.1 and 15.3 min, $0.00 generated. The gate was wrong in
+two independent ways, both now fixed, both worth stating because they generalise to any
+health check in front of a batch job.
+
+**1. The probe was five tokens; the work is twelve thousand.** During this degradation
+a trivial prompt answered while the planner calls beside it were still 503-ing — the
+fidelity wave observed the same thing independently. Re-measured at workload size
+(~10 k tokens), the same service that looked fine on tiny prompts is not:
+
+| model | trivial prompt | ~10 k-token prompt |
+|---|---|---|
+| `gemini-3.7-flash` | 4 / 4 | **4 / 6** |
+| `gemini-3.1-pro-preview` | 4 / 4 | **4 / 6** |
+
+`probe` now sends `PROBE_TOKENS = 8000` of filler and still asks for a one-word answer,
+so it costs input tokens — the thing under test — and about $0.003 per preflight.
+
+**2. The bar was 50 %, which for a pipeline means zero.** A preflight guards a
+BATTERY, and one cell is dozens of model calls that must all land: at per-call success
+p a 20-call cell completes with p²⁰, so p = 0.5 is not "half healthy". Retries soften
+it to roughly p ≥ 0.7 for a coin-flip chance at a cell, so `DEFAULT_MIN_OK` is now
+**0.75** over `DEFAULT_SAMPLE = 6`. The asymmetry settles the tie: refusing wrongly
+costs one 2-minute re-probe, starting wrongly costs hours. This one is a judgement
+call, not a measured optimum, and it is written here so it can be revisited with data.
+
+What did work, in production, on those two lost cells: they were recorded
+`infra_failed` with `score=None` rather than a hard 0.0 (§7 of `docs/EVAL.md`), and the
+retry deadline (§22) stopped each at ~15 min instead of the 56-87 min the same cells
+burned earlier the same morning.
diff --git a/harness/tests/models/test_health.py b/harness/tests/models/test_health.py
index fec3daf2..bc994c3a 100644
--- a/harness/tests/models/test_health.py
+++ b/harness/tests/models/test_health.py
@@ -5,16 +5,33 @@ from __future__ import annotations
from codeverse.models.health import DEFAULT_MIN_OK, Health
-def test_a_model_answering_on_most_keys_is_healthy():
- assert Health("m", n_ok=3, n_tried=4).ok
- assert Health("m", n_ok=2, n_tried=4).ok, f"the bar is {DEFAULT_MIN_OK:.0%} of sampled keys"
-
-
-def test_a_model_answering_on_almost_nothing_is_not():
+def test_a_model_answering_almost_every_call_is_healthy():
+ assert Health("m", n_ok=6, n_tried=6).ok
+ assert Health("m", n_ok=3, n_tried=4).ok, f"the bar is {DEFAULT_MIN_OK:.0%} of sampled calls"
+
+
+def test_a_half_working_model_is_NOT_healthy():
+ """The bar is not 50%. A cell is dozens of calls that must all land, so p=0.5 per
+ call is not "half healthy" — it is a cell that never completes. Measured
+ 2026-08-24: the gate passed flash at 2/4 and the battery lost both its first cells
+ to the retry deadline at ~15 min each."""
+ assert not Health("m", n_ok=2, n_tried=4).ok
+ assert not Health("m", n_ok=4, n_tried=6).ok
assert not Health("m", n_ok=1, n_tried=4).ok
assert not Health("m", n_ok=0, n_tried=4).ok
+def test_the_probe_prompt_is_the_size_of_real_work():
+ """A five-token probe answers when a 12 k-token planner call does not — that false
+ green light is exactly what this gate must not give."""
+ from codeverse.models.health import PROBE_TOKENS, _probe_prompt
+
+ prompt = _probe_prompt()
+ approx_tokens = len(prompt) // 4
+ assert approx_tokens > 0.5 * PROBE_TOKENS, f"probe is only ~{approx_tokens} tokens"
+ assert "pong" in prompt, "the answer must still be one word, so the probe costs input not output"
+
+
def test_no_keys_probed_is_not_reported_as_healthy():
"""An empty probe must never read as a green light."""
assert not Health("m", n_ok=0, n_tried=0).ok