import json,pathlib
R=pathlib.Path(__file__).parent
d=json.loads((R/'run.json').read_text())
counts={};events={};last=None
for row in d['observations']:
 for ob in row['runtime_observation']:
  last=ob
  for k,v in ob['counters'].items():counts[k]=max(counts.get(k,0),v['count'])
  events.update(ob['events'])
marks=[]
for f in (R/'diagnostics').glob('*.jsonl'):
 for line in f.read_text().splitlines():
  x=json.loads(line);marks.append({k:x.get(k) for k in ('stage','phase','thread_role','monotonic_ns','diagnostic_error')})
out=dict(stage=d['stage'],window_result=d.get('window_result'),counter_max=counts,
         events=list(events),last_worker_alive=last.get('worker_alive') if last else None,
         stop_matches_started=last.get('stop_matches_started') if last else None,
         marks=sorted(marks,key=lambda x:x['monotonic_ns']),last=d['observations'][-1] if d['observations'] else None,
         cleanup_verified=d.get('cleanup_verified'),restored=d.get('restored'))
# Chat projection deliberately excludes runtime identifiers and raw status payloads.
print(json.dumps({k:v for k,v in out.items() if k!='last'},ensure_ascii=False,indent=2))
if d['stage']=='completed':(R/'summary.json').write_text(json.dumps(out,indent=2))
