import json
from remote import ROOT, python, REMOTE

code="""import json,subprocess
from pathlib import Path
root=Path(ROOT)
image=json.loads((root/'built.json').read_text())['image']
script="import sys,pytest;sys.path[:0]=['/work','/work/tests','/opt/contextra'];raise SystemExit(pytest.main(['-q','--collect-only','-p','no:cacheprovider','/work/tests/test_event_cooldown.py']))"
p=subprocess.run(['docker','run','--rm','--pull=never','--name','ct-r5-collection-20261101-r01','--user','0:0','--runtime','runc','--network=none','--read-only','--cpus','2','--memory','8g','--pids-limit','128','--tmpfs','/tmp:rw,size=64m','-e','NVIDIA_VISIBLE_DEVICES=void','-e','PYTHONDONTWRITEBYTECODE=1','-v',str(root)+':/work','--entrypoint','python3',image,'-B','-c',script],capture_output=True,timeout=60)
(root/'collection-failure.log').write_bytes(p.stdout+p.stderr)
print(json.dumps(dict(exit_code=p.returncode,output=(p.stdout+p.stderr).decode()[-6000:])))
""".replace('ROOT',repr(REMOTE),1)
data=json.loads(python(code,sudo=True,timeout=90))
(ROOT/'collection-failure.json').write_text(json.dumps(data,indent=2)+'\n')
print(data['output'])
