"""Hole-free concave covering polygons for editing fork-face proposals."""
from __future__ import annotations

import argparse
from collections import Counter
from concurrent.futures import ProcessPoolExecutor
from contextlib import contextmanager
import hashlib
import io
import json
import os
from pathlib import Path
import time
from types import SimpleNamespace
import zipfile

import cv2
import numpy as np
from PIL import Image
import shapely
from shapely import Polygon, box, concave_hull, multipoints, simplify

import pallet.fork_face_simplify as strict
from pallet.fork_face_anylabeling import (
    LABEL,VIEWS,check_space,completed,decode_mask,digest,encoded,load_input,
    load_upstream,make_document,protected_write,read_checked,record_path,
    safe_path,save_status,
)

DEFAULT_CONFIG={
    'version':2,'seed':42,'pilot_targets_per_resolution':32,
    'max_polygon_vertices':24,'max_group_polygons':1,'max_group_vertices':24,
    'minimum_source_coverage':1.0,'fill_internal_holes':True,
    'iou_acceptance_threshold':None,'hull':'concave',
    'hull_ratios':[0,.025,.05,.1,.2,.35,.5,.75,1],
    'simplify_tolerances':[0,.25,.5,.75,1,1.5,2,3,4,6,8],
    'outward_buffers':[0,.25,.5,.75,1,1.5,2,3,4,6,8],
    'shapely_version':'2.1.2',
    **{k:v for k,v in strict.DEFAULT_CONFIG.items() if k.startswith('upstream_')},
}
_CONTEXT={}


def filled_evidence(mask):
    """Fill only enclosed holes; external concavities remain available."""
    padded=np.pad(~mask,1,constant_values=True).astype(np.uint8)
    _,labels=cv2.connectedComponents(padded,connectivity=4)
    outside=labels[0,0]
    holes=(labels[1:-1,1:-1]!=outside)&~mask
    return mask|holes,holes


def enclose_mask(original,config,upstream):
    original=np.asarray(original,bool)
    if original.ndim!=2:raise ValueError('Expected a two-dimensional mask')
    if not original.any():
        return {'status':'original_unresolved','polygons':[],'source_pixels':0,'output_pixels':0,
                'added_pixels':0,'deleted_pixels':0,'source_coverage':None,'iou':None,
                'polygon_count':0,'vertex_count':0,'reason':'original_empty_positive'}
    ys,xs=np.nonzero(original)
    x0,x1,y0,y1=int(xs.min()),int(xs.max())+1,int(ys.min()),int(ys.max())+1
    mask=original[y0:y1,x0:x1]
    evidence,holes=filled_evidence(mask)
    ys,xs=np.nonzero(evidence)
    points=multipoints(np.column_stack((xs,ys)))
    clip=box(0,0,mask.shape[1]-.125,mask.shape[0]-.125)
    best,key=None,None
    checked=0
    seen=set()

    def consider(polygon,parameters):
        nonlocal best,key,checked
        if polygon.geom_type!='Polygon' or not polygon.is_valid or polygon.area<=0:
            return False
        # A buffer can close an outer notch; inner rings are deliberately filled.
        polygon=simplify(Polygon(polygon.exterior),0,preserve_topology=True)
        coordinates=np.round(np.asarray(polygon.exterior.coords[:-1]),8).tolist()
        if not 3<=len(coordinates)<=config['max_polygon_vertices']:
            return False
        if len(set(map(tuple,coordinates)))!=len(coordinates) or not Polygon(coordinates).is_valid:
            return False
        signature=tuple(map(tuple,coordinates))
        if signature in seen:return False
        seen.add(signature)
        if (min(min(p) for p in coordinates)<0 or max(p[0] for p in coordinates)>=mask.shape[1]
                or max(p[1] for p in coordinates)>=mask.shape[0]):return False
        native_coordinates=[[round(x+x0,8),round(y+y0,8)] for x,y in coordinates]
        # PIL scan conversion can change a boundary pixel after a float ring is
        # translated. Evaluate in final native coordinates, then crop the result.
        actual_native=upstream(original.shape,native_coordinates,'polygon')
        actual=actual_native[y0:y1,x0:x1];checked+=1
        if (evidence&~actual).any():return False
        _,remaining_holes=filled_evidence(actual)
        if remaining_holes.any():return False
        # PIL occasionally fills a pixel just beyond the ring's integer bounds.
        # Include it in the area/IoU objective rather than assuming bbox clipping.
        source_pixels=int(mask.sum());output_pixels=int(actual_native.sum())
        metrics={'iou':source_pixels/output_pixels,'source_pixels':source_pixels,
                 'output_pixels':output_pixels,'added_pixels':output_pixels-source_pixels,'deleted_pixels':0}
        candidate_key=(metrics['output_pixels'],polygon.area,len(coordinates),signature)
        if key is None or candidate_key<key:
            key=candidate_key
            best={'status':'enclosure_ready','polygons':
                [native_coordinates],
                **metrics,'source_coverage':1.0,'polygon_count':1,'vertex_count':len(coordinates),
                'filled_original_hole_pixels':int(holes.sum()),'geometry_area':float(polygon.area),
                'parameters':parameters,'minimum_area_scope':'enumerated_candidates_with_vertex_cap'}
        return True

    # A rectangle is a guaranteed legal, covering fallback, including singletons.
    consider(clip,{'method':'bounding_rectangle_fallback','hull_ratio':None,'tolerance':None,'buffer':0})
    hulls_seen=set()
    for ratio in config['hull_ratios']:
        hull=concave_hull(points,ratio=ratio,allow_holes=False)
        if hull.geom_type!='Polygon':
            # Collinear evidence needs a nonzero area. The rectangle already covers it.
            continue
        if hull.wkb in hulls_seen:continue
        hulls_seen.add(hull.wkb)
        simplified_seen=set()
        for tolerance in config['simplify_tolerances']:
            geometry=simplify(hull,tolerance,preserve_topology=True)
            if geometry.wkb in simplified_seen:continue
            simplified_seen.add(geometry.wkb)
            if len(geometry.exterior.coords)-1>config['max_polygon_vertices']:
                continue
            for distance in config['outward_buffers']:
                candidate=geometry if distance==0 else geometry.buffer(distance,join_style='mitre')
                candidate=candidate.intersection(clip)
                passed=consider(candidate,{'method':'concave_cover','hull_ratio':ratio,
                    'tolerance':tolerance,'buffer':distance})
                # Higher buffers strictly enclose this candidate and cannot improve area.
                if passed:break
    if best is None:raise RuntimeError('No legal covering polygon; rectangle fallback failed')
    best['candidates_checked']=checked
    return best


