"""Copy original successful RPC trace lines in a UTC interval, without rewriting them.
Failure/interrupted spans are excluded because their stacks contain local paths.
Usage: python3 extract-rpc.py SOURCE DESTINATION FROM_ISO UNTIL_ISO
"""
import sys,json,datetime,pathlib
source,dest,since,until=sys.argv[1:]
lines=[]
for line in pathlib.Path(source).read_text().splitlines():
    item=json.loads(line)
    if not item.get('name','').startswith('ws.rpc.'): continue
    if item.get('exit',{}).get('_tag')!='Success': continue
    stamp=datetime.datetime.fromtimestamp(int(item['startTimeUnixNano'])/1e9,datetime.timezone.utc).isoformat()
    if since < stamp < until: lines.append(line)
pathlib.Path(dest).write_text('\n'.join(lines)+'\n')
