"""Bounded, editable AnyLabeling polygons derived from frozen fork-face masks.

The raster produced by the pinned AnyLabeling function is the acceptance
criterion. Geometry is only a candidate generator; difficult positives remain
explicit redraw tasks rather than empty segmentation negatives.
"""
from __future__ import annotations

import argparse
import ast
import base64
from collections import Counter, defaultdict
from concurrent.futures import ProcessPoolExecutor
from contextlib import contextmanager
from datetime import datetime
import hashlib
import html
import io
import json
import logging
import math
import os
from pathlib import Path
import shutil
import time
from types import SimpleNamespace
import zipfile

import cv2
import numpy as np
import PIL
from PIL import Image, ImageDraw, ImageOps
import shapely
import importlib.metadata
from shapely import LineString, Polygon, box, make_valid, simplify, union_all
from shapely.ops import split
from shapely.affinity import translate

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': 1, 'seed': 42, 'pilot_targets_per_resolution': 32,
    'minimum_iou': .95, 'max_polygon_vertices': 24,
    'max_group_polygons': 12, 'max_group_vertices': 128,
    'delete_component_area_below': [0, 2, 4, 8],
    'fill_hole_area_at_most': [0, 1, 4, 9],
    'tolerances': [0, .25, .5, .75, 1, 1.5, 2],
    'protected_span_fraction': .25,
    'foreground_connectivity': 8, 'background_connectivity': 4,
    'shapely_version': '2.1.2',
    'upstream_shape_source': 'outputs/fork_face_anylabeling_tooling/anylabeling__views__labeling__utils__shape.py',
    'upstream_shape_sha256': '77a574e131d4e6bbbd4291859317a4bc59bacb6e64111cdcd0880065472015bc',
    'upstream_label_source': 'outputs/fork_face_anylabeling_tooling/anylabeling__views__labeling__label_file.py',
    'upstream_label_sha256': '6ffd52de45420952576cf0cf30718ab637e0ad8375c241f1800ee7ccb2f59e92',
    'upstream_commit': '130e44d1b9afd9d54d950cccfd37ce1298ae1a32',
}
_CONTEXT = {}


def timestamp():
    from zoneinfo import ZoneInfo
    return datetime.now(ZoneInfo('Asia/Shanghai')).isoformat()


def polygon_parts(geometry):
    if geometry.is_empty:
        return []
    if geometry.geom_type == 'Polygon':
        return [geometry]
    if hasattr(geometry, 'geoms'):
        return [p for g in geometry.geoms for p in polygon_parts(g)]
    return []


def mask_geometry(mask):
    """Extract fresh pixel-centre rings, including enclosed holes.

    Zero-area contours (single pixels/lines) use bounded pixel rectangles.
    Line remnants of repaired self-touching contours are restored the same way.
    No dilation, convex hull or joining of separate instances is performed.
    """
    contours, hierarchy = cv2.findContours(mask.astype(np.uint8), cv2.RETR_CCOMP,
                                          cv2.CHAIN_APPROX_SIMPLE)
    if hierarchy is None:
        return []
    hierarchy = hierarchy[0]
    result = []
    for i, contour in enumerate(contours):
        if hierarchy[i, 3] != -1:
            continue
        ring = contour[:, 0, :].astype(float)
        if len(ring) < 3 or cv2.contourArea(contour) == 0:
            # Only degenerate contours take the run-rectangle path.
            pixels = np.zeros(mask.shape, np.uint8)
            cv2.drawContours(pixels, [contour], -1, 1, 1)
            result.extend(run_rectangles(pixels.astype(bool)))
            continue
        holes = []
        child = hierarchy[i, 2]
        while child >= 0:
            points = contours[child][:, 0, :]
            if len(points) >= 3 and cv2.contourArea(contours[child]) > 0:
                holes.append(points)
            child = hierarchy[child, 0]
        geometry = Polygon(ring, holes)
        repaired = geometry if geometry.is_valid else make_valid(geometry)
        result.extend(polygon_parts(repaired))
        # Polygon repair may return measured line branches; keep them.
        if repaired.geom_type == 'GeometryCollection':
            for g in repaired.geoms:
                if g.geom_type in ('LineString', 'MultiLineString'):
                    lines = [g] if g.geom_type == 'LineString' else g.geoms
                    pixels = np.zeros(mask.shape, np.uint8)
                    for line in lines:
                        pts = np.rint(np.asarray(line.coords)).astype(np.int32)
                        cv2.polylines(pixels, [pts], False, 1, 1)
                    result.extend(run_rectangles(pixels.astype(bool) & mask))
    return result


def run_rectangles(mask):
    rectangles, active = [], {}
    for y in range(mask.shape[0]+1):
        if y == mask.shape[0]:
            runs = set()
        else:
            row = np.pad(mask[y].astype(np.int8), (1, 1))
            starts = np.flatnonzero(np.diff(row) == 1)
            ends = np.flatnonzero(np.diff(row) == -1)
            runs = set(zip(starts.tolist(), ends.tolist()))
        for (x, e), top in list(active.items()):
            if (x, e) not in runs:
                rectangles.append(box(float(x), float(top), float(e)-.125, float(y)-.125))
                del active[x, e]
        for run in runs:
            active.setdefault(run, y)
    return rectangles


def cell_geometry(mask):
    """Valid pixel-cell rings also support pinches and one-pixel branches.

    The inset and translation put each orthogonal boundary inside its last
    measured pixel under PIL's integer convention. Actual raster QA remains
    mandatory, especially after Douglas--Peucker introduces diagonal edges.
    """
    cells, active = [], {}
    for y in range(mask.shape[0]+1):
        runs = set()
        if y < mask.shape[0]:
            row = np.pad(mask[y].astype(np.int8), (1, 1))
            runs = set(zip(np.flatnonzero(np.diff(row) == 1).tolist(),
                           np.flatnonzero(np.diff(row) == -1).tolist()))
        for (x, end), top in list(active.items()):
            if (x, end) not in runs:
                cells.append(box(x, top, end, y))
                del active[x, end]
        for run in runs:
            active.setdefault(run, y)
    if not cells:
        return []
    geometry = translate(union_all(cells).buffer(-.375, join_style='mitre'),
                         xoff=-.375, yoff=-.375)
    return polygon_parts(geometry)


