llm-bench

#!/usr/bin/env python3
"""llm-bench CLI: python3 bench.py <instrument> <command> [args...]  (each takes --help)

LANE=<id> selects lanes/<id>.json (endpoint, model, sampler, max_tokens, thinking on/off, auth,
cost cap, provenance); see bench/core/lane.py and CONTRACT.md. Values in .env fill in what the
environment does not set.
"""
import importlib
import os
import sys


def _load_dotenv():
    """Load .env from the script's directory (or cwd) into os.environ.

    Only sets vars that aren't already in the environment (env vars win
    over .env, so you can override per-run without editing the file).
    """
    here = os.path.dirname(os.path.abspath(__file__))
    for candidate in (os.path.join(here, ".env"), os.path.join(os.getcwd(), ".env")):
        if not os.path.isfile(candidate):
            continue
        with open(candidate) as f:
            for line in f:
                line = line.strip()
                if not line or line.startswith("#"):
                    continue
                if "=" not in line:
                    continue
                key, _, val = line.partition("=")
                key = key.strip()
                val = val.strip().strip('"').strip("'")
                if key and key not in os.environ:
                    os.environ[key] = val
        break


# Instrument name -> module path (under bench/tasks/)
INSTRUMENTS = {
    "agentic-fix": "bench.tasks.agentic_fix",      # bug fixes: easy single-turn / hard cross-file
    "agentic-multitask": "bench.tasks.agentic_multitask",  # app builds: easy Go+TS / hard Rust
    "router": "bench.tasks.router",              # small helpers: route / orch / judge / rerank
    "needle": "bench.tasks.needle",
    "loop-battery": "bench.tasks.loop_battery",
    "reasoning": "bench.tasks.reasoning",        # computing knowledge: MMLU-Pro computer science
    "sleuth": "bench.tasks.sleuth",              # troubleshoot broken hosts in containers
    "conformance": "bench.tasks.conformance",    # per-lane preflight (run before any bench)
    "report": "bench.tasks.report",              # capability profile across lanes
    "oracle": "bench.tasks.oracle",              # per-item oracle lane labels + policy sims
}


def main():
    _load_dotenv()
    if len(sys.argv) < 2 or sys.argv[1] in ("-h", "--help"):
        print(__doc__)
        print("\nAvailable instruments:")
        for name in sorted(INSTRUMENTS):
            print(f"  {name}")
        sys.exit(0 if len(sys.argv) > 1 else 1)

    instrument = sys.argv[1]
    if instrument not in INSTRUMENTS:
        print(f"ERROR: unknown instrument {instrument!r}")
        print(f"Available: {', '.join(sorted(INSTRUMENTS))}")
        sys.exit(1)

    # Import the instrument module and pass the remaining args to it.
    # The script's directory must be on sys.path so `bench` package resolves.
    here = os.path.dirname(os.path.abspath(__file__))
    if here not in sys.path:
        sys.path.insert(0, here)

    from bench.core.lane import LaneError
    mod = importlib.import_module(INSTRUMENTS[instrument])
    # Strip the instrument name from argv so the module sees its own args.
    sys.argv = [INSTRUMENTS[instrument]] + sys.argv[2:]
    try:
        mod.main()
    except LaneError as e:
        sys.exit(f"ERROR: {e}")


if __name__ == "__main__":
    main()