"""Offline tests for bench/tasks/loop_battery.py (MockServer only).""" import os import unittest from _util import run_dir, run_main from bench.core import lane as lane_mod, results from bench.core.mockserver import MockServer, chat # noqa: E402 from bench.tasks import loop_battery as lb # noqa: E402 class Scoring(unittest.TestCase): def test_ngram_detects_loop(self): loop = ("so the answer is forty two and we check it again " * 30) rep, uniq = lb.rep_score(loop) self.assertGreaterEqual(rep, 25) self.assertLess(uniq, 0.1) varied = " ".join("w%d" % i for i in range(300)) self.assertEqual(lb.rep_score(varied)[0], 1) self.assertEqual(lb.rep_score("short text")[0], 0) def test_markers(self): n, per = lb.marker_counts("Wait, hmm. Hmm... but wait, actually, no.") self.assertEqual(per["hmm"], 2) self.assertEqual(per["wait"], 2) self.assertEqual(n, sum(per.values())) def test_analyze_cap_hit_means_incorrect_even_with_acceptable_answer(self): a = lb.analyze("conflicting-opt", "hmm " * 50, "There is no single best language.", "length") self.assertTrue(a["answer_ok"]) self.assertTrue(a["cap_hit"]) self.assertFalse(a["correct"]) b = lb.analyze("conflicting-opt", "short", "There is no single best language.", "stop") self.assertTrue(b["correct"]) def test_scanned_content_flag_when_no_reasoning(self): a = lb.analyze("prove-false", "", "wait wait wait", "stop") self.assertTrue(a["scanned_content"]) self.assertEqual(a["markers"], 3) def test_wordcount_rule(self): self.assertFalse(lb.answer_ok("self-ref-wordcount", "My final answer contains exactly seven words here.")) # 8 words self.assertTrue(lb.answer_ok("self-ref-wordcount", "This answer has 5 words.")) self.assertFalse(lb.answer_ok("self-ref-wordcount", "This answer has 12 words.")) self.assertTrue(lb.answer_ok("self-ref-wordcount", "This answer has five words.")) def test_other_rules(self): self.assertTrue(lb.answer_ok("underdetermined-ages", "The ages are 2, 2 and 9.")) self.assertTrue(lb.answer_ok("underdetermined-ages", "You never told me the sum, so it is underdetermined.")) self.assertFalse(lb.answer_ok("underdetermined-ages", "The ages are 1, 6 and 6.")) self.assertTrue(lb.answer_ok("prove-false", "The claim is false: 9 = 3 x 3 is a counterexample.")) self.assertFalse(lb.answer_ok("prove-false", "Proof: let n be odd... therefore n is prime. QED")) self.assertTrue(lb.answer_ok("conflicting-opt", "No single language wins on every axis; it depends.")) self.assertFalse(lb.answer_ok("conflicting-opt", "Rust.")) self.assertTrue(lb.answer_ok("ambiguous-pronoun-math", "The statements contradict each other.")) self.assertTrue(lb.answer_ok("ambiguous-pronoun-math", "Each fruit costs $1.40.")) self.assertFalse(lb.answer_ok("ambiguous-pronoun-math", "Apples cost $1 and oranges cost $2.")) class Run(unittest.TestCase): def test_run_rows_cap_hit_and_loop(self): def reply(body): q = body["messages"][-1]["content"] if q.startswith("What is the single best"): # runaway: hits the cap in a repeating loop return chat("", reasoning="let me double-check that again and again " * 60, finish="length", completion_tokens=16000) return chat("No single option; it depends.", reasoning="short thought", completion_tokens=40) with MockServer() as srv: srv.default = reply lane_mod.set_current(srv.lane(lane_id="lb-run")) rc = run_main(lb, ["run", "--samples", "2", "--items", "conflicting-opt,prove-false"]) self.assertEqual(rc, 0) rows = results.load_rows("loop-battery", "lb-run") self.assertEqual(len(rows), 4) by = {} for r in rows: by.setdefault(r["category"], []).append(r) runaway = by["loop/conflicting-opt"] self.assertTrue(all(r["cap_hit"] and r["correct"] is False and r["status"] == "truncated" for r in runaway)) self.assertTrue(all(r["max_ngram_rep"] > 10 for r in runaway)) self.assertTrue(all(r["markers"] > 0 for r in runaway)) # prove-false answer lacks a counterexample -> not acceptable, but not a cap hit pf = by["loop/prove-false"] self.assertTrue(all(r["cap_hit"] is False and r["answer_ok"] is False and r["correct"] is False for r in pf)) self.assertTrue(os.path.exists(os.path.join(run_dir("loop-battery", "lb-run"), runaway[0]["sidecar"]))) if __name__ == "__main__": unittest.main()