def component_evidence(mask, bbox, config):
    """Compute protected thin foreground and enclosed background once."""
    _, foreground, fs, _ = cv2.connectedComponentsWithStats(mask.astype(np.uint8),
                                                           connectivity=8)
    padded = np.pad(~mask, 1, constant_values=True).astype(np.uint8)
    _, background, hs, _ = cv2.connectedComponentsWithStats(padded, connectivity=4)
    outside = int(background[0, 0])
    background = background[1:-1, 1:-1]
    fraction = config['protected_span_fraction']
    parent_w, parent_h = max(float(bbox['width']), 1), max(float(bbox['height']), 1)
    protected_components, protected_holes, hole_ids = set(), set(), []
    for i, (_, _, w, h, area) in enumerate(fs[1:], 1):
        spanning = w >= fraction*parent_w or h >= fraction*parent_h
        slender = min(w, h) <= 2 or (max(w/h, h/w) >= 4 and area/(w*h) < .35)
        if spanning and slender:
            protected_components.add(i)
    for i, (_, _, w, h, _) in enumerate(hs[1:], 1):
        if i == outside:
            continue
        hole_ids.append(i)
        if w >= fraction*parent_w or h >= fraction*parent_h:
            protected_holes.add(i)
    return foreground, fs, background, hs, hole_ids, protected_components, protected_holes


def cleanup_variants(mask, bbox, config):
    fg, fs, bg, hs, hole_ids, protected_fg, protected_holes = component_evidence(mask, bbox, config)
    seen = set()
    keep_foreground = np.isin(fg, list(protected_fg)) if protected_fg else np.zeros_like(mask)
    for delete_area in config['delete_component_area_below']:
        removed = [i for i in range(1, len(fs)) if fs[i, cv2.CC_STAT_AREA] < delete_area
                   and i not in protected_fg]
        stripped = mask & ~np.isin(fg, removed) if removed else mask.copy()
        for hole_area in config['fill_hole_area_at_most']:
            filled = [i for i in hole_ids if hs[i, cv2.CC_STAT_AREA] <= hole_area
                      and i not in protected_holes]
            cleaned = stripped | np.isin(bg, filled) if filled else stripped.copy()
            sha = hashlib.sha256(cleaned.tobytes()).digest()
            if sha in seen:
                continue
            seen.add(sha)
            # All retained holes, not just the spanning ones, must remain empty.
            retained_holes = [i for i in hole_ids if i not in filled]
            holes = np.isin(bg, retained_holes) if retained_holes else np.zeros_like(mask)
            yield cleaned, keep_foreground, holes, {
                'delete_component_area_below': delete_area,
                'fill_hole_area_at_most': hole_area,
                'removed_components': len(removed), 'filled_holes': len(filled),
                'protected_thin_components': len(protected_fg),
                'protected_spanning_holes': len(protected_holes),
            }


def vertices(polygon):
    return len(polygon.exterior.coords)-1


def split_holes(geometries, max_pieces):
    """Cut through hole interiors; resulting ordinary polygons share boundaries."""
    pending, done = list(geometries), []
    attempts = 0
    while pending:
        polygon = pending.pop()
        if not polygon.interiors:
            done.append(polygon)
            continue
        attempts += 1
        if attempts > 256:
            return None
        hole = max((Polygon(r) for r in polygon.interiors), key=lambda h: h.area)
        centre = hole.representative_point()
        minx, miny, maxx, maxy = polygon.bounds
        candidates = []
        for line in (LineString([(minx-1, centre.y), (maxx+1, centre.y)]),
                     LineString([(centre.x, miny-1), (centre.x, maxy+1)])):
            parts = polygon_parts(split(polygon, line))
            remaining = sum(len(p.interiors) for p in parts)
            if len(parts) > 1 and remaining < len(polygon.interiors):
                candidates.append((remaining, len(parts), sum(vertices(p) for p in parts), parts))
        if not candidates:
            return None
        pending.extend(min(candidates, key=lambda c: c[:3])[3])
        # Pieces cannot be below this bound without recreating a retained hole.
        if len(done) + len(pending) > max_pieces*4:
            return None
    return done


def merge_pieces(pieces, config):
    """Only merge touching pieces if the union is valid and has no hole."""
    pieces = sorted(pieces, key=lambda p: (*p.bounds, p.wkb))
    while True:
        choices = []
        for i in range(len(pieces)):
            for j in range(i+1, len(pieces)):
                a, b = pieces[i], pieces[j]
                if not a.intersects(b) or a.boundary.intersection(b.boundary).length <= 1e-8:
                    continue
                joined = a.union(b)
                if (joined.geom_type == 'Polygon' and joined.is_valid and not joined.interiors
                        and vertices(joined) <= config['max_polygon_vertices']):
                    saved = vertices(a)+vertices(b)-vertices(joined)
                    if saved >= 0:
                        choices.append((-saved, vertices(joined), i, j, joined))
        if not choices:
            break
        _, _, i, j, joined = min(choices, key=lambda c: c[:4])
        pieces = [p for k, p in enumerate(pieces) if k not in (i, j)] + [joined]
    return pieces


def bounded_pieces(geometries, config):
    parts = split_holes(geometries, config['max_group_polygons'])
    if parts is None:
        return None
    parts = merge_pieces(parts, config)
    # A long simple ring can be split without changing its geometry.
    pending, done = parts, []
    while pending:
        polygon = pending.pop()
        if vertices(polygon) <= config['max_polygon_vertices']:
            done.append(polygon)
            continue
        minx, miny, maxx, maxy = polygon.bounds
        if maxx-minx >= maxy-miny:
            middle = (minx+maxx)/2
            line = LineString([(middle, miny-1), (middle, maxy+1)])
        else:
            middle = (miny+maxy)/2
            line = LineString([(minx-1, middle), (maxx+1, middle)])
        cut = polygon_parts(split(polygon, line))
        if len(cut) < 2 or max(vertices(p) for p in cut) >= vertices(polygon):
            return None
        pending.extend(cut)
        if len(pending)+len(done) > config['max_group_polygons']:
            return None
    if (len(done) > config['max_group_polygons']
            or sum(vertices(p) for p in done) > config['max_group_vertices']):
        return None
    return sorted(done, key=lambda p: (*p.bounds, p.wkb))


def fill_polygons(polygons, size, upstream):
    result = np.zeros(size, bool)
    for points in polygons:
        result |= upstream(size, points, 'polygon')
    return result


def pixel_metrics(original, actual):
    intersection = int((original & actual).sum())
    union = int((original | actual).sum())
    return {'iou': intersection/union if union else None,
            'source_pixels': int(original.sum()), 'output_pixels': int(actual.sum()),
            'added_pixels': int((actual & ~original).sum()),
            'deleted_pixels': int((original & ~actual).sum())}


