"""Frozen gallery-human templates, exhaustive guarded transfer, diverse frame quota.

Only writes a fresh output directory. Publication requires independent verification.
"""
from __future__ import annotations

import argparse
from collections import Counter
from concurrent.futures import ThreadPoolExecutor
from contextlib import closing
import html
import json
from pathlib import Path
import shutil
import sqlite3

import cv2
from PIL import Image

from .cluster_targets import digest, load_dataset, protected_snapshot, target_key
from .mask_gallery_review import VERSION as EDITOR_VERSION
from .mask_template_continue import attempt_target, sha
from .mask_template_transfer import (PROJECT, ImageEvidence,
    annotation_inventory, compatible, package_preview, write_json)
from .pose_review_geometry import canonical, polygon_value

VERSION = 'gallery_human_refined_fixed_vertex_v1'
CLASSES = (1, 2, 9, 10, 15)
PARENT = PROJECT/'outputs/pallet_mask_template_bulk_v5'
QUARANTINE = {'bf59aa3d7d604db6fe49debd'}


def read_jsonl(path):
    return [json.loads(line) for line in Path(path).read_text().splitlines() if line]


def write_jsonl(path, values):
    Path(path).write_bytes(b''.join(canonical(value)+b'\n' for value in values))


def readonly(path, immutable=False):
    return sqlite3.connect(Path(path).resolve().as_uri()+'?mode=ro'+('&immutable=1' if immutable else ''), uri=True)


def snapshot_database(live, destination):
    with closing(readonly(live)) as source, closing(sqlite3.connect(destination)) as target:
        source.backup(target)
        if target.execute('PRAGMA integrity_check').fetchone()[0] != 'ok':
            raise ValueError('Review snapshot integrity failure')
        # A standalone snapshot can be read immutable without WAL sidecars.
        target.execute('PRAGMA journal_mode=DELETE')
    destination.chmod(0o400)
    backup = destination.parent/'backups'/destination.name
    backup.parent.mkdir(exist_ok=True)
    shutil.copyfile(destination, backup)
    backup.chmod(0o400)
    return digest(destination)


def protected_pose_keys(db, rows):
    """Use frozen mask-history eligibility; pose-only saves remain sidecar eligible.

    A non-original mask at any revision stays protected even after restoring the
    original rectangle. Deleted, deferred and confirmed targets retain the
    existing inventory protection. All pose events are preserved independently
    through verify_history_preserved, including eligible pose/ruler-only saves.
    """
    current, _, status = annotation_inventory(db, rows)
    protected = {key for key, reason in status.items() if reason != 'eligible'}
    return current, status, protected


