"""Publication 5: direct transfer from all 97 frozen, saved gallery-human edits.

Writes only a fresh output directory; independent verification precedes publication.
"""
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 cv2
import numpy as np
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_refined import (CLASSES, QUARANTINE, protected_pose_keys,
    readonly, read_jsonl, select_diverse, snapshot_database, verify_history_preserved,
    write_jsonl)
from .mask_template_transfer import (PROJECT, ImageEvidence, compatible,
    package_preview, write_json)
from .pose_review_geometry import canonical, polygon_value

VERSION = 'gallery_human_refined_fixed_vertex_v2'
PARENT = PROJECT/'outputs/pallet_mask_template_refined_v2'
EXPECTED_PARENT = 97
PUBLICATION_VERSION = 5
GENERATION_NAMESPACE = 'generation'
SOURCE_PUBLICATIONS = {
    3: PROJECT/'outputs/pallet_mask_template_bulk_v5',
    4: PARENT,
}


def gallery_templates(previous, reviews, events, current, rows, parent, formal_sha,
                      snapshot_sha, publication_paths=None):
    """Bind each latest human edit to its actual frozen source publication."""
    if len(previous) != EXPECTED_PARENT or len({p['target_key'] for p in previous}) != EXPECTED_PARENT:
        raise ValueError('Expected exactly 97 distinct parent proposals')
    if set(reviews) != {p['target_key'] for p in previous}:
        raise ValueError('Every parent target must have its saved human edit')
    publications = {}
    for version, path in (SOURCE_PUBLICATIONS if publication_paths is None else publication_paths).items():
        manifest = json.loads((path/'manifest.json').read_text())
        verification = json.loads((path/'verification.json').read_text())
        proposals_sha = digest(path/'proposals.jsonl')
        if (manifest['publication_version'] != version or manifest['formal_manifest_sha256'] != formal_sha or
                manifest['proposals_sha256'] != proposals_sha or verification['state'] != 'passed' or
                verification['proposals_sha256'] != proposals_sha):
            raise ValueError('Historical publication identity/verification mismatch')
        proposals = read_jsonl(path/'proposals.jsonl')
        by_key = {p['target_key']: p for p in proposals}
        if len(by_key) != len(proposals):
            raise ValueError('Historical publication has duplicate proposals')
        publications[version] = (sha(dict(formal=formal_sha, proposals=proposals_sha,
            editor_version=EDITOR_VERSION)), by_key)
    histories = {}
    for event in events:
        histories.setdefault(event['target_key'], []).append(event)
    templates = {}
    for p in previous:
        key = p['target_key']; row = rows[key]; review = reviews[key]
        if len(review.get('polygon', [])) <= 4:
            raise ValueError('Every parent target needs a saved fine gallery polygon')
        publication = publications.get(review.get('proposal_publication'))
        if publication is None:
            raise ValueError('Unknown gallery source publication')
        binding, source_proposals = publication
        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 or source_proposals.get(key) != p):
            raise ValueError(f'Gallery source/proposal binding mismatch: {key}')
        history = histories.get(key, [])
        if (not history or len({v['revision'] for v in history}) != len(history) or
                max(history, key=lambda v: v['revision']) != review):
            raise ValueError('Gallery current record is not its unique 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', 'ground_truth_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 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)
    # package_preview writes fresh templates at the root; move them out before
    # copying both historical namespaces verbatim, even when target keys overlap.
    (out/GENERATION_NAMESPACE).mkdir()
    (out/'templates').rename(out/GENERATION_NAMESPACE/'templates')
    for directory in ('templates', 'refined'):
        shutil.copytree(parent/directory, out/directory)
        for file in sorted((parent/directory).rglob('*')):
            if file.is_file():
                hashes[str(file.relative_to(parent))] = 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')
    if any(digest(out/path) != value for path, value in hashes.items()):
        raise ValueError('Historical asset copy mismatch')
    return hashes


def rendering_inventory(parent_inventory, templates, combined, new_keys):
    historical = parent_inventory['templates']
    inherited = [dict(t, rendering_id=t.get('rendering_id', 'historical:'+t['row']['target_key'])) for t in historical]
    generation = [dict(t, rendering_id=GENERATION_NAMESPACE+':'+key) for key, t in sorted(templates.items())]
    ids = [t['rendering_id'] for t in inherited+generation]
    if len(ids) != len(set(ids)):
        raise ValueError('Rendering template namespace collision')
    mapping = {p['target_key']: (GENERATION_NAMESPACE+':'+p['template']['target_key'] if p['target_key'] in new_keys
        else parent_inventory['rendering_by_target'][p['target_key']]) for p in combined}
    if not set(mapping.values()) <= set(ids):
        raise ValueError('Rendering template missing')
    return dict(templates=inherited+generation, generation_template_keys=sorted(templates),
        generation_template_namespace=GENERATION_NAMESPACE, historical_templates=len(historical),
        gallery_human_templates=len(templates), rendering_by_target=mapping,
        conflicting_template_keys=sorted({t['row']['target_key'] for t in historical} & templates.keys()))


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=5" 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=5">{"候选" if new else "历史候选"} LabelMe</a> · '
            f'<a href="masks/{key}.png?revision=5">掩膜</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=5')}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>仅用本轮快照中的97份已保存人工精修多边形作为直接模板；新候选不继续传播。逐顶点配准与横梁边界检查沿用原门槛。只匹配同类、同划分、同传感器配置及兼容视角。人工掩膜修改历史、删除、暂缓、确认及已保存图册目标均受保护。仅保存位姿或标尺且掩膜始终为原矩形的目标可生成独立候选；原位姿及全部历史保持不变。</p>
