Skip to content

Official Codebase of 3DCodeVerse

Python

bench: a one-shot claude/codex session that never finished books what it spent, not $0

gaoypeng0cdb115784cc
diff --git a/eval/bench/_oneshot.py b/eval/bench/_oneshot.py
index 85f2a972..284945eb 100644
--- a/eval/bench/_oneshot.py
+++ b/eval/bench/_oneshot.py
@@ -38,6 +38,7 @@ from pydantic import BaseModel, Field
from bench._infra import is_infra_failure
from codeverse3d.agents.backends import (
CLAUDE_SETTING_SOURCES,
+ ClaudeStream,
effort_overrides,
parse_claude_json,
parse_codex_jsonl,
@@ -393,7 +394,12 @@ class ClaudeOneShot:
soft_timeout_s=timeout_s, idle_grace_s=IDLE_GRACE_S, activity_dirs=[cwd])
_dump(out_dir, prompt, argv, proc.stdout, proc.stderr)
env = parse_claude_json(proc.stdout)
- usage = usage_from_envelope(env, self.model or "default") if env else Usage(backend=self.kind, model=self.model)
+ stream = ClaudeStream()
+ for line in proc.stdout.splitlines():
+ stream.feed(line)
+ # a killed session printed no envelope: its per-message usage blocks, as the harness books it
+ usage = usage_from_envelope(env, self.model or "default") if env else stream.usage(self.model or "default")
+ usage.tool_calls = len(stream.calls)
usage.latency_ms = usage.latency_ms or int(proc.duration_s * 1000)
# the answer is EVERY assistant text block in order, not the result envelope's `result`: a reply that
# hits the per-message output limit is auto-continued, and `result` holds only the last continuation
@@ -455,7 +461,10 @@ class CodexOneShot:
_dump(out_dir, prompt, argv, proc.stdout, proc.stderr)
events = parse_codex_jsonl(proc.stdout)
text = last_msg.read_text() if last_msg.is_file() else "\n\n".join(m for m in events.messages if m.strip())
- usage = events.usage(self.model or codex_default_model() or "default")
+ model = self.model or codex_default_model() or "default"
+ # no turn completed but requests were made: an estimate, as CodexAgent books it — never a silent $0
+ estimated = events.turns_completed == 0 and (bool(events.items) or proc.timed_out)
+ usage = events.estimated_usage(model, len(prompt)) if estimated else events.usage(model)
usage.latency_ms = int(proc.duration_s * 1000)
notes = "; ".join(events.errors[:3])
ok = bool(text.strip()) and not proc.timed_out
diff --git a/eval/tests/test_oneshot.py b/eval/tests/test_oneshot.py
index 53efc2af..32eb127d 100644
--- a/eval/tests/test_oneshot.py
+++ b/eval/tests/test_oneshot.py
@@ -157,3 +157,29 @@ def test_a_continued_claude_reply_is_read_whole_not_just_its_last_continuation()
{"type": "result", "subtype": "success", "result": second, "is_error": False},
))
assert assistant_text(stream) == first + second
+
+
+_CLAUDE_KILLED = [ # a reply's usage block, then no result envelope
+ {"type": "system", "subtype": "init"},
+ {"type": "assistant", "message": {"id": "m1", "model": "claude-sonnet-5", "content": [{"type": "text", "text": "```js"}],
+ "usage": {"input_tokens": 9000, "output_tokens": 4000}}}]
+_CODEX_KILLED = [ # an item completed, no turn did
+ {"type": "thread.started", "thread_id": "t"}, {"type": "turn.started"},
+ {"type": "item.completed", "item": {"type": "agent_message", "text": "import bpy\n" + "# pad\n" * 500}}]
+
+
[email protected]("backend, events", [
+ (lambda b: ClaudeOneShot("claude-sonnet-5", binary=b), _CLAUDE_KILLED),
+ (lambda b: CodexOneShot("gpt-5.5", binary=b), _CODEX_KILLED),
+], ids=["claude", "codex"])
+def test_a_session_that_never_finished_books_what_it_spent_not_zero(tmp_path: Path, backend, events):
+ """N82a: the harness books these sessions (stream usage / codex estimate); the one-shot booked $0."""
+ import json as _json
+
+ out = tmp_path / "stdout.jsonl"
+ out.write_text("\n".join(_json.dumps(e) for e in events) + "\n")
+ fake = tmp_path / "cli"
+ fake.write_text(f"#!{sys.executable}\nimport sys\nsys.stdin.read()\nprint(open({str(out)!r}).read())\nsys.exit(1)\n")
+ fake.chmod(0o755)
+ r = backend(str(fake)).generate("brief", out_dir=tmp_path / "o", timeout_s=30)
+ assert r.usage.input_tokens > 0 and r.usage.cost_usd > 0, r.usage