Files
OBLITERATUS/tests/test_mutation_search_confirm.py
Joseph MaglyandClaude Opus 5.5 b04906e15c feat(experiments): bounded mutation search with Pareto selection
Add F19: a replayable, budgeted search over qualified operations only,
with an all-attempt ledger, Pareto-first selection and independently
confirmed finalists.

- The space is built from persistent-pilot eligible registrations
  (declared coefficient grids and forms), selected candidates and
  recovered candidates, and complete sequences in their evidenced
  order; anything else is refused. Confirmation and test rows are
  refused for search. The space is write-once and digest-identified.
- Proposals reuse the project's Optuna TPE multi-objective approach
  with per-trial seeding, so replay reproduces the ledger; pruning uses
  the anytime-valid screening rule.
- An append-only, hash-chained ledger records every trial; trial ids are
  content digests, and an interrupted trial is marked incomplete and
  re-run or discarded once. Incomplete trials never win.
- Trials with invalid outputs, failing or unmeasured collateral
  endpoints, or target gains flagged by the weak-scorer check are
  infeasible. The Pareto front is reported before any pre-declared
  aggregate score.
- Finalists must pass a fresh locked confirmation registration created
  after the search and disjoint from development rows; search winner
  and confirmed finalist are reported separately.
- `obliteratus experiments search plan|run|resume|front|confirm`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 13:26:24 -05:00

158 lines
8.7 KiB
Python

