from PIL import Image,ImageDraw,ImageFont
import json,pathlib,subprocess
root=pathlib.Path(__file__).parent
font=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',22)
big=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',28)
for side,sha in [('base','74a0ccb817'),('head','bb7ff15234')]:
 rows=[json.loads(s) for s in (root/('head-repeat.jsonl' if side=='head' else 'base.jsonl')).read_text().splitlines()]
 video=subprocess.Popen(['ffmpeg','-y','-loglevel','error','-f','rawvideo','-pixel_format','rgb24','-video_size','1280x720','-framerate','5','-i','-','-c:v','libx264','-pix_fmt','yuv420p',str(root/(side+'.mp4'))],stdin=subprocess.PIPE)
 for row in rows:
  im=Image.new('RGB',(1280,720),'#111827'); d=ImageDraw.Draw(im)
  lines=[('Before' if side=='base' else 'After')+' | '+sha,'Linux | real T3 dev server | Codex CLI 0.172.0','Fresh database | gpt-6-luna | systemd agent scopes ON','', 'Command: setsid -f sleep 3654', 'Codex replied: DONE; waiting for idle release','',row['utc'],row['unit'],f"Scope: {row['state']} / {row['substate']}",'']
  lines += [f"PID {p['pid']}: {p['kind']}" for p in row['processes']]
  if not row['processes']:lines+=['No processes remain in the scope.']
  for i,line in enumerate(lines):d.text((35,30+i*39),line,font=big if i==0 else font,fill='#f3f4f6')
  for _ in range(5):video.stdin.write(im.tobytes())
 im.save(root/(side+'.png'))
 video.stdin.close();video.wait()