def simplify_mask(original, bbox, config, upstream, forbidden=None):
    """Enumerate cleaned/simplified candidates and select by actual filled pixels."""
    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': [], 'iou': None,
                'reason': 'original_empty_positive', 'source_pixels': 0,
                'output_pixels': 0, 'added_pixels': 0, 'deleted_pixels': 0,
                'polygon_count': 0, 'vertex_count': 0, 'candidates_checked': 0}
    ys, xs = np.nonzero(original)
    y0, y1 = max(int(ys.min())-1, 0), min(int(ys.max())+2, original.shape[0])
    x0, x1 = max(int(xs.min())-1, 0), min(int(xs.max())+2, original.shape[1])
    mask = original[y0:y1, x0:x1]
    prohibited = (np.asarray(forbidden, bool)[y0:y1, x0:x1] if forbidden is not None
                  else np.zeros_like(mask))
    best, best_key, checked = None, None, 0
    failures = Counter()
    cache = set()
    for cleaned, protected_fg, retained_holes, cleanup in cleanup_variants(mask, bbox, config):
        cache.clear()
        # Each disconnected component needs at least one ordinary polygon.
        # Topology-preserving simplification cannot join them. This exact lower
        # bound avoids expensive geometry for clearly impossible candidates.
        components = cv2.connectedComponents(cleaned.astype(np.uint8),connectivity=8)[0]-1
        if components > config['max_group_polygons']:
            failures['disconnected_component_lower_bound'] += 1
            continue
        for representation, geometries in [('pixel_centres', mask_geometry(cleaned)),
                                           ('pixel_cells', cell_geometry(cleaned))]:
          if len(geometries) > config['max_group_polygons']:
            failures['too_many_disconnected_components'] += 1
            continue
          for tolerance in config['tolerances']:
            candidate_geometry = [p for g in geometries
                                  for p in polygon_parts(simplify(g, tolerance, preserve_topology=True))]
            signature = tuple(p.wkb for p in candidate_geometry)
            if signature in cache:
                continue
            cache.add(signature)
            pieces = bounded_pieces(candidate_geometry, config)
            if not pieces:
                failures['complexity_limit'] += 1
                continue
            polygons = [np.round(np.asarray(p.exterior.coords[:-1], float), 8).tolist() for p in pieces]
            if any(not p.is_valid or p.interiors or p.area <= 0 for p in pieces):
                failures['invalid_geometry'] += 1
                continue
            if any(min(v[0] for v in p) < 0 or min(v[1] for v in p) < 0
                   or max(v[0] for v in p) >= mask.shape[1]
                   or max(v[1] for v in p) >= mask.shape[0] for p in polygons):
                failures['outside_grid'] += 1
                continue
            actual = fill_polygons(polygons, mask.shape, upstream)
            checked += 1
            metrics = pixel_metrics(mask, actual)
            if metrics['iou'] < config['minimum_iou']:
                failures['insufficient_iou'] += 1
                continue
            if (actual & retained_holes).any():
                failures['retained_hole_filled'] += 1
                continue
            if (protected_fg & ~actual).any():
                failures['thin_structure_lost'] += 1
                continue
            if (actual & prohibited).any():
                failures['instance_overlap'] += 1
                continue
            total = sum(len(p) for p in polygons)
            key = (total, len(polygons), -metrics['iou'])
            if best is None or key < best_key:
                best_key = key
                best = {'status': 'simplified_accepted', 'polygons':
                        [[[round(x+x0, 8), round(y+y0, 8)] for x, y in p] for p in polygons],
                        **metrics, 'polygon_count': len(polygons), 'vertex_count': total,
                        'tolerance': tolerance, 'cleanup': cleanup,
                        'ring_representation': representation}
    if best is None:
        best = {'status': 'redraw_required', 'polygons': [], 'iou': None,
                'source_pixels': int(original.sum()), 'output_pixels': 0,
                'added_pixels': None, 'deleted_pixels': None, 'polygon_count': 0,
                'vertex_count': 0, 'reason': 'no_candidate_satisfies_iou_complexity_topology_ownership'}
    best['candidates_checked'] = checked
    best['rejected_candidates'] = dict(failures)
    return best


def initializer(source, output, config, binding, hashes, before_root, 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_root),
                'before_records': before_records,
                'upstream': load_upstream(config['upstream_shape_source'], config['upstream_shape_sha256']),
                'label_file': load_label_file(config['upstream_label_source'], config['upstream_label_sha256'])}


def old_group_counts(frame, view):
    """Read only frozen old polygons for counts, never as a simplification input."""
    ctx = _CONTEXT
    record = ctx['before_records'].get(frame['frame_id'])
    if not record:
        return {}
    relative = record['views'][view]['annotation']
    path = safe_path(ctx['before_root']/'delivery', relative)
    data = path.read_bytes()
    expected = record['files'][relative]
    if hashlib.sha256(data).hexdigest() != expected:
        # A human edit in the old workspace is preserved; use frozen ZIP bytes.
        with zipfile.ZipFile(ctx['before_root']/'pallet_fork_face_anylabeling_v1.zip') as archive:
            data = archive.read(relative)
        if hashlib.sha256(data).hexdigest() != expected:
            raise ValueError('Frozen baseline archive changed')
    counts = defaultdict(lambda: [0, 0])
    for shape in json.loads(data)['shapes']:
        counts[shape['group_id']][0] += 1
        counts[shape['group_id']][1] += len(shape['points'])
    return dict(counts)


