Files
lean-bench/tests/test_pipeline.py
T
claude-bot 598f7fd662
ci / test (push) Successful in 1m10s
ci / lean-project (push) Successful in 6s
docker / image (push) Successful in 3m6s
Rebuild lean-bench: pages in, verified indexed Lean out
Replaces the previous tree entirely. The pipeline is four separable pieces, each
testable on its own:

1. leanbench/llm.py      provider-neutral OpenAI-compatible connector, stdlib only,
                        with retries, JSON coercion and a scripted client so the
                        rest of the code is testable without a network.
2. leanbench/detect.py   per-page detection of formalizable mathematics. Case 1 is
                        mathematics asserted as fact (definition/theorem/lemma/...,
                        deliberately not sub-classified); case 2 is a problem left
                        open to the reader. Hits come back as spans -- start page
                        and index, end page and index -- so several per page is
                        normal. A hit cut off by the page end makes the detector
                        re-read that page together with the next, repeatedly, so an
                        item spread over three pages is found whole.
                        Offsets come from verbatim quotes resolved by
                        leanbench/anchors.py (exact -> whitespace-insensitive ->
                        fuzzy), because models quote well and count badly.
3. leanbench/formalize.py + leanbench/verify.py
                        each span becomes one Lean 4 declaration, judged by the real
                        compiler: full elaboration for case 1, statement legality
                        only for case 2, which has no proof to check. Lean's own
                        diagnostics feed back for a repair attempt. "verified" (Lean
                        accepted it) and "proved" (accepted with no sorry) are kept
                        strictly apart.
4. leanbench/db.py       indexed SQLite plus JSONL export. Every item is indexed by
                        the types of its inputs, or by its own type when it is not a
                        proof, and records the source book (sha256) and the page it
                        starts on -- so `drop --source H --pages A-B` followed by a
                        re-ingest redoes exactly those pages and nothing else.

Infrastructure:

- lean/            Lake project pinned to Mathlib.
- docker/          image carrying Lean 4 and a prebuilt Mathlib. The Mathlib
                   revision and the toolchain are resolved against each other at
                   build time rather than both hardcoded, so the build does not rot.
                   One Docker layer per Lean step, each long one under `timeout`:
                   the gigabyte cache fetch earns a layer that a later failure never
                   invalidates, and a stalled step fails with a message instead of
                   sitting silent for an hour. There is deliberately no `lake build`
                   of our own library -- verification runs `lake env lean` against
                   Mathlib's oleans and needs nothing else, and building the library
                   was pure cost. docker/smoke.py runs the whole pipeline inside the
                   image against the real compiler, with the LLM scripted.
- .gitea/workflows ci (lint + tests in a venv, since the runner image's Python is
                   Debian-managed and refuses installs into itself), docker (build,
                   verify, optionally push), ingest (submit a book by hand). The
                   docker build runs with --progress=plain, because BuildKit's
                   default renderer collapses a long step into one rewritten line
                   and a stall then looks exactly like progress.

The docker workflow is not a throwaway build: it leaves the image, the layer cache,
an idle `lean-bench` container and the `lean-bench-data` / `lean-bench-books`
volumes in place on the runner, and never prunes, so the environment is there to be
used afterwards. Pushing to the registry is optional and cannot fail the build --
Gitea's container registry does not accept the built-in Actions token, so that needs
a REGISTRY_TOKEN secret with write:package.

58 tests; they need neither network nor Lean.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ci5T3gWDhhmqDec3e1XtYZ
2026-09-19 16:22:09 +00:00

159 lines
5.4 KiB
Python

