"""Publication 7: latest direct-human seed union, 100 distinct frames per class.

Only this fresh generation is written. Historical proposals/assets and live review
stores are never rewritten; publication still requires independent verification.
"""
from __future__ import annotations

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

import cv2
import numpy as np
from PIL import Image

from . import mask_quota_completion as original
from .cluster_targets import digest, protected_snapshot
from .mask_quota_saved_extension import file_inventory, load_formal_evidence, original_pose_url
from .mask_template_continue import attempt_target, sha
from .mask_template_refined import (CLASSES, QUARANTINE, read_jsonl, readonly,
    select_diverse, snapshot_database, verify_history_preserved, write_jsonl)
from .mask_template_transfer import PROJECT, annotation_inventory, package_preview, write_json
from .pose_review_geometry import canonical, polygon_value

VERSION = 'direct_human_union_quota100_v1'
PUBLICATION_VERSION = 7
TARGET_PER_CLASS = 100
GENERATION_NAMESPACE = 'quota100_generation'
PARENT = PROJECT/'outputs/pallet_mask_quota_completion_v2'
OUTPUT = PROJECT/'outputs/pallet_mask_quota_100_v2'
DIAGNOSTIC = PROJECT/'outputs/pallet_mask_quota_100_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_class10_manual_supplement_v2'
SOURCE_PUBLICATIONS = {
    3: PROJECT/'outputs/pallet_mask_template_bulk_v5',
    4: PROJECT/'outputs/pallet_mask_template_refined_v2',
    5: PROJECT/'outputs/pallet_mask_template_refined_v3',
    6: PARENT,
}
FALSE_FLAGS = original.FALSE_FLAGS
POSE_ORIGIN, GALLERY_ORIGIN = original.POSE_ORIGIN, original.GALLERY_ORIGIN
MANUAL_SCHEMA = original.MANUAL_SCHEMA


def read_json(path):
    return json.loads(Path(path).read_text())


def indexed(values):
    result = {v['target_key']: v for v in values}
    if len(result) != len(values):
        raise ValueError('Duplicate target key')
    return result


def active_polygon(value):
    return bool(value and value.get('state') not in ('deleted', 'deferred') and
        len(value.get('polygon', [])) > 4)


def quota_counts(previous, new, reviews, current, pose_templates, inherited_manual=(),
                 manual=(), goal=TARGET_PER_CLASS):
    """Latest saved key union, not stale proposal geometry or every pose seed."""
    combined = list(previous)+list(manual)+list(new)
    by_key = indexed(combined)
    if len({p['source']['frame_id'] for p in combined}) != len(combined):
        raise ValueError('Quota repeats a target or frame')
    if any(p['class_id'] not in CLASSES for p in combined):
        raise ValueError('Unexpected quota class')
    old_manual = set(indexed(inherited_manual))
    if not old_manual <= set(indexed(previous)):
        raise ValueError('Inherited manual source absent from parent')
    explicit_manual = old_manual | set(indexed(manual))
    gallery_keys = {k for k, v in reviews.items() if k in by_key and active_polygon(v)
        and v.get('human_edited') is True and v.get('event_kind') == 'human_mask_gallery_revision'
        and (not current.get(k) or current[k].get('state') not in ('deleted', 'deferred'))}
    pose_keys = {k for k in explicit_manual if k in pose_templates and active_polygon(current.get(k))
        and pose_templates[k]['polygon'] == current[k]['polygon']}
    result = {}
    for cls in CLASSES:
        old = [p for p in previous if p['class_id'] == cls]
        human = [p for p in manual if p['class_id'] == cls]
        added = [p for p in new if p['class_id'] == cls]
        keys = {p['target_key'] for p in old+human+added}
        old_keys = {p['target_key'] for p in old}
        saved_gallery = keys & gallery_keys
        saved_pose = keys & pose_keys
        saved = saved_gallery | saved_pose
        total = len(keys)
        if total > goal:
            raise ValueError('Quota exceeds goal')
        old_pending = len(old_keys-saved)
        requested = sum(p.get('manual_source_kind') == 'completed_manual_request' for p in human)
        reused = sum(p.get('manual_source_kind') == 'existing_saved_human_reuse_for_100_goal' for p in human)
        result[cls] = dict(old_total=len(old), manual_added=len(human), newmanual=requested,
            manual_requested=requested, manual_existing_reused=reused,
            added_saved_human=len(human), inherited_manual_sources=len(old_keys & old_manual),
            existing_saved_pose_human=len(old_keys & saved_pose),
            saved_gallery_human=len(saved_gallery), saved_pose_human=len(saved_pose),
            saved_human=len(saved), saved_fine_edits=len(saved),
            prior_machine_pending=old_pending, old_pending_machine=old_pending,
            new_candidates=len(added), new_machine_pending=len(added),
            total=total, missing=goal-total, goal=goal, target_per_class=goal,
            distinct_frames=len({p['source']['frame_id'] for p in old+human+added}))
    return result