def render_preview(image, groups, masks, polygons, size):
    """Side-by-side original evidence and editable output; original PNG is untouched."""
    native = Image.open(io.BytesIO(image)).convert('RGB')
    left = np.asarray(native).copy()
    right = left.copy()
    for group in groups:
        group_id = group['group_id']
        original = masks[group_id]
        color = np.array([255, 72, 48] if group['status'] != 'simplified_accepted'
                         else [32, 215, 130], dtype=np.uint8)
        left[original] = (left[original]*.45+color*.55).astype(np.uint8)
        if polygons[group_id]:
            actual = fill_polygons(polygons[group_id], size, _CONTEXT['upstream'])
            right[actual] = (right[actual]*.45+color*.55).astype(np.uint8)
    scale = max(1, min(4, 640//size[1]))
    tiles = []
    for pixels in (left, right):
        tile = Image.fromarray(pixels).resize((size[1]*scale, size[0]*scale), Image.Resampling.NEAREST)
        tiles.append(tile)
    canvas = Image.new('RGB', (tiles[0].width*2, tiles[0].height+36), (25, 28, 35))
    canvas.paste(tiles[0], (0, 36)); canvas.paste(tiles[1], (tiles[0].width, 36))
    draw = ImageDraw.Draw(canvas)
    draw.text((8, 8), 'Original mask evidence', fill='white')
    draw.text((tiles[0].width+8, 8), 'Editable polygons; RED = redraw task', fill='white')
    for group in groups:
        b = group['bbox']
        coords = (b['x']*scale, b['y']*scale+36,
                  (b['x']+b['width'])*scale, (b['y']+b['height'])*scale+36)
        color = (255, 80, 50) if group['status'] != 'simplified_accepted' else (32, 215, 130)
        for offset in (0, tiles[0].width):
            draw.rectangle((coords[0]+offset, coords[1], coords[2]+offset, coords[3]), outline=color)
            draw.text((coords[0]+offset, coords[1]), f"g{group['group_id']}", fill=color)
    buffer = io.BytesIO(); canvas.save(buffer, format='PNG')
    return buffer.getvalue()


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

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

    target_map = {t['target_id']: t for t in targets}
    for view in VIEWS:
        items = sorted(faces if view == 'faces' else targets,
                       key=lambda i: i['face_id'] if view == 'faces' else i['box_index'])
        groups, polygons, masks, data_masks = [], {}, {}, {}
        ownership = np.zeros(size, np.int32)
        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)
            group_id = item['face_id'] if view == 'faces' else item['box_index']+1
            if group_id in masks:
                raise ValueError('Duplicate source group')
            if (ownership[mask] > 0).any():
                raise ValueError('Frozen source instances overlap')
            ownership[mask] = group_id
            masks[group_id], data_masks[group_id] = mask, data
        counts_before = old_group_counts(frame, view)
        occupied = np.zeros(size, bool)
        for item in items:
            if selected_targets is not None and item['target_id'] not in selected_targets:
                continue
            original = item['mask'] if view == 'faces' else item['union_mask']
            group_id = item['face_id'] if view == 'faces' else item['box_index']+1
            mask = masks[group_id]
            if not mask.any() and (view == 'faces' or item['status'] != 'unresolved_no_mask'):
                raise ValueError('Unexpected empty positive candidate')
            parent = target_map[item['target_id']]
            forbidden = ((ownership > 0) & (ownership != group_id)) | occupied
            result = simplify_mask(mask, parent['bbox'], ctx['config'], ctx['upstream'], forbidden)
            points = result.pop('polygons')
            actual = fill_polygons(points, size, ctx['upstream'])
            if result['status'] == 'simplified_accepted':
                measured = pixel_metrics(mask, actual)
                if measured != {k: result[k] for k in measured} or (actual & forbidden).any():
                    raise ValueError('Native-coordinate raster changed acceptance')
                occupied |= actual
            polygons[group_id] = points
            reference = 'reference_'+original
            write(reference, data_masks[group_id])
            before_polygons, before_vertices = counts_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': parent['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': before_polygons, 'before_vertex_count': before_vertices,
                **result})
        chosen = [t for t in targets if selected_targets is None or t['target_id'] in selected_targets]
        document = make_document(frame, chosen, groups, polygons, view)
        redraw = [g for g in groups if g['status'] != 'simplified_accepted']
        empty_parents = [t for t in chosen if t['status']=='unresolved_no_mask']
        document['flags']['redraw_required'] = bool(redraw or empty_parents)
        document['flags']['unresolved_positive'] = bool(redraw) or any(t['status']=='unresolved_no_mask' for t in chosen)
        document['pallet_segmentation'].update({
            'schema_version': 2, 'simplification_task_hash': ctx['binding']['task_hash'],
            'holes_preserved_by_grouped_polygon_pieces': False,
            'retained_holes_preserved': True, 'minimum_iou': ctx['config']['minimum_iou'],
            'complexity_limits': {k: ctx['config'][k] for k in
                                ('max_polygon_vertices', 'max_group_polygons', 'max_group_vertices')},
            'redraw_group_ids': [g['group_id'] for g in redraw],
            'empty_shapes_do_not_mean_negative': frame['frame_status'] != 'confirmed_negative',
            'redraw_positive_target_ids': sorted({g['target_id'] for g in redraw}),
        })
        if redraw or empty_parents:
            document['text'] = f"{len(redraw)} groups, {len(empty_parents)} empty parents need redraw: see pallet_segmentation and redraw_previews"
        parent_statuses = {}
        for parent in document['pallet_segmentation']['parents']:
            same = [g['status'] for g in groups if g['target_id'] == parent['target_id']]
            if parent['status'] == 'unresolved_no_mask':
                status = 'original_unresolved'
            elif same and all(s == 'simplified_accepted' for s in same):
                status = 'simplified_accepted'
            else:
                status = 'redraw_required'
            parent['simplification_status'] = status
            parent_statuses[parent['target_id']] = status
        prefix = f"{view}/images/{frame['split']}/{frame['frame_key']}"
        write(prefix+'.png', image); write(prefix+'.json', encoded(document))
        preview = None
        if redraw or empty_parents or preview_all:
            preview = f"redraw_previews/{view}/{frame['split']}/{frame['frame_key']}.png"
            preview_groups = list(groups)
            for parent in empty_parents:
                if any(g['target_id']==parent['target_id'] for g in groups):
                    continue
                marker = 'parent_'+str(parent['box_index']+1)
                preview_groups.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, render_preview(image, preview_groups, masks, polygons, size))
        view_records[view] = {'image': prefix+'.png', 'annotation': prefix+'.json',
            'groups': groups, 'parents': parent_statuses, 'preview': preview,
            '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'],
        'domain': frame.get('domain'), 'group_id': frame['group_id'],
        'width': frame['width'], 'height': frame['height'],
        'task_hash': ctx['binding']['task_hash'], 'state': 'success',
        'source_frame_status': frame['frame_status'], 'targets': len(chosen),
        'unresolved_targets': sum(t['status']=='unresolved_no_mask' for t in chosen),
        'views': view_records, 'files': files, 'elapsed_seconds': time.monotonic()-started}
    if persist:
        protected_write(record_path(ctx['output'], frame), encoded(record))
    return record


@contextmanager
def pooled(args, binding, hashes, before_records):
    initargs=(str(args.source), str(args.output), args.config_data, binding, hashes,
              str(args.before), before_records)
    if args.workers == 1:
        initializer(*initargs)
        yield SimpleNamespace(map=lambda fn,items,chunksize=1:map(fn,items))
    else:
        # Do not fork an initialized OpenCV/BLAS thread pool (can deadlock).
        from multiprocessing import get_context
        for variable in ('OPENBLAS_NUM_THREADS','OMP_NUM_THREADS','MKL_NUM_THREADS'):
            os.environ[variable]='1'
        with ProcessPoolExecutor(max_workers=args.workers,mp_context=get_context('spawn'),
                                 initializer=initializer,initargs=initargs) as executor:
            yield executor


def select_pilot(frames, targets, config):
    known = {f['frame_id']: f for f in frames}
    strata = defaultdict(list)
    for frame in frames:
        for target in targets[frame['frame_id']]:
            if target['status'] != 'unresolved_no_mask':
                strata[(frame['width'], frame['height'])].append(target)
    rng = np.random.default_rng(config['seed'])
    selected = []
    for size, rows in sorted(strata.items()):
        count = config['pilot_targets_per_resolution']
        if len(rows) < count:
            raise ValueError(f'Pilot stratum {size} has only {len(rows)} positive targets')
        selected.extend({'target_id': rows[i]['target_id'], 'frame_id': rows[i]['frame_id'],
                         'resolution': list(size)} for i in rng.choice(len(rows), count, replace=False))
    return selected