def initializer(source,output,config,binding,hashes,before,before_records):
    global _CONTEXT
    cv2.setNumThreads(1)
    from threadpoolctl import threadpool_limits
    limiter=threadpool_limits(limits=1)
    _CONTEXT={'source':Path(source),'output':Path(output),'delivery':Path(output)/'delivery',
              'config':config,'binding':binding,'hashes':hashes,'limiter':limiter,
              'before_root':Path(before),'before_records':before_records,
              'upstream':load_upstream(config['upstream_shape_source'],config['upstream_shape_sha256']),
              'label_file':strict.load_label_file(config['upstream_label_source'],config['upstream_label_sha256'])}
    # Shared read-only counters/preview helpers use their own context.
    strict._CONTEXT=_CONTEXT


@contextmanager
def pooled(args,binding,hashes,before):
    initargs=(str(args.source),str(args.output),args.config_data,binding,hashes,str(args.before),before)
    if args.workers==1:
        initializer(*initargs)
        yield SimpleNamespace(map=lambda fn,items,chunksize=1:map(fn,items))
    else:
        from multiprocessing import get_context
        for name in ['OPENBLAS_NUM_THREADS','OMP_NUM_THREADS','MKL_NUM_THREADS']:os.environ[name]='1'
        with ProcessPoolExecutor(max_workers=args.workers,mp_context=get_context('spawn'),
                                 initializer=initializer,initargs=initargs) as executor:
            yield executor


def png(array):
    buffer=io.BytesIO();Image.fromarray(array).save(buffer,format='PNG');return buffer.getvalue()


