Files
OBLITERATUS/tests/test_mutation_search.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

388 lines
22 KiB
Python

"""Bounded mutation search: qualified space, ledger, resume, guards, Pareto selection, budgets.
The qualified operations come from the persistent-candidate fixtures
(``tests/test_persistent_candidates.py``): the planted model's all-token steer
key reaches ``persistent-pilot eligible`` under the test-only fixture allowance,
a steer key with only a supported confirmation stays ``causal``. Trials are
scored by a deterministic stand-in evaluator over synthetic development rows so
that every ledger, budget and selection rule can be checked exactly; the model
path is covered by ``tests/test_mutation_search_confirm.py``.
"""
from __future__ import annotations
import json
import shutil
import pytest
from obliteratus.analysis import search_pareto as pareto
from obliteratus.experiments import search_ledger as sl
from obliteratus.experiments.registry_store import Registry
from obliteratus.experiments.search_confirm import confirm_finalist
from obliteratus.experiments.search_eval import OUTCOMES_SCHEMA, ReplayEvaluator
from obliteratus.experiments.search_run import front, propose, run_search
from obliteratus.experiments.search_space import (
SPEC_SCHEMA, SearchRefused, build_space, plan_search, points, read_space,
)
from tests.test_persistent_candidates import build, env, verify # noqa: F401
pytestmark = pytest.mark.cpu
N = 30
def rows(prefix="search row", role="select"):
flip = [{"id": f"flip:{i}", "task": "flip", "text": f"{prefix} flip {i}", "role": role}
for i in range(N)]
keep = [{"id": f"keep:{i}", "task": "keep", "text": f"{prefix} keep {i}", "role": role}
for i in range(N)]
return flip + keep
@pytest.fixture(scope="module")
def selected(env): # noqa: F811
manifest = build(env, "steer")
assert verify(env, "steer", manifest)["outcome"] == "passed"
return manifest["id"]
def make_spec(env, candidate, **changes): # noqa: F811
run, registration = env["steer"]
spec = {"schema": SPEC_SCHEMA, "seed": 3, "allow_fixture_results": True,
"arms": [{"id": "steer", "kind": "registration", "run": str(run),
"registration": registration,
"coefficient": {"low": 2.0, "high": 6.0, "step": 1.0},
"forms": ["activation_adapter", "weight_edit"]},
{"id": "saved", "kind": "candidate", "run": str(run),
"candidate": candidate}],
"development": {"rows": rows()},
"weak_scorer": {"sample": 12},
"budget": {"max_trials": 11},
"sampler": {"startup_trials": 3}}
for key, value in changes.items():
spec[key] = {**spec[key], **value} if isinstance(spec.get(key), dict) \
and isinstance(value, dict) else value
return spec
def outcome(point, row_ids, stage):
"""Effect grows with the coefficient; three planted exploits and one collateral failure."""
c, form = point["coefficient"], point["form"]
target_ids = [r for r in row_ids if r.startswith("flip:")]
hits = round(len(target_ids) * min(c / 6.0, 1.0) * 0.8)
degenerate = form == "weight_edit" and c in (5.0, 6.0)
target = []
for position, row in enumerate(target_ids):
success = True if degenerate else position < hits
output = "" if degenerate else ("yes" if success else "no")
target.append({"id": row, "success": success, "pristine_success": False,
"invalid": degenerate and c == 6.0, "pristine_invalid": False,
"output": output})
keep_ids = [r for r in row_ids if r.startswith("keep:")]
broken = form == "weight_edit" and c == 4.0
collateral = {"keep": [[True, not (broken and i % 2)] for i, _ in enumerate(keep_ids)]}
return {"target": target, "collateral": {} if stage == "screen" else collateral,
"cost": 100.0 if form == "weight_edit" else 10.0, "compute": float(len(row_ids))}
class Stand:
def __init__(self, crash_at=None, write=0):
self.calls, self.crash_at, self.write = 0, crash_at, write
def evaluate(self, point, row_ids, *, stage, directory, budget):
self.calls += 1
if self.calls == self.crash_at:
(directory / "partial.bin").write_bytes(b"partial")
raise KeyboardInterrupt
for index in range(self.write):
(directory / f"artifact-{index}.bin").write_bytes(b"x" * 64)
return outcome(point, row_ids, stage)
def plan(tmp_path, env, candidate, name="search", **changes): # noqa: F811
return plan_search(make_spec(env, candidate, **changes), tmp_path / name)
def test_unqualified_operations_are_refused_at_space_construction(env, selected, tmp_path): # noqa: F811
causal_run, causal = env["causal"]
spec = make_spec(env, selected)
cases = [
({"id": "x", "kind": "registration", "run": str(causal_run), "registration": causal},
"'causal'.*persistent-pilot eligible"),
({"id": "x", "kind": "weights", "run": str(causal_run)}, "unsupported kind"),
({**spec["arms"][0], "coefficient": {"low": -1.0, "high": 2.0, "step": 1.0}},
"nonzero with the sign"),
({**spec["arms"][0], "coefficient": {"low": 2.0, "high": 1.0, "step": 1.0}},
"low <= high"),
({**spec["arms"][0], "forms": ["full_finetune"]}, "forms must be"),
({**spec["arms"][1], "coefficient": {"low": 1.0, "high": 2.0, "step": 1.0}},
"fixed by its saved artifact"),
({**spec["arms"][1], "recovery": ["sha256:" + "0" * 64]}, "No readable recovered"),
({"id": "x", "kind": "sequence", "store": str(tmp_path), "sequence": "sha256:" + "1" * 64},
"No readable sequence"),
]
for arm, message in cases:
with pytest.raises(SearchRefused, match=message):
build_space({**spec, "arms": [arm]})
# Without the test-only allowance fixture results never reach the required maturity.
with pytest.raises(SearchRefused, match="unqualified operation.*'candidate'"):
build_space({**spec, "allow_fixture_results": False})
# A built but unverified candidate is not qualified.
unverified = build(env, "steer", representation="weight_edit")
with pytest.raises(SearchRefused, match="is built; only a verified"):
build_space({**spec, "arms": [{**spec["arms"][1], "candidate": unverified["id"]}]})
# Arms must compare one target effect.
ablate_run, ablate = env["ablate"]
with pytest.raises(SearchRefused, match="disagree on the target task"):
build_space({**spec, "arms": [spec["arms"][0], {
"id": "abl", "kind": "registration", "run": str(ablate_run),
"registration": ablate}]})
with pytest.raises(SearchRefused, match="unknown search spec keys"):
build_space({**spec, "budget": {"max_trails": 3}})
space = build_space(spec)
assert len(points(space)) == 11 and space["frozen"]["scope"] == "software-contract"
assert {a["maturity"] for a in space["frozen"]["arms"]} == {"persistent-pilot eligible"}
def test_confirmation_and_locked_rows_are_refused_for_search(env, selected): # noqa: F811
spec = make_spec(env, selected)
with pytest.raises(SearchRefused, match="locked role 'test'"):
build_space({**spec, "development": {"rows": rows(role="test")}})
with pytest.raises(SearchRefused, match="locked role 'confirmation'"):
build_space({**spec, "development": {"rows": rows(role="confirmation")}})
run, registration = env["steer"]
locked = Registry(run).get(registration)["frozen"]["confirmation"]["row_digests"]
smuggled = rows() + [{"id": "flip:locked", "task": "flip", "sha256": locked[0],
"role": "select"}]
with pytest.raises(SearchRefused, match="1 development rows belong to registered "
"confirmation sets"):
build_space({**spec, "development": {"rows": smuggled}})
pack = env["pack"]
# The registered confirmation set is the pack's calibrate role: every one of its rows is
# refused; the locked test role is refused outright.
calibrate = {"pack": str(pack.location), "revision": pack.dataset().revision,
"role": "calibrate"}
with pytest.raises(SearchRefused, match="belong to registered confirmation sets"):
build_space({**spec, "development": {"dataset": calibrate}})
with pytest.raises(SearchRefused, match="development role 'test' refused"):
build_space({**spec, "development": {"dataset": {**calibrate, "role": "test"}}})
space = build_space({**spec, "development": {"dataset": env["fresh"]}})
assert space["frozen"]["development"]["mode"] == "dataset"
def test_crash_resume_neither_duplicates_nor_loses_trials(env, selected, tmp_path): # noqa: F811
budget = {"max_trials": 6}
reference = plan(tmp_path, env, selected, "reference", budget=budget)
expected = run_search(tmp_path / "reference", Stand())
crashed = plan(tmp_path, env, selected, "crashed", budget=budget)
assert crashed["id"] == reference["id"]
with pytest.raises(KeyboardInterrupt):
run_search(tmp_path / "crashed", Stand(crash_at=4))
table = sl.trials(sl.Ledger(tmp_path / "crashed").entries())
running = [t for t, r in table.items() if r["status"] == "running"]
assert len(table) == 4 and len(running) == 1
# An in-flight trial can never be selected or confirmed.
state = front(tmp_path / "crashed")
assert running[0] not in {e["trial"] for e in state["pareto_front"]}
assert state["excluded"]["running"][0]["trial"] == running[0]
with pytest.raises(SearchRefused, match="is running; only a completed, feasible"):
confirm_finalist(tmp_path / "crashed", running[0], "sha256:" + "2" * 64)
with pytest.raises(SearchRefused, match="already started; resume"):
run_search(tmp_path / "crashed", Stand())
resumed = run_search(tmp_path / "crashed", Stand(), resume=True)
final = sl.trials(sl.Ledger(tmp_path / "crashed").entries())
assert len(final) == resumed["trials"] == expected["trials"] == 6
assert list(final) == list(sl.trials(sl.Ledger(tmp_path / "reference").entries()))
assert final[running[0]]["attempts"] == 2 and final[running[0]]["incomplete_attempts"] == 1
assert final[running[0]]["status"] in sl.FINISHED
assert resumed["attempts"] == 7 and resumed["ledger_digest"] == expected["ledger_digest"]
# The interrupted attempt's partial files were discarded before its re-run.
assert not [p for p in (tmp_path / "crashed" / "trials").rglob("*") if p.is_file()]
events = [e["event"] for e in sl.Ledger(tmp_path / "crashed").entries()]
assert events.count("incomplete") == 1 and events.count("start") == 7
# Resuming a finished search adds nothing.
again = run_search(tmp_path / "crashed", Stand(), resume=True)
assert again["trials"] == 6 and again["ledger_digest"] == expected["ledger_digest"]
def test_discarded_incomplete_trial_is_counted_once_and_never_wins(env, selected, tmp_path): # noqa: F811
plan(tmp_path, env, selected, budget={"max_trials": 5})
with pytest.raises(KeyboardInterrupt):
run_search(tmp_path / "search", Stand(crash_at=2))
with pytest.raises(SearchRefused, match="incomplete policy"):
run_search(tmp_path / "search", Stand(), resume=True, incomplete="ignore")
state = run_search(tmp_path / "search", Stand(), resume=True, incomplete="discard")
table = sl.trials(sl.Ledger(tmp_path / "search").entries())
ids = [r["trial"] for r in table.values()]
assert len(ids) == len(set(ids)) == 5 and [r["index"] for r in table.values()] == list(range(5))
discarded = [r for r in table.values() if r["status"] == "discarded"]
assert len(discarded) == 1 and discarded[0]["attempts"] == 1
assert state["status_counts"]["discarded"] == 1
assert discarded[0]["trial"] not in state["finalists"]
with pytest.raises(SearchRefused, match="is discarded"):
confirm_finalist(tmp_path / "search", discarded[0]["trial"], "sha256:" + "2" * 64)
def test_degenerate_outputs_are_infeasible_and_flagged(env, selected, tmp_path): # noqa: F811
plan(tmp_path, env, selected)
state = run_search(tmp_path / "search", Stand())
assert state["trials"] == 11 and state["status_counts"] == {"completed": 11}
table = sl.trials(sl.Ledger(tmp_path / "search").entries())
by_point = {(r["point"]["coefficient"], r["point"]["form"]): r for r in table.values()}
invalid = by_point[(6.0, "weight_edit")]["result"]["metrics"]
weak = by_point[(5.0, "weight_edit")]["result"]["metrics"]
collateral = by_point[(4.0, "weight_edit")]["result"]["metrics"]
# Each exploit maximises the target score ...
assert invalid["objectives"]["effect"] == weak["objectives"]["effect"] == 1.0
assert max(e["objectives"]["effect"] for e in state["pareto_front"]) < 1.0
# ... and is infeasible: invalid outputs fail their endpoint and the weak-scorer check
# flags gains that come only from empty output, even when the scorer calls them valid.
assert not invalid["feasible"] and invalid["endpoints"]["invalid_output"]["status"] == "FAIL"
assert invalid["weak_scorer"]["flagged"]
assert not weak["feasible"] and weak["endpoints"]["invalid_output"]["status"] == "PASS"
assert weak["weak_scorer"]["flagged"] and weak["weak_scorer"]["degenerate_gain_fraction"] == 1
assert any("invalid or degenerate outputs" in r for r in weak["infeasible_reasons"])
assert not collateral["feasible"]
assert collateral["endpoints"]["collateral:keep"]["status"] == "FAIL"
excluded = {e["trial"] for e in state["excluded"]["infeasible"]}
assert {by_point[k]["trial"] for k in ((6.0, "weight_edit"), (5.0, "weight_edit"),
(4.0, "weight_edit"))} <= excluded
for trial in excluded:
with pytest.raises(SearchRefused, match="infeasible"):
confirm_finalist(tmp_path / "search", trial, "sha256:" + "2" * 64)
# The independent format check and secondary agreement on their own.
rows_ = [{"id": "a", "success": True, "pristine_success": False, "output": "yes",
"invalid": False, "secondary": False}]
flagged = pareto.weak_scorer(rows_, "t", "increase", {"sample": 4,
"max_degenerate_gain_fraction": 0,
"min_agreement": 0.9})
assert flagged["flagged"] and flagged["agreement"] == 0.0
assert not pareto.format_valid(" ... ") and pareto.format_valid("no")
with pytest.raises(pareto.OutcomeRefused):
pareto.check_outcome({"target": [{"id": "a"}], "collateral": {}, "cost": 0})
def test_pareto_front_is_reported_before_the_declared_weighted_score(env, selected, tmp_path): # noqa: F811
plan(tmp_path, env, selected, "plain")
plain = run_search(tmp_path / "plain", Stand())
assert plain["weighted_score"] == {"declared": False, "weights": None, "ranking": None}
assert plain["selection_basis"].startswith("no aggregate declared")
front_ids = [e["trial"] for e in plain["pareto_front"]]
# Non-dominated: the highest feasible effect at the lowest cost (steer 6.0 adapter and the
# saved candidate tie) plus nothing dominated by them.
effects = {(e["point"]["arm"], e["point"]["coefficient"], e["point"]["form"]):
e["objectives"] for e in plain["pareto_front"]}
assert set(effects) == {("steer", 6.0, "activation_adapter"),
("saved", 6.0, "activation_adapter")}
assert plain["search_winner"] == plain["finalists"][0] and set(plain["finalists"]) == set(
front_ids)
plan(tmp_path, env, selected, "weighted",
selection={"weights": {"effect": 1.0, "retained": 1.0, "cost": 0.001}})
weighted = run_search(tmp_path / "weighted", Stand())
text = json.dumps(weighted, sort_keys=True)
assert weighted["presentation_order"] == ["pareto_front", "weighted_score"]
assert text.index('"pareto_front"') < text.index('"weighted_score"')
ranking = weighted["weighted_score"]["ranking"]
assert {r["trial"] for r in ranking} == {e["trial"] for e in weighted["pareto_front"]}
assert weighted["search_winner"] == ranking[0]["trial"]
assert weighted["selection_basis"] == "pre-declared weights over the Pareto front"
# Weights are part of the space identity: they cannot be chosen after the results.
assert read_space(tmp_path / "weighted")["id"] != read_space(tmp_path / "plain")["id"]
with pytest.raises(SearchRefused, match="already holds search space"):
plan_search(make_spec(env, selected), tmp_path / "weighted")
with pytest.raises(SearchRefused, match="selection.weights"):
build_space(make_spec(env, selected, selection={"weights": {"loss": 1}}))
def test_budgets_pruning_and_early_stopping_are_enforced(env, selected, tmp_path): # noqa: F811
plan(tmp_path, env, selected, "trials", budget={"max_trials": 3})
state = run_search(tmp_path / "trials", Stand())
assert state["trials"] == 3 and state["stopped"] == "budget: max_trials 3 reached"
plan(tmp_path, env, selected, "storage", budget={"max_artifacts": 3, "max_bytes": 10_000})
state = run_search(tmp_path / "storage", Stand(write=2))
table = list(sl.trials(sl.Ledger(tmp_path / "storage").entries()).values())
assert [r["status"] for r in table] == ["completed", "failed"]
assert table[1]["result"]["storage_exceeded"] and state["usage"]["artifacts"] == 2
assert state["stopped"] == "budget: storage budget exceeded"
stored = sorted(p.name for p in (tmp_path / "storage" / "trials").rglob("*.bin"))
assert stored == ["artifact-0.bin", "artifact-1.bin"]
assert table[0]["result"]["artifacts"][0]["bytes"] == 64
plan(tmp_path, env, selected, "cost", budget={"max_cost": 130})
state = run_search(tmp_path / "cost", Stand())
assert state["trials"] == 3 and state["stopped"] == "budget: max_cost 130 reached"
plan(tmp_path, env, selected, "patience", early_stopping={"patience": 2})
state = run_search(tmp_path / "patience", Stand())
assert state["stopped"].startswith("early stopping") and state["trials"] < 11
plan(tmp_path, env, selected, "pruned", pruning={"rows": 12, "below": 0.9})
state = run_search(tmp_path / "pruned", Stand(), max_new_trials=4)
assert state["status_counts"].get("pruned", 0) >= 1
pruned = next(r for r in sl.trials(sl.Ledger(tmp_path / "pruned").entries()).values()
if r["status"] == "pruned")
assert pruned["result"]["screen"]["rows"] == 12
assert pruned["trial"] not in state["finalists"]
def test_replay_with_the_same_seed_reproduces_the_ledger(env, selected, tmp_path, capsys): # noqa: F811
from obliteratus.cli import main
space = plan(tmp_path, env, selected, "first", budget={"max_trials": 7})
first = run_search(tmp_path / "first", Stand())
spec = make_spec(env, selected, budget={"max_trials": 7})
(tmp_path / "spec.json").write_text(json.dumps(spec))
main(["experiments", "search", "plan", str(tmp_path / "spec.json"),
"--out", str(tmp_path / "second")])
assert json.loads(capsys.readouterr().out)["space"] == space["id"]
recorded = {"schema": OUTCOMES_SCHEMA, "points": []}
for point in points(space):
ids = [r["id"] for r in space["frozen"]["development"]["rows"]]
found = outcome(point, ids, "full")
recorded["points"].append({
"point": point, "cost": found["cost"], "compute": found["compute"],
"rows": {r["id"]: {k: v for k, v in r.items() if k != "id"}
for r in found["target"]},
"collateral": {"keep": dict(zip([i for i in ids if i.startswith("keep:")],
found["collateral"]["keep"]))}})
(tmp_path / "outcomes.json").write_text(json.dumps(recorded))
main(["experiments", "search", "run", str(tmp_path / "second"), "--outcomes",
str(tmp_path / "outcomes.json"), "--expect-space", space["id"]])
second = json.loads(capsys.readouterr().out)
assert second["ledger_digest"] == first["ledger_digest"] and second["trials"] == 7
main(["experiments", "search", "front", str(tmp_path / "second")])
assert json.loads(capsys.readouterr().out)["pareto_front"] == second["pareto_front"]
main(["experiments", "search", "resume", str(tmp_path / "second"), "--outcomes",
str(tmp_path / "outcomes.json")])
assert json.loads(capsys.readouterr().out)["ledger_digest"] == first["ledger_digest"]
# Proposals are a pure function of (space, seed, index, earlier trials).
table = sl.trials(sl.Ledger(tmp_path / "first").entries())
for record in table.values():
earlier = {k: v for k, v in table.items() if v["index"] < record["index"]}
assert propose(space, earlier, record["index"]) == record["point"]
other = plan(tmp_path, env, selected, "seeded", seed=11, budget={"max_trials": 7})
assert other["id"] != space["id"]
# Confirmation results or other fields never enter a replay table.
with pytest.raises(SearchRefused, match="not development outcomes"):
ReplayEvaluator({**recorded, "points": [{**recorded["points"][0],
"confirmation": {"outcome": "supported"}}]})
with pytest.raises(SearchRefused, match="outcomes schema"):
ReplayEvaluator({"points": []})
with pytest.raises(SystemExit) as refused:
main(["experiments", "search", "run", str(tmp_path / "second"), "--outcomes",
str(tmp_path / "outcomes.json"), "--expect-space", "sha256:" + "0" * 64])
assert refused.value.code == 4
# An edited ledger entry is refused; a torn final line is ignored.
path = tmp_path / "first" / sl.LEDGER
shutil.copy(path, tmp_path / "ledger.bak")
with path.open("a") as stream:
stream.write('{"torn": ')
assert len(sl.Ledger(tmp_path / "first").entries()) == len(
sl.Ledger(tmp_path / "second").entries()) - 1 # second has one more stop entry
lines = (tmp_path / "ledger.bak").read_text().splitlines()
finish = next(i for i, line in enumerate(lines) if '"finish"' in line)
lines[finish] = lines[finish].replace('"completed"', '"pruned"')
path.write_text("\n".join(lines) + "\n")
with pytest.raises(SearchRefused, match="edited, reordered or removed"):
front(tmp_path / "first")
(tmp_path / "first" / "space.json").write_text(
(tmp_path / "first" / "space.json").read_text().replace('"seed": 3', '"seed": 4'))
with pytest.raises(SearchRefused, match="edited after it was written"):
read_space(tmp_path / "first")