def summarize(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'], 'views': {},
        'proposal_only': True, 'mask_human_reviewed': False,
        'segmentation_accuracy': None, 'elapsed_worker_seconds': sum(r['elapsed_seconds'] for r in records)}
    for view in VIEWS:
        all_groups = [g for r in records for g in r['views'][view]['groups']]
        parent_status = Counter(s for r in records for s in r['views'][view]['parents'].values())
        sizes = sorted({(r['width'], r['height']) for r in records})
        sections = {'all': all_groups}
        sections.update({f'{w}x{h}': [g for r in records if (r['width'],r['height'])==(w,h)
                                    for g in r['views'][view]['groups']] for w, h in sizes})
        info = {'parent_status': dict(parent_status), 'by_resolution': {}}
        for name, groups in sections.items():
            accepted = [g for g in groups if g['status']=='simplified_accepted']
            nonempty = sum(g['status']!='original_unresolved' for g in groups)
            old_p, old_v = sum(g['before_polygon_count'] for g in groups), sum(g['before_vertex_count'] for g in groups)
            new_p, new_v = sum(g['polygon_count'] for g in groups), sum(g['vertex_count'] for g in groups)
            accepted_p = sum(g['before_polygon_count'] for g in accepted)
            accepted_v = sum(g['before_vertex_count'] for g in accepted)
            stats = {'groups': len(groups), 'nonempty_groups': nonempty,
                'status': dict(Counter(g['status'] for g in groups)),
                'automatic_pass_rate': len(accepted)/nonempty if nonempty else None,
                'iou_quantiles': {str(q): float(np.quantile([g['iou'] for g in accepted], q))
                                  for q in [0, .1, .5, .9, 1]} if accepted else {},
                'before_polygons': old_p, 'before_vertices': old_v,
                'after_polygons': new_p, 'after_vertices': new_v,
                'loaded_polygon_reduction': 1-new_p/old_p if old_p else None,
                'loaded_vertex_reduction': 1-new_v/old_v if old_v else None,
                'accepted_only_polygon_reduction': 1-new_p/accepted_p if accepted_p else None,
                'accepted_only_vertex_reduction': 1-new_v/accepted_v if accepted_v else None,
                'added_pixels': sum(g['added_pixels'] for g in accepted),
                'deleted_pixels': sum(g['deleted_pixels'] for g in accepted),
                'max_polygon_count': max((g['polygon_count'] for g in accepted), default=0),
                'max_vertex_count': max((g['vertex_count'] for g in accepted), default=0)}
            if name == 'all':
                info.update(stats)
            else:
                info['by_resolution'][name] = stats
        summary['views'][view] = info
    return summary


