"""Recover the matched original and counterfactual rows for assignment_swaps. Usage: python tools/reconstruct_assignment_swaps.py RECIPES.jsonl OUTPUT.jsonl Use --source-file UPSTREAM.jsonl to read the pinned upstream file locally. """ import argparse import copy import hashlib import json from pathlib import Path import re from urllib.request import urlopen def digest(data): return hashlib.sha256(data).hexdigest() def check(expected, actual, label): if actual != expected: raise ValueError(f"{label} differs from the pinned value") def funders_digest(funders): canonical = json.dumps(funders, sort_keys=True, ensure_ascii=False, separators=(",", ":")) return digest(canonical.encode("utf-8")) def reconstruct(recipe, record): check(recipe["doi"], record["doi"], "DOI") first, second = recipe["swapped_ids"] replacements = {first: second, second: first} pattern = rf"(?