Official Codebase of 3DCodeVerse
Python
bench: an A/B launched from a worktree ran the MAIN tree's harness in both arms, and looked healthy doing it
gaoypengde28e730fe8b
diff --git a/harness/bench/ab_plan.py b/harness/bench/ab_plan.pyindex b79ab634..b8577438 100644--- a/harness/bench/ab_plan.py+++ b/harness/bench/ab_plan.py@@ -57,12 +57,41 @@ from typing import NamedTuplefrom pydantic import BaseModel, Field-from codeverse._compat import UTC-+# ---------------------------------------------------------------------------------------+# sys.path FIRST, and before any `codeverse` import. `spawn_cell` starts each child as a+# FILE path, so `sys.path[0]` is `bench/` and the cwd is NOT on the path; an editable+# install (`__editable__.3dcodeverse-...pth`) then resolves `import codeverse` to whatever+# tree it was installed from. Importing `codeverse._compat` above this line made every+# child of a worktree run the MAIN tree's harness, both arms identically, and the A/B+# measured nothing while looking completely healthy — it happened twice on 2026-08-25+# (`bench/out/plan_loop/C0/invalid_attempt1_maintree_import`, and again to the skills wave).REPO = Path(__file__).resolve().parents[1]if str(REPO) not in sys.path: # `python bench/ab_plan.py` from anywheresys.path.insert(0, str(REPO))+from codeverse._compat import UTC # noqa: E402+++def _assert_local_codeverse() -> None:+ """Refuse to run against a `codeverse` from a different tree.++ A workaround in a launch script (`export PYTHONPATH=...`) protects the person who+ remembers it. This protects the run.+ """+ import codeverse++ got = Path(codeverse.__file__).resolve().parent+ want = REPO / "codeverse"+ if got != want:+ raise SystemExit(+ f"refusing to run: `import codeverse` resolved to {got}, not {want}.\n"+ f" An editable install is shadowing this tree, so both arms would run the same\n"+ f" code and the A/B would measure nothing. Launch with:\n"+ f" PYTHONPATH={REPO} python bench/ab_plan.py ...")+++_assert_local_codeverse()+from bench._ab_report import ARMS, CONTROL, VARIANT, Verdict, write_report # noqa: E402from bench._compare_report import CellResult, load_jsonl # noqa: E402from bench._infra import is_infra_failure # noqa: E402diff --git a/harness/bench/compare_backends.py b/harness/bench/compare_backends.pyindex 416fe54a..407ab53e 100644--- a/harness/bench/compare_backends.py+++ b/harness/bench/compare_backends.py@@ -45,8 +45,9 @@ from typing import Any, Literalfrom pydantic import BaseModel, Field-from codeverse._compat import UTC-+# sys.path BEFORE any `codeverse` import: this file is also run as a script, and an+# editable install would otherwise resolve `codeverse` to the tree it was installed from+# rather than this one. See the same note in `bench/ab_plan.py`.REPO = Path(__file__).resolve().parents[1]if str(REPO) not in sys.path: # `python bench/compare_backends.py` from anywheresys.path.insert(0, str(REPO))@@ -76,6 +77,7 @@ from bench.run_bench import ( # noqa: E402default_run_track,select_prompts,)+from codeverse._compat import UTC # noqa: E402from codeverse.config import get_settings # noqa: E402from codeverse.contracts.artifacts import RenderSet # noqa: E402from codeverse.contracts.run import RunRecord # noqa: E402diff --git a/harness/tests/compare_bench/test_worktree_import.py b/harness/tests/compare_bench/test_worktree_import.pynew file mode 100644index 00000000..a1c44fbf--- /dev/null+++ b/harness/tests/compare_bench/test_worktree_import.py@@ -0,0 +1,61 @@+"""A bench script run from a worktree must use THAT worktree's `codeverse`.++WHY this is worth a test of its own: it has silently voided two A/B runs.+``ab_plan.spawn_cell`` starts each child as a FILE path, so ``sys.path[0]`` is ``bench/``+and the cwd is not on the path. Any ``import codeverse`` before the script's own+``sys.path`` bootstrap therefore resolves through whatever editable install is present+(``__editable__.3dcodeverse-<v>.pth`` installs a meta-path finder pinned to the tree it was+installed from). Both arms then run the same foreign code, every switch under test is+inert, and the report looks completely healthy — the failure has no symptom at all.++Caught on 2026-08-25 in `bench/out/plan_loop/C0/invalid_attempt1_maintree_import` (worked+around in a launch script) and again in the skills wave's first A/B, where the variant arm+ran a tree with no skills package and materialised nothing. A launch-script workaround+protects whoever remembers it; these two checks protect the run.+"""++from __future__ import annotations++import re+from pathlib import Path++import pytest++HARNESS = Path(__file__).resolve().parents[2]+BENCH = HARNESS / "bench"+#: every bench module that bootstraps sys.path because it is also run as a script+SCRIPTS = sorted(p for p in BENCH.glob("*.py") if "sys.path.insert" in p.read_text())+++def test_there_is_at_least_one_such_script():+ assert SCRIPTS, "no bench script bootstraps sys.path any more — has the layout changed?"++[email protected]("script", SCRIPTS, ids=[p.name for p in SCRIPTS])+def test_the_sys_path_bootstrap_comes_before_any_codeverse_import(script: Path):+ text = script.read_text()+ boot = text.index("sys.path.insert")+ first = re.search(r"^\s*(?:from|import)\s+codeverse\b", text, re.M)+ if first is None:+ pytest.skip(f"{script.name} imports no codeverse")+ assert first.start() > boot, (+ f"{script.name} imports codeverse at line {text[:first.start()].count(chr(10)) + 1}, "+ f"before its sys.path bootstrap at line {text[:boot].count(chr(10)) + 1}. A child "+ f"spawned by file path would resolve codeverse through the editable install instead "+ f"of this tree, and the A/B would compare a tree against itself.")+++def test_ab_plan_refuses_to_run_against_a_foreign_codeverse():+ """The belt as well as the braces: ordering can regress, the guard cannot be silent."""+ text = (BENCH / "ab_plan.py").read_text()+ assert "_assert_local_codeverse" in text+ assert "refusing to run" in text+++def test_the_guard_actually_compares_the_resolved_package_to_this_tree():+ import codeverse+ from bench.ab_plan import REPO, _assert_local_codeverse++ assert Path(codeverse.__file__).resolve().parent == REPO / "codeverse", (+ "the test suite itself is importing a foreign codeverse")+ _assert_local_codeverse() # must not raise in a correctly-resolved tree