"""Replay new human provenance, actual optimization, no-leakage and supportedK selection."""
from collections import Counter,defaultdict
import csv
from pathlib import Path
import sys

import cv2
import numpy as np

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.cluster_targets import digest
from pallet.mask_model_training_data import read,write,jsonl,require,sha
from pallet.mask_taxonomy_cycle2_data import ROOT,REQUESTS,verify,sources_current,update_classes,integrate
from pallet.mask_taxonomy_cycle2_train import RUN,CONFIG,TRAINING_CODE
from pallet import mask_taxonomy_data as old
from pallet.mask_class_keypoints import mask_fidelity,corner_geometry,SETTINGS
from pallet.pose_review_geometry import raster_mask,polygon_value
from pallet.mask_template_refined import readonly
from contextlib import closing
import json


def verify_all():
    require(not (REQUESTS/'verification.json').exists(),'Preserve independent cycle2 seal')
    manifest,training=verify();require(sources_current(),'Live sources changed')
    base=jsonl(old.ROOT/'private/training.jsonl');classes=read(ROOT/'classes.json');prior_classes=read(old.ROOT/'classes.json')
    props={p['target_key']:p for p in jsonl(old.REQUESTS/'proposals.jsonl')}
    with closing(readonly(ROOT/'private/cycle1_reviews.sqlite3')) as db:saved={k:json.loads(v) for k,v in db.execute('SELECT target_key,payload FROM reviews')}
    accepted,excluded=integrate(props,saved,prior_classes,digest(old.REQUESTS/'manifest.json'),accept_unsaved=True)
    require(accepted==jsonl(ROOT/'private/accepted_cycle1.jsonl') and len(accepted)==1829 and len(excluded)==2,'Batch acceptance does not replay')
    require(Counter(r['geometry_source'] for r in accepted)==Counter(latest_saved_human_correct=1405,explicit_user_batch_acceptance_of_unchanged_proposal=424),'Saved vs batch-confirmed provenance conflated')
    expected={r['target_key']:r for r in base+accepted};require(classes==update_classes(prior_classes,base+accepted),'Fine labels/parent mappings or small-group calculation changed')
    for r in training:
        ref=expected[r['target_key']];require(r['class_id']==ref['class_id'] and r['polygon']==ref['polygon'],'Latest human class/coordinates changed')
        require(r['K']==len(r['polygon'])==classes[str(r['class_id'])]['K'],'FineK mismatch')
        polygon_value(r['polygon'],*r['source']['size']);mask=cv2.imread(str(ROOT/r['mask']),0)>0;valid=cv2.imread(str(ROOT/r['valid']),0)>0
        x1,y1,x2,y2=r['roi'];full=raster_mask(r['polygon'],*r['source']['size'])
        require(np.array_equal(mask,full[y1:y2,x1:x2]) and full.sum()==mask.sum() and valid[mask].all(),'Exact native mask/ignore supervision differs')
    plan=read(RUN/'execution_plan.json');require(not plan['old_pallet_checkpoints_used'] and plan['generic_weights_sha256'].startswith('f37072fd')
        and plan['dataset_manifest_sha256']==digest(ROOT/'dataset_manifest.json'),'Fresh generic start on the frozen dataset required')
    for name,h in plan['code_sha256'].items():require(digest(PROJECT/name)==digest(RUN/'code_snapshot'/name.replace('/','__'))==h,'Pinned training implementation changed')
    for phase,records,epochs in (('independent_branch',[r for r in training if r['evaluation_branch_split']=='train'],CONFIG['evaluation_epochs']),('full_pool',training,CONFIG['full_pool_epochs'])):
        receipt=read(RUN/phase/'training_receipt.json');history=[read(p) for p in sorted((RUN/phase).glob('epoch_*.json'))]
        require(len(history)==epochs and receipt['records']==len(records) and receipt['checkpoint_sha256']==digest(RUN/phase/'final_ema.pt'),'Actual training/checkpoint receipt missing')
        require(receipt['optimization_steps']==sum(h['epoch_updates'] for h in history)>0,'Optimizer did not really run')
        for h in history:
            require(all(np.isfinite(v) for v in h['mean_losses'].values()) and all(h['class_exposures'].get(str(c),0)>=n for c,n in Counter(r['class_id'] for r in records).items()),'Nonfinite loss or dropped fine class')
    held=jsonl(RUN/'independent_branch/holdout_predictions.jsonl');metrics=read(RUN/'independent_branch/holdout_metrics.json')
    require({r['target_key'] for r in held}=={r['target_key'] for r in training if r['evaluation_branch_split']=='holdout'},'Changed holdout denominator')
    require(abs(metrics['fine_top1']-sum(r['fine_index']==r['predicted_fine_index'] for r in held)/len(held))<1e-10,'Heldout metric not replayable')
    predictions={p['target_key']:p for p in jsonl(ROOT/'private/predictions.jsonl')};candidates={p['target_key']:p for p in jsonl(ROOT/'private/candidates.jsonl')}
    for name,h in read(ROOT/'inference_implementation.json')['code_sha256'].items():require(digest(PROJECT/name)==digest(ROOT/'inference_code_snapshot'/name.replace('/','__'))==h,'Inference/strictK implementation changed')
    require(set(predictions)==set(candidates),'Missing remaining-pool inference')
    proposals=jsonl(REQUESTS/'proposals.jsonl');pm=read(REQUESTS/'manifest.json');quotas=read(ROOT/'release_quota_report.json')
    require(pm['checkpoint_sha256']==read(RUN/'training_complete.json')['full_pool']==digest(RUN/'full_pool/final_ema.pt') and pm['proposals_sha256']==digest(REQUESTS/'proposals.jsonl')
        and pm['training_dataset_manifest_sha256']==digest(ROOT/'dataset_manifest.json') and pm['source_binding_sha256']==digest(ROOT/'source_binding.json')
        and pm['inference_predictions_sha256']==digest(ROOT/'private/predictions.jsonl')==read(ROOT/'inference_complete.json')['predictions_sha256'],'Task/model/source/inference binding differs')
    localization={p['target_key']:p for p in jsonl(ROOT/'private/release_localization_checks.jsonl')};blocked=set(read(ROOT/'private/protection.json')['protected_image_hashes'])
    old_images={p['image_sha256'] for p in props.values()};seen=set();counts=Counter()
    from pycocotools import mask as mu
    for p in proposals:
        pred=predictions[p['target_key']];cls=p['class_id'];info=classes[str(cls)]
        require(pred['classified_class_id']==cls and pred['classification_gate_passed'] and pred['geometry_gate_passed'] and not localization[p['target_key']]['rejected_reasons'],'Withheld or wrong-target prediction entered queue')
        require(p['polygon']==pred['polygon'] and len(p['polygon'])==p['K']==info['K'],'ClassK differs from actual supported model output')
        require(p['image_sha256']==candidates[p['target_key']]['image_sha256'] and p['image_sha256'] not in blocked|old_images|seen,'Old/duplicate/protected image counted as new')
        require(not p['template_used'] and not p['quality_override_used'] and pred['conversion']['inserted_or_collinear_padding_points']==0,'Template/padding/quality bypass filled quota')
        polygon_value(p['polygon'],*p['source']['size']);rle=pred['model_mask_rle'];mask=mu.decode(dict(size=rle['size'],counts=rle['counts'].encode())).astype(bool)
        fidelity=mask_fidelity(p['polygon'],mask,origin=tuple(pred['roi'][:2]),native_size=p['source']['size']);g=corner_geometry(p['polygon'])
        require(fidelity['iou']>=SETTINGS['minimum_model_iou'] and fidelity['hd95']<=max(2.,.1*p['source']['bbox']['height']),'Serialized geometry lost native model fidelity')
        require(g['minimum_edge_length']>=1.5-1e-5 and g['minimum_abs_turn_degrees']>=18-1e-5 and g['minimum_corner_deviation']>=.55-1e-5,'Unsupported padded corner')
        seen.add(p['image_sha256']);counts[cls]+=1
    require(len(quotas)==41 and all(q['selected_new_images']==counts[int(c)]<=100 and q['gap']==100-counts[int(c)] for c,q in quotas.items()),'Wrong per-fine-class100 quota')
    with (REQUESTS/'classification.csv').open(encoding='utf-8-sig',newline='') as stream:csv_rows=list(csv.DictReader(stream))
    require({r['target_key'] for r in csv_rows}==set(predictions) and len(csv_rows)==len(predictions),'Classification CSV not complete')
    for name,h in pm['public_assets'].items():require(digest(REQUESTS/name)==h,'Public asset changed')
    # Interaction code stays v6; only title/draft key and observational evaluation copy differ.
    provenance=read(ROOT/'cycle2_ui_provenance.json');require(all(digest(ROOT/'cycle2_ui_source'/name)==h for name,h in provenance['source_assets_sha256'].items()),'Staged UI changed')
    smoke=read(ROOT/'ui_transformation_smoke.json');require(smoke['new_UI_sha256']==digest(REQUESTS/'review.js') and smoke['code_sha256']==digest(PROJECT/'pallet/mask_taxonomy_cycle2_review.py')
        and smoke['interaction_pan_navigation_save_and_ACK_block_byte_identical'],'UI transformation/control-code equivalence differs')
    require(sources_current() and old.sources_current(),'New source changed or older service source inventory invalidated')
    value=dict(state='passed_cycle2_actual_training_sources_new_images_exactK',manifest_sha256=digest(REQUESTS/'manifest.json'),training_targets=len(training),
        saved_correct_new=1405,batch_confirmed_unsaved_new=424,drafts_not_used=2,original_actions_not_rewritten=True,original_fine_labels_preserved=True,
        classes=41,parents=9,holdout_targets=len(held),not_new_blind_prospective_evaluation=True,inference_targets=len(predictions),
        selected_new_distinct_images=len(proposals),gap=4100-len(proposals),per_class_counts=dict(counts),old_image_reuse=0,
        template_padding_or_quality_bypass=0,old_source_stores_and_formal_labels_unchanged=True,code_sha256=digest(__file__))
    write(REQUESTS/'verification.json',value);print(value,flush=True)


if __name__=='__main__':verify_all()
