"""The bridge from markdown rubrics to the codeworld-gym rubric database. This is where curation meets the actual experiment, so the failure modes are about information silently not arriving: a rubric that loses its applicability condition fires on the wrong programs, and one that loses its provenance retrieves at the wrong weight. """ from __future__ import annotations import sys from pathlib import Path import pytest ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) import export_rubric_db as ex CONCEPTUAL = """### P1 — Prefix-truncated flattened pixel features - **Pattern**: Detects feature extraction that flattens a pixel array and keeps only a fixed-length prefix. - **Detection procedure**: 1. Locate the flatten call. - **Predicted impact**: - Add score for C9 (Available signal never used): 3 - **Applies when**: An image classification pipeline builds features from raw pixels. """ ERROR = """--- id: E001 family: error_focused exception_class: ValueError competition: tabular-playground-series-dec-2021 --- ### Asymmetric column dropping between fit and transform - **Input condition**: `code`: train drops only the target while test drops the identifier - **Code requirement**: a column-aware transformer requires identical named columns at fit and transform - **Prediction**: the transform receives a frame missing a fitted column and raises `ValueError` """ def write(tmp_path, name, text): p = tmp_path / name p.write_text(text) return p def test_conceptual_item_carries_its_applicability_condition(tmp_path): """The world model sees the item text and nothing else. A criterion that reads correctly only beside its own heading fires on the wrong programs once it is retrieved against unrelated code.""" item = ex.to_item(ex.parse(write(tmp_path, "c.md", CONCEPTUAL)), "conceptual") assert "Applies when" in item assert "image classification pipeline" in item.lower() assert item.startswith("1. ") def test_conceptual_is_a_principle_and_error_is_a_hard_rule(tmp_path): """A conceptual rubric predicts a worse score on a clean run. Tagging it as a hard rule would let the world model fail a program that works.""" c = ex.to_item(ex.parse(write(tmp_path, "c.md", CONCEPTUAL)), "conceptual") e = ex.to_item(ex.parse(write(tmp_path, "e.md", ERROR)), "error_focused") assert c.endswith("[Principle]") assert e.endswith("[Hard Rule]") def test_error_item_keeps_the_exception_and_the_trigger(tmp_path): item = ex.to_item(ex.parse(write(tmp_path, "e.md", ERROR)), "error_focused") assert "ValueError" in item assert "Applies when" in item and "Predicted failure" in item def test_front_matter_is_parsed_and_stripped_from_the_body(tmp_path): r = ex.parse(write(tmp_path, "e.md", ERROR)) assert r["meta"]["exception_class"] == "ValueError" assert r["meta"]["competition"] == "tabular-playground-series-dec-2021" assert not r["body"].startswith("---") assert r["title"].startswith("Asymmetric column dropping") def test_p1_prefix_is_stripped_from_the_title(tmp_path): r = ex.parse(write(tmp_path, "c.md", CONCEPTUAL)) assert r["title"] == "Prefix-truncated flattened pixel features" def test_unparseable_file_returns_none_rather_than_a_broken_item(tmp_path): assert ex.parse(write(tmp_path, "junk.md", "no heading here\n")) is None def test_codeworld_gym_can_load_the_export(tmp_path): """The claim that matters: codeworld-gym's own loader accepts it. A format we merely believe is right is not a bridge.""" import importlib.util import json export = tmp_path / "export.jsonl" item = ex.to_item(ex.parse(write(tmp_path, "c.md", CONCEPTUAL)), "conceptual") export.write_text(json.dumps({ "task_id": "dog-breed-identification", "source": "rubric_gen/conceptual", "rubric": item}) + "\n") db_py = ROOT.parent / "codeworld-gym" / "codeworld_gym" / "db.py" if not db_py.is_file(): pytest.skip("codeworld-gym not present") spec = importlib.util.spec_from_file_location("cwgdb", db_py) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) db = mod.RubricDatabase.from_judgements([str(export)]) assert len(db) == 1 loaded = db.items[0] assert loaded.kind == "principle" assert loaded.task_id == "dog-breed-identification" # The tag is stripped by the loader; the substance must survive. assert "Applies when" in loaded.text assert "[Principle]" not in loaded.text