Skip to content

Official Codebase of 3DCodeVerse

Python

review round 1: the four blocking bugs

ziyaoef6023fe1552
diff --git a/harness/codeverse/flywheel/_git.py b/harness/codeverse/flywheel/_git.py
index 5efd1103..03678337 100644
--- a/harness/codeverse/flywheel/_git.py
+++ b/harness/codeverse/flywheel/_git.py
@@ -1,8 +1,8 @@
"""Read-only git helpers for the flywheel: file trees at a commit, binary-safe.
The exporter / pairs builder must never touch the working tree of a run
-(another process may be resuming it), so everything here goes through
-``git archive`` / ``git ls-tree`` on the run's repository.
+(another process may be resuming it), so everything here reads the object database
+of the run's repository (``ls-tree`` / ``cat-file`` / ``diff``).
Read-only is not the same as safe: a run's ``.git/config`` and ``.gitattributes``
were writable by the agent, and git config can name a program to RUN. Every
@@ -14,9 +14,7 @@ adds :data:`~codeverse.workspace.GIT_SAFE_DIFF_FLAGS`.
from __future__ import annotations
-import io
import subprocess
-import tarfile
from pathlib import Path
from codeverse.workspace import GIT_SAFE_DIFF_FLAGS, GIT_SAFE_FLAGS, Workspace, git_safe_env
@@ -29,7 +27,21 @@ _SKIP_PARTS = {"node_modules", "__pycache__", ".git"}
class GitReadError(RuntimeError):
- """A git read (ls-tree / archive) failed or the commit does not exist."""
+ """A git read (ls-tree / cat-file / diff) failed or the commit does not exist."""
+
+
+def _run_stdin(ws: Workspace, stdin: bytes, *args: str) -> subprocess.CompletedProcess[bytes]:
+ """:func:`_run` with input on stdin (``cat-file --batch`` reads its object list there)."""
+ try:
+ proc = subprocess.run(
+ ["git", *GIT_SAFE_FLAGS, *args], cwd=ws.root, capture_output=True, check=False,
+ env=git_safe_env(), timeout=_GIT_TIMEOUT_S, input=stdin,
+ )
+ except (OSError, subprocess.SubprocessError) as e:
+ raise GitReadError(f"git {' '.join(args)} in {ws.root}: {e}") from e
+ if proc.returncode != 0:
+ raise GitReadError(f"git {' '.join(args)} in {ws.root}: {proc.stderr.decode('utf-8', errors='replace')[:400]}")
+ return proc
def _run(ws: Workspace, *args: str) -> subprocess.CompletedProcess[bytes]:
@@ -80,23 +92,41 @@ def list_files_at(ws: Workspace, commit: str) -> list[str]:
def read_tree_at(ws: Workspace, commit: str) -> dict[str, bytes]:
- """``{repo-relative path: bytes}`` for every code file at ``commit`` (binary-safe)."""
+ """``{repo-relative path: bytes}`` for every code file at ``commit`` (binary-safe).
+
+ ``ls-tree`` + ``cat-file --batch``, never ``git archive``: archive renders content
+ through ``convert_to_working_tree``, so an agent-planted ``filter.<name>.smudge``
+ RUNS — and unlike textconv there is no flag to turn it off (``-c
+ core.attributesFile=/dev/null`` only silences the GLOBAL attributes file; the
+ in-repo ``.gitattributes`` and ``.git/info/attributes`` are still read).
+ ``cat-file`` hands back the raw blob and applies no filter unless asked."""
if not commit_exists(ws, commit):
raise GitReadError(f"commit {commit!r} not found in {ws.root}")
- # `git archive` errors on a pathspec that matches nothing, so only pass roots that exist.
- listed = list_files_at(ws, commit)
- present = [r for r in CODE_ROOTS if any(p.startswith(r + "/") for p in listed)]
- if not present:
+ entries: list[tuple[str, str]] = [] # (blob sha, path)
+ out = _run(ws, "ls-tree", "-r", "-z", commit, "--", *CODE_ROOTS).stdout
+ for rec in out.decode("utf-8", errors="replace").split("\0"):
+ if not rec:
+ continue
+ meta, _, path = rec.partition("\t")
+ parts = meta.split()
+ if len(parts) < 3 or parts[1] != "blob" or not _keep(path):
+ continue
+ entries.append((parts[2], path))
+ if not entries:
return {}
- data = _run(ws, "archive", "--format=tar", commit, "--", *present).stdout
+ proc = _run_stdin(ws, b"".join(f"{sha}\n".encode() for sha, _ in entries), "cat-file", "--batch")
files: dict[str, bytes] = {}
- with tarfile.open(fileobj=io.BytesIO(data), mode="r:") as tar:
- for m in tar.getmembers():
- if not m.isfile() or not _keep(m.name):
- continue
- fh = tar.extractfile(m)
- if fh is not None:
- files[m.name] = fh.read()
+ buf, pos = proc.stdout, 0
+ for _, path in entries:
+ nl = buf.find(b"\n", pos)
+ if nl < 0:
+ raise GitReadError(f"cat-file --batch ended early reading {path!r} from {ws.root}")
+ header = buf[pos:nl].decode("utf-8", errors="replace").split()
+ if len(header) != 3:
+ raise GitReadError(f"cat-file --batch: unreadable object for {path!r}: {' '.join(header)}")
+ size = int(header[2])
+ files[path] = buf[nl + 1:nl + 1 + size]
+ pos = nl + 1 + size + 1 # the trailing newline git writes after every object
return files
diff --git a/harness/codeverse/languages/urdf/__init__.py b/harness/codeverse/languages/urdf/__init__.py
index 255ccd61..38f3336f 100644
--- a/harness/codeverse/languages/urdf/__init__.py
+++ b/harness/codeverse/languages/urdf/__init__.py
@@ -11,7 +11,7 @@ import re
import shutil
import time
import xml.etree.ElementTree as ET
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from pathlib import Path
from typing import Any
@@ -550,7 +550,8 @@ def compute_urdf_frames(plan: ArticulatedPlan) -> UrdfFrames:
lower, upper = j.lower - j.rest, j.upper - j.rest
mim = None
if j.mimic is not None:
- # instanced links get one joint each; each copy follows the same driver
+ # every copy of an instanced follower follows the SAME driver; the driver's own
+ # name is resolved after emission, when the instance suffixes are known
mim = (to_snake(j.mimic.joint), float(j.mimic.multiplier), float(j.mimic.offset))
joints.append(JointRow(name=to_snake(j.name) + suffix, type=j.type, parent=parent, child=child,
origin_xyz=(0.0, 0.0, 0.0), axis=tuple(float(v) for v in j.axis), lower=lower, upper=upper,
@@ -564,6 +565,19 @@ def compute_urdf_frames(plan: ArticulatedPlan) -> UrdfFrames:
for k, c in enumerate(children):
add_joint(j, c, parent, "" if len(children) == 1 else f"_{k + 1}")
+ # A driver whose child is instanced exists only as <name>_1..._n, so the plan's name
+ # names no joint in the file: follow the first instance, the rule an instanced PARENT
+ # link already uses above. The plan validator guarantees the target is a plan joint,
+ # so anything unresolved here is a bug in this function, not in the plan.
+ emitted = {jr.name for jr in joints}
+ for i, jr in enumerate(joints):
+ if jr.mimic is None or jr.mimic[0] in emitted:
+ continue
+ target, mult, off = jr.mimic
+ if f"{target}_1" not in emitted:
+ raise ValueError(f"joint {jr.name}: mimic target {target!r} was not emitted; joints are {sorted(emitted)}")
+ joints[i] = replace(jr, mimic=(f"{target}_1", mult, off))
+
# link frames: root at origin, others at their pivot
for name, p, center in parts:
frame = (0.0, 0.0, 0.0) if name == root else pivot_of.get(name, (0.0, 0.0, 0.0))
diff --git a/harness/codeverse/spatial/joints_poses.py b/harness/codeverse/spatial/joints_poses.py
index ef3ea9fc..7491f44f 100644
--- a/harness/codeverse/spatial/joints_poses.py
+++ b/harness/codeverse/spatial/joints_poses.py
@@ -67,10 +67,15 @@ def pose_label(robot: Robot, q: dict[str, float]) -> str:
def limit_poses(robot: Robot) -> list[tuple[str, dict[str, float]]]:
- """``rest`` + every movable joint at its lower and upper limit (deduped) —
- the pose set used for the articulation contact sheet."""
+ """``rest`` + every INPUT joint at its lower and upper limit (deduped) — the pose set
+ behind the articulation contact sheet and ``joint_sweep(joints=...)``.
+
+ Independent joints only, for the same reason as :func:`pose_samples`: ``fk`` resolves a
+ driven joint from the one it follows and ignores a value handed in for it, so a tile
+ labelled ``<driven>@upper`` would render the rest pose under a label that says it moved
+ (an 8-rib umbrella: 16 mislabelled duplicates)."""
out: list[tuple[str, dict[str, float]]] = [("rest", {})]
- for j in robot.movable_joints():
+ for j in robot.independent_joints():
if j.type == "continuous":
cands = [("half", math.pi / 2)]
else:
diff --git a/harness/codeverse/spatial/mcp_server.py b/harness/codeverse/spatial/mcp_server.py
index 343eef05..d95b5de6 100644
--- a/harness/codeverse/spatial/mcp_server.py
+++ b/harness/codeverse/spatial/mcp_server.py
@@ -106,12 +106,28 @@ def observation_content(obs: Observation) -> list[Any]:
# the slice makes the ceiling hard: truncate's "N chars omitted" marker overshoots its
# own budget by a few characters, and this is the last bound before the vendor's prompt
blocks: list[Any] = [types.TextContent(type="text", text=truncate(text, MAX_TEXT_CHARS)[:MAX_TEXT_CHARS])]
- spent = 0
+ # images[0] is the contact sheet (image_budget keeps it first) and every later image is
+ # a per-view repeat of it, so the budget STOPS at the first one that does not fit rather
+ # than skipping it: dropping the sheet and shipping three views of the same thing would
+ # be the worst of both. Whatever is dropped is said in the text — a FAIL verdict that
+ # silently shipped no picture is the case this guards.
+ kept, spent, dropped = [], 0, 0
for p in obs.images[:max_images_for(obs)]:
b64 = encode_image(p)
- if not b64 or spent + len(b64) > MAX_IMAGE_BYTES:
- continue # pixels are bounded by the downscale; bytes are bounded here
+ if b64 is None:
+ dropped += 1
+ continue
+ if spent + len(b64) > MAX_IMAGE_BYTES:
+ dropped += len(obs.images[:max_images_for(obs)]) - len(kept) - dropped
+ break
spent += len(b64)
+ kept.append(b64)
+ if dropped:
+ blocks[0] = types.TextContent(
+ type="text",
+ text=f"{blocks[0].text}\n[{dropped} image(s) not attached: over the "
+ f"{MAX_IMAGE_BYTES}-byte payload bound or unreadable; the files are in the workspace]")
+ for b64 in kept:
blocks.append(types.ImageContent(type="image", data=b64, mime_type="image/png"))
return blocks
diff --git a/harness/tests/flywheel_cli/test_flywheel.py b/harness/tests/flywheel_cli/test_flywheel.py
index 6275f65c..1d07e1cf 100644
--- a/harness/tests/flywheel_cli/test_flywheel.py
+++ b/harness/tests/flywheel_cli/test_flywheel.py
@@ -115,6 +115,31 @@ def test_a_planted_diff_driver_never_runs(fake_run, tmp_path):
assert files == ["src/blob.bin"] and "src/blob.bin" in text
+def test_a_planted_smudge_filter_never_runs(fake_run, tmp_path):
+ """The same window, the other content-rendering command: ``git archive`` renders
+ blobs through ``convert_to_working_tree``, so a planted ``filter.<name>.smudge``
+ RUNS — and archive has no ``--no-filters``, while ``-c core.attributesFile`` only
+ silences the GLOBAL attributes file. ``read_tree_at`` therefore reads the object
+ database directly (``ls-tree`` + ``cat-file --batch``), which applies no filter.
+
+ Planted with ``.git/info/attributes`` — no commit needed, nothing the harness
+ sanitises on the read path."""
+ ws, _rec = fake_run
+ fired = tmp_path / "smudged"
+ (ws.src / "model.py").write_text("# real content\n")
+ commit = ws.commit("content")
+ (ws.root / ".git" / "info").mkdir(parents=True, exist_ok=True)
+ (ws.root / ".git" / "info" / "attributes").write_text("* filter=evil\n")
+ (ws.root / ".gitattributes").write_text("* filter=evil\n")
+ ws._git("config", "--local", "filter.evil.smudge", f"sh -c 'echo pwned >> {fired}; cat'")
+ ws._git("config", "--local", "filter.evil.required", "false")
+
+ files = _git.read_tree_at(ws, commit)
+
+ assert not fired.exists(), f"a planted smudge filter ran: {fired.read_text()!r}"
+ assert files["src/model.py"] == b"# real content\n" # the raw blob, unfiltered
+
+
def test_diff_between_refuses_a_sha_the_repo_does_not_have(fake_run):
"""The recorded round shas are the only usable handles, and one the repository no
longer holds must raise rather than diff against an empty tree."""
diff --git a/harness/tests/spatial_tools/test_mcp_server.py b/harness/tests/spatial_tools/test_mcp_server.py
index 85d02d3f..f844cad2 100644
--- a/harness/tests/spatial_tools/test_mcp_server.py
+++ b/harness/tests/spatial_tools/test_mcp_server.py
@@ -64,25 +64,40 @@ def test_result_payload_is_bounded_by_outcome(tmp_path: Path) -> None:
assert sum(1 for b in blocks if b.type == "image") == n_images
-def test_image_bytes_are_bounded_not_only_the_count(tmp_path: Path) -> None:
- """The count and the 1024 px downscale bound PIXELS, not bytes: the largest recorded
- articulation sheet still encodes to ~395 k base64 characters, so four of them would be
- a bigger payload than the text cap the same result respects."""
+def test_the_byte_bound_keeps_the_sheet_drops_the_rest_and_says_so(tmp_path: Path) -> None:
+ """The count and the 1024 px downscale bound PIXELS, not bytes. images[0] is the
+ contact sheet and the rest are per-view repeats of it, so the budget must stop at the
+ first image that does not fit — dropping the sheet and shipping the views would be the
+ worst of both — and the text must say what did not come."""
from PIL import Image
from codeverse.spatial.mcp_server import MAX_IMAGE_BYTES, encode_image
- noisy = tmp_path / "noise.png"
- rnd = __import__("random").Random(0)
- img = Image.new("RGB", (1024, 1024))
- img.putdata([(rnd.randrange(256), rnd.randrange(256), rnd.randrange(256)) for _ in range(1024 * 1024)])
- img.save(noisy)
- one = encode_image(str(noisy))
- assert one is not None and len(one) > MAX_IMAGE_BYTES / 3 # a real sheet is this heavy
- blocks = observation_content(Observation(ok=True, text="t", images=[str(noisy)] * 4))
+ def noise(name: str, side: int) -> str:
+ rnd = __import__("random").Random(len(name))
+ img = Image.new("RGB", (side, side))
+ img.putdata([(rnd.randrange(256), rnd.randrange(256), rnd.randrange(256)) for _ in range(side * side)])
+ p = tmp_path / name
+ img.save(p)
+ return str(p)
+
+ small, heavy = noise("small.png", 64), noise("heavy.png", 1024)
+ assert len(encode_image(small) or "") < MAX_IMAGE_BYTES / 4 < len(encode_image(heavy) or "")
+
+ # an in-budget sheet is KEPT (the bug this pins: it used to be skipped for the views)
+ blocks = observation_content(Observation(ok=True, text="t", images=[small, small, small, small]))
+ assert sum(1 for b in blocks if b.type == "image") == 4 and "not attached" not in blocks[0].text
+
+ # the budget stops at the first image that does not fit, and the text says how many
+ blocks = observation_content(Observation(ok=True, text="t", images=[small, heavy, small, small]))
images = [b for b in blocks if b.type == "image"]
- assert sum(len(b.data) for b in images) <= MAX_IMAGE_BYTES
- assert len(images) < 4 # the byte budget, not the count, is what stopped it
+ assert len(images) == 1 and sum(len(b.data) for b in images) <= MAX_IMAGE_BYTES
+ assert "3 image(s) not attached" in blocks[0].text
+
+ # a FAIL verdict ships one image; if that one is over budget it ships none WITH a note
+ blocks = observation_content(Observation(ok=False, text="t", images=[heavy, small]))
+ assert not [b for b in blocks if b.type == "image"]
+ assert "1 image(s) not attached" in blocks[0].text
def test_is_error_is_failed_not_the_verdict(tmp_path: Path) -> None:
diff --git a/harness/tests/urdf_joints/test_mimic.py b/harness/tests/urdf_joints/test_mimic.py
index 970f9935..152f73d3 100644
--- a/harness/tests/urdf_joints/test_mimic.py
+++ b/harness/tests/urdf_joints/test_mimic.py
@@ -11,6 +11,7 @@ from pathlib import Path
import pytest
+from codeverse.contracts.plan import ArticulatedPlan
from codeverse.languages.urdf import lint_workspace, render_urdf
from codeverse.spatial.joints import (
UrdfError,
@@ -84,6 +85,21 @@ def test_pose_samples_drive_only_the_degrees_of_freedom(tmp_path):
assert fk(r, opened)["rib_a"][:3, :3].tolist() != fk(r, {})["rib_a"][:3, :3].tolist()
+def test_the_contact_sheet_and_joint_narrowing_show_only_input_joints(tmp_path):
+ """`limit_poses` feeds the articulation sheet and `joint_sweep(joints=...)`: a tile for
+ a driven joint would be the rest pose with a label saying it moved."""
+ from codeverse.spatial.joints_model import fk
+ from codeverse.spatial.joints_poses import limit_poses
+
+ r = _robot(tmp_path)
+ labels = [lab for lab, _ in limit_poses(r)]
+ assert labels == ["rest", "runner_slide@upper"], labels
+ assert not any("rib_" in lab for lab in labels)
+ # and the one non-rest pose really moves the ribs, through the driver
+ _, q = limit_poses(r)[1]
+ assert fk(r, q)["rib_a"][:3, :3].tolist() != fk(r, {})["rib_a"][:3, :3].tolist()
+
+
def test_the_sweep_poses_the_mechanism_the_way_it_moves(tmp_path):
r = _robot(tmp_path)
rep = sweep_collisions(r, pose_samples(r))
@@ -132,3 +148,72 @@ def test_the_lint_accepts_a_real_coupling_and_names_a_broken_one(tmp_path, monke
(ws.src / "robot.urdf").write_text(RIB.replace('joint="runner_slide" multiplier="4"', 'joint="ghost" multiplier="4"'))
errs = [f for f in lint_workspace(ws).findings if "mimic" in f.message and f.severity.value == "error"]
assert len(errs) == 1 and "names no joint" in errs[0].message
+
+
+# ------------------------------------------------------------------ the plan's own rules
+def _plan(**joint_overrides):
+ """The worked articulated example with a second joint that can carry a coupling."""
+ import copy
+
+ from codeverse.contracts.common import Track
+ from codeverse.tracks.planner import plan_example
+
+ d = copy.deepcopy(plan_example(Track.ARTICULATED_OBJECT))
+ d["parts"].append({"name": "Lid", "role": "top lid", "description": "a flat lid", "attach_to": "Cabinet",
+ "bbox": {"center": [0, 0, 0.61], "extents": [0.4, 0.5, 0.02]}, "material": "oak"})
+ d["joints"].append({"name": "LidHinge", "type": "revolute", "parent": "Cabinet", "child": "Lid",
+ "axis": [1, 0, 0], "pivot": [0, 0.21, 0.6], "lower": 0.0, "upper": 1.2, "rest": 0.0,
+ "motion": "the lid tilts up"})
+ d["joints"][1].update(joint_overrides)
+ return d
+
+
+def test_a_declared_coupling_validates_and_survives_the_round_trip():
+ d = _plan(mimic={"joint": "DrawerSlide", "multiplier": 2.0, "offset": 0.1})
+ plan = ArticulatedPlan.model_validate(d)
+ assert plan.joints[1].mimic is not None and plan.joints[1].mimic.joint == "DrawerSlide"
+ assert plan.joints[1].mimic.multiplier == 2.0 and plan.joints[1].mimic.offset == 0.1
+
+
[email protected]("mimic,msg", [
+ ({"joint": "NoSuchJoint"}, "is not a joint in this plan"),
+ ({"joint": "LidHinge"}, "mimics itself"),
+ ({"joint": "DrawerSlide", "multiplier": 0.0}, "multiplier 0"),
+])
+def test_a_broken_coupling_is_refused_by_the_plan(mimic, msg):
+ with pytest.raises(Exception, match=msg):
+ ArticulatedPlan.model_validate(_plan(mimic=mimic))
+
+
+def test_a_coupling_that_names_a_fixed_joint_is_refused():
+ d = _plan(mimic={"joint": "CabinetWeld"})
+ d["parts"].append({"name": "Plinth", "role": "base", "description": "a plinth", "attach_to": "Cabinet",
+ "bbox": {"center": [0, 0, -0.05], "extents": [0.4, 0.5, 0.1]}, "material": "oak"})
+ d["joints"].append({"name": "CabinetWeld", "type": "fixed", "parent": "Cabinet", "child": "Plinth",
+ "axis": [0, 0, 1], "pivot": [0, 0, 0], "lower": 0.0, "upper": 0.0, "motion": "rigid"})
+ with pytest.raises(Exception, match="which is fixed and never moves"):
+ ArticulatedPlan.model_validate(d)
+
+
+def test_a_mimic_cycle_is_refused():
+ d = _plan(mimic={"joint": "DrawerSlide"})
+ d["joints"][0]["mimic"] = {"joint": "LidHinge"}
+ with pytest.raises(Exception, match="loops back through"):
+ ArticulatedPlan.model_validate(d)
+
+
+def test_a_mimic_of_an_instanced_driver_follows_the_first_instance():
+ """An instanced driver exists only as ``<name>_1..._n``; writing the plan's bare name
+ made the skeleton fail its own lint and `load_urdf` (a scissor/pantograph plan is the
+ likely place to hit it)."""
+ from codeverse.languages.urdf import compute_urdf_frames
+
+ d = _plan()
+ d["parts"][1]["instances"] = 2 # the drawer, driven by DrawerSlide
+ d["joints"][1]["mimic"] = {"joint": "DrawerSlide", "multiplier": 0.5}
+ urdf = render_urdf(compute_urdf_frames(ArticulatedPlan.model_validate(d)))
+ assert '<mimic joint="drawer_slide_1" multiplier="0.5" offset="0"/>' in urdf
+ assert "drawer_slide_1" in urdf and "drawer_slide_2" in urdf
+ names = {ln.split('"')[1] for ln in urdf.splitlines() if "<joint name=" in ln}
+ refs = {ln.split('"')[1] for ln in urdf.splitlines() if "<mimic joint=" in ln}
+ assert refs <= names, f"dangling mimic reference: {refs - names}"
diff --git a/harness/tests/urdf_joints/test_sweep.py b/harness/tests/urdf_joints/test_sweep.py
index 37359337..6322250f 100644
--- a/harness/tests/urdf_joints/test_sweep.py
+++ b/harness/tests/urdf_joints/test_sweep.py
@@ -162,6 +162,7 @@ def test_joints_argument_narrows_the_render_to_those_joints(monkeypatch, tmp_pat
class _Robot:
joints = {n: _J(n, -1.0, 1.0) for n in ("hinge", "slide", "knob")}
def movable_joints(self): return list(self.joints.values())
+ def independent_joints(self): return list(self.joints.values()) # no coupling in this fake
full = ts._poses_for(_Robot(), None)
assert full is None, "no filter = the full articulation sheet, unchanged"