mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 02:24:12 +01:00
402 lines
18 KiB
Python
402 lines
18 KiB
Python
"""Ask the Lab drives real offline interventions; planner text is untrusted data."""
|
||
|
||
from copy import deepcopy
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
import threading
|
||
|
||
import pytest
|
||
import torch
|
||
|
||
from obliteratus import ask_lab as ask
|
||
from obliteratus import ui_neuron_lab as lab
|
||
from obliteratus.interventions import NeuronSession
|
||
from tests.test_neuron_lab import local_model, bounded_threads, hooks # noqa: F401, F811
|
||
|
||
|
||
pytestmark = [pytest.mark.cpu, pytest.mark.integration]
|
||
PROMPTS = dict(contrast_a="hello world", contrast_b="hello safe", probe="hello test", control_probe="answer safe")
|
||
GOAL = "Discover a difference without claiming a semantic trait."
|
||
|
||
|
||
@pytest.fixture
|
||
def session(local_model): # noqa: F811
|
||
instance = lab.NeuronLabSession()
|
||
instance.load(str(local_model))
|
||
yield instance
|
||
instance.close()
|
||
|
||
|
||
def prepare(session):
|
||
ask.act(session, "save", text=GOAL, prompts=PROMPTS)
|
||
ask.act(session, "message", text="scan")
|
||
assert session.ask_lab["stage"] == "scanned"
|
||
|
||
|
||
def test_real_scan_inspection_controlled_study_and_reset_preserve_weights(session):
|
||
original = {key: value.clone() for key, value in session.handle.model.state_dict().items()}
|
||
before_hooks = hooks(session.handle.model)
|
||
prepare(session)
|
||
ask.act(session, "message", text="inspect candidate 1")
|
||
candidate = session.ask_lab["candidate"]
|
||
assert session.inspection["layer"] == candidate["layer"]
|
||
assert session.inspection["neuron"] == candidate["neuron"]
|
||
assert session.inspection["tokens"]
|
||
ask.act(session, "message", text="test candidate 1", tokens=3)
|
||
state = session.ask_lab
|
||
receipt = state["evidence"]
|
||
assert state["stage"] == "tested"
|
||
assert session.recipe.edits == ()
|
||
expected = {"baseline", "candidate", "opposite", "random", "zero", "reset"}
|
||
assert set(receipt["conditions"]) == expected
|
||
assert set(receipt["utility"]["conditions"]) == expected
|
||
for group in [receipt, receipt["utility"]]:
|
||
assert group["verification"]["status"] == "passed"
|
||
assert group["verification"]["first_logits_exact"]
|
||
assert group["verification"]["token_ids_exact"]
|
||
assert group["verification"]["hook_registry_restored"]
|
||
trials = group["conditions"]
|
||
assert trials["baseline"]["token_ids"] == trials["zero"]["token_ids"] == trials["reset"]["token_ids"]
|
||
assert all(trial["tokens"] <= 3 for trial in trials.values())
|
||
assert all(trial["stop_reason"] in {"eos", "token_limit"} for trial in trials.values())
|
||
edits = {name: receipt["conditions"][name]["recipe"]["edits"][0] for name in ["candidate", "opposite", "random", "zero"]}
|
||
assert edits["opposite"]["value"] == -edits["candidate"]["value"]
|
||
assert edits["zero"]["value"] == 0
|
||
assert edits["random"]["neuron"] != edits["candidate"]["neuron"]
|
||
down = NeuronSession(session.handle, session.recipe).sites[candidate["layer"]][1]
|
||
expected_size = receipt["calibration"]["target_write_norm"]
|
||
for name in ["candidate", "opposite", "random"]:
|
||
edit = edits[name]
|
||
actual_size = abs(edit["value"]) * float(down.weight[:, edit["neuron"]].detach().norm())
|
||
assert actual_size == pytest.approx(expected_size, rel=1e-6)
|
||
assert session.comparison["run_id"] == receipt["run_id"]
|
||
assert session.comparison["prompt"] == PROMPTS["probe"]
|
||
assert session.comparison["decoding"]["rendered_prompt_sha256"] == hashlib.sha256(PROMPTS["probe"].encode()).hexdigest()
|
||
ask.act(session, "message", text="verify reset")
|
||
assert session.comparison["verification"]["status"] == "passed"
|
||
assert hooks(session.handle.model) == before_hooks
|
||
assert all(torch.equal(original[key], value) for key, value in session.handle.model.state_dict().items())
|
||
|
||
|
||
def test_local_planner_is_unedited_and_does_not_execute_generated_prompts(session, monkeypatch):
|
||
session.edit("add", 1, 0, 1, "add", 10.)
|
||
saved = session.recipe
|
||
calls = []
|
||
def planner(inputs, recipe, limit):
|
||
calls.append((inputs, recipe, limit))
|
||
return dict(text=json.dumps(PROMPTS), stop_reason="eos", tokens=30), torch.zeros(11)
|
||
monkeypatch.setattr(session, "_generate", planner)
|
||
ask.act(session, "message", text="Find a neuron related to careful answers")
|
||
assert len(calls) == 1 and calls[0][2] == 128
|
||
assert calls[0][1].edits == ()
|
||
assert session.recipe == saved
|
||
assert session.scout is None and session.inspection is None
|
||
assert session.ask_lab["plan"]["source"] == "model"
|
||
assert session.ask_lab["plan"]["planner"]["model"] == session.handle.model_name
|
||
assert session.ask_lab["stage"] == "draft"
|
||
assert session.ask_lab["candidates"] == []
|
||
|
||
|
||
@pytest.mark.parametrize("text,stop", [
|
||
(json.dumps(PROMPTS), "token_limit"),
|
||
("not JSON", "eos"),
|
||
(json.dumps(dict(PROMPTS, action="test")), "eos"),
|
||
(json.dumps(dict(PROMPTS, probe=PROMPTS["contrast_a"])), "eos"),
|
||
])
|
||
def test_bad_planner_outputs_do_not_become_experiments(session, monkeypatch, text, stop):
|
||
monkeypatch.setattr(session, "_generate", lambda *args: (dict(text=text, stop_reason=stop, tokens=12), None))
|
||
with pytest.raises(ValueError):
|
||
ask.act(session, "draft", text=GOAL)
|
||
assert session.ask_lab["stage"] == "failed"
|
||
assert session.ask_lab["evidence"] is None
|
||
assert session.scout is None and session.comparison is None
|
||
|
||
|
||
def test_unknown_text_is_a_draft_not_an_embedded_tool_call(session, monkeypatch):
|
||
calls = []
|
||
monkeypatch.setattr(session, "_generate", lambda *args: (calls.append(args) or dict(text=json.dumps(PROMPTS), stop_reason="eos", tokens=20), None))
|
||
monkeypatch.setattr(session, "scan", lambda *args: pytest.fail("Quoted commands must not execute"))
|
||
monkeypatch.setattr(session, "verify_reset", lambda: pytest.fail("Negated reset must not execute"))
|
||
ask.act(session, "message", text="Do not reset; explain what ‘scan’ would do")
|
||
assert len(calls) == 1
|
||
assert session.ask_lab["stage"] == "draft"
|
||
|
||
|
||
@pytest.mark.parametrize("goal,prompts", [
|
||
("", PROMPTS), ("x" * 801, PROMPTS), (GOAL, []),
|
||
(GOAL, dict(PROMPTS, extra="value")),
|
||
(GOAL, dict(PROMPTS, probe=5)),
|
||
(GOAL, dict(PROMPTS, probe="x" * 401)),
|
||
(GOAL, dict(PROMPTS, probe=" HELLO WORLD ")),
|
||
])
|
||
def test_plan_boundary_checks_reject_ambiguous_or_unbounded_inputs(goal, prompts):
|
||
with pytest.raises(ValueError):
|
||
ask.validate_plan(goal, prompts)
|
||
|
||
|
||
def test_new_plan_discards_captures_and_evidence_and_preserves_manual_recipe(session):
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=2)
|
||
session.edit("add", 1, 0, 1, "add", 2.)
|
||
manual = session.recipe
|
||
ask.act(session, "save", text="New question", prompts=dict(PROMPTS, probe="safe world"))
|
||
assert session.recipe == manual
|
||
assert session.comparison is session.inspection is session.scout is None
|
||
assert session._comparison_reference is None
|
||
assert session.ask_lab["candidate"] is session.ask_lab["evidence"] is None
|
||
assert session.ask_lab["stage"] == "draft"
|
||
|
||
|
||
def test_manual_stack_is_not_silently_destroyed_by_study(session):
|
||
prepare(session)
|
||
session.edit("add", 1, 0, 1, "add", 3.)
|
||
manual = session.recipe
|
||
# A user may have changed manual settings after the drafted study.
|
||
ask.act(session, "save", text=GOAL, prompts=PROMPTS)
|
||
ask.act(session, "scan")
|
||
with pytest.raises(ValueError, match="Clear manual edits"):
|
||
ask.act(session, "test", tokens=2)
|
||
assert session.recipe == manual
|
||
assert session.comparison is None
|
||
assert session.ask_lab["stage"] == "failed"
|
||
|
||
|
||
@pytest.mark.parametrize("dose,tokens", [(True, 2), (float("nan"), 2), (0, 2), (.2, 2), (.02, 0), (.02, 33), (.02, True)])
|
||
def test_test_budgets_are_validated_before_any_generation(session, monkeypatch, dose, tokens):
|
||
prepare(session)
|
||
monkeypatch.setattr(session, "_generate", lambda *args: pytest.fail("Invalid bound must not run"))
|
||
with pytest.raises(ValueError):
|
||
ask.act(session, "test", dose=dose, tokens=tokens)
|
||
assert session.ask_lab["evidence"] is None
|
||
|
||
|
||
def test_failure_keeps_partial_evidence_without_success_receipt_or_hooks(session, monkeypatch):
|
||
prepare(session)
|
||
before = hooks(session.handle.model)
|
||
generate = session._generate
|
||
calls = []
|
||
def fail_third(*args):
|
||
calls.append(args)
|
||
if len(calls) == 3:
|
||
raise RuntimeError("Injected failure")
|
||
return generate(*args)
|
||
monkeypatch.setattr(session, "_generate", fail_third)
|
||
with pytest.raises(RuntimeError, match="Injected failure"):
|
||
ask.act(session, "test", tokens=2)
|
||
s = session.ask_lab
|
||
assert s["stage"] == "failed"
|
||
assert s["evidence"]["verification"]["status"] == "failed"
|
||
assert set(s["evidence"]["conditions"]) == {"baseline", "candidate"}
|
||
assert session.comparison is None
|
||
assert hooks(session.handle.model) == before
|
||
with pytest.raises(ValueError, match="Complete a study"):
|
||
ask.export_report(session)
|
||
|
||
|
||
def test_nested_operation_does_not_clear_a_pending_cancellation(session):
|
||
before = hooks(session.handle.model)
|
||
with session._operation():
|
||
session.cancel()
|
||
with pytest.raises(RuntimeError, match="cancelled"):
|
||
session.inspect("hello world", 0, 1)
|
||
assert hooks(session.handle.model) == before
|
||
# A genuinely new action may clear the cancellation.
|
||
session.inspect("hello world", 0, 1)
|
||
assert session.inspection is not None
|
||
|
||
|
||
def test_cancelling_a_real_study_preserves_completed_trials_and_removes_hooks(session, monkeypatch):
|
||
prepare(session)
|
||
before = hooks(session.handle.model)
|
||
generate = session._generate
|
||
completed = []
|
||
def cancel_after_second(*args):
|
||
result = generate(*args)
|
||
completed.append(result)
|
||
if len(completed) == 2:
|
||
session.cancel()
|
||
return result
|
||
monkeypatch.setattr(session, "_generate", cancel_after_second)
|
||
with pytest.raises(RuntimeError, match="cancelled"):
|
||
ask.act(session, "test", tokens=2)
|
||
assert len(completed) == 2
|
||
assert set(session.ask_lab["evidence"]["conditions"]) == {"baseline", "candidate"}
|
||
assert session.ask_lab["evidence"]["verification"]["status"] == "failed"
|
||
assert session.ask_lab["stage"] == "failed"
|
||
assert session.recipe.edits == () and session.comparison is None
|
||
assert hooks(session.handle.model) == before
|
||
|
||
|
||
def test_reset_mismatch_replaces_prior_success_in_evidence_and_summary(session, monkeypatch):
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=2)
|
||
assert session.ask_lab["evidence"]["verification"]["status"] == "passed"
|
||
generate = session._generate
|
||
def changed_logits(*args):
|
||
trial, logits = generate(*args)
|
||
return trial, logits + 1
|
||
monkeypatch.setattr(session, "_generate", changed_logits)
|
||
ask.act(session, "reset")
|
||
evidence = session.ask_lab["evidence"]
|
||
assert evidence["verification"]["status"] == "mismatch"
|
||
assert evidence["verification"]["reset"] == session.comparison["verification"]
|
||
assert evidence["conditions"]["reset"] == session.comparison["trials"]["reset"]
|
||
assert "did not all match baseline" in ask.summarize(session.ask_lab)
|
||
report = json.loads(Path(ask.export_report(session)).read_text())
|
||
assert report["verification"]["status"] == "mismatch"
|
||
|
||
|
||
def test_concurrent_action_is_rejected_without_mutating_the_study(session):
|
||
prepare(session)
|
||
errors = []
|
||
with session._operation():
|
||
def request():
|
||
try:
|
||
ask.act(session, "inspect")
|
||
except RuntimeError as error:
|
||
errors.append(str(error))
|
||
thread = threading.Thread(target=request)
|
||
thread.start()
|
||
thread.join(timeout=2)
|
||
assert not thread.is_alive()
|
||
assert errors and "busy" in errors[0]
|
||
assert session.ask_lab["stage"] == "scanned"
|
||
|
||
|
||
def test_exports_omit_source_text_and_tokens_by_default_and_are_session_owned(session):
|
||
session.set_system_prompt("hello private system")
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=2)
|
||
private = json.loads(Path(ask.export_report(session)).read_text())
|
||
full_path = Path(ask.export_report(session, include_text=True))
|
||
full = json.loads(full_path.read_text())
|
||
assert full["goal"] == GOAL and full["probe"] == PROMPTS["probe"]
|
||
assert full["system_prompt"] == "hello private system"
|
||
assert private["system_prompt_sha256"] == hashlib.sha256(b"hello private system").hexdigest()
|
||
assert full["candidate"]["layer"] == session.ask_lab["candidate"]["layer"]
|
||
assert full["discovery"]["layers"] == session.ask_lab["discovery"]["layers"]
|
||
assert full["discovery"]["tokenization_a"]["rendered_prompt_sha256"]
|
||
assert full["schema"] == "obliteratus.ask-lab.v1" and full["versions"]["torch"]
|
||
assert private["goal_sha256"] == hashlib.sha256(GOAL.encode()).hexdigest()
|
||
def verify(value):
|
||
if isinstance(value, dict):
|
||
assert not {"token_ids", "messages"}.intersection(value)
|
||
for key in {"text", "goal", "probe", "control_probe", "contrast_a", "contrast_b", "system_prompt", "prompt_a", "prompt_b"}:
|
||
assert not isinstance(value.get(key), str)
|
||
for child in value.values(): verify(child)
|
||
elif isinstance(value, list):
|
||
for child in value: verify(child)
|
||
verify(private)
|
||
assert not any(raw in json.dumps(private) for raw in [GOAL, *PROMPTS.values()])
|
||
assert full_path.parent == session.directory
|
||
assert private["verification"]["status"] == "passed"
|
||
session.close()
|
||
assert not full_path.exists()
|
||
|
||
|
||
def test_new_candidate_clears_shared_receipt_and_stale_download(session):
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=2)
|
||
session.ask_lab["export_file"] = ask.export_report(session)
|
||
ask.act(session, "inspect", rank=2)
|
||
assert session.comparison is session._comparison_reference is None
|
||
assert session.ask_lab["evidence"] is session.ask_lab["export_file"] is None
|
||
with pytest.raises(ValueError, match="no baseline"):
|
||
ask.act(session, "message", text="reset")
|
||
ask.act(session, "scan")
|
||
assert session.inspection is session.comparison is None
|
||
|
||
|
||
def test_later_reset_cannot_erase_failed_zero_dose_check(session, monkeypatch):
|
||
prepare(session)
|
||
original = session._generate
|
||
def mismatched_zero(inputs, recipe, limit):
|
||
trial, logits = original(inputs, recipe, limit)
|
||
if recipe.edits and recipe.edits[0].value == 0:
|
||
logits = logits + .01
|
||
return trial, logits
|
||
monkeypatch.setattr(session, "_generate", mismatched_zero)
|
||
ask.act(session, "test", tokens=2)
|
||
assert session.ask_lab["evidence"]["verification"]["status"] == "mismatch"
|
||
ask.act(session, "reset")
|
||
verification = session.ask_lab["evidence"]["verification"]
|
||
assert verification["status"] == "mismatch"
|
||
assert not verification["zero"]["first_logits_exact"]
|
||
assert verification["reset"]["first_logits_exact"]
|
||
assert "did not all match" in ask.summarize(session.ask_lab)
|
||
|
||
|
||
def test_ask_reset_refuses_another_panels_baseline_without_generating(session, monkeypatch):
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=2)
|
||
previous = deepcopy(session.ask_lab)
|
||
session.compare("different safe prompt", 2)
|
||
assert session.ask_lab is None
|
||
other_run = session.comparison["run_id"]
|
||
def no_generation(*args):
|
||
pytest.fail("Ask reset must not replay another panel's baseline")
|
||
monkeypatch.setattr(session, "_generate", no_generation)
|
||
with pytest.raises(ValueError, match="owned by this study"):
|
||
ask.act(session, "reset")
|
||
# Also fail closed if stale state is restored by an external caller.
|
||
session.ask_lab = previous
|
||
with pytest.raises(ValueError, match="owned by this study"):
|
||
ask.act(session, "reset")
|
||
assert session.comparison["run_id"] == other_run
|
||
assert session.comparison["verification"]["status"] == "not_run"
|
||
|
||
|
||
def test_empty_sessions_can_plan_and_read_status_without_loading_models():
|
||
one, two = lab.NeuronLabSession(), lab.NeuronLabSession()
|
||
try:
|
||
ask.act(one, "example")
|
||
assert one.handle is None and one.ask_lab["plan"]["source"] == "edited"
|
||
ask.act(two, "message", text="status")
|
||
assert two.ask_lab["plan"] is None
|
||
assert one.ask_lab is not two.ask_lab
|
||
with pytest.raises(ValueError, match="Connect a local"):
|
||
ask.act(two, "message", text="find careful answers")
|
||
for _ in range(20):
|
||
ask.act(one, "message", text="status")
|
||
assert len(one.ask_lab["messages"]) == 12
|
||
finally:
|
||
one.close()
|
||
two.close()
|
||
|
||
|
||
def test_verification_reports_mismatch_instead_of_inventing_reset_success():
|
||
trials = {k: dict(token_ids=[1]) for k in ["baseline", "zero", "reset"]}
|
||
logits = {k: torch.zeros(3) for k in trials}
|
||
logits["zero"][0] = 1
|
||
result = ask._verification("baseline", trials, logits, True)
|
||
assert result["status"] == "mismatch"
|
||
assert result["token_ids_exact"] and not result["first_logits_exact"]
|
||
assert result["weight_checksum_checked"] is False
|
||
trials["reset"]["token_ids"] = [2]
|
||
result = ask._verification("baseline", trials, logits, False)
|
||
assert not result["token_ids_exact"] and not result["hook_registry_restored"]
|
||
|
||
|
||
def test_invalid_ranks_and_unknown_actions_do_not_use_candidates(session):
|
||
prepare(session)
|
||
with pytest.raises(ValueError, match="choose a candidate"):
|
||
ask.act(session, "inspect", rank=True)
|
||
with pytest.raises(ValueError, match="Unknown Ask"):
|
||
ask.act(session, "execute arbitrary tool")
|
||
assert session.inspection is None
|
||
|
||
|
||
def test_summaries_distinguish_partial_and_truncated_evidence(session):
|
||
assert "No completed test" in ask.summarize(ask.fresh_state())
|
||
prepare(session)
|
||
ask.act(session, "test", tokens=1)
|
||
evidence = session.ask_lab["evidence"]
|
||
count = sum(t["stop_reason"] != "eos" for group in [evidence["conditions"],evidence["utility"]["conditions"]] for t in group.values())
|
||
summary = ask.summarize(session.ask_lab)
|
||
assert f"{count}/12 responses hit the token limit" in summary
|
||
assert "do not prove" in summary
|
||
altered = deepcopy(session.ask_lab)
|
||
altered["evidence"]["utility"]["verification"]["status"] = "mismatch"
|
||
assert "did not all match" in ask.summarize(altered)
|