Skip to content

Official Codebase of 3DCodeVerse

Python

anthropic structured output bypassed strip_control_chars: a model NUL reached argv

gaoypengb1de85b5aaff
diff --git a/harness/codeverse/models/anthropic.py b/harness/codeverse/models/anthropic.py
index 9775e180..86354501 100644
--- a/harness/codeverse/models/anthropic.py
+++ b/harness/codeverse/models/anthropic.py
@@ -22,7 +22,7 @@ from codeverse.models.keypool import MAX_WAIT_S
from codeverse.models.parts import Stopwatch
from codeverse.models.pricing import cache_write_surcharge, estimate_cost
from codeverse.models.retry import with_retries
-from codeverse.models.schema_utils import JsonParseError, parse_json_lenient
+from codeverse.models.schema_utils import JsonParseError, parse_json_lenient, strip_control_chars
log = logging.getLogger(__name__)
@@ -147,7 +147,13 @@ class AnthropicModel:
parsed: Any = None
if request.response_schema is not None:
if submit is not None:
- parsed = submit
+ # The SDK hands back tool input already parsed, so it never passes through
+ # parse_json_lenient -> strip_control_chars the way the gemini/openai text
+ # paths do. A model NUL in a `submit` field would otherwise survive into
+ # plan.json and detonate stages later at subprocess.Popen ("embedded null
+ # byte"): the identical failure strip_control_chars was written to end, and
+ # sanitising at ingestion is the only place that covers every model.
+ parsed = strip_control_chars(submit)
if not text:
text = json.dumps(parsed)
elif not calls:
diff --git a/harness/codeverse/tracks/brief.py b/harness/codeverse/tracks/brief.py
index b8f3836f..994e38dd 100644
--- a/harness/codeverse/tracks/brief.py
+++ b/harness/codeverse/tracks/brief.py
@@ -26,6 +26,7 @@ from codeverse.contracts.chat import ChatMessage, ChatRequest, ImagePart
from codeverse.contracts.common import Track, Usage
from codeverse.contracts.plan import EngineeringBrief
from codeverse.contracts.spec import Spec
+from codeverse.models.schema_utils import parse_json_lenient
from codeverse.prompts import load_text, prompt_hash, render
log = logging.getLogger(__name__)
@@ -134,7 +135,9 @@ def expand_brief(spec: Spec, model_id: str, *, model: Any | None = None, events:
response_schema=EngineeringBrief.model_json_schema(), temperature=0.3, thinking="low",
max_output_tokens=BRIEF_MAX_TOKENS, label="planner-brief"))
usage = usage + resp.usage
- raw = resp.parsed if resp.parsed is not None else json.loads(resp.text or "{}")
+ # 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)
if not brief.is_useful:
raise ValueError(f"brief has {len(brief.sub_assemblies)} sub-assemblies and "
diff --git a/harness/tests/models/test_control_chars.py b/harness/tests/models/test_control_chars.py
index 0da53229..af6ba2cb 100644
--- a/harness/tests/models/test_control_chars.py
+++ b/harness/tests/models/test_control_chars.py
@@ -69,3 +69,78 @@ def test_the_subprocess_backstop_names_the_offender(tmp_path):
poisoned = "harness_instructions: envelope is 0.078 \x00 0.300 m"
with pytest.raises(ValueError, match=r"argv\[2\] contains a NUL at offset \d+"):
run_with_watchdog(["echo", "ok", poisoned], cwd=tmp_path, env=None, soft_timeout_s=5)
+
+
+def test_the_anthropic_submit_tool_path_is_sanitised_too():
+ """CP-2: the SDK hands `submit` tool input back already parsed, so it never met
+ parse_json_lenient. gemini/openai set ``parsed = parse_json_lenient(text)``
+ unconditionally and were clean; anthropic set ``parsed = submit`` raw, so a model
+ NUL reached plan.json (tracks/planner.py reads resp.parsed first) and detonated at
+ Popen four stages later — the very failure this module's fix was written to end."""
+ from types import SimpleNamespace as NS
+
+ from codeverse.contracts.chat import ChatMessage, ChatRequest
+ from codeverse.models.anthropic import AnthropicModel
+ from codeverse.models.anthropic_convert import SUBMIT_TOOL
+
+ block = NS(type="tool_use", id="t1", name=SUBMIT_TOOL,
+ input={"description": "0.078 \x00 0.300 slab"})
+ reply = NS(id="m1", model="claude-opus-5", content=[block], stop_reason="tool_use",
+ stop_details=None,
+ usage=NS(input_tokens=10, output_tokens=2,
+ cache_read_input_tokens=0, cache_creation_input_tokens=0))
+
+ class FakeClient:
+ def __init__(self):
+ self.messages = NS(create=lambda **kw: reply)
+
+ model = AnthropicModel("claude-opus-5", client=FakeClient(), sleep=lambda s: None)
+ resp = model.generate(ChatRequest(
+ messages=[ChatMessage.user("hi")],
+ response_schema={"type": "object", "properties": {"description": {"type": "string"}}},
+ ))
+ assert "\x00" not in resp.parsed["description"]
+ assert resp.parsed["description"] == "0.078 0.300 slab"
+ # the text mirror the harness derives from it must be clean as well
+ assert "\x00" not in (resp.text or "")
+
+
+def test_the_brief_text_fallback_is_sanitised(tmp_path):
+ """CP-2 (second instance): tracks/brief.py fell back to ``json.loads(resp.text)``,
+ which is neither lenient nor sanitised — so a NUL in a brief field reached the plan
+ the same way the anthropic submit path did. Uses the TEXT path (parsed=None), which
+ is what every provider returns when the model answers without the submit tool."""
+ from codeverse.contracts.chat import ChatResponse
+ from codeverse.contracts.common import Language, Track, Usage
+ from codeverse.contracts.plan import EngineeringBrief, RefDimension, SubAssembly
+ from codeverse.contracts.spec import Spec
+ from codeverse.tracks.brief import expand_brief
+
+ clean = EngineeringBrief(
+ object_name="Grinder", reference="Peugeot 1920s box grinder",
+ one_line="a wooden box grinder",
+ dimensions_m=[RefDimension(name="width", meters=0.14)],
+ sub_assemblies=[SubAssembly(name="burr", purpose="grinds", parts=["burr"])],
+ mechanism="the crank turns the shaft",
+ visible_from_outside=["the drawer seam"],
+ signature_features=["open hopper", "front drawer"], materials=["body: beech"])
+ payload = json.loads(clean.model_dump_json())
+ payload["mechanism"] = "the crank turns 0.078 \x00 0.300 the shaft"
+
+ class TextOnlyModel:
+ """Returns JSON as TEXT with parsed=None — the json.loads fallback branch."""
+
+ provider, model = "fake", "fake-1"
+
+ def supports_vision(self) -> bool:
+ return True
+
+ def generate(self, request):
+ return ChatResponse(text=json.dumps(payload), parsed=None, usage=Usage())
+
+ spec = Spec(id="s1", track=Track.STATIC_OBJECT, language=Language.BLENDER,
+ prompt="a hand-crank coffee grinder")
+ brief, _ = expand_brief(spec, "fake:planner", model=TextOnlyModel(), cache_dir=tmp_path)
+ assert brief is not None, "the brief must still be produced, only cleaned"
+ assert "\x00" not in brief.mechanism
+ assert brief.mechanism == "the crank turns 0.078 0.300 the shaft"