"""Usage: python3 summarize.py BASE_TRACE HEAD_TRACE OUTPUT_PARENT
Reads only WorkspaceSearchIndex.search spans (never uploads full server logs).
Run picker-sample.js after each full /settings/projects reload: warm-up then
21 measured openings, on each commit with T3CODE_DEV_SEED=0 vp run dev.
The recorded captures were separate extra openings after measurement.
"""
import json, sys, statistics, pathlib, datetime, platform
out=pathlib.Path(sys.argv[3])/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
out.mkdir(parents=True)
report={'environment':{'os':platform.system(),'architecture':platform.machine(),'node':'26.13.1','browser':'Chromium preview','viewportCss':[1280,800],'theme':'light','fixture':{'directories':100,'typescriptFiles':10000,'images':0}},'metric':'WorkspaceSearchIndex.search durationMs from the real server, issued by the web icon picker','samplePolicy':'21 warm UI searches per side; full page navigation/reload each time; base discards first index warm-up; head discards initial warm-up plus one driver-calibration search. Subsequent recording searches excluded.'}
for side,path,skip,sha in [('before',sys.argv[1],1,'def3722faa3695034c03058e6ca73c31db353e7c'),('after',sys.argv[2],2,'780960d4b9599625ce9c0ea9d6600974e7d28be2')]:
 spans=[json.loads(l) for l in open(path) if '"name":"WorkspaceSearchIndex.search"' in l]
 selected=spans[skip:skip+21]
 assert len(selected)==21
 values=[s['durationMs'] for s in selected]
 report[side]={'commit':sha,'n':21,'medianMs':statistics.median(values),'minMs':min(values),'maxMs':max(values),'rangeMs':max(values)-min(values),'samplesMs':values,'result':'No image files found. on every completed opening','warmupMs':[s['durationMs'] for s in spans[:skip]]}
 (out/(side+'-search-spans.ndjson')).write_text(''.join(json.dumps(s)+'\n' for s in selected))
b,a=report['before'],report['after']
report['medianReductionMs']=b['medianMs']-a['medianMs']
report['speedup']=b['medianMs']/a['medianMs']
report['improvementExceedsBothFullRanges']=report['medianReductionMs']>max(b['rangeMs'],a['rangeMs'])
(out/'measurement.json').write_text(json.dumps(report,indent=2)+'\n')
lines=['REAL APP PROJECT ICON SEARCH — SAME MACHINE','Linux x86_64 | Node 26.13.1 | Chromium | 1280x800 CSS | light theme','100 directories, 10,000 .ts files, zero images; 21 warm UI searches per side','Metric: real server WorkspaceSearchIndex.search span duration, milliseconds','']
for side in ['before','after']:
 s=report[side];lines += [side.upper()+' '+s['commit'],f"median {s['medianMs']:.3f} ms | min {s['minMs']:.3f} | max {s['maxMs']:.3f} | range {s['rangeMs']:.3f}", 'samples: '+', '.join(f'{x:.3f}' for x in s['samplesMs']),s['result'],'']
lines += [f"Median reduction: {report['medianReductionMs']:.3f} ms; speedup {report['speedup']:.2f}x",'Reduction exceeds both full run ranges: '+str(report['improvementExceedsBothFullRanges']),'Server span timings, not historical synchronous heartbeat-delay measurements.','Historical Linux arm64 numbers were not remeasured; this run is Linux x86_64.']
(out/'measurement.txt').write_text('\n'.join(lines)+'\n')
print(out)
print('\n'.join(lines))
