import datetime as dt,json,sys from pathlib import Path root=Path(__file__).resolve().parents[2] source=root/'source.json' if not source.exists(): print('SOURCE_MISSING: preserve the last good artifact',file=sys.stderr);sys.exit(2) d=json.loads(source.read_text(encoding='utf-8')) if not d.get('records'): print('SOURCE_INCOMPLETE: preserve the last good artifact',file=sys.stderr);sys.exit(3) result={'generatedAt':dt.datetime.now(dt.timezone.utc).isoformat(),'sourceWindowEnd':d['sourceWindowEnd'],'count':len(d['records']),'records':d['records'],'delivery':'local artifact only'} out=root/'last-good.json';tmp=root/'candidate.json' tmp.write_text(json.dumps(result,indent=2),encoding='utf-8');tmp.replace(out) print(json.dumps(result))