Skip to content

Official Codebase of 3DCodeVerse

Python

models: clip the image model's per-attempt socket to its remaining budget

gaoypengb7dee93cd48c
diff --git a/harness/codeverse/models/gemini_image.py b/harness/codeverse/models/gemini_image.py
index 81e7f76d..49c747d8 100644
--- a/harness/codeverse/models/gemini_image.py
+++ b/harness/codeverse/models/gemini_image.py
@@ -38,6 +38,7 @@ from PIL import Image
from codeverse.contracts.common import Usage
from codeverse.models.base import ModelError
from codeverse.models.gemini import (
+ HTTP_TIMEOUT_FLOOR_S,
GeminiModel,
_default_keys,
_retry_after_s,
@@ -45,7 +46,7 @@ from codeverse.models.gemini import (
failure_outcome,
shared_pool,
)
-from codeverse.models.gemini_convert import FATAL_FINISH, parse_usage
+from codeverse.models.gemini_convert import FATAL_FINISH, clip_timeout, parse_usage
from codeverse.models.keypool import MAX_WAIT_S, KeyPool
from codeverse.models.parts import Stopwatch
from codeverse.models.pricing import estimate_cost
@@ -269,11 +270,20 @@ class GeminiImageModel:
http_options=types.HttpOptions(timeout=int(self.timeout_s * 1000)),
)
# the caller's budget clips the retry deadline, never extends it (the same
- # contract as GeminiModel.generate with ChatRequest.max_wait_s)
+ # contract as GeminiModel.generate with ChatRequest.max_wait_s), and each
+ # attempt's HTTP read timeout is the REMAINING budget, floored like
+ # GeminiModel._attempt_config — a 60 s image budget no longer holds a
+ # 180 s socket past its deadline.
budget = RETRY_DEADLINE_S if max_wait_s is None else min(RETRY_DEADLINE_S, float(max_wait_s))
+ deadline = time.monotonic() + budget
+
+ def _attempt_config() -> types.GenerateContentConfig:
+ remaining = max(HTTP_TIMEOUT_FLOOR_S, deadline - time.monotonic())
+ return clip_timeout(config, int(min(self.timeout_s, remaining) * 1000))
+
return rotate_with_retries(
self.pool,
- lambda key: self._call(key, model, contents, config, size),
+ lambda key: self._call(key, model, contents, _attempt_config(), size),
classify=classify_exception,
outcome_of=failure_outcome,
max_attempts=self.max_attempts,
diff --git a/harness/tests/flywheel_cli/test_gemini_image.py b/harness/tests/flywheel_cli/test_gemini_image.py
index ec4d6d58..863a1cb2 100644
--- a/harness/tests/flywheel_cli/test_gemini_image.py
+++ b/harness/tests/flywheel_cli/test_gemini_image.py
@@ -93,3 +93,41 @@ def test_all_keys_dead_raises_the_key_error():
with pytest.raises(ModelError) as ei:
m.generate_with_usage("brushed steel")
assert ei.value.status == 403 and len(set(used)) == 2
+
+
+def test_a_small_image_budget_shortens_the_per_attempt_http_timeout():
+ """max_wait_s=60 must clip the attempt's HTTP read timeout below the 180 s
+ constant (floored at HTTP_TIMEOUT_FLOOR_S) — mirrors GeminiModel._attempt_config."""
+ configs: list = []
+
+ class _Capture(_Client):
+ def _generate(self, *, model, contents, config):
+ configs.append(config)
+ return super()._generate(model=model, contents=contents, config=config)
+
+ used: list[str] = []
+ script = [_image_response()]
+ pool = KeyPool(["key_a", "key_b"])
+ m = GeminiImageModel("test-image", fallback=None, pool=pool, max_attempts=1,
+ sleep=lambda s: None, client_factory=lambda key: _Capture(key, script, used))
+ m.generate("a red cube", size=512, max_wait_s=60.0)
+ assert len(configs) == 1
+ t_ms = configs[0].http_options.timeout
+ assert 20_000 <= t_ms <= 60_000, t_ms # clipped to the budget, floored at 20 s
+
+
+def test_no_image_budget_keeps_the_configured_read_timeout():
+ configs: list = []
+
+ class _Capture(_Client):
+ def _generate(self, *, model, contents, config):
+ configs.append(config)
+ return super()._generate(model=model, contents=contents, config=config)
+
+ used: list[str] = []
+ script = [_image_response()]
+ pool = KeyPool(["key_a", "key_b"])
+ m = GeminiImageModel("test-image", fallback=None, pool=pool, max_attempts=1,
+ sleep=lambda s: None, client_factory=lambda key: _Capture(key, script, used))
+ m.generate("a red cube", size=512)
+ assert configs[0].http_options.timeout == 180_000 # min(180 s, 900 s remaining) = the constant