def convert_frame(job):
    frame,targets,faces,selected,preview_all=job
    ctx=_CONTEXT;source,delivery,hashes=ctx['source'],ctx['delivery'],ctx['hashes']
    prior=completed(ctx['output'],frame,ctx['binding'])
    if prior is not None:
        for relative in prior['files']:
            if relative.startswith('reference_'):
                old=relative[len('reference_'):]
                if digest(safe_path(source,old))!=hashes[old]:raise ValueError('Frozen source changed on resume')
        if digest(source/frame['image'])!=hashes[frame['image']]:raise ValueError('Frozen image changed on resume')
        return prior
    check_space(ctx['output']);started=time.monotonic();size=(frame['height'],frame['width'])
    image=read_checked(source,frame['image'],hashes)
    with Image.open(io.BytesIO(image)) as im:
        im.load()
        if im.size!=(size[1],size[0]):raise ValueError('Native image size mismatch')
    files,views={},{}

    def write(relative,data):
        protected_write(safe_path(delivery,relative),data)
        files[relative]=hashlib.sha256(data).hexdigest()

    parents={t['target_id']:t for t in targets}
    chosen=[t for t in targets if selected is None or t['target_id'] in selected]
    for view in VIEWS:
        items=faces if view=='faces' else targets
        items=sorted((i for i in items if selected is None or i['target_id'] in selected),
                     key=lambda i:i['face_id'] if view=='faces' else i['box_index'])
        groups,polygons,masks,actuals=[],{},{},{}
        before=strict.old_group_counts(frame,view)
        for item in items:
            original=item['mask'] if view=='faces' else item['union_mask']
            data=read_checked(source,original,hashes);mask=decode_mask(data,size)
            if not mask.any() and (view=='faces' or item['status']!='unresolved_no_mask'):
                raise ValueError('Unexpected empty positive candidate')
            group_id=item['face_id'] if view=='faces' else item['box_index']+1
            if group_id in masks:raise ValueError('Duplicate group')
            result=enclose_mask(mask,ctx['config'],ctx['upstream']);points=result.pop('polygons')
            actual=strict.fill_polygons(points,size,ctx['upstream'])
            if (mask&~actual).any():raise ValueError('Native-coordinate coverage failed')
            masks[group_id]=mask;polygons[group_id]=points;actuals[group_id]=actual
            reference='reference_'+original;write(reference,data)
            old_polygons,old_vertices=before.get(group_id,(0,0))
            groups.append({'group_id':group_id,'pallet_group_id':item['box_index']+1,
                'face_id':item.get('face_id'),'target_id':item['target_id'],'bbox':parents[item['target_id']]['bbox'],
                'mask':reference,'mask_sha256':hashes[original],'pixels':int(mask.sum()),
                'source_status':item['status'],'proposal_only':True,'mask_human_reviewed':False,
                'before_polygon_count':old_polygons,'before_vertex_count':old_vertices,**result})
        counts=np.zeros(size,np.uint16)
        for actual in actuals.values():counts+=actual
        overlap=counts>1
        for group in groups:
            group['overlap_pixels']=int((actuals[group['group_id']]&overlap).sum())
            group['enclosure_overlap_needs_review']=group['overlap_pixels']>0
        document=make_document(frame,chosen,groups,polygons,view)
        unresolved=[t for t in chosen if t['status']=='unresolved_no_mask']
        document['flags'].update({'redraw_required':bool(unresolved),'enclosure_overlap':bool(overlap.any())})
        document['pallet_segmentation'].update({
            'schema_version':3,'enclosure_task_hash':ctx['binding']['task_hash'],
            'holes_preserved_by_grouped_polygon_pieces':False,
            'internal_holes_deliberately_filled':True,'iou_is_acceptance_condition':False,
            'source_mask_coverage_required':1.0,'hull':'concave',
            'max_polygon_vertices':ctx['config']['max_polygon_vertices'],
            'overlap_pixels':int(overlap.sum()),'overlaps_require_human_review':True,
            'empty_shapes_do_not_mean_negative':frame['frame_status']!='confirmed_negative',
            'minimum_area_scope':'enumerated_candidates_with_vertex_cap',
        })
        for parent in document['pallet_segmentation']['parents']:
            parent['enclosure_status']='original_unresolved' if parent['status']=='unresolved_no_mask' else 'enclosure_ready'
        if unresolved:document['text']=f'{len(unresolved)} positive parent(s) have no mask: redraw using parents/bbox'
        if overlap.any():document['text']+='; enclosing polygons overlap; inspect instance boundaries'
        prefix=f"{view}/images/{frame['split']}/{frame['frame_key']}"
        write(prefix+'.png',image);write(prefix+'.json',encoded(document))
        overlap_path=f"enclosure_overlaps/{view}/{frame['split']}/{frame['frame_key']}.png"
        write(overlap_path,png(overlap.astype(np.uint8)*255))
        preview=None
        if preview_all or unresolved or overlap.any():
            preview=f"previews/{view}/{frame['split']}/{frame['frame_key']}.png"
            display=[{**g,'status':'simplified_accepted' if g['status']=='enclosure_ready' else g['status']} for g in groups]
            # Empty face parents have no face group; show their parent identity.
            for parent in unresolved:
                if any(g['target_id']==parent['target_id'] for g in display):continue
                marker='parent_'+str(parent['box_index']+1)
                display.append({'group_id':marker,'target_id':parent['target_id'],'bbox':parent['bbox'],'status':'original_unresolved'})
                masks[marker]=np.zeros(size,bool);polygons[marker]=[]
            write(preview,strict.render_preview(image,display,masks,polygons,size))
        views[view]={'image':prefix+'.png','annotation':prefix+'.json','groups':groups,
                     'preview':preview,'overlap_mask':overlap_path,'overlap_pixels':int(overlap.sum()),
                     'polygons':sum(len(p) for p in polygons.values()),
                     'vertices':sum(len(p) for ps in polygons.values() for p in ps)}
    for field in ['instances','conflicts']:write('reference_'+frame[field],read_checked(source,frame[field],hashes))
    record={'frame_id':frame['frame_id'],'frame_key':frame['frame_key'],'split':frame['split'],
        'group_id':frame['group_id'],'domain':frame.get('domain'),'width':frame['width'],'height':frame['height'],
        'task_hash':ctx['binding']['task_hash'],'source_frame_status':frame['frame_status'],
        'targets':len(chosen),'parents':[{'target_id':t['target_id'],'bbox':t['bbox'],'box_index':t['box_index'],
          'status':'original_unresolved' if t['status']=='unresolved_no_mask' else 'enclosure_ready'} for t in chosen],
        'unresolved_targets':len(unresolved),'state':'success','views':views,'files':files,
        'elapsed_seconds':time.monotonic()-started}
    protected_write(record_path(ctx['output'],frame),encoded(record));return record


