mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
356 lines
17 KiB
Python
356 lines
17 KiB
Python
"""Presentation contracts: escaped content, measured signals and honest states."""
|
||
|
||
from html.parser import HTMLParser
|
||
import math
|
||
import re
|
||
from types import SimpleNamespace
|
||
|
||
import pytest
|
||
|
||
from obliteratus.interventions import NeuronEdit, NeuronRecipe
|
||
from obliteratus.neuron_lab_visuals import (
|
||
LAB_CSS, render_comparison, render_feedback, render_hero, render_inspector, render_stack,
|
||
)
|
||
|
||
|
||
pytestmark = pytest.mark.cpu
|
||
|
||
|
||
class Markup(HTMLParser):
|
||
def __init__(self, source):
|
||
super().__init__()
|
||
self.elements, self.words = [], []
|
||
self.feed(source)
|
||
|
||
def handle_starttag(self, tag, attrs):
|
||
self.elements.append((tag, dict(attrs)))
|
||
|
||
def handle_data(self, data):
|
||
self.words.append(data)
|
||
|
||
@property
|
||
def text(self):
|
||
return " ".join(self.words)
|
||
|
||
def tagged(self, tag):
|
||
return [attrs for kind, attrs in self.elements if kind == tag]
|
||
|
||
def classes(self, name):
|
||
return [attrs for _, attrs in self.elements if name in attrs.get("class", "").split()]
|
||
|
||
|
||
def capture(values, *, tokens=None):
|
||
tokens = tokens or [f"token-{i}" for i in range(len(values))]
|
||
finite = [value for value in values if isinstance(value, (int, float)) and math.isfinite(value)]
|
||
return {
|
||
"layer": 3, "neuron": 17,
|
||
"tokens": [[i, token, value] for i, (token, value) in enumerate(zip(tokens, values))],
|
||
"statistics": {"mean": sum(finite) / len(finite) if finite else None,
|
||
"min": min(finite, default=None), "max": max(finite, default=None),
|
||
"positive_fraction": sum(value > 0 for value in finite) / len(finite) if finite else None},
|
||
}
|
||
|
||
|
||
def test_initial_state_contains_no_invented_measurements():
|
||
hero = Markup(render_hero())
|
||
assert "No model connected" in hero.text and "OBLITERATUS / NEURON LAB" in hero.text
|
||
assert "NEURON" in hero.text and "LAB" in hero.text
|
||
inspector = Markup(render_inspector())
|
||
assert "No activations captured yet" in inspector.text
|
||
assert not inspector.tagged("svg") and not inspector.classes("nl-stat")
|
||
stack = Markup(render_stack())
|
||
assert "No intervention yet" in stack.text
|
||
comparison = Markup(render_comparison())
|
||
assert len(comparison.classes("nl-condition")) == 2
|
||
assert not comparison.classes("nl-trial-metrics")
|
||
assert "Trait and capability accuracy are untested" in comparison.text
|
||
|
||
|
||
def test_hero_uses_actual_model_geometry_and_enabled_count():
|
||
recipe = NeuronRecipe("model", (NeuronEdit(0, 1, "zero"), NeuronEdit(1, 2, "flip", enabled=False)))
|
||
session = SimpleNamespace(handle=SimpleNamespace(model_name="local/Qwen"),
|
||
layers=[{}, {}, {}], recipe=recipe)
|
||
hero = Markup(render_hero(session))
|
||
assert "local/Qwen" in hero.text and "3 layers" in hero.text
|
||
assert "1 enabled edit" in hero.text
|
||
assert "Runtime only" in hero.text and "Raw activation units" in hero.text
|
||
|
||
|
||
def test_ambient_hero_is_labelled_decorative_and_has_no_measurement_claim():
|
||
hero = Markup(render_hero())
|
||
field = hero.tagged("svg")[0]
|
||
assert field["aria-hidden"] == "true" and field["focusable"] == "false"
|
||
assert field["data-kind"] == "decorative"
|
||
assert "Ambient field · schematic" in hero.text
|
||
assert not hero.classes("nl-stat") and not hero.classes("nl-live")
|
||
assert "layers" not in hero.text and "enabled edits" not in hero.text
|
||
# Decorative SVG references stay inside the document: no remote images or scripts.
|
||
ids = {attrs["id"] for _, attrs in hero.elements if "id" in attrs}
|
||
for _, attrs in hero.elements:
|
||
for value in attrs.values():
|
||
for reference in re.findall(r"url\(([^)]+)\)", value):
|
||
assert reference.startswith("#") and reference[1:] in ids
|
||
assert not hero.tagged("image") and not hero.tagged("script")
|
||
|
||
|
||
@pytest.mark.parametrize("name,visible", [
|
||
("/Users/researcher/checkpoints/Qwen3-4B/", "Qwen3-4B"),
|
||
(r"C:\Research\Models\Qwen3-4B", "Qwen3-4B"),
|
||
(r"\\server\checkpoints\Qwen3-4B", "Qwen3-4B"),
|
||
("~/models/Qwen3-4B", "Qwen3-4B"),
|
||
("./cache/Qwen3-4B", "Qwen3-4B"),
|
||
("Qwen/Qwen3-4B", "Qwen/Qwen3-4B"),
|
||
("tiny-model", "tiny-model"),
|
||
("/", "/"),
|
||
])
|
||
def test_hero_shortens_explicit_local_paths_but_keeps_full_accessible_identity(name, visible):
|
||
session = SimpleNamespace(handle=SimpleNamespace(model_name=name), layers=[], recipe=None)
|
||
markup = Markup(render_hero(session))
|
||
model = markup.classes("nl-model-name")[0]
|
||
assert model["title"] == name and model["aria-label"] == name
|
||
assert visible in markup.text
|
||
if visible != name:
|
||
assert name not in markup.text
|
||
|
||
|
||
def test_local_model_basename_still_escapes_hostile_markup():
|
||
name = '/tmp/checkpoints/<img src=x onerror="bad">'
|
||
session = SimpleNamespace(handle=SimpleNamespace(model_name=name), layers=[], recipe=None)
|
||
source = render_hero(session)
|
||
markup = Markup(source)
|
||
assert not markup.tagged("img") and "<img" in source
|
||
assert markup.classes("nl-model-name")[0]["aria-label"] == name
|
||
|
||
|
||
@pytest.mark.parametrize("value", ["<script>alert(1)</script>", '\"><img src=x onerror="boom">',
|
||
"a & b ' c <svg/onload=bad>"])
|
||
def test_untrusted_model_token_prompt_output_and_feedback_are_text_only(value):
|
||
session = SimpleNamespace(handle=SimpleNamespace(model_name=value), layers=[], recipe=None)
|
||
sources = [render_hero(session), render_inspector(capture([1.], tokens=[value])),
|
||
render_comparison({"prompt": value, "trials": {"recipe": {"text": value}}}),
|
||
render_feedback("Neuron inspect failed: " + value)]
|
||
for source in sources:
|
||
markup = Markup(source)
|
||
assert value in markup.text or value in str(markup.elements)
|
||
assert not markup.tagged("script") and not markup.tagged("img")
|
||
assert not any(key.lower().startswith("on") for _, attrs in markup.elements for key in attrs)
|
||
assert "<script>" not in source and "<img " not in source
|
||
|
||
|
||
def test_signed_trace_and_keyboard_token_values_preserve_measurements():
|
||
markup = Markup(render_inspector(capture([-2., 0., 3.])))
|
||
bars = markup.tagged("rect")
|
||
assert [bar["fill"] for bar in bars] == ["#79d4db", "#b59aff"]
|
||
baseline = next(line for line in markup.tagged("line") if line.get("stroke") == "#62686f")
|
||
zero = float(baseline["y1"])
|
||
negative, positive = bars
|
||
assert float(negative["y"]) == pytest.approx(zero)
|
||
assert float(positive["y"]) + float(positive["height"]) == pytest.approx(zero)
|
||
assert float(negative["height"]) / float(positive["height"]) == pytest.approx(2 / 3)
|
||
chips = markup.classes("nl-token")
|
||
assert len(chips) == 3 and all(chip["tabindex"] == "0" for chip in chips)
|
||
assert "−2 raw" in chips[0]["aria-label"] and "0 raw" in chips[1]["aria-label"]
|
||
assert "+3 raw" in chips[2]["data-reading"]
|
||
assert "L03 / N17" in markup.text and "33%" in markup.text
|
||
assert "one prompt, not a behavioral explanation" in markup.text
|
||
|
||
|
||
@pytest.mark.parametrize("values", [[0., 0.], [-4., -1.], [1., 4.]])
|
||
def test_zero_and_one_sided_traces_keep_an_honest_zero_axis(values):
|
||
markup = Markup(render_inspector(capture(values)))
|
||
baseline = next(line for line in markup.tagged("line") if line.get("stroke") == "#62686f")
|
||
zero = float(baseline["y1"])
|
||
if all(value == 0 for value in values):
|
||
assert not markup.tagged("rect")
|
||
assert "All captured values shown are zero" in markup.text
|
||
assert "Observed range 0 to 0" in markup.tagged("svg")[0]["aria-label"]
|
||
assert not any("+1" in word or "−1" in word for word in markup.words)
|
||
elif all(value < 0 for value in values):
|
||
assert all(float(bar["y"]) == zero for bar in markup.tagged("rect"))
|
||
assert "0%" in markup.text
|
||
else:
|
||
assert all(float(bar["y"]) + float(bar["height"]) == pytest.approx(zero)
|
||
for bar in markup.tagged("rect"))
|
||
assert "100%" in markup.text
|
||
|
||
|
||
@pytest.mark.parametrize("values", [[float("nan"), float("inf"), -float("inf")],
|
||
[1., float("nan"), -2.],
|
||
[1e308, -1e308], [1e-308, -1e-308]])
|
||
def test_nonfinite_and_extreme_finite_numbers_never_make_invalid_svg(values):
|
||
source = render_inspector(capture(values))
|
||
markup = Markup(source)
|
||
for tag in ("rect", "line", "text"):
|
||
for element in markup.tagged(tag):
|
||
for name in ("x", "y", "x1", "x2", "y1", "y2", "height", "width"):
|
||
if name in element:
|
||
assert math.isfinite(float(element[name]))
|
||
assert '="nan"' not in source and '="inf"' not in source
|
||
if not any(math.isfinite(value) for value in values):
|
||
assert "No finite activations to plot" in markup.text
|
||
assert "3 unavailable values" in markup.text
|
||
assert not markup.tagged("svg")
|
||
|
||
|
||
def test_trace_bound_is_explicit_and_retains_token_order():
|
||
source = render_inspector(capture([float(i % 3 - 1) for i in range(600)]))
|
||
markup = Markup(source)
|
||
assert "First 512 of 600 positions" in markup.text
|
||
assert len(markup.classes("nl-token")) == 512
|
||
assert "token-511" in markup.text and "token-512" not in markup.text
|
||
assert len(markup.tagged("rect")) <= 512
|
||
assert "No activations captured yet" in Markup(render_inspector({"tokens": "malformed"})).text
|
||
|
||
|
||
def test_oversized_numeric_payload_is_unavailable_and_plain_text_format_is_explicit():
|
||
markup = Markup(render_inspector({"tokens": [[0, "token", 10 ** 400]],
|
||
"statistics": {"mean": 10 ** 400}}))
|
||
assert "No finite activations to plot" in markup.text
|
||
assert "—" in markup.text and not markup.tagged("svg")
|
||
comparison = Markup(render_comparison({"decoding": {"chat_template_applied": False}}))
|
||
assert "Plain text input" in comparison.text and "Model chat template" not in comparison.text
|
||
|
||
|
||
def test_recipe_stack_names_order_scope_disabled_rows_and_exact_operators():
|
||
recipe = NeuronRecipe("model", (
|
||
NeuronEdit(0, 2, "add", -3.), NeuronEdit(0, 2, "scale", 2.),
|
||
NeuronEdit(1, 3, "flip"), NeuronEdit(2, 4, "zero", enabled=False),
|
||
NeuronEdit(2, 4, "set", 7.), NeuronEdit(3, 5, "clamp", lower=-1., upper=1.),
|
||
), scope="generated_tokens")
|
||
markup = Markup(render_stack(recipe))
|
||
assert len(markup.classes("nl-edit")) == 6
|
||
assert len(markup.classes("nl-disabled")) == 1
|
||
assert "5 enabled / 6 stored" in markup.text
|
||
for expression in ("a − 3", "a × 2", "−a", "[−1, 1]"):
|
||
assert expression in markup.text
|
||
assert "Zero · Disabled" in markup.text
|
||
assert "Generated tokens" in markup.text
|
||
assert "first answer prediction stays baseline" in markup.text
|
||
assert markup.text.index("Add") < markup.text.index("Scale") < markup.text.index("Flip")
|
||
assert "top to bottom" in markup.text
|
||
|
||
|
||
def test_empty_stack_guidance_is_independent_of_responsive_control_position():
|
||
text = Markup(render_stack()).text
|
||
assert "Choose a channel operation, then apply it to the stack." in text
|
||
assert "below" not in text and "above" not in text and "left" not in text
|
||
|
||
|
||
def test_recipe_dict_and_unknown_operator_do_not_inject_markup():
|
||
recipe = {"edits": [{"layer": 2, "neuron": 3, "operation": "<script>evil</script>"},
|
||
{"layer": 4, "neuron": 5, "operation": "clamp", "upper": 4.}],
|
||
"scope": "<img src=x>"}
|
||
markup = Markup(render_stack(recipe))
|
||
assert not markup.tagged("script") and not markup.tagged("img")
|
||
assert "Unavailable" in markup.text and "Scope unrecorded" in markup.text
|
||
assert "[−∞, 4]" in markup.text
|
||
|
||
|
||
def test_comparison_cards_keep_real_zero_metrics_and_quality_claims_separate():
|
||
recipe = NeuronRecipe("model", (NeuronEdit(1, 2, "zero"),)).to_dict()
|
||
comparison = {
|
||
"prompt": "A shared question", "decoding": {"do_sample": False, "use_cache": False,
|
||
"chat_template_applied": True},
|
||
"trials": {
|
||
"baseline": {"text": "First answer", "tokens": 0, "seconds": 0.,
|
||
"first_token_kl_from_baseline": 0., "repeated_token_fraction": 0.,
|
||
"invalid_output_heuristic": False},
|
||
"recipe": {"text": "Another answer", "recipe": recipe, "tokens": 12,
|
||
"seconds": 1.25, "first_token_kl_from_baseline": .032,
|
||
"repeated_token_fraction": .25, "invalid_output_heuristic": True},
|
||
},
|
||
}
|
||
markup = Markup(render_comparison(comparison))
|
||
assert "First answer" in markup.text and "Another answer" in markup.text
|
||
assert "0.00s" in markup.text and "1.25s" in markup.text and "0.032" in markup.text
|
||
assert "Repeated tokens 0%" in markup.text and "Repeated tokens 25%" in markup.text
|
||
assert "Output heuristic flagged" in markup.text and "No output heuristic flag" in markup.text
|
||
assert "1 ENABLED EDIT / ALL TOKENS" in markup.text
|
||
assert "A shared question" in markup.text and "Model chat template" in markup.text
|
||
assert "Greedy decoding" in markup.text and "Fresh no-cache forwards" in markup.text
|
||
assert len(markup.classes("nl-trial-metrics")) == 2
|
||
assert len(markup.classes("nl-waiting")) == 0
|
||
|
||
|
||
def test_missing_output_and_nonfinite_metrics_are_unavailable_not_zero():
|
||
markup = Markup(render_comparison({"trials": {
|
||
"baseline": {"tokens": float("nan"), "seconds": float("inf"),
|
||
"first_token_kl_from_baseline": float("nan"), "repeated_token_fraction": 2.},
|
||
"recipe": {"text": ""}, "control": {"text": "control response"},
|
||
}}))
|
||
assert "Output text was not included" in markup.text
|
||
assert "No visible text was generated" in markup.text
|
||
assert "control response" in markup.text
|
||
assert "Repeated tokens —" in markup.text and "Output heuristic unrecorded" in markup.text
|
||
assert "Greedy decoding" not in markup.text and "Fresh no-cache forwards" not in markup.text
|
||
assert "nan" not in markup.text and "inf" not in markup.text
|
||
|
||
|
||
def test_feedback_is_concise_and_does_not_mislabel_cancellation_or_failure():
|
||
assert "Connect a model" in Markup(render_feedback()).text
|
||
assert "Choose a channel" in Markup(render_feedback(loaded=True)).text
|
||
done = Markup(render_feedback("Neuron inspect complete.\n\nMemory admission warning: long details"))
|
||
assert "DONE" in done.text and "long details" not in done.text
|
||
assert done.classes("nl-feedback")[0]["role"] == "status"
|
||
failed = Markup(render_feedback("Neuron compare failed: Load a neuron model first"))
|
||
assert "ATTENTION" in failed.text and "Load a neuron model first" in failed.text
|
||
assert failed.classes("nl-feedback-error")
|
||
cancelled = Markup(render_feedback("Cancellation requested; waiting for forward."))
|
||
assert "CANCELLATION" in cancelled.text and "DONE" not in cancelled.text
|
||
assert len(Markup(render_feedback("x" * 2000)).text) < 400
|
||
|
||
|
||
def test_style_is_scoped_and_does_not_load_external_resources():
|
||
assert "@import" not in LAB_CSS and "url(" not in LAB_CSS
|
||
scopes = []
|
||
for block in re.finditer(r"([^{}]+)\{|}", LAB_CSS):
|
||
if block.group(1) is None:
|
||
scopes.pop()
|
||
continue
|
||
selector = block.group(1).strip()
|
||
if selector.startswith("@keyframes"):
|
||
assert re.fullmatch(r"@keyframes nl-[a-z-]+", selector)
|
||
elif selector.startswith("@media"):
|
||
pass
|
||
elif any(scope.startswith("@keyframes") for scope in scopes):
|
||
assert selector in {"from", "to"}
|
||
else:
|
||
assert all(part.strip().startswith("#neuron-lab") for part in selector.split(","))
|
||
scopes.append(selector)
|
||
assert not scopes
|
||
assert "prefers-reduced-motion" in LAB_CSS
|
||
assert '#neuron-lab[data-motion="paused"]' in LAB_CSS
|
||
assert "animation-play-state: paused !important" in LAB_CSS
|
||
|
||
|
||
def test_selection_and_slider_styles_override_the_native_theme_explicitly():
|
||
# Checked controls need a visible fill and mark after the general input
|
||
# background reset; keyboard focus intentionally uses a different color.
|
||
for kind in ("checkbox", "radio"):
|
||
assert f'#neuron-lab input[type="{kind}"]:checked::after' in LAB_CSS
|
||
assert f'#neuron-lab input[type="{kind}"]:focus-visible' in LAB_CSS
|
||
assert "background-color: var(--nl-violet) !important; background-image: none !important" in LAB_CSS
|
||
assert "outline: 2px solid var(--nl-cyan) !important" in LAB_CSS
|
||
assert '--slider-color: var(--nl-violet) !important' in LAB_CSS
|
||
assert '::-webkit-slider-runnable-track' in LAB_CSS
|
||
assert 'var(--range_progress, 0%)' in LAB_CSS
|
||
assert '::-moz-range-progress { background-color: var(--nl-violet)' in LAB_CSS
|
||
|
||
|
||
def test_receipt_requires_all_checks_and_only_renders_present_conditions():
|
||
receipt = {"run_id": "abc<unsafe>", "trials": {"baseline": {"text": "hello"}, "recipe": {"text": "hola"}},
|
||
"verification": {"status": "not_run"}}
|
||
markup = Markup(render_comparison(receipt))
|
||
assert len(markup.classes("nl-condition")) == 2
|
||
assert "Reset not checked yet" in markup.text
|
||
receipt["verification"] = {"status": "passed"}
|
||
assert "Reset differs" in Markup(render_comparison(receipt)).text
|
||
receipt["trials"]["reset"] = {"text": "hello", "stop_reason": "token_limit"}
|
||
receipt["verification"].update({k: True for k in ("token_ids_exact", "first_logits_exact", "hook_registry_restored", "input_replayed_exactly")})
|
||
markup = Markup(render_comparison(receipt))
|
||
assert "Reset verified" in markup.text and "Token limit reached" in markup.text
|
||
assert len(markup.classes("nl-condition")) == 3
|
||
assert not markup.tagged("unsafe")
|
||
receipt["verification"]["status"] = "failed"
|
||
assert "Reset check incomplete" in Markup(render_comparison(receipt)).text
|