Skip to content

Official Codebase of 3DCodeVerse

Python

tools: the image path gets a real byte bound, and max_images_for gets an honest reason

ziyao6971e85278f5
diff --git a/harness/codeverse/spatial/mcp_server.py b/harness/codeverse/spatial/mcp_server.py
index 784eec95..343eef05 100644
--- a/harness/codeverse/spatial/mcp_server.py
+++ b/harness/codeverse/spatial/mcp_server.py
@@ -8,9 +8,10 @@ observation text (+ compact numbers when small) and one ``ImageContent`` per
observation image (base64 PNG, long side ≤ 1024 px, at most ``MAX_IMAGES``).
``is_error`` is ``Observation.failed`` (the tool could not run) — never a
negative verdict, which is an ordinary result whose text leads with FAIL.
-Every result is bounded here — ``MAX_TEXT_CHARS`` of text and a per-outcome image
-budget (:func:`max_images_for`) — because this is the last place the harness owns
-before the payload becomes the vendor's prompt.
+Every result is bounded here — ``MAX_TEXT_CHARS`` of text, at most
+:func:`max_images_for` images, and ``MAX_IMAGE_BYTES`` of encoded image data across
+them — because this is the last place the harness owns before the payload becomes
+the vendor's prompt.
Track / language default to ``<ws>/spec.json`` so agents need no flags.
"""
@@ -40,6 +41,11 @@ MAX_NUMBERS_CHARS = 1200
#: ``compare_reference``) never goes through those, and nothing may hand the model an
#: unbounded payload from here.
MAX_TEXT_CHARS = 6000
+#: ceiling on the encoded image data of ONE result. The count and the 1024 px long side
+#: bound pixels, not bytes: the largest recorded articulation sheet (1,966,609 B,
+#: 1176x3350) still encodes to 394,988 base64 characters after the downscale, so without
+#: this a legal result could hand the model ~280 k tokens of image.
+MAX_IMAGE_BYTES = 400_000
SERVER_NAME = "3dcv"
@@ -68,15 +74,21 @@ def encode_image(path: str, max_side: int = MAX_IMAGE_SIDE) -> str | None:
def max_images_for(obs: Observation) -> int:
- """Image budget for ONE result, by outcome — the guard on the error path.
-
- A vendor CLI that receives ``is_error`` stringifies the WHOLE result, image parts
- included: gemini-cli's error branch prints ``safeJsonStringify(rawResponseParts)``,
- so a 275 kB contact sheet arrives as ~366 k characters of base64 TEXT (~261 k prompt
- tokens) instead of ~516 as an inline image, and its own 40 000-char truncation does
- not fire for a multi-part MCP result (docs/COST.md §30). So a failed observation
- ships no image (nothing rendered is evidence about a tool that could not run), and a
- FAIL verdict ships one — the sheet, which ``image_budget`` keeps first.
+ """Image budget for ONE result, by outcome.
+
+ A tool that could not run ships no image: nothing was rendered that is evidence
+ about it, and a vendor CLI that receives ``is_error`` stringifies the WHOLE result,
+ image parts included — gemini-cli's error branch prints
+ ``safeJsonStringify(rawResponseParts)``, so a 275 kB contact sheet would arrive as
+ ~366 k characters of base64 TEXT (~261 k prompt tokens) instead of ~516 as an inline
+ image, and its own 40 000-char truncation does not fire for a multi-part MCP result
+ (docs/COST.md §30). A FAIL verdict is NOT an error here (``is_error`` is
+ ``obs.failed``), so it takes the vendor's ordinary image path; it still ships one
+ image rather than four because the first is the sheet ``image_budget`` keeps, the
+ rest are per-view repeats of a result the agent is being told to fix, and the
+ one-image rule is also what keeps the blow-up bounded if ``is_error`` is ever
+ re-coupled to ``not ok`` (``test_is_error_is_failed_not_the_verdict`` pins that
+ coupling).
"""
if obs.failed:
return 0
@@ -94,10 +106,13 @@ 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
for p in obs.images[:max_images_for(obs)]:
b64 = encode_image(p)
- if b64:
- blocks.append(types.ImageContent(type="image", data=b64, mime_type="image/png"))
+ if not b64 or spent + len(b64) > MAX_IMAGE_BYTES:
+ continue # pixels are bounded by the downscale; bytes are bounded here
+ spent += len(b64)
+ blocks.append(types.ImageContent(type="image", data=b64, mime_type="image/png"))
return blocks
diff --git a/harness/docs/COST.md b/harness/docs/COST.md
index 43074d2c..22c2f3b4 100644
--- a/harness/docs/COST.md
+++ b/harness/docs/COST.md
@@ -1380,8 +1380,10 @@ come from.
**verdict** — so every gate that ran and answered FAIL reached the model as a broken call.
**Selector** (every number in this section, unless another one is named): the files
-`bench/out/*/**/run/trajectories/*/stdout.json`, 386 of them, of which **224 are
-non-empty** — one per gemini-cli session that reported stats; the other 162 are
+`bench/out/*/**/run/trajectories/*/stdout.json` **as they stood at 2026-09-02 20:20 UTC**
+— the corpus is live and grows with every battery, so a later re-run reads larger
+numbers (2026-09-03 00:30: 390 files, 6 154 calls, 60 of 302 rounds) — 386 of them, of
+which **224 are non-empty** — one per gemini-cli session that reported stats; the other 162 are
zero-byte (the CLI died or was killed before printing its JSON) and every one of the
386 belongs to a gemini-cli arm. `run/telemetry/trajectories` is a symlink to the same directory:
count it once (a glob that follows it doubles every number). Per file, sum
diff --git a/harness/tests/spatial_tools/test_mcp_server.py b/harness/tests/spatial_tools/test_mcp_server.py
index 4bfa23d0..85d02d3f 100644
--- a/harness/tests/spatial_tools/test_mcp_server.py
+++ b/harness/tests/spatial_tools/test_mcp_server.py
@@ -64,6 +64,39 @@ 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."""
+ 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))
+ 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
+
+
+def test_is_error_is_failed_not_the_verdict(tmp_path: Path) -> None:
+ """The one coupling every other bound assumes: a FAIL verdict is an ordinary result,
+ so the vendor sends its images as images. Re-couple this to ``not obs.ok`` and the
+ base64-as-text blow-up of docs/COST.md §30 comes back."""
+ import inspect
+
+ from codeverse.spatial import mcp_server
+
+ src = inspect.getsource(mcp_server.make_server)
+ assert "is_error=obs.failed" in src and "is_error=not obs.ok" not in src
+
+
def test_cli_list(stool_ctx: ToolContext, capsys: pytest.CaptureFixture[str]) -> None:
assert main(["--workspace", str(stool_ctx.workspace.root), "--list"]) == 0
data = json.loads(capsys.readouterr().out)