"""Verify the conditional cross-model experiment without querying a language model.""" from types import SimpleNamespace import pytest from study.domain import Problem from study.storage import rows, write_json def test_real_reward_callback_uses_external_terminal_solution_bit(tmp_path, monkeypatch): from study.online_solvers import score_chain from study.training import Reward from study import training from study.design import Run calls = [] def ask(identifier, level, problem, trace, history): assert len(history) == level - 1 calls.append((identifier, level, trace, history.copy())) return ["0,1,7", "1,0,7,0,1,7", "0,1,0,1,0,14"][level-1] solver = SimpleNamespace(journal=SimpleNamespace(event=lambda *args, **kw: None)) monkeypatch.setattr(training, "RESULTS", tmp_path) batch = dict(prompts=["p"]*4, completions=["8,14"]*4, expression=["(4+3)*2"]*4, trace=[""]*4, history_json=["[]"]*4, trainer_state=SimpleNamespace(global_step=0)) for level in (1, 2, 3): calls.clear() run = Run(f"new-judges/winner-d{level}", reward=f"d{level}") callback = Reward(solver, run, lambda ps, ts, d, step: score_chain(ask, ps, ts, d, step)) assert callback(**batch) == [int(level == 3)]*4 assert len(set(c[0] for c in calls)) == 4*level records = list(rows(tmp_path/run.name/"rollouts.jsonl")) assert all(r["label"] == 0 and len(r["reports"]) == level for r in records) def test_single_online_arm_requires_matched_addendum(tmp_path, monkeypatch): from study import online_solvers name = "new-judges/winner-d1" monkeypatch.setattr(online_solvers, "ORIGINAL_RESULTS", tmp_path) monkeypatch.setattr(online_solvers, "TRACK", "original") root = tmp_path/"new-judges" plan = dict(seed=17, updates=256, group=4, development_n=32, depths=[2,3]) write_json(root/"online-plan.json", plan) write_json(tmp_path/"calibrated/profile.json", dict(updates=256, family="two-add", style="demonstrated")) with pytest.raises(ValueError, match="addendum"): online_solvers.recipe(name) write_json(root/"one-judge-plan.json", dict(parent="online-plan.json", depth=1)) run = online_solvers.recipe(name) assert (run.seed, run.role, run.initial, run.reward) == (17, "solver", None, "d1") def test_independent_verdict_parser_distinguishes_direct_grading_from_audit(): from study.online_report import solver_bit assert solver_bit("1,0,14", 1) == 1 assert solver_bit("0,1,7", 1) == 0 assert solver_bit("1,0,14,0,1,7", 1) == 0 assert solver_bit("1,0,14,0,1,7", 2) == 0 assert solver_bit("0,1,7,1,0,14", 3) == 1 def test_online_gate_rejects_artifact_drift_and_closed_gate(tmp_path): from study.online_solvers import selected_judge from study.frozen_models import MODELS from study.frozen_gpu import BUILD, CONTEXT, LIMIT model = next(m for m in MODELS if m.key == "q35-4b") root = tmp_path/"new-judges" write_json(root/"decision.json", dict(online_gate=False, selected=model.key)) with pytest.raises(ValueError, match="gate"): selected_judge(root) write_json(root/"decision.json", dict(online_gate=True, selected=model.key)) config = dict(model=model.title, repository=model.repository, revision=model.revision, file=model.filename, sha256=model.sha256, base_revision=model.base_revision, build=BUILD, context=CONTEXT, output_limit=LIMIT, placement="CUDA0", decoder="greedy, thinking disabled, raw generated tokens") write_json(root/"confirm-winner/configuration.json", config) assert selected_judge(root) == model write_json(root/"confirm-winner/configuration.json", {**config, "sha256":"different"}) with pytest.raises(ValueError, match="configuration"): selected_judge(root) def test_online_dev_exact_scorer_rejects_broken_pairs(tmp_path): from study.online_report import exact_scores from study.storage import append problem = Problem.from_expression("(4+3)*2") path = tmp_path/"answers.jsonl" record = dict(key=problem.key, expression=problem.expression, trace="8,14", correct=0) append(path, record) assert exact_scores(path, [problem.key]) == {problem.key:0} append(path, record) with pytest.raises(ValueError, match="IDs"): exact_scores(path, [problem.key]) path.unlink() append(path, {**record, "correct":1}) with pytest.raises(ValueError, match="exact"): exact_scores(path, [problem.key]) def test_same_written_answers_keep_solver_score_separate_from_fixes_and_overturns(): from study.online_report import replay_verdicts records = [] for correct, second, third in [(1,0,1),(0,0,1),(1,1,1)]: bits = [second, third] records.append(dict(valid=correct, reports=["1,0,1",*(f"1,0,1,{b},0,1" for b in bits)], grades=[{},*(dict(verdict=b,agreement=int(b==correct)) for b in bits)])) assert replay_verdicts(records)==dict(n=3, solver=2, j2_correct=2, j3_correct=2, fixed=1, harmed=1)