#!/usr/bin/env python3 """Independently mirror the training supervisor's durable phase state to HF.""" import fcntl, hashlib, json, os, time from pathlib import Path from huggingface_hub import HfApi, hf_hub_download ROOT=Path('/marimo/disef_cars');STATUS=ROOT/'status/status.json';REPO='DanielNguyen121/DisEF-Cars' def sha(path):return hashlib.file_digest(Path(path).open('rb'),'sha256').hexdigest() def main(): lock=(ROOT/'training_status_sync.lock').open('a+');fcntl.flock(lock,fcntl.LOCK_EX|fcntl.LOCK_NB) api=HfApi(token=os.environ['HF_TOKEN']);last=None;failures=0 while True: if not STATUS.exists():time.sleep(30);continue state=json.loads(STATUS.read_text());assert state.get('run_id')=='cars_disef_spc_seed1_20261025' digest=sha(STATUS);terminal=state.get('current_phase')=='COMPLETED' or any(v=='BLOCKED' for v in state.get('phases',{}).values()) if digest!=last: try: api.upload_file(path_or_fileobj=str(STATUS),path_in_repo='status/status.json',repo_id=REPO,repo_type='dataset',commit_message=f"Cars {state.get('current_phase')} phase state") remote=Path(hf_hub_download(REPO,'status/status.json',repo_type='dataset',force_download=True)) assert sha(remote)==digest last=digest;failures=0;print('SYNCED',state.get('current_phase'),state.get('updated_at'),flush=True) except Exception as e: failures+=1;print('SYNC_RETRY',failures,type(e).__name__,str(e)[:120],flush=True) if terminal and failures>=10:raise if terminal and digest==last:return time.sleep(min(300,30*max(1,failures))) if __name__=='__main__':main()