"""Run the archived, unmodified ending predicates without starting the game/model."""
import ast
from datetime import datetime, timezone
from hashlib import sha256
import json
from pathlib import Path
import re
import sys
from urllib.request import Request, urlopen
import warnings

work=Path(__file__).resolve().parent
source_url='https://github.com/latitudegames/AIDungeon/raw/refs/heads/develop/story/utils.py'
commit='9cc7ae792ab9387b2a24b337ffb9d78643043f40'
pinned_url=f'https://raw.githubusercontent.com/latitudegames/AIDungeon/{commit}/story/utils.py'
started=datetime.now(timezone.utc).isoformat()
requests=[]
for label,url in [('develop',source_url),('pinned',pinned_url)]:
    with urlopen(Request(url,headers={'User-Agent':'Mozilla/5.0'}),timeout=60) as response:
        body=response.read()
        (work/f'utils-{label}.py').write_bytes(body)
        requests.append(dict(label=label,url=url,final_url=response.url,status=response.status,bytes=len(body),sha256=sha256(body).hexdigest()))
assert (work/'utils-develop.py').read_bytes()==(work/'utils-pinned.py').read_bytes()
source=(work/'utils-pinned.py').read_text(encoding='utf8')
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter('always')
    tree=ast.parse(source,filename='utils-pinned.py')
nodes=[n for n in tree.body if isinstance(n,ast.FunctionDef) and n.name in {'player_died','player_won'}]
assert len(nodes)==2
definitions={n.name:dict(first_line=n.lineno,last_line=n.end_lineno,text=ast.get_source_segment(source,n)) for n in nodes}
namespace={'re':re}
exec(compile(ast.Module(body=nodes,type_ignores=[]),'original-ending-predicates','exec'),namespace)
fixtures=[
    ('death-control','You die.'),
    ('word-prefix','You diet carefully and remain alive.'),
    ('quoted-warning','The sign says "you die" if you enter, but you stay outside.'),
    ('negated-death','You do not die.'),
    ('win-control','You retire.'),
    ('past-tense','You retired from the watch, but the adventure continues.'),
    ('negated-retirement','You do not retire.'),
    ('both-predicates','You retire, then you die.')
]
rows=[]
for label,text in fixtures:
    rows.append(dict(case=label,input=text,player_died=namespace['player_died'](text),player_won=namespace['player_won'](text)))
record=dict(operator='Codex desktop',date_beijing='2026-11-21',mode='Local Python function probe; no game or model launched',started_utc=started,completed_utc=datetime.now(timezone.utc).isoformat(),python=sys.version,source_requests=requests,pinned_commit=commit,source_bytes_equal=True,extracted_definitions=definitions,parse_warnings=[str(w.message) for w in caught],cases=rows,stop='Stopped after these original predicate calls. No GPT-2 weights, generated story, full game loop, current web game, patch or corrected-function test.')
(work/'probe-record.json').write_text(json.dumps(record,ensure_ascii=False,indent=2),encoding='utf8')
output=json.dumps(dict(mode=record['mode'],source_sha256=requests[1]['sha256'],cases=rows,stop=record['stop']),ensure_ascii=False,indent=2)
(work/'probe-output.txt').write_text(output+'\n',encoding='utf8')
print(output)
