"""Read a task statement and verify its published record bindings.""" import hashlib import json from pathlib import Path import re from common import ROOT, contained _cached_index = None def _signature(path): stat = path.stat() return stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns def _index(root): global _cached_index root = Path(root).resolve() names = ['data/crossswe-bench.jsonl', 'data/task-bindings.jsonl'] paths = [root / name for name in names] checks = root / 'CHECKSUMS.sha256' signature = tuple(_signature(path) for path in [*paths, checks]) expected = {} for line in checks.read_text().splitlines(): digest, name = line.split(' ', 1) if name in names: if name in expected: raise ValueError('Duplicate dataset checksum entry') expected[name] = digest if set(expected) != set(names): raise ValueError('Missing dataset checksum entry') payloads = [path.read_bytes() for path in paths] digests = [hashlib.sha256(raw).hexdigest() for raw in payloads] for name, digest in zip(names, digests): if digest != expected[name]: raise ValueError('Dataset checksum does not match') if signature != tuple(_signature(path) for path in [*paths, checks]): raise ValueError('Dataset files changed while reading') key = root, *digests if _cached_index is not None and _cached_index[0] == key: return _cached_index[1] bindings = {} for line in payloads[1].decode('utf-8').splitlines(): item = json.loads(line) iid = item['instance_id'] if iid in bindings: raise ValueError('Duplicate task binding') for field in ['task_sha256', 'source_task_sha256']: if not isinstance(item[field], str) or not re.fullmatch('[0-9a-f]{64}', item[field]): raise ValueError('Invalid task binding checksum') bindings[iid] = item index = {} for line in payloads[0].decode('utf-8').splitlines(): row = json.loads(line) iid = row['instance_id'] text = row['problem_statement'] if not isinstance(iid, str) or not iid or iid in index: raise ValueError('Invalid or duplicate dataset instance ID') contained(root / 'instances', iid) if not isinstance(text, str) or not text.strip(): raise ValueError('Missing problem statement') if iid not in bindings or bindings[iid]['source_task_sha256'] != row['original_task_sha256']: raise ValueError('Source task binding does not match') index[iid] = text, bindings[iid]['task_sha256'], 'statements/' + iid + '.md' if set(index) != set(bindings): raise ValueError('Dataset and task-binding IDs differ') _cached_index = key, index return index def read_problem_statement(task, root=ROOT): root = Path(root).resolve() iid = task['instance_id'] task_path = contained(root / 'instances', iid) / 'task.json' try: text, expected, statement_path = _index(root)[iid] except KeyError as error: raise ValueError('Task is absent from the dataset') from error raw = task_path.read_bytes() if hashlib.sha256(raw).hexdigest() != expected or json.loads(raw) != task: raise ValueError('Published task checksum does not match') if contained(root, statement_path).read_text(encoding='utf-8') != text: raise ValueError('Statement file does not match the dataset') return text