#!/usr/bin/env python3 """Rebuild data/ from the closebench package. Deterministic: the same closebench version yields byte-identical files. python build.py # closebench installed (pip install closebench==0.1.1) python build.py --src /path/to/repo # or import it from a source checkout Writes, one JSON object per line, UTF-8, compact separators, fixed key order: data/worlds-{lang}.jsonl what the agent sees: seed, mode, lang, company, period, accounts, entries, documents data/truth-{lang}.jsonl the answer key: seed, mode, lang, errors Seeds 1..50 are `hard` worlds, seeds 1001..1050 are `absence` worlds -- the mode is encoded in the seed, exactly as closebench.cli.mode_for does it. """ from __future__ import annotations import argparse, hashlib, json, os, sys HARD_SEEDS = range(1, 51) ABSENCE_SEEDS = range(1001, 1051) SEEDS = [*HARD_SEEDS, *ABSENCE_SEEDS] LANGS = ("en", "ru") HERE = os.path.dirname(os.path.abspath(__file__)) DATA = os.path.join(HERE, "data") def mode_for(seed: int) -> str: """Same rule as closebench.cli.mode_for: seed >= 1000 selects `absence`.""" return "absence" if seed >= 1000 else "hard" def import_closebench(src: str | None = None): """Return (World, version). `src` is a source checkout to put on sys.path.""" src = src or os.environ.get("CLOSEBENCH_SRC") if src: sys.path.insert(0, src) try: import closebench from closebench.world import World except ImportError: sys.exit("closebench is not importable: `pip install closebench==0.1.1` " "or pass --src /path/to/closebench checkout") return World, closebench.__version__ def world_record(world) -> dict: """Agent-visible part of a built World, in the column order used by the dataset.""" snap = world.snapshot() return { "seed": world.seed, "mode": world.difficulty, "lang": world.lang, "company": snap["company"], "period": snap["period"], "accounts": snap["accounts"], "entries": snap["entries"], "documents": snap["documents"], } def truth_record(world) -> dict: """Planted errors of a built World -- the grader's answer key.""" return {"seed": world.seed, "mode": world.difficulty, "lang": world.lang, "errors": world.truth()["errors"]} def dumps(obj) -> str: return json.dumps(obj, ensure_ascii=False, separators=(",", ":")) def sha256(path: str) -> str: h = hashlib.sha256() with open(path, "rb") as f: for chunk in iter(lambda: f.read(1 << 20), b""): h.update(chunk) return h.hexdigest() def build(World, outdir: str = DATA) -> list[dict]: os.makedirs(outdir, exist_ok=True) rows = [] for lang in LANGS: wp = os.path.join(outdir, f"worlds-{lang}.jsonl") tp = os.path.join(outdir, f"truth-{lang}.jsonl") with open(wp, "w", encoding="utf-8", newline="\n") as fw, \ open(tp, "w", encoding="utf-8", newline="\n") as ft: for sd in SEEDS: w = World(seed=sd, lang=lang, difficulty=mode_for(sd)).build() fw.write(dumps(world_record(w)) + "\n") ft.write(dumps(truth_record(w)) + "\n") rows.append({"lang": lang, "seed": sd, "mode": w.difficulty, "entries": len(w.ledger.entries), "documents": len(w.documents), "errors": len(w.errors), "kinds": [e.kind for e in w.errors]}) return rows def summarise(rows: list[dict]) -> str: out = [] for lang in LANGS: for mode in ("hard", "absence"): rs = [r for r in rows if r["lang"] == lang and r["mode"] == mode] rng = lambda k: f"{min(r[k] for r in rs)}-{max(r[k] for r in rs)}" out.append(f" {lang} {mode:<8} worlds={len(rs):3d} entries/world={rng('entries'):>8} " f"documents/world={rng('documents'):>6} errors/world={rng('errors'):>4} " f"errors total={sum(r['errors'] for r in rs)}") return "\n".join(out) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--src", help="closebench source checkout to import from (if not pip-installed)") ap.add_argument("--outdir", default=DATA) a = ap.parse_args() World, version = import_closebench(a.src) print(f"closebench {version}; seeds hard={HARD_SEEDS.start}..{HARD_SEEDS.stop - 1} " f"absence={ABSENCE_SEEDS.start}..{ABSENCE_SEEDS.stop - 1}; langs={','.join(LANGS)}") rows = build(World, a.outdir) print(summarise(rows)) total = 0 for name in sorted(os.listdir(a.outdir)): p = os.path.join(a.outdir, name) size = os.path.getsize(p); total += size print(f" {name:<18} {size:>10,d} bytes sha256={sha256(p)}") print(f" total {total / 1e6:.1f} MB (uncompressed; below the 200 MB threshold, no gzip)") if __name__ == "__main__": main()