Official Codebase of 3DCodeVerse
Python
materialize: takes the catalog's cookbook text — its own workspace-first path resolver goes, so an agent-written file can no longer shadow the harness cookbook
gaoypeng4787985ce6e1
diff --git a/harness/codeverse3d/agents/materialize.py b/harness/codeverse3d/agents/materialize.pyindex 9d0d7a21..678687c8 100644--- a/harness/codeverse3d/agents/materialize.py+++ b/harness/codeverse3d/agents/materialize.py@@ -35,11 +35,9 @@ from __future__ import annotationsimport jsonimport logging-import shutilfrom pathlib import Pathfrom codeverse3d.agents.cli_common import default_mcp_command-from codeverse3d.prompts import PROMPTS_DIRfrom codeverse3d.workspace import Workspacelog = logging.getLogger(__name__)@@ -192,22 +190,13 @@ def codex_mcp_overrides(mcp_command: list[str]) -> list[str]:]-def _resolve_cookbook(ws: Workspace, cookbook_rel: str) -> Path | None:- if not cookbook_rel:- return None- for cand in (ws.root / cookbook_rel, PROMPTS_DIR / cookbook_rel, Path(cookbook_rel)):- if cand.is_file():- return cand- return None--# --------------------------------------------------------------------------- entrydef materialize_workspace(ws: Workspace,*,agent_kind: str,contract_md: str,- cookbook_rel: str,+ cookbook_text: str,spatial_tools: bool,mcp_command: list[str] | None = None,) -> None:@@ -223,22 +212,25 @@ def materialize_workspace(a cookbook that did not resolve, was written and read by nobody. That is the exactfailure ``tracks/common.cookbook_rel_for`` was fixed for on 2026-08-29 (an articulatedrun told the agent "No cookbook is available" while its 24 kB cookbook sat on disk);- it is a log line now, where someone reading the run can see it (2026-08-30)."""+ it is a log line now, where someone reading the run can see it (2026-08-30).++ ``cookbook_text`` is what the catalog loaded (``RunContext.cookbook_text``): until+ 2026-09-22 this took a path and resolved it itself, workspace first — so a+ ``<ws>/blender/cookbook.md`` the agent wrote shadowed the harness cookbook."""mcp_command = list(mcp_command) if mcp_command else default_mcp_command(ws)# cookbook: copy into the harness-owned .3dcode/ dir so every CLI can read it in-workspace- src = _resolve_cookbook(ws, cookbook_rel)- if src is not None:+ if cookbook_text:dest = ws.root / C3D_DIR / "cookbook.md"dest.parent.mkdir(parents=True, exist_ok=True)- shutil.copyfile(src, dest)+ dest.write_text(cookbook_text, encoding="utf-8")cookbook_note = (f"The cookbook for this language — copyable, verified snippets and skeletons — is at "f"`{C3D_DIR}/cookbook.md` (relative to the workspace root). Read the relevant sections ""before writing code and copy its patterns exactly.")else:- log.warning("cookbook not found: %r — the %s session gets the contract only", cookbook_rel, agent_kind)+ log.warning("cookbook not found — the %s session gets the contract only", agent_kind)cookbook_note = "No cookbook is available in this session; rely on the contract below."body = _body(agent_kind, contract_md, cookbook_note, spatial_tools, mcp_command, ws)diff --git a/harness/codeverse3d/tracks/candidates.py b/harness/codeverse3d/tracks/candidates.pyindex feb4cd32..9f9b3ca4 100644--- a/harness/codeverse3d/tracks/candidates.py+++ b/harness/codeverse3d/tracks/candidates.py@@ -147,7 +147,7 @@ def make_candidate_context(track: Any, ctx: RunContext, k: int) -> RunContext:shutil.copyfile(path, sub_ws.root / path.name)if not ctx.single_shot:ctx.services.materialize(sub_ws, agent_kind=ctx.agent_kind, contract_md=track.agent_contract_md(ctx),- cookbook_rel=ctx.cookbook_rel, spatial_tools=True)+ cookbook_text=ctx.cookbook_text, spatial_tools=True)sub_ws.commit("skeleton")# own event log (the run log keeps only candidate.*) and a one-sample judge for rankingreturn replace(ctx, ws=sub_ws, extra={}, events=EventLog(sub_ws.events_path),diff --git a/harness/codeverse3d/tracks/common.py b/harness/codeverse3d/tracks/common.pyindex 8cda99ff..8fd41075 100644--- a/harness/codeverse3d/tracks/common.py+++ b/harness/codeverse3d/tracks/common.py@@ -116,9 +116,9 @@ class Services:return default_joint_sweep(ws, plan, out_dir)# ---- agents' workspace materialisation + tool cards- def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_rel: str, spatial_tools: bool) -> None:+ def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_text: str, spatial_tools: bool) -> None:_import("codeverse3d.agents.materialize", "materialize_workspace")(- ws, agent_kind=agent_kind, contract_md=contract_md, cookbook_rel=cookbook_rel, spatial_tools=spatial_tools)+ ws, agent_kind=agent_kind, contract_md=contract_md, cookbook_text=cookbook_text, spatial_tools=spatial_tools)def tool_cards(self, track: str, language: str) -> str:try:diff --git a/harness/codeverse3d/tracks/lifecycle.py b/harness/codeverse3d/tracks/lifecycle.pyindex 7414c75d..2870881b 100644--- a/harness/codeverse3d/tracks/lifecycle.py+++ b/harness/codeverse3d/tracks/lifecycle.py@@ -439,7 +439,7 @@ class BaseTrack:kind = ctx.agent_kindif ctx.state.materialized_for == kind:return- self.services.materialize(ctx.ws, agent_kind=kind, contract_md=self.agent_contract_md(ctx), cookbook_rel=ctx.cookbook_rel,+ self.services.materialize(ctx.ws, agent_kind=kind, contract_md=self.agent_contract_md(ctx), cookbook_text=ctx.cookbook_text,spatial_tools=True)ctx.state.materialized_for = kindctx.state.save(ctx.ws)diff --git a/harness/codeverse3d/tracks/scene_assets.py b/harness/codeverse3d/tracks/scene_assets.pyindex a346b473..f55e73ce 100644--- a/harness/codeverse3d/tracks/scene_assets.py+++ b/harness/codeverse3d/tracks/scene_assets.py@@ -409,7 +409,7 @@ def build_blender_asset(ctx: RunContext, asset: AssetPlan, *, judge: bool) -> Aswrite_blender_skeleton(sub_ws, sub.plan, ground_tol_m=HERO_GROUND_TOL_M)sub_ws.commit("skeleton")if not ctx.single_shot:- ctx.services.materialize(sub_ws, agent_kind=ctx.agent_kind, contract_md=sub.contract_text, cookbook_rel=sub.cookbook_rel,+ ctx.services.materialize(sub_ws, agent_kind=ctx.agent_kind, contract_md=sub.contract_text, cookbook_text=sub.cookbook_text,spatial_tools=True)files = sub.runtime.expected_files(sub.plan)timeout_s = asset_timeout_s(ctx, 180)diff --git a/harness/docs/INTERFACES.md b/harness/docs/INTERFACES.mdindex edcfe486..c982ff14 100644--- a/harness/docs/INTERFACES.md+++ b/harness/docs/INTERFACES.md@@ -170,10 +170,12 @@ Every run is read from its ledger alone (`addons.costreport.audit.read_run` / `r```pythonfrom codeverse3d.agents import get_coding_agent # (agent_id) -> CodingAgent .run(job) .available() .id .kind .modelfrom codeverse3d.agents.materialize import materialize_workspace, codex_mcp_overrides-materialize_workspace(ws, *, agent_kind, contract_md, cookbook_rel, spatial_tools, mcp_command=None) -> None+materialize_workspace(ws, *, agent_kind, contract_md, cookbook_text, spatial_tools, mcp_command=None) -> None+# cookbook_text = RunContext.cookbook_text, the catalog's text (Δ 2026-09-22: was cookbook_rel, resolved workspace-first,+# so an agent-written <ws>/<lang>/cookbook.md shadowed the harness cookbook); "" → "No cookbook is available" + a warning# mcp_command defaults to cli_common.default_mcp_command(ws) = [sys.executable, -m codeverse3d.spatial.mcp_server --workspace …]# (None/[] = the default — no ValueError); tracks/common.Services.materialize takes no mcp_command-# writes AGENTS.md + GEMINI.md + CLAUDE.md (same body), ws/.3dcode/cookbook.md (Δ copied in: gemini-cli cannot read+# writes AGENTS.md + GEMINI.md + CLAUDE.md (same body), ws/.3dcode/cookbook.md (Δ written in: gemini-cli cannot read# outside the workspace), .geminiignore/.aiexclude, and MCP wiring:# gemini-cli → ws/.gemini/settings.json only gets context.fileFiltering.respectGitIgnore=false; the 3dcode server# and mcp.allowed live in the per-session system settings (Δ that file is agent-writable — audit 2026-08-27)diff --git a/harness/tests/agents/test_claude_code.py b/harness/tests/agents/test_claude_code.pyindex eccadd28..b6ab8ad3 100644--- a/harness/tests/agents/test_claude_code.py+++ b/harness/tests/agents/test_claude_code.py@@ -85,7 +85,7 @@ def test_parse_envelope_variants():def test_argv_includes_mcp_when_materialized(tmp_ws: Workspace):- materialize_workspace(tmp_ws, agent_kind="claude-code", contract_md="c", cookbook_rel="", spatial_tools=True,+ materialize_workspace(tmp_ws, agent_kind="claude-code", contract_md="c", cookbook_text="", spatial_tools=True,mcp_command=["python", "-m", "codeverse3d.spatial.mcp_server", "--workspace", str(tmp_ws.root)])a = ClaudeCodeAgent("sonnet", binary="claude")s = begin_session(AgentJob(workspace=str(tmp_ws.root), prompt="p", system_append="S", max_turns=7,diff --git a/harness/tests/agents/test_cli_common.py b/harness/tests/agents/test_cli_common.pyindex e0e6433f..b72f1874 100644--- a/harness/tests/agents/test_cli_common.py+++ b/harness/tests/agents/test_cli_common.py@@ -162,7 +162,7 @@ def test_mcp_command_resolution(tmp_ws: Workspace):assert mcp_command_for(tmp_ws, job2) == ["python", "-m", "x"]# a workspace .mcp.json NEVER wins: the agent works in that directory and could# otherwise choose what the next round's CLI launches (audit 2026-08-27)- materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_rel="", spatial_tools=True,+ materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_text="", spatial_tools=True,mcp_command=default_mcp_command(tmp_ws, language="cadquery"))assert mcp_command_for(tmp_ws, job2) == ["python", "-m", "x"] # still the typed job(tmp_ws.root / ".mcp.json").write_text(json.dumps(diff --git a/harness/tests/agents/test_codex.py b/harness/tests/agents/test_codex.pyindex ed183587..a3e1ad9f 100644--- a/harness/tests/agents/test_codex.py+++ b/harness/tests/agents/test_codex.py@@ -141,9 +141,10 @@ def test_live_codex_mcp_tool_call_is_not_cancelled(tmp_ws: Workspace):from codeverse3d.agents.cli_common import default_mcp_commandfrom codeverse3d.agents.materialize import materialize_workspace+ from codeverse3d.prompts import load_texttrimesh.creation.box().export(tmp_ws.artifacts / "object.glb")- materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_rel="threejs/cookbook.md", spatial_tools=True,+ materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_text=load_text("threejs/cookbook.md"), spatial_tools=True,mcp_command=default_mcp_command(tmp_ws, language="threejs"))a = CodexAgent("")res = a.run(AgentJob(workspace=str(tmp_ws.root), prompt="Call the 3dcode MCP tool `measure` exactly once, then reply with the "diff --git a/harness/tests/agents/test_gemini_cli.py b/harness/tests/agents/test_gemini_cli.pyindex 27696b0b..c8bbe0a9 100644--- a/harness/tests/agents/test_gemini_cli.py+++ b/harness/tests/agents/test_gemini_cli.py@@ -221,7 +221,7 @@ def test_agent_planted_mcp_server_never_reaches_the_cli(tmp_ws: Workspace, agentfrom codeverse3d.agents.cli_common import begin_session, default_mcp_command, release_sessionfrom codeverse3d.agents.materialize import materialize_workspace- materialize_workspace(tmp_ws, agent_kind="gemini-cli", contract_md="c", cookbook_rel="",+ materialize_workspace(tmp_ws, agent_kind="gemini-cli", contract_md="c", cookbook_text="",spatial_tools=True, mcp_command=default_mcp_command(tmp_ws))planted = tmp_ws.root / ".gemini" / "settings.json"data = json.loads(planted.read_text())diff --git a/harness/tests/agents/test_materialize.py b/harness/tests/agents/test_materialize.pyindex e6c7f540..571c5f2a 100644--- a/harness/tests/agents/test_materialize.py+++ b/harness/tests/agents/test_materialize.py@@ -13,8 +13,8 @@ from codeverse3d.workspace import WorkspaceCONTRACT = "## blender contract\nWrite pure bpy into src/model.py."-def _mat(ws: Workspace, kind: str = "gemini-cli", spatial: bool = True, cookbook: str = "missing/cookbook.md"):- return materialize_workspace(ws, agent_kind=kind, contract_md=CONTRACT, cookbook_rel=cookbook,+def _mat(ws: Workspace, kind: str = "gemini-cli", spatial: bool = True, cookbook: str = ""):+ return materialize_workspace(ws, agent_kind=kind, contract_md=CONTRACT, cookbook_text=cookbook,spatial_tools=spatial, mcp_command=default_mcp_command(ws))@@ -105,12 +105,10 @@ def test_spatial_disabled_drops_server_and_documents_absence(tmp_ws: Workspace):assert "No spatial tools are available" in (tmp_ws.root / "AGENTS.md").read_text()-def test_cookbook_copied_when_found(tmp_ws: Workspace, tmp_path, caplog):- cb = tmp_path / "cookbook.md"- cb.write_text("# cookbook\nsnippet")+def test_cookbook_copied_when_found(tmp_ws: Workspace, caplog):with caplog.at_level(logging.WARNING, logger="codeverse3d.agents.materialize"):- _mat(tmp_ws, cookbook=str(cb))- assert (tmp_ws.root / C3D_DIR / "cookbook.md").read_text().startswith("# cookbook")+ _mat(tmp_ws, cookbook="# cookbook\nsnippet")+ assert (tmp_ws.root / C3D_DIR / "cookbook.md").read_text() == "# cookbook\nsnippet"assert f"{C3D_DIR}/cookbook.md" in (tmp_ws.root / "AGENTS.md").read_text()assert not [r for r in caplog.records if r.name == "codeverse3d.agents.materialize"], \"a resolved cookbook must not warn"@@ -122,9 +120,8 @@ def test_missing_cookbook_warns_where_someone_can_see_it(tmp_ws: Workspace, caplon disk). It used to land in a `Materialized.warnings` list every caller threw away; thelog line is the whole signal now, so it has to fire."""with caplog.at_level(logging.WARNING, logger="codeverse3d.agents.materialize"):- _mat(tmp_ws, cookbook="nope/cookbook.md")- assert any("cookbook not found" in r.getMessage() and "nope/cookbook.md" in r.getMessage()- for r in caplog.records)+ _mat(tmp_ws, cookbook="")+ assert any("cookbook not found" in r.getMessage() for r in caplog.records)assert "No cookbook is available" in (tmp_ws.root / "AGENTS.md").read_text()@@ -147,7 +144,7 @@ def test_codex_overrides_are_valid_toml_fragments():def test_default_mcp_command_is_the_backends_interpreter(tmp_ws: Workspace):# one place knows the command: the body + codex overrides quote sys.executable, never bare "python"- materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_rel="", spatial_tools=True)+ materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_text="", spatial_tools=True)body = (tmp_ws.root / "AGENTS.md").read_text()assert default_mcp_command(tmp_ws)[0] == sys.executableassert f"command: `{' '.join(default_mcp_command(tmp_ws))}`" in bodydiff --git a/harness/tests/orchestrator_tracks/fakes.py b/harness/tests/orchestrator_tracks/fakes.pyindex bb893c00..049038c4 100644--- a/harness/tests/orchestrator_tracks/fakes.py+++ b/harness/tests/orchestrator_tracks/fakes.py@@ -378,7 +378,7 @@ class FakeServices(Services):fix_hint=f"shrink {j.child} by 17 mm along the axis") for j in list(getattr(plan, "joints", []))[: self.sweep_errors]]return GateReport(gate="joint_sweep", passed=not findings, findings=findings), views- def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_rel: str, spatial_tools: bool) -> None:+ def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_text: str, spatial_tools: bool) -> None:(ws.root / "AGENTS.md").write_text(contract_md)self.materialized.append(agent_kind)diff --git a/harness/tests/prompts/manifest.py b/harness/tests/prompts/manifest.pyindex 1e6405b6..db0c74ef 100644--- a/harness/tests/prompts/manifest.py+++ b/harness/tests/prompts/manifest.py@@ -204,8 +204,8 @@ class ManifestServices(FakeServices):def tool_cards(self, track: str, language: str) -> str:return Services.tool_cards(self, track, language)- def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_rel: str, spatial_tools: bool) -> None:- Services.materialize(self, ws, agent_kind=agent_kind, contract_md=contract_md, cookbook_rel=cookbook_rel,+ def materialize(self, ws: Workspace, *, agent_kind: str, contract_md: str, cookbook_text: str, spatial_tools: bool) -> None:+ Services.materialize(self, ws, agent_kind=agent_kind, contract_md=contract_md, cookbook_text=cookbook_text,spatial_tools=spatial_tools)self.materialized.append(agent_kind)self.contexts.append((ws.root, {n: (ws.root / n).read_text() for n in CONTEXT_FILES if (ws.root / n).is_file()}))