Files
OBLITERATUS/tests/test_neuron_lab_prism_views.py

121 lines
5.8 KiB
Python

"""Prompt Prism display provenance and browser callback lifecycle."""
import json
from pathlib import Path
import pytest
from obliteratus.neuron_lab_prism_views import render_prism
from tests.test_ui_neuron_lab_wiring import ui, local_model, bounded_threads, callback # noqa: F401
pytestmark = pytest.mark.cpu
def test_empty_and_measured_views_do_not_invent_outcomes():
empty = render_prism()
assert "READY TO REFRACT" in empty and "twelve runs" in empty
assert "Reset matched" not in empty
report = {"variants": [{"label": "<script>label</script>", "prompt": "<img src=x>",
"rendered_prompt": "<system>private instruction</system><user>transformed</user>",
"input_token_ids": [1], "input_token_strings": ["<token>"],
"trials": {"baseline": {"text": "<script>output</script>"}}, "measurements": {}}]}
html = render_prism(report)
assert "<script>" not in html and "<img" not in html
assert "&lt;token&gt;" in html and "&lt;script&gt;output" in html
assert "Exact rendered model input" in html and "&lt;system&gt;private instruction" in html
assert "Output comparison unavailable" in html
assert "Reset needs investigation" in html and "Reset matched" not in html
assert "unavailable nats" in html
report["variants"][0]["measurements"] = {
"recipe_token_ids_changed": False, "reset_token_ids_equal": True,
"reset_first_logits_equal": False, "first_token_kl_from_baseline": 0.1,
}
html = render_prism(report)
assert "Output tokens unchanged" in html and "0.1 nats" in html
assert "Reset matched" not in html
report["variants"][0]["measurements"].update(
reset_first_logits_equal=True, recipe_token_ids_changed=True,
)
html = render_prism(report)
assert "Reset matched" in html and "Output tokens changed" in html
def test_prism_callbacks_execute_export_redact_and_invalidate(ui, local_model): # noqa: F811
session = ui.state.kwargs["value"]()
try:
callback(ui, "Load neuron model")(session, str(local_model), "cpu", "", True)
result = callback(ui, "Refract the prompt")(session, "hello world", 1.0)
assert result[1].startswith("Neuron prism_run complete.")
assert len(session.prism["variants"]) == 4
assert result[37] == {"selected": "prism"}
assert result[39].count("Reset matched") == 4
result = callback(ui, "Export prompt prism")(session, False)
report = json.loads(Path(result[40]).read_text())
assert report["text_payloads_included"] is False
assert "system_prompt" not in report
assert all("prompt" not in form and "input_token_strings" not in form for form in report["variants"])
callback(ui, "Export prompt prism")(session, True)
assert json.loads(Path(session.prism_file).read_text())["variants"][0]["prompt"] == "hello world"
callback(ui, "Apply to stack")(session, "add", 1, 0, 1, "zero", 0., -1., 1., True, "all_tokens")
assert session.prism is None and session.prism_file is None
callback(ui, "Refract the prompt")(session, "hello world", 1)
callback(ui, "Apply prompt setup")(session, "new context")
assert session.prism is None and session.prism_file is None
callback(ui, "Refract the prompt")(session, "hello world", 1)
callback(ui, "Reset all neuron controls")(session)
assert session.prism is None and session.prism_file is None
finally:
session.close()
def test_failed_prism_request_clears_old_evidence_and_export(ui, local_model): # noqa: F811
session = ui.state.kwargs["value"]()
try:
callback(ui, "Load neuron model")(session, str(local_model), "cpu", "", True)
callback(ui, "Refract the prompt")(session, "hello world", 1)
callback(ui, "Export prompt prism")(session, False)
result = callback(ui, "Refract the prompt")(session, "", 1)
assert "failed" in result[1] and session.prism is None
assert result[40] is None and "READY TO REFRACT" in result[39]
result = callback(ui, "Export prompt prism")(session, False)
assert "before exporting" in result[1]
with pytest.raises(ValueError, match="Prism completion tokens"):
session.prism_run("hello", float("inf"))
finally:
session.close()
def test_failed_redacted_export_never_returns_previous_plaintext_file(ui, local_model, monkeypatch): # noqa: F811
session = ui.state.kwargs["value"]()
try:
callback(ui, "Load neuron model")(session, str(local_model), "cpu", "", True)
callback(ui, "Refract the prompt")(session, "hello world", 1)
callback(ui, "Export prompt prism")(session, True)
assert session.prism_file is not None
def failed_write(*args, **kwargs):
raise OSError("disk full")
monkeypatch.setattr(Path, "write_text", failed_write)
result = callback(ui, "Export prompt prism")(session, False)
assert "disk full" in result[1]
assert session.prism_file is None and result[40] is None
finally:
session.close()
def test_prism_wrapper_preserves_cancellation_at_engine_boundary(ui, local_model, monkeypatch): # noqa: F811
from obliteratus import neuron_lab_prism
session = ui.state.kwargs["value"]()
try:
callback(ui, "Load neuron model")(session, str(local_model), "cpu", "", True)
original = neuron_lab_prism._run_prism_locked
def cancelled_entry(*args, **kwargs):
session.cancel()
return original(*args, **kwargs)
monkeypatch.setattr(neuron_lab_prism, "_run_prism_locked", cancelled_entry)
monkeypatch.setattr(session.handle.model, "forward", lambda **kwargs: pytest.fail("cancelled run executed"))
result = callback(ui, "Refract the prompt")(session, "hello world", 1)
assert "cancelled" in result[1] and session.prism is None
finally:
session.close()