import datetime as dt,json,sys from pathlib import Path root=Path(__file__).resolve().parents[2] source=json.loads((root/'source.json').read_text(encoding='utf-8')) if not source.get('records'): print('SOURCE_INCOMPLETE: retain the last good output',file=sys.stderr);sys.exit(3) runs=root/'runs.jsonl' sequence=len(runs.read_text(encoding='utf-8').splitlines())+1 if runs.exists() else 1 out={'sequence':sequence,'ranAt':dt.datetime.now(dt.timezone.utc).isoformat(),'count':len(source['records']),'delivery':'local file only'} with runs.open('a',encoding='utf-8') as f:f.write(json.dumps(out)+'\n') candidate=root/'candidate.json';candidate.write_text(json.dumps(out,indent=2),encoding='utf-8');candidate.replace(root/'last-good.json') print(json.dumps(out))