Skip to content

Official Codebase of 3DCodeVerse

Python

planner: expand_brief asks through ask_structured — the hand-built copy of its request goes; a failed call now reports what the provider billed

gaoypengd535123ffc08
diff --git a/harness/codeverse3d/models/schema_utils.py b/harness/codeverse3d/models/schema_utils.py
index 270b7244..1ddccd9d 100644
--- a/harness/codeverse3d/models/schema_utils.py
+++ b/harness/codeverse3d/models/schema_utils.py
@@ -370,13 +370,13 @@ def ask_structured(model: Any, schema: type[BaseModel], *, system: str, text: st
label: str) -> tuple[Any, Usage, str]:
"""One structured call → ``(validated object | None, usage, error)``.
- Five callers (reference gate / image-prompt plan / mismatch diff, the texture
- material plan and the scene texture pack) built the same ChatRequest
+ Six callers (reference gate / image-prompt plan / mismatch diff, the texture
+ material plan, the scene texture pack and the planner's brief) built the same ChatRequest
(thinking="low", the 65 536 ceiling, the 900 s wait), caught the same two failure
families and parsed the same two ways. They differ only in what they RETURN on
failure, which is why this hands the error back rather than raising or deciding.
Not for callers that need to separate parse errors from call errors
- (``judges.pairwise``, ``tracks.planner``).
+ (``judges.pairwise``, ``tracks.planner.plan``).
"""
req = ChatRequest(messages=[ChatMessage.user(text, images=list(images) or None)], system=system,
response_schema=schema.model_json_schema(), temperature=temperature,
diff --git a/harness/codeverse3d/tracks/planner.py b/harness/codeverse3d/tracks/planner.py
index 6c699adc..8ad1a98c 100644
--- a/harness/codeverse3d/tracks/planner.py
+++ b/harness/codeverse3d/tracks/planner.py
@@ -37,7 +37,7 @@ from codeverse3d.contracts.common import Language, Track, Usage
from codeverse3d.contracts.plan import AcceptanceItem, EngineeringBrief
from codeverse3d.contracts.spec import Spec
from codeverse3d.conventions import LANGUAGE_FRAME, frame_doc, to_pascal, to_snake
-from codeverse3d.models.schema_utils import parse_json_lenient
+from codeverse3d.models.schema_utils import ask_structured, parse_json_lenient
from codeverse3d.proc import NULL_EVENTS, sha256_file
from codeverse3d.prompts import load_text, prompt_hash, render
from codeverse3d.prompts.catalog import language_text
@@ -51,7 +51,6 @@ log = logging.getLogger(__name__)
#: tracks the object-shaped brief applies to (graphics/scene get budgets only)
BRIEF_TRACKS = (Track.STATIC_OBJECT, Track.ARTICULATED_OBJECT)
BRIEF_TEMPLATE = "tracks/brief_object.j2"
-BRIEF_MAX_TOKENS = 65_536
# ----------------------------------------------------------------------------- brief
@@ -139,15 +138,10 @@ def expand_brief(spec: Spec, model_id: str, *, model: Any | None = None, events:
if images:
user += f"\n\n{len(images)} reference image(s) are attached — read the dimensions and features off them."
try:
- resp = model.generate(ChatRequest(
- messages=[ChatMessage.user(user, images=images or None)], system=system,
- response_schema=EngineeringBrief.model_json_schema(), temperature=0.3, thinking="low",
- max_output_tokens=BRIEF_MAX_TOKENS, max_wait_s=900.0, label="planner-brief"))
- usage = usage + resp.usage
- # parse_json_lenient, not json.loads: it tolerates fences/prose AND strips the
- # C0 controls a model can emit, which json.loads would carry into the brief.
- raw = resp.parsed if resp.parsed is not None else parse_json_lenient(resp.text or "{}")
- brief = EngineeringBrief.model_validate(raw)
+ brief, usage, error = ask_structured(model, EngineeringBrief, system=system, text=user, images=images,
+ temperature=0.3, label="planner-brief")
+ if brief is None:
+ raise ValueError(error)
if not brief.is_useful:
raise ValueError(f"brief has {len(brief.sub_assemblies)} sub-assemblies and "
f"{len(brief.signature_features)} signature features — nothing to plan from")
diff --git a/harness/tests/orchestrator_tracks/test_planner_depth.py b/harness/tests/orchestrator_tracks/test_planner_depth.py
index 8928fa67..5e8860e8 100644
--- a/harness/tests/orchestrator_tracks/test_planner_depth.py
+++ b/harness/tests/orchestrator_tracks/test_planner_depth.py
@@ -274,6 +274,19 @@ def test_brief_failure_is_never_fatal(tmp_path):
assert brief is None and usage.cost_usd == 0.0
+
+def test_a_failed_brief_call_keeps_what_the_provider_billed(tmp_path):
+ """expand_brief goes through ``ask_structured``: a ModelError's ``usage`` (a billed
+ bad reply) is returned, where the hand-built call reported the failure as free."""
+ from codeverse3d.contracts.common import Usage
+ from codeverse3d.models.base import ModelError
+
+ def billed(req):
+ raise ModelError("bad json", usage=Usage(backend="fake", cost_usd=0.002))
+
+ brief, usage = BR.expand_brief(_spec(), "fake:planner", model=FakeChatModel(billed), cache_dir=tmp_path)
+ assert brief is None and usage.cost_usd == pytest.approx(0.002)
+
def test_brief_switch_and_track_scope(monkeypatch, switch):
switch("C3D_PLAN_BRIEF", None)
assert BR.brief_enabled(_spec())