Official Codebase of 3DCodeVerse
Python
flywheel/gallery: one collision-proof RunId for every run
gaoypeng3dc9b200435f
diff --git a/harness/bench/complexity_report.py b/harness/bench/complexity_report.pyindex 785b10b8..709624f3 100644--- a/harness/bench/complexity_report.py+++ b/harness/bench/complexity_report.py@@ -30,6 +30,8 @@ from typing import Anysys.path.insert(0, str(Path(__file__).resolve().parents[0].parent))+from codeverse.contracts.run import RunId # noqa: E402+from codeverse.flywheel.record import find_run_dirs # noqa: E402from codeverse.proc import read_json_or_none # noqa: E402from codeverse.spatial.complexity import COMPLEXITY_WEIGHTS, ComplexityVector, band_of # noqa: E402@@ -102,7 +104,7 @@ def _minutes(record: dict[str, Any]) -> float:return round(sum(float(r.get("duration_s") or 0.0) for r in record.get("rounds") or []) / 60.0, 2)-def row_for(run: Path, battery: str) -> Row | None:+def row_for(run: Path, battery: str, slug: str | None = None) -> Row | None:record = _read_json(run / "record.json")if not record:return None@@ -115,7 +117,7 @@ def row_for(run: Path, battery: str) -> Row | None:plan_parts = len((record.get("plan") or {}).get("parts") or [])row = Row(battery=battery,- slug=run.name,+ slug=slug or run.name,track=(record.get("spec") or {}).get("track", ""),language=(record.get("spec") or {}).get("language", ""),tier=next((t for t in (record.get("spec") or {}).get("tags", []) if t in ("easy", "medium", "hard")), ""),@@ -138,15 +140,20 @@ def row_for(run: Path, battery: str) -> Row | None:return row-def iter_runs(root: Path) -> Iterable[tuple[str, Path]]:- """``(battery, run dir)`` for a battery dir, a runs dir or a single run."""+def iter_runs(root: Path) -> Iterable[tuple[str, str, Path]]:+ """``(battery, slug, run dir)`` for a battery dir, a runs dir or a single run.++ Discovery and naming go through the same machinery as the flywheel+ (``find_run_dirs`` + ``RunId``), so a nested battery (compare_backends+ ``cells/<id>/<arm>/run``, ab_plan ``arms/…/cells/…/run``) yields one distinct+ slug per run instead of dozens of rows all called ``run``."""if (root / "record.json").is_file():- yield root.parent.parent.name if root.parent.name == "runs" else root.parent.name, root+ battery = root.parent.parent.name if root.parent.name == "runs" else root.parent.name+ yield battery, root.name, rootreturn- runs = root / "runs" if (root / "runs").is_dir() else root- for d in sorted(p for p in runs.iterdir() if p.is_dir()):- if (d / "record.json").is_file():- yield (root.name if runs != root else root.parent.name), d+ battery = root.name if (root / "runs").is_dir() else (root.parent.name or root.name)+ for d in find_run_dirs(root):+ yield battery, RunId(battery=battery, rel=d.relative_to(root).as_posix()).slug, ddef collect(roots: Sequence[Path], *, recursive: bool = False) -> tuple[list[Row], int]:@@ -162,8 +169,8 @@ def collect(roots: Sequence[Path], *, recursive: bool = False) -> tuple[list[Rowfor t in targets:if not t.is_dir():continue- for battery, run in iter_runs(t):- row = row_for(run, battery)+ for battery, slug, run in iter_runs(t):+ row = row_for(run, battery, slug)if row is None:skipped += 1else:diff --git a/harness/codeverse/cli/flywheel_cmd.py b/harness/codeverse/cli/flywheel_cmd.pyindex b23be261..7f9222c9 100644--- a/harness/codeverse/cli/flywheel_cmd.py+++ b/harness/codeverse/cli/flywheel_cmd.py@@ -90,23 +90,24 @@ def caption_cmd(from codeverse.flywheel.record import iter_runs, load_recordif all_runs:- targets = [(ws, rec) for ws, rec in iter_runs(target, on_error=lambda d, e: warn(f"skip {d.name}: {e}"))]+ targets = [(fr.ws, fr.record, fr.run_id.slug)+ for fr in iter_runs(target, on_error=lambda d, e: warn(f"skip {d.name}: {e}"))]else:ws = C.open_workspace(target, runs_dir)- targets = [(ws, load_record(ws))]+ targets = [(ws, load_record(ws), None)] # no scan root → basename identitydone = skipped = failed = 0- for ws, rec in targets:- if not force and load_captions(ws, rec, out).get("detailed"):+ for ws, rec, slug in targets:+ if not force and load_captions(ws, rec, out, slug=slug).get("detailed"):skipped += 1continuetry:- caps = caption_sample(ws, rec, model, out_dir=out)+ caps = caption_sample(ws, rec, model, out_dir=out, slug=slug)except CaptionError as e:failed += 1warn(str(e))continuedone += 1- console.print(f"[bold]{ws.root.name}[/bold]: {caps.instruction}")+ console.print(f"[bold]{slug or ws.root.name}[/bold]: {caps.instruction}")ok(f"captioned {done}, skipped {skipped} (already), failed {failed}")if failed and not done:raise typer.Exit(code=1)diff --git a/harness/codeverse/contracts/run.py b/harness/codeverse/contracts/run.pyindex cf7fb375..f0fe515c 100644--- a/harness/codeverse/contracts/run.py+++ b/harness/codeverse/contracts/run.py@@ -4,9 +4,10 @@ from __future__ import annotationsfrom datetime import UTC, datetimefrom enum import StrEnum+from pathlib import PurePosixPathfrom typing import Any-from pydantic import BaseModel, Field+from pydantic import BaseModel, ConfigDict, Fieldfrom codeverse.contracts.artifacts import BuildResult, GateReport, Measurement, RenderSetfrom codeverse.contracts.common import Usage@@ -15,6 +16,81 @@ from codeverse.contracts.plan import ArticulatedPlan, GraphicsPlan, ScenePlan, Sfrom codeverse.contracts.skills import SkillsUsagefrom codeverse.contracts.spec import Spec+# --------------------------------------------------------------------------- identity+#: battery-layout path segments that are pure plumbing, never part of a run's identity+RUN_PATH_NOISE = frozenset({"arms", "cells", "runs"})+++class RunId(BaseModel):+ """Where a run directory sits inside its scan root — and the ONE name to call it.++ Run identity used to be ``ws.root.name`` everywhere, which is right for the flat+ ``runs/<id>`` layout and catastrophically wrong for nested batteries+ (compare_backends ``cells/<id>/<arm>/run``, ab_plan+ ``arms/<arm>/cells/<id>/<slug>/run``): hundreds of distinct runs are all named+ ``run``, so exports rmtree'd each other, the SQLite index tripped its PRIMARY KEY+ and the gallery collapsed whole batteries into one entry.++ ``battery`` labels the scan root the run was found under; ``rel`` is the run+ directory's posix path relative to that root. Only the scanner still knows the+ root, so only the scanner can mint one of these+ (``flywheel.record.run_id_for``)."""++ model_config = ConfigDict(frozen=True)++ battery: str = Field(description="label of the scan root this run was found under")+ rel: str = Field(description="posix path of the run dir relative to the scan root")++ def _parts(self) -> tuple[str, ...]:+ return PurePosixPath(self.rel).parts++ @property+ def slug(self) -> str:+ """The stable, filesystem-safe name of this run.++ * a flat ``rel`` (no parent directories — today's ``runs/<id>`` layout) keeps+ the basename UNCHANGED, so every existing dataset/index/side-car stays valid;+ * a nested ``rel`` joins the meaningful path segments with ``__``, dropping the+ structural noise (:data:`RUN_PATH_NOISE`) and a trailing ``run`` — e.g.+ ``cells/cmp_easy_stool/harness_api/run`` → ``cmp_easy_stool__harness_api``.+ ``.attemptN`` retry suffixes live on a kept segment and survive.++ Path segments cannot contain ``/``, so the join alone makes the slug a single+ path component; nothing else is mangled."""+ parts = self._parts()+ if len(parts) <= 1:+ return parts[0] if parts else self.battery+ meaningful = [p for p in parts if p not in RUN_PATH_NOISE]+ if len(meaningful) > 1 and meaningful[-1] == "run":+ meaningful.pop() # the literal `run` dir a battery cell wraps its harness run in+ if not meaningful:+ meaningful = [parts[-1]]+ return "__".join(meaningful)++ @property+ def cell(self) -> str | None:+ """The prompt/cell id segment (``cells/<cell>/…``) when the layout has one."""+ parts = self._parts()+ if "cells" in parts:+ i = parts.index("cells")+ if i + 1 < len(parts):+ return parts[i + 1]+ return None++ @property+ def arm(self) -> str | None:+ """The arm segment: ``arms/<arm>/…`` (ab_plan) or the directory between the+ cell and its ``run`` (compare_backends), when the layout has one."""+ parts = self._parts()+ if "arms" in parts:+ i = parts.index("arms")+ return parts[i + 1] if i + 1 < len(parts) else None+ if "cells" in parts:+ i = parts.index("cells")+ if i + 2 < len(parts) and parts[i + 2] != "run":+ return parts[i + 2]+ return None+class RunStatus(StrEnum):PLANNING = "planning"diff --git a/harness/codeverse/cost/reconstruct.py b/harness/codeverse/cost/reconstruct.pyindex d9a52da4..ba8ee82f 100644--- a/harness/codeverse/cost/reconstruct.py+++ b/harness/codeverse/cost/reconstruct.py@@ -35,6 +35,7 @@ from pathlib import Pathfrom typing import Anyfrom codeverse.contracts.common import Usage+from codeverse.contracts.run import RunIdfrom codeverse.cost.ledger import price_callfrom codeverse.cost.types import CallCost, Role, Stage, role_for_stage, stage_for_labelfrom codeverse.proc import read_json_or_none, read_jsonl_lenient@@ -404,7 +405,7 @@ def reconstruct_cell(cell_dir: str | Path, *, recheck: bool = False) -> RunLedge"""A ``bench/compare_backends.py`` one-shot cell (``gen/attempt*`` + ``eval/eval.json``)."""root = Path(cell_dir)cell = _read_json(root / "cell.json")- run = f"{root.parent.name}/{root.name}"+ run = RunId(battery="", rel=f"{root.parent.name}/{root.name}").slug # <prompt>__<arm>led = RunLedger(run=run, path=root, track="static_object", language="blender",generator=str(cell.get("arm") or root.name), judge_model=str(cell.get("judge") or ""),status=str(cell.get("status") or ""), passed=bool(cell.get("passed")),@@ -467,7 +468,7 @@ def reconstruct(path: str | Path, *, recheck: bool = False) -> RunLedger:return reconstruct_run(p, recheck=recheck)if (p / "run" / "record.json").is_file():led = reconstruct_run(p / "run", recheck=recheck)- led.run = f"{p.parent.name}/{p.name}"+ led.run = RunId(battery="", rel=f"{p.parent.name}/{p.name}").slug # <prompt>__<arm># a compare cell also spends OUTSIDE the harness run it wraps: the fixed# evaluator's judge, and a repair arm's extra generations. `run_cell` opens a# ledger for the cell itself (nested around the run's own), and that spend isdiff --git a/harness/codeverse/flywheel/captions.py b/harness/codeverse/flywheel/captions.pyindex f65235ae..aaaf3a74 100644--- a/harness/codeverse/flywheel/captions.py+++ b/harness/codeverse/flywheel/captions.py@@ -154,13 +154,17 @@ def validate_captions(caps: Captions, language: Language) -> list[str]:def caption_sample(- ws: Workspace, record: RunRecord, model_id: str, *, model: object | None = None, out_dir: Path | str | None = None+ ws: Workspace, record: RunRecord, model_id: str, *, model: object | None = None,+ out_dir: Path | str | None = None, slug: str | None = None) -> Captions:"""Caption the best round of ``record``.Default: store into ``record.extra["captions"]`` + ``<ws>/captions.json`` andrewrite ``record.json``. With ``out_dir`` the workspace is left untouched and- ``<out_dir>/<slug>.json`` (captions + provenance) is written instead."""+ ``<out_dir>/<slug>.json`` (captions + provenance) is written instead — ``slug``+ should be the run's :class:`~codeverse.contracts.run.RunId` slug; without one the+ side-car falls back to the directory basename (flat layouts only: every nested+ battery run is named ``run`` and the side-cars would overwrite each other)."""if model is None:from codeverse.models import get_chat_model@@ -202,7 +206,7 @@ def caption_sample(if out_dir is not None:out = Path(out_dir)out.mkdir(parents=True, exist_ok=True)- (out / f"{ws.root.name}.json").write_text(json.dumps(payload, indent=2, ensure_ascii=False))+ (out / f"{slug or ws.root.name}.json").write_text(json.dumps(payload, indent=2, ensure_ascii=False))return capsws.write_json(ws.root / "captions.json", payload)ws.write_json(ws.record_path, record)diff --git a/harness/codeverse/flywheel/export.py b/harness/codeverse/flywheel/export.pyindex 91c4bd9f..3765c8ec 100644--- a/harness/codeverse/flywheel/export.py+++ b/harness/codeverse/flywheel/export.py@@ -30,10 +30,10 @@ from typing import Anyfrom pydantic import BaseModel, Fieldfrom codeverse.contracts.common import ENTRY_FILE, Language-from codeverse.contracts.run import RunRecord+from codeverse.contracts.run import RunId, RunRecordfrom codeverse.flywheel import sample as Sfrom codeverse.flywheel.quality import DuplicateGroup, mark_duplicates-from codeverse.flywheel.record import iter_runs+from codeverse.flywheel.record import FoundRun, iter_runsfrom codeverse.workspace import Workspacelog = logging.getLogger(__name__)@@ -59,15 +59,21 @@ class ExportReport(BaseModel):description="non-fatal degradations, e.g. parquet skipped for a missing extra")-def load_captions(ws: Workspace, record: RunRecord, captions_dir: Path | None = None) -> dict[str, Any]:+def load_captions(+ ws: Workspace, record: RunRecord, captions_dir: Path | None = None, *, slug: str | None = None+) -> dict[str, Any]:"""Full captions dict (text fields + provenance) from record.extra, ``<ws>/captions.json``- or ``<captions_dir>/<slug>.json``; ``{}`` when the run is not captioned."""+ or ``<captions_dir>/<slug>.json``; ``{}`` when the run is not captioned.++ ``slug`` is the run's :class:`~codeverse.contracts.run.RunId` slug; without it the+ side-car falls back to the directory basename (correct only for flat layouts —+ every nested battery run is named ``run`` and would read a stranger's captions)."""caps = record.extra.get("captions") or {}if caps.get("detailed"):return dict(caps)candidates = [ws.root / "captions.json"]if captions_dir is not None:- candidates.append(Path(captions_dir) / f"{ws.root.name}.json")+ candidates.append(Path(captions_dir) / f"{slug or ws.root.name}.json")for p in candidates:if p.is_file():try:@@ -84,10 +90,14 @@ def _caption_texts(caps: dict[str, Any]) -> dict[str, str]:def export_one(- ws: Workspace, record: RunRecord, out_dir: Path, *, overwrite: bool = True, captions_dir: Path | None = None+ ws: Workspace, record: RunRecord, out_dir: Path, *, overwrite: bool = True,+ captions_dir: Path | None = None, run_id: RunId | None = None) -> Path:- """Write the sample folder for one run; returns the sample dir."""- key = S.sample_key(ws)+ """Write the sample folder for one run; returns the sample dir.++ ``run_id`` carries the collision-safe identity minted at discovery time; without+ one the key falls back to the directory basename (flat layouts only)."""+ key = run_id.slug if run_id is not None else S.sample_key(ws)dest = out_dir / S.sample_rel_dir(record, key)rnd = S.best_round_record(record)files, code_source = S.code_files_for_round(ws, rnd)@@ -104,7 +114,7 @@ def export_one(renders = S.copy_renders(ws, rnd, dest)meshes = S.copy_link_meshes(ws, dest) if record.spec.language is Language.URDF_BLENDER else []textured = S.copy_textured(ws, record, dest)- captions = load_captions(ws, record, captions_dir)+ captions = load_captions(ws, record, captions_dir, slug=key)(dest / "captions.json").write_text(json.dumps(_caption_texts(captions), indent=2, ensure_ascii=False))all_files = sorted(written + renders + meshes + textured + ["captions.json", "meta.json"])meta = S.build_meta(@@ -148,7 +158,21 @@ def export_samples(def _bad(d: Path, e: Exception) -> None:rep.skipped[str(d)] = f"invalid record: {e}"- for ws, rec in iter_runs(runs_dir, on_error=_bad):+ # Fail FAST on identity collisions: two run dirs whose samples would land in the+ # same folder must abort before anything is written — the rmtree-before-write in+ # export_one is how 79 nested-battery records used to collapse into ONE sample.+ # A dest left by an EARLIER pass stays overwritable; only same-pass duplicates raise.+ found: list[FoundRun] = list(iter_runs(runs_dir, on_error=_bad))+ dest_owner: dict[str, Path] = {}+ for ws, rec, rid in found:+ rel = str(S.sample_rel_dir(rec, rid.slug))+ prev = dest_owner.get(rel)+ if prev is not None:+ raise S.SampleError(+ f"duplicate sample id {rel!r}: {prev} and {ws.root} would export onto each other")+ dest_owner[rel] = ws.root++ for ws, rec, rid in found:rep.n_runs += 1rnd = S.best_round_record(rec)j = S.effective_judgment(rnd) if rnd is not None else None@@ -167,7 +191,7 @@ def export_samples(continuetry:dest = export_one(ws, rec, out, overwrite=overwrite,- captions_dir=Path(captions_dir) if captions_dir else None)+ captions_dir=Path(captions_dir) if captions_dir else None, run_id=rid)except S.SampleError as e:rep.skipped[str(ws.root)] = str(e)continuediff --git a/harness/codeverse/flywheel/index.py b/harness/codeverse/flywheel/index.pyindex 5e1c3f57..0b211f6b 100644--- a/harness/codeverse/flywheel/index.py+++ b/harness/codeverse/flywheel/index.py@@ -10,7 +10,7 @@ import sqlite3from pathlib import Pathfrom typing import Any-from codeverse.contracts.run import RoundRecord, RunRecord+from codeverse.contracts.run import RoundRecord, RunId, RunRecordfrom codeverse.flywheel.quality import prompt_hash, quality_tierfrom codeverse.flywheel.record import effective_judgment, iter_runsfrom codeverse.workspace import Workspace@@ -22,7 +22,8 @@ CREATE TABLE runs (baseline_score REAL, final_score REAL, best_round INTEGER, n_rounds INTEGER, passed INTEGER,cost_usd REAL, input_tokens INTEGER, output_tokens INTEGER,started_at TEXT, finished_at TEXT, duration_s REAL, error TEXT, has_captions INTEGER,- gate_errors INTEGER, quality_tier TEXT, best_commit TEXT+ gate_errors INTEGER, quality_tier TEXT, best_commit TEXT,+ rel TEXT, arm TEXT, cell TEXT -- run dir relative to the scan root + battery layout segments);CREATE TABLE rounds (slug TEXT, round_index INTEGER, kind TEXT, commit_sha TEXT, agent_backend TEXT,@@ -41,7 +42,7 @@ CREATE INDEX idx_rounds_slug ON rounds(slug);"""-def _run_row(ws: Workspace, rec: RunRecord) -> tuple:+def _run_row(ws: Workspace, rec: RunRecord, rid: RunId) -> tuple:best = next((r for r in rec.rounds if r.index == rec.best_round), None)best_j = effective_judgment(best) if best is not None else None # degraded → unjudgedpassed = None if best_j is None else int(best_j.passed)@@ -50,7 +51,7 @@ def _run_row(ws: Workspace, rec: RunRecord) -> tuple:score = best_j.overall if best_j is not None else Nonetier = quality_tier(passed=None if passed is None else bool(passed), gate_errors=n_err, score=score)return (- ws.root.name, str(ws.root), rec.spec.track.value, rec.spec.language.value, rec.spec.prompt,+ rid.slug, str(ws.root), rec.spec.track.value, rec.spec.language.value, rec.spec.prompt,prompt_hash(rec.spec.prompt), rec.spec.backends.generator, rec.spec.backends.planner,rec.spec.backends.judge, rec.status.value, rec.baseline_score, rec.final_score, rec.best_round,len(rec.rounds), passed, rec.total_usage.cost_usd, rec.total_usage.input_tokens,@@ -58,6 +59,7 @@ def _run_row(ws: Workspace, rec: RunRecord) -> tuple:rec.finished_at.isoformat() if rec.finished_at else None, dur, rec.error,int(bool((rec.extra.get("captions") or {}).get("detailed"))),n_err, tier, best.commit if best is not None else "",+ rid.rel, rid.arm, rid.cell,)@@ -94,9 +96,17 @@ def build_index(runs_dir: Path | str, out_sqlite: Path | str) -> int:con = sqlite3.connect(tmp)try:con.executescript(_SCHEMA)- for ws, rec in iter_runs(runs_dir):- slug = ws.root.name- con.execute(f"INSERT INTO runs VALUES ({','.join('?' * 26)})", _run_row(ws, rec))+ seen: dict[str, str] = {}+ for ws, rec, rid in iter_runs(runs_dir):+ slug = rid.slug+ try:+ con.execute(f"INSERT INTO runs VALUES ({','.join('?' * 29)})", _run_row(ws, rec, rid))+ except sqlite3.IntegrityError as e:+ # never INSERT OR REPLACE: a silent overwrite is the bug this guards+ raise sqlite3.IntegrityError(+ f"duplicate run slug {slug!r}: {seen.get(slug, '<unknown>')} and {ws.root}"+ ) from e+ seen[slug] = str(ws.root)con.executemany(f"INSERT INTO rounds VALUES ({','.join('?' * 13)})", [_round_row(slug, r) for r in rec.rounds])con.executemany(f"INSERT INTO usage VALUES ({','.join('?' * 12)})",[row for r in rec.rounds for row in _usage_rows(slug, r)])diff --git a/harness/codeverse/flywheel/pairs.py b/harness/codeverse/flywheel/pairs.pyindex cd86f4de..3a016969 100644--- a/harness/codeverse/flywheel/pairs.py+++ b/harness/codeverse/flywheel/pairs.py@@ -69,10 +69,10 @@ def _side(ws: Workspace, rnd: RoundRecord, *, cache: dict[str, dict[str, Any]])}-def _base(ws: Workspace, rec: RunRecord, kind: str) -> dict[str, Any]:+def _base(ws: Workspace, rec: RunRecord, kind: str, *, slug: str | None = None) -> dict[str, Any]:return {"kind": kind,- "run": ws.root.name,+ "run": slug or ws.root.name,"workspace": str(ws.root),"prompt": rec.spec.prompt,"prompt_hash": prompt_hash(rec.spec.prompt),@@ -82,7 +82,8 @@ def _base(ws: Workspace, rec: RunRecord, kind: str) -> dict[str, Any]:}-def preference_pairs(ws: Workspace, rec: RunRecord, *, min_delta: float) -> list[dict[str, Any]]:+def preference_pairs(ws: Workspace, rec: RunRecord, *, min_delta: float,+ slug: str | None = None) -> list[dict[str, Any]]:cache: dict[str, dict[str, Any]] = {}# degraded (judge-outage) verdicts are glitches, not 0.0 scores — leave those rounds outjudged = [r for r in rec.rounds if effective_judgment(r) is not None and r.commit]@@ -93,7 +94,7 @@ def preference_pairs(ws: Workspace, rec: RunRecord, *, min_delta: float) -> listdelta = hi_j.overall - lo_j.overall # type: ignore[union-attr]if delta < min_delta or lo.commit == hi.commit:continue- pair = _base(ws, rec, "preference")+ pair = _base(ws, rec, "preference", slug=slug)pair.update(chosen=_side(ws, hi, cache=cache),rejected=_side(ws, lo, cache=cache),@@ -120,7 +121,7 @@ def _error_of(rnd: RoundRecord) -> dict[str, Any]:}-def repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]:+def repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:"""(broken round, error) → (the next round that builds).Matched structurally: the tracks never emit a ``kind='repair'`` round@@ -135,7 +136,7 @@ def repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]:fixed = next((r for r in rounds[k + 1 :] if r.build is not None and r.build.ok and r.commit), None)if fixed is None or broken.commit == fixed.commit:continue- pair = _base(ws, rec, "repair")+ pair = _base(ws, rec, "repair", slug=slug)pair.update(source="round",chosen=_side(ws, fixed, cache=cache),@@ -166,7 +167,7 @@ def _round_build_errors(ws: Workspace, index: int) -> list[str]:return out-def in_round_repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]:+def in_round_repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:"""Builds fixed by ``build_with_repair`` *inside* a round.``tracks/steps.py`` commits ``rNN <kind>: generated`` right before the@@ -191,7 +192,7 @@ def in_round_repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]if rej_files == chosen["files"]:continue # the repair changed nothing under the code rootserrors = _round_build_errors(ws, rnd.index)- pair = _base(ws, rec, "repair")+ pair = _base(ws, rec, "repair", slug=slug)pair.update(source="in_round",chosen=chosen,@@ -206,11 +207,11 @@ def in_round_repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]return out-def trajectory_repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Any]]:+def trajectory_repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:"""In-session repairs (failing build → fixing build) mined from api-agent transcripts."""out = []for tr in mine_run(ws):- pair = _base(ws, rec, "repair")+ pair = _base(ws, rec, "repair", slug=slug)pair.update(source="trajectory",trajectory=tr.trajectory,@@ -228,37 +229,41 @@ def trajectory_repair_pairs(ws: Workspace, rec: RunRecord) -> list[dict[str, Anyreturn out-def cross_backend_pairs(groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord]]], *, min_delta: float) -> list[dict[str, Any]]:+def cross_backend_pairs(+ groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord, str]]], *, min_delta: float+) -> list[dict[str, Any]]:+ """``groups`` values are ``(ws, record, slug)`` — the slug is the RunId slug the+ scan minted (``ws.root.name`` collides across nested battery layouts)."""out = []for (_h, _t, _l), runs in groups.items():- gens = {r.spec.backends.generator for _, r in runs}+ gens = {r.spec.backends.generator for _, r, _s in runs}if len(gens) < 2:continuecands = []- for ws, rec in runs:+ for ws, rec, slug in runs:best = next((r for r in rec.rounds if r.index == rec.best_round), None)if best is None or effective_judgment(best) is None: # unjudged or degraded verdictcontinue- cands.append((ws, rec, best))+ cands.append((ws, rec, slug, best))if len(cands) < 2:continue- cands.sort(key=lambda c: effective_judgment(c[2]).overall, reverse=True) # type: ignore[union-attr]- w_ws, w_rec, w_rnd = cands[0]+ cands.sort(key=lambda c: effective_judgment(c[3]).overall, reverse=True) # type: ignore[union-attr]+ w_ws, w_rec, w_slug, w_rnd = cands[0]w_side = _side(w_ws, w_rnd, cache={})w_side["generator"] = w_rec.spec.backends.generator- for ws, rec, rnd in cands[1:]:+ for ws, rec, slug, rnd in cands[1:]:delta = effective_judgment(w_rnd).overall - effective_judgment(rnd).overall # type: ignore[union-attr]if rec.spec.backends.generator == w_rec.spec.backends.generator or delta < min_delta:continue- pair = _base(w_ws, w_rec, "cross_backend")+ pair = _base(w_ws, w_rec, "cross_backend", slug=w_slug)l_side = _side(ws, rnd, cache={})l_side["generator"] = rec.spec.backends.generator- l_side["run"] = ws.root.name+ l_side["run"] = slugpair.update(chosen=w_side, rejected=l_side, delta=round(delta, 4),reason=f"best-of across generators: {w_rec.spec.backends.generator} > {rec.spec.backends.generator}",- candidates=[{"run": c[0].root.name, "generator": c[1].spec.backends.generator,- "score": effective_judgment(c[2]).overall} for c in cands], # type: ignore[union-attr]+ candidates=[{"run": c[2], "generator": c[1].spec.backends.generator,+ "score": effective_judgment(c[3]).overall} for c in cands], # type: ignore[union-attr])out.append(pair)return out@@ -272,15 +277,17 @@ def build_pairs(``trajectories`` adds in-session repair pairs mined from agent transcripts."""out = Path(out_jsonl)out.parent.mkdir(parents=True, exist_ok=True)- groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord]]] = defaultdict(list)+ groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord, str]]] = defaultdict(list)n = 0tmp = out.with_suffix(out.suffix + ".tmp")with tmp.open("w") as fh:- for ws, rec in iter_runs(runs_dir):- groups[(prompt_hash(rec.spec.prompt), rec.spec.track.value, rec.spec.language.value)].append((ws, rec))- pairs = preference_pairs(ws, rec, min_delta=min_delta) + repair_pairs(ws, rec) + in_round_repair_pairs(ws, rec)+ for ws, rec, rid in iter_runs(runs_dir):+ slug = rid.slug+ groups[(prompt_hash(rec.spec.prompt), rec.spec.track.value, rec.spec.language.value)].append((ws, rec, slug))+ pairs = (preference_pairs(ws, rec, min_delta=min_delta, slug=slug)+ + repair_pairs(ws, rec, slug=slug) + in_round_repair_pairs(ws, rec, slug=slug))if trajectories:- pairs += trajectory_repair_pairs(ws, rec)+ pairs += trajectory_repair_pairs(ws, rec, slug=slug)for pair in pairs:fh.write(json.dumps(pair, ensure_ascii=False) + "\n")n += 1diff --git a/harness/codeverse/flywheel/record.py b/harness/codeverse/flywheel/record.pyindex db4d6d75..a0eb02e5 100644--- a/harness/codeverse/flywheel/record.py+++ b/harness/codeverse/flywheel/record.py@@ -19,12 +19,12 @@ from collections.abc import Callable, Iteratorfrom datetime import UTC, datetimefrom functools import lru_cachefrom pathlib import Path-from typing import Any+from typing import Any, NamedTuplefrom codeverse import __version__from codeverse.config import get_settingsfrom codeverse.contracts.common import Usage-from codeverse.contracts.run import RoundRecord, RunRecord+from codeverse.contracts.run import RoundRecord, RunId, RunRecordfrom codeverse.flywheel.code_quality import code_quality_blockfrom codeverse.workspace import Workspace@@ -316,6 +316,30 @@ def is_run_dir(p: Path) -> bool:return p.is_dir() and (p / "record.json").is_file()+def battery_label(root: Path | str) -> str:+ """Label for a scan root: the battery name for ``bench/out/<battery>/runs``,+ else the directory's own name (the same rule the gallery sections use)."""+ root = Path(root)+ if root.name == "runs" and root.parent.name and root.parent.parent.name == "out":+ return root.parent.name+ return root.name or str(root)+++def run_id_for(root: Path | str, run_dir: Path) -> RunId:+ """The :class:`~codeverse.contracts.run.RunId` of ``run_dir`` as found under scan+ root ``root``. Identity is minted HERE, at discovery time — the Workspace+ resolves its root and forgets where the scan started, so it cannot do this."""+ return RunId(battery=battery_label(root), rel=run_dir.relative_to(Path(root)).as_posix())+++class FoundRun(NamedTuple):+ """One discovered run: workspace + parsed record + identity within the scan root."""++ ws: Workspace+ record: RunRecord+ run_id: RunId++def find_run_dirs(root: Path | str, *, predicate: Callable[[Path], bool] = is_run_dir,max_depth: int = RUN_SEARCH_DEPTH) -> list[Path]:"""Run directories under ``root``, sorted.@@ -351,8 +375,9 @@ def find_run_dirs(root: Path | str, *, predicate: Callable[[Path], bool] = is_rudef iter_runs(runs_dir: Path | str, *, on_error: Callable[[Path, Exception], None] | None = None-) -> Iterator[tuple[Workspace, RunRecord]]:- """Yield ``(Workspace, RunRecord)`` for every run directory holding a record.json.+) -> Iterator[FoundRun]:+ """Yield a :class:`FoundRun` ``(ws, record, run_id)`` for every run directory+ holding a record.json.Invalid records raise ``RecordError`` unless ``on_error`` is given, in whichcase it is called and iteration continues (exporters collect skip reasons).@@ -382,4 +407,4 @@ def iter_runs(raiseon_error(d, e)continue- yield ws, rec+ yield FoundRun(ws, rec, run_id_for(root, d))diff --git a/harness/codeverse/flywheel/sample.py b/harness/codeverse/flywheel/sample.pyindex 27899904..ab209aaf 100644--- a/harness/codeverse/flywheel/sample.py+++ b/harness/codeverse/flywheel/sample.py@@ -126,6 +126,10 @@ class SampleMeta(BaseModel):def sample_key(ws: Workspace) -> str:+ """FALLBACK key: the directory basename — correct for the flat ``runs/<id>``+ layout only. Exporters that scanned a root pass the collision-safe+ :class:`~codeverse.contracts.run.RunId` slug instead (``export_one(run_id=…)``):+ in nested battery layouts every run dir is literally named ``run``."""return ws.root.namediff --git a/harness/codeverse/gallery/index.py b/harness/codeverse/gallery/index.pyindex d683a521..fd30a863 100644--- a/harness/codeverse/gallery/index.py+++ b/harness/codeverse/gallery/index.py@@ -15,13 +15,15 @@ from __future__ import annotationsimport timefrom pathlib import Path-from codeverse.contracts.run import RunRecord+from codeverse.contracts.run import RunId, RunRecordfrom codeverse.flywheel.quality import quality_tierfrom codeverse.flywheel.record import (RecordError,+ battery_label,best_round_index,effective_judgment,find_run_dirs,+ is_run_dir,load_record,)from codeverse.flywheel.sample import gate_error_summary, telemetry_digest@@ -47,10 +49,7 @@ def default_roots(base: Path | str = ".") -> list[Path]:def root_label(root: Path) -> str:"""Section label: ``runs`` for the plain runs dir, the battery name for a bench out dir."""- root = Path(root)- if root.name == "runs" and root.parent.name and root.parent.parent.name == "out":- return root.parent.name- return root.name or str(root)+ return battery_label(Path(root))def _rel(run_dir: Path, path: Path | str | None) -> str:@@ -218,8 +217,11 @@ def _complexity(ws: Workspace, rec: RunRecord) -> tuple[float | None, str, dict[return float(block["index"]), str(block.get("band") or ""), axes-def entry_from_record(battery: str, ws: Workspace, rec: RunRecord) -> RunEntry:- """A complete card from a parsed record (never raises: every field degrades)."""+def entry_from_record(battery: str, ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> RunEntry:+ """A complete card from a parsed record (never raises: every field degrades).++ ``slug`` is the RunId slug the scan minted; without one the directory basename is+ used (correct for flat layouts only — nested battery runs are all named ``run``)."""best = best_round_index(rec)rnd = next((r for r in rec.rounds if r.index == best), None)j = effective_judgment(rnd) if rnd is not None else None@@ -232,7 +234,7 @@ def entry_from_record(battery: str, ws: Workspace, rec: RunRecord) -> RunEntry:plan = rec.plancx_index, cx_band, cx_axes = _complexity(ws, rec)return RunEntry(- battery=battery, slug=ws.root.name, path=str(ws.root), state="ok",+ battery=battery, slug=slug or ws.root.name, path=str(ws.root), state="ok",title=(getattr(plan, "object_name", "") or getattr(plan, "title", "") or "") if plan else "",prompt=rec.spec.prompt, track=rec.spec.track.value, language=rec.spec.language.value,generator=rec.spec.backends.generator, judge=rec.spec.backends.judge,@@ -258,38 +260,66 @@ def _spec_fields(run_dir: Path) -> dict[str, str]:return {k: str(spec.get(k) or "") for k in ("prompt", "track", "language")}-def entry_for_dir(battery: str, run_dir: Path) -> RunEntry:- """One run directory → an entry, whatever state it is in."""+def entry_for_dir(battery: str, run_dir: Path, *, slug: str | None = None) -> RunEntry:+ """One run directory → an entry, whatever state it is in.++ ``slug`` is the RunId slug the scan minted (default: the directory basename,+ which is only unique in flat layouts)."""ws = Workspace(run_dir)+ slug = slug or run_dir.namefields = _spec_fields(run_dir)if not (run_dir / "record.json").is_file():- return RunEntry(battery=battery, slug=run_dir.name, path=str(run_dir), state="pending",+ return RunEntry(battery=battery, slug=slug, path=str(run_dir), state="pending",error="no record.json yet (run in progress or never finished)",links=entry_links(ws, None, None), **fields)try:rec = load_record(ws)except (RecordError, OSError, ValueError) as e:- return RunEntry(battery=battery, slug=run_dir.name, path=str(run_dir), state="broken",+ return RunEntry(battery=battery, slug=slug, path=str(run_dir), state="broken",error=str(e)[:400], links=entry_links(ws, None, None), **fields)try:- return entry_from_record(battery, ws, rec)+ return entry_from_record(battery, ws, rec, slug=slug)except Exception as e: # noqa: BLE001 - one weird record must not break the page- return RunEntry(battery=battery, slug=run_dir.name, path=str(run_dir), state="broken",+ return RunEntry(battery=battery, slug=slug, path=str(run_dir), state="broken",error=f"{type(e).__name__}: {e}"[:400], links=entry_links(ws, None, None), **fields)-def is_run_dir(p: Path) -> bool:- return p.is_dir() and ((p / "record.json").is_file() or (p / "spec.json").is_file())+def _pending_direct_children(root: Path, found: set[Path]) -> list[Path]:+ """Direct children with a spec.json but no record.json yet — a bench mid-write+ still deserves a readable (pending) card. Only DIRECT children qualify: a run is+ otherwise gated on record.json exactly like ``flywheel.record.is_run_dir``, because+ accepting spec-only directories during descent counted every nested ``eval/``+ judge workspace of a battery cell as a phantom run."""+ if not root.is_dir():+ return []+ return [d for d in sorted(root.iterdir())+ if d.is_dir() and d not in found+ and (d / "spec.json").is_file() and not (d / "record.json").is_file()]def scan_root(root: Path, label: str | None = None) -> RootSection:"""Runs under ``root``. Uses ``find_run_dirs`` rather than a one-level``iterdir()``: a compare_backends or ab_plan battery directory holds its runs fourand five levels down, so pointing the gallery at one built a page "of 0 runs" and- exited 0 — the silent-empty failure, on a directory full of real runs."""+ exited 0 — the silent-empty failure, on a directory full of real runs.++ Every entry is named by its :class:`~codeverse.contracts.run.RunId` slug, so two+ nested runs whose directories are both called ``run`` stay two entries. A slug+ that still repeats within the root gets ``#2``/``#3`` — the same precedent as+ duplicate section labels in :func:`build_index`."""+ root = Path(root)label = label or root_label(root)- entries = [entry_for_dir(label, d) for d in find_run_dirs(root, predicate=is_run_dir)]- return RootSection(label=label, path=str(Path(root).resolve()), entries=entries)+ found = find_run_dirs(root, predicate=is_run_dir)+ dirs = sorted({*found, *_pending_direct_children(root, set(found))})+ used: dict[str, int] = {}+ entries: list[RunEntry] = []+ for d in dirs:+ slug = RunId(battery=label, rel=d.relative_to(root).as_posix()).slug+ used[slug] = used.get(slug, 0) + 1+ if used[slug] > 1:+ slug = f"{slug}#{used[slug]}"+ entries.append(entry_for_dir(label, d, slug=slug))+ return RootSection(label=label, path=str(root.resolve()), entries=entries)def build_index(roots: list[Path] | list[str]) -> GalleryIndex:diff --git a/harness/codeverse/gallery/server.py b/harness/codeverse/gallery/server.pyindex 382fa5bb..11f4990c 100644--- a/harness/codeverse/gallery/server.py+++ b/harness/codeverse/gallery/server.py@@ -237,7 +237,7 @@ class GalleryApp:return Response.html(render_broken_detail(broken, self.urls))from codeverse.gallery.index import entry_from_record- fresh = entry_from_record(entry.battery, ws, rec) # newest rounds, even mid-bench+ fresh = entry_from_record(entry.battery, ws, rec, slug=entry.slug) # newest rounds, even mid-benchprev, nxt = self.neighbours(entry)return Response.html(render_detail(fresh, self.urls, ws, rec, prev=prev, nxt=nxt))diff --git a/harness/tests/flywheel_cli/test_flywheel.py b/harness/tests/flywheel_cli/test_flywheel.pyindex e7d847bc..5eedfe88 100644--- a/harness/tests/flywheel_cli/test_flywheel.py+++ b/harness/tests/flywheel_cli/test_flywheel.py@@ -400,14 +400,22 @@ def test_a_battery_dir_is_discovered_rather_than_reported_as_zero_runs(tmp_path)def test_a_gallery_of_a_battery_dir_is_not_a_gallery_of_zero_runs(tmp_path):"""CP-6, the other half: gallery/index.scan_root did a single `root.iterdir()`, so- `3dcv gallery build bench/out/<ab battery>` printed "gallery of 0 runs" and exit 0."""+ `3dcv gallery build bench/out/<ab battery>` printed "gallery of 0 runs" and exit 0.++ A nested run is gated on record.json — the same rule as flywheel.record.is_run_dir.+ The cell's sibling ``eval/`` judge workspace only has a spec.json and used to be+ counted as a phantom run; it must not appear. The entry is named by its RunId slug,+ not the degenerate directory basename ``run``."""from codeverse.gallery.index import scan_rootab = tmp_path / "ab_aa_noise"- d = ab / "arms" / "control" / "cells" / "ctrl_med_chair" / "harness_api-agent" / "run"- d.mkdir(parents=True)- (d / "spec.json").write_text("{}")- assert len(scan_root(ab).entries) == 1+ cell = ab / "arms" / "control" / "cells" / "ctrl_med_chair" / "harness_api-agent"+ (cell / "run").mkdir(parents=True)+ (cell / "run" / "record.json").write_text("{}")+ (cell / "eval").mkdir()+ (cell / "eval" / "spec.json").write_text("{}")+ section = scan_root(ab)+ assert [e.slug for e in section.entries] == ["control__ctrl_med_chair__harness_api-agent"]def test_an_empty_battery_dir_still_refuses_to_look_like_an_empty_dataset(tmp_path):diff --git a/harness/tests/flywheel_cli/test_run_identity.py b/harness/tests/flywheel_cli/test_run_identity.pynew file mode 100644index 00000000..3030883f--- /dev/null+++ b/harness/tests/flywheel_cli/test_run_identity.py@@ -0,0 +1,167 @@+"""Run identity (RunId): the slug rule, and that nested batteries no longer collide.++The real bench/out tree holds 396 run dirs with only 169 distinct basenames — 211 are+literally ``run`` (compare_backends ``cells/<id>/<arm>/run``, ab_plan+``arms/<arm>/cells/<id>/<slug>/run``). Keying anything on ``ws.root.name`` therefore+made exports rmtree each other, blew the SQLite PRIMARY KEY, collapsed gallery entries+and bled captions/pairs across runs. These tests pin the fix end to end.+"""++from __future__ import annotations++import json+import sqlite3+from pathlib import Path++import pytest++from codeverse.contracts.run import RunId+from codeverse.flywheel.export import export_samples, load_captions+from codeverse.flywheel.index import build_index as build_sqlite_index+from codeverse.flywheel.pairs import build_pairs+from codeverse.flywheel.record import FoundRun, battery_label, iter_runs, run_id_for+from codeverse.flywheel.sample import SampleError+from tests.flywheel_cli.conftest import make_fake_run+++# --------------------------------------------------------------------------- slug rule+def test_flat_layout_keeps_the_plain_basename():+ """Back-compat: a rel with no parent directories is the basename, unchanged."""+ assert RunId(battery="runs", rel="wooden_chair_ab12cd34").slug == "wooden_chair_ab12cd34"+ # retry cells keep their .attemptN suffix — it distinguishes them from the original+ assert RunId(battery="h2h_scene_v1",+ rel="h2h_b01_neon_alley.attempt1").slug == "h2h_b01_neon_alley.attempt1"+++def test_a_runs_wrapper_is_structural_noise():+ assert RunId(battery="articulated_v1_flash", rel="runs/art_easy_laptop").slug == "art_easy_laptop"+++def test_compare_backends_layout_slug_arm_cell():+ rid = RunId(battery="compare_v1_full",+ rel="cells/cmp_easy_stool/harness_api-agent_gemini_gemini-3.7-flash/run")+ assert rid.slug == "cmp_easy_stool__harness_api-agent_gemini_gemini-3.7-flash"+ assert rid.cell == "cmp_easy_stool"+ assert rid.arm == "harness_api-agent_gemini_gemini-3.7-flash"+++def test_ab_plan_layout_slug_arm_cell_and_attempt_suffix():+ rid = RunId(battery="plan_loop",+ rel="C0/arms/control/cells/arch_hard_bay_window/"+ "harness_api-agent_gemini_gemini-3.7-flash/run")+ assert rid.slug == "C0__control__arch_hard_bay_window__harness_api-agent_gemini_gemini-3.7-flash"+ assert rid.arm == "control" and rid.cell == "arch_hard_bay_window"+ retry = RunId(battery="plan_loop",+ rel="heldout/arms/control/cells/comp_hard_drafting_table/"+ "harness_api-agent_gemini_gemini-3.7-flash.attempt1/run")+ assert retry.slug.endswith("harness_api-agent_gemini_gemini-3.7-flash.attempt1")+++def test_battery_label_and_run_id_for(tmp_path: Path):+ runs = tmp_path / "bench" / "out" / "static_v9" / "runs"+ (runs / "stool_a1").mkdir(parents=True)+ assert battery_label(runs) == "static_v9"+ rid = run_id_for(runs, runs / "stool_a1")+ assert rid.battery == "static_v9" and rid.rel == "stool_a1" and rid.slug == "stool_a1"+++# --------------------------------------------------------------------------- shared tree+def _nested_battery(tmp_path: Path) -> Path:+ """ONE root, two real runs whose directories are BOTH named ``run``."""+ root = tmp_path / "compare_v9"+ make_fake_run(root / "cells" / "cmp_a_stool" / "armx", "run", prompt="a stool")+ make_fake_run(root / "cells" / "cmp_b_lamp" / "armx", "run", prompt="a lamp")+ return root+++def _colliding_root(tmp_path: Path) -> Path:+ """Two rels that legitimately reduce to the SAME slug ``x``."""+ root = tmp_path / "mixed"+ make_fake_run(root / "runs", "x") # runs/x -> slug x+ make_fake_run(root / "cells" / "x", "run") # cells/x/run -> slug x+ return root+++def test_iter_runs_mints_distinct_identities_for_basename_run(tmp_path: Path):+ root = _nested_battery(tmp_path)+ found = list(iter_runs(root))+ assert all(isinstance(f, FoundRun) for f in found)+ assert all(f.ws.root.name == "run" for f in found) # the basename really is degenerate+ assert sorted(f.run_id.slug for f in found) == ["cmp_a_stool__armx", "cmp_b_lamp__armx"]+++# --------------------------------------------------------------------------- export+def test_export_of_two_nested_runs_writes_two_sample_dirs(tmp_path: Path):+ root = _nested_battery(tmp_path)+ out = tmp_path / "ds"+ rep = export_samples(root, out)+ assert rep.n_exported == 2 and rep.n_indexed == 2, rep.skipped+ a = out / "static_object" / "blender" / "cmp_a_stool__armx"+ b = out / "static_object" / "blender" / "cmp_b_lamp__armx"+ assert a.is_dir() and b.is_dir()+ assert json.loads((a / "meta.json").read_text())["prompt"] == "a stool"+ assert json.loads((b / "meta.json").read_text())["prompt"] == "a lamp"+ keys = {json.loads(line)["key"] for line in (out / "metadata.jsonl").read_text().splitlines()}+ assert keys == {"cmp_a_stool__armx", "cmp_b_lamp__armx"}+++def test_export_raises_on_a_slug_collision_before_writing_anything(tmp_path: Path):+ root = _colliding_root(tmp_path)+ out = tmp_path / "ds"+ with pytest.raises(SampleError, match="duplicate sample id") as ei:+ export_samples(root, out)+ msg = str(ei.value)+ assert "cells/x/run" in msg and "runs/x" in msg, "both run dirs must be named"+ assert list(out.rglob("meta.json")) == [], "nothing may be written on a collision"+++# --------------------------------------------------------------------------- sqlite index+def test_sqlite_index_holds_two_rows_with_rel_arm_cell(tmp_path: Path):+ root = _nested_battery(tmp_path)+ db = tmp_path / "idx.sqlite"+ assert build_sqlite_index(root, db) == 2+ con = sqlite3.connect(db)+ rows = con.execute("SELECT slug, rel, arm, cell FROM runs ORDER BY slug").fetchall()+ con.close()+ assert rows == [+ ("cmp_a_stool__armx", "cells/cmp_a_stool/armx/run", "armx", "cmp_a_stool"),+ ("cmp_b_lamp__armx", "cells/cmp_b_lamp/armx/run", "armx", "cmp_b_lamp"),+ ]+++def test_sqlite_index_collision_names_both_workspaces(tmp_path: Path):+ root = _colliding_root(tmp_path)+ with pytest.raises(sqlite3.IntegrityError) as ei:+ build_sqlite_index(root, tmp_path / "idx.sqlite")+ msg = str(ei.value)+ assert "duplicate run slug 'x'" in msg+ assert "cells/x/run" in msg and "runs/x" in msg+++# --------------------------------------------------------------------------- pairs+def test_pairs_run_field_is_the_slug_not_run(tmp_path: Path):+ root = _nested_battery(tmp_path)+ out = tmp_path / "pairs.jsonl"+ n = build_pairs(root, out, min_delta=0.05)+ assert n == 2 # one preference pair per run (0.55 → 0.80)+ runs = {json.loads(line)["run"] for line in out.read_text().splitlines()}+ assert runs == {"cmp_a_stool__armx", "cmp_b_lamp__armx"}+++# --------------------------------------------------------------------------- captions+def test_caption_sidecars_do_not_bleed_across_nested_runs(tmp_path: Path):+ from codeverse.flywheel.captions import caption_sample+ from tests.flywheel_cli.test_captions import GOOD, FakeModel++ root = _nested_battery(tmp_path)+ side = tmp_path / "caps"+ found = {f.run_id.slug: f for f in iter_runs(root)}+ a = found["cmp_a_stool__armx"]+ b = found["cmp_b_lamp__armx"]+ caption_sample(a.ws, a.record, "fake:fake", model=FakeModel([GOOD]),+ out_dir=side, slug=a.run_id.slug)+ assert (side / "cmp_a_stool__armx.json").is_file()+ assert not (side / "run.json").exists(), "the degenerate basename side-car is the bleed"+ assert load_captions(a.ws, a.record, side, slug=a.run_id.slug)["detailed"] == GOOD["detailed"]+ assert load_captions(b.ws, b.record, side, slug=b.run_id.slug) == {}, \+ "the un-captioned run must not pick up its neighbour's side-car"diff --git a/harness/tests/gallery/test_index.py b/harness/tests/gallery/test_index.pyindex bde145c3..d7827b2a 100644--- a/harness/tests/gallery/test_index.py+++ b/harness/tests/gallery/test_index.py@@ -100,3 +100,50 @@ def test_duplicate_labels_are_disambiguated(tmp_path: Path):b.mkdir(parents=True)index = build_index([a, b])assert [s.label for s in index.sections] == ["runs", "runs#2"]+++# --------------------------------------------------------------------------- run identity+def test_nested_battery_runs_get_distinct_findable_slugs(tmp_path: Path):+ """Two compare-style runs whose dirs are BOTH named ``run`` used to collapse into+ one gallery key; each now carries its RunId slug and both are findable."""+ from tests.flywheel_cli.conftest import make_fake_run++ root = tmp_path / "compare_v9"+ make_fake_run(root / "cells" / "cmp_a_stool" / "armx", "run", prompt="a stool")+ make_fake_run(root / "cells" / "cmp_b_lamp" / "armx", "run", prompt="a lamp")+ index = build_index([root])+ (section,) = index.sections+ assert sorted(e.slug for e in section.entries) == ["cmp_a_stool__armx", "cmp_b_lamp__armx"]+ a = index.find("compare_v9", "cmp_a_stool__armx")+ b = index.find("compare_v9", "cmp_b_lamp__armx")+ assert a is not None and a.prompt == "a stool"+ assert b is not None and b.prompt == "a lamp"+ assert a.key != b.key+++def test_nested_eval_workspaces_are_not_counted_as_runs(tmp_path: Path):+ """A battery cell's ``eval/`` judge workspace has a spec.json but no record.json —+ it is not a run (52 phantom entries per battery under the old spec-or-record rule).+ A spec-only DIRECT child of the root is still a legitimate pending card."""+ from tests.flywheel_cli.conftest import make_fake_run++ root = tmp_path / "compare_v9"+ make_fake_run(root / "cells" / "cmp_a_stool" / "armx", "run", prompt="a stool")+ eval_dir = root / "cells" / "cmp_a_stool" / "armx" / "eval"+ eval_dir.mkdir()+ (eval_dir / "spec.json").write_text(json.dumps({"prompt": "judge ws"}))+ (section,) = build_index([root]).sections+ assert [e.slug for e in section.entries] == ["cmp_a_stool__armx"]+++def test_duplicate_slugs_within_a_root_are_disambiguated(tmp_path: Path):+ """Two rels that reduce to the same slug follow the label#2 precedent — a viewer+ must still see both runs (exporters fail loudly instead; the gallery is read-only)."""+ from tests.flywheel_cli.conftest import make_fake_run++ root = tmp_path / "mixed"+ make_fake_run(root / "runs", "x", prompt="first")+ make_fake_run(root / "cells" / "x", "run", prompt="second")+ (section,) = build_index([root]).sections+ assert [e.slug for e in section.entries] == ["x", "x#2"] # cells/x/run sorts first+ assert {e.prompt for e in section.entries} == {"first", "second"}