def gallery_templates(previous, reviews, events, current, rows, parent, formal_sha, snapshot_sha):
    """Validate the saved source identity and distinguish pose/gallery revisions."""
    if len(previous) != 56 or len({p['target_key'] for p in previous}) != 56:
        raise ValueError('Expected exactly 56 distinct parent proposals')
    templates = {}
    binding = sha(dict(formal=formal_sha, proposals=digest(parent/'proposals.jsonl'), editor_version=EDITOR_VERSION))
    for p in previous:
        key = p['target_key']; row = rows[key]; review = reviews.get(key)
        if review is None or len(review.get('polygon', [])) <= 4:
            raise ValueError('Every parent target needs a saved fine gallery polygon')
        identity = {k: row[k] for k in ('target_key', 'frame_id', 'split', 'box_index', 'bbox', 'size', 'source_pcd_sha256')}
        identity.update(formal_manifest_sha256=formal_sha, image_sha256=digest(parent/'images'/f'{key}.png'))
        if (p['source'] != row or p['class_id'] != row['final_class_id'] or p['formal_manifest_sha256'] != formal_sha or
                review['source'] != identity or review['source_binding'] != sha(identity) or
                review['target_key'] != key or review['base_proposal_id'] != sha(p) or review['binding'] != binding):
            raise ValueError(f'Gallery source/proposal binding mismatch: {key}')
        history = [v for v in events if v['target_key'] == key]
        if not history or max(history, key=lambda v: v['revision']) != review:
            raise ValueError('Gallery current record is not its latest event')
        if review['event_kind'] != 'human_mask_gallery_revision' or not review['human_edited']:
            raise ValueError('Template is not a saved human gallery edit')
        if any(review.get(flag) for flag in ('human_mask_confirmed', 'human_pose_confirmed', 'automatic_training_eligible', 'robot_command_eligible')):
            raise ValueError('Unexpected gallery GT/training/robot claim')
        polygon_value(review['polygon'], *row['size'])
        if review['vertex_count'] != len(review['polygon']):
            raise ValueError('Gallery polygon vertex count mismatch')
        old = current.get(key)
        provenance = dict(target_key=key, revision=review['revision'], payload_sha256=sha(review),
            source_binding=review['source_binding'], polygon_sha256=sha(review['polygon']), snapshot_sha256=snapshot_sha)
        templates[key] = dict(row=row, polygon=review['polygon'], revision=old['revision'] if old else 0,
            current_payload_sha256=sha(old) if old else None, human_polygon_revision=review['revision'],
            human_event_sha256=sha(review), human_mask_confirmed=False,
            origin='mask_gallery_human_revision', gallery_review=provenance)
    return templates


def select_diverse(previous, candidates, goal=50):
    """One new target per frame; prefer underrepresented collection groups."""
    selected = []; dispositions = {}
    # Global distinctness also prevents repeating a frame across classes.
    frames = {p['source']['frame_id'] for p in previous}
    for cls in CLASSES:
        old = [p for p in previous if p['class_id'] == cls]
        shortfall = max(0, goal-len({p['source']['frame_id'] for p in old}))
        groups = Counter(p['source']['group_id'] for p in old)
        pending = [p for p in candidates if p['class_id'] == cls]
        while pending:
            pending.sort(key=lambda p: (groups[p['source']['group_id']],
                -p['metrics'].get('boundary_ncc', 0), -p['metrics'].get('ecc', 0), p['target_key']))
            p = pending.pop(0); frame = p['source']['frame_id']; key = p['target_key']
            if frame in frames:
                dispositions[key] = 'accepted_duplicate_frame_not_selected'
            elif shortfall == 0:
                dispositions[key] = 'accepted_quota_full_not_selected'
            else:
                selected.append(p); frames.add(frame); groups[p['source']['group_id']] += 1
                shortfall -= 1; dispositions[key] = 'new_proposal'
    return sorted(selected, key=lambda p: p['target_key']), dispositions


def preserve_parent_assets(parent, out, previous):
    hashes = {}
    for p in previous:
        key = p['target_key']
        for folder, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            relative = f'{folder}/{key}.{extension}'
            destination = out/relative
            if destination.exists():
                raise ValueError('Historical target asset collision')
            shutil.copyfile(parent/relative, destination)
            hashes[relative] = digest(parent/relative)
            if digest(destination) != hashes[relative]:
                raise ValueError('Historical asset copy mismatch')
    # New rendering templates have their own namespace, including overlapping keys.
    (out/'refined').mkdir(exist_ok=True)
    (out/'templates').rename(out/'refined/templates')
    shutil.copytree(parent/'templates', out/'templates')
    for file in (parent/'templates').glob('*.json'):
        hashes[f'templates/{file.name}'] = digest(file)
    if (parent/'vertex2_before_after.png').exists():
        shutil.copyfile(parent/'vertex2_before_after.png', out/'vertex2_before_after.png')
        hashes['vertex2_before_after.png'] = digest(parent/'vertex2_before_after.png')
    return hashes


