Official Codebase of 3DCodeVerse
Python
models: gemini drops raw['wasted_usage'] and raw['hedged'] — the ledger's extra rows are the one record of discarded paid round-trips (D84)
gaoypengaeffe1b11912
diff --git a/harness/codeverse3d/models/gemini.py b/harness/codeverse3d/models/gemini.pyindex d64f4880..35c6585b 100644--- a/harness/codeverse3d/models/gemini.py+++ b/harness/codeverse3d/models/gemini.py@@ -421,7 +421,6 @@ class GeminiModel:budget = retry_budget_s(request.max_wait_s)deadline = time.monotonic() + budget # mirrors rotate_with_retries' own deadlinestats: dict[str, Any] = {}- wasted: list[Usage] = [] # discarded round-trips the provider still billedresp = rotate_with_retries(self.pool,lambda key: self._once(@@ -437,16 +436,11 @@ class GeminiModel:retry_after=_retry_after_s,label=f"gemini {self.model}",stats=stats,- on_attempt=self._attempt_hook(wasted),+ on_attempt=self._attempt_hook(),**({} if self.storm_attempts is None else {"storm_attempts": self.storm_attempts}),)# how hard the call was, next to which key served it (``_once``): ledger fieldsresp.raw["attempts"] = int(stats.get("attempts", 0))- resp.raw["hedged"] = int(stats.get("hedged", 0))- if wasted:- # money the winner's usage does not include; a hedge loser that lands AFTER- # this returns reaches the ledger through the sink, never this field- resp.raw["wasted_usage"] = sum(wasted[1:], wasted[0])return respdef _config(self, request: ChatRequest) -> types.GenerateContentConfig:@@ -465,11 +459,10 @@ class GeminiModel:a 300 s socket — the dominant deadline overshoot (audit 2026-08-27)."""return clip_timeout(config, int(attempt_timeout_s(deadline, self.timeout_s) * 1000))- def _attempt_hook(self, wasted: list[Usage]) -> OnAttempt:- """Per-round-trip observer. It collects every DISCARDED round-trip that still- cost money into ``wasted`` (published on ``resp.raw["wasted_usage"]`` — the only- place a budget guard can see that money) and feeds the ledger's per-attempt sink- when the metering layer installed one (``cost.instrument.MeteredChatModel``).+ def _attempt_hook(self) -> OnAttempt:+ """Per-round-trip observer: feeds the ledger's per-attempt sink when the metering+ layer installed one (``cost.instrument.MeteredChatModel``) — a DISCARDED round-trip+ that still cost money is booked there as an ``extra`` row, the only record of it (D84).Captured once per logical call so a hedge loser landing later, in its own thread,still reports through it."""sink = attempt_sink()@@ -481,8 +474,6 @@ class GeminiModel:else:usage = getattr(t.err, "usage", None) or Usage()outcome, error = t.outcome, str(t.err)- if discarded and usage.cost_usd:- wasted.append(usage)if sink is not None:sink(AttemptRecord(attempt=no, key=t.key, outcome=outcome,discarded=discarded, usage=usage, error=error))diff --git a/harness/codeverse3d/tracks/generation.py b/harness/codeverse3d/tracks/generation.pyindex cc083783..a2ff70da 100644--- a/harness/codeverse3d/tracks/generation.py+++ b/harness/codeverse3d/tracks/generation.py@@ -406,9 +406,7 @@ def generate_files(label=f"{task.label}:r{task.round:02d}",)resp = model.generate(req)- # + every paid round-trip the winner does not represent (a billed-but-invalid- # response, a late hedge loser): the ledger records those as source="extra"- usage = resp.usage + (resp.raw.get("wasted_usage") or Usage())+ usage = resp.usage# the clock is not enforced here: the response is already paid for, and raising would# discard it before transcript/parse/write_files — the round's phase boundary does# (steps._run_phase)diff --git a/harness/tests/models/test_gemini_scheduling.py b/harness/tests/models/test_gemini_scheduling.pyindex 8dcb8f18..63b64fea 100644--- a/harness/tests/models/test_gemini_scheduling.py+++ b/harness/tests/models/test_gemini_scheduling.py@@ -84,7 +84,7 @@ def test_max_wait_s_must_be_positive():def test_a_clean_call_reports_one_attempt_and_a_failed_call_carries_its_count():m, _log, _ = make_model([text_response("hi")])r = m.generate(ChatRequest(messages=[ChatMessage.user("x")]))- assert r.raw["attempts"] == 1 and r.raw["hedged"] == 0 and r.raw["key"] == "…k1"+ assert r.raw["attempts"] == 1 and r.raw["key"] == "…k1"hedged, log, _ = make_model([api_error(503), api_error(503), text_response("recovered")],@@ -93,7 +93,7 @@ def test_a_clean_call_reports_one_attempt_and_a_failed_call_carries_its_count():)recovered = hedged.generate(ChatRequest(messages=[ChatMessage.user("x")]))assert recovered.text == "recovered" and len(log) == 3- assert recovered.raw["attempts"] == 3 and recovered.raw["hedged"] == 1+ assert recovered.raw["attempts"] == 3assert recovered.raw["key"] in ("…k2", "…k3")m2, _log2, _ = make_model(