analyst-workbench / source /tests /test_tasks.py
ajinkyamulay's picture
v7: user-switch fallback, 8 Repo Repair libraries, AW-8 result
edce79f verified
Raw History Blame Contribute Delete
7.59 kB
import os
import pytest
from workbench.environment import WorkbenchEnvironment
from workbench.models import WorkbenchAction
from workbench.tasks import ALL_TASK_IDS, TASK_IDS, build_task
SEEDS = range(int(os.environ.get("WB_SEEDS", "25")))
@pytest.mark.parametrize("task_id", ALL_TASK_IDS)
def test_oracle_scores_one_and_wrong_answers_do_not(task_id):
if task_id.startswith("swe-repo-"):
pytest.skip("graded on workspace files; see test_repo_repair")
for seed in SEEDS:
task = build_task(task_id, seed)
assert task.grade(task.oracle) == 1.0, (task_id, seed, task.oracle)
assert task.grade("") == 0.0
assert task.grade("totally wrong") == 0.0
for wrong in task.wrong:
assert task.grade(wrong) < 1.0, (task_id, seed, wrong, task.oracle)
assert len(task.instruction) < 800
assert sum(len(c) for c in task.files.values()) < 60_000
@pytest.mark.parametrize("task_id", ALL_TASK_IDS)
def test_same_seed_is_deterministic(task_id):
a, b = build_task(task_id, 7), build_task(task_id, 7)
assert a.files == b.files and a.oracle == b.oracle and a.instruction == b.instruction
def test_episode_tools_and_budget():
env = WorkbenchEnvironment()
obs = env.reset(task_id="fin-loan-balance", seed=3)
assert not obs.done and "loan.txt" in obs.output
obs = env.step(WorkbenchAction(tool="read", path="loan.txt", lines=3))
assert "Principal" in obs.output
obs = env.step(WorkbenchAction(tool="read", path="loan.txt", start=0, lines=500))
assert "Principal" in obs.output and not obs.done
obs = env.step(WorkbenchAction(tool="grep", pattern="APR"))
assert "loan.txt:" in obs.output
obs = env.step(WorkbenchAction(tool="python", code="print(open('loan.txt').read().count('\\n'))"))
assert obs.output.strip().isdigit()
obs = env.step(WorkbenchAction(tool="read", path="../etc/passwd"))
assert obs.output.startswith("Error")
oracle = build_task("fin-loan-balance", 3).oracle
obs = env.step(WorkbenchAction(tool="submit", answer=oracle))
assert obs.done and obs.reward == 1.0
env.reset(task_id="math-grid-paths", seed=1)
for _ in range(20):
obs = env.step(WorkbenchAction(tool="ls"))
assert obs.done and obs.reward == 0.0 and obs.remaining_steps == 0
env.close()
def test_python_timeout():
env = WorkbenchEnvironment()
env.reset(task_id="math-grid-paths", seed=1)
obs = env.step(WorkbenchAction(tool="python", code="while True: pass"))
assert "killed" in obs.output or "CPU" in obs.output or obs.output
assert not obs.done
env.close()
def test_grep_catastrophic_regex_times_out_without_hanging():
env = WorkbenchEnvironment()
env.reset(task_id="sci-longest-orf", seed=1)
obs = env.step(WorkbenchAction(tool="grep", pattern="([ACGT]+)+X"))
assert obs.output.startswith("Error") and not obs.done
obs = env.step(WorkbenchAction(tool="grep", pattern="ATG", path="sequence.fasta"))
assert "matches" in obs.output
env.close()
def test_thousands_separators_and_cents_rule():
from workbench.grading import parts_score, close, exact
assert parts_score("APAC,10,450.25", [exact("APAC"), close(10450.25, abs_tol=0.01)]) == 1.0
from workbench.tasks.office import _usd_cents
assert _usd_cents(133.93, 1.12) == 15000 # 150.0016 USD rounds to the cap and complies
assert _usd_cents(45.46, 1.1) == 5001
def test_submit_that_raises_still_ends_episode():
env = WorkbenchEnvironment()
env.reset(task_id="math-grid-paths", seed=1)
env._task.grade = lambda answer: 1 / 0
obs = env.step(WorkbenchAction(tool="submit", answer="3"))
assert obs.done and obs.reward == 0.0
env.close()
def test_level_ids_and_plain_ids_agree():
for t in TASK_IDS:
assert build_task(t, 3).oracle == build_task(f"{t}@1", 3).oracle
assert build_task(f"{t}-l2", 3).oracle == build_task(f"{t}@2", 3).oracle
@pytest.mark.skipif("swe-repo-repair" not in TASK_IDS, reason="vendored libraries not present")
@pytest.mark.parametrize("level", [1, 2, 3, 4])
def test_repo_repair_pristine_fixes_and_broken_does_not(level):
for seed in range(int(os.environ.get("WB_REPO_SEEDS", "3"))):
task = build_task(f"swe-repo-repair@{level}", seed)
pristine = task.meta["pristine"]
broken = {k: v for k, v in task.files.items() if k in pristine}
assert broken != pristine
assert task.grade(pristine) == 1.0, (level, seed, task.meta["library"])
assert task.grade(broken) == 0.0, (level, seed)
if level in (2, 3): # fixing one of the two bugs gives half
first = task.meta["mutants"][0]
half = dict(broken)
half[first["path"]] = pristine[first["path"]] if sum(m["path"] == first["path"] for m in task.meta["mutants"]) == 1 else half[first["path"]]
if half != broken:
assert task.grade(half) == 0.5, (seed, task.meta["library"])
def test_write_and_edit_tools():
env = WorkbenchEnvironment()
env.reset(task_id="fin-loan-balance", seed=3)
obs = env.step(WorkbenchAction(tool="write", path="notes/a.txt", content="alpha beta\n"))
assert "Wrote" in obs.output
obs = env.step(WorkbenchAction(tool="edit", path="notes/a.txt", old="beta", new="gamma"))
assert "Edited" in obs.output
obs = env.step(WorkbenchAction(tool="read", path="notes/a.txt"))
assert "alpha gamma" in obs.output
obs = env.step(WorkbenchAction(tool="edit", path="notes/a.txt", old="zeta", new="x"))
assert obs.output.startswith("Error")
obs = env.step(WorkbenchAction(tool="write", path="../escape.txt", content="x"))
assert obs.output.startswith("Error")
env.close()
def test_task_id_levels():
from workbench.tasks import is_valid
assert is_valid("ind-oee@2") and is_valid("ind-oee-l2") and not is_valid("ind-oee@3")
assert not is_valid("ind-oee@x") and not is_valid("nope@1")
if "swe-repo-repair" in TASK_IDS:
assert is_valid("swe-repo-repair-l4") and not is_valid("swe-repo-repair@5")
@pytest.mark.skipif("swe-repo-repair" not in TASK_IDS, reason="vendored libraries not present")
def test_repo_library_families():
libs = [t for t in TASK_IDS if t.startswith("swe-repo-") and t != "swe-repo-repair"]
assert len(libs) == 8
for fam in libs:
task = build_task(f"{fam}-l3", 0)
assert fam.endswith(task.meta["library"].replace("_", "-"))
assert task.grade(task.meta["pristine"]) == 1.0
@pytest.mark.skipif("swe-repo-repair" not in TASK_IDS, reason="vendored libraries not present")
def test_repo_repair_grader_resists_tampering():
task = build_task("swe-repo-toolz-l4", 1)
pristine = task.meta["pristine"]
broken = {k: v for k, v in task.files.items() if k in pristine}
first = task.meta["mutants"][0]["path"]
# a deleted or unparseable library file scores 0, even with everything else fixed
assert task.grade({k: v for k, v in pristine.items() if k != first}) == 0.0
assert task.grade(dict(pristine, **{first: pristine[first] + "\n)(\n"})) == 0.0
# comments and blank lines do not matter
assert task.grade({k: "# reviewed\n" + v + "\n\n" for k, v in pristine.items()}) == 1.0
# any change outside the broken functions scores 0, e.g. patching pytest from __init__
init = next(k for k in pristine if k.endswith("__init__.py"))
assert task.grade(dict(pristine, **{init: pristine[init] + "\nimport _pytest\n"})) == 0.0
# the broken state scores 0; one of three fixed scores one third
assert task.grade(broken) == 0.0