def manual_source_proposals(requests, pose_templates, current, rows, previous, pose_sha,
                            formal_sha, classification_sha, quarantine=()):
    """Count only explicit completed requests; independently of the old 50 cap."""
    indexed(requests)
    previous_keys = set(indexed(previous))
    frames = {p['source']['frame_id'] for p in previous}
    direct = original.seed_union(pose_templates, {}, pose_sha, quarantine)
    result = []
    for request in sorted(requests, key=lambda r: r['target_key']):
        key = request['target_key']; row = rows[key]
        value, template = current.get(key), direct.get(key)
        if key in previous_keys or row['frame_id'] in frames or key in quarantine:
            raise ValueError('Manual source repeats a published frame or is quarantined')
        fields = ('target_key', 'target_id', 'frame_id', 'split', 'group_id', 'box_index',
            'bbox', 'size', 'source_pcd_sha256', 'policy_id', 'viewpoint_candidate')
        if (request['class_id'] != row['final_class_id'] or
                request['source']['formal_manifest_sha256'] != formal_sha or
                any(request['source'].get(k) != row.get(k) for k in fields)):
            raise ValueError('Manual request source identity changed')
        if (not active_polygon(value) or template is None or
                template['polygon'] != value['polygon'] or template['revision'] != value['revision'] or
                template['current_payload_sha256'] != sha(value)):
            raise ValueError('Manual request lacks a current direct human polygon')
        if any(value.get(flag) for flag in FALSE_FLAGS):
            raise ValueError('Manual source unexpectedly confirmed or eligible')
        polygon_value(value['polygon'], *row['size'])
        frames.add(row['frame_id'])
        result.append(dict(schema=MANUAL_SCHEMA, generation_schema=VERSION, origin=POSE_ORIGIN,
            target_key=key, class_id=row['final_class_id'], source=row,
            polygon=value['polygon'], vertex_count=len(value['polygon']),
            status='human_edited_draft_pending_confirmation', human_edited=True,
            source_label_kind='human_mask_source_manual_template_draft',
            manual_source_kind='completed_manual_request',
            **dict.fromkeys(FALSE_FLAGS, False), pose_snapshot_saved=True,
            pose_store_written=False, gallery_store_written=False, machine_transfer_performed=False,
            new_human_edit_created=False,
            mask_semantics='saved_human_fork_face_contour_pending_confirmation',
            template=original.template_metadata(template), rendering_id=GENERATION_NAMESPACE+':'+key,
            target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
            snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha,
            class_assignments_sha256=classification_sha, metrics={},
            flags=['manual_source_not_machine_transfer', 'saved_human_mask_requires_confirmation'],
            manual_request_sha256=sha(request), original_pose_url=request.get('pose_annotation_url'),
            geometry_pose_policy='original_pose_unchanged; independent_gallery_review_not_imported'))
    quota_counts(previous, [], {}, current, pose_templates, manual=result)
    return result


def existing_human_reuse(pose_templates, current, rows, previous, requested, pose_sha,
                        formal_sha, classification_sha, quarantine=()):
    """Authorized existing labels, not fabricated requests or new human edits.

    Any conflicting frame, quarantine, inactive source or eligibility claim is
    reported as excluded rather than forcing the expected production count.
    """
    direct = original.seed_union(pose_templates, {}, pose_sha)
    excluded_keys = set(indexed(previous)) | set(indexed(requested))
    frames = {p['source']['frame_id'] for p in list(previous)+list(requested)}
    result, inventory = [], []
    identity_fields = ('target_key', 'target_id', 'frame_id', 'split', 'group_id',
        'box_index', 'bbox', 'size', 'source_pcd_sha256', 'policy_id')
    for key, t in sorted(direct.items()):
        row, value = rows[key], current.get(key)
        if key in excluded_keys or row['final_class_id'] not in CLASSES:
            continue
        reason = ('quarantined' if key in quarantine else
            'duplicate_frame' if row['frame_id'] in frames else
            'inactive_or_mismatched_current' if not active_polygon(value) or
                value['polygon'] != t['polygon'] or value['revision'] != t['revision'] or
                sha(value) != t['current_payload_sha256'] else
            'source_identity_mismatch' if any(value.get('source', {}).get(f) != row[f] for f in identity_fields) else
            'confirmed_or_eligible' if any(value.get(flag) for flag in FALSE_FLAGS) else 'counted')
        inventory.append(dict(target_key=key, class_id=row['final_class_id'], reason=reason))
        if reason != 'counted':
            continue
        polygon_value(value['polygon'], *row['size'])
        frames.add(row['frame_id'])
        result.append(dict(schema=MANUAL_SCHEMA, generation_schema=VERSION, origin=POSE_ORIGIN,
            target_key=key, class_id=row['final_class_id'], source=row,
            polygon=value['polygon'], vertex_count=len(value['polygon']),
            status='human_edited_draft_pending_confirmation', human_edited=True,
            source_label_kind='human_mask_source_manual_template_draft',
            manual_source_kind='existing_saved_human_reuse_for_100_goal',
            **dict.fromkeys(FALSE_FLAGS, False), pose_snapshot_saved=True,
            pose_store_written=False, gallery_store_written=False, machine_transfer_performed=False,
            new_human_edit_created=False,
            mask_semantics='saved_human_fork_face_contour_pending_confirmation',
            template=original.template_metadata(t), rendering_id=GENERATION_NAMESPACE+':'+key,
            target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
            snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha,
            class_assignments_sha256=classification_sha, metrics={},
            flags=['manual_source_not_machine_transfer', 'saved_human_mask_requires_confirmation',
                'existing_human_reuse_not_new_edit'],
            manual_reuse_authorization='Explicit user authorization to count existing genuine saved '
                'original-pose human masks toward the 100-per-class goal; no synthetic request',
            original_pose_url=original_pose_url(requested, key),
            geometry_pose_policy='original_pose_unchanged; independent_gallery_review_not_imported'))
    return result, inventory


