from pathlib import Path import json,datetime,shutil B=Path(__file__).resolve().parents[1] for name in ['execution','execution-added']: r=B/name;ledger=json.loads((r/'ledger.json').read_text()) for key,value in ledger['calls'].items(): if value['state']=='reserved':value['state']='interrupted_by_operator_after_payment_required';value['note']='Process stopped. Charge unknown: retain full reservation.' (r/'ledger.json').write_text(json.dumps(ledger,indent=2)) for run in (r/'runs').iterdir(): if not run.is_dir():continue p=run/'result.json'; result=json.loads(p.read_text())if p.exists()else {'run_id':run.name,'stages':{}} conv=run/'conversation.json';stage='revision'if result['stages'].get('initial')else'initial' if stage not in result['stages']: result['stages'][stage]={'status':'interrupted: operator stopped after provider Payment Required','elapsed_min':None,'answer':'','finished':datetime.datetime.now(datetime.timezone.utc).isoformat(),'timing_note':'No completed-stage timer persisted. Do not invent elapsed time.'} out=run/'workspace/output' if out.exists():shutil.copytree(out,run/stage/'output',dirs_exist_ok=True) (run/stage).mkdir(exist_ok=True,parents=True);(run/stage/'final.txt').write_text('No completed final response. Partial files preserved.\n') p.write_text(json.dumps(result,indent=2)) actual=0;reserve=0 for name in ['execution','execution-added']: d=json.loads((B/name/'ledger.json').read_text()) actual+=sum(float(x.get('actual',0))for x in d['calls'].values());reserve+=sum(float(x['reserved'])for x in d['calls'].values()if'actual'not in x) state={'status':'Stopped on provider Payment Required; full second pass incomplete','actual_recorded_usd':actual,'unsettled_reserved_usd':reserve,'conservative_committed_usd':actual+reserve,'authorized_cap_usd':100,'note':'No paid dispatch may resume without reconciling account access and remaining capacity. Published findings must label incomplete coverage.'} (B/'FINAL-STATUS.json').write_text(json.dumps(state,indent=2));print(json.dumps(state,indent=2))