from __future__ import annotations
from pathlib import Path
from leanbench.config import Settings
from leanbench.db import Database
from leanbench.document import Document
from leanbench.llm import ScriptedClient
from leanbench.pipeline import ingest
from .conftest import detection_reply, formalize_reply
DETECT_PAGE_ONE = detection_reply(
[
{
"kind": "statement",
"label": "Definition 1.6.1",
"summary": "divisibility",
"start_quote": "Definition 1.6.1. Let a and b be integers.",
"end_quote": "b = a k.",
"continues_past_end": False,
},
{
"kind": "statement",
"label": "Theorem 1.6.3",
"summary": "gcd exists",
"start_quote": "Theorem 1.6.3. Let a and b be nonzero integers.",
"end_quote": "the set of common",
"continues_past_end": True,
},
]
)
DETECT_BOTH_PAGES = detection_reply(
[
{
"kind": "statement",
"label": "Definition 1.6.1",
"summary": "divisibility",
"start_quote": "Definition 1.6.1. Let a and b be integers.",
"end_quote": "b = a k.",
"continues_past_end": False,
},
{
"kind": "statement",
"label": "Theorem 1.6.3",
"summary": "gcd exists",
"start_quote": "Theorem 1.6.3. Let a and b be nonzero integers.",
"end_quote": "the greatest common divisor.",
"continues_past_end": False,
},
{
"kind": "problem",
"label": "Exercise 1.6.4",
"summary": "mutual divisibility",
"start_quote": "Exercise 1.6.4. Show that if a divides b and b divides a",
"end_quote": "a = b or a = -b.",
"continues_past_end": False,
},
]
)
def _settings(tmp_path: Path) -> Settings:
settings = Settings.from_env(dotenv=None)
settings.database = str(tmp_path / "leanbench.sqlite")
settings.repair_attempts = 0
return settings
def test_end_to_end_run_stores_indexed_items(tmp_path: Path, two_page_book, fake_checker) -> None:
client = ScriptedClient(
[DETECT_PAGE_ONE, DETECT_BOTH_PAGES]
+ [
formalize_reply(name="divides_def",
lean_code="def divides (a b : Int) : Prop := ∃ k : Int, b = a * k"),
formalize_reply(name="gcd_exists",
lean_code="theorem gcd_exists (a b : Int) : True := by\n sorry"),
formalize_reply(name="dvd_antisymm'",
lean_code="theorem dvd_antisymm' (a b : Int) : True := by\n sorry"),
]
)
settings = _settings(tmp_path)
database = Database(settings.database)
result = ingest(
two_page_book.path,
settings=settings,
client=client,
database=database,
checker=fake_checker,
title="Test Book",
)
summary = result.summary()
assert summary["detected"] == 3
assert summary["problems"] == 1
assert summary["multi_page"] == 1
assert summary["stored"] == 3
assert summary["verified"] == 3
assert database.stats()["items"] == 3
by_type = database.search(types=["Int"])
assert len(by_type) == 3, "every item is reachable through its types"
problems = database.search(kind="problem")
assert len(problems) == 1 and problems[0]["start_page"] == 2
database.close()
def test_reingesting_a_page_range_replaces_only_that_range(
tmp_path: Path, two_page_book: Document, fake_checker
) -> None:
settings = _settings(tmp_path)
database = Database(settings.database)
first = ScriptedClient([DETECT_PAGE_ONE, DETECT_BOTH_PAGES] + [formalize_reply()] * 3)
ingest(two_page_book.path, settings=settings, client=first, database=database,
checker=fake_checker)
assert database.stats()["items"] == 3
# redo page 2 only: the page-1 item survives, page-2 items are rebuilt
second = ScriptedClient(
[
detection_reply(
[
{
"kind": "problem",
"label": "Exercise 1.6.5",
"summary": "odd plus odd",
"start_quote": "Exercise 1.6.5. Prove that the sum of two odd",
"end_quote": "odd integers is even.",
"continues_past_end": False,
}
]
),
formalize_reply(name="odd_add_odd"),
]
)
ingest(two_page_book.path, settings=settings, client=second, database=database,
checker=fake_checker, first_page=2, last_page=2)
pages = sorted(item["start_page"] for item in database.search(limit=100))
assert pages == [1, 1, 2], "page 1 untouched, page 2 replaced by the new run"
database.close()
def test_a_run_without_lean_stores_items_but_claims_nothing(
tmp_path: Path, two_page_book: Document
) -> None:
from leanbench.verify import NullChecker
client = ScriptedClient([DETECT_PAGE_ONE, DETECT_BOTH_PAGES] + [formalize_reply()] * 3)
settings = _settings(tmp_path)
database = Database(settings.database)
result = ingest(two_page_book.path, settings=settings, client=client, database=database,
checker=NullChecker())
assert result.summary()["stored"] == 3
assert result.summary()["verified"] == 0
assert database.search(verified=True) == []
database.close()