def audit_inherited_manual(db, manual, pose_templates, current, pose_sha):
    """Keep canonical legacy proposals; freeze their *current* source separately."""
    direct = original.seed_union(pose_templates, {}, pose_sha)
    records, audit = [], []
    for p in sorted(manual, key=lambda v: v['target_key']):
        key = p['target_key']; value, t = current.get(key), direct.get(key)
        active = active_polygon(value) and t is not None and t['polygon'] == value['polygon']
        changed = (value is None or sha(value) != p['target_snapshot_payload_sha256'])
        item = dict(target_key=key, original_proposal_sha256=sha(p),
            historical_revision=p['target_expected_revision'],
            current_revision=value['revision'] if value else None,
            current_payload_sha256=sha(value) if value else None,
            latest_active_direct_human=bool(active), source_changed=changed,
            polygon_changed=value is None or value.get('polygon') != p['polygon'],
            historical_assets_preserved=True)
        audit.append(item)
        if active:
            live_source = dict(p, polygon=value['polygon'], template=original.template_metadata(t),
                target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
                snapshot_sha256=pose_sha)
            record = original.freeze_manual_sources(db, [live_source])[0]
        else:
            raw_rows = db.execute('SELECT payload FROM annotations WHERE target_key=?', (key,)).fetchall()
            raw = raw_rows[0][0] if raw_rows else None
            record = dict(schema='inactive_legacy_pose_mask_source_audit_v1', target_key=key,
                snapshot_sha256=pose_sha, current_payload_raw=raw, current_payload=value,
                current_payload_raw_sha256=hashlib.sha256(raw.encode()).hexdigest() if raw else None)
        records.append(dict(record, inherited_manual_source=True, legacy_source_audit=item))
    return records, audit


def proposal_from_attempt(key, row, accepted, templates, current, pose_sha, formal_sha, classification_sha):
    p = original.proposal_from_attempt(key, row, accepted, templates, current,
        pose_sha, formal_sha, classification_sha)
    return dict(p, schema=VERSION, continuation=dict(p['continuation'], batch=PUBLICATION_VERSION,
        all_compatible_templates_considered=True), target_per_class=TARGET_PER_CLASS)


def rendering_inventory(parent_inventory, templates, combined, new_keys, manual_keys=()):
    inherited = parent_inventory['templates']
    generation = [dict(t, rendering_id=GENERATION_NAMESPACE+':'+k) for k, 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')
    added = set(new_keys) | set(manual_keys)
    mapping = {p['target_key']: (GENERATION_NAMESPACE+':'+p['template']['target_key']
        if p['target_key'] in added 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, rendering_by_target=mapping,
        generation_template_namespace=GENERATION_NAMESPACE, generation_template_keys=sorted(templates),
        historical_templates=len(inherited),
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())))


def preserve_parent_assets(parent, out, previous):
    """Move only the new renderer's templates; copy every historical namespace."""
    hashes = {}
    (out/GENERATION_NAMESPACE).mkdir()
    (out/'templates').rename(out/GENERATION_NAMESPACE/'templates')
    for p in previous:
        for folder, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            relative = f'{folder}/{p["target_key"]}.{extension}'
            if (out/relative).exists():
                raise ValueError('Historical asset collision')
            shutil.copyfile(parent/relative, out/relative)
            hashes[relative] = digest(parent/relative)
    for directory in ('templates', 'refined', 'generation', 'quota_generation'):
        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/name) != value for name, value in hashes.items()):
        raise ValueError('Historical asset copy mismatch')
    return hashes


