mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Start milestone 4 (F16): turn a confirmed temporary intervention into the smallest persistent representation that expresses it, as a separate artifact, and verify the saved candidate itself after a fresh load. - Builds only for registry keys computed as persistent-pilot eligible (fixture models only with a test-only allowance marking outputs software-contract). The reference model is never written to; its digests are checked before and after. - Forms: a removable hook-free activation adapter (all operations), a LoRA adapter through the existing PEFT exporter (all-token ablation on a plain linear site), or a separate weight-edited copy (projection or bias shift). Auto picks the feasible form with the fewest stored elements. - A write-once, content-addressed manifest records parent identity and fingerprints, recipe and direction binding, every edited parameter with before/after digests, compatibility checks, artifact digests and build-time tolerances. - Verification reloads the candidate from disk and compares reference, temporary hook and candidate arms on independent rows: the edit must match the registered recipe, effects and outputs must agree with the temporary intervention, the useful-effect threshold and every collateral endpoint must hold. Failures are permanent and never selected. - `obliteratus experiments candidate build|verify|show`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
278 lines
15 KiB
Python
278 lines
15 KiB
Python
"""Persistent candidate builder: the smallest saved form of a validated temporary edit.
|
|
|
|
``build_candidate`` takes an unlocked confirmation registration whose key
|
|
(target, method, model revision, task, backend) has the computed maturity
|
|
``persistent-pilot eligible`` and writes a separately identified candidate under
|
|
``<run>/persistent-candidates/<id>/``:
|
|
|
|
* ``artifact/``: the saved representation (``candidate_edits``): a removable
|
|
activation adapter, a removable LoRA adapter or an edited model copy;
|
|
* ``candidate.json``: the manifest, whose id is the canonical digest of its
|
|
content: parent identity (model id, revision, full weight fingerprint and
|
|
the digest of every source file), recipe and direction binding, edit
|
|
manifest, compatibility checks, artifact digests and declared tolerances.
|
|
|
|
The reference artifact is read, never written: its file digests are taken
|
|
before and after the build and must be identical. ``candidate_verify`` reloads
|
|
the saved candidate and measures it; ``candidate_status`` reports state (only a
|
|
candidate whose every verification passed is ``selected``) and attachments.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import shutil
|
|
|
|
import torch
|
|
|
|
from obliteratus.analysis import dossier_refs as refs
|
|
from obliteratus.analysis import maturity
|
|
from obliteratus.analysis.capture_identity import weight_fingerprint
|
|
from obliteratus.analysis.dossier_interventions import (
|
|
SOFTWARE_CONTRACT, evidence_scope, hypothesis_digest,
|
|
)
|
|
from obliteratus.concepts.dataset import identity
|
|
from obliteratus.experiments import candidate_edits as edits
|
|
from obliteratus.experiments import candidate_io as cio
|
|
from obliteratus.experiments.candidate_edits import CandidateRefused
|
|
from obliteratus.experiments.registry_store import Registry, actor_name, now, tool
|
|
|
|
CANDIDATE_DIR = "persistent-candidates"
|
|
MANIFEST = "candidate.json"
|
|
ARTIFACT = "artifact"
|
|
VERIFICATIONS = "verifications"
|
|
ATTACHMENTS = "attachments.jsonl"
|
|
SCHEMA = "obliteratus.persistent-candidate"
|
|
REQUIRED = maturity.LADDER[-1]
|
|
DEFAULT_TOLERANCES = {"target_rate": 0.05, "agreement": 0.95, "logit_atol": 1e-4}
|
|
EQUIVALENCE = ("not assumed: runtime hooks, adapters and weight edits are different "
|
|
"computations; the saved candidate is reloaded and measured against the "
|
|
"temporary intervention")
|
|
SELECTION_RULE = ("smallest stored element count among representations whose compatibility "
|
|
"checks pass; ties prefer removable adapters")
|
|
EVIDENCE_NOTE = ("persistent-pilot evidence: a reloaded candidate reproduced the temporary "
|
|
"effect and passed its collateral endpoints on independent rows; descriptive, "
|
|
"it does not change the registry's computed maturity; deployment "
|
|
"qualification is a separate step")
|
|
|
|
|
|
def tensor_sha256(tensor):
|
|
raw = tensor.detach().contiguous().cpu().reshape(-1).view(torch.uint8).numpy().tobytes()
|
|
return hashlib.sha256(raw).hexdigest()
|
|
|
|
|
|
def tensor_digests(model):
|
|
"""``{name: sha256}`` of every parameter and buffer."""
|
|
return {name: tensor_sha256(t) for name, t in [*model.named_parameters(),
|
|
*model.named_buffers()]}
|
|
|
|
|
|
def gate(run_dir, registration, *, allow_fixture_results=False):
|
|
"""The verified registration and its key's level, or refuse below the required level."""
|
|
registry = Registry(run_dir)
|
|
if not registry.exists():
|
|
raise CandidateRefused(f"No confirmation registry in {run_dir}")
|
|
document = registry.get(registration)
|
|
if document["kind"] != "confirmation":
|
|
raise CandidateRefused(f"{registration} is a {document['kind']}; build from the "
|
|
"unlocked confirmation registration")
|
|
documents = registry.registrations()
|
|
if any(d["frozen"]["amends"] == registration for d in documents):
|
|
raise CandidateRefused(f"{registration} was amended; build from its amendment")
|
|
if not any(e["event"] == "unlock" and e["registration"] == registration
|
|
for e in registry.events()):
|
|
raise CandidateRefused(f"{registration} has no unlocked confirmation result")
|
|
from obliteratus.experiments.registry_status import key_level
|
|
level = key_level(run_dir, document["frozen"], allow_fixture_results=allow_fixture_results)
|
|
if maturity.LADDER.index(level) < maturity.LADDER.index(REQUIRED):
|
|
raise CandidateRefused(f"Computed maturity of {document['frozen']['target_key']} is "
|
|
f"{level!r}; a persistent candidate needs {REQUIRED!r}")
|
|
return document, level
|
|
|
|
|
|
def _scope(spec, dossier, allow_fixture_results):
|
|
scope = evidence_scope(spec["model"]["model_class"],
|
|
[spec["model"]["id"], dossier["provenance"]["model"]["model_id"]])
|
|
if allow_fixture_results:
|
|
scope = {"scope": SOFTWARE_CONTRACT,
|
|
"reason": "built with the test-only fixture allowance: " + scope["reason"]}
|
|
return scope
|
|
|
|
|
|
def _select(requested, operation, module, vector, width):
|
|
options = {}
|
|
for name in edits.REPRESENTATIONS:
|
|
checks = edits.compatibility(name, operation, module, width)
|
|
if name == "lora" and all(c["passed"] for c in checks):
|
|
checks.append(edits.bias_check(module, vector))
|
|
feasible = all(c["passed"] for c in checks)
|
|
options[name] = {"checks": checks, "feasible": feasible,
|
|
"elements": edits.elements(name, module, vector) if feasible else None}
|
|
feasible = [n for n in edits.REPRESENTATIONS if options[n]["feasible"]]
|
|
if requested == "auto":
|
|
if not feasible: # pragma: no cover - the activation adapter fits every operation
|
|
raise CandidateRefused("No representation expresses this operation")
|
|
chosen = min(feasible, key=lambda n: (options[n]["elements"], n not in edits.REMOVABLE))
|
|
else:
|
|
edits.compatibility(requested, operation, module, width)
|
|
if requested not in feasible:
|
|
failed = [c["detail"] for c in options[requested]["checks"] if not c["passed"]]
|
|
raise CandidateRefused(f"Representation {requested} cannot express the operation: "
|
|
+ "; ".join(failed))
|
|
chosen = requested
|
|
return chosen, {"requested": requested, "chosen": chosen, "rule": SELECTION_RULE,
|
|
"options": options}
|
|
|
|
|
|
def _write_edit(chosen, out, model, tokenizer, target, operation, model_io, spec):
|
|
module_name, module, vector = target["module"], target["layer"], target["vector"]
|
|
coefficient = operation["decision_coefficient"]
|
|
record = {"representation": chosen, "removable": chosen in edits.REMOVABLE,
|
|
"module": module_name, "site": target["geometry"]["site"],
|
|
"operation": {k: operation[k] for k in ("kind", "token_scope", "units")},
|
|
"coefficient": coefficient, "vector_sha256": target["record"]["vector_sha256"],
|
|
"rank": 1 if vector.ndim == 1 else int(vector.shape[0]), "tensors": []}
|
|
if chosen == "activation_adapter":
|
|
edits.save_activation_adapter(out, vector, edits.adapter_config(
|
|
module_name, operation, coefficient))
|
|
record["tensors"] = [{"name": "vectors", "sha256": tensor_sha256(vector.cpu())}]
|
|
record["changed_parameters"] = []
|
|
elif chosen == "lora":
|
|
pairs = edits.lora_pairs(module_name, module, vector, coefficient)
|
|
edits.save_lora(out, pairs, spec["model"]["id"], spec["model"]["revision"])
|
|
record["tensors"] = [{"name": name, "b_sha256": tensor_sha256(b),
|
|
"a_sha256": tensor_sha256(a), "rank": int(a.shape[0])}
|
|
for name, (b, a) in edits.read_lora(out).items()]
|
|
record["changed_parameters"] = []
|
|
else:
|
|
before = tensor_digests(model)
|
|
changed = edits.apply_weight_edit(model, module_name, edits.weight_edit(
|
|
module, operation, vector, coefficient))
|
|
after = tensor_digests(model)
|
|
model_io.save(model, tokenizer, out)
|
|
record["changed_parameters"] = sorted(changed)
|
|
record["tensors"] = [{"name": n, "before_sha256": before[n], "after_sha256": after[n]}
|
|
for n in sorted(changed)]
|
|
record["candidate_weights"] = weight_fingerprint(model, mode="full")
|
|
record["unchanged"] = "every other parameter and buffer of the parent"
|
|
return record
|
|
|
|
|
|
def build_candidate(run_dir, registration, source, *, representation="auto", model_io=None,
|
|
tolerances=None, allow_fixture_results=False, actor=None):
|
|
"""Build and write one candidate; returns its manifest (``location`` added)."""
|
|
from obliteratus.experiments import intervention_checks as checks
|
|
from obliteratus.experiments.inputs import require_unmodified
|
|
from obliteratus.experiments.intervention_spec import load_intervention_spec
|
|
from obliteratus.experiments.spec import spec_digest
|
|
run_dir, source = Path(run_dir).resolve(), Path(source).resolve()
|
|
document, level = gate(run_dir, registration, allow_fixture_results=allow_fixture_results)
|
|
frozen = document["frozen"]
|
|
spec = load_intervention_spec(frozen["recipe"]["spec"])
|
|
if spec_digest(spec) != frozen["recipe"]["spec_digest"]:
|
|
raise CandidateRefused("Frozen spec no longer reproduces its digest")
|
|
root = run_dir / CANDIDATE_DIR
|
|
if cio.overlaps(source, root) or cio.overlaps(source, run_dir / "registry"):
|
|
raise CandidateRefused("The reference artifact must lie outside the candidate store")
|
|
declared = {**DEFAULT_TOLERANCES, **(tolerances or {})}
|
|
unknown = set(declared) - set(DEFAULT_TOLERANCES)
|
|
if unknown:
|
|
raise CandidateRefused(f"Unknown tolerances {sorted(unknown)}")
|
|
model_io = model_io or cio.TransformersIO()
|
|
before = cio.tree_digests(source)
|
|
prepared = checks.prepare(spec, run_dir)
|
|
handle = model_io.load(source, spec["model"])
|
|
model, tokenizer = handle.model, handle.tokenizer
|
|
target = checks.verify_model(prepared, spec, model, tokenizer, require_unmodified(model))
|
|
parent_full = weight_fingerprint(model, mode="full")
|
|
operation = spec["operation"]
|
|
chosen, selection = _select(representation, operation, target["layer"], target["vector"],
|
|
target["geometry"]["width"])
|
|
staging = cio.staging_dir(root)
|
|
try:
|
|
edit = _write_edit(chosen, staging / ARTIFACT, model, tokenizer, target, operation,
|
|
model_io, spec)
|
|
if cio.tree_digests(source) != before:
|
|
raise CandidateRefused("The reference artifact changed during the build")
|
|
files = cio.tree_digests(staging / ARTIFACT)
|
|
dossier, ident = prepared["dossier"], target["identity"]
|
|
manifest = {
|
|
"schema": SCHEMA, "schema_version": 1, "id": None, "created_at": now(),
|
|
"actor": actor_name(actor), "tool": tool(),
|
|
"scope": _scope(spec, dossier, allow_fixture_results),
|
|
"registration": {"id": registration, "target_key": frozen["target_key"],
|
|
"spec_digest": frozen["recipe"]["spec_digest"],
|
|
"maturity_at_build": level, "required": REQUIRED,
|
|
"allow_fixture_results": bool(allow_fixture_results)},
|
|
"parent": {"model_id": spec["model"]["id"],
|
|
"revision": ident["checkpoint"]["model_revision"],
|
|
"dtype": spec["model"]["dtype"], "source": str(source),
|
|
"files": before, "files_sha256": cio.tree_sha256(before),
|
|
"weights": ident["weights"], "weights_full": parent_full,
|
|
"tokenizer_vocab_sha256": ident["tokenizer"]["vocab_sha256"]},
|
|
"recipe": {"spec_digest": frozen["recipe"]["spec_digest"],
|
|
"dossier": {"path": refs.stored_path(prepared["path"], run_dir),
|
|
"hypothesis_sha256": hypothesis_digest(dossier["hypothesis"])},
|
|
"estimate": {k: prepared["estimate"][k] for k in ("id", "type", "site")},
|
|
"target": target["record"], "declared": prepared["declared"]},
|
|
"edit": edit, "selection": selection,
|
|
"compatibility": selection["options"][chosen]["checks"],
|
|
"artifact": {"path": ARTIFACT, "files": files, "sha256": cio.tree_sha256(files),
|
|
"size_bytes": cio.tree_size(staging / ARTIFACT)},
|
|
"tolerances": declared, "equivalence": EQUIVALENCE}
|
|
manifest["id"] = manifest_id(manifest)
|
|
(staging / MANIFEST).write_text(json.dumps(manifest, indent=2, sort_keys=True,
|
|
allow_nan=False) + "\n", encoding="utf-8")
|
|
path = cio.promote(staging, root / manifest["id"].removeprefix("sha256:"))
|
|
except BaseException:
|
|
shutil.rmtree(staging, ignore_errors=True)
|
|
raise
|
|
finally:
|
|
cleanup = getattr(handle, "cleanup", None)
|
|
if callable(cleanup):
|
|
cleanup()
|
|
return {**manifest, "location": str(path)}
|
|
|
|
|
|
def manifest_id(manifest):
|
|
return identity({k: v for k, v in manifest.items() if k not in ("id", "location")})
|
|
|
|
|
|
def candidate_path(run_dir, candidate):
|
|
hexid = str(candidate).removeprefix("sha256:")
|
|
return Path(run_dir) / CANDIDATE_DIR / hexid
|
|
|
|
|
|
def read_candidate(run_dir, candidate):
|
|
"""``(manifest, path)``; refused when missing or edited after it was written."""
|
|
path = candidate_path(run_dir, candidate)
|
|
try:
|
|
manifest = json.loads((path / MANIFEST).read_text(encoding="utf-8"))
|
|
except (OSError, ValueError) as error:
|
|
raise CandidateRefused(f"No readable candidate {candidate}: {error}") from error
|
|
if manifest.get("schema") != SCHEMA or manifest_id(manifest) != manifest.get("id") \
|
|
or path.name != str(manifest.get("id", "")).removeprefix("sha256:"):
|
|
raise CandidateRefused(f"Candidate {path.name} manifest was edited after it was written")
|
|
return manifest, path
|
|
|
|
|
|
def artifact_problems(manifest, path):
|
|
"""Differences between the recorded and the stored artifact files."""
|
|
try:
|
|
found = cio.tree_digests(path / manifest["artifact"]["path"])
|
|
except FileNotFoundError as error:
|
|
return [str(error)]
|
|
recorded = manifest["artifact"]["files"]
|
|
problems = [f"artifact file {n} changed" for n in sorted(set(found) & set(recorded))
|
|
if found[n] != recorded[n]]
|
|
problems += [f"artifact file {n} missing" for n in sorted(set(recorded) - set(found))]
|
|
problems += [f"unrecorded artifact file {n}" for n in sorted(set(found) - set(recorded))]
|
|
return problems
|
|
|
|
|
|
__all__ = ["CANDIDATE_DIR", "DEFAULT_TOLERANCES", "EVIDENCE_NOTE", "REQUIRED",
|
|
"artifact_problems", "build_candidate", "candidate_path", "gate", "manifest_id",
|
|
"read_candidate", "tensor_digests", "tensor_sha256"]
|