Skip to content

Official Codebase of 3DCodeVerse

Python

gemini-cli: a quota 429 gets a third rotated attempt (transient non-quota failures keep two)

ziyao68f61a692fa7
diff --git a/harness/codeverse/agents/backends.py b/harness/codeverse/agents/backends.py
index 68dbbeb9..74322657 100644
--- a/harness/codeverse/agents/backends.py
+++ b/harness/codeverse/agents/backends.py
@@ -14,7 +14,7 @@ argv: ``gemini -p <prompt> -m <model> --approval-mode yolo --skip-trust --output
priced at its own rate. Requested model absent from ``stats.models`` →
``model_substituted``.
* one retry (rotated key when the pool has one, else the same key) on transient
- failures (429 / 503 / empty response); a throttled/empty pool never raises.
+ failures (503 / empty response), two on a quota 429; a throttled/empty pool never raises.
"""
from __future__ import annotations
@@ -62,6 +62,8 @@ SYSTEM_SETTINGS = {
}
#: how long a retry waits for a *different* healthy key before reusing the same one
RETRY_KEY_WAIT_S = 10.0
+#: attempts allowed when the failure is a quota 429 (transient non-quota failures keep 2)
+QUOTA_MAX_ATTEMPTS = 3
def retry_window_left(timeout_s: float, elapsed_s: float) -> float | None:
@@ -227,7 +229,11 @@ class GeminiCliAgent:
usage_total = usage_total + outcome["usage"]
pool.report(key, "429" if outcome["quota"] else ("ok" if outcome["ok"] else "5xx"))
pool.release() # one acquire per attempt: keep the pool's in-flight gauge honest
- if outcome["ok"] or outcome["exit_reason"] in ("timeout", "model_substituted") or not outcome["transient"] or attempts >= 2:
+ # a per-minute 429 on the single key a CLI process holds clears within the
+ # rotated retry's wait; ab_fewer_turns (2026-08-29) lost 4 of 10 cells to
+ # two 429s in a row, so a quota failure gets one more rotated attempt
+ max_attempts = QUOTA_MAX_ATTEMPTS if outcome["quota"] else 2
+ if outcome["ok"] or outcome["exit_reason"] in ("timeout", "model_substituted") or not outcome["transient"] or attempts >= max_attempts:
break
next_soft = retry_window_left(job.timeout_s, time.monotonic() - t0)
if next_soft is None:
diff --git a/harness/tests/agents/test_gemini_cli.py b/harness/tests/agents/test_gemini_cli.py
index 7496ffce..af78416a 100644
--- a/harness/tests/agents/test_gemini_cli.py
+++ b/harness/tests/agents/test_gemini_cli.py
@@ -21,11 +21,11 @@ assert os.environ.get("GEMINI_API_KEY"), "no key in env"
assert "GEMINI_API_KEYS" not in os.environ
assert "FAKE_SERVICE_API_KEY" not in os.environ, "secret leaked"
assert os.path.isfile(os.environ["GEMINI_CLI_SYSTEM_SETTINGS_PATH"])
-if mode in ("fail_once", "fail_once_503", "fail_always"):
+if mode in ("fail_once", "fail_once_503", "fail_always", "fail_twice_429"):
marker = "artifacts/attempts.txt"
n = int(open(marker).read()) if os.path.exists(marker) else 0
open(marker, "w").write(str(n + 1))
- if n == 0 or mode == "fail_always":
+ if n == 0 or mode == "fail_always" or (mode == "fail_twice_429" and n < 2):
if mode == "fail_once_503":
print("Error when talking to Gemini API: got status: UNAVAILABLE 503", file=sys.stderr)
sys.exit(247)
@@ -255,3 +255,20 @@ def test_live_gemini_cli_creates_file(tmp_ws: Workspace):
assert (tmp_ws.src / "hello.txt").read_text().strip() == "hi"
assert res.usage.input_tokens > 0 and res.usage.model == "gemini-3.7-flash"
assert any(f.path == "src/hello.txt" for f in res.files_changed)
+
+
+def test_quota_failure_gets_a_third_rotated_attempt(tmp_ws: Workspace, agent: GeminiCliAgent, monkeypatch):
+ """Two 429s in a row on two keys, the third key answers: the cell is not lost."""
+ monkeypatch.setattr(get_settings(), "gemini_api_keys", ["k1", "k2", "k3"])
+ from codeverse.models import gemini as gm
+ for sig in [s for s in list(gm._pools) if s and s[0] in ("k1", "only")]:
+ gm._pools.pop(sig, None)
+ monkeypatch.setenv("FAKE_MODE", "fail_twice_429")
+ res = agent.run(_job(tmp_ws))
+ assert res.ok, res.errors
+ assert (tmp_ws.artifacts / "attempts.txt").read_text() == "3"
+ rec = json.loads((Path(res.transcript_path).parent / "result.json").read_text())
+ assert rec["attempts"] == 3
+ lines = [json.loads(ln) for ln in Path(res.transcript_path).read_text().splitlines()]
+ keys = [ln["key_tail"] for ln in lines if ln["kind"] == "invoke"]
+ assert len(keys) == 3 and len(set(keys)) == 3 # a fresh key each time