def summary_records(records,binding):
    summary={'frames':len(records),'targets':sum(r['targets'] for r in records),
        'splits':dict(Counter(r['split'] for r in records)),
        'confirmed_negative_frames':sum(r['source_frame_status']=='confirmed_negative' for r in records),
        'original_unresolved_targets':sum(r['unresolved_targets'] for r in records),
        'task_hash':binding['task_hash'],'hull':'concave','source_mask_coverage_required':1.0,
        'internal_holes_filled':True,'iou_acceptance_threshold':None,
        'proposal_only':True,'mask_human_reviewed':False,'segmentation_accuracy':None,'views':{}}
    for view in VIEWS:
        sizes=sorted({(r['width'],r['height']) for r in records})
        sections={'all':records,**{f'{w}x{h}':[r for r in records if (r['width'],r['height'])==(w,h)] for w,h in sizes}}
        info={'by_resolution':{}}
        for name,rows in sections.items():
            groups=[g for r in rows for g in r['views'][view]['groups']]
            ready=[g for g in groups if g['status']=='enclosure_ready']
            old_p=sum(g['before_polygon_count'] for g in groups);old_v=sum(g['before_vertex_count'] for g in groups)
            after_p=sum(g['polygon_count'] for g in ready);after_v=sum(g['vertex_count'] for g in ready)
            stats={'groups':len(groups),'ready_groups':len(ready),'status':dict(Counter(g['status'] for g in groups)),
                'source_coverage_min':min((g['source_coverage'] for g in ready),default=None),
                'iou_quantiles_descriptive_only':{str(q):float(np.quantile([g['iou'] for g in ready],q))
                                                for q in [0,.1,.5,.9,1]} if ready else {},
                'area_inflation_quantiles':{str(q):float(np.quantile([g['output_pixels']/g['source_pixels'] for g in ready],q))
                                           for q in [.1,.5,.9,1]} if ready else {},
                'before_polygons':old_p,'after_polygons':after_p,'before_vertices':old_v,'after_vertices':after_v,
                'polygon_reduction':1-after_p/old_p if old_p else None,'vertex_reduction':1-after_v/old_v if old_v else None,
                'max_polygon_vertices':max((g['vertex_count'] for g in ready),default=0),
                'added_pixels':sum(g['added_pixels'] for g in ready),'deleted_pixels':sum(g['deleted_pixels'] for g in ready),
                'original_hole_pixels_filled':sum(g['filled_original_hole_pixels'] for g in ready),
                'overlap_frames':sum(r['views'][view]['overlap_pixels']>0 for r in rows),
                'overlap_pixels':sum(r['views'][view]['overlap_pixels'] for r in rows),
                'overlap_groups':sum(g['overlap_pixels']>0 for g in ready),
                'rectangle_fallback_groups':sum(g['parameters']['method']=='bounding_rectangle_fallback' for g in ready)}
            if name=='all':info.update(stats)
            else:info['by_resolution'][name]=stats
        summary['views'][view]=info
    return summary


def verify_record(record):
    ctx=_CONTEXT;delivery,config=ctx['delivery'],ctx['config'];size=(record['height'],record['width'])
    counts=Counter()
    for view,paths in record['views'].items():
        doc=json.loads(safe_path(delivery,paths['annotation']).read_text())
        image_path=safe_path(delivery,paths['annotation']).parent/doc['imagePath']
        if (image_path.resolve()!=safe_path(delivery,paths['image']).resolve()
                or doc['imageWidth']!=size[1] or doc['imageHeight']!=size[0]):raise ValueError('Image/JSON pairing failed')
        with Image.open(image_path) as image:
            image.load()
            if image.size!=(size[1],size[0]):raise ValueError('Native grid changed')
        official=ctx['label_file'](str(safe_path(delivery,paths['annotation'])))
        if len(official.shapes)!=len(doc['shapes']) or official.other_data['pallet_segmentation']!=doc['pallet_segmentation']:
            raise ValueError('Official AnyLabeling load lost metadata')
        flags,meta=doc['flags'],doc['pallet_segmentation']
        if (flags['human_reviewed'] or flags['mask_human_reviewed'] or not flags['unreviewed']
                or not flags['proposal_only'] or flags['confirmed_negative']!=(record['source_frame_status']=='confirmed_negative')):
            raise ValueError('Unreviewed/negative status changed')
        if (meta['frame_id']!=record['frame_id'] or meta['original_split']!=record['split']
                or meta['group_id']!=record['group_id'] or meta['view']!=view
                or meta['enclosure_task_hash']!=record['task_hash'] or meta['groups']!=paths['groups']):
            raise ValueError('Frame/source/instance metadata changed')
        if meta['iou_is_acceptance_condition'] or not meta['internal_holes_deliberately_filled']:
            raise ValueError('Enclosure rule differs from user instruction')
        rendered=strict.grouped_pixels(doc['shapes'],size,ctx['upstream'],config)
        expected={g['group_id'] for g in meta['groups'] if g['status']=='enclosure_ready'}
        if set(rendered)!=expected:raise ValueError('Incomplete enclosure grouping')
        shapes=Counter(s['group_id'] for s in doc['shapes']);occupied=np.zeros(size,np.uint16)
        for shape in doc['shapes']:
            group=next(g for g in meta['groups'] if g['group_id']==shape['group_id'])
            if (shape['pallet_mask_part']['pallet_group_id']!=group['pallet_group_id']
                    or shape['pallet_mask_part']['face_id']!=group['face_id']):raise ValueError('Parent/face attribution changed')
        for group in meta['groups']:
            data=safe_path(delivery,group['mask']).read_bytes()
            if hashlib.sha256(data).hexdigest()!=group['mask_sha256']:raise ValueError('Reference mask changed')
            mask=decode_mask(data,size)
            if group['status']=='original_unresolved':
                if mask.any() or group['polygon_count']:raise ValueError('Empty positive status changed')
                continue
            if group['status']!='enclosure_ready' or not mask.any():raise ValueError('Unknown positive status')
            actual=rendered[group['group_id']];evidence,holes=filled_evidence(mask)
            if (evidence&~actual).any():raise ValueError('Original pixels or enclosed holes not fully covered')
            _,remaining_holes=filled_evidence(actual)
            if remaining_holes.any():raise ValueError('Output enclosure contains raster holes')
            metrics=strict.pixel_metrics(mask,actual)
            if metrics!={k:group[k] for k in metrics} or group['source_coverage']!=1:
                raise ValueError('Coverage/change measurements differ')
            if shapes[group['group_id']]!=1 or group['polygon_count']!=1:
                raise ValueError('Expected one closed polygon per nonempty group')
            shape=next(s for s in doc['shapes'] if s['group_id']==group['group_id'])
            if len(shape['points'])!=group['vertex_count'] or len(shape['points'])>24:
                raise ValueError('Polygon vertex cap violated')
            if int(holes.sum())!=group['filled_original_hole_pixels']:raise ValueError('Hole-fill measurement differs')
            occupied+=actual
            counts[view+'_ready']+=1
        overlap=occupied>1
        stored=decode_mask(safe_path(delivery,paths['overlap_mask']).read_bytes(),size)
        if not np.array_equal(overlap,stored) or int(overlap.sum())!=meta['overlap_pixels']:
            raise ValueError('Enclosure overlap diagnostic differs')
        if flags['enclosure_overlap']!=bool(overlap.any()):raise ValueError('Overlap review flag missing')
        for group in meta['groups']:
            actual=rendered.get(group['group_id'],np.zeros(size,bool))
            if int((actual&overlap).sum())!=group['overlap_pixels']:raise ValueError('Group overlap diagnostic differs')
        if record['unresolved_targets'] and (not flags['unresolved_positive'] or flags['confirmed_negative']):
            raise ValueError('Unresolved positive became a negative')
        counts[view+'_groups']+=len(meta['groups'])
    return dict(counts)