def build_report(out, combined, new_keys, counts, reviews):
    table = ''.join(f'<tr><td>{cls}</td><td>{c["saved_fine_edits"]}</td><td>{c["new_candidates"]}</td>'
        f'<td>{c["total"]}</td><td>{c["missing"]}</td></tr>' for cls, c in counts.items())
    cards = []
    for p in sorted(combined, key=lambda p: (p['class_id'], p['target_key'])):
        key = p['target_key']; new = key in new_keys
        label = '【本轮新增】机器候选 · 待人工复核' if new else f'【已保存精修 r{reviews[key]["revision"]}】原始候选对照'
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" data-batch="{"new" if new else "previous"}"'
            f'{"" if new else " hidden"}><h2>Class {p["class_id"]} · {label}</h2>'
            f'<img loading="lazy" src="cases/{key}.png?revision=4" alt="模板与候选对照">'
            f'<p>目标：{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>'
            f'<p>{"精修模板顶点数：" if new else "历史候选顶点数："}{p["vertex_count"]} · '
            f'<a href="labelme/{key}.json?revision=4">{"候选" if new else "历史候选"} LabelMe</a> · '
            f'<a href="masks/{key}.png?revision=4">掩膜</a></p></article>')
    page = '''<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>人工精修模板 · 新一轮掩膜候选</title><style>@font-face{font-family:PreviewSans;src:url('report-font.otf?revision=4')}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1500px}a{color:#77ceff}img{width:100%;height:auto}article{border:1px solid #425269;margin:20px 0;padding:12px;border-radius:8px}p{overflow-wrap:anywhere}td,th{padding:7px;border:1px solid #425269}table{border-collapse:collapse}select{font:inherit;padding:6px}[hidden]{display:none!important}</style>
<h1>人工精修模板 · 本轮新增候选</h1><p>仅用本轮快照中的56份已保存人工精修多边形作为直接模板；新候选不继续传播。逐顶点配准与横梁边界检查沿用原门槛。只匹配同类、同划分、同传感器配置及兼容视角。人工掩膜修改历史、删除、暂缓、确认及已保存图册目标均受保护。仅保存位姿或标尺且掩膜始终为原矩形的目标可生成独立候选；原位姿及全部历史保持不变。</p>
<p>每类目标为累计50个不同帧。已穷尽可用兼容目标；缺额如实保留。新结果均为待复核草稿，未确认为GT，不进入训练或机器人控制。</p>
<p>原56份候选及图像、掩膜、对照图原样保留；原卡片为历史对照，最新人工精修在在线编辑器中加载，导出已保存修订请使用编辑器。新卡片左侧为已保存精修模板，右侧为机器候选。</p>
<table><tr><th>Class</th><th>已保存精修</th><th>本轮新增</th><th>累计不同帧</th><th>缺额</th></tr>'''+table+'''</table>
<p>筛选类别 <select id="filter"><option value="">全部</option>'''+''.join(f'<option>{c}</option>' for c in CLASSES)+'''</select>
<label><input type="checkbox" id="only-new" checked>只看本轮新增</label></p>'''+''.join(cards)+'''
<script>const cls=document.querySelector('#filter'),only=document.querySelector('#only-new');function filter(){document.querySelectorAll('article').forEach(a=>a.hidden=(!!cls.value&&a.dataset.class!==cls.value)||(only.checked&&a.dataset.batch!=='new'));}cls.onchange=only.onchange=filter;filter();</script></html>'''
    (out/'report_zh.html').write_text(page)
    from fontTools import subset
    from fontTools.ttLib import TTFont
    source = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
    font = TTFont(source); sub = subset.Subsetter(); sub.populate(text=page); sub.subset(font); font.save(out/'report-font.otf')
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    if not required <= set(TTFont(out/'report-font.otf').getBestCmap()):
        raise ValueError('Report font coverage failure')
    write_json(out/'font_coverage.json', dict(chinese_glyphs=len(required), all_report_Chinese_present=True,
        font_source_sha256=digest(source), font_sha256=digest(out/'report-font.otf')))


