#!/usr/bin/env python3
import os,sys,subprocess,threading,json,time
from pathlib import Path
root=Path(__file__).parent
run=root/('runtime-'+str(time.time_ns()));run.mkdir()
(run/'args.json').write_text(json.dumps(sys.argv[1:]))
p=subprocess.Popen(['/usr/local/bin/claude',*sys.argv[1:]],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=None)
def feed():
 with (run/'stdin.ndjson').open('wb') as log:
  while True:
   chunk=os.read(0,65536)
   if not chunk: break
   log.write(chunk);log.flush()
   try:p.stdin.write(chunk);p.stdin.flush()
   except BrokenPipeError:break
 try:p.stdin.close()
 except:pass
threading.Thread(target=feed,daemon=True).start()
with (run/'stdout.ndjson').open('wb') as log:
 while True:
  chunk=p.stdout.read1(65536)
  if not chunk:break
  log.write(chunk);log.flush();os.write(1,chunk)
sys.exit(p.wait())
