"""Independent raw-human/mask/LoRA/new-image and joint-review bundle verification."""
from collections import Counter,defaultdict
from contextlib import closing
from pathlib import Path
import json
import sys

import cv2
import numpy as np

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.mask_model_training_data import read,write,jsonl,require,sha
from pallet.cluster_targets import digest
from pallet.mask_template_refined import readonly
from pallet.pallet_description_data import ROOT,verify,sources_current,accept_batch
from pallet.pallet_description_mask import RUN
from pallet.pallet_description_vlm_repair import RUN as VLM_RUN
from pallet.pallet_description_review import SETS,validate_description
from pallet.pallet_description_output import decode_output
from pallet.pallet_description_build import description_draft
from pallet import mask_taxonomy_cycle2_data as prior
from pallet.mask_class_keypoints import corner_geometry,mask_fidelity,SETTINGS
from pallet.pose_review_geometry import polygon_value,raster_mask


def verify_training():
    out=ROOT/'joint_training_verification_v2.json';require(not out.exists(),'Preserve previous independent training receipt');m,records=verify();require(sources_current(),'Source changed')
    with closing(readonly(ROOT/'private/cycle2_reviews.sqlite3')) as db:reviews={k:json.loads(v) for k,v in db.execute('SELECT target_key,payload FROM reviews')}
    props={p['target_key']:p for p in jsonl(prior.REQUESTS/'proposals.jsonl')};classes=read(ROOT/'classes.json')
    accepted=accept_batch(props,reviews,classes,digest(prior.REQUESTS/'manifest.json'));require(accepted==jsonl(ROOT/'private/accepted_cycle2.jsonl'),'New human source does not replay')
    expected={r['target_key']:r for r in jsonl(prior.ROOT/'private/training.jsonl')+accepted}
    for r in records:
        ref=expected[r['target_key']];require(r['class_id']==ref['class_id'] and r['polygon']==ref['polygon'],'Latest human class/polygon changed')
        full=raster_mask(r['polygon'],*r['source']['size']);x1,y1,x2,y2=r['roi'];mask=cv2.imread(str(ROOT/r['mask']),0)>0;valid=cv2.imread(str(ROOT/r['valid']),0)>0
        require(np.array_equal(mask,full[y1:y2,x1:x2]) and mask.sum()==full.sum() and valid[mask].all(),'Native supervision clipped or changed')
    plan=read(RUN/'execution_plan.json')
    require(not plan['old_pallet_checkpoints_used'] and plan['dataset_manifest_sha256']==digest(ROOT/'dataset_manifest.json'),'Wrong mask training data/start')
    for name,h in plan['code_sha256'].items():require(digest(PROJECT/name)==digest(RUN/'code_snapshot'/name.replace('/','__'))==h,'Mask training implementation changed')
    steps={}
    for phase in ('independent_branch','full_pool'):
        receipt=read(RUN/phase/'training_receipt.json');history=[read(p) for p in sorted((RUN/phase).glob('epoch_*.json'))]
        require(receipt['checkpoint_sha256']==digest(RUN/phase/'final_ema.pt') and receipt['optimization_steps']==sum(h['epoch_updates'] for h in history)>0,'Missing actual optimizer evidence')
        require(all(np.isfinite(list(h['mean_losses'].values())).all() for h in history),'Nonfinite mask loss');steps[phase]=receipt['optimization_steps']
    candidates={r['target_key'] for r in jsonl(ROOT/'private/candidates.jsonl')};predictions=jsonl(ROOT/'private/predictions.jsonl')
    require(candidates=={p['target_key'] for p in predictions} and len(candidates)==len(predictions),'Incomplete remaining pool inference')
    vlm=read(VLM_RUN/'training_complete.json');phases={}
    for phase in ('component_branch','full_pool'):
        folder=VLM_RUN/phase;receipt=read(folder/'training_receipt.json');plan=read(folder/'execution_plan.json')
        require(receipt['optimizer_steps']>0 and receipt['adapter_sha256']==digest(folder/'adapter/adapter_model.safetensors'),'No actual LoRA training or changed adapter')
        require(plan['masked_missing_observations'] and plan['frozen_vision'],'Observation missing labels trained as negatives or vision unexpectedly unfrozen')
        require(plan['source_code_sha256']==receipt['source_code_sha256']==digest(PROJECT/'pallet/pallet_description_vlm_repair.py'),'VLM code changed mid-training')
        require(plan['initial_adapter_sha256']==digest(ROOT/'vlm_run_v1'/phase/'adapter/adapter_model.safetensors'),'Repair warm-start crosses prior train/holdout branches')
        require(plan['label_mask']=='only_tokens_overlapping_missing_null_value_keep_structural_terminators_and_EOS','Wrong missing-observation mask');phases[phase]=receipt
    desc=jsonl(ROOT/'description_data/records.jsonl');require(len(desc)==5615 and {d['target_key'] for d in desc}==set(expected),'Description dataset missing targets')
    require(all(d['observation_supervised']==bool(d['target']['观察']) and not d['missing_note_is_normal'] for d in desc),'Unannotated images mislabeled normal')
    groups=defaultdict(set)
    for d in desc:groups[d['split']].add(d['component'])
    require(not groups['train']&groups['holdout'],'VLM component leakage')
    write(out,dict(state='passed_round3_raw_human_real_mask_LoRA_and_partial_labels',targets=len(records),new_human_targets=len(accepted),
        human_note_targets=sum(d['observation_supervised'] for d in desc),mask_optimizer_steps=steps,LoRA_receipts=phases,
        not_new_blind_prospective_evaluation=True,observations_not_certified=True,source_manifest_sha256=digest(ROOT/'dataset_manifest.json'),code_sha256=digest(__file__)))


