import collections, glob, json, re
paths=glob.glob('artifacts/bs2-search-20260912/memory/*.json')
def signature(s):
    d=collections.defaultdict(collections.Counter)
    for step in s['timeline']['steps']:
        d[step['source']][step['operation']]+=1
    return d
base=json.load(open(next(p for p in paths if '/baseline-' in p)))
b=signature(base)
groups=collections.Counter(); details=[]
for p in sorted(paths):
    if 'candidate-' not in p: continue
    s=json.load(open(p)); c=signature(s)
    changed=tuple(sorted(k for k in b.keys()|c.keys() if b[k]!=c[k] and k is not None))
    groups[changed]+=1
    details.append({'scope':s['scope'],'changed_operations':changed,'peak_bytes':s['effective_peak_bytes'], 'exchange_estimate':s['peak']['exchange_rows']})
print('candidate operation changes:',dict(groups))
print('effective tensor+support peak range:', min(d['peak_bytes'] for d in details),max(d['peak_bytes'] for d in details))
print('baseline',base['effective_peak_bytes'])
json.dump(details,open('artifacts/bs2-search-20260912/candidates.json','w'),indent=2)