def verify_history_preserved(live, frozen):
    with closing(readonly(live)) as db, closing(readonly(frozen, True)) as snapshot:
        live_events = {row[0]: row[1:] for row in db.execute('SELECT id,target_key,revision,payload FROM events')}
        for row in snapshot.execute('SELECT id,target_key,revision,payload FROM events'):
            if live_events.get(row[0]) != row[1:]:
                raise ValueError('Live human history changed during generation')


def generate(output, workers=4):
    out = Path(output).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory')
    parent_manifest = json.loads((PARENT/'manifest.json').read_text())
    parent_verification = json.loads((PARENT/'verification.json').read_text())
    if parent_verification['state'] != 'passed' or parent_verification['proposals_sha256'] != digest(PARENT/'proposals.jsonl'):
        raise ValueError('Parent proposals lack matching independent verification')
    previous = read_jsonl(PARENT/'proposals.jsonl'); previous_keys = {p['target_key'] for p in previous}
    if len({p['source']['frame_id'] for p in previous}) != 56 or set(p['class_id'] for p in previous) != set(CLASSES):
        raise ValueError('Parent frame/class inventory changed')
    formal = Path(json.loads((PROJECT/'configs/active_formal_dataset.json').read_text())['dataset'])
    classification = PROJECT/'outputs/pallet_appearance_center_reclassification_v1/targets.jsonl'
    classification_sha = digest(classification); formal_sha = digest(formal/'manifest.json')
    if classification_sha != parent_manifest['classification_sha256']:
        raise ValueError('Classification changed since parent publication')
    annotations, sources = load_dataset(formal)
    rows = {r['target_key']: r for r in read_jsonl(classification)}
    identities = {target_key(a['frame_id'], i): (a, b) for a in annotations for i, b in enumerate(a['boxes'])}
    if set(identities) != set(rows):
        raise ValueError('Formal/classification target coverage mismatch')
    for key, row in rows.items():
        a, box = identities[key]; s = sources[a['frame_id']]
        if (row['bbox'] != box or any(row[k] != a[k] for k in ('frame_id', 'split', 'group_id')) or
                row['source_pcd_sha256'] != s['source_anchor_hash'] or row['policy_id'] != s['sensor_policy']['policy_id']):
            raise ValueError('Formal/classification source identity mismatch')
        row['size'] = s['size']
    out.mkdir(parents=True)
    protected = protected_snapshot(formal); write_json(out/'protected_formal_before.json', protected)
    pose_live = PROJECT/'outputs/pallet_pose_human_review_v1/annotations.sqlite3'
    gallery_live = PROJECT/'outputs/pallet_mask_gallery_reviews_v1/mask_reviews.sqlite3'
    pose_snapshot = out/'reviews_snapshot.sqlite3'; gallery_snapshot = out/'gallery_reviews_snapshot.sqlite3'
    pose_sha = snapshot_database(pose_live, pose_snapshot); gallery_sha = snapshot_database(gallery_live, gallery_snapshot)
    with closing(readonly(pose_snapshot, True)) as db:
        current, status, pose_protected = protected_pose_keys(db, rows)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        binding = json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])
        if binding['formal_manifest_sha256'] != formal_sha:
            raise ValueError('Pose snapshot formal binding mismatch')
    with closing(readonly(gallery_snapshot, True)) as db:
        if db.execute("SELECT value FROM metadata WHERE key='formal_manifest_sha256'").fetchone()[0] != formal_sha:
            raise ValueError('Gallery snapshot formal binding mismatch')
        reviews = {k: json.loads(raw) for k, raw in db.execute('SELECT target_key,payload FROM reviews')}
        gallery_events = [json.loads(raw) for raw, in db.execute('SELECT payload FROM events ORDER BY id')]
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews UNION ALL SELECT target_key,revision,payload FROM events'):
            value = json.loads(raw)
            if key != value['target_key'] or revision != value['revision'] or key not in rows:
                raise ValueError('Gallery relational identity mismatch')
    templates = gallery_templates(previous, reviews, gallery_events, current, rows, PARENT, formal_sha, gallery_sha)
    gallery_protected = set(reviews) | {v['target_key'] for v in gallery_events}
    quarantine = QUARANTINE | set(parent_manifest.get('preserved_quarantined_targets', [])) | set(parent_manifest.get('quarantined_targets', []))
    outcomes = []; work = []
    for key, row in sorted(rows.items()):
        reason = ('previously_quarantined' if key in quarantine else 'already_published' if key in previous_keys else
            'gallery_history_protected' if key in gallery_protected else status[key])
        if reason == 'eligible' and row['final_class_id'] not in CLASSES:
            reason = 'class_not_requested'
        if reason == 'eligible':
            work.append((key, row))
        else:
            outcomes.append(dict(target_key=key, class_id=row['final_class_id'], reason=reason))
    write_json(out/'protected_targets.json', dict(pose_mask_history_or_noneligible=sorted(pose_protected),
        policy='Unchanged annotation_inventory eligibility: any non-original mask history (including restored rectangles), '
            'deleted, deferred or confirmed targets protected. Original-rectangle-only pose/ruler saves remain eligible '
            'for sidecar proposals. All pose events preserved by immutability checks; no pose database writes.',
        gallery_saved_or_history=sorted(gallery_protected), quarantine=sorted(quarantine), eligibility=status))
    write_jsonl(out/'refined_annotations.jsonl', [reviews[k] for k in sorted(templates)])
    evidence = ImageEvidence(formal, rows, sources)
    # Verify gallery image bytes represent the actual native source pixels.
    for p in previous:
        with Image.open(PARENT/'images'/f'{p["target_key"]}.png') as image:
            import numpy as np
            if not np.array_equal(np.asarray(image), evidence.image(p['source']['frame_id'])):
                raise ValueError('Parent gallery image differs from formal source pixels')
    attempts = []; accepted_proposals = []
    print(json.dumps(dict(state='scanning', templates=len(templates), eligible_targets=len(work),
        eligible_by_class=dict(Counter(row['final_class_id'] for _, row in work)), pose_mask_history_or_noneligible=len(pose_protected))), flush=True)
    def worker(item):
        return item, attempt_target(*item, templates, evidence)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (accepted, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            attempts.extend(trials); outcomes.append(outcome)
            if accepted:
                t = templates[accepted['template_key']]; old = current.get(key)
                if len(accepted['polygon']) != len(t['polygon']) or not compatible(t['row'], row):
                    raise ValueError('Transfer violated frozen compatibility/vertex count')
                template = dict(target_key=t['row']['target_key'], class_id=row['final_class_id'], split=t['row']['split'],
                    snapshot_revision=t['revision'], current_payload_sha256=t['current_payload_sha256'],
                    human_polygon_revision=t['human_polygon_revision'], human_event_sha256=t['human_event_sha256'],
                    vertex_count=len(t['polygon']), template_human_confirmed=False,
                    origin=t['origin'], gallery_review=t['gallery_review'])
                accepted_proposals.append(dict(schema=VERSION, target_key=key, class_id=row['final_class_id'], source=row,
                    polygon=accepted['polygon'], vertex_count=len(accepted['polygon']), status='machine_draft_pending_visual_review',
                    human_mask_confirmed=False, human_pose_confirmed=False, automatic_training_eligible=False,
                    robot_command_eligible=False, production_saved=False, ground_truth_eligible=False,
                    mask_semantics='inherited_human_fork_face_contour_with_aperture_notches', template=template,
                    target_expected_revision=old['revision'] if old else 0, target_snapshot_payload_sha256=sha(old) if old else None,
                    snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha, class_assignments_sha256=classification_sha,
                    metrics=accepted['metrics'], flags=['machine_mask_requires_review', 'template_is_unconfirmed_human_gallery_edit']+
                        (['class_assignment_requires_review'] if row.get('classification_review_required') else []),
                    continuation=dict(batch=4, template_rank=accepted['template_rank'],
                        all_compatible_targets_scanned=True, recursive_candidate_propagation=False),
                    geometry_pose_policy='explicit_adoption_only; rebuild_geometry; preserve_old_review_history'))
            if number % 200 == 0:
                print(json.dumps(dict(scanned=number, accepted=len(accepted_proposals), attempts=len(attempts))), flush=True)
    new, dispositions = select_diverse(previous, accepted_proposals)
    for outcome in outcomes:
        if outcome['target_key'] in dispositions:
            outcome['reason'] = dispositions[outcome['target_key']]
    combined = sorted(previous+new, key=lambda p: p['target_key']); new_keys = {p['target_key'] for p in new}
    if len({p['target_key'] for p in combined}) != len(combined) or new_keys & (quarantine | gallery_protected | pose_protected):
        raise ValueError('Protected or duplicated proposal target')
    for name, values in (('proposals', combined), ('new_proposals', new), ('accepted_candidates', accepted_proposals),
                         ('registration_attempts', attempts), ('outcomes', outcomes)):
        write_jsonl(out/f'{name}.jsonl', values)
    old_canonical = {p['target_key']: canonical(p) for p in previous}
    if any(canonical(p) != old_canonical[p['target_key']] for p in read_jsonl(out/'proposals.jsonl') if p['target_key'] in old_canonical):
        raise ValueError('Parent proposal object changed')
    counts = {}
    for cls in CLASSES:
        old_count = sum(p['class_id'] == cls for p in previous); new_count = sum(p['class_id'] == cls for p in new)
        class_outcomes = [v for v in outcomes if v['class_id'] == cls]
        counts[cls] = dict(saved_fine_edits=old_count, new_candidates=new_count, total=old_count+new_count,
            missing=max(0, 50-old_count-new_count), distinct_frames=len({p['source']['frame_id'] for p in combined if p['class_id'] == cls}),
            goal=50, templates=sum(t['row']['final_class_id'] == cls for t in templates.values()),
            eligible=sum(row['final_class_id'] == cls for _, row in work),
            compatible_targets=sum(v['reason'] in ('new_proposal', 'image_similarity_below_gate', 'registration_or_boundary_rejected',
                'accepted_duplicate_frame_not_selected', 'accepted_quota_full_not_selected') for v in class_outcomes),
            preview=old_count+new_count, outcomes=dict(Counter(v['reason'] for v in class_outcomes)))
    historical = json.loads((PARENT/'template_inventory.json').read_text())['templates']
    inventory = dict(templates=[dict(t, rendering_id='historical:'+t['row']['target_key']) for t in historical]+
        [dict(t, rendering_id='refined:'+key) for key, t in sorted(templates.items())],
        generation_template_keys=sorted(templates), historical_templates=len(historical), gallery_human_templates=len(templates),
        rendering_by_target={p['target_key']: ('refined:' if p['target_key'] in new_keys else 'historical:')+p['template']['target_key'] for p in combined},
        conflicting_template_keys=sorted({t['row']['target_key'] for t in historical} & templates.keys()),
        classes=counts, eligibility=status, snapshot_saved=len(current), snapshot_events=pose_events)
    write_json(out/'template_inventory.json', inventory)
    package_preview(out, new, templates, rows, evidence, counts)
    copied = preserve_parent_assets(PARENT, out, previous)
    write_json(out/'preserved_parent_assets.json', copied)
    (out/'refined/labelme').mkdir()
    for key, t in templates.items():
        write_json(out/'refined/templates'/f'{key}.json', t)
        write_json(out/'refined/labelme'/f'{key}.json', dict(version='5.0.0', imagePath=f'../../images/{key}.png', imageData=None,
            imageWidth=rows[key]['size'][0], imageHeight=rows[key]['size'][1], annotation_scope='single_target_mask_review_not_full_frame',
            other_objects_unchanged=True, flags=dict(human_edited=True, human_confirmed=False, automatic_training_eligible=False, robot_command_eligible=False),
            shapes=[dict(label='pallet', shape_type='polygon', points=t['polygon'], group_id=rows[key]['box_index'],
                flags=dict(human_edited=True, human_confirmed=False))], mask_gallery_review=reviews[key]))
    build_report(out, combined, new_keys, counts, reviews)
    used = json.loads((PARENT/'used_image_hashes.json').read_text())
    for name, value in evidence.used.items():
        if name in used and used[name] != value:
            raise ValueError('Parent source image hash conflict')
        used[name] = value
    for name, value in used.items():
        if digest(formal/name) != value:
            raise ValueError('Used source image changed')
    write_json(out/'used_image_hashes.json', used)
    after = protected_snapshot(formal); write_json(out/'protected_formal_after.json', after)
    if after != protected:
        raise ValueError('Protected formal files changed')
    verify_history_preserved(pose_live, pose_snapshot); verify_history_preserved(gallery_live, gallery_snapshot)
    write_json(out/'shortfalls.json', {cls: dict(missing=c['missing'], outcomes=c['outcomes'],
        explanation='All eligible targets scanned with unchanged gates; mask-history/deleted/deferred/confirmed and gallery-history targets excluded; '
            'original-rectangle-only pose saves eligible for sidecar proposals; distinct frames only.') for cls, c in counts.items()})
    manifest = dict(state='refined_pending_independent_verification', publication_version=4, version=VERSION,
        parent=str(PARENT.relative_to(PROJECT)), parent_proposals_sha256=digest(PARENT/'proposals.jsonl'),
        parent_verification_sha256=digest(PARENT/'verification.json'), previous_proposals=len(previous), new_proposals=len(new),
        proposals=len(combined), classes=counts, new_by_class=dict(Counter(p['class_id'] for p in new)),
        new_by_vertices=dict(Counter(p['vertex_count'] for p in new)), formal_manifest_sha256=formal_sha,
        classification_sha256=classification_sha, snapshot_sha256=pose_sha, gallery_snapshot_sha256=gallery_sha,
        snapshot_saved=len(current), snapshot_events=pose_events, gallery_snapshot_saved=len(reviews), gallery_snapshot_events=len(gallery_events),
        generation_templates=56, scanned_eligible_targets=len(work), accepted_before_diversity=len(accepted_proposals),
        outcomes=dict(Counter(v['reason'] for v in outcomes)), preserved_quarantined_targets=sorted(quarantine),
        proposals_sha256=digest(out/'proposals.jsonl'), new_proposals_sha256=digest(out/'new_proposals.jsonl'),
        refined_annotations_sha256=digest(out/'refined_annotations.jsonl'), template_inventory_sha256=digest(out/'template_inventory.json'),
        code_sha256=digest(__file__), transfer_code_sha256=digest(PROJECT/'pallet/mask_template_transfer.py'),
        boundary_code_sha256=digest(PROJECT/'pallet/mask_boundary_review.py'), attempt_code_sha256=digest(PROJECT/'pallet/mask_template_continue.py'),
        production_database_writes=0, original_splits_preserved=True, cross_split_template_transfers=0,
        recursive_candidate_propagation=False, thresholds_relaxed=False, automatic_human_confirmation=False,
        automatic_training_eligible=False, robot_command_eligible=False, ground_truth_eligible=False)
    write_json(out/'manifest.json', manifest)
    write_json(out/'artifact_hashes.json', {str(p.relative_to(out)): digest(p) for p in sorted(out.rglob('*')) if p.is_file()})
    print(json.dumps(dict(state=manifest['state'], new=len(new), total=len(combined), classes=counts)), flush=True)


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=PROJECT/'outputs/pallet_mask_template_refined_v2')
    parser.add_argument('--workers', type=int, default=4)
    args = parser.parse_args()
    if not 1 <= args.workers <= 8:
        raise ValueError('Workers must be 1–8')
    cv2.setNumThreads(1)
    generate(args.output, args.workers)


if __name__ == '__main__':
    main()
