LexBench-Web-Fetch / tools /task_population.py
gehuageyan's picture msq6's picture
Add reviewed primary-content task scope revision (#3)
46d9107
Raw History Blame
3.59 kB
"""Validate and select the proposed population without altering frozen editions.
Selection operates on already resolved task records, after applying the proposed
revision once. It never applies revisions or substitutes another suite's task.
"""
from collections import Counter
from hashlib import sha256
import json
from pathlib import Path
POPULATION = 'definitions/populations/primary-content-20261103.json'
def identities(rows):
keys = [(row['suite'], row['id']) for row in rows]
if len(keys) != len(set(keys)):
raise ValueError('Duplicate suite/task identity')
return keys
def validate(root, specification=None):
root = Path(root).resolve()
spec = specification if specification is not None else json.loads((root / POPULATION).read_text())
if spec['schema_version'] != 1 or spec['kind'] != 'task_population_selection':
raise ValueError('Unsupported population schema')
if spec['revision'] != spec['id']:
raise ValueError('Population and revision must agree')
revision_path = root / 'definitions/annotation-revisions' / (spec['revision'] + '.json')
if sha256(revision_path.read_bytes()).hexdigest() != spec.get('revision_sha256'):
raise ValueError('Population revision hash mismatch')
selected, excluded = identities(spec['cases']), identities(spec['excluded'])
if len(selected) != spec['task_count'] or dict(Counter(s for s, _ in selected)) != spec['suite_counts']:
raise ValueError('Population counts do not match selected tasks')
frozen = set()
for suite, binding in spec['frozen_task_bindings'].items():
path = (root / binding['path']).resolve()
if not path.is_relative_to(root / 'definitions'):
raise ValueError('Frozen source must be inside definitions')
raw = path.read_bytes()
if sha256(raw).hexdigest() != binding['sha256']:
raise ValueError('Frozen source hash mismatch: ' + suite)
rows = json.loads(raw)['cases']
keys = identities([{'suite': suite, 'id': row['id']} for row in rows])
frozen.update(keys)
if set(selected) & set(excluded) or set(selected) | set(excluded) != frozen:
raise ValueError('Selection and exclusions must partition the frozen tasks')
return spec
def select(root, records, revision):
"""Return selected resolved records; reject missing or unexpected inputs."""
spec = validate(root)
if revision != spec['revision']:
raise ValueError('Resolved records must use the selected revision')
records = list(records)
keys = identities(records)
wanted = identities(spec['cases'])
allowed = set(wanted) | set(identities(spec['excluded']))
if not set(wanted).issubset(keys) or not set(keys).issubset(allowed):
raise ValueError('Missing selected tasks or unexpected input tasks')
by_key = dict(zip(keys, records))
return [by_key[key] for key in wanted]
def main(argv=None):
"""Emit runner-compatible case IDs for one suite in the 530-task selection."""
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--root', type=Path, default=Path(__file__).resolve().parents[1])
parser.add_argument('--suite', required=True, help='Suite to pass to the exporter and runner')
args = parser.parse_args(argv)
spec = validate(args.root)
if args.suite not in spec['suite_counts']:
parser.error('Unknown suite: ' + args.suite)
print(','.join(row['id'] for row in spec['cases'] if row['suite'] == args.suite))
if __name__ == '__main__':
main()