def run_pilot(args, manifest, frames, targets, faces, binding, before):
    selection = select_pilot(frames, targets, args.config_data)
    protected_write(args.output/'pilot_selection.json', encoded(selection))
    chosen = {r['target_id'] for r in selection}
    jobs = [(f, targets[f['frame_id']], faces[f['frame_id']], chosen, True, True)
            for f in frames if any(t['target_id'] in chosen for t in targets[f['frame_id']])]
    # Pilot is an independent workspace. Full export never reuses partial-frame labels.
    pilot_args = SimpleNamespace(**vars(args)); pilot_args.output = args.output/'pilot'
    pilot_args.output.mkdir(parents=True, exist_ok=True)
    started = time.monotonic()
    records = []
    with pooled(pilot_args, 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 = summarize(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(args.output/'pilot_report.json', encoded(summary))
    protected_write(args.output/'pilot/records.jsonl', b''.join(encoded(r) for r in records))
    render_reports(args.output/'pilot/delivery', records, summary, '160目标试跑')
    # Re-render every pilot polygon via official AnyLabeling before freezing.
    initializer(str(args.source), str(pilot_args.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_selection_sha256': digest(args.output/'pilot_selection.json'),
        'pilot_report_sha256': digest(args.output/'pilot_report.json'),
        'pilot_verified': True, 'source_segmentation_accuracy_claimed': False}))
    print(json.dumps({'stage':'pilot_complete','summary':summary}, ensure_ascii=False), flush=True)
    return summary


def grouped_pixels(shapes, size, upstream, config):
    groups = {}
    for shape in shapes:
        points = np.asarray(shape['points'], float)
        if (shape['shape_type'] != 'polygon' or shape['label'] != LABEL
                or points.ndim != 2 or points.shape[1] != 2
                or not 3 <= len(points) <= config['max_polygon_vertices']
                or not np.isfinite(points).all() or len(set(map(tuple, points))) != len(points)
                or points.min() < 0 or points[:, 0].max() >= size[1]
                or points[:, 1].max() >= size[0]):
            raise ValueError('Invalid or unbounded polygon')
        if not Polygon(points).is_valid or Polygon(points).area <= 0:
            raise ValueError('Self-intersecting or zero-area polygon')
        group = shape['group_id']
        if not isinstance(group, int) or isinstance(group, bool) or group <= 0:
            raise ValueError('Invalid group identity')
        if shape['flags']['mask_human_reviewed'] or not shape['flags']['proposal_only']:
            raise ValueError('Initial polygons must remain unreviewed proposals')
        if group not in groups:
            groups[group] = np.zeros(size, bool)
        groups[group] |= upstream(size, points.tolist(), 'polygon')
    return groups


def verify_record(record):
    ctx = _CONTEXT
    delivery, config = ctx['delivery'], ctx['config']
    size = (record['height'], record['width'])
    with Image.open(delivery/'reference_instances'/record['split']/(record['frame_key']+'.png')) as image:
        image.load(); source_occupied = np.asarray(image) > 0
    counts = Counter()
    for view, paths in record['views'].items():
        doc = json.loads(safe_path(delivery, paths['annotation']).read_text())
        if (doc['imagePath'] != record['frame_key']+'.png'
                or doc['imageWidth'] != size[1] or doc['imageHeight'] != size[0]):
            raise ValueError('Image/JSON identity or native size mismatch')
        image_path = safe_path(delivery, paths['annotation']).parent/doc['imagePath']
        if image_path.resolve() != safe_path(delivery, paths['image']).resolve():
            raise ValueError('Nonportable imagePath')
        with Image.open(image_path) as image:
            image.load()
            if image.size != (size[1], size[0]):
                raise ValueError('Native image 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 LabelFile did not preserve shapes/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('Review status or negative identity 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['simplification_task_hash'] != record['task_hash']):
            raise ValueError('Frame/source split/task identity changed')
        if meta['groups'] != paths['groups']:
            raise ValueError('Group metadata changed')
        rendered = grouped_pixels(doc['shapes'], size, ctx['upstream'], config)
        groups = meta['groups']
        accepted = {g['group_id'] for g in groups if g['status']=='simplified_accepted'}
        if set(rendered) != accepted:
            raise ValueError('Redraw/unresolved group contains polygons or accepted group is missing')
        redo = [g['group_id'] for g in groups if g['status']!='simplified_accepted']
        empty_parents = any(p['status']=='unresolved_no_mask' for p in meta['parents'])
        if meta['redraw_group_ids'] != redo or flags['redraw_required'] != bool(redo or empty_parents):
            raise ValueError('Redraw identity lost')
        if redo and (not flags['unresolved_positive'] or flags['confirmed_negative']):
            raise ValueError('An empty positive became a negative')
        shape_counts, vertex_counts = Counter(), Counter()
        group_map = {g['group_id']:g for g in groups}
        for shape in doc['shapes']:
            group = group_map[shape['group_id']]
            part = shape['pallet_mask_part']
            if (part['pallet_group_id'] != group['pallet_group_id']
                    or part['face_id'] != group['face_id']):
                raise ValueError('Face/parent attribution changed')
            shape_counts[shape['group_id']] += 1
            vertex_counts[shape['group_id']] += len(shape['points'])
        occupied = np.zeros(size, bool)
        for group in groups:
            data = safe_path(delivery, group['mask']).read_bytes()
            if hashlib.sha256(data).hexdigest() != group['mask_sha256']:
                raise ValueError('Original reference mask changed')
            original = decode_mask(data, size)
            if original.sum() != group['source_pixels']:
                raise ValueError('Original mask size changed')
            if group['status'] == 'original_unresolved':
                if original.any():
                    raise ValueError('Nonempty positive was made originally unresolved')
                continue
            if not original.any():
                raise ValueError('Empty group lost unresolved status')
            if group['status'] == 'redraw_required':
                if group['iou'] is not None or group['polygon_count'] or group['vertex_count']:
                    raise ValueError('Redraw task was silently counted as accepted')
                continue
            if group['status'] != 'simplified_accepted':
                raise ValueError('Unknown group outcome')
            actual = rendered[group['group_id']]
            metrics = pixel_metrics(original, actual)
            if metrics != {k:group[k] for k in metrics} or metrics['iou'] < config['minimum_iou']:
                raise ValueError('Raster IoU/changes do not satisfy acceptance')
            if (shape_counts[group['group_id']] > config['max_group_polygons']
                    or vertex_counts[group['group_id']] > config['max_group_vertices']
                    or shape_counts[group['group_id']] != group['polygon_count']
                    or vertex_counts[group['group_id']] != group['vertex_count']):
                raise ValueError('Group complexity limit violated')
            if (actual & occupied).any() or (actual & source_occupied & ~original).any():
                raise ValueError('New instance overlap')
            occupied |= actual
            # Reconstruct protection from the recorded cleanup choices.
            fg, fs, bg, hs, holes, thin, protected_holes = component_evidence(original, group['bbox'], config)
            filled = [i for i in holes if hs[i,cv2.CC_STAT_AREA] <= group['cleanup']['fill_hole_area_at_most']
                      and i not in protected_holes]
            retained = [i for i in holes if i not in filled]
            if retained and (actual & np.isin(bg, retained)).any():
                raise ValueError('A retained/main hole was filled')
            if thin and (np.isin(fg, list(thin)) & ~actual).any():
                raise ValueError('A protected thin component was lost')
            counts[view+'_accepted'] += 1
        counts[view+'_groups'] += len(groups)
        counts[view+'_parents'] += len(meta['parents'])
    return dict(counts)


def load_label_file(path, expected):
    """Run the unchanged official serialization class with GUI imports stubbed."""
    if digest(path) != expected:
        raise ValueError('Pinned AnyLabeling LabelFile changed')
    tree = ast.parse(Path(path).read_text())
    tree.body = [n for n in tree.body if not isinstance(n, ast.ImportFrom) or n.level == 0]
    scope = {'__version__':'0.4.43','logger':logging.getLogger(__name__),
        'utils':SimpleNamespace(apply_exif_orientation=ImageOps.exif_transpose,
            img_b64_to_arr=lambda s:np.asarray(Image.open(io.BytesIO(base64.b64decode(s)))))}
    exec(compile(tree,str(path),'exec'),scope)
    return scope['LabelFile']


def check_label_save(path, temporary, label_file):
    original = json.loads(Path(path).read_text())
    loaded = label_file(str(path))
    shapes = []
    for shape in loaded.shapes:
        shape = dict(shape); other = shape.pop('other_data'); shape.update(other)
        shapes.append(shape)
    loaded.save(filename=str(temporary),shapes=shapes,image_path=loaded.image_path,
        image_height=original['imageHeight'],image_width=original['imageWidth'],image_data=None,
        other_data=loaded.other_data,flags=loaded.flags)
    restored = json.loads(Path(temporary).read_text())
    if restored != original:
        raise ValueError('Official AnyLabeling save changed polygon metadata')
    return len(shapes)


def render_reports(delivery, records, summary, title):
    lines = [f'# 叉取面多边形简化：{title}', '',
        f"处理{summary['frames']:,}帧、{summary['targets']:,}个父托盘。原mask作为机器几何参考；IoU不代表分割正确率。", '',
        '每个自动通过组：IoU≥95%，每片≤24顶点、每组≤12片和128顶点；无法达到者保留原mask预览并作为待重画正例。', '',
        '| 版本 | 非空组 | 自动通过 | 待重画 | 通过率 | IoU最小/中位 | 加载片段 | 加载顶点 |',
        '|---|---:|---:|---:|---:|---:|---:|---:|']
    for view in VIEWS:
        s = summary['views'][view]; q=s['iou_quantiles']
        lines.append(f"| {view} | {s['nonempty_groups']:,} | {s['status'].get('simplified_accepted',0):,} | "
            f"{s['status'].get('redraw_required',0):,} | {(s['automatic_pass_rate'] or 0):.2%} | "
            f"{q.get('0',0):.4f}/{q.get('0.5',0):.4f} | {s['after_polygons']:,} | {s['after_vertices']:,} |")
    lines.extend(['', '各分辨率结果：', '', '| 版本/分辨率 | 非空组 | 通过率 | 待重画 | 原片段→加载片段 | 原顶点→加载顶点 |',
                  '|---|---:|---:|---:|---:|---:|'])
    for view in VIEWS:
        for resolution, s in summary['views'][view]['by_resolution'].items():
            lines.append(f"| {view}/{resolution} | {s['nonempty_groups']:,} | {(s['automatic_pass_rate'] or 0):.2%} | "
                f"{s['status'].get('redraw_required',0):,} | {s['before_polygons']:,}→{s['after_polygons']:,} | "
                f"{s['before_vertices']:,}→{s['after_vertices']:,} |")
    lines.extend(['', '片段/顶点减少包含待重画组不再加载原多边形的部分；自动通过组自身的减少比例另见summary.json，不能把隐藏待重画任务当作已完成标注。', '',
        '原263张确认负例保持身份。原41个无mask正例和新增待重画目标均不作为分割负例。两版独立，推荐逐托盘版人工修正。', '',
        '自动规则仅改善可编辑性。几何mask本身可能包含后壁、地面或其他错误，仍须人工确认叉取面。', '',
        '[Shapely 2.1.2保拓扑Douglas–Peucker](https://shapely.readthedocs.io/en/2.1.2/reference/shapely.simplify.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']]
            if candidates:
                for record in candidates[:2]:
                    preview = record['views'][view]['preview']
                    examples.append(f'<figure><img loading="lazy" src="{html.escape(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}' \
        'table{border-collapse:collapse}td,th{border:1px solid #bbb;padding:7px}img{max-width:100%}' \
        'figure{padding:12px;background:white}pre{white-space:pre-wrap}</style>'+body+''.join(examples)
    protected_write(delivery/'report_zh.html', page.encode())


def instructions(summary):
    return f'''# 可人工编辑的叉取面分割修正包

1. 解压后在AnyLabeling打开 `pallets/images/train`（推荐），或val/test目录。逐面版是 `faces/images/<split>`，两版独立保存。
2. 同一group_id的多片多边形共同表示一个托盘/面片。每片最多24顶点；每组最多12片、128顶点。自动通过组与原mask实际填充IoU至少95%。
3. **待重画正例可能没有任何可见多边形，不代表负例。** 查看JSON的 `flags/redraw_required`、`pallet_segmentation/groups`、`parents`及 `redraw_tasks.jsonl`；原mask在 `reference_masks`，同名叠加比较图在 `redraw_previews/<view>/<split>`。红框为待重画身份，左侧为原mask证据，右侧为可编辑多边形。
4. 给待重画目标添加类别 `{LABEL}` 的polygon，使用JSON记录的group_id。同组可画多片，用片段避开叉孔与缺测；不要把父框当作填充分割。框只在元数据和预览中保留。原无mask目标也有明确的父框和身份，须独立判断。
5. 全部初始 `human_reviewed=false`、`mask_human_reviewed=false`、`unreviewed=true`、`proposal_only=true`。完成每张后保存并记录完成状态；原263张确认负例才是明确的空目标。新增空正例不能导入为负例。
6. 图像坐标已含原180°校正；所有PNG保持原生尺寸。原划分/采集组/父托盘/面编号在JSON内；不要对图像再次旋转或缩放。
7. 只编辑工作副本。恢复/verify/package会拒绝已变更的JSON，保护人工修改；保留冻结ZIP，再另行交回修正版本。旧工作区未被改写。

本包共{summary['frames']:,}帧、{summary['targets']:,}个父托盘。IoU仅衡量与原机器mask的接近程度，没有人工分割准确率。两版的待重画负担及真实通过率见 `report_zh.html` / `summary.json`。

校验：在包根目录运行 `sha256sum -c checksums.sha256`。`validation.json`记录官方填充、LabelFile读取/保存及覆盖率验收；桌面GUI没有运行。
'''


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 and configuration freeze required')
    started = time.monotonic()
    records = []
    jobs = [(f, targets[f['frame_id']], faces[f['frame_id']], None, i%256==0, True)
            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']}
                save_status(args.output/'workflow_status.json', status)
                print(json.dumps(status), flush=True)
    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))
    target_records, groups, redraw = [], [], []
    for record in records:
        parents = sorted(record['views']['pallets']['parents'])
        for target_id in parents:
            target_records.append({'frame_id':record['frame_id'],'split':record['split'],
                'group_id':record['group_id'],'target_id':target_id,
                'views':{v:record['views'][v]['parents'][target_id] for v in VIEWS},
                'proposal_only':True,'mask_human_reviewed':False})
        for view in VIEWS:
            for group in record['views'][view]['groups']:
                row={'frame_id':record['frame_id'],'frame_key':record['frame_key'],'split':record['split'],
                     'capture_group_id':record['group_id'],'view':view,
                     'annotation':record['views'][view]['annotation'],
                     'preview':record['views'][view]['preview'],**group}
                groups.append(row)
                if group['status']!='simplified_accepted':
                    redraw.append({**row,'positive_redraw_task':True,'segmentation_negative':False})
    for name, rows in [('targets',target_records),('groups',groups),('redraw_tasks',redraw)]:
        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 view in VIEWS:
        protected_write(delivery/view/'labels.txt',(LABEL+'\n').encode())
    for module in [Path(__file__),Path(__file__).with_name('fork_face_anylabeling.py')]:
        protected_write(delivery/'source'/module.name,module.read_bytes())
    for name in ('run_manifest.json','configuration_freeze.json','pilot_report.json','pilot_selection.json'):
        protected_write(delivery/'provenance'/name,(args.output/name).read_bytes())
    protected_write(delivery/'source'/'requirements.txt',
        ('shapely==2.1.2\nnumpy=='+np.__version__+'\nopencv-python-headless=='+cv2.__version__+
         '.86\npillow=='+PIL.__version__+'\nmarkdown-it-py=='+importlib.metadata.version('markdown-it-py')+
         '\nthreadpoolctl=='+importlib.metadata.version('threadpoolctl')+'\n').encode())
    tests=Path(__file__).resolve().parent.parent/'tests/test_fork_face_simplify.py'
    if tests.exists():
        protected_write(delivery/'source'/tests.name,tests.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())
    summary = summarize(records,binding)
    protected_write(delivery/'summary.json',encoded(summary))
    protected_write(delivery/'manifest.json',encoded({'binding':binding,'summary':summary}))
    protected_write(delivery/'README_zh.md',instructions(summary).encode())
    render_reports(delivery,records,summary,'全量导出')
    save_status(args.output/'workflow_status.json',{'stage':'export_complete',**summary})
    return records,summary


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('All source frames require completed records')
    parents = [target for r in records for target in r['views']['pallets']['parents']]
    if len(parents) != manifest['summary']['targets'] or len(set(parents)) != len(parents):
        raise ValueError('Incomplete/duplicate parent target coverage')
    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)
    # Recheck every actually used frozen image/mask/instance/diagnostic asset.
    used = {'frames.jsonl','targets.jsonl','faces.jsonl','summary.json'}
    for record in records:
        used.add(f"images/{record['split']}/{record['frame_key']}.png")
        used.update(p[len('reference_'):] for p in record['files'] if p.startswith('reference_'))
    for relative in sorted(used):
        if digest(safe_path(args.source,relative)) != manifest['files'][relative]:
            raise ValueError(f'Original geometry asset changed: {relative}')
    protected_count = 0
    original_manifest = args.source.parent/'run_manifest.json'
    if original_manifest.exists():
        original = json.loads(original_manifest.read_text())
        if original.get('protected_files'):
            dataset = Path(original['binding']['dataset'])
            for relative, expected in original['protected_files'].items():
                if digest(safe_path(dataset,relative)) != expected:
                    raise ValueError(f'Formal detection annotation changed: {relative}')
                protected_count += 1
    # Test unmodified official load/save on all pilot documents in a temp directory.
    import tempfile
    label_file = load_label_file(args.config_data['upstream_label_source'],args.config_data['upstream_label_sha256'])
    pilot_records = [json.loads(l) for l in (args.output/'pilot/records.jsonl').read_bytes().splitlines()]
    saved = 0
    with tempfile.TemporaryDirectory(prefix='fork-face-save-') as directory:
        for record in pilot_records:
            for view in VIEWS:
                check_label_save(args.output/'pilot/delivery'/record['views'][view]['annotation'],
                                 Path(directory)/'save.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(parents),
        'view_counts':dict(counts),'used_source_files_unchanged':len(used),
        'protected_formal_files_unchanged':protected_count,
        'official_anylabeling_load_documents':len(records)*2,
        'official_anylabeling_save_documents':saved,'gui_application_tested':False,
        'actual_raster_iou_and_complexity_passed':True,'no_new_instance_overlap':True,
        'retained_holes_and_protected_thin_structures_passed':True,
        'empty_positives_never_promoted_to_negatives':True,
        '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 recheck_verified(output, binding):
    """Close the verification-to-package gap, including human edits after verify."""
    delivery = Path(output)/'delivery'
    validation = json.loads((Path(output)/'validation.json').read_text())
    if validation['state']!='passed' or validation['task_hash']!=binding['task_hash']:
        raise ValueError('Full verification required before package')
    if (delivery/'validation.json').read_bytes() != encoded(validation):
        raise ValueError('Validation record changed')
    if digest(delivery/'verified_assets.json') != validation['verified_assets_sha256']:
        raise ValueError('Verified asset snapshot changed')
    snapshot = json.loads((delivery/'verified_assets.json').read_text())
    if snapshot['task_hash'] != binding['task_hash']:
        raise ValueError('Verified task binding changed')
    expected = snapshot['files']
    present = {str(p.relative_to(delivery)) for p in delivery.rglob('*') if p.is_file()
               and str(p.relative_to(delivery)) not in
               ('verified_assets.json','validation.json','checksums.sha256')}
    if present != set(expected):
        raise ValueError('Verified workspace file coverage changed; human edits are protected')
    for relative, sha in expected.items():
        if digest(safe_path(delivery,relative)) != sha:
            raise ValueError(f'Verified file changed; human edits are protected: {relative}')
    return validation, {**expected,'verified_assets.json':digest(delivery/'verified_assets.json'),
                        'validation.json':digest(delivery/'validation.json')}


def run_package(args, binding):
    output, delivery = args.output, args.output/'delivery'
    validation, assets = 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_simplified_v1.zip'
    temporary = archive.with_suffix('.partial')
    with zipfile.ZipFile(temporary,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=4) as z:
        for i,(relative,expected) in enumerate(sorted(assets.items())):
            if i%512==0:
                check_space(output)
            path = safe_path(delivery,relative)
            if digest(path)!=expected:
                raise ValueError('Workspace changed while packaging')
            z.write(path,relative)
    with zipfile.ZipFile(temporary) as z:
        if len(z.namelist())!=len(assets) or z.testzip() is not None:
            raise ValueError('ZIP CRC/member count failed')
        for relative,expected in assets.items():
            if hashlib.sha256(z.read(relative)).hexdigest()!=expected:
                raise ValueError('ZIP member SHA-256 failed')
        # Extract representative split/resolution/status documents into a new root.
        import tempfile
        label_file = load_label_file(args.config_data['upstream_label_source'],args.config_data['upstream_label_sha256'])
        seen, portable = set(), 0
        with tempfile.TemporaryDirectory(prefix='fork-face-portable-') as directory:
            for line in (delivery/'frames.jsonl').open():
                r=json.loads(line)
                for view in VIEWS:
                    status_key=tuple(sorted({g['status'] for g in r['views'][view]['groups']}))
                    key=(r['split'],r['width'],r['height'],view,status_key)
                    if key in seen:
                        continue
                    seen.add(key)
                    for relative in (r['views'][view]['annotation'],r['views'][view]['image']):
                        path=safe_path(Path(directory),relative);path.parent.mkdir(parents=True,exist_ok=True)
                        path.write_bytes(z.read(relative))
                    path=Path(directory)/r['views'][view]['annotation']
                    check_label_save(path,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':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):
    import sys
    arguments=list(sys.argv[1:] if argv is None else argv)
    if '--cover-mask' in arguments:
        from pallet.fork_face_enclosure import main as enclosure_main
        return enclosure_main([a for a in arguments if a!='--cover-mask'])
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--cover-mask',action='store_true',help='Use hole-filled concave enclosures without an IoU threshold')
    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_simplified_v1'))
    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_simplify_v1.json'))
    parser.add_argument('--workers',type=int,default=8)
    args=parser.parse_args(arguments)
    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 separate from source masks and old human workspace')
    args.config_data=json.loads(args.config.read_text())
    if set(args.config_data)!=set(DEFAULT_CONFIG):
        raise ValueError('Configuration contains missing/unknown fields')
    config=args.config_data
    if (shapely.__version__!=config['shapely_version'] or config['shapely_version']!='2.1.2'
            or not .95<=config['minimum_iou']<=1
            or not 3<=config['max_polygon_vertices']<=24 or not 1<=config['max_group_polygons']<=12
            or not 3<=config['max_group_vertices']<=128):
        raise ValueError('Pinned dependency or approved quality/complexity limits violated')
    load_upstream(config['upstream_shape_source'],config['upstream_shape_sha256'])
    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}
    binding={'algorithm':'bounded_topology_raster_candidates_v1','config':config,
        'source_export_manifest_sha256':digest(args.source/'export_manifest.json'),
        'source_task_hash':manifest['task_hash'], 'source_code_sha256':digest(__file__),
        'helper_code_sha256':digest(Path(__file__).with_name('fork_face_anylabeling.py')),
        'before_index_sha256':digest(before_path) if before_path.exists() else None,
        'dependencies':{'shapely':shapely.__version__,'geos':shapely.geos_version_string,
                        'numpy':np.__version__,'opencv':cv2.__version__,'pillow':PIL.__version__,
                        'threadpoolctl':importlib.metadata.version('threadpoolctl'),
                        'markdown-it-py':importlib.metadata.version('markdown-it-py')},
        'views':list(VIEWS),'label':LABEL}
    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()
