"""Evidence labels and untrusted-content boundaries for Ask the Lab.""" from copy import deepcopy from html.parser import HTMLParser import pytest from obliteratus.ask_lab_views import ASK_LAB_CSS, render_ask_lab def evidence(): return { "run_id": "run-123", "probe": "Give a short greeting.", "conditions": {name: {"text": f"Recorded {name}", "stop_reason": "eos"} for name in ("baseline", "candidate", "opposite", "random", "zero", "reset")}, "verification": {"status": "passed", "token_ids_exact": True, "first_logits_exact": True, "hook_registry_restored": True}, "qualification": "Exploratory comparison; capability accuracy untested.", "utility": {"changed": False, "score": None}, } class Tags(HTMLParser): def __init__(self, source): super().__init__() self.tags = [] self.feed(source) def handle_starttag(self, tag, attrs): self.tags.append((tag, dict(attrs))) @pytest.mark.parametrize("state", [None, {}, [], {"stage": "unknown", "messages": None}]) def test_idle_invitation_is_not_a_measured_result(state): result = render_ask_lab(state) assert "Got a hunch?" in result assert "Your loaded model drafts a test" in result assert "6 conditions × 2 prompts × ≤32 new tokens" in result assert "Reset verified" not in result assert "Results" not in result def test_plan_provenance_and_candidate_are_separate_from_findings(): result = render_ask_lab({"stage": "scanned", "candidate": {"layer": 2, "neuron": 17, "rank": 1}, "plan": {"goal": "Answer in Spanish", "source": "model", "contrast_a": "Hello", "contrast_b": "Hola", "probe": "Greet me", "control_probe": "2 + 2?"}}) assert "Model draft" in result assert "The scan uses A and B. The other prompts test the edit." in result assert "L2 / N17" in result assert "Candidate" in result assert "Greet me" in result and "2 + 2?" in result assert "ask-lab-phase-active" in result assert "Reset verified" not in result def test_conversation_retains_last_twelve_messages_only(): messages = [{"role": "user" if i % 2 else "assistant", "text": f"entry {i} END"} for i in range(15)] result = render_ask_lab({"messages": messages}) assert "entry 0 END" not in result and "entry 2 END" not in result assert "entry 3 END" in result and "entry 14 END" in result assert result.count('class="ask-lab-message ') == 12 assert 'role="log"' in result def test_exact_receipt_and_all_conditions_are_visible(): result = render_ask_lab({"stage": "tested", "evidence": evidence()}) assert "Reset verified · exact replay" in result assert "Before" in result and "After reset" in result for name in ("baseline", "candidate", "opposite", "random", "zero", "reset"): assert f"Recorded {name}" in result assert "full weight checksum" in result assert "Sanity check · raw results" in result assert "Finished" in result @pytest.mark.parametrize("field,value", [ ("status", "mismatch"), ("status", None), ("token_ids_exact", False), ("first_logits_exact", False), ("hook_registry_restored", False), ("token_ids_exact", "true"), ("first_logits_exact", 1), ]) def test_reset_requires_passed_status_and_exact_boolean_checks(field, value): record = evidence() record["verification"][field] = value result = render_ask_lab({"stage": "tested", "evidence": record}) assert "Reset verified" not in result assert "ask-lab-receipt-verified" not in result assert "Reset not verified" in result def test_incomplete_receipt_does_not_display_verified_badge(): record = evidence() del record["conditions"]["reset"] result = render_ask_lab({"stage": "tested", "evidence": record}) assert "Reset verified" not in result assert "Incomplete evidence" in result assert "No recorded output" in result def test_failed_action_marks_previous_evidence_without_current_success(): result = render_ask_lab({"stage": "failed", "error": "Cancelled while running", "evidence": evidence()}) assert "Run stopped" in result assert "Previous evidence · latest action failed" in result assert "Cancelled while running" in result assert "Reset verified" not in result def test_error_alone_suppresses_stale_success(): result = render_ask_lab({"stage": "tested", "error": "The recipe changed", "evidence": evidence()}) assert "Reset verified" not in result assert "ask-lab-receipt-verified" not in result def test_token_limit_is_disclosed_for_each_recorded_output(): record = evidence() record["conditions"]["candidate"]["stop_reason"] = "token_limit" record["conditions"]["opposite"].pop("stop_reason") result = render_ask_lab({"evidence": record}) assert "Token limit reached · may be cut short" in result assert "Completion status unavailable" in result assert "Recorded candidate" in result def test_unknown_source_and_interrupted_outputs_remain_explicit(): record = evidence() record["conditions"]["candidate"]["stop_reason"] = "budget" result = render_ask_lab({"plan": {"goal": "A test", "source": []}, "evidence": record}) assert "Draft · source not recorded" in result assert "Interrupted · output incomplete" in result def test_all_model_and_user_fields_are_inert_text_not_markup(): attack = '&' record = evidence() record.update(run_id=attack, probe=attack, qualification=attack, utility={attack: attack}) for trial in record["conditions"].values(): trial["text"] = attack state = {"stage": attack, "error": attack, "evidence": record, "messages": [{"role": attack, "text": attack}], "plan": dict.fromkeys(("goal", "contrast_a", "contrast_b", "probe", "control_probe", "source"), attack), "candidate": {"layer": attack, "neuron": attack}} rendered = render_ask_lab(state) tags = Tags(rendered).tags assert not any(tag in {"img", "script", "iframe", "style"} for tag, _ in tags) assert not any(key.startswith("on") for _, attrs in tags for key in attrs) assert '<img src=x onerror="alert(1)">' in rendered assert "L— / N—" in rendered assert 'class="ask-lab-message ask-lab-message-note"' in rendered assert not any(attack in value for _, attrs in tags for value in attrs.values() if value) def test_renderer_does_not_mutate_or_synthesize_measurements(): state = {"stage": "tested", "evidence": evidence()} before = deepcopy(state) render_ask_lab(state) assert state == before assert "accuracy" not in state["evidence"]["utility"] def test_css_stays_scoped_and_supports_small_or_reduced_motion_views(): assert "@media(max-width:850px)" in ASK_LAB_CSS assert "@media(prefers-reduced-motion:reduce)" in ASK_LAB_CSS assert "#neuron-lab .ask-lab-shell" in ASK_LAB_CSS assert "summary:focus-visible" in ASK_LAB_CSS assert "url(" not in ASK_LAB_CSS