def reports(delivery,records,summary):
    lines=['# 叉取面凹包多边形修正包','',
        f"覆盖{summary['frames']:,}帧、{summary['targets']:,}个父托盘，原划分与采集组不变。",'',
        '按用户新规则：填平内部孔洞和破损，保留外轮廓凹陷；所有非空组用一个最多24顶点的闭合多边形覆盖原mask全部像素。IoU没有通过阈值，仅作面积变化记录。', '',
        '| 版本 | 非空组/闭包 | 原片段→新片段 | 原顶点→新顶点 | 覆盖率 | 重叠需复核组 |',
        '|---|---:|---:|---:|---:|---:|']
    for view in VIEWS:
        s=summary['views'][view]
        lines.append(f"| {view} | {s['ready_groups']:,} | {s['before_polygons']:,}→{s['after_polygons']:,} | "
            f"{s['before_vertices']:,}→{s['after_vertices']:,} | {s['source_coverage_min'] or 0:.0%} | {s['overlap_groups']:,} |")
    lines.extend(['','| 版本/分辨率 | 闭包组 | 最大顶点 | 覆盖率 | IoU中位（描述值） | 新增像素 |',
                  '|---|---:|---:|---:|---:|---:|'])
    for view in VIEWS:
        for resolution,s in summary['views'][view]['by_resolution'].items():
            lines.append(f"| {view}/{resolution} | {s['ready_groups']:,} | {s['max_polygon_vertices']} | "
                f"{s['source_coverage_min'] or 0:.0%} | {s['iou_quantiles_descriptive_only'].get('0.5',0):.4f} | {s['added_pixels']:,} |")
    lines.extend(['','凹包比凸包允许外轮廓凹陷。本版枚举凹包紧度、保拓扑简化与向外补偿，在实际像素覆盖率100%、单片≤24顶点的候选中选择填充面积最小者；面积相同再比较几何面积和顶点。这里的最小是固定候选范围内的最小，不宣称任意多边形的全局最优。', '',
        '闭包会新增像素并可能在相邻托盘或面片之间重叠。每个实例单独生成、不跨实例合并；重叠图和JSON标志保留，供人工裁定，不通过删掉原mask像素强行去重。建议优先修正逐托盘版。', '',
        '原263张人工负例保持明确空目标；41个原无mask正例保留父框和未解决身份，不计负例。所有新分割为未人工复核建议，几何闭包覆盖率不代表分割正确率。', '',
        '[Shapely 2.1.2凹包](https://shapely.readthedocs.io/en/2.1.2/reference/shapely.concave_hull.html)，'
        '[AnyLabeling官方填充](https://github.com/vietanhdev/anylabeling/blob/130e44d1b9afd9d54d950cccfd37ce1298ae1a32/anylabeling/views/labeling/utils/shape.py)。'])
    markdown='\n'.join(lines)+'\n';protected_write(delivery/'report_zh.md',markdown.encode())
    from markdown_it import MarkdownIt
    body=MarkdownIt('commonmark').enable('table').render(markdown)
    examples=[]
    for view in VIEWS:
        for resolution in summary['views'][view]['by_resolution']:
            candidates=[r for r in records if f"{r['width']}x{r['height']}"==resolution and r['views'][view]['preview']]
            for record in candidates[:2]:
                examples.append(f'<figure><img loading="lazy" src="{record["views"][view]["preview"]}"><figcaption>'
                    f'{view}/{resolution}/{record["frame_key"]}</figcaption></figure>')
    page='<!doctype html><meta charset="utf-8"><title>叉取面凹包</title><style>'\
        'body{font:16px sans-serif;max-width:1200px;margin:30px auto;background:#f5f6f8;color:#222}'\
        'td,th{border:1px solid #bbb;padding:7px}table{border-collapse:collapse}img{max-width:100%}'\
        'figure{padding:12px;background:white}</style>'+body+''.join(examples)
    protected_write(delivery/'report_zh.html',page.encode())