def build_report(out, combined, new_keys, counts, reviews, current, inherited_manual=(), manual=()):
    manual_keys = set(indexed(inherited_manual)) | set(indexed(manual))
    added_manual = set(indexed(manual))
    total = lambda field: sum(c[field] for c in counts.values())
    table = ''.join(f'<tr><td>{cls}</td><td>{c["old_total"]}</td><td>{c["manual_added"]}</td>'
        f'<td>{c["saved_gallery_human"]}</td><td>{c["saved_pose_human"]}</td>'
        f'<td>{c["old_pending_machine"]}</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']; is_manual = key in manual_keys
        if key in new_keys:
            label = '【本轮新增机器候选】待人工复核'
        elif key in added_manual and p.get('manual_source_kind') == 'existing_saved_human_reuse_for_100_goal':
            label = f'【既有人工轮廓复用 r{p["target_expected_revision"]}】不是新人工编辑，不是机器迁移，未确认真值'
        elif key in added_manual:
            label = f'【本轮计入人工描绘 r{p["target_expected_revision"]}】不是机器迁移，未确认真值'
        elif key in reviews and active_polygon(reviews[key]):
            label = f'【已保存图册人工修订 r{reviews[key]["revision"]}】历史候选对照，不是新候选'
        elif is_manual and active_polygon(current.get(key)):
            label = f'【既有原位姿人工描绘 r{current[key]["revision"]}】保留历史快照，未确认真值'
        else:
            label = '【历史候选】待人工复核，未计为已保存人工轮廓'
        link = (f'<p><a href="{html.escape(p.get("original_pose_url") or "", quote=True)}">打开原位姿人工标注</a>；'
            '图中为发布时人工快照，没有机器评分；独立图册保存历史未导入。</p>' if is_manual else '')
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" '
            f'data-batch="{"new" if key in new_keys else "previous"}" '
            f'data-kind="{"manual" if is_manual else "machine"}"'
            f'{"" if key in new_keys else " hidden"}><h2>类别 {p["class_id"]} · {label}</h2>'
            f'<img loading="lazy" src="cases/{key}.png?revision=7" alt="{"人工快照" if is_manual else "直接人工模板与候选对照"}">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>{link}'
            f'<a href="labelme/{key}.json?revision=7">多边形（LabelMe）</a> · '
            f'<a href="masks/{key}.png?revision=7">掩膜</a></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=7')}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1500px}a{color:#77ceff}img{width:100%}article{border:1px solid #425269;margin:20px 0;padding:12px}td,th{padding:7px;border:1px solid #425269}table{border-collapse:collapse}[hidden]{display:none!important}</style>
<h1>第七版 · 每类一百个不同帧（待独立核验）</h1>
<p>直接种子来自最新已保存图册人工多边形与原始位姿人工多边形的并集，均超过四个顶点；同目标优先图册版本。未保存机器候选不作种子，其他原位姿模板不自动计入人工配额。全部剩余合格目标均以原严格方法扫描，不递归传播，不放宽门槛。</p>
<p>仅同类、同划分、同传感器配置和兼容视角迁移，保持人工模板顶点数与顺序；相关度、配准及横梁边界原检查不变。各类累计上限为100个不同帧，缺额如实保留。所有人工与机器草稿均未确认真值，不自动进入训练或机器人控制。</p>
<p>历史237份候选对象和所有图像、掩膜、标注文件原样保留。历史卡片不是最新人工轮廓的渲染；最新保存修订请在在线编辑器查看或导出。左侧直接人工模板与右侧候选对照仅用于本轮新增机器结果；原位姿人工快照没有机器迁移或虚构评分。</p>
'''
    page += (f'<p>历史候选共 {total("old_total")} 份；本轮计入已保存人工描绘 {total("manual_added")} 份，'
        f'其中完成人工请求 {total("manual_requested")} 份，既有已保存人工轮廓复用 {total("manual_existing_reused")} 份（不是新人工编辑）。'
        f'已保存图册人工修订 {total("saved_gallery_human")} 份，原位姿人工轮廓 {total("saved_pose_human")} 份，'
        f'人工来源去重共 {total("saved_human")} 份；历史机器候选待复核 {total("old_pending_machine")} 份。'
        f'本轮新增机器候选 {total("new_candidates")} 份，累计 {total("total")} 个不同帧，距500帧仍缺 {total("missing")} 帧。已保存不等于已确认真值。</p>')
    page += '<table><tr><th>类别</th><th>历史总数</th><th>新计入人工</th><th>已保存图册人工</th><th>已保存原位姿人工</th><th>历史待复核</th><th>新增机器候选</th><th>累计不同帧</th><th>缺额</th></tr>'+table+'</table>'
    page += '<p>筛选类别 <select id="filter"><option value="">全部</option>'+''.join(f'<option>{c}</option>' for c in CLASSES)+'</select> '
    page += '<label><input type="checkbox" id="only-new" checked>只看本轮新增机器候选</label></p>'+''.join(cards)
    page += """<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')
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    with TTFont(source) as font:
        if not required <= set(font.getBestCmap()):
            raise ValueError('Report source font coverage failure')
        sub = subset.Subsetter(); sub.populate(text=page); sub.subset(font)
        font.save(out/'report-font.otf')
    with TTFont(out/'report-font.otf') as font:
        if not required <= set(font.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 validate_snapshot_rows(db, rows, table):
    for key, revision, raw in db.execute(f'SELECT target_key,revision,payload FROM {table}'):
        value = json.loads(raw)
        if key not in rows or value['target_key'] != key or value['revision'] != revision:
            raise ValueError('Review relational identity mismatch')


def verified_parent(parent):
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 6 or manifest.get('proposals') != 237 or
            manifest.get('manual_proposals') != 30 or seal.get('state') != 'passed' or
            seal.get('publication_version') != 6 or seal.get('proposals_verified') != 237 or
            seal.get('manifest_sha256') != digest(parent/'manifest.json') or
            seal.get('proposals_sha256') != digest(parent/'proposals.jsonl') or
            manifest.get('proposals_sha256') != seal['proposals_sha256']):
        raise ValueError('Parent requires matching independent verification of 237 entries')
    hashes = file_inventory(parent)
    for name, expected in read_json(parent/'artifact_hashes.json').items():
        if name not in hashes or hashes[name] != expected:
            raise ValueError('Frozen parent artifact changed: '+name)
    frozen = dict(manifest['frozen_code_sha256'])
    frozen['pallet/mask_quota_saved_extension.py'] = manifest['extension_code_sha256']
    for name, expected in frozen.items():
        if digest(PROJECT/name) != expected:
            raise ValueError('Frozen transfer/generator code changed: '+name)
    frozen['pallet/mask_quota_100.py'] = digest(__file__)
    return manifest, hashes, frozen


def generate(output=OUTPUT, workers=4, method='original', parent=PARENT, requests_directory=REQUESTS):
    """Explicit exhaustive scan; no DB/config/service writes or automatic publication."""
    if method != 'original':
        raise ValueError('Only the frozen original method is implemented')
    if not 1 <= workers <= 8:
        raise ValueError('Workers must be 1–8')
    out, parent = Path(output).resolve(), Path(parent).resolve()
    request_dir = Path(requests_directory).resolve()
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory')
    manifest, parent_hashes, frozen_code = verified_parent(parent)
    diagnostic_hashes = file_inventory(DIAGNOSTIC) if DIAGNOSTIC.is_dir() and DIAGNOSTIC != out else {}
    configuration_hashes = file_inventory(PROJECT/'configs')
    formal, rows, evidence = load_formal_evidence(manifest)
    formal_sha, classification_sha = manifest['formal_manifest_sha256'], manifest['classification_sha256']
    paths = dict(SOURCE_PUBLICATIONS); paths[6] = parent
    publications = original.publication_inventory(paths, formal_sha)
    protected = protected_snapshot(formal)
    if (len(protected) != 20768 or protected != read_json(parent/'protected_formal_before.json') or
            protected != read_json(parent/'protected_formal_after.json')):
        raise ValueError('Protected 20768 formal files differ from parent')
    previous, inherited_manual = read_jsonl(parent/'proposals.jsonl'), read_jsonl(parent/'manual_proposals.jsonl')
    previous_keys, inherited_keys = set(indexed(previous)), set(indexed(inherited_manual))
    if (len(previous) != 237 or len(inherited_manual) != 30 or
            Counter(p['class_id'] for p in previous) != {1: 50, 2: 50, 9: 50, 10: 37, 15: 50} or
            len({p['source']['frame_id'] for p in previous}) != 237 or
            any(p['source'] != rows[p['target_key']] or p['formal_manifest_sha256'] != formal_sha
                for p in previous) or any(indexed(previous).get(p['target_key']) != p for p in inherited_manual)):
        raise ValueError('Parent canonical/source inventory changed')
    requests = read_jsonl(request_dir/'requests.jsonl')
    request_seal = read_json(request_dir/'verification.json')
    request_hashes = {str(request_dir/name): digest(request_dir/name)
        for name in ('requests.jsonl', 'manifest.json', 'verification.json')}
    if (len(requests) != 13 or Counter(r['class_id'] for r in requests) != {10: 13} or
            request_seal.get('state') != 'passed' or request_seal.get('request_count') != 13 or
            request_seal.get('requests_sha256') != digest(request_dir/'requests.jsonl')):
        raise ValueError('Expected unchanged verified 13-item Class 10 request queue')
    out.mkdir(parents=True)
    write_json(out/'protected_formal_before.json', protected)
    write_json(out/'parent_file_inventory.json', parent_hashes)
    write_json(out/'configuration_input_hashes.json', configuration_hashes)
    if diagnostic_hashes:
        write_json(out/'preserved_diagnostic_file_inventory.json', diagnostic_hashes)
    (out/'parent_evidence').mkdir()
    for name in ('manifest.json', 'verification.json', 'proposals.jsonl', 'manual_proposals.jsonl',
            'manual_source_records.jsonl', 'new_proposals.jsonl', 'template_inventory.json', 'requests.jsonl'):
        shutil.copyfile(parent/name, out/'parent_evidence'/name)
    for name in frozen_code:
        destination = out/'frozen_code'/name
        destination.parent.mkdir(parents=True, exist_ok=True)
        shutil.copyfile(PROJECT/name, destination)
    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, gallery_snapshot = out/'reviews_snapshot.sqlite3', out/'gallery_reviews_snapshot.sqlite3'
    pose_sha, gallery_sha = snapshot_database(pose_live, pose_snapshot), snapshot_database(gallery_live, gallery_snapshot)
    with closing(readonly(pose_snapshot, True)) as db:
        current, pose_templates, status = annotation_inventory(db, rows)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        if json.loads(db.execute("SELECT value FROM metadata WHERE key='binding'").fetchone()[0])['formal_manifest_sha256'] != formal_sha:
            raise ValueError('Pose formal binding mismatch')
        for table in ('annotations', 'events'):
            validate_snapshot_rows(db, rows, table)
        inherited_sources, inherited_audit = audit_inherited_manual(db, inherited_manual, pose_templates, current, pose_sha)
    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 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 table in ('reviews', 'events'):
            validate_snapshot_rows(db, rows, table)
    gallery = original.gallery_templates(previous, reviews, gallery_events, current, rows,
        parent, formal_sha, gallery_sha, publication_paths=paths)
    quarantine = QUARANTINE | set(manifest.get('preserved_quarantined_targets', [])) | set(manifest.get('quarantined_targets', []))
    templates = original.seed_union(pose_templates, gallery, pose_sha, quarantine)
    requested_manual = manual_source_proposals(requests, pose_templates, current, rows, previous,
        pose_sha, formal_sha, classification_sha, quarantine)
    reused_manual, reuse_inventory = existing_human_reuse(pose_templates, current, rows, previous,
        requested_manual, pose_sha, formal_sha, classification_sha, quarantine)
    manual = sorted(requested_manual+reused_manual, key=lambda p: p['target_key'])
    manual_keys = set(indexed(manual))
    manual_reuse_keys = set(indexed(reused_manual))
    with closing(readonly(pose_snapshot, True)) as db:
        manual_sources = original.freeze_manual_sources(db, manual)
    with closing(readonly(parent/'reviews_snapshot.sqlite3', True)) as db:
        _, old_pose_templates, _ = annotation_inventory(db, rows)
    new_pose_seed_only = sorted(set(pose_templates)-set(old_pose_templates)-manual_keys)
    base_counts = quota_counts(previous, [], reviews, current, pose_templates, inherited_manual, manual)
    if len(requested_manual) != 13 or len(manual) != 13+len(reused_manual):
        raise ValueError('Completed requests/reused sources partition mismatch')
    all_sources = sorted(inherited_sources+manual_sources, key=lambda p: p['target_key'])
    shutil.copyfile(request_dir/'requests.jsonl', out/'requests.jsonl')
    for name, values in (('manual_proposals', manual),
            ('manual_source_records', manual_sources), ('all_manual_sources', all_sources),
            ('manual_reuse_inventory', reuse_inventory),
            ('inherited_manual_source_audit', inherited_audit),
            ('refined_annotations', [reviews[k] for k in sorted(reviews)])):
        write_jsonl(out/f'{name}.jsonl', values)
    gallery_protected = set(reviews) | {v['target_key'] for v in gallery_events}
    pose_protected = {k for k, reason in status.items() if reason != 'eligible'}
    work, outcomes = original.target_inventory(rows, previous, status, gallery_protected, quarantine, manual_keys)
    write_json(out/'protected_targets.json', dict(previous=sorted(previous_keys),
        counted_manual_source_keys=sorted(inherited_keys | manual_keys), newly_counted_manual_source_keys=sorted(manual_keys),
        manual_reuse_keys=sorted(manual_reuse_keys), requested_manual_keys=sorted(indexed(requested_manual)),
        pose_mask_history_or_noneligible=sorted(pose_protected), gallery_saved_or_history=sorted(gallery_protected),
        quarantine=sorted(quarantine), eligibility=status))
    write_json(out/'snapshot_manifest.json', dict(formal_manifest_sha256=formal_sha,
        pose=dict(path=pose_snapshot.name, backup='backups/'+pose_snapshot.name, sha256=pose_sha,
            saved=len(current), events=pose_events),
        gallery=dict(path=gallery_snapshot.name, backup='backups/'+gallery_snapshot.name, sha256=gallery_sha,
            saved=len(reviews), events=len(gallery_events)),
        manual_requests=dict(directory=str(request_dir), count=len(requests), frozen_input_sha256=request_hashes),
        source_publications={v: {k: str(value) if k == 'path' else value
            for k, value in pub.items() if k != 'proposals'} for v, pub in publications.items()}))
    for request in requests:
        row = rows[request['target_key']]
        evidence.image(row['frame_id'])
        if request['source']['image_sha256'] != evidence.used[request['source']['image']]:
            raise ValueError('Manual request formal image changed')
    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 image differs from formal pixels')
    print(json.dumps(dict(state='scanning', publication_version=PUBLICATION_VERSION,
        templates=len(templates), templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        gallery_saved=len(reviews), manual_added=len(manual), manual_requested=len(requested_manual),
        manual_existing_reused=len(reused_manual), inherited_manual_sources=len(inherited_manual),
        inherited_manual_changed=[v['target_key'] for v in inherited_audit if v['source_changed']],
        unexpected_new_pose_seed_only=new_pose_seed_only, base_count=len(previous)+len(manual),
        base_classes={cls: c['total'] for cls, c in base_counts.items()},
        base_gaps={cls: c['missing'] for cls, c in base_counts.items()},
        eligible_targets=len(work), workers=workers, method=method)), flush=True)
    attempts, accepted_proposals = [], []
    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:
                accepted_proposals.append(proposal_from_attempt(key, row, accepted, templates,
                    current, pose_sha, formal_sha, classification_sha))
            if number % 200 == 0:
                print(json.dumps(dict(scanned=number, accepted=len(accepted_proposals), attempts=len(attempts))), flush=True)
    new, dispositions = select_diverse(previous+manual, accepted_proposals, goal=TARGET_PER_CLASS)
    for outcome in outcomes:
        if outcome['target_key'] in dispositions:
            outcome['reason'] = dispositions[outcome['target_key']]
    combined = sorted(previous+manual+new, key=lambda p: p['target_key'])
    new_keys = set(indexed(new))
    if new_keys & (previous_keys | manual_keys | quarantine | gallery_protected | pose_protected):
        raise ValueError('Protected target selected')
    if len(outcomes) != len(rows) or set(indexed(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)
    restored = indexed(read_jsonl(out/'proposals.jsonl'))
    if any(canonical(restored[k]) != canonical(p) for k, p in indexed(previous).items()):
        raise ValueError('Historical canonical proposal changed')
    counts = quota_counts(previous, new, reviews, current, pose_templates, inherited_manual, manual)
    for cls, c in counts.items():
        class_outcomes = [v for v in outcomes if v['class_id'] == cls]
        c.update(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=c['total'], outcomes=dict(Counter(v['reason'] for v in class_outcomes)))
    inventory = rendering_inventory(read_json(parent/'template_inventory.json'), templates, combined, new_keys, manual_keys)
    inventory.update(schema=VERSION, publication_version=PUBLICATION_VERSION, target_per_class=TARGET_PER_CLASS,
        classes=counts, eligibility=status, snapshot_saved=len(current), snapshot_events=pose_events,
        generation_templates=len(templates), gallery_snapshot_saved=len(reviews), previous_proposals=len(previous),
        manual_proposals=len(manual), inherited_manual_sources=len(inherited_manual), all_manual_sources=len(all_sources),
        manual_requested=len(requested_manual), manual_existing_reused=len(reused_manual),
        manual_reuse_keys=sorted(manual_reuse_keys), requested_manual_keys=sorted(indexed(requested_manual)),
        counted_manual_source_keys=sorted(inherited_keys | manual_keys),
        newly_counted_manual_source_keys=sorted(manual_keys), unexpected_new_pose_seed_only=new_pose_seed_only)
    write_json(out/'template_inventory.json', inventory)
    package_preview(out, new, templates, rows, evidence, counts)
    original.package_manual_preview(out, manual, evidence)
    copied = preserve_parent_assets(parent, out, previous)
    write_json(out/'preserved_parent_assets.json', copied)
    for key, t in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', t)
    build_report(out, combined, new_keys, counts, reviews, current, inherited_manual, manual)
    contact = Image.new('RGB', (1480, len(CLASSES)*305), '#121923')
    for i, cls in enumerate(CLASSES):
        p = next(p for p in new+manual+previous if p['class_id'] == cls)
        with Image.open(out/'cases'/f'{p["target_key"]}.png') as image:
            contact.paste(image, (0, i*305))
    contact.save(out/'preview_all_classes.png')
    used = read_json(parent/'used_image_hashes.json')
    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
    if any(digest(formal/name) != value for name, value in used.items()):
        raise ValueError('Used formal 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 (digest(pose_snapshot) != pose_sha or digest(gallery_snapshot) != gallery_sha or
            any(digest(out/'backups'/p.name) != digest(p) for p in (pose_snapshot, gallery_snapshot))):
        raise ValueError('Frozen review snapshot/backup changed')
    if (file_inventory(parent) != parent_hashes or file_inventory(PROJECT/'configs') != configuration_hashes or
            any(digest(PROJECT/name) != value for name, value in frozen_code.items()) or
            any(digest(name) != value for name, value in request_hashes.items()) or
            digest(out/'requests.jsonl') != digest(request_dir/'requests.jsonl') or
            diagnostic_hashes and file_inventory(DIAGNOSTIC) != diagnostic_hashes):
        raise ValueError('Frozen parent/code/config/request input changed during generation')
    if any(digest(out/name) != value for name, value in copied.items()):
        raise ValueError('Historical asset changed during generation')
    write_json(out/'shortfalls.json', {cls: dict(missing=c['missing'], outcomes=c['outcomes'],
        explanation='Every remaining eligible target was scanned with all compatible direct-human templates '
            'above the unchanged initial gate. Original ECC/NCC/forward-backward/beam gates and distinct-frame '
            'selection at goal 100 were retained; no forced fill or recursive seed propagation. '
            'The 13 completed manual requests and authorized existing saved-human reuse are counted '
            'separately; original requests are unchanged, and other pose seeds do not join quota implicitly.')
        for cls, c in counts.items()})
    result = dict(state='quota_pending_independent_verification', schema=VERSION, version=VERSION,
        publication_version=PUBLICATION_VERSION, target_per_class=TARGET_PER_CLASS, target_total=500,
        parent=str(parent), parent_manifest_sha256=parent_hashes['manifest.json'],
        parent_proposals_sha256=parent_hashes['proposals.jsonl'], parent_verification_sha256=parent_hashes['verification.json'],
        preserved_diagnostic_generation=str(DIAGNOSTIC) if diagnostic_hashes else None,
        preserved_diagnostic_manifest_sha256=diagnostic_hashes.get('manifest.json'),
        previous_proposals=len(previous), new_proposals=len(new), manual_proposals=len(manual), manual_added=len(manual),
        manual_requested=len(requested_manual), manual_existing_reused=len(reused_manual),
        manual_reuse_keys=sorted(manual_reuse_keys), requested_manual_keys=sorted(indexed(requested_manual)),
        manual_reuse_excluded=[v for v in reuse_inventory if v['reason'] != 'counted'],
        inherited_manual_sources=len(inherited_manual), all_manual_sources=len(all_sources),
        inherited_manual_source_revisions_changed=[v['target_key'] for v in inherited_audit if v['source_changed']],
        base_proposals=len(previous)+len(manual), base_classes=base_counts, proposals=len(combined), classes=counts,
        manual_by_class=dict(Counter(p['class_id'] for p in manual)), manual_source_schema=MANUAL_SCHEMA,
        counted_manual_source_keys=sorted(inherited_keys | manual_keys), newly_counted_manual_source_keys=sorted(manual_keys),
        manual_source_snapshots_imported_into_gallery_store=False,
        new_by_class=dict(Counter(p['class_id'] 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,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        unexpected_new_pose_seed_only=new_pose_seed_only, unexpected_pose_seeds_implicitly_counted=0,
        method=method, scanned_eligible_targets=len(work), accepted_before_diversity=len(accepted_proposals),
        registration_attempts=len(attempts), outcomes=dict(Counter(v['reason'] for v in outcomes)),
        saved_gallery_human=sum(c['saved_gallery_human'] for c in counts.values()),
        existing_saved_pose_human=sum(c['existing_saved_pose_human'] for c in counts.values()),
        saved_pose_human=sum(c['saved_pose_human'] for c in counts.values()),
        saved_human=sum(c['saved_human'] for c in counts.values()),
        old_pending_machine=sum(c['old_pending_machine'] for c in counts.values()),
        new_machine_pending=len(new), missing=sum(c['missing'] for c in counts.values()),
        preserved_quarantined_targets=sorted(quarantine), manual_request_input_sha256=request_hashes,
        code_sha256=digest(__file__), frozen_code_sha256=frozen_code,
        transfer_code_sha256=frozen_code['pallet/mask_template_transfer.py'],
        boundary_code_sha256=frozen_code['pallet/mask_boundary_review.py'],
        attempt_code_sha256=frozen_code['pallet/mask_template_continue.py'],
        production_database_writes=0, configuration_writes=0, service_writes=0,
        protected_formal_files=len(protected), original_splits_preserved=True, cross_split_template_transfers=0,
        recursive_candidate_propagation=False, thresholds_relaxed=False, forced_fill=False,
        new_human_edits_created=0, research_candidates_added=0, automatic_human_confirmation=False,
        automatic_training_eligible=False, robot_command_eligible=False, ground_truth_eligible=False)
    for name in ('proposals', 'new_proposals', 'manual_proposals', 'manual_source_records',
            'all_manual_sources', 'inherited_manual_source_audit', 'requests', 'refined_annotations',
            'manual_reuse_inventory', 'accepted_candidates', 'registration_attempts', 'outcomes'):
        result[name+'_sha256'] = digest(out/f'{name}.jsonl')
    result['template_inventory_sha256'] = digest(out/'template_inventory.json')
    write_json(out/'manifest.json', result)
    write_json(out/'generation_validation.json', dict(state='self_checks_passed_not_independent_verification',
        publication_version=PUBLICATION_VERSION, old_canonical_preserved=237,
        old_assets_bytewise_preserved=len(copied), inherited_manual_sources=len(inherited_manual),
        newly_counted_manual_sources=len(manual), all_manual_sources=len(all_sources),
        independent_verification_performed=False, published=False, live_database_writes=0,
        configuration_writes=0, service_writes=0, protected_formal_files=len(protected)))
    (out/'GENERATION_HANDOFF.md').write_text(
        '# Publication 7 — pending independent verification\n\n'
        f'{len(combined)} distinct frames = unchanged parent 237 + requested saved-human {len(requested_manual)} '
        f'+ authorized existing-human reuse {len(reused_manual)} + new strict-machine {len(new)}. '
        f'Goal: 100 each / 500 total. Missing: {result["missing"]}.\n\n'
        '`new_proposals.jsonl` contains ONLY this generation’s selected machine drafts; '
        f'`manual_proposals.jsonl` and `manual_source_records.jsonl` contain {len(manual)} saved-human additions, '
        f'partitioned as {len(requested_manual)} completed Class 10 requests and {len(reused_manual)} existing labels '
        'with `manual_source_kind=existing_saved_human_reuse_for_100_goal`. '
        '`requests.jsonl` remains the exact original 13-item queue; reuse has no fabricated requests. '
        'The 30 inherited manual proposals/assets remain canonical; their latest source audit is separate. '
        f'`all_manual_sources.jsonl` freezes all {len(all_sources)} explicit manual sources against the new pose snapshot, '
        'including exact current/event payload bytes; inherited revisions, if changed, are never silently overwritten. '
        'Other newly saved original pose polygons are seed-only, not implicit human quota additions.\n\n'
        'Latest gallery edits are validated against publications 3/4/5/6 and win duplicate seed keys. '
        'Historical pending machine candidates are not seeds. `quota100_generation` holds only new rendering '
        'templates; all four historical template namespaces and 237 proposal assets remain byte-identical. '
        'Parent inventory, canonical objects, manifest and independent seal are pinned; parent metadata is '
        'archived under `parent_evidence`, not presented as a new verification.\n\n'
        'Full original attempt_target scan, unchanged .55 initial similarity / .84 ECC / .78 NCC / '
        '1.5 forward-backward gate and original beam guard; select_diverse goal explicitly 100. '
        'No relaxed thresholds, recursive propagation, forced fill, DB/config/service writes, new human edits, '
        'automatic confirmation, training eligibility or robot eligibility.\n')
    write_json(out/'artifact_hashes.json', file_inventory(out))
    return result


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--output', type=Path, default=OUTPUT)
    parser.add_argument('--workers', type=int, default=4)
    parser.add_argument('--method', choices=('original',), default='original')
    args = parser.parse_args()
    cv2.setNumThreads(1)
    print(json.dumps(generate(args.output, args.workers, args.method)), flush=True)


if __name__ == '__main__':
    main()