def verify_bundle(mode):
    settings=SETS[mode];out=settings['root'];require(not (out/'verification.json').exists(),'Preserve previous task seal');m=read(out/'manifest.json');props=jsonl(out/'proposals.jsonl');classes=read(ROOT/'classes.json')
    require(read(ROOT/'joint_training_verification_v2.json')['state']=='passed_round3_raw_human_real_mask_LoRA_and_partial_labels','Independent mask/LoRA/source replay required before task seal')
    require(len(props)==len({p['target_key'] for p in props})==m['request_count'],'Duplicate/missing task identities')
    require(m['dataset_manifest_sha256']==digest(ROOT/'dataset_manifest.json') and m['source_binding_sha256']==digest(ROOT/'source_binding.json')
        and m['vlm_adapter_sha256']==digest(VLM_RUN/'full_pool/adapter/adapter_model.safetensors') and m['mask_checkpoint_sha256']==digest(RUN/'full_pool/final_ema.pt'),'Wrong model/source binding')
    require(m['proposals_sha256']==digest(out/'proposals.jsonl'),'Changed proposals')
    receipt=read(ROOT/f'description_inference_{mode}.json')
    require(receipt['predictions_sha256']==m['description_predictions_sha256']==digest(ROOT/f'descriptions_{mode}.jsonl') and receipt['output_parser_sha256']==digest(PROJECT/'pallet/pallet_description_output.py'),'Description output/parser changed')
    originals={r['target_key']:r for r in jsonl(ROOT/'private/training.jsonl')};pred={r['target_key']:r for r in jsonl(ROOT/'private/predictions.jsonl')};desc={r['target_key']:r for r in jsonl(ROOT/f'descriptions_{mode}.jsonl')}
    blocked=set(read(ROOT/'private/protection.json')['protected_image_hashes']);localization={r['target_key']:r for r in jsonl(ROOT/'private/release_localization_checks.jsonl')};seen=set();counts=Counter()
    from pycocotools import mask as mu
    require(set(desc)=={p['target_key'] for p in props} and len(desc)==receipt['targets'],'Description inference omitted or duplicated targets')
    for p in props:
        key=p['target_key'];info=classes[str(p['class_id'])];require(len(p['polygon'])==p['K']==info['K'],'TaskK differs from fine class');polygon_value(p['polygon'],*p['source']['size']);validate_description(p['description'])
        require(p['description_raw']==desc[key]['raw'] and p['description_model_sha256']==desc[key]['adapter_sha256']==m['vlm_adapter_sha256'],'Description not actual image/model output')
        decoded=decode_output(desc[key]['raw'])
        require(all(desc[key].get(k)==v for k,v in decoded.items()) and p['description']==description_draft(decoded),'Description recovery/draft does not replay raw model output')
        require(p['description_parse_valid']==decoded['valid'] and p['description_structure_recovered']==decoded['structure_recovered'] and p['description_observation_withheld']==decoded['observation_withheld'],'Recovery wrongly counted as strict JSON success')
        require(p['description']['observation_state']=='unreviewed','Machine draft promoted to human completion')
        if decoded['observation_withheld']:
            require(not decoded['valid'] and decoded['fields'] is None and (decoded['recovered_fields'] or {}).get('观察') is None and '需人工填写' in p['description']['observation'],'Malformed observation presented as factual caption')
        if mode=='existing':
            r=originals[key];require(p['class_id']==r['class_id'] and p['polygon']==r['polygon'] and p['geometry_previously_completed'],'Previously finished geometry/class changed')
        else:
            r=pred[key];require(key not in originals and p['image_sha256'] not in blocked|seen,'Old or repeated image counted as new')
            require(r['classification_gate_passed'] and r['geometry_gate_passed'] and not localization[key]['rejected_reasons'] and r['classified_class_id']==p['class_id'] and r['polygon']==p['polygon'],'Rejected/wrong-source prediction published')
            require(r['conversion']['inserted_or_collinear_padding_points']==0,'Artificial K padding')
            rle=r['model_mask_rle'];mask=mu.decode(dict(size=rle['size'],counts=rle['counts'].encode())).astype(bool);f=mask_fidelity(p['polygon'],mask,origin=tuple(r['roi'][:2]),native_size=p['source']['size']);g=corner_geometry(p['polygon'])
            require(f['iou']>=.9 and f['hd95']<=max(2.,.1*r['source']['bbox']['height']) and g['minimum_abs_turn_degrees']>=18-1e-5 and g['minimum_edge_length']>=1.5-1e-5 and g['minimum_corner_deviation']>=.55-1e-5,'Unsupported exactK geometry')
        seen.add(p['image_sha256']);counts[p['class_id']]+=1
    if mode=='new':
        quotas=read(ROOT/'release_quota_report.json');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()),'Incorrect fine-class quota')
    else:require({p['target_key'] for p in props}==set(originals),'Existing pool omitted completed targets')
    for name,h in m['public_assets'].items():require(digest(out/name)==h,'Changed public asset')
    require(sources_current(),'Source changed during verification')
    write(out/'verification.json',dict(state='passed_joint_description_source_K_and_no_old_image_reuse',mode=mode,manifest_sha256=digest(out/'manifest.json'),
        targets=len(props),unique_images=len(seen),counts=dict(counts),old_source_labels_and_histories_unchanged=True,all_descriptions_are_unconfirmed_drafts=True,
        original_geometry_preloaded_completed=mode=='existing',strict_JSON_valid=receipt['schema_valid'],structure_recovered=receipt['structure_only_recovered'],observation_withheld=receipt['observation_withheld'],output_parser_sha256=receipt['output_parser_sha256'],code_sha256=digest(__file__)))
    print(dict(state='passed_joint_description_source_K_and_no_old_image_reuse',mode=mode,targets=len(props)),flush=True)


if __name__=='__main__':
    import argparse
    p=argparse.ArgumentParser();p.add_argument('phase',choices=['training','new','existing']);a=p.parse_args();(verify_training() if a.phase=='training' else verify_bundle(a.phase))
