Official Codebase of 3DCodeVerse
Python
agents: three accounting/transient bugs in the vendor backends; one import, one prompt composer
gaoypeng42d8fe3bfffa
diff --git a/harness/codeverse3d/agents/backends.py b/harness/codeverse3d/agents/backends.pyindex 40afa6a3..2ce77f02 100644--- a/harness/codeverse3d/agents/backends.py+++ b/harness/codeverse3d/agents/backends.py@@ -239,7 +239,10 @@ class GeminiCliAgent(_CliAgent):soft_timeout_s=next_soft, model=self.model, key_tail=key[-4:])outcome = self._interpret(s, proc)usage_total = usage_total + outcome["usage"]- pool.report(key, "429" if outcome["quota"] else ("ok" if outcome["ok"] else "5xx"))+ # the pool is shared with every API call: a hang, a substituted model or the+ # agent's own failure is not the key's fault ("skip" = counters untouched)+ pool.report(key, "429" if outcome["quota"] else "ok" if outcome["ok"]+ else "5xx" if outcome["transient"] else "skip")finally:# one acquire per attempt: keep the pool's in-flight gauge honest — also when# invoke raises, or one slot of the PROCESS-WIDE pool is gone for good@@ -336,12 +339,17 @@ def parse_claude_json(stdout: str) -> dict[str, Any] | None:def usage_from_envelope(env: dict[str, Any], model: str) -> Usage:+ """``input_tokens`` is the TOTAL prompt (uncached + cache reads + cache writes), the+ convention pricing.py and anthropic.py share. The envelope's own ``input_tokens`` is the+ uncached part only, and CostBucket clamps ``cached`` to ``input``: every cache-read token+ past it and every cache-write token used to vanish from the ledger."""u = env.get("usage") or {}+ cache_read = int(u.get("cache_read_input_tokens") or 0)usage = Usage(backend="claude-code", model=model,- input_tokens=int(u.get("input_tokens") or 0),+ input_tokens=int(u.get("input_tokens") or 0) + cache_read + int(u.get("cache_creation_input_tokens") or 0),output_tokens=int(u.get("output_tokens") or 0),- cached_tokens=int(u.get("cache_read_input_tokens") or 0),+ cached_tokens=cache_read,thoughts_tokens=int(((u.get("output_tokens_details") or {}).get("thinking_tokens")) or 0),tool_calls=max(int(env.get("num_turns") or 0) - 1, 0),cost_usd=float(env.get("total_cost_usd") or 0.0),@@ -434,8 +442,10 @@ class ClaudeCodeAgent(_CliAgent):elif env is None or proc.rc != 0:reason, ok = "error", Falseerrors.append(f"rc={proc.rc}; no result envelope; stderr tail: {tail(proc.stderr, 1500)}")- if is_transient_failure(proc.stderr, proc.stdout):- reason = "budget" if is_quota_failure(proc.stderr) else "error"+ # a 529 / overloaded exit is the provider's, not the task's (AgentResult.transient)+ transient = is_transient_failure(proc.stderr, proc.stdout)+ if transient and is_quota_failure(proc.stderr):+ reason = "budget"elif env.get("is_error") or str(env.get("subtype", "")).startswith("error"):ok = Falsesub = str(env.get("subtype", ""))@@ -745,11 +755,7 @@ class AntigravityAgent(_CliAgent):def _compose_prompt_agy(s: Session) -> str:"""agy has no system-prompt flag; prepend harness instructions + the absolute workspace path."""- job = s.jobhead = f"Workspace root: {s.ws.root}\nWork ONLY inside it, using your file tools."if (s.ws.root / "AGENTS.md").is_file():head += " Read AGENTS.md there first and follow it."- head += "\n"- if job.system_append:- head += f"\n<harness_instructions>\n{job.system_append}\n</harness_instructions>\n"- return f"{head}\n{job.prompt}"+ return f"{head}\n\n{_compose_prompt(s.job)}"diff --git a/harness/codeverse3d/agents/cli_common.py b/harness/codeverse3d/agents/cli_common.pyindex 0ba345c8..4da01b07 100644--- a/harness/codeverse3d/agents/cli_common.py+++ b/harness/codeverse3d/agents/cli_common.py@@ -409,7 +409,7 @@ def begin_session(job: AgentJob, kind: str) -> Session:label, attempt = _session_label(ws, job.label or kind, round_index)traj = Trajectory(ws.trajectory_dir(label, round_index))traj.write_prompt(job.prompt, job.system_append)- hints = frozenset(h for h in (str(x).strip() for x in job.files_hint) if h)+ hints = _clean(job.files_hint)lock = _session_lock(ws) if kind in EXCLUSIVE_KINDS else Noneif lock is not None:lock.acquire()@@ -572,7 +572,6 @@ def invoke(on_stdout: Callable[[str], None] | None = None,stdin: str | None = None,attempt: int = 1,- idle_grace_s: float | None = None,soft_timeout_s: float | None = None,**invoke_extra: Any,) -> CompletedProc:@@ -607,7 +606,7 @@ def invoke(soft = float(s.job.timeout_s if soft_timeout_s is None else soft_timeout_s)proc = run_with_watchdog(argv, cwd=s.ws.root, env=env, soft_timeout_s=soft, hard_timeout_s=soft + 300.0,- idle_grace_s=IDLE_GRACE_S if idle_grace_s is None else idle_grace_s, poll_s=POLL_S,+ idle_grace_s=IDLE_GRACE_S, poll_s=POLL_S,on_line=on_line, stdin=stdin, activity_dirs=[s.ws.src, s.ws.public],)suffix = "" if attempt == 1 else f".{attempt}"diff --git a/harness/codeverse3d/agents/registry.py b/harness/codeverse3d/agents/registry.pyindex cba23afd..abea23c2 100644--- a/harness/codeverse3d/agents/registry.py+++ b/harness/codeverse3d/agents/registry.py@@ -63,21 +63,8 @@ def get_coding_agent(agent_id: str) -> CodingAgent:@lru_cache(maxsize=32)def _build_agent(agent_id: str) -> CodingAgent:- kind, model = parse_agent_id(agent_id)- if kind == "gemini-cli":- from codeverse3d.agents.backends import GeminiCliAgent+ kind, model = parse_agent_id(agent_id) # an unknown kind has raised already+ from codeverse3d.agents import backends- return GeminiCliAgent(model)- if kind == "claude-code":- from codeverse3d.agents.backends import ClaudeCodeAgent-- return ClaudeCodeAgent(model)- if kind == "codex":- from codeverse3d.agents.backends import CodexAgent-- return CodexAgent(model)- if kind == "agy":- from codeverse3d.agents.backends import AntigravityAgent-- return AntigravityAgent(model)- raise ValueError(f"unknown agent kind {kind!r}; known: {KINDS}")+ return {"gemini-cli": backends.GeminiCliAgent, "claude-code": backends.ClaudeCodeAgent,+ "codex": backends.CodexAgent, "agy": backends.AntigravityAgent}[kind](model)diff --git a/harness/tests/agents/test_claude_code.py b/harness/tests/agents/test_claude_code.pyindex 55223c9f..5583861a 100644--- a/harness/tests/agents/test_claude_code.py+++ b/harness/tests/agents/test_claude_code.py@@ -47,6 +47,12 @@ assert "--dangerously-skip-permissions" in args and "--output-format" in argsassert "--strict-mcp-config" in argsassert "FAKE_SERVICE_API_KEY" not in os.environmode = os.environ.get("FAKE_MODE", "ok")+if mode == "overloaded": # what `claude -p` exits with when the API is out of capacity+ print('API Error: 529 {"type":"error","error":{"type":"overloaded_error","message":"Overloaded"}}', file=sys.stderr)+ sys.exit(1)+if mode == "rate_limited":+ print("API Error: 429 rate_limit_error: This request would exceed your rate limit", file=sys.stderr)+ sys.exit(1)os.makedirs("src", exist_ok=True)open("src/hello.txt", "w").write("hi")env = json.loads(''' + repr(json.dumps(ENVELOPE)) + r''')@@ -65,7 +71,10 @@ def test_parse_envelope_variants():assert parse_claude_json(json.dumps([{"type": "assistant"}, ENVELOPE]))["type"] == "result"assert parse_claude_json("") is Noneu = usage_from_envelope(ENVELOPE, "sonnet")- assert u.input_tokens == 10 and u.cached_tokens == 5000 and u.cost_usd == 0.0123 and u.model == "claude-sonnet-4-6"+ assert u.cached_tokens == 5000 and u.cost_usd == 0.0123 and u.model == "claude-sonnet-4-6"+ # input_tokens is the TOTAL prompt (10 uncached + 5 000 read + 200 written), as for every+ # other backend: the uncached 10 alone let CostBucket clamp 4 990 cached + the 200 away+ assert u.input_tokens == 5210def test_argv_includes_mcp_when_materialized(tmp_ws: Workspace):@@ -107,6 +116,19 @@ def test_fake_run_success_and_error(tmp_ws: Workspace, fake_bin, monkeypatch):monkeypatch.setenv("FAKE_MODE", "error")res2 = a.run(AgentJob(workspace=str(tmp_ws.root), prompt="hello", label="c2", timeout_s=30))assert not res2.ok and res2.exit_reason == "budget" and "error_max_turns" in res2.errors[0]+ assert res2.transient is False # the task's own failure+++def test_an_overloaded_exit_is_transient(tmp_ws: Workspace, fake_bin, monkeypatch):+ """A 529 exit is the provider's, not the task's: it must say so (AgentResult.transient) —+ the storm fallback and the repair loop's stop both key on it, and it was always False."""+ a = ClaudeCodeAgent("sonnet", binary=fake_bin("claude", FAKE_CLAUDE))+ monkeypatch.setenv("FAKE_MODE", "overloaded")+ res = a.run(AgentJob(workspace=str(tmp_ws.root), prompt="hello", label="o", timeout_s=30))+ assert not res.ok and res.exit_reason == "error" and res.transient is True+ monkeypatch.setenv("FAKE_MODE", "rate_limited")+ res = a.run(AgentJob(workspace=str(tmp_ws.root), prompt="hello", label="q", timeout_s=30))+ assert not res.ok and res.exit_reason == "budget" and res.transient is True@pytest.mark.livediff --git a/harness/tests/agents/test_gemini_cli.py b/harness/tests/agents/test_gemini_cli.pyindex fcc4c59d..a8fa4de0 100644--- a/harness/tests/agents/test_gemini_cli.py+++ b/harness/tests/agents/test_gemini_cli.py@@ -33,6 +33,9 @@ if mode in ("fail_once", "fail_once_503", "fail_always", "fail_twice_429"):sys.exit(1)if mode == "hang":time.sleep(60)+if mode == "crash": # the agent's own failure: nothing transient about it+ print("TypeError: Cannot read properties of undefined (reading 'parts')", file=sys.stderr)+ sys.exit(2)if mode == "storm": # what gemini-cli 0.53 prints, 8-17 times per session, in a 503 stormfor i in range(4):print(f"Attempt {i + 1} failed with status 503. Retrying with backoff... _ApiError: UNAVAILABLE", file=sys.stderr)@@ -225,6 +228,25 @@ def test_a_timeout_after_a_503_streak_is_marked_transient(tmp_ws: Workspace, ageassert any("4 x 503" in e for e in res.errors), res.errors+def test_the_key_pool_hears_a_5xx_only_when_the_failure_was_transient(tmp_ws: Workspace, agent: GeminiCliAgent,+ monkeypatch):+ """The pool is shared with every API call and a "5xx" costs the key 20 % of its health: a+ hang, a substituted model or the agent's own crash is not the KEY's fault."""+ from codeverse3d.agents import backends as gc++ monkeypatch.setattr("codeverse3d.agents.cli_common.IDLE_GRACE_S", 1.0)+ pool = gc._key_pool(get_settings().gemini_api_keys) # noqa: SLF001+ for mode, timeout_s in (("substitute", 20), ("crash", 20), ("hang", 1)):+ monkeypatch.setenv("FAKE_MODE", mode)+ assert not agent.run(_job(tmp_ws, timeout_s=timeout_s, label=mode)).ok+ st = pool.stats()+ assert (st["5xx"], st["ok"], st["error"]) == (0, 0, 0), st+ monkeypatch.setenv("FAKE_MODE", "fail_once_503") # a real 503, then a rotated success+ assert agent.run(_job(tmp_ws, label="storm")).ok+ st = pool.stats()+ assert (st["5xx"], st["ok"]) == (1, 1), st++def test_unavailable_when_no_keys(tmp_ws: Workspace, agent: GeminiCliAgent, monkeypatch):monkeypatch.setattr(get_settings(), "gemini_api_keys", [])ok, why = agent.available()