"""Audit #17 + #18: multiple alignment, and conservation counted from it. ESM-2 gives a learned opinion about whether a substitution looks plausible. Conservation across real homologs gives an OBSERVED fact: in 40 orthologs this position is serine 40 times. They fail differently — the model is weakest exactly where the audit says (membrane, disordered, multi-domain) and a frequency count is unaffected by any of that. The tests that matter are about the INPUT, not the algorithm. Conservation is only as meaningful as the homolog set, and a set of near-identical sequences makes every column look invariant. A tool that reports that as "conserved" without comment is worse than no tool. """ import pytest from dee.core import agent_tools as t from dee.core import conservation as C from dee.core import orchestrator as orch # Position 1,3,4,5,6 invariant; position 2 varies across all five. HOMOLOGS = ["MAKQWLTVEG", "MSKQWLTVEG", "MTKQWLSVEG", "MAKQWLTVDG", "MGKQWLTIEG"] def test_an_invariant_column_is_found_and_a_variable_one_is_not(): r = C.score(HOMOLOGS) by_pos = {c["anchor_position"]: c for c in r["conservation"]} assert by_pos[5]["call"] == "invariant" and by_pos[5]["agreement_pct"] == 100.0 assert by_pos[2]["call"] == "variable" and by_pos[2]["agreement_pct"] < 60 def test_entropy_is_never_reported_as_negative_zero(): """-sum(...) over an all-agreeing column yields IEEE -0.0, which prints as '-0.0 bits' and reads like a bug in a number users are asked to trust.""" r = C.score(HOMOLOGS) for c in r["conservation"]: assert c["entropy_bits"] >= 0.0 assert str(c["entropy_bits"]) != "-0.0" def test_a_redundant_homolog_set_is_flagged_and_marked_untrustworthy(): """The failure that would make this tool actively misleading. Forty sequences at 99% identity are ONE sequence counted forty times, and every column will look invariant.""" r = C.score(["MAKQWLTVEG"] * 4 + ["MAKQWLTVEA"]) assert r["trustworthy"] is False assert r["diversity_warning"] and "redundancy" in r["diversity_warning"] assert "more divergent" in r["diversity_warning"] def test_a_diverse_set_is_trusted(): """A checker that never trusts anything is a checker nobody reads.""" r = C.score(HOMOLOGS) assert r["trustworthy"] is True and r["diversity_warning"] is None def test_too_few_sequences_is_refused_with_a_way_forward(): r = C.score(["MAKQ", "MSKQ"]) assert r["ok"] is False and r["kind"] == "too_few" assert "BLAST" in r["next"] def test_positions_are_numbered_along_the_anchor_the_user_designs_against(): r = C.score(HOMOLOGS, positions=[5]) assert [(c["anchor_position"], c["anchor_residue"]) for c in r["conservation"]] \ == [(5, "W")] def test_indels_do_not_shift_the_anchor_numbering(): """A homolog with a deletion must not renumber the sequence the user is designing against — that is how a conservation call lands on the wrong residue.""" r = C.score(["MAKQWLTVEG", "MAKWLTVEG", "MAKQWLTVEG"]) positions = [c["anchor_position"] for c in r["conservation"] if c["anchor_position"]] assert positions == sorted(positions) assert max(positions) == 10 # anchor length, unchanged by the gap def test_the_alignment_says_it_is_approximate(): """Progressive alignment is not a simultaneous optimum. Every practical tool approximates; pretending otherwise is the dishonest part.""" m = C.align_many(HOMOLOGS) assert m["ok"] and "Approximate" in m["method"] assert all(len(r["aligned"]) == m["columns"] for r in m["rows"]) def test_the_anchor_is_the_longest_sequence(): m = C.align_many(["MAK", "MAKQWLTVEG", "MAKQ"], ["a", "b", "c"]) assert m["anchor"] == "b" def test_it_positions_itself_against_esm2_rather_than_as_a_confirmation(): """Two independent signals. Averaging them would destroy the only thing that makes having both worthwhile.""" r = C.score(HOMOLOGS) assert "complements ESM-2" in r["caveat"] assert "disagreement is worth investigating" in r["caveat"] def test_conservation_is_framed_as_an_observation_not_a_property(): r = C.score(HOMOLOGS) assert "OBSERVATION" in r["caveat"] assert "not a property of the protein" in r["caveat"] def test_it_is_reachable_ungated_and_specced(): assert "check_conservation" in t._TOOLS assert any(s["function"]["name"] == "check_conservation" for s in orch.TOOL_SPECS) assert orch._requires_confirm("check_conservation") is False def test_the_summary_leads_with_untrustworthiness_when_it_applies(): r = t.execute_tool("check_conservation", {"sequences": ["MAKQWLTVEG"] * 4 + ["MAKQWLTVEA"]}, auth_anonymous=True) assert "too redundant to trust" in orch._summarize("check_conservation", r) def test_the_spec_makes_the_agent_relay_both_limits(): d = next(s["function"]["description"] for s in orch.TOOL_SPECS if s["function"]["name"] == "check_conservation") assert "approximate" in d assert "diversity_warning" in d and "trustworthy" in d assert "complements ESM-2" in d