def instructions(summary):
    return f'''# 填平内部破损的凹包AnyLabeling修正包

推荐解压后在AnyLabeling打开 `pallets/images/train`（或val/test）；逐面版为 `faces/images/<split>`，两版独立保存。

- 所有原非空mask组均提供一个polygon、最多24顶点。保留外轮廓凹陷，内部孔洞和破损不扣除，全部原mask像素被覆盖。IoU没有通过阈值。
- 凹包在固定候选与顶点上限内选填充面积最小者。单个断开的原mask也包进同一个组；不同托盘/面片从不合并身份。
- 原mask在 `reference_masks`；前后对比在 `previews`，左原mask、右凹包。左侧红色表示原无mask正例；同坐标重叠图在 `enclosure_overlaps`。闭包重叠时JSON有 `enclosure_overlap` / `overlap_pixels`，须人工检查。
- 类别 `pallet_fork_face`，group_id及父托盘/面ID保持。覆盖并不表示叉取面身份正确，仍须逐张检查与修正。
- 原263张人工负例才是确认空目标。41个原无mask正例没有可包围像素，保留父框及未解决状态，见 `unresolved_targets.jsonl`；空JSON不能自动视为负例。
- 全部初始human_reviewed=false、mask_human_reviewed=false、unreviewed=true、proposal_only=true。图像已包含180°校正，不再旋转、缩放。
- 保留冻结ZIP，在副本修改并保存JSON。重复导出、verify和package会拒绝覆盖已修改文件。完成后单独交回修正结果及实际检查状态。

{summary['frames']:,}帧、{summary['targets']:,}个父目标；详情见 `report_zh.html`、`summary.json`、`validation.json`。校验未修改文件：`sha256sum -c checksums.sha256`。无分割准确率或正式研究测试结论。
'''


def run_pilot(args,manifest,frames,targets,faces,binding,before):
    selection=strict.select_pilot(frames,targets,args.config_data)
    protected_write(args.output/'pilot_selection.json',encoded(selection));chosen={r['target_id'] for r in selection}
    pilot=SimpleNamespace(**vars(args));pilot.output=args.output/'pilot';pilot.output.mkdir(parents=True,exist_ok=True)
    jobs=[(f,targets[f['frame_id']],faces[f['frame_id']],chosen,True) for f in frames
          if any(t['target_id'] in chosen for t in targets[f['frame_id']])]
    records=[];started=time.monotonic()
    with pooled(pilot,binding,manifest['files'],before) as executor:
        for record in executor.map(convert_frame,jobs,chunksize=1):
            records.append(record)
            if len(records)%16==0:print(json.dumps({'stage':'pilot','frames':len(records),'total':len(jobs)}),flush=True)
    records.sort(key=lambda r:r['frame_id']);summary=summary_records(records,binding)
    previous=args.output/'pilot_report.json'
    summary['wall_seconds']=json.loads(previous.read_text())['wall_seconds'] if previous.exists() else time.monotonic()-started
    protected_write(previous,encoded(summary))
    protected_write(pilot.output/'records.jsonl',b''.join(encoded(r) for r in records))
    reports(pilot.output/'delivery',records,summary)
    initializer(str(args.source),str(pilot.output),args.config_data,binding,manifest['files'],str(args.before),before)
    for record in records:verify_record(record)
    protected_write(args.output/'configuration_freeze.json',encoded({'task_hash':binding['task_hash'],
        'config':args.config_data,'pilot_verified':True,'pilot_selection_sha256':digest(args.output/'pilot_selection.json'),
        'pilot_report_sha256':digest(previous),'iou_threshold_removed_by_user':True}))
    print(json.dumps({'stage':'pilot_complete','summary':summary},ensure_ascii=False),flush=True)


def run_export(args,manifest,frames,targets,faces,binding,before):
    freeze=json.loads((args.output/'configuration_freeze.json').read_text())
    if freeze['task_hash']!=binding['task_hash'] or not freeze['pilot_verified']:raise ValueError('Verified pilot required')
    records=[];started=time.monotonic()
    jobs=[(f,targets[f['frame_id']],faces[f['frame_id']],None,i%128==0) for i,f in enumerate(frames)]
    with pooled(args,binding,manifest['files'],before) as executor:
        for record in executor.map(convert_frame,jobs,chunksize=1):
            records.append(record)
            if len(records)%128==0 or len(records)==len(frames):
                status={'stage':'export','completed':len(records),'total':len(frames),
                        'elapsed_seconds':round(time.monotonic()-started,2),'task_hash':binding['task_hash']}
                print(json.dumps(status),flush=True);save_status(args.output/'workflow_status.json',status)
    records.sort(key=lambda r:r['frame_id']);delivery=args.output/'delivery'
    protected_write(delivery/'frames.jsonl',b''.join(encoded(r) for r in records))
    parents,groups,unresolved=[],[],[]
    for r in records:
        for parent in r['parents']:
            row={'frame_id':r['frame_id'],'frame_key':r['frame_key'],'split':r['split'],'group_id':r['group_id'],
                 'proposal_only':True,'mask_human_reviewed':False,**parent}
            parents.append(row)
            if parent['status']=='original_unresolved':unresolved.append({**row,'segmentation_negative':False})
        for view in VIEWS:
            for group in r['views'][view]['groups']:
                groups.append({'frame_id':r['frame_id'],'frame_key':r['frame_key'],'split':r['split'],
                               'capture_group_id':r['group_id'],'view':view,**group})
    for name,rows in [('targets',parents),('groups',groups),('unresolved_targets',unresolved)]:
        protected_write(delivery/(name+'.jsonl'),b''.join(encoded(r) for r in rows))
    for name in ['frames.jsonl','targets.jsonl','faces.jsonl','summary.json']:
        protected_write(delivery/'provenance'/name,read_checked(args.source,name,manifest['files']))
    for name in ['run_manifest.json','configuration_freeze.json','pilot_selection.json','pilot_report.json']:
        protected_write(delivery/'provenance'/name,(args.output/name).read_bytes())
    for module in [Path(__file__),Path(strict.__file__),Path(strict.__file__).with_name('fork_face_anylabeling.py')]:
        protected_write(delivery/'source'/module.name,module.read_bytes())
    protected_write(delivery/'source/config.json',encoded(args.config_data))
    for key,name in [('upstream_shape_source','anylabeling_shape.py'),('upstream_label_source','anylabeling_label_file.py')]:
        protected_write(delivery/'source'/name,Path(args.config_data[key]).read_bytes())
    import importlib.metadata
    packages=['numpy','shapely','opencv-python-headless','pillow','markdown-it-py','threadpoolctl']
    protected_write(delivery/'source/requirements.txt', ''.join(f'{p}=={importlib.metadata.version(p)}\n' for p in packages).encode())
    tests=Path(__file__).resolve().parent.parent/'tests/test_fork_face_enclosure.py'
    if tests.exists():protected_write(delivery/'source'/tests.name,tests.read_bytes())
    for view in VIEWS:protected_write(delivery/view/'labels.txt',(LABEL+'\n').encode())
    summary=summary_records(records,binding)
    protected_write(delivery/'summary.json',encoded(summary));protected_write(delivery/'README_zh.md',instructions(summary).encode())
    protected_write(delivery/'manifest.json',encoded({'binding':binding,'summary':summary}))
    reports(delivery,records,summary);save_status(args.output/'workflow_status.json',{'stage':'export_complete',**summary})
    return records


