"""Research must stay advisory and bound to source evidence and root review.""" import copy import importlib.util from pathlib import Path import pytest @pytest.fixture def inputs(tmp_path, monkeypatch): source = Path(__file__).parents[1] / "simulation/semantic-learning" monkeypatch.syspath_prepend(str(source)) spec = importlib.util.spec_from_file_location( "tested_research_gate", source / "research_gate.py" ) m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) baseline = tmp_path / "baseline.json" baseline.write_text('{"synthetic":true}') brief = dict( schema_version=m.SCHEMA, root_actor="root", scout_actor="scout", trigger="user_request", question="Where does review effort go?", baseline=dict(path="baseline.json", sha256=m.checksum(baseline)), ) src = dict( id="primary", title="Synthetic documentation", url="https://example.com/docs", kind="official_docs", checked_utc="2026-10-25T10:00:00Z", supports="A workflow exists", limitations="No result on our data", ) idea = dict( id="measure", title="Bounded measurement", basis="external", source_ids=["primary"], evidence="Source describes workflow", hypothesis="May reduce repeated work", minimal_test="Compare equal budgets", success="Lower review effort at same quality", failure="More errors or no gain", cost="Small metadata-only test", risks="Correlated reviewers", not_applicable="No measured bottleneck", ) report = dict( brief_sha256=m.digest(brief), baseline_option="Keep current workflow", sources=[src], ideas=[idea], unknowns="Active effort is not measured", recommendation="Measure before switching", ) decision = dict( actor="root", report_sha256=m.digest(report), source_checks=["primary"], verdicts=[ dict( idea_id="measure", outcome="test", reason="Useful bounded uncertainty reduction", revisit_when="After the measured pilot", ) ], test_plan=dict( idea_id="measure", control="Unchanged workflow", data_selection="Preselected physical groups", metric="Review effort with fixed quality audit", success="Complete evidence without extra errors", failure="Missing timing or changed quality", budget=dict(max_units=4, max_dispatches=8, max_wall_seconds=1800, gpu_training_runs=0), ), ) return m, tmp_path, brief, report, decision def test_selected_idea_is_only_a_draft_not_execution_authority(inputs): m, root, b, r, d = inputs result = m.review_research(root, b, r, d) assert result["selected_idea"] == "measure" assert result["next_action"] == "freeze_separate_experiment" assert not any( result[k] for k in ("execution_authorized", "training_authorized", "map_write_authorized") ) @pytest.mark.parametrize( "change", ["actor", "baseline", "report", "citation", "training", "unverified"] ) def test_rejects_escalation_stale_evidence_and_unverified_selection(inputs, change): m, root, b, r, d = inputs if change == "actor": d["actor"] = "scout" elif change == "baseline": (root / "baseline.json").write_text("changed") elif change == "report": r["recommendation"] = "new unchecked recommendation" elif change == "citation": r["ideas"][0]["source_ids"] = ["invented"] d["report_sha256"] = m.digest(r) elif change == "training": d["test_plan"]["budget"]["gpu_training_runs"] = 1 else: d["source_checks"] = [] with pytest.raises(ValueError): m.review_research(root, b, r, d) def test_no_useful_idea_is_a_valid_closed_result(inputs): m, root, b, r, d = inputs r.update(ideas=[], sources=[]) d.update(report_sha256=m.digest(r), source_checks=[], verdicts=[], test_plan=None) result = m.review_research(root, b, r, d) assert result["selected_idea"] is None assert result["next_action"] == "keep_current_workflow" def test_only_one_test_can_be_selected(inputs): m, root, b, r, d = inputs r["ideas"].append(dict(copy.deepcopy(r["ideas"][0]), id="second")) d["verdicts"].append(dict(d["verdicts"][0], idea_id="second")) d["report_sha256"] = m.digest(r) with pytest.raises(ValueError, match="at most one"): m.review_research(root, b, r, d) def test_output_budget_and_schema_do_not_accept_commands(inputs): m, root, b, r, d = inputs r["execute"] = "start a worker" d["report_sha256"] = m.digest(r) with pytest.raises(ValueError, match="fields"): m.review_research(root, b, r, d) del r["execute"] r["unknowns"] = "x" * m.MAX_BYTES with pytest.raises(ValueError, match="byte limit"): m.review_research(root, b, r, d)