<p>每类目标为累计50个不同帧。已穷尽可用兼容目标；缺额如实保留。新结果均为待复核草稿，未确认为GT，不进入训练或机器人控制。</p>
<p>原97份候选及图像、掩膜、对照图原样保留；原卡片为历史对照，最新人工精修在在线编辑器中加载，导出已保存修订请使用编辑器。新卡片左侧为已保存精修模板，右侧为机器候选。</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 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_manifest['publication_version'] != PUBLICATION_VERSION-1 or
            parent_verification['state'] != 'passed' or
            parent_verification['proposals_sha256'] != digest(PARENT/'proposals.jsonl')):
        raise ValueError('Parent proposals lack matching independent verification')
    frozen_code = {'pallet/mask_template_refined.py': parent_manifest['code_sha256'],
        'pallet/mask_template_transfer.py': parent_manifest['transfer_code_sha256'],
        'pallet/mask_boundary_review.py': parent_manifest['boundary_code_sha256'],
        'pallet/mask_template_continue.py': parent_manifest['attempt_code_sha256']}
    if any(digest(PROJECT/name) != value for name, value in frozen_code.items()):
        raise ValueError('Frozen generation/transfer/threshold code changed')
    previous = read_jsonl(PARENT/'proposals.jsonl'); previous_keys = {p['target_key'] for p in previous}
    if (len(previous) != EXPECTED_PARENT or len(previous_keys) != EXPECTED_PARENT or
            len({p['source']['frame_id'] for p in previous}) != EXPECTED_PARENT or
            Counter(p['class_id'] for p in previous) != {1: 27, 2: 5, 9: 20, 10: 8, 15: 37}):
        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'] or formal_sha != parent_manifest['formal_manifest_sha256']:
        raise ValueError('Formal/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)
    for p in previous:
        with Image.open(PARENT/'images'/f'{p["target_key"]}.png') as image:
            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=PUBLICATION_VERSION, 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
            len({p['source']['frame_id'] for p in combined}) != len(combined) or
            new_keys & (quarantine | gallery_protected | pose_protected)):
        raise ValueError('Protected or duplicated proposal target/frame')
    if len(outcomes) != len(rows) or {v['target_key'] for v in outcomes} != set(rows):
        raise ValueError('Incomplete outcome inventory')
    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)))
    inventory = rendering_inventory(json.loads((PARENT/'template_inventory.json').read_text()), templates, combined, new_keys)
    inventory.update(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/GENERATION_NAMESPACE/'labelme').mkdir()
    for key, t in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', t)
        write_json(out/GENERATION_NAMESPACE/'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)
    if any(digest(PROJECT/name) != value for name, value in frozen_code.items()):
        raise ValueError('Frozen code changed during generation')
    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=PUBLICATION_VERSION, 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=len(templates), generation_template_namespace=GENERATION_NAMESPACE,
        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'),
        parent_generator_code_sha256=frozen_code['pallet/mask_template_refined.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_v3')
    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()
