Files
OBLITERATUS/tests/test_observation_prune.py
Joseph MaglyandClaude Opus 5.5 434e4a7235 feat(experiments): weight fingerprint, capture retention and attempt pruning
Weights edited in place under the same model id and revision were not
detected, expired captures still loaded, and run stores could not be
pruned because reused steps reference earlier attempts.

- Fingerprint: a deterministic fingerprint of every parameter and
  buffer (names, shapes, dtypes and a fixed strided sample of each
  tensor, with an optional full digest) joins the capture identity and
  the runner's model identity. Captures and steps from before the
  fingerprint are refused or recomputed with a recorded reason. The
  pipeline's entry-point cache identity is unchanged.
- Retention: load_capture refuses captures past expires_at unless
  inspection is requested; comparison inspects stored captures
  regardless of expiry.
- Pruning: `obliteratus experiments prune` (dry run by default) keeps
  the latest complete attempt, the newest N, active attempts and every
  attempt a kept one reuses, records a pruned event before deleting,
  and finishes interrupted removals on the next applied prune.

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

221 lines
10 KiB
Python

"""Weight identity in reused runner steps, and pruning of observation attempts."""
from __future__ import annotations
import hashlib
import json
import pytest
import torch
from obliteratus.analysis.capture_identity import digest
from obliteratus.cli import main
from obliteratus.experiments import (
ObservationCancelled, ObservationStore, StoreError, compare_results, load_spec,
run_observation, spec_digest,
)
from obliteratus.experiments.prune import prune_attempts, references
from tests.test_observation_runs import CharTokenizer, build_pack, factory, make_spec, tiny_gpt2
pytestmark = pytest.mark.cpu
@pytest.fixture
def pack_dir(tmp_path):
return build_pack(tmp_path / "pack")
def small_spec(pack_dir, **changes):
return make_spec(pack_dir, profile=False, decoding={"max_new_tokens": 2}, **changes)
def buffered():
model = tiny_gpt2()
model.transformer.register_buffer("calibration", torch.arange(8, dtype=torch.float32))
return model
def run_for(spec, store):
return ObservationStore(store).get(spec_digest(load_spec(spec)))
def step_record(run, attempt, name):
return json.loads((run.attempt_path(attempt) / "steps" / name / "step.json").read_text())
@pytest.mark.parametrize("target", ["parameter", "buffer"])
def test_in_place_weight_edits_are_not_reused_and_reloads_are(pack_dir, tmp_path, target):
spec, store = small_spec(pack_dir), tmp_path / "store"
first = run_observation(spec, store, model_factory=factory(model=buffered()))
assert first["inputs"]["model"]["weights"]["mode"] == "sampled"
reloaded = run_observation(spec, store, rerun=True, model_factory=factory(model=buffered()))
assert all(step["reused_from"]["attempt"] == 1 for step in reloaded["steps"].values())
edited = buffered()
tensor = edited.lm_head.weight if target == "parameter" else edited.transformer.calibration
with torch.no_grad():
tensor.view(-1)[0] += 0.5
changed = run_observation(spec, store, rerun=True, model_factory=factory(model=edited))
assert all(step["reused_from"] is None for step in changed["steps"].values())
run = run_for(spec, store)
for name in changed["steps"]:
field = "capture_identity_sha256" if name == "capture" else "model.weights.sha256"
refused = step_record(run, 3, name)["reuse_refused"]
assert refused == {"attempt": 1, "reason": f"identity differs at {field}"}
assert step_record(run, 1, "capture")["reuse_refused"] is None
def test_steps_recorded_before_the_fingerprint_are_recomputed_with_a_reason(pack_dir, tmp_path):
spec, store = small_spec(pack_dir), tmp_path / "store"
run_observation(spec, store, model_factory=factory())
run = run_for(spec, store)
for step_file in (run.attempt_path(1) / "steps").glob("*/step.json"):
record = json.loads(step_file.read_text())
record["identity"]["model"].pop("weights")
record["identity_sha256"] = digest(record["identity"])
record.pop("reuse_refused")
step_file.write_text(json.dumps(record))
again = run_observation(spec, store, rerun=True, model_factory=factory())
assert again["state"] == "complete"
assert all(step["reused_from"] is None for step in again["steps"].values())
assert step_record(run, 2, "generate-answers")["reuse_refused"] == {
"attempt": 1, "reason": "step predates weight fingerprint; recomputed"}
def snapshot(root):
return {path.relative_to(root).as_posix(): hashlib.sha256(path.read_bytes()).hexdigest()
for path in sorted(root.rglob("*")) if path.is_file()}
def build_history(pack_dir, store):
"""Attempts: 1 cancelled, 2 complete (reuses 1), 3 complete (other tokenizer),
4 complete (reuses 1 and 2), 5 failed."""
spec = small_spec(pack_dir)
with pytest.raises(ObservationCancelled):
run_observation(spec, store, model_factory=factory(), check_cancelled=_cancel_after(6))
run_observation(spec, store, model_factory=factory())
run_observation(spec, store, rerun=True,
model_factory=factory(tokenizer=CharTokenizer("local/other")))
fourth = run_observation(spec, store, rerun=True, model_factory=factory())
def broken(_section):
raise RuntimeError("loader exploded")
with pytest.raises(RuntimeError):
run_observation(spec, store, rerun=True, model_factory=broken)
return spec, fourth
def _cancel_after(calls):
seen = []
def check():
seen.append(1)
if len(seen) >= calls:
raise ObservationCancelled("stop")
return check
def test_prune_keeps_referenced_and_latest_complete_attempts(pack_dir, tmp_path):
store = tmp_path / "store"
spec, fourth = build_history(pack_dir, store)
run = run_for(spec, store)
assert [a["state"] for a in run.history()] == [
"cancelled", "complete", "complete", "complete", "failed"]
assert {step["reused_from"]["attempt"] for step in fourth["steps"].values()} == {1, 2}
assert references(run, 4) == {1, 2} and references(run, 2) == {1}
before = snapshot(run.path)
plan = prune_attempts(spec, store, keep=0)
assert snapshot(run.path) == before # a dry run changes nothing
assert plan["applied"] is False
assert [entry["attempt"] for entry in plan["remove"]] == [3, 5]
kept = {entry["attempt"]: entry["reasons"] for entry in plan["kept"]}
assert kept[4] == ["latest complete attempt"]
assert kept[1] == ["step artifacts referenced by attempt 4", "step artifacts referenced by attempt 2"]
assert kept[2] == ["step artifacts referenced by attempt 4"]
assert prune_attempts(spec, store)["remove"] == [
entry for entry in plan["remove"] if entry["attempt"] == 3] # default keep=1 keeps 5
applied = prune_attempts(spec, store, keep=0, apply=True)
assert applied["applied"] is True and applied["remove"] == plan["remove"]
assert not run.attempt_path(3).exists() and not run.attempt_path(5).exists()
assert all(run.attempt_path(n).is_dir() for n in (1, 2, 4))
history = {a["attempt"]: a for a in run.history()}
assert [history[n]["state"] for n in sorted(history)] == [
"cancelled", "complete", "pruned", "complete", "pruned"]
event = history[3]["events"][-1]
assert [e["state"] for e in history[3]["events"]] == ["pending", "running", "complete", "pruned"]
assert event["previous_state"] == "complete" and event["removed"]["path"] == "attempts/0003"
assert event["removed"]["bytes"] > 0 and event["reason"] == "pruned with keep=0"
assert prune_attempts(spec, store, keep=0, apply=True)["remove"] == []
# The latest result still loads, and a resumed run reuses the remaining steps.
assert run_observation(spec, store, model_factory=factory())["attempt"] == 4
resumed = run_observation(spec, store, rerun=True, model_factory=factory())
assert resumed["attempt"] == 6
assert {step["reused_from"]["attempt"] for step in resumed["steps"].values()} == {1, 2}
assert compare_results(resumed, fourth)["equal"] is True
def test_prune_finishes_interrupted_removals_and_respects_the_lock(pack_dir, tmp_path):
store = tmp_path / "store"
spec = small_spec(pack_dir, capture=None)
run_observation(spec, store, model_factory=factory())
run_observation(spec, store, rerun=True,
model_factory=factory(tokenizer=CharTokenizer("local/other")))
run = run_for(spec, store)
run.append_event({"attempt": 1, "state": "pruned", "at": "now", "reason": "interrupted",
"error_type": None})
assert prune_attempts(spec, store)["unfinished"] == [1]
assert run.attempt_path(1).is_dir()
assert prune_attempts(spec, store, apply=True)["remove"] == []
assert not run.attempt_path(1).exists()
with run.lock():
with pytest.raises(StoreError, match="running"):
prune_attempts(spec, store)
for bad in (-1, True, "2"):
with pytest.raises(ValueError, match="keep"):
prune_attempts(spec, store, keep=bad)
def test_prune_refuses_malformed_reuse_records_and_keeps_open_attempts(pack_dir, tmp_path):
store = tmp_path / "store"
spec = small_spec(pack_dir, capture=None)
run_observation(spec, store, model_factory=factory())
run_observation(spec, store, rerun=True, model_factory=factory())
run = run_for(spec, store)
stale = run.new_attempt() # left pending, as if its process were still loading
plan = prune_attempts(spec, store, keep=0)
assert {e["attempt"]: e["reasons"] for e in plan["kept"]}[stale.number] == ["attempt is pending"]
assert run.history()[-1]["state"] == "pending" # a dry run records nothing
step = run.attempt_path(2) / "steps" / "generate-answers" / "step.json"
record = json.loads(step.read_text())
record["reused_from"] = {"attempt": "one"}
step.write_text(json.dumps(record))
with pytest.raises(StoreError, match="malformed reused_from"):
prune_attempts(spec, store, apply=True)
def test_prune_cli_is_a_dry_run_unless_applied(pack_dir, tmp_path, capsys):
store = tmp_path / "store"
spec = small_spec(pack_dir, capture=None)
run_observation(spec, store, model_factory=factory())
run_observation(spec, store, rerun=True,
model_factory=factory(tokenizer=CharTokenizer("local/other")))
spec_path = tmp_path / "spec.json"
spec_path.write_text(json.dumps(spec))
main(["experiments", "prune", str(spec_path), "--store", str(store)])
dry = json.loads(capsys.readouterr().out)
assert dry["applied"] is False and [e["attempt"] for e in dry["remove"]] == [1]
assert run_for(spec, store).attempt_path(1).is_dir()
main(["experiments", "prune", str(spec_path), "--store", str(store), "--keep", "1", "--apply"])
assert json.loads(capsys.readouterr().out)["applied"] is True
assert not run_for(spec, store).attempt_path(1).exists()
main(["experiments", "history", str(spec_path), "--store", str(store)])
assert [a["state"] for a in json.loads(capsys.readouterr().out)["attempts"]] == [
"pruned", "complete"]
with pytest.raises(SystemExit) as exit_info:
main(["experiments", "prune", str(spec_path), "--store", str(tmp_path / "empty")])
assert exit_info.value.code == 4
assert "No observation run" in capsys.readouterr().err