mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
571 lines
24 KiB
Python
571 lines
24 KiB
Python
"""Concept Lab callback wiring and session guards without Gradio or a model.
|
|
|
|
The release CPU lane has no Gradio, so ``build_concept_lab`` is driven through a
|
|
recording stand-in that captures each registered callback. The callbacks are
|
|
then invoked directly to check the server-side state machine they expose.
|
|
"""
|
|
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
from obliteratus import ui_concept_lab
|
|
from obliteratus.ui_concept_lab import ConceptLabSession, build_concept_lab, concept_choices
|
|
|
|
pytestmark = pytest.mark.cpu
|
|
PACK = Path(__file__).resolve().parent / "fixtures/concept_packs/valid_minimal"
|
|
|
|
|
|
class _GradioError(Exception):
|
|
pass
|
|
|
|
|
|
class _Component:
|
|
def __init__(self, kind, *args, **kwargs):
|
|
self.kind, self.args, self.kwargs = kind, args, kwargs
|
|
self.clicks, self.changes = [], []
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *exc):
|
|
return False
|
|
|
|
def click(self, fn, inputs, outputs, **kwargs):
|
|
self.clicks.append((fn, inputs, outputs, kwargs))
|
|
|
|
def change(self, fn, inputs, outputs, **kwargs):
|
|
self.changes.append((fn, inputs, outputs, kwargs))
|
|
|
|
|
|
class _FakeGradio:
|
|
Error = _GradioError
|
|
|
|
def __init__(self):
|
|
self.components = []
|
|
|
|
def update(self, **kwargs):
|
|
return kwargs
|
|
|
|
def __getattr__(self, kind):
|
|
def build(*args, **kwargs):
|
|
component = _Component(kind, *args, **kwargs)
|
|
self.components.append(component)
|
|
return component
|
|
return build
|
|
|
|
|
|
@dataclass
|
|
class _Run:
|
|
evaluation: object = None
|
|
repair: object = None
|
|
|
|
|
|
@dataclass
|
|
class _Report:
|
|
verdict: str = "pass"
|
|
notes: list = field(default_factory=list)
|
|
|
|
|
|
class _FakeSession:
|
|
"""Stand-in honouring the real session's step ordering, without a model."""
|
|
|
|
instances = []
|
|
|
|
def __init__(self, source, model, seed, refusal_size, tokens):
|
|
if not model:
|
|
raise ValueError("Enter a model id or local model directory")
|
|
self.args = (source, model, seed, refusal_size, tokens)
|
|
self.target = SimpleNamespace(id=f"user/{source}", tier="user", caveats=["fixture"])
|
|
self.direction = self.run = self.artifact = None
|
|
self.pristine = self.modified = ""
|
|
self.closed = False
|
|
_FakeSession.instances.append(self)
|
|
|
|
def close(self):
|
|
self.closed = True
|
|
|
|
def can_apply(self, reason=""):
|
|
report = self.direction.validation if self.direction else {}
|
|
return self.run is None and report.get("verdict") in {"pass", "fail"} and (
|
|
report["verdict"] != "fail" or bool(reason.strip()))
|
|
|
|
def extract(self):
|
|
self.direction = SimpleNamespace(validation={})
|
|
|
|
def validate(self):
|
|
self.direction.validation = {"verdict": "fail"}
|
|
|
|
def apply(self, mode, layers, positions, alpha, reason, preview,
|
|
project_embeddings=False, tied_embeddings=None, steering_scale="recipe",
|
|
subspace=False, reference="zero", preserve=""):
|
|
if not self.can_apply(reason):
|
|
raise ValueError("Validate first; a failed gate requires a nonempty override reason")
|
|
self.run = _Run()
|
|
self.embedding_options = (project_embeddings, tied_embeddings)
|
|
self.geometry_options = (steering_scale, subspace, reference, preserve)
|
|
self.pristine, self.modified = preview, f"{mode}:{preview}"
|
|
|
|
def evaluate(self, alphas=None):
|
|
self.alphas = alphas
|
|
self.run.evaluation = _Report()
|
|
|
|
def repair(self):
|
|
raise RuntimeError("repair backend unavailable")
|
|
|
|
def export(self, kind, publish, reason, original, allow_collateral_fail=""):
|
|
self.allow_collateral_fail = allow_collateral_fail
|
|
self.artifact = f"/exports/{kind}.zip"
|
|
|
|
|
|
@pytest.fixture
|
|
def lab(monkeypatch):
|
|
gradio = _FakeGradio()
|
|
monkeypatch.setitem(sys.modules, "gradio", gradio)
|
|
monkeypatch.setattr(ui_concept_lab, "ConceptLabSession", _FakeSession)
|
|
monkeypatch.setattr(ui_concept_lab, "concept_choices", lambda: [("Refusal", "refusal")])
|
|
_FakeSession.instances = []
|
|
build_concept_lab({"Tiny": "tiny/model"})
|
|
buttons = {c.args[0]: c for c in gradio.components if c.kind == "Button"}
|
|
state = next(c for c in gradio.components if c.kind == "State")
|
|
override = next(c for c in gradio.components
|
|
if c.kind == "Textbox" and c.kwargs.get("label") == "Gate override reason")
|
|
return SimpleNamespace(gradio=gradio, buttons=buttons, state=state, override=override)
|
|
|
|
|
|
def _callback(lab, label):
|
|
(fn, inputs, outputs, kwargs), = lab.buttons[label].clicks
|
|
return fn, inputs, outputs, kwargs
|
|
|
|
|
|
def _interactive(result):
|
|
"""Map the six step buttons to their interactive flags."""
|
|
labels = ("Extract", "Validate", "Apply", "Evaluate", "Repair", "Export")
|
|
return {label: update["interactive"] for label, update in zip(labels, result[8:])}
|
|
|
|
|
|
def test_layout_registers_every_step_with_named_apis_and_session_cleanup(lab):
|
|
state = lab.state
|
|
assert state.kwargs["time_to_live"] == 3600
|
|
session = _FakeSession("x", "m", 1, 1, 1)
|
|
state.kwargs["delete_callback"](session)
|
|
assert session.closed
|
|
state.kwargs["delete_callback"](None) # Expired empty state is a no-op.
|
|
|
|
names = {label: _callback(lab, label)[3]["api_name"] for label in lab.buttons}
|
|
assert names == {
|
|
"Define": "concept_define", "Extract": "concept_extract",
|
|
"Validate": "concept_validate", "Apply": "concept_apply",
|
|
"Evaluate": "concept_evaluate", "Repair": "concept_repair",
|
|
"Export": "concept_export",
|
|
}
|
|
assert all(_callback(lab, label)[3]["concurrency_id"] == "concept_lab"
|
|
for label in lab.buttons)
|
|
# Every button except Define starts disabled until its precondition holds.
|
|
assert {label for label, button in lab.buttons.items()
|
|
if button.kwargs.get("interactive") is False} == set(lab.buttons) - {"Define"}
|
|
model = next(c for c in lab.gradio.components
|
|
if c.kind == "Dropdown" and c.kwargs.get("label") == "Concept model")
|
|
assert model.kwargs["choices"] == [("Tiny", "tiny/model")]
|
|
|
|
|
|
def test_full_callback_flow_enables_steps_in_order(lab):
|
|
define = _callback(lab, "Define")[0]
|
|
result = define(None, "pack", "tiny/model", 7, 3, 2)
|
|
session = result[0]
|
|
assert session.args == ("pack", "tiny/model", 7, 3, 2)
|
|
assert result[1] == "Defined user/pack (user). fixture"
|
|
assert result[2:8] == (None, None, None, "", "", None)
|
|
assert _interactive(result) == {
|
|
"Extract": True, "Validate": False, "Apply": False,
|
|
"Evaluate": False, "Repair": False, "Export": False,
|
|
}
|
|
|
|
result = _callback(lab, "Extract")[0](session, "")
|
|
assert result[1] == "Extract complete."
|
|
assert _interactive(result)["Validate"] and not _interactive(result)["Extract"]
|
|
|
|
result = _callback(lab, "Validate")[0](session, "")
|
|
assert result[2] == {"verdict": "fail"}
|
|
assert not _interactive(result)["Apply"] # Failed gate without an override reason.
|
|
|
|
apply = _callback(lab, "Apply")[0]
|
|
result = apply(session, "", "project", "all", "all_tokens", 1.0, "", "hi")
|
|
assert result[1].startswith("Apply failed: Validate first")
|
|
assert session.run is None
|
|
|
|
(toggle, inputs, target, kwargs), = lab.override.changes
|
|
assert target is lab.buttons["Apply"] and kwargs["queue"] is False
|
|
assert toggle(session, "documented reason") == {"interactive": True}
|
|
assert toggle(session, " ") == {"interactive": False}
|
|
assert toggle(None, "documented reason") == {"interactive": False}
|
|
|
|
result = apply(session, "why", "steer", "all", "all_tokens", 1.0, "why", "hi")
|
|
assert result[1] == "Apply complete."
|
|
assert result[5:7] == ("hi", "steer:hi")
|
|
assert _interactive(result)["Evaluate"] and not _interactive(result)["Apply"]
|
|
|
|
result = _callback(lab, "Evaluate")[0](session, "", "0,0.5,1")
|
|
assert session.alphas == "0,0.5,1"
|
|
assert result[3] == {"verdict": "pass", "notes": []}
|
|
assert _interactive(result)["Repair"] and _interactive(result)["Export"]
|
|
|
|
result = _callback(lab, "Repair")[0](session, "")
|
|
assert result[1] == "Repair failed: repair backend unavailable"
|
|
assert result[4] is None
|
|
|
|
session.run.repair = _Report(verdict="repaired")
|
|
result = _callback(lab, "Export")[0](session, "", "direction", False, "", False,
|
|
"Reviewed collateral")
|
|
assert session.allow_collateral_fail == "Reviewed collateral"
|
|
assert result[1] == "Export complete."
|
|
assert result[4] == {"verdict": "repaired", "notes": []}
|
|
assert result[7] == "/exports/direction.zip"
|
|
assert not _interactive(result)["Repair"]
|
|
|
|
|
|
def test_mode_defaults_remove_and_positive_doses_are_explicit(lab):
|
|
mode = next(c for c in lab.gradio.components
|
|
if c.kind == "Dropdown" and c.kwargs.get("label") == "Mode")
|
|
alpha = next(c for c in lab.gradio.components
|
|
if c.kind == "Slider" and c.kwargs.get("label") == "Coefficient")
|
|
assert mode.kwargs["value"] == "project"
|
|
assert alpha.kwargs["value"] == 1
|
|
assert alpha.args == (-3, 3)
|
|
(change, inputs, outputs, _), = mode.changes
|
|
assert inputs == [mode] and outputs == [alpha]
|
|
assert change("steer") == {"value": -1}
|
|
assert change("project") == {"value": 1}
|
|
ladder = next(c for c in lab.gradio.components
|
|
if c.kind == "Textbox" and c.kwargs.get("label") == "Steering dose ladder")
|
|
assert [float(value) for value in ladder.kwargs["value"].split(",")] == [
|
|
-3, -2, -1.5, -1, -.5, 0,
|
|
]
|
|
assert ladder in _callback(lab, "Evaluate")[1]
|
|
|
|
|
|
def test_embedding_controls_default_off_and_reach_apply(lab):
|
|
project = next(c for c in lab.gradio.components
|
|
if c.kwargs.get("label") == "Project input embeddings (orthogonalize / lora)")
|
|
tied = next(c for c in lab.gradio.components
|
|
if c.kwargs.get("label") == "Tied embedding policy")
|
|
assert project.kwargs["value"] is False
|
|
assert tied.kwargs["value"] == ""
|
|
assert [value for _, value in tied.args[0]] == ["", "edit_both", "untie"]
|
|
callback, inputs, _, _ = _callback(lab, "Apply")
|
|
assert inputs[-6:-4] == [project, tied]
|
|
session = _FakeSession("pack", "tiny/model", 1, 1, 1)
|
|
session.extract()
|
|
session.validate()
|
|
result = callback(session, "why", "orthogonalize", "all", "all_tokens", 1.,
|
|
"why", "hi", True, "untie")
|
|
assert result[1] == "Apply complete."
|
|
assert session.embedding_options == (True, "untie")
|
|
|
|
|
|
def test_redefine_closes_previous_session_and_failed_define_keeps_it(lab):
|
|
define = _callback(lab, "Define")[0]
|
|
first = define(None, "pack", "tiny/model", 1, 1, 1)[0]
|
|
second = define(first, "pack", "tiny/model", 2, 1, 1)[0]
|
|
assert first.closed and not second.closed
|
|
with pytest.raises(ValueError, match="Enter a model"):
|
|
define(second, "pack", None, 3, 1, 1)
|
|
assert not second.closed
|
|
|
|
|
|
def test_step_without_session_raises_user_facing_error(lab):
|
|
with pytest.raises(_GradioError, match="Define a concept run first"):
|
|
_callback(lab, "Extract")[0](None, "")
|
|
|
|
|
|
def test_user_tier_choices_are_namespaced(monkeypatch):
|
|
packs = [
|
|
SimpleNamespace(manifest={"id": "refusal", "tier": "core", "title": "Ignored"}),
|
|
SimpleNamespace(manifest={"id": "mine", "tier": "user", "title": "Mine"}),
|
|
SimpleNamespace(manifest={"id": "user/kept", "tier": "user", "title": "Kept"}),
|
|
SimpleNamespace(manifest={"id": "pain", "tier": "research", "title": "Pain"}),
|
|
]
|
|
monkeypatch.setattr(ui_concept_lab, "_registry",
|
|
lambda: SimpleNamespace(list_packs=lambda: packs))
|
|
assert concept_choices() == [
|
|
("Refusal", "refusal"), ("Mine", "user/mine"), ("Kept", "user/kept"), ("Pain", "pain"),
|
|
]
|
|
|
|
|
|
def test_session_rejects_blank_model_before_creating_storage(monkeypatch):
|
|
created = []
|
|
monkeypatch.setattr(ui_concept_lab.tempfile, "TemporaryDirectory",
|
|
lambda **kwargs: created.append(kwargs))
|
|
with pytest.raises(ValueError, match="Enter a model id"):
|
|
ConceptLabSession(str(PACK), " ")
|
|
assert created == []
|
|
|
|
|
|
def test_session_removes_storage_when_pipeline_construction_fails(monkeypatch):
|
|
seen = {}
|
|
|
|
def broken_pipeline(model, **kwargs):
|
|
seen.update(kwargs, model=model)
|
|
assert Path(kwargs["output_dir"]).parent.is_dir()
|
|
raise RuntimeError("cannot open model")
|
|
|
|
monkeypatch.setattr(ui_concept_lab, "AbliterationPipeline", broken_pipeline)
|
|
with pytest.raises(RuntimeError, match="cannot open model"):
|
|
ConceptLabSession(str(PACK), " tiny/model ", seed="5", refusal_size=3.0,
|
|
max_new_tokens=2.0)
|
|
assert seen["model"] == "tiny/model"
|
|
assert (seen["seed"], seen["refusal_eval_size"], seen["refusal_eval_max_new_tokens"]) == (5, 3, 2)
|
|
assert seen["device"] == "cpu" and seen["gate_mode"] == "report"
|
|
assert not Path(seen["output_dir"]).parent.exists()
|
|
|
|
|
|
def _bare_session(monkeypatch, **attrs):
|
|
session = object.__new__(ConceptLabSession)
|
|
session.__dict__.update(attrs)
|
|
monkeypatch.setattr(ConceptLabSession, "_admit_custom_phase",
|
|
lambda self, stage, operation: None)
|
|
return session
|
|
|
|
|
|
def test_apply_requires_preview_prompt_after_capturing_evaluation(monkeypatch):
|
|
captured = []
|
|
pipeline = SimpleNamespace(
|
|
_prepare_concept_preservation=lambda: None,
|
|
_capture_concept_evaluation=lambda: captured.append("evaluation"),
|
|
_evaluation_battery=SimpleNamespace(generate=lambda *a: pytest.fail("generated")),
|
|
)
|
|
session = _bare_session(
|
|
monkeypatch, run=None, pipeline=pipeline,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}),
|
|
)
|
|
monkeypatch.setattr(ui_concept_lab, "enforce_gate", lambda report: captured.append(report))
|
|
with pytest.raises(ValueError, match="Enter a preview prompt"):
|
|
session.apply("project", "all", "all_tokens", 1.0, "", " ")
|
|
assert captured == [{"verdict": "pass", "override_gate": None}, "evaluation"]
|
|
assert session.run is None
|
|
|
|
|
|
@pytest.mark.parametrize("project_embeddings,tied_embeddings", [
|
|
(False, ""), (True, "edit_both"), (True, "untie"),
|
|
])
|
|
def test_apply_threads_embedding_policy_to_intervention(monkeypatch, project_embeddings,
|
|
tied_embeddings):
|
|
from unittest.mock import Mock
|
|
|
|
model = object()
|
|
pipeline = SimpleNamespace(
|
|
_prepare_concept_preservation=Mock(), _preserved_directions=[],
|
|
_capture_concept_evaluation=Mock(), _capture_concept_repair=Mock(),
|
|
_evaluation_battery=SimpleNamespace(generate=Mock(return_value=["preview"]), decoding={}),
|
|
handle=SimpleNamespace(model=model),
|
|
)
|
|
session = _bare_session(
|
|
monkeypatch, run=None, pipeline=pipeline,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}),
|
|
)
|
|
monkeypatch.setattr(ui_concept_lab, "enforce_gate", lambda report: None)
|
|
apply = Mock(return_value=object())
|
|
monkeypatch.setattr(ui_concept_lab, "apply_concept_intervention", apply)
|
|
session.apply("lora", "all", "all_tokens", 1., "", "hello",
|
|
project_embeddings, tied_embeddings)
|
|
assert apply.call_args.args == (model, session.direction, "lora")
|
|
assert apply.call_args.kwargs["project_embeddings"] is project_embeddings
|
|
assert apply.call_args.kwargs["tied_embeddings"] == (tied_embeddings or None)
|
|
assert session.run is apply.return_value
|
|
|
|
|
|
def test_repair_cannot_be_recorded_twice(monkeypatch):
|
|
session = _bare_session(
|
|
monkeypatch, run=SimpleNamespace(evaluation=object(), repair=object()),
|
|
pipeline=SimpleNamespace(_repair_backend=pytest.fail),
|
|
)
|
|
with pytest.raises(ValueError, match="already been recorded"):
|
|
session.repair()
|
|
|
|
|
|
@pytest.mark.parametrize("mode,scale,subspace,reference", [
|
|
("project", "raw", False, "zero"),
|
|
("steer", "recipe", True, "zero"),
|
|
("steer", "recipe", False, "mean"),
|
|
("lora", "recipe", False, "mean"),
|
|
("project", "unknown", False, "zero"),
|
|
("project", "recipe", False, "unknown"),
|
|
])
|
|
def test_geometry_controls_reject_incompatible_options_before_capture(
|
|
monkeypatch, mode, scale, subspace, reference):
|
|
session = _bare_session(monkeypatch, run=None, pipeline=None,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}))
|
|
with pytest.raises(ValueError):
|
|
session.apply(mode, "all", "all_tokens", 1., "", "hello",
|
|
steering_scale=scale, subspace=subspace, reference=reference)
|
|
|
|
|
|
@pytest.mark.parametrize("mode,scale,subspace,reference", [
|
|
("steer", "raw", False, "zero"),
|
|
("steer", "vector", False, "zero"),
|
|
("project", "recipe", True, "mean"),
|
|
("orthogonalize", "recipe", True, "zero"),
|
|
])
|
|
def test_geometry_and_preservation_reach_intervention_and_repair(
|
|
monkeypatch, mode, scale, subspace, reference):
|
|
from unittest.mock import Mock
|
|
|
|
protected = object()
|
|
pipeline = SimpleNamespace(
|
|
_prepare_concept_preservation=Mock(), _preserved_directions=[protected],
|
|
_capture_concept_evaluation=Mock(), _capture_concept_repair=Mock(),
|
|
_evaluation_battery=SimpleNamespace(generate=Mock(return_value=["preview"]), decoding={}),
|
|
handle=SimpleNamespace(model=object(), num_layers=2),
|
|
)
|
|
session = _bare_session(monkeypatch, run=None, pipeline=pipeline,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"},
|
|
subspaces={0: object()}))
|
|
monkeypatch.setattr(ui_concept_lab, "enforce_gate", lambda report: None)
|
|
monkeypatch.setattr("obliteratus.concepts.intervention._selected_layers", lambda *args: [0])
|
|
monkeypatch.setattr("obliteratus.concepts.geometry.intervention_source_layer", lambda *args: 0)
|
|
apply = Mock(return_value=object())
|
|
monkeypatch.setattr(ui_concept_lab, "apply_concept_intervention", apply)
|
|
session.apply(mode, "all", "all_tokens", .5, "", "hello",
|
|
steering_scale=scale, subspace=subspace, reference=reference,
|
|
preserve="refusal\n /tmp/protected.safetensors\n")
|
|
assert pipeline._preserve_specs == ["refusal", "/tmp/protected.safetensors"]
|
|
options = apply.call_args.kwargs
|
|
assert options["subspace"] is subspace and options["reference"] == reference
|
|
assert options["preserve"] == [protected]
|
|
if mode == "steer":
|
|
assert options["scale"] == scale
|
|
else:
|
|
assert "scale" not in options
|
|
repair_options = pipeline._capture_concept_repair.call_args.kwargs["intervention_options"]
|
|
assert repair_options == {key: value for key, value in options.items() if key != "preserve"}
|
|
|
|
|
|
def test_rank_k_requires_saved_basis_at_every_resolved_source_before_capture(monkeypatch):
|
|
from unittest.mock import Mock
|
|
|
|
capture = Mock()
|
|
session = _bare_session(
|
|
monkeypatch, run=None,
|
|
pipeline=SimpleNamespace(handle=SimpleNamespace(num_layers=2),
|
|
_prepare_concept_preservation=capture),
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}, subspaces={0: object()}),
|
|
)
|
|
monkeypatch.setattr("obliteratus.concepts.intervention._selected_layers", lambda *args: [0, 1])
|
|
monkeypatch.setattr("obliteratus.concepts.geometry.intervention_source_layer",
|
|
lambda direction, layer: layer)
|
|
with pytest.raises(ValueError, match="saved subspace geometry for layers"):
|
|
session.apply("project", "all", "all_tokens", 1., "", "hello", subspace=True)
|
|
capture.assert_not_called()
|
|
|
|
|
|
def test_mean_reference_rejects_input_embedding_projection_before_capture(monkeypatch):
|
|
session = _bare_session(monkeypatch, run=None, pipeline=None,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}))
|
|
with pytest.raises(ValueError, match="Input embedding projection supports zero"):
|
|
session.apply("orthogonalize", "all", "all_tokens", 1., "", "hello",
|
|
project_embeddings=True, reference="mean")
|
|
|
|
|
|
@pytest.mark.parametrize("preserve", [None, "", " \n"])
|
|
def test_empty_preserve_restores_pack_defaults_after_previous_attempt(monkeypatch, preserve):
|
|
from unittest.mock import Mock
|
|
|
|
defaults = ["refusal"]
|
|
pipeline = SimpleNamespace(
|
|
_preserve_specs=["previous-attempt.safetensors"],
|
|
_prepare_concept_preservation=Mock(), _preserved_directions=[],
|
|
_capture_concept_evaluation=Mock(), _capture_concept_repair=Mock(),
|
|
_evaluation_battery=SimpleNamespace(generate=Mock(return_value=["preview"]), decoding={}),
|
|
handle=SimpleNamespace(model=object()),
|
|
)
|
|
session = _bare_session(
|
|
monkeypatch, run=None, pipeline=pipeline, _default_preserve_specs=defaults,
|
|
direction=SimpleNamespace(validation={"verdict": "pass"}),
|
|
)
|
|
monkeypatch.setattr(ui_concept_lab, "enforce_gate", lambda report: None)
|
|
monkeypatch.setattr(ui_concept_lab, "apply_concept_intervention", Mock(return_value=object()))
|
|
session.apply("project", "all", "all_tokens", 1., "", "hello", preserve=preserve)
|
|
assert pipeline._preserve_specs is defaults
|
|
pipeline._prepare_concept_preservation.assert_called_once_with()
|
|
|
|
|
|
def test_new_geometry_controls_reach_apply_callback(lab):
|
|
callback, inputs, _, _ = _callback(lab, "Apply")
|
|
assert [component.kwargs["label"] for component in inputs[-4:]] == [
|
|
"Steering scale", "Remove full saved rank-k subspace", "Erasure reference",
|
|
"Preserve directions",
|
|
]
|
|
session = _FakeSession("pack", "tiny/model", 1, 1, 1)
|
|
session.extract()
|
|
session.validate()
|
|
result = callback(session, "why", "project", "all", "all_tokens", 1., "why", "hi",
|
|
False, "", "recipe", True, "mean", "refusal")
|
|
assert result[1] == "Apply complete."
|
|
assert session.geometry_options == ("recipe", True, "mean", "refusal")
|
|
|
|
|
|
def test_callback_exposes_admission_warning_without_changing_gate(lab):
|
|
import logging
|
|
session = _FakeSession("pack", "tiny/model", 1, 1, 1)
|
|
extract = session.extract
|
|
|
|
def warned_extract():
|
|
logging.getLogger("obliteratus.phase_admission").warning("estimated workspace is unknown")
|
|
extract()
|
|
|
|
session.extract = warned_extract
|
|
result = _callback(lab, "Extract")[0](session, "")
|
|
assert result[1].startswith("Extract complete.")
|
|
assert "Memory admission warning: estimated workspace is unknown" in result[1]
|
|
assert result[2] == session.direction.validation
|
|
assert _interactive(result)["Validate"]
|
|
|
|
|
|
@pytest.mark.parametrize(("text", "invalid", "refusal"), [
|
|
("A normal answer to the operator's question.", False, False),
|
|
("I'm sorry, but I can't help with that.", False, True),
|
|
(" I'm sorry" * 24, True, False),
|
|
(" illegally" * 48, True, False),
|
|
("", True, False),
|
|
])
|
|
def test_dose_ladder_invalid_output_for_operator_preview(monkeypatch, text, invalid, refusal):
|
|
from unittest.mock import Mock
|
|
|
|
report = SimpleNamespace(metadata={}, save=Mock())
|
|
battery = SimpleNamespace(
|
|
generate=Mock(return_value=[text]), decoding={}, finish=Mock(return_value=report),
|
|
)
|
|
model = object()
|
|
manager = SimpleNamespace(remove=Mock(), install=Mock())
|
|
run = SimpleNamespace(
|
|
mode="steer", manager=manager, config=object(), layers=[0],
|
|
positions="all_tokens", validation={"verdict": "pass"}, parameters={"scale": "vector"},
|
|
)
|
|
session = _bare_session(
|
|
monkeypatch, run=run, direction=object(), preview_prompt="An arbitrary operator prompt",
|
|
pipeline=SimpleNamespace(
|
|
_preserved_directions=[],
|
|
_evaluation_battery=battery, handle=SimpleNamespace(model=model), output_dir="output",
|
|
),
|
|
)
|
|
doses = {"doses": []}
|
|
|
|
def ladder(model_arg, direction, samples, **kwargs):
|
|
assert model_arg is model and direction is session.direction
|
|
assert kwargs["scale"] == "vector"
|
|
assert samples(model_arg) == [{"invalid_output": invalid, "refusal": refusal}]
|
|
return doses
|
|
|
|
monkeypatch.setattr("obliteratus.concepts.intervention.evaluate_dose_ladder", ladder)
|
|
session.evaluate()
|
|
battery.generate.assert_called_once_with([session.preview_prompt], battery.decoding)
|
|
manager.remove.assert_called_once_with()
|
|
manager.install.assert_called_once_with(
|
|
model, run.config, measure=True, reset_measurements=False,
|
|
)
|
|
assert report.metadata["steering_dose_ladder"] == doses
|
|
report.save.assert_called_once_with("output")
|