def run_verify(args,manifest,frames,binding,before):
    records=[completed(args.output,f,binding) for f in frames]
    if any(r is None for r in records):raise ValueError('Incomplete export')
    targets=[p['target_id'] for r in records for p in r['parents']]
    if len(targets)!=manifest['summary']['targets'] or len(set(targets))!=len(targets):raise ValueError('Parent coverage failed')
    counts=Counter()
    with pooled(args,binding,manifest['files'],before) as executor:
        for i,result in enumerate(executor.map(verify_record,records,chunksize=8),1):
            counts.update(result)
            if i%1024==0 or i==len(records):print(json.dumps({'stage':'verify','completed':i,'total':len(records)}),flush=True)
    used={'frames.jsonl','targets.jsonl','faces.jsonl','summary.json'}
    for r in records:
        used.add(f"images/{r['split']}/{r['frame_key']}.png")
        used.update(p[len('reference_'):] for p in r['files'] if p.startswith('reference_'))
    for p in sorted(used):
        if digest(safe_path(args.source,p))!=manifest['files'][p]:raise ValueError('Frozen source changed')
    original_manifest=args.source.parent/'run_manifest.json';protected_count=0
    if original_manifest.exists():
        parent=json.loads(original_manifest.read_text())
        for p,expected in parent.get('protected_files',{}).items():
            if digest(safe_path(Path(parent['binding']['dataset']),p))!=expected:raise ValueError('Formal detection labels changed')
            protected_count+=1
    import tempfile
    label_file=strict.load_label_file(args.config_data['upstream_label_source'],args.config_data['upstream_label_sha256'])
    saved=0
    with tempfile.TemporaryDirectory(prefix='enclosure-save-') as directory:
        for line in (args.output/'pilot/records.jsonl').open():
            record=json.loads(line)
            for view in VIEWS:
                strict.check_label_save(args.output/'pilot/delivery'/record['views'][view]['annotation'],
                                        Path(directory)/'saved.json',label_file);saved+=1
    delivery=args.output/'delivery'
    assets={str(p.relative_to(delivery)):digest(p) for p in sorted(delivery.rglob('*')) if p.is_file()
            and str(p.relative_to(delivery)) not in ('verified_assets.json','validation.json','checksums.sha256')}
    protected_write(delivery/'verified_assets.json',encoded({'task_hash':binding['task_hash'],'files':assets}))
    result={'state':'passed','task_hash':binding['task_hash'],'frames_checked':len(records),'parent_targets_checked':len(targets),
        'view_counts':dict(counts),'official_anylabeling_load_documents':len(records)*2,
        'official_anylabeling_save_documents':saved,'gui_application_tested':False,
        'original_source_coverage_100_percent':True,'original_holes_filled':True,
        'one_polygon_per_nonempty_group':True,'polygon_vertex_limit_24_passed':True,
        'iou_is_acceptance_condition':False,'overlap_diagnostics_verified':True,
        'used_source_files_unchanged':len(used),'protected_formal_files_unchanged':protected_count,
        'verified_assets_sha256':digest(delivery/'verified_assets.json')}
    protected_write(delivery/'validation.json',encoded(result));protected_write(args.output/'validation.json',encoded(result))
    save_status(args.output/'workflow_status.json',{'stage':'verified',**result});return result