"""Bounded mutation search on the planted fixture model, then locked confirmation of finalists.
The search evaluates the eligible all-token steer registration at two coefficients
and its verified persistent candidate on independent development rows with the
model evaluator. Finalists are then checked against fresh confirmation
registrations (amendments on new pack revisions) registered after the search:
one whose rows make the planted effect impossible (control answers ``no``) and
one on ordinary fresh rows.
"""
from __future__ import annotations
import shutil
import pytest
from obliteratus.concepts.pack import ConceptPack
from obliteratus.experiments import registry as reg
from obliteratus.experiments.search_confirm import confirm_finalist
from obliteratus.experiments.search_eval import ModelEvaluator
from obliteratus.experiments.search_run import front, run_search
from obliteratus.experiments.search_space import SPEC_SCHEMA, SearchRefused, plan_search
from tests.test_confirmation_registry import _partition
from tests.test_labeled_activations import build_pack
from tests.test_persistent_candidates import _intervene, build, env, steer, verify # noqa: F401
pytestmark = pytest.mark.cpu
def _shifted(offset, control_answer):
def layout(rows_by_path):
_partition(rows_by_path)
for path, items in rows_by_path.items():
if path.startswith("battery/"):
continue
for row in items:
word, _, number = row["text"].rpartition(" ")
row["text"] = f"{word} {(int(number) + offset) % 400}"
if path.startswith("controls/"):
row["answer"] = control_answer
return layout
def _dataset(root, name, offset, control_answer):
pack = build_pack(root / name, _shifted(offset, control_answer))
return {"pack": str(root / name), "revision": pack.dataset().revision, "role": "select"}
@pytest.fixture(scope="module")
def searched(env, tmp_path_factory): # noqa: F811
root = tmp_path_factory.mktemp("search")
run = shutil.copytree(env["steer"][0], root / "steer")
local = {**env, "steer": (run, env["steer"][1])}
manifest = build(local, "steer")
assert verify(local, "steer", manifest)["outcome"] == "passed"
# Built before any amendment (the build gate needs the unamended registration): a
# verified candidate of another form and an unverified one.
other = build(local, "steer", representation="weight_edit")
assert verify(local, "steer", other)["outcome"] == "passed"
unverified = build(local, "steer", representation="weight_edit")
spec = {"schema": SPEC_SCHEMA, "seed": 1, "allow_fixture_results": True,
"arms": [{"id": "steer", "kind": "registration", "run": str(run),
"registration": env["steer"][1],
"coefficient": {"low": 2.0, "high": 6.0, "step": 4.0}},
{"id": "saved", "kind": "candidate", "run": str(run),
"candidate": manifest["id"]}],
"development": {"dataset": env["fresh"]},
"budget": {"max_trials": 3, "max_artifacts": 2, "max_bytes": 100_000}}
plan_search(spec, root / "out", pack_resolver=ConceptPack.load)
space = plan_search(spec, root / "out", pack_resolver=ConceptPack.load) # idempotent
evaluator = ModelEvaluator(space, env["source"], model_io=env["io"],
pack_resolver=ConceptPack.load)
state = run_search(root / "out", evaluator)
return {"root": root, "run": run, "env": local, "candidate": manifest["id"],
"other": other["id"], "unverified": unverified["id"], "out": root / "out", "state": state, "space": space}
def test_model_search_scores_qualified_points_on_development_rows(searched):
state = searched["state"]
assert state["trials"] == 3 and state["status_counts"] == {"completed": 3}
assert state["stopped"] == "budget: max_trials 3 reached"
by_point = {}
from obliteratus.experiments import search_ledger as sl
for record in sl.trials(sl.Ledger(searched["out"]).entries()).values():
by_point[(record["point"]["arm"], record["point"]["coefficient"])] = record["result"]
strong, saved = by_point[("steer", 6.0)], by_point[("saved", 6.0)]
assert strong["metrics"]["objectives"]["effect"] >= 0.5
assert strong["metrics"]["feasible"] and saved["metrics"]["feasible"]
# The saved candidate reproduces the temporary operation at its coefficient.
assert saved["metrics"]["objectives"] == strong["metrics"]["objectives"]
assert by_point[("steer", 2.0)]["metrics"]["objectives"]["effect"] < \
strong["metrics"]["objectives"]["effect"]
# The first registration-arm trial saved its adapter; the budget then allowed no more.
saved_files = [len(r["artifacts"]) for (arm, _), r in by_point.items() if arm == "steer"]
assert sorted(saved_files) == [0, 2] and state["usage"]["artifacts"] == 2
assert saved["artifacts"] == []
assert {e["point"]["arm"] for e in state["pareto_front"]} == {"steer", "saved"}
def test_finalists_need_a_bound_candidate_and_a_fresh_locked_registration(searched):
local, run, out = searched["env"], searched["run"], searched["out"]
state = front(out)
trial = next(e["trial"] for e in state["pareto_front"]
if e["point"] == {"arm": "steer", "coefficient": 6.0,
"form": "activation_adapter", "recovery": None})
original = local["steer"][1]
with pytest.raises(SearchRefused, match="needs a built persistent candidate"):
confirm_finalist(out, trial, original)
with pytest.raises(SearchRefused, match="existed when the space was built"):
confirm_finalist(out, trial, original, candidate=searched["candidate"],
allow_fixture_results=True)
root = searched["root"]
impossible = _dataset(root, "impossible", 160, "no")
fresh = _dataset(root, "fresh-confirm", 240, "yes")
spec = steer(local)
spec["dataset"] = impossible
first = reg.register(run, spec, amends=original, pack_resolver=ConceptPack.load)
pending = confirm_finalist(out, trial, first["id"], candidate=searched["candidate"],
allow_fixture_results=True)
assert pending["status"] == "pending" and not pending["confirmed"]
result = _intervene(local, run, spec)
assert reg.unlock(run, first["id"], result["location"]["result"])["outcome"] != "supported"
failed = confirm_finalist(out, trial, first["id"], candidate=searched["candidate"],
allow_fixture_results=True)
assert failed["status"] == "not confirmed"
assert failed["outcome"] in ("not_supported", "inconclusive")
assert front(out)["confirmations"]["not_confirmed"] == [trial]
spec = steer(local)
spec["dataset"] = fresh
second = reg.register(run, spec, amends=first["id"], pack_resolver=ConceptPack.load)
result = _intervene(local, run, spec)
assert reg.unlock(run, second["id"], result["location"]["result"])["outcome"] == "supported"
# Fixture-model confirmations count only with the test-only allowance.
plain = confirm_finalist(out, trial, second["id"], candidate=searched["candidate"])
assert plain["status"] == "not confirmed" and "software-contract" in plain["reasons"][-1]
confirmed = confirm_finalist(out, trial, second["id"], candidate=searched["candidate"],
allow_fixture_results=True)
assert confirmed["status"] == "confirmed" and confirmed["outcome"] == "supported"
report = front(out)
assert report["confirmations"]["confirmed_finalists"] == [trial]
assert report["search_winner"] in {e["trial"] for e in report["pareto_front"]}
weak = next(e["trial"] for e in report["pareto_front"] if e["point"]["arm"] == "saved")
with pytest.raises(SearchRefused, match="registration .* is not in the finalist's runs"):
confirm_finalist(out, weak, "sha256:" + "3" * 64, allow_fixture_results=True)
[low] = [t for t, r in _trials(out).items() if r["point"]["coefficient"] == 2.0]
assert low not in {e["trial"] for e in report["pareto_front"]}
with pytest.raises(SearchRefused, match="not on the development Pareto front"):
confirm_finalist(out, low, second["id"], candidate=searched["candidate"])
# The candidate must be selected and bound to the trial's point (here: its form).
with pytest.raises(SearchRefused, match="is not selected; verify it"):
confirm_finalist(out, trial, second["id"], candidate=searched["unverified"])
with pytest.raises(SearchRefused, match="not bound to the trial"):
confirm_finalist(out, trial, second["id"], candidate=searched["other"])
def _trials(out):
from obliteratus.experiments import search_ledger as sl
return sl.trials(sl.Ledger(out).entries())