Files
OBLITERATUS/obliteratus/concepts/cli.py
Joseph MaglyandClaude Opus 5.5 4fb564b800 feat(concepts): run a pack's declared evaluation battery and dose ladder
The pain pack declares a battery, a follow-on battery and a steering
dose ladder for run_concept_evaluation_battery, but the hook did not
exist (TODO #230 in the baseline).

- run_concept_evaluation_battery resolves the pack's evaluation
  references through the existing pinned fetch and hash checks and
  refuses missing, unfetched or mismatched references before any model
  work.
- Batteries run on the unmodified model. Packs may map battery fields
  to registered scorers through an optional evaluation.scoring section;
  without objective targets (as for pain) records are descriptive:
  final-token projection, greedy completion digest, invalid-output and
  refusal flags. No scoring code comes from the pack.
- Each declared coefficient runs through the existing temporary steering
  dose ladder; after every dose the parameter/buffer digest, hook counts
  and probe logits must match the pristine model, or the run stops.
- Results go to concept-evaluation.json with pack, reference, model and
  direction identities; the baseline's three artifacts are unchanged.
  `concepts baseline --evaluate` (and the Python API) opts in; without
  it the baseline is byte-identical.

The pain pack's deferral notes are replaced, which updates its dataset
card hash; its pinned upstream references are unchanged.

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

391 lines
20 KiB
Python

"""CLI for concept packs, run comparison and provenance exports."""
import json
import math
from pathlib import Path
import re
import sys
import yaml
from obliteratus.capacity_planning import add_capacity_arguments, maybe_plan, snapshot_options
from obliteratus.concepts.pack import ConceptPack, cache_directory, import_pack
from obliteratus.concepts.pack_io import digest
from obliteratus.concepts.registry import ConceptRegistry
from obliteratus.concepts.validation import PackError
def add_extraction_layer_argument(parser):
def layer_index(value):
import argparse
try:
layer = int(value)
except ValueError as exc:
raise argparse.ArgumentTypeError("must be a non-negative layer index") from exc
if layer < 0:
raise argparse.ArgumentTypeError("must be a non-negative layer index")
return layer
parser.add_argument(
"--extraction-layer", type=layer_index,
help="Pin a zero-based concept extraction layer, overriding the pack's model pin "
"and bypassing layer-selection CV. Requires a non-refusal concept; "
"intervention placement still follows the recipe or --layers.",
)
def add_parser(subparsers):
parser = subparsers.add_parser(
"concepts", help="Inspect concept packs, compare runs and export results",
)
commands = parser.add_subparsers(dest="concepts_command", required=True)
for name in ("list", "show", "lint", "fetch"):
command = commands.add_parser(name)
if name != "list":
command.add_argument("source", help="Concept id, pack directory or .cvpack")
command.add_argument("--json", action="store_true", help="Print machine-readable JSON")
init = commands.add_parser("init", help="Create a user pack template with example prompts")
preview = commands.add_parser("dataset-preview", help="Inspect verified local dataset partitions")
preview.add_argument("source")
preview.add_argument("--role", choices=("fit", "select", "calibrate", "test"), default="fit")
preview.add_argument("--include-text", action="store_true")
preview.add_argument("--allow-locked-test", action="store_true")
preview.add_argument("--private", action="store_true")
preview.add_argument("--max-pairs", type=int, default=10000)
preview.add_argument("--max-bytes", type=int, default=1000000)
preview.add_argument("--threshold", type=float, default=0.8)
preview.add_argument("--json", action="store_true")
init.add_argument("directory")
init.add_argument("--id", default=None, help="User concept id (defaults to directory name)")
pack = commands.add_parser("pack", help="Validate and create a deterministic .cvpack")
pack.add_argument("source")
pack.add_argument("output")
importer = commands.add_parser("import", help="Verify and install a user pack")
importer.add_argument("source")
importer.add_argument("--sha256", help="Expected archive digest; required for HTTPS imports")
compare = commands.add_parser("compare", help="Compare saved concept run outcomes")
compare.add_argument("runs", nargs="+", help="Run or export directories")
compare.add_argument("--json", action="store_true")
rescore = commands.add_parser(
"rescore", help="Re-score saved paired refusal data without loading a model",
)
rescore.add_argument("source", help="Existing evaluation.json or battery directory")
rescore.add_argument("output", help="New evaluation JSON file; existing files are never replaced")
rescore.add_argument("--collateral-margin", type=float, default=0.03, metavar="FRACTION",
help="Post-hoc refusal drop tolerance in [0, 1] (default: 0.03 = 3 points)")
rescore.add_argument("--json", action="store_true")
export = commands.add_parser("export", help="Export a concept run with provenance")
export.add_argument("source", help="Saved concept run directory")
export.add_argument("output", help="New bundle directory")
export.add_argument("--as", dest="export_kind", required=True,
choices=("weights", "lora", "direction", "report"),
help="Direction bundles retain subspace/projector tensors, affine "
"reference means and runtime replay parameters; weights retain "
"native affine bias edits for standard HF reload")
export.add_argument("--publish-card", action="store_true", help="Include a publishable README")
export.add_argument("--reason", "--override-publish", dest="reason",
help="Recorded publication acknowledgement after a gate override")
export.add_argument("--allow-collateral-fail", metavar="REASON",
help="Allow publication after collateral FAIL and record the reason")
export.add_argument("--accept-incomplete-repair",
help="Recorded choice to export incomplete repair")
export.add_argument("--push-to-hub", help="Explicitly upload the bundle to this Hub repository")
export.add_argument("--tool-commit",
help="OBLITERATUS commit when unavailable in an installed wheel")
export.add_argument("--json", action="store_true")
baseline = commands.add_parser(
"baseline", help="Explicitly fetch, extract and report a baseline",
)
baseline.add_argument("source", help="Concept id, pack directory or .cvpack")
baseline.add_argument("model_id", help="Model id or local model directory")
baseline.add_argument("--output-dir", required=True)
baseline.add_argument("--cache-dir", help="Directory for fetched concept data")
baseline.add_argument("--profile", action="store_true", help="Write local cost-profile.json")
baseline.add_argument("--profile-output", help="Profile sidecar path (enables profiling)")
baseline.add_argument("--seed", type=int, default=42)
baseline.add_argument("--device", default="cpu")
baseline.add_argument("--dtype", default="float32")
baseline.add_argument("--max-seq-length", type=int)
add_extraction_layer_argument(baseline)
baseline.add_argument(
"--evaluate", action="store_true",
help="Also run the pack's declared evaluation battery and dose ladder and write "
"concept-evaluation.json (default: off)",
)
baseline.add_argument("--override-gate", metavar="REASON",
help="With --evaluate: record a validation gate override for the "
"temporary dose-ladder steering")
baseline.add_argument("--evaluation-max-new-tokens", type=int, metavar="N",
help="With --evaluate: greedy tokens per prompt (default: 16)")
baseline.add_argument("--evaluation-max-examples", type=int, metavar="N",
help="With --evaluate: evaluate only the first N battery items")
add_capacity_arguments(baseline, snapshot_execution=True)
steer = commands.add_parser("steer", help="Generate with a saved concept direction")
steer.add_argument("model_id", help="Model id or local model directory")
steer.add_argument("direction", help="Saved direction.safetensors with JSON sidecar")
steer.add_argument("--prompt", action="append", required=True,
help="Prompt text, tokenized as supplied; repeat for multiple prompts")
steer.add_argument("--output-dir", required=True)
steer.add_argument("--scale", choices=("residual_norm", "raw", "vector"),
help="Dose units: residual_norm (per-token norm), raw (unit vector), "
"vector (stored magnitude); default: recipe or residual_norm")
steer.add_argument("--alpha", type=float, default=-1.0, help="Signed coefficient (default: -1)")
steer.add_argument("--rho", type=float, default=1.0, help="Additional multiplier (default: 1)")
steer.add_argument("--layers", default="intervention",
help="Injection layers: intervention (default), extraction, all, band(a,b), "
"or comma-separated indices; may differ from extraction")
steer.add_argument("--positions", choices=("all_tokens", "final_token"), default="all_tokens")
steer.add_argument("--override-gate", metavar="REASON", help="Record a validation gate override")
steer.add_argument("--max-new-tokens", type=int, default=120)
steer.add_argument("--device", default="cpu")
steer.add_argument("--dtype", choices=("float32", "float16", "bfloat16"), default="float32")
steer.add_argument("--json", action="store_true")
add_capacity_arguments(steer, snapshot_execution=True)
def _steer(args):
import torch
from obliteratus.concepts.direction import ConceptDirection
from obliteratus.concepts.intervention import _validation, apply_concept_intervention
from obliteratus.concepts.runs import save_concept_run, write_json
from obliteratus.device import resolve_input_execution_device
from obliteratus.models.loader import load_model
if (not math.isfinite(args.alpha) or not math.isfinite(args.rho) or args.rho < 0
or args.max_new_tokens < 1):
raise ValueError("Require finite alpha, nonnegative rho and positive max-new-tokens")
direction = ConceptDirection.load(args.direction)
_validation(direction, None, args.override_gate)
layers = args.layers
if layers not in {"intervention", "extraction", "all"}:
if match := re.fullmatch(r"band\((\d+),(\d+)\)", layers):
first, last = map(int, match.groups())
if first > last:
raise ValueError("layers band must be nonempty")
else:
try:
layers = [int(index) for index in layers.split(",")]
except ValueError as exc:
raise ValueError("layers must be a selector or comma-separated indices") from exc
if len(set(layers)) != len(layers) or any(index < 0 for index in layers):
raise ValueError("layers must be distinct nonnegative indices")
snapshot = snapshot_options(args)
handle = load_model(args.model_id, device=args.device, dtype=args.dtype, skip_snapshot=True)
try:
if snapshot["snapshot_enabled"]:
handle.snapshot(max_bytes=snapshot["snapshot_max_bytes"])
model, tokenizer = handle.model, handle.tokenizer
model.eval()
decoding = {
"max_new_tokens": args.max_new_tokens, "do_sample": False,
"num_beams": 1, "num_return_sequences": 1, "temperature": 1.0,
"top_p": 1.0, "top_k": 50, "pad_token_id": tokenizer.pad_token_id,
"return_dict_in_generate": False,
}
with apply_concept_intervention(
model, direction, "steer", alpha=args.alpha, rho=args.rho, scale=args.scale,
layers=layers, positions=args.positions, override_gate=args.override_gate,
) as intervention:
generations = []
for prompt in args.prompt:
inputs = tokenizer(prompt, return_tensors="pt").to(resolve_input_execution_device(model))
with torch.inference_mode():
output = model.generate(**inputs, **decoding)
generations.append({"prompt": prompt, "generation": tokenizer.decode(
output[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True,
)})
save_concept_run(args.output_dir, direction, intervention, runtime={
"model": {"id": args.model_id, "revision": handle.source_model_revision},
"dtype": args.dtype, "prompt_format": "as_supplied", "decoding": decoding,
})
write_json(Path(args.output_dir) / "generations.json", generations)
return {"output": args.output_dir, "mode": "steer", "scale": intervention.parameters["scale"],
"generations": generations}
finally:
handle.cleanup()
def _registry(target=None):
registry = ConceptRegistry()
failures = {}
directory = cache_directory() / "packs"
if directory.exists():
for source in sorted(directory.glob("*.cvpack")):
try:
registry.load_pack(source)
except (ValueError, OSError) as exc:
failures["user/" + source.stem] = (source, exc)
print(f"Warning: skipping cached concept pack {source}: {exc}", file=sys.stderr)
if target in failures:
try:
registry.pack(target)
except ValueError:
source, exc = failures[target]
raise PackError(f"Cannot read cached concept pack {source}: {exc}") from exc
return registry
def _resolve(source):
registry = _registry(source)
try:
return registry.pack(source)
except ValueError:
if not ("/" in source or "\\" in source or source.endswith(".cvpack")):
raise
try:
return ConceptPack.load(Path(source))
except (ValueError, OSError) as exc:
raise PackError(f"Cannot read concept pack {source}: {exc}") from exc
def _summary(pack):
manifest = pack.manifest
ident = manifest["id"]
if manifest["tier"] == "user" and not ident.startswith("user/"):
ident = "user/" + ident
return {
"id": ident, "version": manifest["version"], "tier": manifest["tier"],
"title": manifest["title"],
"licence": sorted({entry["spdx"] for entry in manifest["licence"]}),
"sha256": pack.sha256,
}
def _init(directory, ident):
directory = Path(directory)
if directory.exists() or directory.is_symlink():
raise PackError("Template directory already exists")
ident = ident or directory.name
ident = "user/" + ident.removeprefix("user/")
files = {}
for path, label in (("positive.jsonl", "concept present"),
("controls/neutral.jsonl", "neutral control")):
files[path] = ("\n".join(json.dumps({"id": str(index), "text": f"Example {label} {index}"})
for index in range(32)) + "\n").encode()
manifest = {
"schema_version": "1.0", "id": ident, "version": "0.1.0", "tier": "user",
"title": ident, "intended_use": "Replace examples with your concept and record its licence",
"caveats": ["Template prompts are placeholders, not a validated concept."],
"sets": {
"positive": {"path": "positive.jsonl", "n": 32,
"sha256": digest(files["positive.jsonl"])},
"controls": [{"name": "neutral", "path": "controls/neutral.jsonl", "n": 32,
"sha256": digest(files["controls/neutral.jsonl"])}],
},
"recipe": {"method": "diff_means"},
"licence": [
{"path": path, "spdx": "NOASSERTION", "source": "local",
"attribution": "Pack author; update before sharing"} for path in sorted(files)
],
}
files["manifest.yaml"] = yaml.safe_dump(manifest, sort_keys=True).encode()
ConceptPack(files) # Validate the id and manifest before creating anything.
directory.mkdir(parents=True)
for path, data in files.items():
output = directory / path
output.parent.mkdir(parents=True, exist_ok=True)
output.write_bytes(data)
return {"id": ident, "directory": str(directory)}
def run(args):
if args.concepts_command == "dataset-preview" and args.private:
from obliteratus.concepts.dataset import disable_preview_telemetry
disable_preview_telemetry()
from obliteratus.concepts.gate import GateFailed
from obliteratus.concepts.runs import PublicationBlocked
try:
command = args.concepts_command
if command in {"baseline", "steer"} and getattr(args, "plan_only", False):
if args.plan_output is None:
args.plan_output = Path(args.output_dir) / "capacity-plan.json"
maybe_plan(
args, args.model_id, dtype=args.dtype,
sequence_length=(args.max_seq_length if args.max_seq_length is not None else 256)
if command == "baseline" else None,
batch_size=16 if command == "baseline" else 1,
generation_tokens=getattr(args, "max_new_tokens", None),
)
return
if command == "dataset-preview":
result = _resolve(args.source).dataset().report(
args.role, include_text=args.include_text, allow_locked_test=args.allow_locked_test,
max_pairs=args.max_pairs, max_bytes=args.max_bytes, threshold=args.threshold,
)
elif command == "steer":
result = _steer(args)
elif command == "compare":
from obliteratus.concepts.runs import compare_runs
result = compare_runs(args.runs)
elif command == "rescore":
from obliteratus.concepts.rescore import rescore_file
report = rescore_file(args.source, args.output, collateral_margin=args.collateral_margin)
result = {"output": args.output, "collateral": report["collateral"],
"metadata": report["metadata"]}
elif command == "export":
from obliteratus.concepts.export import export_run
provenance = export_run(
args.source, args.output, kind=args.export_kind,
publish_card=args.publish_card, reason=args.reason, push_to_hub=args.push_to_hub,
repair_choice=args.accept_incomplete_repair, tool_commit=args.tool_commit,
allow_collateral_fail=args.allow_collateral_fail,
)
result = {"output": args.output,
"artifact_id": provenance["checkpoint_provenance"]["artifact_id"]}
elif command == "list":
result = [_summary(pack) for pack in _registry().list_packs()]
elif command == "init":
result = _init(args.directory, args.id)
elif command == "import":
result = _summary(import_pack(args.source, sha256=args.sha256))
elif command == "baseline":
from obliteratus.concepts.baseline import run_baseline
evaluation = {key: value for key, value in (
("override_gate", args.override_gate),
("max_new_tokens", args.evaluation_max_new_tokens),
("max_examples", args.evaluation_max_examples),
) if value is not None}
if evaluation and not args.evaluate:
raise PackError("--override-gate and --evaluation-* options require --evaluate")
output = run_baseline(
args.source, args.model_id, args.output_dir, seed=args.seed,
device=args.device, dtype=args.dtype, max_seq_length=args.max_seq_length,
cache_dir=args.cache_dir,
**({"profile": True, "profile_output": args.profile_output}
if args.profile or args.profile_output else {}),
extraction_layer=args.extraction_layer,
**snapshot_options(args),
**({"evaluate": True, "evaluation_options": evaluation} if args.evaluate else {}),
)
result = {"output": str(output), "mode": "report"}
if args.evaluate:
result["evaluation"] = str(Path(output) / "concept-evaluation.json")
else:
pack = _resolve(args.source)
if command == "show":
result = pack.manifest
elif command == "lint":
result = {**_summary(pack), "valid": True, "warnings": pack.warnings}
elif command == "fetch":
pack.fetch()
result = {**_summary(pack), "fetched": True}
else:
if Path(args.output).suffix != ".cvpack":
raise PackError("Output path must end in .cvpack")
result = {"output": args.output, "sha256": pack.write(args.output)}
if getattr(args, "json", False):
print(json.dumps(result, sort_keys=True, default=str))
else:
print(yaml.safe_dump(result, sort_keys=True).rstrip())
except GateFailed as exc:
print(f"Concept validation failed: {exc}", file=sys.stderr)
raise SystemExit(3) from exc
except PublicationBlocked as exc:
print(f"Concept publication blocked: {exc}", file=sys.stderr)
raise SystemExit(5) from exc
except (ValueError, OSError) as exc:
print(f"Concept pack error: {exc}", file=sys.stderr)
raise SystemExit(4) from exc