def run_package(args,binding):
    output,delivery=args.output,args.output/'delivery'
    validation,assets=strict.recheck_verified(output,binding)
    receipt_path=output/'delivery_manifest.json'
    if receipt_path.exists():
        receipt=json.loads(receipt_path.read_text())
        if receipt['task_hash']!=binding['task_hash'] or digest(output/receipt['archive_name'])!=receipt['archive_sha256']:
            raise ValueError('Published archive changed')
        return receipt
    check_space(output)
    protected_write(delivery/'checksums.sha256',''.join(f'{sha}  {p}\n' for p,sha in sorted(assets.items())).encode())
    assets['checksums.sha256']=digest(delivery/'checksums.sha256')
    archive=output/'pallet_fork_face_anylabeling_enclosure_v2.zip';temporary=archive.with_suffix('.partial')
    with zipfile.ZipFile(temporary,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=4) as z:
        for i,(p,expected) in enumerate(sorted(assets.items())):
            if i%512==0:check_space(output)
            path=safe_path(delivery,p)
            if digest(path)!=expected:raise ValueError('Workspace changed during packaging')
            z.write(path,p)
    import tempfile
    label_file=strict.load_label_file(args.config_data['upstream_label_source'],args.config_data['upstream_label_sha256'])
    portable=0;seen=set()
    with zipfile.ZipFile(temporary) as z:
        if len(z.namelist())!=len(assets) or z.testzip() is not None:raise ValueError('ZIP CRC/count failed')
        for p,expected in assets.items():
            if hashlib.sha256(z.read(p)).hexdigest()!=expected:raise ValueError('ZIP member hash failed')
        with tempfile.TemporaryDirectory(prefix='enclosure-portable-') as directory:
            for line in (delivery/'frames.jsonl').open():
                r=json.loads(line)
                for view in VIEWS:
                    key=(r['split'],r['width'],r['height'],view,r['source_frame_status'],bool(r['views'][view]['overlap_pixels']))
                    if key in seen:continue
                    seen.add(key)
                    for p in [r['views'][view]['annotation'],r['views'][view]['image']]:
                        path=safe_path(Path(directory),p);path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(z.read(p))
                    strict.check_label_save(Path(directory)/r['views'][view]['annotation'],Path(directory)/'saved.json',label_file);portable+=1
    temporary.replace(archive);sha=digest(archive)
    protected_write(archive.with_suffix('.zip.sha256'),f'{sha}  {archive.name}\n'.encode())
    receipt={'state':'complete','task_hash':binding['task_hash'],'archive_name':archive.name,
        'archive_bytes':archive.stat().st_size,'archive_sha256':sha,'zip_members':len(assets),
        'crc_passed':True,'all_member_sha256_passed':True,'portable_load_save_documents':portable,
        'validated_files_rechecked_before_package':True,'validation':validation,
        'summary':json.loads((delivery/'summary.json').read_text()),'finished_at':strict.timestamp()}
    save_status(receipt_path,receipt);save_status(output/'workflow_status.json',receipt)
    print(json.dumps(receipt,ensure_ascii=False),flush=True);return receipt


def main(argv=None):
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--stage',choices=['pilot','export','verify','package','all'],default='all')
    parser.add_argument('--source',type=Path,default=Path('outputs/pallet_fork_face_masks_v1/delivery'))
    parser.add_argument('--output',type=Path,default=Path('outputs/pallet_fork_face_anylabeling_enclosure_v2'))
    parser.add_argument('--before',type=Path,default=Path('outputs/pallet_fork_face_anylabeling_v1'))
    parser.add_argument('--config',type=Path,default=Path('configs/fork_face_enclosure_v2.json'))
    parser.add_argument('--workers',type=int,default=8)
    args=parser.parse_args(argv)
    if not 1<=args.workers<=8:parser.error('workers must be in [1,8]')
    args.source,args.output,args.before=args.source.resolve(),args.output.resolve(),args.before.resolve()
    if any(args.output.is_relative_to(p) or p.is_relative_to(args.output) for p in [args.source,args.before]):
        parser.error('Output must be independent of source masks and old workspace')
    args.config_data=json.loads(args.config.read_text());config=args.config_data
    if (set(config)!=set(DEFAULT_CONFIG) or shapely.__version__!='2.1.2' or config['shapely_version']!='2.1.2'
            or config['minimum_source_coverage']!=1 or not config['fill_internal_holes']
            or config['iou_acceptance_threshold'] is not None or config['hull']!='concave'
            or not 3<=config['max_polygon_vertices']<=24 or config['max_group_polygons']!=1):
        raise ValueError('Config/dependencies differ from approved covering rule')
    load_upstream(config['upstream_shape_source'],config['upstream_shape_sha256'])
    strict.load_label_file(config['upstream_label_source'],config['upstream_label_sha256'])
    args.output.mkdir(parents=True,exist_ok=True);check_space(args.output)
    manifest,frames,targets,faces=load_input(args.source)
    before_path=args.before/'delivery/frames.jsonl'
    before_rows=[json.loads(l) for l in before_path.read_bytes().splitlines()] if before_path.exists() else []
    before={r['frame_id']:r for r in before_rows}
    import importlib.metadata
    packages=['shapely','numpy','opencv-python-headless','pillow','markdown-it-py','threadpoolctl']
    binding={'algorithm':'single_hole_free_concave_cover_v2','config':config,
        'source_export_manifest_sha256':digest(args.source/'export_manifest.json'),'source_task_hash':manifest['task_hash'],
        'source_code':{p.name:digest(p) for p in [Path(__file__),Path(strict.__file__),Path(strict.__file__).with_name('fork_face_anylabeling.py')]},
        'before_index_sha256':digest(before_path) if before_path.exists() else None,
        'dependencies':{p:importlib.metadata.version(p) for p in packages},'geos':shapely.geos_version_string,
        'user_rule':'fill internal damage; concave enclosure; IoU not required','views':list(VIEWS)}
    binding['task_hash']=hashlib.sha256(encoded(binding)).hexdigest()
    protected_write(args.output/'run_manifest.json',encoded(binding))
    for stage in ['pilot','export','verify','package'] if args.stage=='all' else [args.stage]:
        if stage=='pilot':run_pilot(args,manifest,frames,targets,faces,binding,before)
        elif stage=='export':run_export(args,manifest,frames,targets,faces,binding,before)
        elif stage=='verify':run_verify(args,manifest,frames,binding,before)
        else:run_package(args,binding)


if __name__=='__main__':main()
