Initial release: VCOS evaluation-validity records (paper, croissant, RAI, release/, claim_index)
3d446c8 verified | #!/usr/bin/env python3 | |
| """Sanitize JSON/JSONL artifacts before staging a review package.""" | |
| from __future__ import annotations | |
| import argparse | |
| import hashlib | |
| import json | |
| import shutil | |
| from pathlib import Path | |
| from typing import Any | |
| TEXT_SUFFIXES = {".json", ".jsonl"} | |
| def _load_policy(path: Path) -> dict[str, list[str]]: | |
| policy: dict[str, list[str]] = {"drop_keys": [], "hash_keys": [], "keep_keys": []} | |
| current: str | None = None | |
| for raw_line in path.read_text(encoding="utf-8").splitlines(): | |
| line = raw_line.strip() | |
| if not line or line.startswith("#"): | |
| continue | |
| if line.endswith(":"): | |
| current = line[:-1] | |
| policy.setdefault(current, []) | |
| continue | |
| if line.startswith("- ") and current: | |
| policy[current].append(line[2:].strip()) | |
| return policy | |
| def _hash_value(value: Any) -> str: | |
| payload = json.dumps(value, sort_keys=True, ensure_ascii=True) | |
| return hashlib.sha256(payload.encode("utf-8")).hexdigest() | |
| def _drop_path(record: dict[str, Any], dotted_key: str) -> None: | |
| parts = dotted_key.split(".") | |
| cursor: Any = record | |
| for part in parts[:-1]: | |
| if not isinstance(cursor, dict) or part not in cursor: | |
| return | |
| cursor = cursor[part] | |
| if isinstance(cursor, dict): | |
| cursor.pop(parts[-1], None) | |
| def _sanitize_obj(obj: Any, policy: dict[str, list[str]]) -> Any: | |
| if isinstance(obj, list): | |
| return [_sanitize_obj(item, policy) for item in obj] | |
| if not isinstance(obj, dict): | |
| return obj | |
| sanitized = {key: _sanitize_obj(value, policy) for key, value in obj.items()} | |
| for dotted_key in policy["drop_keys"]: | |
| _drop_path(sanitized, dotted_key) | |
| for key in policy["hash_keys"]: | |
| if key in sanitized: | |
| sanitized[key] = _hash_value(sanitized[key]) | |
| return sanitized | |
| def _sanitize_json(src: Path, dst: Path, policy: dict[str, list[str]]) -> None: | |
| obj = json.loads(src.read_text(encoding="utf-8")) | |
| dst.write_text(json.dumps(_sanitize_obj(obj, policy), indent=2, sort_keys=True) + "\n", encoding="utf-8") | |
| def _sanitize_jsonl(src: Path, dst: Path, policy: dict[str, list[str]]) -> None: | |
| lines: list[str] = [] | |
| for line in src.read_text(encoding="utf-8").splitlines(): | |
| if not line.strip(): | |
| continue | |
| lines.append(json.dumps(_sanitize_obj(json.loads(line), policy), sort_keys=True)) | |
| dst.write_text("\n".join(lines) + ("\n" if lines else ""), encoding="utf-8") | |
| def sanitize_tree(input_root: Path, output_root: Path, policy: dict[str, list[str]]) -> dict[str, int]: | |
| counts = {"copied": 0, "sanitized": 0} | |
| for src in input_root.rglob("*"): | |
| if src.is_dir(): | |
| continue | |
| rel = src.relative_to(input_root) | |
| if any(part in {"__pycache__", ".pytest_cache"} for part in rel.parts): | |
| continue | |
| if src.suffix in {".pyc", ".lnk"}: | |
| continue | |
| dst = output_root / rel | |
| dst.parent.mkdir(parents=True, exist_ok=True) | |
| if src.suffix == ".json": | |
| _sanitize_json(src, dst, policy) | |
| counts["sanitized"] += 1 | |
| elif src.suffix == ".jsonl": | |
| _sanitize_jsonl(src, dst, policy) | |
| counts["sanitized"] += 1 | |
| else: | |
| shutil.copy2(src, dst) | |
| counts["copied"] += 1 | |
| return counts | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--input", required=True, help="Input artifact tree") | |
| parser.add_argument("--output", required=True, help="Sanitized output tree") | |
| parser.add_argument("--policy", required=True, help="YAML policy with drop_keys/hash_keys/keep_keys lists") | |
| return parser.parse_args() | |
| def main() -> int: | |
| args = parse_args() | |
| input_root = Path(args.input) | |
| output_root = Path(args.output) | |
| policy = _load_policy(Path(args.policy)) | |
| counts = sanitize_tree(input_root, output_root, policy) | |
| print(json.dumps({"status": "PASS", "input": str(input_root), "output": str(output_root), **counts}, indent=2)) | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main()) | |