Skip to content

Official Codebase of 3DCodeVerse

Python

flywheel: carry the v0.0.x run layout, Usage and labels itself so it reads old runs against the trimmed plugin

gaoypeng982837599ea4
diff --git a/flywheel/c3d_flywheel/cli/_common.py b/flywheel/c3d_flywheel/cli/_common.py
index f49690f4..fb4af436 100644
--- a/flywheel/c3d_flywheel/cli/_common.py
+++ b/flywheel/c3d_flywheel/cli/_common.py
@@ -6,12 +6,12 @@ from pathlib import Path
from typing import Annotated, Any
import typer
-from codeverse3d.workspace import Workspace
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from c3d_flywheel.legacy import LegacySpec, RunRecord
+from c3d_flywheel.runlayout import RunWorkspace
console = Console(emoji=False)
err_console = Console(stderr=True, style="bold red", emoji=False)
@@ -53,18 +53,18 @@ def runs_root(runs_dir: Path | None) -> Path:
return Path(runs_dir) if runs_dir is not None else DEFAULT_RUNS_DIR
-def open_workspace(slug: str, runs_dir: Path | None = None) -> Workspace:
+def open_workspace(slug: str, runs_dir: Path | None = None) -> RunWorkspace:
"""A run named by its directory, or by its slug under the runs root."""
p = Path(slug)
if p.is_dir() and (p / "spec.json").is_file():
- return Workspace(p)
- ws = Workspace(runs_root(runs_dir) / slug)
+ return RunWorkspace(p)
+ ws = RunWorkspace(runs_root(runs_dir) / slug)
if not ws.exists():
raise CliError(f"run not found: {ws.root} (no spec.json)")
return ws
-def load_spec(ws: Workspace) -> LegacySpec:
+def load_spec(ws: RunWorkspace) -> LegacySpec:
try:
return LegacySpec.model_validate_json(ws.spec_path.read_text())
except Exception as e:
@@ -112,7 +112,7 @@ def print_record_summary(record: RunRecord, ws_root: Path | None = None) -> None
if record.error:
lines.append(f"[red]error: {record.error}[/red]")
if ws_root is not None:
- ws = Workspace(ws_root)
+ ws = RunWorkspace(ws_root)
lines.append(f"workspace: {ws_root}")
picked = next((r for r in record.rounds if r.index == s.round), None)
if picked is not None and picked.renders is not None and picked.renders.contact_sheet:
diff --git a/flywheel/c3d_flywheel/cli/layout_cmd.py b/flywheel/c3d_flywheel/cli/layout_cmd.py
index 2d69e1d2..a3245708 100644
--- a/flywheel/c3d_flywheel/cli/layout_cmd.py
+++ b/flywheel/c3d_flywheel/cli/layout_cmd.py
@@ -8,7 +8,6 @@ import json
from typing import Annotated, Any
import typer
-from codeverse3d.workspace import Workspace
from rich.table import Table
from c3d_flywheel.cli import _common as C
@@ -28,6 +27,7 @@ from c3d_flywheel.legacy import (
RunRecord,
SettingsSnapshot,
)
+from c3d_flywheel.runlayout import RunWorkspace, read_jsonl_lenient
def _human_bytes(n: int) -> str:
@@ -44,8 +44,8 @@ def _section(title: str) -> None:
# --------------------------------------------------------------------------- status
-def print_status(ws: Workspace, record: RunRecord | None, n_events: int) -> None:
- from codeverse3d.proc import read_json_or_none, read_jsonl_lenient
+def print_status(ws: RunWorkspace, record: RunRecord | None, n_events: int) -> None:
+ from codeverse3d.proc import read_json_or_none
_section("STATUS — where the run stands")
if record is not None:
@@ -98,7 +98,7 @@ def _deliverable_table(d: RunDeliverable) -> Table:
return t
-def _unpackaged_artifact_rows(ws: Workspace) -> dict[str, Any]:
+def _unpackaged_artifact_rows(ws: RunWorkspace) -> dict[str, Any]:
rows: dict[str, Any] = {}
for name in ("object.glb", "object.stl", "object.step", "robot.urdf", "preview.gif"):
p = ws.artifacts / name
@@ -109,7 +109,7 @@ def _unpackaged_artifact_rows(ws: Workspace) -> dict[str, Any]:
return rows
-def print_deliverable(ws: Workspace, record: RunRecord) -> None:
+def print_deliverable(ws: RunWorkspace, record: RunRecord) -> None:
from c3d_flywheel import select
from c3d_flywheel.record.deliverable import load_deliverable
@@ -129,7 +129,7 @@ def print_deliverable(ws: Workspace, record: RunRecord) -> None:
# --------------------------------------------------------------------------- (b) evidence
-def print_evidence(ws: Workspace, record: RunRecord) -> None:
+def print_evidence(ws: RunWorkspace, record: RunRecord) -> None:
from c3d_flywheel import select
from c3d_flywheel.dataset.sample import gate_error_summary
from c3d_flywheel.record.record import effective_judgment
@@ -185,7 +185,7 @@ def _roles_table(settings: SettingsSnapshot) -> Table:
return t
-def print_cost_and_settings(ws: Workspace, record: RunRecord) -> None:
+def print_cost_and_settings(ws: RunWorkspace, record: RunRecord) -> None:
from c3d_flywheel.record.telemetry import build_telemetry
_section("COST & SETTINGS — token price and the key step settings")
diff --git a/flywheel/c3d_flywheel/cost/ledger.py b/flywheel/c3d_flywheel/cost/ledger.py
index c5a100bd..9234f74d 100644
--- a/flywheel/c3d_flywheel/cost/ledger.py
+++ b/flywheel/c3d_flywheel/cost/ledger.py
@@ -7,9 +7,6 @@ from collections.abc import Iterable, Sequence
from pathlib import Path
from typing import Any
-from codeverse3d.contracts.common import Usage
-from codeverse3d.proc import iter_jsonl_lines
-
from c3d_flywheel.cost.pricing import (
estimate_cost,
price_provenance,
@@ -21,6 +18,8 @@ from c3d_flywheel.cost.types import (
Summary,
normalise_ids,
)
+from c3d_flywheel.legacy import Usage
+from c3d_flywheel.runlayout import iter_jsonl_lines
log = logging.getLogger(__name__)
diff --git a/flywheel/c3d_flywheel/cost/pricing.py b/flywheel/c3d_flywheel/cost/pricing.py
index c84466b0..b77c47d3 100644
--- a/flywheel/c3d_flywheel/cost/pricing.py
+++ b/flywheel/c3d_flywheel/cost/pricing.py
@@ -12,7 +12,7 @@ import threading
from dataclasses import dataclass
from typing import Any
-from codeverse3d.contracts.common import Usage
+from c3d_flywheel.legacy import Usage
log = logging.getLogger(__name__)
diff --git a/flywheel/c3d_flywheel/costreport/audit.py b/flywheel/c3d_flywheel/costreport/audit.py
index 5889acb3..f4934776 100644
--- a/flywheel/c3d_flywheel/costreport/audit.py
+++ b/flywheel/c3d_flywheel/costreport/audit.py
@@ -6,12 +6,11 @@ from collections.abc import Iterable
from dataclasses import dataclass, field
from pathlib import Path
-from codeverse3d.contracts.common import Usage
from codeverse3d.proc import read_json_or_none
from c3d_flywheel.cost.ledger import load_ledger, price_call, summarise
from c3d_flywheel.cost.types import CallCost, Stage, Summary
-from c3d_flywheel.legacy import RunId
+from c3d_flywheel.legacy import RunId, Usage
from c3d_flywheel.record.record import (
RecordError,
find_run_dirs,
diff --git a/flywheel/c3d_flywheel/dataset/export.py b/flywheel/c3d_flywheel/dataset/export.py
index 8fd09360..3e1532d4 100644
--- a/flywheel/c3d_flywheel/dataset/export.py
+++ b/flywheel/c3d_flywheel/dataset/export.py
@@ -11,7 +11,6 @@ from typing import Any, NamedTuple
from codeverse3d.contracts.common import ENTRY_FILE, Language
from codeverse3d.proc import read_json_or_none, sha256_file, write_json_atomic, write_text_atomic
-from codeverse3d.workspace import Workspace
from pydantic import BaseModel, Field
from c3d_flywheel.dataset import sample as S
@@ -19,6 +18,7 @@ from c3d_flywheel.dataset.quality import DuplicateGroup, code_sha256, mark_dupli
from c3d_flywheel.legacy import RunId, RunRecord
from c3d_flywheel.record.git_history import CODE_ROOTS
from c3d_flywheel.record.record import FoundRun, iter_runs
+from c3d_flywheel.runlayout import RunWorkspace
log = logging.getLogger(__name__)
@@ -45,7 +45,7 @@ class ExportReport(BaseModel):
def load_captions(
- ws: Workspace, record: RunRecord, captions_dir: Path | None = None, *, slug: str | None = None
+ ws: RunWorkspace, record: RunRecord, captions_dir: Path | None = None, *, slug: str | None = None
) -> dict[str, Any]:
caps = record.extra.get("captions") or {}
if caps.get("detailed"):
@@ -73,7 +73,7 @@ class ExportedSample(NamedTuple):
def export_one(
- ws: Workspace, record: RunRecord, out_dir: Path, *,
+ ws: RunWorkspace, record: RunRecord, out_dir: Path, *,
captions_dir: Path | None = None, run_id: RunId | None = None
) -> ExportedSample:
key = run_id.slug if run_id is not None else S.sample_key(ws)
diff --git a/flywheel/c3d_flywheel/dataset/pairs.py b/flywheel/c3d_flywheel/dataset/pairs.py
index acf30d37..7550eefe 100644
--- a/flywheel/c3d_flywheel/dataset/pairs.py
+++ b/flywheel/c3d_flywheel/dataset/pairs.py
@@ -7,14 +7,12 @@ from collections import defaultdict
from pathlib import Path
from typing import Any
-from codeverse3d.proc import read_jsonl_lenient
-from codeverse3d.workspace import Workspace
-
from c3d_flywheel import select
from c3d_flywheel.dataset.quality import prompt_id
from c3d_flywheel.legacy import RoundRecord, RunRecord
from c3d_flywheel.record import git_history
from c3d_flywheel.record.record import effective_judgment, iter_runs, skip_unreadable
+from c3d_flywheel.runlayout import RunWorkspace, read_jsonl_lenient
__all__ = ["build_pairs", "preference_pairs", "repair_pairs", "in_round_repair_pairs",
"cross_backend_pairs"]
@@ -22,7 +20,7 @@ __all__ = ["build_pairs", "preference_pairs", "repair_pairs", "in_round_repair_p
MAX_INLINE_CODE = 200_000
-def _side(ws: Workspace, rnd: RoundRecord, *, cache: dict[str, dict[str, Any]]) -> dict[str, Any]:
+def _side(ws: RunWorkspace, rnd: RoundRecord, *, cache: dict[str, dict[str, Any]]) -> dict[str, Any]:
key = rnd.commit or f"wt:{rnd.index}"
if key not in cache:
try:
@@ -45,7 +43,7 @@ def _side(ws: Workspace, rnd: RoundRecord, *, cache: dict[str, dict[str, Any]])
}
-def _base(ws: Workspace, rec: RunRecord, kind: str, *, slug: str | None = None) -> dict[str, Any]:
+def _base(ws: RunWorkspace, rec: RunRecord, kind: str, *, slug: str | None = None) -> dict[str, Any]:
return {
"kind": kind,
"run": slug or ws.root.name,
@@ -62,7 +60,7 @@ def _base(ws: Workspace, rec: RunRecord, kind: str, *, slug: str | None = None)
MIN_PREFERENCE_DELTA = 0.05
-def preference_pairs(ws: Workspace, rec: RunRecord, *, min_delta: float,
+def preference_pairs(ws: RunWorkspace, rec: RunRecord, *, min_delta: float,
slug: str | None = None) -> list[dict[str, Any]]:
cache: dict[str, dict[str, Any]] = {}
judged = [r for r in rec.rounds if effective_judgment(r) is not None and r.commit]
@@ -100,7 +98,7 @@ def _error_of(rnd: RoundRecord) -> dict[str, Any]:
}
-def repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:
+def repair_pairs(ws: RunWorkspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:
cache: dict[str, dict[str, Any]] = {}
out = []
rounds = rec.rounds
@@ -123,7 +121,7 @@ def repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> l
return out
-def _round_build_errors(ws: Workspace, index: int) -> list[str]:
+def _round_build_errors(ws: RunWorkspace, index: int) -> list[str]:
out: list[str] = []
for ev in read_jsonl_lenient(ws.events_path, dicts_only=True):
if ev.get("event") == "build.done" and ev.get("round") == index and ev.get("ok") is False:
@@ -133,7 +131,7 @@ def _round_build_errors(ws: Workspace, index: int) -> list[str]:
return out
-def in_round_repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:
+def in_round_repair_pairs(ws: RunWorkspace, rec: RunRecord, *, slug: str | None = None) -> list[dict[str, Any]]:
cache: dict[str, dict[str, Any]] = {}
out = []
for rnd in rec.rounds:
@@ -169,7 +167,7 @@ def in_round_repair_pairs(ws: Workspace, rec: RunRecord, *, slug: str | None = N
def cross_backend_pairs(
- groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord, str]]], *, min_delta: float
+ groups: dict[tuple[str, str, str], list[tuple[RunWorkspace, RunRecord, str]]], *, min_delta: float
) -> list[dict[str, Any]]:
out = []
for (_h, _t, _l), runs in groups.items():
@@ -209,7 +207,7 @@ def cross_backend_pairs(
def build_pairs(runs_dir: Path | str, out_jsonl: Path | str, *, min_delta: float = MIN_PREFERENCE_DELTA) -> int:
out = Path(out_jsonl)
out.parent.mkdir(parents=True, exist_ok=True)
- groups: dict[tuple[str, str, str], list[tuple[Workspace, RunRecord, str]]] = defaultdict(list)
+ groups: dict[tuple[str, str, str], list[tuple[RunWorkspace, RunRecord, str]]] = defaultdict(list)
n = 0
tmp = out.with_suffix(out.suffix + ".tmp")
with tmp.open("w") as fh:
diff --git a/flywheel/c3d_flywheel/dataset/refine.py b/flywheel/c3d_flywheel/dataset/refine.py
index 2e0bc0ec..bf2502be 100644
--- a/flywheel/c3d_flywheel/dataset/refine.py
+++ b/flywheel/c3d_flywheel/dataset/refine.py
@@ -8,7 +8,6 @@ from pathlib import Path
from typing import Any
from codeverse3d.contracts.artifacts import Severity
-from codeverse3d.workspace import Workspace
from pydantic import BaseModel, Field
from c3d_flywheel.dataset.pairs import MIN_PREFERENCE_DELTA
@@ -20,6 +19,7 @@ from c3d_flywheel.record.git_history import (
read_tree_at,
)
from c3d_flywheel.record.record import effective_judgment, effective_score, iter_runs
+from c3d_flywheel.runlayout import RunWorkspace
REFINE_KINDS: tuple[str, ...] = ("refine", "rewrite", "detail")
MAX_DIFF_BYTES = 200_000
@@ -76,7 +76,7 @@ def outcome_of(delta: float | None, threshold: float) -> str:
return "regressed" if delta <= -threshold else "unchanged"
-def _transition(ws: Workspace, rec: RunRecord, prev: RoundRecord, cur: RoundRecord, *, battery: str, run: str,
+def _transition(ws: RunWorkspace, rec: RunRecord, prev: RoundRecord, cur: RoundRecord, *, battery: str, run: str,
threshold: float, max_diff_bytes: int, with_code: bool) -> RefineTransition:
diff, total, truncated = diff_between(ws, prev.commit, cur.commit, max_bytes=max_diff_bytes)
changed = changed_files_between(ws, prev.commit, cur.commit)
diff --git a/flywheel/c3d_flywheel/dataset/sample.py b/flywheel/c3d_flywheel/dataset/sample.py
index 54a5508d..e0fdd853 100644
--- a/flywheel/c3d_flywheel/dataset/sample.py
+++ b/flywheel/c3d_flywheel/dataset/sample.py
@@ -7,14 +7,7 @@ from datetime import datetime
from pathlib import Path
from typing import Any
-from codeverse3d.contracts.common import (
- ENTRY_FILE,
- LANGUAGE_LABEL,
- TRACK_INFO,
- Language,
- code_file,
-)
-from codeverse3d.workspace import Workspace
+from codeverse3d.contracts.common import ENTRY_FILE, Language
from pydantic import BaseModel, Field
from c3d_flywheel import select
@@ -24,10 +17,11 @@ from c3d_flywheel.dataset.quality import (
prompt_id,
quality_tier,
)
-from c3d_flywheel.legacy import RoundRecord, RunRecord
+from c3d_flywheel.legacy import LANGUAGE_LABEL, TRACK_LABEL, RoundRecord, RunRecord, code_file
from c3d_flywheel.record import git_history
from c3d_flywheel.record.deliverable import load_deliverable, round_outputs
from c3d_flywheel.record.record import effective_judgment, round_summary, stop_reason
+from c3d_flywheel.runlayout import RunWorkspace
MAX_GLB_BYTES = 20 * 1024 * 1024
SOURCE_NAME = "3dcodeverse"
@@ -104,7 +98,7 @@ class SampleMeta(BaseModel):
description="compact accounting digest from record.telemetry ({} when absent)")
-def sample_key(ws: Workspace) -> str:
+def sample_key(ws: RunWorkspace) -> str:
return ws.root.name
@@ -116,12 +110,12 @@ def sample_rel_dir(record: RunRecord, key: str) -> Path:
return Path(record.spec.track.value) / record.spec.language.value / key
-def exported_round(ws: Workspace, record: RunRecord) -> RoundRecord | None:
+def exported_round(ws: RunWorkspace, record: RunRecord) -> RoundRecord | None:
shown = select.summarise(ws.root, record=record).round
return next((r for r in record.rounds if r.index == shown), record.rounds[-1] if record.rounds else None)
-def code_files_for_round(ws: Workspace, rnd: RoundRecord | None) -> tuple[dict[str, bytes], str]:
+def code_files_for_round(ws: RunWorkspace, rnd: RoundRecord | None) -> tuple[dict[str, bytes], str]:
"""The round's code at its commit; a packaged ``deliverable/`` only when its manifest names that round."""
if rnd is None:
return git_history.read_working_tree(ws), "working_tree"
@@ -134,7 +128,7 @@ def code_files_for_round(ws: Workspace, rnd: RoundRecord | None) -> tuple[dict[s
+ (f"round {manifest.round}" if manifest is not None else "no manifest"))
-def _deliverable_code(ws: Workspace) -> dict[str, bytes]:
+def _deliverable_code(ws: RunWorkspace) -> dict[str, bytes]:
files: dict[str, bytes] = {}
for root in git_history.CODE_ROOTS:
base = ws.deliverable / root
@@ -164,7 +158,7 @@ def write_code_tree(dest: Path, files: dict[str, bytes], language: Language) ->
return code_name, written
-def copy_renders(ws: Workspace, rnd: RoundRecord | None, dest: Path) -> list[str]:
+def copy_renders(ws: RunWorkspace, rnd: RoundRecord | None, dest: Path) -> list[str]:
out: list[str] = []
rdir = dest / "renders"
rdir.mkdir(parents=True, exist_ok=True)
@@ -201,7 +195,7 @@ def copy_renders(ws: Workspace, rnd: RoundRecord | None, dest: Path) -> list[str
return out
-def copy_link_meshes(ws: Workspace, rnd: RoundRecord | None, dest: Path) -> list[str]:
+def copy_link_meshes(ws: RunWorkspace, rnd: RoundRecord | None, dest: Path) -> list[str]:
out_dir = round_outputs(ws, rnd)
src = out_dir / "meshes" if out_dir is not None else None
if src is None or not src.is_dir():
@@ -233,7 +227,7 @@ def gate_error_summary(rnd: RoundRecord | None) -> dict[str, int]:
def build_meta(
- ws: Workspace, record: RunRecord, *, key: str, entry: str, files: list[str], renders: list[str],
+ ws: RunWorkspace, record: RunRecord, *, key: str, entry: str, files: list[str], renders: list[str],
rnd: RoundRecord | None, code_source: str, code: dict[str, bytes] | None = None,
captions: dict[str, Any] | None = None,
) -> SampleMeta:
@@ -251,7 +245,7 @@ def build_meta(
id=sample_id(record, key),
key=key,
name=name,
- type=TRACK_INFO[spec.track].label,
+ type=TRACK_LABEL[spec.track],
track=spec.track.value,
language=spec.language.value,
language_label=LANGUAGE_LABEL[spec.language],
diff --git a/flywheel/c3d_flywheel/gallery/detail.py b/flywheel/c3d_flywheel/gallery/detail.py
index c45d0f77..003ca670 100644
--- a/flywheel/c3d_flywheel/gallery/detail.py
+++ b/flywheel/c3d_flywheel/gallery/detail.py
@@ -6,7 +6,6 @@ from pathlib import Path
from urllib.parse import quote
from codeverse3d.contracts.common import ENTRY_FILE
-from codeverse3d.workspace import Workspace
from c3d_flywheel.gallery.cards import fmt, gallery_figure, tier_tag, verdict_tag
from c3d_flywheel.gallery.code import CODE_CSS, numbered, read_text, src_files
@@ -16,6 +15,7 @@ from c3d_flywheel.gallery.theme import esc, footer, page_shell, top_bar
from c3d_flywheel.gallery.urls import UrlMaker
from c3d_flywheel.legacy import RoundRecord, RunRecord
from c3d_flywheel.record.record import effective_judgment
+from c3d_flywheel.runlayout import RunWorkspace
DETAIL_CSS = CODE_CSS + """
.hero{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.05fr);gap:var(--s-4);align-items:start}
@@ -222,7 +222,7 @@ def _complexity_panel(entry: RunEntry, rec: RunRecord) -> str:
def _renders_panel(entry: RunEntry, urls: UrlMaker, rec: RunRecord) -> str:
- ws = Workspace(entry.path)
+ ws = RunWorkspace(entry.path)
rnd = _picked(entry, rec)
figs: list[tuple[str, str]] = []
if rnd is not None and rnd.renders is not None:
@@ -330,7 +330,7 @@ document.addEventListener('keydown',function(e){
"""
-def render_detail(entry: RunEntry, urls: UrlMaker, ws: Workspace, rec: RunRecord, *,
+def render_detail(entry: RunEntry, urls: UrlMaker, ws: RunWorkspace, rec: RunRecord, *,
prev: RunEntry | None = None, nxt: RunEntry | None = None) -> str:
nav = "".join(f"<a href='#{a}'>{a}</a>" for a in
("rounds", "judge", "complexity", "measurement", "renders", "cost", "code"))
diff --git a/flywheel/c3d_flywheel/gallery/index.py b/flywheel/c3d_flywheel/gallery/index.py
index 0765a2e0..bccfa841 100644
--- a/flywheel/c3d_flywheel/gallery/index.py
+++ b/flywheel/c3d_flywheel/gallery/index.py
@@ -7,7 +7,6 @@ from pathlib import Path
from codeverse3d.conventions import views_by_preference
from codeverse3d.proc import read_json_or_none
-from codeverse3d.workspace import Workspace
from c3d_flywheel import select
from c3d_flywheel.dataset.quality import quality_tier
@@ -30,6 +29,7 @@ from c3d_flywheel.record.record import (
is_run_dir,
load_record,
)
+from c3d_flywheel.runlayout import RunWorkspace
DEFAULT_RUNS = "runs"
DEFAULT_BENCH_GLOB = "eval/bench/out/*/runs"
@@ -44,7 +44,7 @@ def default_roots(base: Path | str = ".") -> list[Path]:
return roots
-def run_rel(ws: Workspace, path: Path | str | None) -> str:
+def run_rel(ws: RunWorkspace, path: Path | str | None) -> str:
"""``path`` (rebased into this workspace) as a run-relative posix string, or ``""``
when it is outside the run."""
if not path:
@@ -62,7 +62,7 @@ def _first_file(run_dir: Path, *rels: str) -> str:
return ""
-def _round_sheet(ws: Workspace, rec: RunRecord, index: int) -> str:
+def _round_sheet(ws: RunWorkspace, rec: RunRecord, index: int) -> str:
rnd = next((r for r in rec.rounds if r.index == index), None)
if rnd is not None and rnd.renders is not None and rnd.renders.contact_sheet:
rel = run_rel(ws, rnd.renders.contact_sheet)
@@ -71,7 +71,7 @@ def _round_sheet(ws: Workspace, rec: RunRecord, index: int) -> str:
return _first_file(ws.root, f"artifacts/renders/r{index:02d}/sheet.png")
-def picked_sheet(ws: Workspace, rec: RunRecord, picked: int | None) -> str:
+def picked_sheet(ws: RunWorkspace, rec: RunRecord, picked: int | None) -> str:
return (_round_sheet(ws, rec, picked) if picked is not None else "") or _first_file(
ws.root, "deliverable/sheet.png", "artifacts/frames_sheet.png")
@@ -79,7 +79,7 @@ def picked_sheet(ws: Workspace, rec: RunRecord, picked: int | None) -> str:
HERO_PREFERENCE = ("front_right_high", "back_left_high", "front_right_low", "front_left_low", "front")
-def hero_view(ws: Workspace, rec: RunRecord, picked: int | None) -> tuple[str, str, int]:
+def hero_view(ws: RunWorkspace, rec: RunRecord, picked: int | None) -> tuple[str, str, int]:
rnd = next((r for r in rec.rounds if r.index == picked), None)
if rnd is None or rnd.renders is None or not rnd.renders.views:
return "", "", 0
@@ -93,12 +93,12 @@ def hero_view(ws: Workspace, rec: RunRecord, picked: int | None) -> tuple[str, s
return rel, humanize_view(chosen.name), len(views)
-def _articulation_sheet(ws: Workspace, picked: int | None) -> str:
+def _articulation_sheet(ws: RunWorkspace, picked: int | None) -> str:
rel = f"artifacts/renders/r{picked:02d}/poses/articulation_sheet.png" if picked is not None else ""
return rel if rel and (ws.root / rel).is_file() else ""
-def entry_links(ws: Workspace, rec: RunRecord | None, picked: int | None) -> list[RunLink]:
+def entry_links(ws: RunWorkspace, rec: RunRecord | None, picked: int | None) -> list[RunLink]:
run = ws.root
rnd = next((r for r in rec.rounds if r.index == picked), None) if rec is not None else None
out = round_outputs(ws, rnd)
@@ -141,7 +141,7 @@ def entry_links(ws: Workspace, rec: RunRecord | None, picked: int | None) -> lis
return links
-def round_rows(ws: Workspace, rec: RunRecord) -> list[RoundRow]:
+def round_rows(ws: RunWorkspace, rec: RunRecord) -> list[RoundRow]:
rounds = {r.index: r for r in rec.rounds}
return [RoundRow(**{**row.model_dump(), "commit": row.commit[:12]}, sheet=_round_sheet(ws, rec, row.index),
gates={k: v for k, v in gate_error_summary(rounds[row.index]).items() if v})
@@ -152,7 +152,7 @@ _CX_AXES = ("part_count", "assembly_depth", "tri_count", "materials",
"silhouette", "feature_density", "symmetry_groups", "hollowness")
-def _complexity(ws: Workspace, rec: RunRecord, picked: int | None) -> tuple[float | None, str, dict[str, float]]:
+def _complexity(ws: RunWorkspace, rec: RunRecord, picked: int | None) -> tuple[float | None, str, dict[str, float]]:
try:
block = select.round_complexity_block(next((r for r in rec.rounds if r.index == picked), None))
except Exception: # noqa: BLE001
@@ -163,7 +163,7 @@ def _complexity(ws: Workspace, rec: RunRecord, picked: int | None) -> tuple[floa
return float(block["index"]), str(block.get("band") or ""), axes
-def entry_from_record(battery: str, ws: Workspace, rec: RunRecord, *, slug: str | None = None) -> RunEntry:
+def entry_from_record(battery: str, ws: RunWorkspace, rec: RunRecord, *, slug: str | None = None) -> RunEntry:
summary = select.summarise(ws.root, record=rec)
picked = summary.round
rnd = next((r for r in rec.rounds if r.index == picked), None)
@@ -198,7 +198,7 @@ def _spec_fields(run_dir: Path) -> dict[str, str]:
def entry_for_dir(battery: str, run_dir: Path, *, slug: str | None = None) -> RunEntry:
- ws = Workspace(run_dir)
+ ws = RunWorkspace(run_dir)
slug = slug or run_dir.name
fields = _spec_fields(run_dir)
if not (run_dir / "record.json").is_file():
diff --git a/flywheel/c3d_flywheel/gallery/server.py b/flywheel/c3d_flywheel/gallery/server.py
index 7b662509..16b1f117 100644
--- a/flywheel/c3d_flywheel/gallery/server.py
+++ b/flywheel/c3d_flywheel/gallery/server.py
@@ -20,6 +20,7 @@ from c3d_flywheel.gallery.model import FILTER_KEYS, RunEntry, match, sort_entrie
from c3d_flywheel.gallery.page import render_index
from c3d_flywheel.gallery.theme import esc, footer, page_shell, top_bar
from c3d_flywheel.gallery.urls import PathError, UrlMaker, content_type, safe_join
+from c3d_flywheel.runlayout import RunWorkspace
DEFAULT_HOST = "127.0.0.1"
DEFAULT_PORT = 8765
@@ -184,11 +185,9 @@ class GalleryApp:
return Response(content_type=ctype, path=target, headers=headers)
def _detail(self, entry: RunEntry) -> Response:
- from codeverse3d.workspace import Workspace
-
from c3d_flywheel.record.record import RecordError, load_record
- ws = Workspace(entry.path)
+ ws = RunWorkspace(entry.path)
try:
rec = load_record(ws)
except (RecordError, OSError, ValueError) as e:
diff --git a/flywheel/c3d_flywheel/legacy.py b/flywheel/c3d_flywheel/legacy.py
index 99c0f13c..cf784add 100644
--- a/flywheel/c3d_flywheel/legacy.py
+++ b/flywheel/c3d_flywheel/legacy.py
@@ -18,10 +18,63 @@ from codeverse3d.contracts.artifacts import (
Measurement,
RenderSet,
)
-from codeverse3d.contracts.common import Usage
+from codeverse3d.contracts.common import ENTRY_FILE, Language, Track
from codeverse3d.contracts.spec import Spec
from pydantic import BaseModel, ConfigDict, Field
+# ===================================================================== labels (the plugin keeps only the enums)
+TRACK_LABEL: dict[Track, str] = {
+ Track.STATIC_OBJECT: "3D Objects",
+ Track.ARTICULATED_OBJECT: "Articulated Objects",
+ Track.SCENE: "3D Scenes",
+ Track.GRAPHICS: "Procedural Graphics",
+}
+
+LANGUAGE_LABEL: dict[Language, str] = {
+ Language.BLENDER: "Blender Python",
+ Language.CADQUERY: "CadQuery (Python)",
+ Language.URDF_BLENDER: "URDF + Blender Python",
+ Language.THREEJS: "Three.js (ESM)",
+ Language.SCENE_THREEJS: "Three.js scene (multi-file ESM + GLSL)",
+ Language.SCENE_BLENDER: "Blender scene (multi-file bpy)",
+ Language.GLSL_SHADER: "GLSL fragment shader",
+ Language.OPENGL_PYTHON: "OpenGL (moderngl Python + GLSL)",
+}
+
+
+def code_file(language: Language) -> str:
+ """Top-level dataset copy of the entry file: ``code`` + the entry suffix."""
+ entry = ENTRY_FILE[language]
+ return "code" + entry[entry.rfind(".") :]
+
+
+# ===================================================================== usage (one model / agent call)
+class Usage(BaseModel):
+ """Normalised token/cost accounting for ONE model or agent call, as v0.0.x recorded it."""
+
+ backend: str = ""
+ model: str = ""
+ input_tokens: int = 0
+ output_tokens: int = 0
+ cached_tokens: int = 0
+ thoughts_tokens: int = 0
+ tool_calls: int = 0
+ cost_usd: float = 0.0
+ latency_ms: int = 0
+
+ def __add__(self, other: Usage) -> Usage:
+ return Usage(
+ backend=self.backend or other.backend,
+ model=self.model if self.model == other.model else (self.model or other.model),
+ input_tokens=self.input_tokens + other.input_tokens,
+ output_tokens=self.output_tokens + other.output_tokens,
+ cached_tokens=self.cached_tokens + other.cached_tokens,
+ thoughts_tokens=self.thoughts_tokens + other.thoughts_tokens,
+ tool_calls=self.tool_calls + other.tool_calls,
+ cost_usd=self.cost_usd + other.cost_usd,
+ latency_ms=self.latency_ms + other.latency_ms,
+ )
+
# ===================================================================== spec + plan (as recorded)
class Budget(BaseModel):
diff --git a/flywheel/c3d_flywheel/record/deliverable.py b/flywheel/c3d_flywheel/record/deliverable.py
index 37e04e53..c4f19754 100644
--- a/flywheel/c3d_flywheel/record/deliverable.py
+++ b/flywheel/c3d_flywheel/record/deliverable.py
@@ -14,10 +14,10 @@ from pathlib import Path
from codeverse3d.contracts.common import ENTRY_FILE, Language, Track
from codeverse3d.proc import write_json_atomic
-from codeverse3d.workspace import Workspace
from c3d_flywheel.legacy import DeliverableFile, RoundRecord, RunDeliverable, RunRecord
from c3d_flywheel.record import git_history
+from c3d_flywheel.runlayout import RunWorkspace
log = logging.getLogger(__name__)
@@ -34,7 +34,7 @@ _ARTIFACT_ROLES: tuple[tuple[str, str], ...] = (
_MESH_DIR = "meshes"
-def keep_round_artifacts(ws: Workspace, round_index: int) -> list[str]:
+def keep_round_artifacts(ws: RunWorkspace, round_index: int) -> list[str]:
"""Copy the round's build outputs to ``artifacts/rNN/`` (names relative to ``artifacts/``).
Real copies, never hard links, so a later in-place build cannot change a kept round.
@@ -54,7 +54,7 @@ def keep_round_artifacts(ws: Workspace, round_index: int) -> list[str]:
class _Writer:
"""Collects files into ``deliverable/`` as real copies (a hard link would let edits break the manifest sha256)."""
- def __init__(self, ws: Workspace):
+ def __init__(self, ws: RunWorkspace):
self.ws = ws
self.files: list[DeliverableFile] = []
@@ -69,7 +69,7 @@ class _Writer:
self.add_bytes(rel, src.read_bytes(), role)
-def _code_tree(ws: Workspace, rnd: RoundRecord | None) -> dict[str, bytes]:
+def _code_tree(ws: RunWorkspace, rnd: RoundRecord | None) -> dict[str, bytes]:
"""``src/**`` (+ ``public/**``) at the round's commit; the working tree only when no round is named."""
if rnd is None:
return git_history.read_working_tree(ws)
@@ -78,7 +78,7 @@ def _code_tree(ws: Workspace, rnd: RoundRecord | None) -> dict[str, bytes]:
return git_history.read_tree_at(ws, rnd.commit)
-def round_outputs(ws: Workspace, rnd: RoundRecord | None) -> Path | None:
+def round_outputs(ws: RunWorkspace, rnd: RoundRecord | None) -> Path | None:
"""The build outputs retained for this round; never substitute another build."""
if rnd is None:
return None
@@ -86,7 +86,7 @@ def round_outputs(ws: Workspace, rnd: RoundRecord | None) -> Path | None:
return kept if kept.is_dir() else None
-def _copy_artifacts(ws: Workspace, record: RunRecord, rnd: RoundRecord | None, out: Path | None, w: _Writer) -> None:
+def _copy_artifacts(ws: RunWorkspace, record: RunRecord, rnd: RoundRecord | None, out: Path | None, w: _Writer) -> None:
if out is None:
return
for name, role in _ARTIFACT_ROLES:
@@ -100,7 +100,7 @@ def _copy_artifacts(ws: Workspace, record: RunRecord, rnd: RoundRecord | None, o
w.add_file(src, f"frames/{src.name}", "frames")
-def _copy_sheet(ws: Workspace, rnd: RoundRecord | None, w: _Writer) -> None:
+def _copy_sheet(ws: RunWorkspace, rnd: RoundRecord | None, w: _Writer) -> None:
if rnd is None or rnd.renders is None or not rnd.renders.contact_sheet:
return
sheet = ws.rebase(rnd.renders.contact_sheet)
@@ -108,7 +108,7 @@ def _copy_sheet(ws: Workspace, rnd: RoundRecord | None, w: _Writer) -> None:
w.add_file(sheet, f"sheet{sheet.suffix or '.png'}", "sheet")
-def _copy_captions(ws: Workspace, record: RunRecord, w: _Writer) -> None:
+def _copy_captions(ws: RunWorkspace, record: RunRecord, w: _Writer) -> None:
caps = record.extra.get("captions") or {}
if not caps.get("detailed"):
w.add_file(ws.root / "captions.json", "captions.json", "captions")
@@ -118,7 +118,7 @@ def _copy_captions(ws: Workspace, record: RunRecord, w: _Writer) -> None:
indent=2, ensure_ascii=False).encode(), "captions")
-def build_deliverable(ws: Workspace, record: RunRecord, round_index: int | None, *, clean: bool = True) -> RunDeliverable:
+def build_deliverable(ws: RunWorkspace, record: RunRecord, round_index: int | None, *, clean: bool = True) -> RunDeliverable:
"""(Re)build ``<run>/deliverable/`` for round ``round_index`` and return the manifest (idempotent)."""
rnd = next((r for r in record.rounds if r.index == round_index), None)
out = round_outputs(ws, rnd)
@@ -142,7 +142,7 @@ def build_deliverable(ws: Workspace, record: RunRecord, round_index: int | None,
return manifest
-def load_deliverable(ws: Workspace) -> RunDeliverable | None:
+def load_deliverable(ws: RunWorkspace) -> RunDeliverable | None:
"""``deliverable/manifest.json``, else None."""
path = ws.deliverable_manifest_path
if not path.is_file():
diff --git a/flywheel/c3d_flywheel/record/git_history.py b/flywheel/c3d_flywheel/record/git_history.py
index 5d7b6579..d3242a57 100644
--- a/flywheel/c3d_flywheel/record/git_history.py
+++ b/flywheel/c3d_flywheel/record/git_history.py
@@ -11,7 +11,7 @@ import subprocess
from collections.abc import Collection
from pathlib import Path
-from codeverse3d.workspace import GIT_SAFE_DIFF_FLAGS, GIT_SAFE_FLAGS, Workspace, git_safe_env
+from c3d_flywheel.runlayout import GIT_SAFE_DIFF_FLAGS, GIT_SAFE_FLAGS, RunWorkspace, git_safe_env
_GIT_TIMEOUT_S = 60
@@ -24,7 +24,7 @@ class GitReadError(RuntimeError):
"""A git read (ls-tree / cat-file / diff) failed or the commit does not exist."""
-def _run(ws: Workspace, *args: str, stdin: bytes | None = None) -> subprocess.CompletedProcess[bytes]:
+def _run(ws: RunWorkspace, *args: str, stdin: bytes | None = None) -> subprocess.CompletedProcess[bytes]:
"""One sanitised git invocation; every failure is a :class:`GitReadError`."""
try:
proc = subprocess.run(
@@ -39,7 +39,7 @@ def _run(ws: Workspace, *args: str, stdin: bytes | None = None) -> subprocess.Co
return proc
-def commit_by_subject(ws: Workspace, subject: str) -> str | None:
+def commit_by_subject(ws: RunWorkspace, subject: str) -> str | None:
"""SHA of the newest commit whose subject line equals ``subject`` (None = no match)."""
try:
out = _run(ws, "log", "--format=%H%x00%s").stdout
@@ -57,7 +57,7 @@ def _keep(rel: str) -> bool:
return bool(parts) and parts[0] in CODE_ROOTS and not any(p in _SKIP_PARTS for p in parts)
-def read_tree_at(ws: Workspace, commit: str, *, paths: Collection[str] | None = None) -> dict[str, bytes]:
+def read_tree_at(ws: RunWorkspace, commit: str, *, paths: Collection[str] | None = None) -> dict[str, bytes]:
"""``{repo-relative path: bytes}`` for every code file at ``commit``; ``paths`` narrows it.
``ls-tree`` + ``cat-file --batch``, never ``git archive``: archive runs an agent-planted
@@ -95,7 +95,7 @@ def read_tree_at(ws: Workspace, commit: str, *, paths: Collection[str] | None =
return files
-def diff_between(ws: Workspace, before: str, after: str, *, max_bytes: int | None = None) -> tuple[str, int, bool]:
+def diff_between(ws: RunWorkspace, before: str, after: str, *, max_bytes: int | None = None) -> tuple[str, int, bool]:
"""Unified diff of the code roots between two commits: ``(text, total_bytes, truncated)``.
``total_bytes`` is the uncapped size. An unknown sha raises."""
@@ -107,13 +107,13 @@ def diff_between(ws: Workspace, before: str, after: str, *, max_bytes: int | Non
return raw.decode("utf-8", errors="replace"), total, False
-def changed_files_between(ws: Workspace, before: str, after: str) -> list[str]:
+def changed_files_between(ws: RunWorkspace, before: str, after: str) -> list[str]:
"""Sorted code-root paths that differ between two commits."""
out = _run(ws, "diff", *GIT_SAFE_DIFF_FLAGS, "--name-only", "-z", before, after, "--", *CODE_ROOTS).stdout
return sorted(p for p in out.decode("utf-8", errors="replace").split("\0") if p and _keep(p))
-def read_working_tree(ws: Workspace) -> dict[str, bytes]:
+def read_working_tree(ws: RunWorkspace) -> dict[str, bytes]:
"""The current ``src/`` + ``public/``, for a round with no commit."""
files: dict[str, bytes] = {}
for root in CODE_ROOTS:
diff --git a/flywheel/c3d_flywheel/record/record.py b/flywheel/c3d_flywheel/record/record.py
index e037a3c5..29b1f7a6 100644
--- a/flywheel/c3d_flywheel/record/record.py
+++ b/flywheel/c3d_flywheel/record/record.py
@@ -8,9 +8,9 @@ from pathlib import Path
from typing import Any, NamedTuple
from codeverse3d.contracts.artifacts import Judgment
-from codeverse3d.workspace import Workspace
from c3d_flywheel.legacy import RoundRecord, RunId, RunRecord
+from c3d_flywheel.runlayout import RunWorkspace
log = logging.getLogger(__name__)
@@ -81,8 +81,8 @@ def round_summary(r: RoundRecord) -> dict[str, Any]:
}
-def load_record(ws: Workspace | Path | str) -> RunRecord:
- ws = ws if isinstance(ws, Workspace) else Workspace(ws)
+def load_record(ws: RunWorkspace | Path | str) -> RunRecord:
+ ws = ws if isinstance(ws, RunWorkspace) else RunWorkspace(ws)
if not ws.record_path.is_file():
raise RecordError(f"no record.json in {ws.root}")
try:
@@ -127,13 +127,13 @@ def battery_label(root: Path | str) -> str:
def run_id_for(root: Path | str, run_dir: Path) -> RunId:
- """The ``RunId`` of ``run_dir`` under scan root ``root`` (minted here: a Workspace forgets the scan root)."""
+ """The ``RunId`` of ``run_dir`` under scan root ``root`` (minted here: a RunWorkspace forgets the scan root)."""
return RunId(battery=battery_label(root), rel=run_dir.relative_to(Path(root)).as_posix())
class FoundRun(NamedTuple):
- ws: Workspace
+ ws: RunWorkspace
record: RunRecord
run_id: RunId
@@ -201,7 +201,7 @@ def iter_runs(
f"({', '.join(battery_dirs)}/) but holds no record.json within "
f"{RUN_SEARCH_DEPTH} levels")
for d in children:
- ws = Workspace(d)
+ ws = RunWorkspace(d)
try:
rec = load_record(ws)
except RecordError as e:
diff --git a/flywheel/c3d_flywheel/record/telemetry.py b/flywheel/c3d_flywheel/record/telemetry.py
index 7b9108e8..a65fc50a 100644
--- a/flywheel/c3d_flywheel/record/telemetry.py
+++ b/flywheel/c3d_flywheel/record/telemetry.py
@@ -4,18 +4,17 @@ from __future__ import annotations
import logging
-from codeverse3d.workspace import Workspace
-
from c3d_flywheel.cost.ledger import load_ledger, summarise
from c3d_flywheel.cost.types import STAGE_ORDER, CallCost
from c3d_flywheel.legacy import CostSummary, RunRecord, RunTelemetry, StageCost
from c3d_flywheel.record.record import effective_score
+from c3d_flywheel.runlayout import RunWorkspace
log = logging.getLogger(__name__)
# --------------------------------------------------------------------------- the ledger (c3d_flywheel.cost)
-def ledger_rows(ws: Workspace) -> list[CallCost]:
+def ledger_rows(ws: RunWorkspace) -> list[CallCost]:
"""The run's priced per-call rows (none without a ledger)."""
try:
return load_ledger(ws.root)
@@ -47,6 +46,6 @@ def cost_summary(record: RunRecord, rows: list[CallCost]) -> CostSummary:
)
-def build_telemetry(ws: Workspace, record: RunRecord) -> RunTelemetry:
+def build_telemetry(ws: RunWorkspace, record: RunRecord) -> RunTelemetry:
"""The cost summary of one run, computed from its ledger (no settings snapshot: that is only ever read)."""
return RunTelemetry(cost=cost_summary(record, ledger_rows(ws)))
diff --git a/flywheel/c3d_flywheel/runlayout.py b/flywheel/c3d_flywheel/runlayout.py
new file mode 100644
index 00000000..8c491839
--- /dev/null
+++ b/flywheel/c3d_flywheel/runlayout.py
@@ -0,0 +1,179 @@
+"""The v0.0.x run directory as the harness laid it out, read-only.
+
+The plugin's ``codeverse3d.workspace.Workspace`` is a project workspace (``spec.json`` / ``src/`` /
+``artifacts/`` / ``judge/``); a RECORDED run adds ``record.json``, ``events.jsonl``, ``run_state.json``,
+``deliverable/``, ``telemetry/`` and a git repository of ``src/``. This module carries those paths,
+the sanitised git read helpers and the JSONL readers the flywheel needs; nothing here writes a run
+except ``pick`` (``deliverable/`` + ``selection.json``).
+"""
+
+from __future__ import annotations
+
+import contextlib
+import json
+import logging
+import subprocess
+from collections.abc import Iterator
+from pathlib import Path
+from typing import Any
+
+from codeverse3d.proc import write_json_atomic
+from pydantic import BaseModel
+
+DELIVERABLE_DIR = "deliverable"
+TELEMETRY_DIR = "telemetry"
+
+#: prepended to every git argv run against a run directory: an agent-planted hook, fsmonitor or
+#: global attributes file never runs. ``-c`` outranks ``.git/config``.
+GIT_SAFE_FLAGS = ("-c", "core.hooksPath=/dev/null", "-c", "core.fsmonitor=false",
+ "-c", "core.attributesFile=/dev/null")
+#: added to every git command that renders file content: a named diff / textconv driver in the
+#: agent-writable .git/config is not reachable by ``-c`` and would execute without these.
+GIT_SAFE_DIFF_FLAGS = ("--no-ext-diff", "--no-textconv")
+#: a has_commit probe that has not answered by then answers no
+HAS_COMMIT_TIMEOUT_S = 60
+
+
+def _git_home() -> str:
+ """HOME for git in a run directory: ours, never the run's."""
+ d = Path.home() / ".cache" / "c3d_flywheel" / "githome"
+ with contextlib.suppress(OSError):
+ d.mkdir(parents=True, exist_ok=True)
+ return str(d)
+
+
+def git_safe_env(**extra: str) -> dict[str, str]:
+ """Environment for git in an agent-written repository: no system / global config, fixed PATH,
+ no credential prompt."""
+ return {"PATH": "/usr/bin:/bin:/usr/local/bin", "HOME": _git_home(),
+ "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_NOSYSTEM": "1",
+ "GIT_TERMINAL_PROMPT": "0", **extra}
+
+
+# --------------------------------------------------------------------------- jsonl
+def iter_jsonl_lines(path: Path | str) -> Iterator[tuple[int, str]]:
+ """``(line_no, line)`` for every non-blank line of a JSONL file, 1-based.
+
+ Missing file → nothing; invalid bytes are replaced so one bad byte costs at most that line."""
+ p = Path(path)
+ if not p.is_file():
+ return
+ for i, line in enumerate(p.read_text(encoding="utf-8", errors="replace").splitlines()):
+ if line.strip():
+ yield i + 1, line
+
+
+def read_jsonl_lenient(
+ path: Path | str, *, log: logging.Logger | None = None, dicts_only: bool = False
+) -> list[Any]:
+ """Every parseable JSON line of ``path``; unparseable lines are skipped (debug-logged to ``log``
+ when given), ``dicts_only`` also drops non-object rows."""
+ out: list[Any] = []
+ for i, line in iter_jsonl_lines(path):
+ try:
+ row = json.loads(line)
+ except ValueError as e:
+ if log is not None:
+ log.debug("%s:%d unreadable: %s", path, i, e)
+ continue
+ if dicts_only and not isinstance(row, dict):
+ continue
+ out.append(row)
+ return out
+
+
+# --------------------------------------------------------------------------- the run dir
+class RunWorkspace:
+ """One recorded run directory."""
+
+ def __init__(self, root: Path | str):
+ self.root = Path(root).resolve()
+
+ def rebase(self, stored: str | Path) -> Path:
+ """A stored (possibly foreign absolute) path resolved against THIS run directory.
+
+ Relative paths join the root; an absolute path outside it is re-rooted by its longest
+ trailing segment that exists here, else returned as-is."""
+ p = Path(stored)
+ if not p.is_absolute():
+ return self.root / p
+ if not p.is_relative_to(self.root):
+ parts = p.parts
+ for i in range(1, len(parts)):
+ candidate = self.root.joinpath(*parts[i:])
+ if candidate.exists():
+ return candidate
+ return p
+
+ # ----------------------------------------------------------------- paths
+ @property
+ def src(self) -> Path:
+ return self.root / "src"
+
+ @property
+ def public(self) -> Path:
+ return self.root / "public"
+
+ @property
+ def artifacts(self) -> Path:
+ return self.root / "artifacts"
+
+ @property
+ def deliverable(self) -> Path:
+ return self.root / DELIVERABLE_DIR
+
+ @property
+ def telemetry(self) -> Path:
+ return self.root / TELEMETRY_DIR
+
+ @property
+ def deliverable_manifest_path(self) -> Path:
+ return self.deliverable / "manifest.json"
+
+ def round_artifacts(self, round_index: int) -> Path:
+ return self.artifacts / f"r{round_index:02d}"
+
+ @property
+ def spec_path(self) -> Path:
+ return self.root / "spec.json"
+
+ @property
+ def plan_path(self) -> Path:
+ return self.root / "plan.json"
+
+ @property
+ def state_path(self) -> Path:
+ return self.root / "run_state.json"
+
+ @property
+ def record_path(self) -> Path:
+ return self.root / "record.json"
+
+ @property
+ def events_path(self) -> Path:
+ return self.root / "events.jsonl"
+
+ # ----------------------------------------------------------------- lifecycle
+ def exists(self) -> bool:
+ return self.spec_path.is_file()
+
+ def ensure_layout(self) -> None:
+ """The directories ``pick`` writes into."""
+ self.deliverable.mkdir(parents=True, exist_ok=True)
+
+ def write_json(self, path: Path, model: BaseModel | dict[str, Any]) -> None:
+ data = model.model_dump(mode="json") if isinstance(model, BaseModel) else model
+ write_json_atomic(path, data)
+
+ # ----------------------------------------------------------------- git (read-only)
+ def has_commit(self, commit: str) -> bool:
+ if not commit:
+ return False
+ try:
+ proc = subprocess.run(
+ ["git", *GIT_SAFE_FLAGS, "cat-file", "-e", f"{commit}^{{commit}}"], cwd=self.root,
+ capture_output=True, check=False, env=git_safe_env(), timeout=HAS_COMMIT_TIMEOUT_S,
+ )
+ except (OSError, subprocess.SubprocessError):
+ return False
+ return proc.returncode == 0
diff --git a/flywheel/c3d_flywheel/select.py b/flywheel/c3d_flywheel/select.py
index 472ddedb..cdf8e896 100644
--- a/flywheel/c3d_flywheel/select.py
+++ b/flywheel/c3d_flywheel/select.py
@@ -10,7 +10,6 @@ from typing import Literal
from codeverse3d.contracts.spec import FIXED_PARTS_GATE, PROJECT_GATE_PREFIX
from codeverse3d.proc import read_json_or_none
-from codeverse3d.workspace import Workspace
from pydantic import BaseModel, Field
from c3d_flywheel.legacy import RoundRecord, RunRecord
@@ -21,6 +20,7 @@ from c3d_flywheel.record.record import (
round_complexity,
stop_reason,
)
+from c3d_flywheel.runlayout import RunWorkspace
log = logging.getLogger(__name__)
@@ -67,7 +67,7 @@ class Selection(BaseModel):
# --------------------------------------------------------------------------- rows
def _load(run_dir: Path | str, record: RunRecord | None) -> RunRecord:
- return record if record is not None else load_record(Workspace(run_dir))
+ return record if record is not None else load_record(RunWorkspace(run_dir))
def round_rows(run_dir: Path | str, *, record: RunRecord | None = None) -> list[RoundRow]:
@@ -94,7 +94,7 @@ def _ranked(rows: list[RoundRow]) -> list[RoundRow]:
# --------------------------------------------------------------------------- a round's own outputs
-def round_file(ws: Workspace, rnd: RoundRecord | None, name: str = "object.glb") -> Path | None:
+def round_file(ws: RunWorkspace, rnd: RoundRecord | None, name: str = "object.glb") -> Path | None:
out = round_outputs(ws, rnd)
p = out / name if out is not None else None
return p if p is not None and p.is_file() else None
@@ -117,7 +117,7 @@ def fallback_round(rounds: Iterable[RoundRecord]) -> int | None:
# --------------------------------------------------------------------------- summarise
-def load_selection(ws: Workspace) -> Selection | None:
+def load_selection(ws: RunWorkspace) -> Selection | None:
data = read_json_or_none(ws.root / SELECTION_NAME)
try:
return Selection.model_validate(data) if data is not None else None
@@ -129,7 +129,7 @@ def summarise(run_dir: Path | str, *, record: RunRecord | None = None) -> RunSum
rec = _load(run_dir, record)
rows = round_rows(run_dir, record=rec)
by_index = {r.index: r for r in rows}
- sel = load_selection(Workspace(run_dir))
+ sel = load_selection(RunWorkspace(run_dir))
if sel is not None and sel.round in by_index:
picked, method = sel.round, sel.method
else:
@@ -144,7 +144,7 @@ def summarise(run_dir: Path | str, *, record: RunRecord | None = None) -> RunSum
# --------------------------------------------------------------------------- package
def package(run_dir: Path | str, round_index: int, *, method: Method = "round") -> Path:
- ws = Workspace(run_dir)
+ ws = RunWorkspace(run_dir)
rec = load_record(ws)
if not any(r.index == round_index for r in rec.rounds):
raise ValueError(f"no round {round_index} in {ws.root} (rounds: {[r.index for r in rec.rounds]})")