"""Transactional two-step edit sequences: preconditions and the frozen plan. ``plan_sequence`` admits an ordered pair of validated edits (first, second) and writes a versioned, write-once plan under ``/sequences//plan.json``. Each component is either a selected persistent candidate (every verification passed) or a confirmation registration whose key is ``persistent-pilot eligible``; its key must still pass the candidate maturity gate. The pair needs an interaction result for exactly that ordered sequence: a result that ran only ``A>B`` admits ``A>B`` and refuses ``B>A``. Whether an interaction or an order effect was detected is recorded from the result; it is descriptive evidence. The plan id is the canonical digest of the frozen content (components in order, reference files, interaction evidence, evaluation dataset, tolerances), so the reversed order is a different sequence. Execution, resume, final verification and rollback are in ``sequence_run`` and ``sequence_rollback``. """ from __future__ import annotations import json from pathlib import Path from obliteratus.analysis import dossier_refs as refs from obliteratus.analysis import interaction_stats as istats from obliteratus.concepts.dataset import identity from obliteratus.experiments import candidate as cand from obliteratus.experiments import candidate_io as cio from obliteratus.experiments import candidate_status as cstatus from obliteratus.experiments.candidate_edits import CandidateRefused from obliteratus.experiments.registry_store import RegistryRefused, actor_name, now, tool SEQUENCE_DIR = "sequences" PLAN = "plan.json" PLAN_SCHEMA = "obliteratus.edit-sequence-plan" SCHEMA_VERSION = 1 LENGTH = 2 COMPOSABLE = ("activation_adapter", "weight_edit") ORDER_RULE = ("an ordered pair is admitted only when an interaction result measured that order; " "the reversed order is a different sequence with its own identity and evidence") EQUIVALENCE = ("not assumed: each step's saved artifact is reloaded and measured against the " "composed temporary operations; mathematical invertibility is not a rollback") class SequenceRefused(CandidateRefused): """The sequence cannot be planned, run, resumed or rolled back as requested.""" def _signature(frozen, spec): op, hyp = spec["operation"], frozen["hypothesis"] return {"dossier": hyp["dossier"]["name"], "dossier_created_at": hyp["dossier"]["created_at"], "hypothesis_sha256": hyp["dossier"]["hypothesis_sha256"], "estimate": hyp["estimate"]["id"], "model_id": spec["model"]["id"], "operation": {"kind": op["kind"], "site": op["site"], "token_scope": op["token_scope"], "units": op["units"], "coefficient": op["decision_coefficient"]}} def resolve_component(ref, *, allow_fixture_results=False): """One admitted component: its source, registration, gate level and signature.""" from obliteratus.experiments.intervention_spec import load_intervention_spec run = Path(ref["run"]).resolve() if ("candidate" in ref) == ("registration" in ref): raise SequenceRefused("A component names exactly one of candidate or registration") if "candidate" in ref: manifest, path = cand.read_candidate(run, ref["candidate"]) state = cstatus.candidate_state(path) if not state["selected"]: raise SequenceRefused(f"Candidate {manifest['id']} is {state['state']}; only a " "verified (passed, selected) candidate enters a sequence") problems = cand.artifact_problems(manifest, path) if problems: raise SequenceRefused(f"Candidate {manifest['id']} artifact: {'; '.join(problems)}") representation = manifest["edit"]["representation"] if representation not in COMPOSABLE: raise SequenceRefused(f"Representation {representation} is not composed by the " f"sequence executor; use one of {COMPOSABLE}") registration, fixture = manifest["registration"]["id"], \ manifest["registration"]["allow_fixture_results"] source = {"kind": "candidate", "candidate": manifest["id"], "representation": representation, "module": manifest["edit"]["module"], "vector_sha256": manifest["edit"]["vector_sha256"], "artifact_sha256": manifest["artifact"]["sha256"], "parent_files_sha256": manifest["parent"]["files_sha256"], "verifications": state["verifications"], "scope": manifest["scope"]["scope"]} else: registration, fixture = ref["registration"], bool(allow_fixture_results) source = {"kind": "registration", "representation": "activation_adapter"} try: document, level = cand.gate(run, registration, allow_fixture_results=fixture) except (CandidateRefused, RegistryRefused) as error: raise SequenceRefused(str(error)) from error frozen = document["frozen"] spec = load_intervention_spec(frozen["recipe"]["spec"]) return {"run": str(run), "source": source, "signature": _signature(frozen, spec), "registration": {"id": registration, "target_key": frozen["target_key"], "spec_digest": frozen["recipe"]["spec_digest"], "maturity": level, "allow_fixture_results": fixture}, "target_task": spec["target_task"], "declared": frozen["thresholds"], "spec": spec} def _result_signature(result, name): part, op = result["components"][name], result["spec"]["components"][name]["operation"] return {"dossier": part["dossier"]["name"], "dossier_created_at": part["dossier"]["created_at"], "hypothesis_sha256": part["dossier"]["hypothesis_sha256"], "estimate": part["estimate"]["id"], "model_id": result["spec"]["model"]["id"], "operation": {k: op[k] for k in ("kind", "site", "token_scope", "units", "coefficient")}} def _summary(outcomes, positive, negative): if positive in outcomes: return positive return istats.UNTESTED if not outcomes or istats.UNTESTED in outcomes else negative def interaction_evidence(path, components): """The interaction result admitting this ordered pair, or refuse.""" from obliteratus.experiments.interaction_runner import load_interaction_result if path is None: raise SequenceRefused("No interaction evidence: a sequence needs an interaction " "result (experiments interact run) for its ordered pair") try: result = load_interaction_result(path, verify=True) except (OSError, ValueError) as error: raise SequenceRefused(f"Interaction result {path} does not verify: {error}") from error names = {} for position, component in enumerate(components): match = [n for n in ("A", "B") if _result_signature(result, n) == component["signature"]] if not match: raise SequenceRefused(f"No interaction evidence for component {position + 1} " f"({component['signature']['dossier']}/" f"{component['signature']['estimate']}) in {path}") names[position] = match[0] if names[0] == names[1]: raise SequenceRefused("Both components match the same interaction component") order = f"{names[0]}>{names[1]}" analysis = result["analysis"] sensitivity = {task: (body.get("target") or body)["outcome"] for task, body in analysis["order_sensitivity"].items()} dependence = _summary(set(sensitivity.values()), istats.ORDER_SENSITIVE, istats.NO_ORDER_DIFFERENCE) if order not in result["orders"]: raise SequenceRefused(f"Order {order} was not measured by the interaction result (orders " f"{result['orders']}; order dependence {dependence}); the untested " "order is refused") interaction = {task: orders[order]["target"]["outcome"] for task, orders in analysis["interactions"].items()} file = Path(result["location"]["result"]) return {"result": {"path": str(file.resolve()), "sha256": refs.file_sha256(file)}, "spec_digest": result["spec_digest"], "order": order, "orders_tested": result["orders"], "interaction": interaction, "interaction_detected": _summary(set(interaction.values()), istats.DETECTED, istats.NOT_DETECTED) == istats.DETECTED, "order_sensitivity": sensitivity, "order_dependence": dependence, "order_pinned": dependence != istats.NO_ORDER_DIFFERENCE, "evidence_scope": result["evidence_scope"]["scope"], "dataset": {"revision": result["inputs"]["dataset"]["revision"], "role": result["inputs"]["dataset"]["role"]}, "rule": ORDER_RULE} def _shared(components, source): first = components[0]["spec"] for key in ("model", "tasks", "decoding"): if any(c["spec"][key] != first[key] for c in components[1:]): raise SequenceRefused(f"Components differ in {key}; a sequence evaluates one model " "on one task set") for position, component in enumerate(components): parent = component["source"].get("parent_files_sha256") if parent is not None and parent != source["files_sha256"]: raise SequenceRefused(f"Component {position + 1} was built from another reference " "artifact than the selected reference") if component["source"]["representation"] == "weight_edit" and any( c["source"]["representation"] != "weight_edit" for c in components[:position]): raise SequenceRefused("A weight edit after an activation adapter would run before " "it; that order is not expressed") return {"model": first["model"], "tasks": first["tasks"], "decoding": first["decoding"], "seed": first["seed"], "probes": first["probes"]} def _scope(components, evidence): scopes = {c["source"].get("scope") for c in components} | {evidence["evidence_scope"]} fixture = any(c["registration"]["allow_fixture_results"] for c in components) return "software-contract" if fixture or "software-contract" in scopes else "model-evidence" def plan_sequence(store, components, source, interaction, dataset, *, tolerances=None, allow_fixture_results=False, actor=None): """Admit and freeze an ordered two-step sequence; returns the plan (``location`` added).""" store, source = Path(store).resolve(), Path(source).resolve() if len(components) != LENGTH: raise SequenceRefused(f"A sequence has exactly {LENGTH} steps (two-step sequences only)") resolved = [resolve_component(c, allow_fixture_results=allow_fixture_results) for c in components] if resolved[0]["signature"] == resolved[1]["signature"]: raise SequenceRefused("Both steps are the same edit") if cio.overlaps(source, store / SEQUENCE_DIR) or any(cio.overlaps(source, c["run"]) for c in resolved): raise SequenceRefused("The reference artifact must lie outside the sequence and " "candidate stores") files = cio.tree_digests(source) reference = {"source": str(source), "files": files, "files_sha256": cio.tree_sha256(files)} evidence = interaction_evidence(interaction, resolved) if (evidence["dataset"]["revision"], evidence["dataset"]["role"]) == \ (dataset.get("revision"), dataset.get("role")): raise SequenceRefused("Evaluation rows are the interaction result's rows; evaluate the " "sequence on independent rows") declared = {**cand.DEFAULT_TOLERANCES, **(tolerances or {})} if set(declared) - set(cand.DEFAULT_TOLERANCES): raise SequenceRefused(f"Unknown tolerances {sorted(set(declared) - set(cand.DEFAULT_TOLERANCES))}") frozen = {"length": LENGTH, "reference": reference, "interaction": evidence, "dataset": {"pack": dataset["pack"], "revision": dataset["revision"], "role": dataset["role"], "allow_locked_test": bool(dataset.get("allow_locked_test", False))}, "tolerances": declared, **_shared(resolved, reference), "components": [{k: v for k, v in c.items() if k != "spec"} | {"position": i + 1} for i, c in enumerate(resolved)], "scope": _scope(resolved, evidence), "equivalence": EQUIVALENCE} plan = {"schema": PLAN_SCHEMA, "schema_version": SCHEMA_VERSION, "id": identity(frozen), "frozen": frozen} path = sequence_path(store, plan["id"]) if (path / PLAN).is_file(): return {**read_plan(store, plan["id"])[0], "location": str(path)} plan.update(created_at=now(), actor=actor_name(actor), tool=tool()) staging = cio.staging_dir(store / SEQUENCE_DIR) (staging / PLAN).write_text(json.dumps(plan, indent=2, sort_keys=True, allow_nan=False) + "\n", encoding="utf-8") cio.promote(staging, path) return {**plan, "location": str(path)} def sequence_path(store, sequence): return Path(store) / SEQUENCE_DIR / str(sequence).removeprefix("sha256:") def read_plan(store, sequence): """``(plan, path)``; refused when missing or edited after it was written.""" path = sequence_path(store, sequence) try: plan = json.loads((path / PLAN).read_text(encoding="utf-8")) except (OSError, ValueError) as error: raise SequenceRefused(f"No readable sequence {sequence}: {error}") from error if plan.get("schema") != PLAN_SCHEMA or plan.get("schema_version") != SCHEMA_VERSION \ or identity(plan.get("frozen")) != plan.get("id") \ or path.name != str(plan["id"]).removeprefix("sha256:"): raise SequenceRefused(f"Sequence {path.name} plan was edited after it was written") return plan, path __all__ = ["COMPOSABLE", "LENGTH", "PLAN_SCHEMA", "SEQUENCE_DIR", "SequenceRefused", "interaction_evidence", "plan_sequence", "read_plan", "resolve_component", "sequence_path"]