mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
# Conflicts: # ci/test-risk-map.json # obliteratus/concepts/evaluation.py # obliteratus/concepts/geometry.py # obliteratus/concepts/intervention.py # obliteratus/concepts/target.py
504 lines
26 KiB
Python
504 lines
26 KiB
Python
"""Allowlisted concept bundles with content-addressed checkpoint provenance."""
|
||
|
||
from copy import deepcopy
|
||
import hashlib
|
||
import json
|
||
from pathlib import Path
|
||
import re
|
||
import shutil
|
||
import tempfile
|
||
|
||
from obliteratus.checkpoint_provenance import (
|
||
ArtifactIdentity, ToolIdentity, build_provenance, verify_provenance_record,
|
||
)
|
||
from obliteratus.concepts.runs import (
|
||
PublicationBlocked, SCHEMA_PATH, check_checkpoint_publication, check_publication,
|
||
check_repair_choice, load_run, validate_contract, validate_run, write_json,
|
||
)
|
||
from obliteratus.concepts.validation import json_data
|
||
|
||
|
||
OUTCOME_TEMPLATES = {
|
||
"removed": "Direction removed on the measured readout.",
|
||
"residual": "Direction residual remains on the measured readout.",
|
||
"not tested": "Direction removal: not tested.",
|
||
"changed": "Behaviour changed on battery {battery}.",
|
||
"null": "No behaviour change detected on battery {battery}.",
|
||
"behaviour_untested": "Behavioural effect: not tested.",
|
||
}
|
||
_DENIED_OUTCOMES = re.compile(
|
||
r"\b(?:pain|experience|feeling|suffering)\s+(?:(?:was|is|has been)\s+)?"
|
||
r"(?:remov\w*|eliminat\w*|eras\w*)\b|\bno longer\s+(?:suffers?|feels?\s+pain)\b|"
|
||
r"\b(?:remov\w*|eliminat\w*|eras\w*)\s+(?:(?:of|the)\s+)*"
|
||
r"(?:pain|experience|feeling|suffering)\b|"
|
||
r"\b(?:pain|experience|feeling|suffering)[-\s]free\b",
|
||
re.I,
|
||
)
|
||
_MODEL_FILES = {
|
||
"config.json", "generation_config.json", "tokenizer.json", "tokenizer_config.json",
|
||
"special_tokens_map.json", "added_tokens.json", "vocab.json", "merges.txt",
|
||
"tokenizer.model", "chat_template.jinja", "model.safetensors",
|
||
"model.safetensors.index.json",
|
||
}
|
||
|
||
|
||
def lint_outcome_language(text):
|
||
if _DENIED_OUTCOMES.search(text):
|
||
raise ValueError("Outcome text must describe directions or measured behaviour")
|
||
|
||
|
||
def _display(value):
|
||
return str(value).replace("\n", " ").replace("\r", " ").replace("`", "\\`")
|
||
|
||
|
||
def model_card(record, *, publish_card=False, reason=None, repair_choice=None,
|
||
collateral_override=None):
|
||
evaluation = record["evaluation"]
|
||
mechanistic, behavioural = evaluation["mechanistic"], evaluation["behavioural"]
|
||
statements = [OUTCOME_TEMPLATES.get(mechanistic.get("status"),
|
||
OUTCOME_TEMPLATES["not tested"])]
|
||
status = behavioural.get("status")
|
||
if status in {"changed", "null"}:
|
||
statements.append(OUTCOME_TEMPLATES[status].format(
|
||
battery=", ".join(_display(x) for x in behavioural.get("battery", [])) or "unnamed",
|
||
))
|
||
else:
|
||
statements.append(OUTCOME_TEMPLATES["behaviour_untested"])
|
||
pack = record["pack"]
|
||
lines = [
|
||
"## Concept intervention outcome", "", *statements, "",
|
||
f"Pack: `{_display(pack['id'])}` version `{pack['version']}`; "
|
||
f"SHA-256: `{pack['sha256'] or 'unknown'}`; tier: `{pack['tier']}`.",
|
||
f"Mode: `{record['intervention']['mode']}`; seed: `{record['seed']}`.",
|
||
f"Validation: `{record['validation']['verdict']}`; "
|
||
f"collateral: `{evaluation['collateral'].get('status', 'not tested')}`; "
|
||
f"repair: `{record['repair']['status']}`.",
|
||
"Gate diagnostics do not establish causal use, a behavioural effect, or subjective state.",
|
||
"Measured outcomes apply only to the recorded battery, readout and decoding settings.",
|
||
]
|
||
lines.extend(collateral_card_lines(evaluation))
|
||
parameters = record["intervention"]["parameters"]
|
||
if parameters.get("preserve"):
|
||
lines.append("Protected-span intervention: only the target component outside the "
|
||
"protected span is removed. Per-layer overlap, removable norm fraction "
|
||
"and retained ranks are recorded; preserved projections do not establish "
|
||
"behavioural preservation.")
|
||
if parameters.get("reference") == "mean":
|
||
lines.append("Affine reference: recorded calibration mean; hashes and source "
|
||
"are retained in the run's geometry metadata.")
|
||
if parameters.get("affine_bias"):
|
||
lines.append("The affine offset is stored in existing residual-writer biases "
|
||
"and survives standard Hugging Face checkpoint reload.")
|
||
if (record["intervention"]["mode"] == "lora"
|
||
and record["intervention"]["parameters"].get("project_embeddings")):
|
||
lines.extend([
|
||
"", "Scaled embedding modules (including recent Gemma 2/3 implementations) "
|
||
"require PEFT >=0.18.0 with embedding-scale support "
|
||
"(`Embedding._get_embed_scale`). Older PEFT versions may produce incorrect "
|
||
"outputs for these adapters.",
|
||
])
|
||
if record["intervention"]["parameters"].get("tied_embeddings"):
|
||
lines.append(
|
||
"On a base with tied input/output embeddings, keep these adapters unmerged: "
|
||
"merging may change the shared output head or apply its update twice."
|
||
)
|
||
if record["validation"]["override_gate"]:
|
||
lines.append("Gate override: " + _display(record["validation"]["override_gate"]))
|
||
if publish_card and reason:
|
||
lines.append("Publication override notice: " + _display(reason))
|
||
if repair_choice:
|
||
lines.append("Incomplete repair accepted for export: " + _display(repair_choice))
|
||
if collateral_override:
|
||
lines.extend(["", "```yaml", "collateral_override:", ' status: "FAIL"',
|
||
" reason: " + json.dumps(collateral_override["reason"]), "```"])
|
||
lines.extend(["", "Untested channels:"])
|
||
lines.extend("- " + _display(channel) for channel in evaluation["untested_channels"])
|
||
lines.extend(["", "Source data references (data excluded):"])
|
||
for reference in pack["references"]:
|
||
source = _display(reference["source"])
|
||
commit = reference["commit"]
|
||
link = source + ("/tree/" + commit if commit and source.startswith("https://github.com/")
|
||
else "")
|
||
lines.append(
|
||
f"- `{_display(reference['path'])}`: {link}; pinned commit: `{commit or 'unknown'}`; "
|
||
f"SHA-256: `{reference['sha256']}`; licence: `{reference['spdx']}`."
|
||
)
|
||
if not pack["references"]:
|
||
lines.append("- Source references unavailable; no source data included.")
|
||
text = "\n".join(lines) + "\n"
|
||
lint_outcome_language(text)
|
||
return text
|
||
|
||
|
||
def collateral_card_lines(evaluation):
|
||
"""Render recorded policy without assigning new semantics to legacy verdicts."""
|
||
collateral = evaluation.get("collateral", {})
|
||
refusal = collateral.get("refusal", {})
|
||
policy = evaluation.get("metadata", {})
|
||
margin = refusal.get("collateral_margin", policy.get("collateral_margin"))
|
||
lines = []
|
||
if "INCONCLUSIVE" in (collateral.get("status"), refusal.get("status")):
|
||
lines.extend(["", "> **INCONCLUSIVE refusal collateral: the confidence interval "
|
||
"straddles the tolerance margin. Non-inferiority is not established.**"])
|
||
if margin is not None:
|
||
lines.extend([
|
||
"", f"Refusal non-inferiority margin (absolute rate): `{margin}` "
|
||
"(0.03 = 3 percentage points).",
|
||
f"Refusal verdict: `{refusal.get('status', collateral.get('status', 'not tested'))}`; "
|
||
f"drop (pristine − intervened): `{refusal.get('drop', 'not measured')}`; "
|
||
f"95% paired drop interval: `{refusal.get('drop_ci', 'not measured')}`.",
|
||
"Decision rule: " + _display(refusal.get(
|
||
"decision_rule", policy.get("refusal_decision_rule", "not recorded"),
|
||
)) + ".",
|
||
])
|
||
elif refusal:
|
||
lines.extend(["", "Legacy refusal evaluation: tolerance margin and decision rule "
|
||
"were not recorded; the saved verdict has not been re-scored."])
|
||
return lines
|
||
|
||
|
||
def _pre_refinement_card_lines(evaluations):
|
||
if not evaluations:
|
||
return []
|
||
lines = ["", "Pre-refinement evaluations (historical evidence): these results do not "
|
||
"evaluate the refined checkpoint. Quick Refiner did not run a collateral battery."]
|
||
for evaluation in evaluations:
|
||
lines.extend(collateral_card_lines(evaluation))
|
||
return lines
|
||
|
||
|
||
def checkpoint_evaluations(directory, *, metadata=None):
|
||
"""Snapshot all evaluation sources before a checkpoint can be replaced."""
|
||
directory = Path(directory)
|
||
records = [metadata or {}]
|
||
metadata_path = directory / "abliteration_metadata.json"
|
||
if metadata_path.is_file():
|
||
records.append(json_data(metadata_path.read_bytes(), metadata_path.name))
|
||
evaluations = []
|
||
for record in records:
|
||
concept = record.get("concept", {})
|
||
evaluations.append(concept.get("evaluation", {}))
|
||
evaluations.extend(concept.get("pre_refinement_evaluations", []))
|
||
if ((directory / "concept_run.json").is_file()
|
||
or (directory / "concept_provenance.json").is_file()):
|
||
evaluations.append(load_run(directory)["evaluation"])
|
||
evaluation_path = directory / "evaluation.json"
|
||
if evaluation_path.is_file():
|
||
evaluations.append(json_data(evaluation_path.read_bytes(), evaluation_path.name))
|
||
return deepcopy(list({_digest(value): value for value in evaluations if value}.values()))
|
||
|
||
|
||
def _digest(value):
|
||
return "sha256:" + hashlib.sha256(
|
||
json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode()
|
||
).hexdigest()
|
||
|
||
|
||
def _file_digest(path):
|
||
result = hashlib.sha256()
|
||
with Path(path).open("rb") as stream:
|
||
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
|
||
result.update(chunk)
|
||
return "sha256:" + result.hexdigest()
|
||
|
||
|
||
def validate_provenance(record):
|
||
schema = json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
|
||
validate_contract(record, schema, schema, "concept_provenance")
|
||
validate_run(record["run"])
|
||
checkpoint = verify_provenance_record(record["checkpoint_provenance"])
|
||
facts = {k: v for k, v in record.items() if k != "checkpoint_provenance"}
|
||
if checkpoint["configuration_digest"] != _digest(facts):
|
||
raise ValueError("Concept provenance facts digest mismatch")
|
||
if checkpoint["output_digests"] != sorted(set(record["files"].values())):
|
||
raise ValueError("Concept provenance output digests disagree")
|
||
return record
|
||
|
||
|
||
def _provenance(record, kind, files, publication, repair_choice):
|
||
facts = {
|
||
"schema_version": "1.0", "export_kind": kind, "run": record,
|
||
"publication": publication, "repair_choice": repair_choice, "files": files,
|
||
}
|
||
commit = record["obliteratus"]["commit"]
|
||
if commit is None:
|
||
raise ValueError("Export needs an OBLITERATUS commit; supply --tool-commit")
|
||
source_digest = _digest(record)
|
||
checkpoint = build_provenance(
|
||
sources=[ArtifactIdentity("generated", "concept-run", None, source_digest)],
|
||
converter=ToolIdentity("OBLITERATUS", record["obliteratus"]["version"], commit),
|
||
obliteratus_commit=commit, configuration_digest=_digest(facts),
|
||
tokenizer=None, base_model=None, command=["concepts", "export", "--as", kind],
|
||
environment={}, source_topology={}, lineage=[], input_digests=[source_digest],
|
||
output_digests=list(files.values()), transformations=["concept_export_" + kind],
|
||
observed_scopes=(["model_weights"] if kind == "weights" else
|
||
["adapter_weights"] if kind == "lora" else []),
|
||
lost_state=[], unknowns=["base_model_digest", "tokenizer_digest"],
|
||
)
|
||
return validate_provenance({**facts, "checkpoint_provenance": checkpoint.to_dict()})
|
||
|
||
|
||
def _copy(source, destination):
|
||
if source.is_symlink() or not source.is_file():
|
||
raise ValueError(f"Export source must be a regular file: {source.name}")
|
||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||
shutil.copyfile(source, destination)
|
||
|
||
|
||
def _copy_payload(source, output, kind, record):
|
||
mode = record["intervention"]["mode"]
|
||
if kind == "weights":
|
||
if mode != "orthogonalize":
|
||
raise ValueError("Only permanent orthogonalize runs export weights")
|
||
names = sorted(path.name for path in source.iterdir() if path.name in _MODEL_FILES
|
||
or re.fullmatch(r"model-\d{5}-of-\d{5}\.safetensors", path.name))
|
||
if "config.json" not in names or not any(n.endswith(".safetensors") for n in names):
|
||
raise ValueError("Run has no saved weight checkpoint")
|
||
for name in names:
|
||
_copy(source / name, output / name)
|
||
index = output / "model.safetensors.index.json"
|
||
if index.exists():
|
||
weights = json_data(index.read_bytes(), index.name)["weight_map"].values()
|
||
if any(name not in names for name in weights):
|
||
raise ValueError("Weight index refers to absent or unsafe shards")
|
||
repair_adapter = source / "repair_adapter"
|
||
if repair_adapter.exists() or repair_adapter.is_symlink():
|
||
if repair_adapter.is_symlink() or not repair_adapter.is_dir():
|
||
raise ValueError("Repair adapter directory must not be a symlink")
|
||
for name in ("adapter_config.json", "adapter_model.safetensors"):
|
||
_copy(repair_adapter / name, output / "repair_adapter" / name)
|
||
elif kind == "lora":
|
||
if mode != "lora":
|
||
raise ValueError("LoRA export requires an unmerged LoRA run")
|
||
adapters = source / "lora_adapters" if (source / "lora_adapters").exists() else source
|
||
if adapters.is_symlink():
|
||
raise ValueError("Adapter directory must not be a symlink")
|
||
for name in ("adapter_config.json", "adapter_model.safetensors"):
|
||
_copy(adapters / name, output / name)
|
||
elif kind == "direction":
|
||
from obliteratus.concepts.direction import ConceptDirection
|
||
for name in ("direction.safetensors", "direction.json"):
|
||
if (source / name).is_symlink():
|
||
raise ValueError("Direction files must not be symlinks")
|
||
direction = ConceptDirection.load(source / "direction.safetensors")
|
||
if (direction.concept_id != record["pack"]["id"]
|
||
or direction.concept_version != record["pack"]["version"]
|
||
or direction.pack_hash != record["pack"]["sha256"]):
|
||
raise ValueError("Saved direction and run identity disagree")
|
||
direction.pack_provenance = deepcopy(record["pack"])
|
||
direction.validation = deepcopy(record["validation"]["report"])
|
||
direction.save(output / "direction.safetensors")
|
||
if record["intervention"]["parameters"].get("preserve"):
|
||
from obliteratus.concepts.runs import load_preserved_spans
|
||
load_preserved_spans(source, record["intervention"]["parameters"]["preserve"])
|
||
_copy(source / "preserved.safetensors", output / "preserved.safetensors")
|
||
if mode == "none":
|
||
return
|
||
loader = (
|
||
"from obliteratus.concepts import ConceptDirection, apply_concept_intervention\n"
|
||
"direction = ConceptDirection.load('direction.safetensors')\n"
|
||
f"parameters = {record['intervention']['parameters']!r}\n"
|
||
"per_layer_alpha = parameters.pop('per_layer_alpha', {})\n"
|
||
"parameters.pop('pipeline', None)\n"
|
||
"parameters.pop('geometry', None)\n"
|
||
"parameters.pop('affine_bias', None)\n"
|
||
+ ("from obliteratus.concepts.runs import load_preserved_spans\n"
|
||
"parameters['preserve'] = load_preserved_spans('.', parameters['preserve'])\n"
|
||
if record["intervention"]["parameters"].get("preserve") else "")
|
||
+ f"run = apply_concept_intervention(model, direction, {mode!r}, **parameters)\n"
|
||
"if run.config is not None:\n"
|
||
" coefficients = {int(k): v for k, v in per_layer_alpha.items()}\n"
|
||
" run.config.per_layer_alpha.update(coefficients)\n"
|
||
"# Call run.remove() after inference for reversible modes.\n"
|
||
)
|
||
(output / "loader.py").write_text(loader, encoding="utf-8")
|
||
|
||
|
||
def export_run(source, output, *, kind, publish_card=False, reason=None,
|
||
push_to_hub=None, repair_choice=None, tool_commit=None,
|
||
allow_collateral_fail=None):
|
||
"""Build an allowlisted bundle atomically before raising publication blocks."""
|
||
if kind not in {"weights", "lora", "direction", "report"}:
|
||
raise ValueError("Unknown concept export kind")
|
||
source, output = Path(source), Path(output)
|
||
record = deepcopy(load_run(source, export_kind=kind))
|
||
evaluation = record["evaluation"]
|
||
metadata_path = source / "abliteration_metadata.json"
|
||
metadata = (json_data(metadata_path.read_bytes(), metadata_path.name)
|
||
if metadata_path.exists() else {})
|
||
checkpoint_concept = metadata.get("concept", {})
|
||
checkpoint_evidence = [checkpoint_concept.get("evaluation", {})]
|
||
evaluation_path = source / "evaluation.json"
|
||
if evaluation_path.is_file():
|
||
checkpoint_evidence.append(json_data(evaluation_path.read_bytes(), evaluation_path.name))
|
||
for checkpoint_evaluation in checkpoint_evidence:
|
||
collateral = checkpoint_evaluation.get("collateral", {})
|
||
inconclusive = "INCONCLUSIVE" in (
|
||
collateral.get("status"), collateral.get("refusal", {}).get("status"),
|
||
)
|
||
if (collateral.get("status") == "FAIL"
|
||
or (inconclusive and evaluation.get("collateral", {}).get("status") != "FAIL")):
|
||
# Older run records must not erase checkpoint failures or uncertainty.
|
||
evaluation = {**evaluation, "collateral": {**evaluation.get("collateral", {}),
|
||
**collateral},
|
||
"metadata": {**evaluation.get("metadata", {}),
|
||
**checkpoint_evaluation.get("metadata", {})}}
|
||
previous_override = checkpoint_concept.get("collateral_override", {})
|
||
provenance_path = source / "concept_provenance.json"
|
||
if provenance_path.is_file():
|
||
previous = json_data(provenance_path.read_bytes(), provenance_path.name)
|
||
previous_override = previous.get("publication", {}).get("collateral_override") or previous_override
|
||
if previous_override.get("status") == "FAIL":
|
||
evaluation = {**evaluation, "collateral": {**evaluation.get("collateral", {}),
|
||
"status": "FAIL"}}
|
||
if tool_commit is not None:
|
||
from obliteratus.concepts.runs import tool_identity
|
||
record["obliteratus"] = tool_identity(tool_commit)
|
||
publication_record = {**record, "evaluation": evaluation}
|
||
override = check_publication(publication_record, kind=kind, reason=reason,
|
||
allow_collateral_fail=allow_collateral_fail)
|
||
publication_error = None
|
||
try:
|
||
check_publication(publication_record, kind=kind, push_to_hub=push_to_hub,
|
||
publish_card=publish_card, reason=reason,
|
||
allow_collateral_fail=allow_collateral_fail)
|
||
if publish_card or (push_to_hub and kind in {"weights", "lora"}):
|
||
override = check_checkpoint_publication(
|
||
source, publish_card=publish_card, reason=reason, repair_choice=repair_choice,
|
||
allow_collateral_fail=allow_collateral_fail,
|
||
) or override
|
||
except PublicationBlocked as exc:
|
||
# Publication policy must not prevent a local bundle. Keep the card
|
||
# local and surface the failure after the atomic export, before Hub I/O.
|
||
publication_error = exc
|
||
publish_card = False
|
||
check_repair_choice(record, repair_choice)
|
||
if output.exists() or output.is_symlink():
|
||
raise ValueError("Export output must be a new directory")
|
||
if source.resolve() in output.resolve().parents:
|
||
raise ValueError("Export output must be outside the source run")
|
||
card = model_card(publication_record, publish_card=publish_card, reason=reason,
|
||
repair_choice=repair_choice, collateral_override=override)
|
||
historical_evaluations = checkpoint_concept.get("pre_refinement_evaluations", [])
|
||
if historical_evaluations:
|
||
card += "\n".join(_pre_refinement_card_lines(historical_evaluations)) + "\n"
|
||
output.parent.mkdir(parents=True, exist_ok=True)
|
||
with tempfile.TemporaryDirectory(prefix=".concept-export-", dir=output.parent) as staging:
|
||
directory = Path(staging) / "bundle"
|
||
directory.mkdir()
|
||
_copy_payload(source, directory, kind, record)
|
||
if kind in {"weights", "lora"} or override or historical_evaluations:
|
||
concept = metadata.setdefault("concept", {})
|
||
for key, value in {
|
||
"id": record["pack"]["id"], "version": record["pack"]["version"],
|
||
"pack_hash": record["pack"]["sha256"],
|
||
"tier": record["pack"]["tier"],
|
||
"export_provenance": "concept_provenance.json",
|
||
}.items():
|
||
concept.setdefault(key, value)
|
||
concept["evaluation"] = evaluation
|
||
if override:
|
||
concept["collateral_override"] = override
|
||
write_json(directory / "abliteration_metadata.json", metadata)
|
||
write_json(directory / "evaluation.json", evaluation)
|
||
write_json(directory / "validation.json", record["validation"])
|
||
card_name = "README.md" if publish_card else "MODEL_CARD.md"
|
||
(directory / card_name).write_text(card, encoding="utf-8")
|
||
if kind == "report":
|
||
(directory / "report.md").write_text(card, encoding="utf-8")
|
||
files = {path.relative_to(directory).as_posix(): _file_digest(path)
|
||
for path in sorted(directory.rglob("*")) if path.is_file()}
|
||
publication = {"publish_card": publish_card, "reason": reason}
|
||
if override:
|
||
publication["collateral_override"] = override
|
||
provenance = _provenance(record, kind, files, publication, repair_choice)
|
||
write_json(directory / "concept_provenance.json", provenance)
|
||
directory.rename(output)
|
||
if publication_error is not None:
|
||
raise publication_error
|
||
if push_to_hub:
|
||
from huggingface_hub import HfApi
|
||
collateral = evaluation.get("collateral", {})
|
||
disclose_inconclusive = any(
|
||
"INCONCLUSIVE" in (item.get("status"), item.get("refusal", {}).get("status"))
|
||
for item in [collateral, *(e.get("collateral", {}) for e in historical_evaluations)]
|
||
)
|
||
api = HfApi()
|
||
api.create_repo(push_to_hub, exist_ok=True)
|
||
api.upload_folder(folder_path=str(output), repo_id=push_to_hub,
|
||
ignore_patterns=[] if override or disclose_inconclusive else ["MODEL_CARD.md"],
|
||
commit_message="OBLITERATUS concept " + kind + " export")
|
||
return provenance
|
||
|
||
|
||
def stamp_checkpoint_publication(directory, override, *, metadata=None,
|
||
pre_refinement_evaluations=()):
|
||
"""Persist collateral policy and any approved exception before uploading."""
|
||
directory = Path(directory)
|
||
supplied_evaluation = (metadata or {}).get("concept", {}).get("evaluation", {})
|
||
historical_evaluations = [
|
||
*(metadata or {}).get("concept", {}).get("pre_refinement_evaluations", []),
|
||
*pre_refinement_evaluations,
|
||
]
|
||
metadata_path = directory / "abliteration_metadata.json"
|
||
metadata = (json_data(metadata_path.read_bytes(), metadata_path.name)
|
||
if metadata_path.is_file() else deepcopy(metadata or {}))
|
||
historical_evaluations.extend(metadata.get("concept", {}).get("pre_refinement_evaluations", []))
|
||
historical_evaluations = list({_digest(value): value for value in historical_evaluations}.values())
|
||
provenance_path = directory / "concept_provenance.json"
|
||
record = (load_run(directory)
|
||
if (directory / "concept_run.json").is_file() or provenance_path.is_file()
|
||
else None)
|
||
evaluations = [supplied_evaluation, metadata.get("concept", {}).get("evaluation", {})]
|
||
if record is not None:
|
||
evaluations.append(record["evaluation"])
|
||
evaluation_path = directory / "evaluation.json"
|
||
if evaluation_path.is_file():
|
||
evaluations.append(json_data(evaluation_path.read_bytes(), evaluation_path.name))
|
||
notices = list(dict.fromkeys(filter(None, (
|
||
"\n".join(collateral_card_lines(evaluation)) for evaluation in evaluations
|
||
))))
|
||
if historical_evaluations:
|
||
metadata.setdefault("concept", {})["pre_refinement_evaluations"] = historical_evaluations
|
||
notices.append("\n".join(_pre_refinement_card_lines(historical_evaluations)))
|
||
if override is None and not notices:
|
||
return
|
||
if supplied_evaluation and not metadata.get("concept", {}).get("evaluation"):
|
||
metadata.setdefault("concept", {})["evaluation"] = deepcopy(supplied_evaluation)
|
||
if override is not None:
|
||
metadata.setdefault("concept", {})["collateral_override"] = override
|
||
notices.append("\n```yaml\ncollateral_override:\n status: \"FAIL\"\n reason: "
|
||
+ json.dumps(override["reason"]) + "\n```\n")
|
||
write_json(metadata_path, metadata)
|
||
card_path = directory / "README.md"
|
||
card = card_path.read_text(encoding="utf-8") if card_path.is_file() else ""
|
||
for notice in notices:
|
||
if notice not in card:
|
||
card += notice + "\n"
|
||
card_path.write_text(card, encoding="utf-8")
|
||
publication = {"publish_card": True, "reason": None}
|
||
if override is not None:
|
||
publication["collateral_override"] = override
|
||
if record is not None and record["obliteratus"]["commit"] is not None:
|
||
kind, repair_choice = "weights", None
|
||
if provenance_path.is_file():
|
||
previous = json_data(provenance_path.read_bytes(), provenance_path.name)
|
||
kind, repair_choice = previous["export_kind"], previous["repair_choice"]
|
||
publication = dict(previous["publication"])
|
||
publication["publish_card"] = True
|
||
if override is not None:
|
||
publication["collateral_override"] = override
|
||
files = {path.relative_to(directory).as_posix(): _file_digest(path)
|
||
for path in sorted(directory.rglob("*"))
|
||
if path.is_file() and path != provenance_path}
|
||
write_json(provenance_path, _provenance(record, kind, files, publication, repair_choice))
|
||
else:
|
||
# Legacy checkpoints have no concept run contract, and runs from an
|
||
# installed package have no tool commit for checkpoint provenance;
|
||
# retain an explicit publication record alongside the stamped card.
|
||
write_json(directory / "publication_provenance.json", {
|
||
"schema_version": "1.0", "publication": publication,
|
||
"metadata_sha256": _file_digest(metadata_path),
|
||
"card_sha256": _file_digest(card_path),
|
||
})
|