"""Reproduce the fixed Korean-keyword examples against the pinned source.

Python 3.9+, standard library only. Run: python reproduce.py
Downloads one public source file, verifies SHA-256, extracts only the
ForbiddenWords.check_following method, then prints exact example verdicts.
Does not call a model or measure leaderboard effects.
Demo code: Byungwoong Yoo / Woong Works, with AI assistance. MIT license.
Fetched upstream source: Allganize IFEval-Ko (dataset metadata: Apache-2.0).
"""
import ast
import hashlib
import json
import re
import urllib.request

SOURCE = 'https://huggingface.co/datasets/allganize/IFEval-Ko/resolve/54199e3801116897697babf341865741dcd06fc8/ifeval_ko/instructions.py'
SHA256 = 'afdbf1e2cafe8f3a1480d36aeff245365e1f896bacd87cd3c1ea48278e673842'
EXAMPLES = ['열쇠', '열쇠는', '열쇠의', '열쇠를', '{"수수께끼":"열쇠는 작은 몸으로 문을 엽니다."}']

def upstream_checker(source_bytes):
    actual = hashlib.sha256(source_bytes).hexdigest()
    if actual != SHA256:
        raise ValueError('Pinned source hash mismatch: ' + actual)
    tree = ast.parse(source_bytes.decode('utf-8'))
    cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'ForbiddenWords')
    method = next(n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name == 'check_following')
    module = ast.fix_missing_locations(ast.Module(body=[method], type_ignores=[]))
    namespace = {'re': re}
    exec(compile(module, '<pinned ForbiddenWords.check_following>', 'exec'), namespace)
    instance = type('FixedKeyword', (), {'_forbidden_words': ['열쇠']})()
    return lambda text: bool(namespace['check_following'](instance, text))

def main():
    with urllib.request.urlopen(SOURCE, timeout=30) as response:
        source_bytes = response.read()
    checker = upstream_checker(source_bytes)
    rows = [{'word': '열쇠', 'text': text, 'official_pass': checker(text), 'literal_contains': '열쇠' in text} for text in EXAMPLES]
    assert [r['official_pass'] for r in rows] == [False, True, True, True, True]
    # ASCII-escaped output is robust to legacy Windows console encodings.
    print(json.dumps({'source': SOURCE, 'source_sha256': SHA256, 'rows': rows}, ensure_ascii=True, indent=2))

if __name__ == '__main__':
    main()
