Official Codebase of 3DCodeVerse
Python
claude-code: a session's tool_calls are the calls its stream shows, not num_turns - 1
gaoypeng1cc3dbfcd7ec
diff --git a/harness/codeverse3d/agents/backends.py b/harness/codeverse3d/agents/backends.pyindex b15eb4f9..3e5c9fdf 100644--- a/harness/codeverse3d/agents/backends.py+++ b/harness/codeverse3d/agents/backends.py@@ -647,7 +647,6 @@ def usage_from_envelope(env: dict[str, Any], model: str) -> Usage:output_tokens=int(u.get("output_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),latency_ms=int(env.get("duration_ms") or 0),)@@ -734,9 +733,10 @@ class ClaudeCodeAgent(_CliAgent):stream = ClaudeStream()proc = invoke(s, self.build_argv(s), self.build_env(s), prompt=job.prompt, on_stdout=stream.feed)ended = time.time()- record_tool_calls(s, stream.calls, source="claude-code stream-json", skills_index=stream.skills)+ n_calls = record_tool_calls(s, stream.calls, source="claude-code stream-json", skills_index=stream.skills)env = parse_claude_json(proc.stdout)usage = usage_from_envelope(env, self.model) if env else stream.usage(self.model)+ usage.tool_calls = n_callsusage.latency_ms = usage.latency_ms or int(proc.duration_s * 1000)text = str((env or {}).get("result") or "")turns = int((env or {}).get("num_turns") or 0)@@ -764,7 +764,7 @@ class ClaudeCodeAgent(_CliAgent):else:reason, ok = "completed", Truereturn finish_session(- s, ok=ok, exit_reason=reason, text=text, usage=usage, tool_calls=max(turns - 1, 0), turns=turns,+ s, ok=ok, exit_reason=reason, text=text, usage=usage, tool_calls=n_calls, turns=turns,errors=errors, transient=transient, quota=quota,provider_wait_s=provider_wait([(t, b) for t, b, _ in stream.retries], ended, stream.progress),rc=proc.rc, killed_reason=proc.killed_reason, num_turns=turns,diff --git a/harness/tests/agents/test_claude_code.py b/harness/tests/agents/test_claude_code.pyindex b26adf0e..c16cc9fc 100644--- a/harness/tests/agents/test_claude_code.py+++ b/harness/tests/agents/test_claude_code.py@@ -67,6 +67,8 @@ if mode == "error":env["is_error"] = True; env["subtype"] = "error_max_turns"if "--append-system-prompt" in args:env["result"] += " | sys=" + args[args.index("--append-system-prompt") + 1]+print(json.dumps({"type": "assistant", "message": {"id": "m1", "content": [+ {"type": "tool_use", "id": "t1", "name": "Write", "input": {"file_path": "src/hello.txt"}}]}}))print("some log line")print(json.dumps(env))'''@@ -104,7 +106,9 @@ def test_fake_run_success_and_error(tmp_ws: Workspace, fake_bin, monkeypatch):a = ClaudeCodeAgent("sonnet", binary=fake_bin("claude", FAKE_CLAUDE))res = a.run(AgentJob(workspace=str(tmp_ws.root), prompt="hello", label="c", system_append="SYS", timeout_s=30))assert res.ok and res.exit_reason == "completed", res.errors- assert res.text.endswith("sys=SYS") and res.usage.cost_usd == 0.0123 and res.tool_calls == 2+ assert res.text.endswith("sys=SYS") and res.usage.cost_usd == 0.0123+ # the calls the stream shows, not num_turns - 1 (N75: 24 "calls" for 3 real ones)+ assert res.tool_calls == res.usage.tool_calls == 1assert res.turns == 3 # the envelope's num_turns, in the typed resultassert [f.path for f in res.files_changed] == ["src/hello.txt", "src/prompt.txt"]assert (tmp_ws.src / "prompt.txt").read_text() == "hello" # on stdin, byte for byte