FinGovBench / scripts /build_release.py
redqueen77's picture
Expand FinGovBench rc4 to 3,000 cases and add controlled evaluation
5e5bb15 verified
Raw History Blame Contribute Delete
6.57 kB
#!/usr/bin/env python3
"""Build the sealed, Hugging Face-ready FinGovBench v1 release candidate."""
from __future__ import annotations
import hashlib
import json
from collections import Counter, defaultdict
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SOURCES = [
ROOT / "data/sec_revisions_v1.jsonl",
ROOT / "data/controlled_structural_v1.jsonl",
ROOT / "data/delegated_action_v1.jsonl",
]
def rows(path: Path) -> list[dict]:
return [json.loads(line) for line in path.read_text().splitlines() if line.strip()]
def sha256(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def write_jsonl(path: Path, records: list[dict]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text("".join(json.dumps(record, sort_keys=True) + "\n" for record in records))
def public_record(record: dict, *, include_telemetry: bool) -> dict:
result = {key: value for key, value in record.items() if key not in {"gold", "post_action_observation"}}
# Generator bookkeeping is never a method input. In particular, controlled
# fault names and topology indices would leak the answer-key construction.
result.pop("controlled_metadata", None)
result["workflow_context"] = result.pop("ddas")
graph = result.pop("graph")
# The public object is a method-neutral workflow declaration. SSEG,
# flat monitors, rules engines and other methods must build their own
# internal representation from the same components and dependencies.
result["workflow"] = {
"representation": "declared_workflow_dependency_map_v1",
"components": graph["nodes"],
"dependencies": graph["edges"],
}
if not include_telemetry:
result.pop("telemetry", None)
# Construction flags are labels, not method inputs.
result["event"] = {
key: value
for key, value in result["event"].items()
if key != "controlled_fault"
}
result["label_status"] = "sealed"
result["task_mode"] = "propagation" if include_telemetry else "closed_loop"
return result
def label_record(record: dict) -> dict:
gold = dict(record["gold"])
gold["affected_components"] = gold.pop("affected_nodes")
gold["revalidation_components"] = gold.pop("revalidation_scope")
return {
"case_id": record["case_id"],
"event_group_id": record["event_group_id"],
"split": record["split"],
"gold": gold,
"post_action_observation": record["post_action_observation"],
"evaluation_strata": {
"origin": record["case_origin"],
"track": record["track"],
"domain": record["domain"],
"scenario": (
record.get("controlled_metadata", {}).get("counterfactual_archetype", "controlled")
if record["case_origin"] == "controlled"
else record.get("revision_metadata", {}).get("revision_kind", "reported_value_revision")
),
},
"label_basis": (
"official_sec_reported_value_pair_and_declared_workflow_contract_pending_independent_adjudication"
if record["case_origin"] == "genuine_revision"
else "prespecified_controlled_event_and_declared_workflow_contract"
),
}
def main() -> None:
records = [record for path in SOURCES for record in rows(path)]
if len(records) != 3000:
raise SystemExit(f"expected 3000 cases, found {len(records)}")
ids = [record["case_id"] for record in records]
if len(ids) != len(set(ids)):
raise SystemExit("duplicate case IDs")
group_splits: dict[str, set[str]] = defaultdict(set)
authority_splits: dict[str, set[str]] = defaultdict(set)
for record in records:
group_splits[record["event_group_id"]].add(record["split"])
authority_splits[record["ddas"]["authority_id"]].add(record["split"])
leaking_groups = {group: sorted(splits) for group, splits in group_splits.items() if len(splits) > 1}
leaking_authorities = {group: sorted(splits) for group, splits in authority_splits.items() if len(splits) > 1}
if leaking_groups or leaking_authorities:
raise SystemExit(f"split leakage: groups={leaking_groups}, authorities={leaking_authorities}")
records.sort(key=lambda record: record["case_id"])
release = ROOT / "data/fingovbench_v1.jsonl"
write_jsonl(release, records)
for split in ("development", "validation", "test"):
selected = [record for record in records if record["split"] == split]
write_jsonl(ROOT / f"hf/closed_loop_inputs/{split}.jsonl", [public_record(record, include_telemetry=False) for record in selected])
write_jsonl(ROOT / f"hf/propagation_inputs/{split}.jsonl", [public_record(record, include_telemetry=True) for record in selected])
label_root = ROOT / ("private/sealed_labels" if split == "test" else "hf/public_labels")
write_jsonl(label_root / f"{split}.jsonl", [label_record(record) for record in selected])
artifacts = [release, *sorted((ROOT / "hf/closed_loop_inputs").glob("*.jsonl")), *sorted((ROOT / "hf/propagation_inputs").glob("*.jsonl")), *sorted((ROOT / "hf/public_labels").glob("*.jsonl")), *sorted((ROOT / "private/sealed_labels").glob("*.jsonl"))]
manifest = {
"benchmark": "FinGovBench",
"version": "1.0.0-rc4",
"cases": len(records),
"origins": dict(Counter(record["case_origin"] for record in records)),
"domains": dict(Counter(record["domain"] for record in records)),
"splits": dict(Counter(record["split"] for record in records)),
"iceberg_visibility": dict(Counter(record["gold"]["iceberg_visibility"] for record in records)),
"governance_actions": dict(Counter(record["gold"]["governance_action"] for record in records)),
"event_groups": len(group_splits),
"authorities": len(authority_splits),
"group_leakage": leaking_groups,
"authority_leakage": leaking_authorities,
"source_tracks": [str(path.relative_to(ROOT)) for path in SOURCES],
"checksums": {str(path.relative_to(ROOT)): sha256(path) for path in artifacts},
"label_status": "algorithmically verified; external domain-expert adjudication pending",
"benchmark_method_dependency": "none; SSEG is an optional evaluated method",
}
(ROOT / "release_manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n")
print(json.dumps(manifest, indent=2, sort_keys=True))
if __name__ == "__main__":
main()