"""Publication 14 draft: real gallery saves, protected frames, frozen strict gates.

Writes only a new output directory. Does not publish, issue tasks, update the UI,
write production stores, or mistake assistant drafts for human annotation events.
"""
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 . import mask_remaining_round3 as previous_generation
from . import mask_saved_preannotations as saved
from .mask_template_continue import attempt_target, sha
from .mask_template_refined import readonly, read_jsonl, snapshot_database, verify_history_preserved, write_jsonl
from .mask_template_transfer import PROJECT, annotation_inventory, write_json
from .cluster_targets import digest, protected_snapshot
from .mask_quota_saved_extension import file_inventory, indexed, load_formal_evidence
from .pose_review_geometry import canonical

VERSION = 'remaining_saved_gallery_round4_v1'
PUBLICATION_VERSION = 14
GENERATION_NAMESPACE = 'remaining_round4_generation'
PARENT = previous_generation.OUTPUT
REQUESTS = PROJECT/'outputs/pallet_mask_preannotated_requests_v1'
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_round4_v1'
CLASSES, ALL_CLASSES = previous_generation.CLASSES, previous_generation.ALL_CLASSES
MANUAL_SCHEMA, FALSE_FLAGS = saved.SCHEMA, saved.FALSE_FLAGS
USER_STATEMENT = '完成复核和修正，继续'
HISTORICAL_NAMESPACES = previous_generation.HISTORICAL_NAMESPACES + (previous_generation.GENERATION_NAMESPACE,)
PENDING_ORIGINAL_RECTANGLES = previous_generation.PENDING_ORIGINAL_RECTANGLES
read_json = previous_generation.read_json
original, prior = previous_generation.original, previous_generation.prior
remaining, expand = previous_generation.remaining, previous_generation.expand
PARENT_PINS = {
    'manifest.json': 'f2f985337f5efb17543e1f2df5588196fb6c597fc9f6167be52a14e68381e8d1',
    'verification.json': '7d234a89eb44c1b799d9dea7594faa404f98c131d0bc424f23ca15a3f5893388',
    'proposals.jsonl': '87fbde4aa3ddfdfd4b608364673c7a5d4d392d57f21312743852c650515d846a',
    'artifact_hashes.json': 'df2dda6cf0ee2bee5f42ff60439026f000faef73807055579fc4c360b60e4d85',
    'all_manual_sources.jsonl': '2ae591b0996208e5378a78aa760efecdc5bc92eeae9930e2c924d6d338511360',
    'new_proposals.jsonl': 'cc1929719743fe5741b1402d0e77b90a60556d396020674658910726b5834818',
    'reviewed_original_rectangle_source_records.jsonl': 'e0370bfb8ccd384e14078433d3920ee2e17aa054d39e012cf72905c0c4d2867f',
    'pending_original_rectangle_saves.jsonl': '9369c59877a315f8adc172f680f0722794bdfb978969501c7977d9e26bd7907e',
    'review_statement_bindings.json': '0aeb909c1c21711a6d8af5660b1a7f3dbfe6c96b1564963607bc05440656aa29',
}
REQUEST_PINS = {
    'manifest.json': 'ce4052c2c9cd739894b55b98b5c1c12e156191f2abf78bc92e2fcdc34cfa9cb7',
    'verification.json': '8403c2f92a2bd73da4f874278d83af7dc4a2380b0e29a0af83e9481ed8e8e054',
    'proposals.jsonl': '1d2d4856ad4602d21e4bbd98f3e8dca6e372cd9c5a1d1d33fb5d511bd7e95a28',
    'requests.jsonl': 'fe7b568365cef9bf12db8ee19d93c3aee9cb8ce02a6fe494cbc3c1f2aff83adc',
    'input_manifest.json': 'cab794461d9950415f4b9758846ab284c2b420252c08e2e9bcba135a0f99875a',
    'internal/build_decisions.json': '014c637a29bac543074b3cda6d1e58cec78caac23dd5747ca6ff797da7bca49c',
    'internal/deferred_sources.json': '767c201cabb56e822f39b8decc6a89478e48e0e994334426f2fd4db4483e7c24',
}


def verified_inputs(parent, requests):
    parent_hashes, request_hashes = file_inventory(parent), file_inventory(requests)
    for hashes, pins in ((parent_hashes, PARENT_PINS), (request_hashes, REQUEST_PINS)):
        if any(hashes.get(n) != h for n, h in pins.items()):
            raise ValueError('Pinned independently verified input changed')
    manifest, seal = read_json(parent/'manifest.json'), read_json(parent/'verification.json')
    if (manifest.get('publication_version') != 13 or manifest.get('proposals') != 1478 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 13 or
            seal.get('manifest_sha256') != parent_hashes['manifest.json'] or
            seal.get('proposals_sha256') != parent_hashes['proposals.jsonl'] or
            seal.get('artifact_hashes_sha256') != parent_hashes['artifact_hashes.json']):
        raise ValueError('Matching independently verified publication 13 required')
    for n, h in read_json(parent/'artifact_hashes.json').items():
        if parent_hashes.get(n) != h:
            raise ValueError('Frozen parent artifact changed: '+n)
    for n, h in manifest['frozen_code_sha256'].items():
        if digest(PROJECT/n) != h:
            raise ValueError('Frozen original engine changed: '+n)
    task_manifest, seal = read_json(requests/'manifest.json'), read_json(requests/'verification.json')
    if (seal.get('state') != 'passed' or seal.get('request_count') != 37 or
            seal.get('manifest_sha256') != request_hashes['manifest.json'] or
            seal.get('proposals_sha256') != request_hashes['proposals.jsonl'] or
            seal.get('parent_verification_sha256') != parent_hashes['verification.json'] or
            task_manifest['parent_manifest_sha256'] != parent_hashes['manifest.json'] or
            task_manifest['parent_proposals_sha256'] != parent_hashes['proposals.jsonl']):
        raise ValueError('Matching independent preannotation task seal required')
    for assets in (seal['public_assets'], task_manifest['public_assets']):
        if any(request_hashes.get(n) != h for n, h in assets.items()):
            raise ValueError('Verified task resource changed')
    if request_hashes['independent_verification.json'] != request_hashes['verification.json']:
        raise ValueError('Task independent seal differs')
    return manifest, parent_hashes, request_hashes


def user_instruction(requests, parent_new):
    return dict(schema='continuation_user_instruction_v1', date='2026-10-25', statement_zh=USER_STATEMENT,
        observed_parent_candidate_keys=sorted(indexed(parent_new)), observed_current_request_keys=sorted(indexed(requests)),
        actual_parent_new_candidate_count=len(parent_new), actual_current_request_count=len(requests),
        counts_authority='real frozen gallery current and raw human events; not assistant drafts or conversational counts',
        future_new_manual_tasks_require_assistant_preannotation=True,
        current_original_rectangle_no_change_authorized=False,
        historical_original_rectangle_statement_bindings_unchanged=True,
        creates_human_event=False, confirms_ground_truth=False, authorizes_propagation=False)


def historical_review_bindings(parent, records):
    result = []
    for group in read_json(parent/'review_statement_bindings.json')['historical_groups']:
        path = parent/group['path']
        statement = read_json(path)
        if digest(path) != group['file_sha256'] or sha(statement) != group['statement_sha256']:
            raise ValueError('Historical original-box statement bytes/binding changed')
        keys = set(group['target_keys'])
        if any(r['user_statement_sha256'] != sha(statement) for r in records if r['target_key'] in keys):
            raise ValueError('Historical original-box source rebound')
        result.append(dict(group, path='parent_evidence/'+group['path'], statement=statement))
    if set().union(*(set(g['target_keys']) for g in result)) != set(indexed(records)):
        raise ValueError('Historical original-box statement coverage mismatch')
    return result


def historical_task_inventory(rows, task_directory):
    """Reserve whole frames of previous issued tasks and inspected draft rejections."""
    files = sorted(p/'requests.jsonl' for p in (PROJECT/'outputs').glob('pallet_mask*')
        if p.is_dir() and (p/'requests.jsonl').is_file() and p != OUTPUT)
    files += [task_directory/'internal/shortlist.jsonl', task_directory/'internal/deferred_sources.json',
        task_directory/'internal/build_decisions.json', task_directory/'internal/assistant_decisions.json']
    keys, hashes = set(), {}
    def visit(value):
        if isinstance(value, dict):
            if isinstance(value.get('target_key'), str) and value['target_key'] in rows:
                keys.add(value['target_key'])
            for k, v in value.items():
                if k in rows:
                    keys.add(k)
                visit(v)
        elif isinstance(value, list):
            for v in value:
                visit(v)
    for path in files:
        hashes[str(path.relative_to(PROJECT))] = digest(path)
        visit(read_jsonl(path) if path.suffix == '.jsonl' else read_json(path))
    return keys, hashes


def target_inventory(rows, locked, status, pose_protected, gallery_keys, quarantine, reserved_frames=()):
    work, outcomes = previous_generation.target_inventory(rows, locked, status, pose_protected,
        gallery_keys, quarantine, ())
    active = []
    protected_frames = {rows[k]['frame_id'] for k in set(pose_protected) | set(gallery_keys) | set(quarantine)}
    protected_frames.update(reserved_frames)
    for key, row in work:
        if row['frame_id'] in protected_frames:
            outcomes.append(dict(target_key=key, class_id=row['final_class_id'],
                reason='whole_frame_history_or_task_reserved', registration_attempts=0))
        else:
            active.append((key, row))
    return active, outcomes


def generation_templates(candidates, counted_keys, gallery, formal_sha, classification_sha):
    templates = previous_generation.generation_templates(candidates, counted_keys, gallery, formal_sha, classification_sha)
    return {k: dict(t, rendering_id=GENERATION_NAMESPACE+':'+k) for k, t in templates.items()}


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, machine_draft=True, human_edited=False, machine_transfer_performed=True,
        classification_is_candidate_not_truth=True, target_per_class=100,
        rendering_id=GENERATION_NAMESPACE+':'+accepted['template_key'],
        continuation=dict(p['continuation'], batch=PUBLICATION_VERSION, all_compatible_templates_considered=True))


def class_counts(previous, manual, new, reviews, sources, reviewed, dispositions,
                 templates, rows, work, outcomes, capacity):
    counts = previous_generation.class_counts(previous, manual, new, reviews, sources, reviewed,
        dispositions, templates, rows, work, outcomes, capacity)
    for cls, values in counts.items():
        gallery_manual = sum(p['class_id'] == cls for p in manual)
        values.update(manual_modified_added=gallery_manual, saved_gallery_manual_added=gallery_manual,
            manual_four_point_sources=values['manual_four_point_sources']+gallery_manual,
            manual_pose_added=0, assistant_drafts_counted_before_human_save=0)
    return counts


def saved_status(proposals, reviews, sources, snapshot_sha):
    audit = []
    for proposal in proposals:
        key = proposal['target_key']
        review, record = reviews[key], sources[key]
        audit.append(dict(target_key=key, class_id=proposal['class_id'], actual_saved=True,
            polygon_changed_from_proposal=review['polygon'] != proposal['polygon'],
            saved_review_unchanged=review['polygon'] == proposal['polygon'],
            human_new_edit_claim=False, new_human_edit_created=False, proposal_sha256=sha(proposal),
            proposal_polygon_sha256=sha(proposal['polygon']), current_revision=review['revision'],
            current_polygon_sha256=sha(review['polygon']), current_payload_sha256=sha(review),
            current_payload_raw_sha256=record['current_payload_raw_sha256'], human_event_id=record['human_event_id'],
            human_event_sha256=record['human_event_sha256'], gallery_snapshot_sha256=snapshot_sha,
            source_binding=review['source_binding'], proposal_publication=review['proposal_publication'],
            binding=review['binding'], base_proposal_id=review['base_proposal_id']))
    unchanged = sorted(r['target_key'] for r in audit if r['saved_review_unchanged'])
    return dict(parent_new_machine_candidates=len(proposals), actually_saved=len(audit), pending=0,
        polygon_changed_from_proposal=sum(r['polygon_changed_from_proposal'] for r in audit),
        saved_unchanged=len(unchanged), saved_keys=sorted(indexed(proposals)), unchanged_keys=unchanged, pending_keys=[],
        saved_evidence_authority='real gallery current/raw latest event pairs with every historical binding checked',
        unchanged_save_is_real_review_not_new_human_modification=True, new_human_edits_created=0), audit


def preserve_assets(parent, out, previous, task_dir):
    hashes = {}
    for source, destination in ((parent, 'parent_evidence'), (task_dir, 'preannotation_source_evidence')):
        shutil.copytree(source, out/destination)
        hashes.update({destination+'/'+n: h for n, h in file_inventory(source).items()})
    for proposal in previous:
        for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            n = f'{directory}/{proposal["target_key"]}.{extension}'
            shutil.copyfile(parent/n, out/n)
            hashes[n] = digest(parent/n)
    for namespace in HISTORICAL_NAMESPACES:
        shutil.copytree(parent/namespace, out/namespace)
        hashes.update({namespace+'/'+n: h for n, h in file_inventory(parent/namespace).items()})
    for n in ('vertex2_before_after.png', 'FONT_LICENSE.txt', 'review_dispositions.jsonl',
            'review_disposition_statement.json', 'reviewed_original_rectangle_source_records.jsonl',
            'review_completion_statement.json', 'new_review_completion_statement.json',
            'pending_original_rectangle_saves.jsonl', 'all_manual_sources.jsonl'):
        shutil.copyfile(parent/n, out/n)
        hashes[n] = digest(parent/n)
    if any(digest(out/n) != h for n, h in hashes.items()):
        raise ValueError('Historical asset or private evidence bytes changed')
    return hashes


def rendering_inventory(parent_inventory, templates, manual, combined, new_keys):
    inventory = previous_generation.rendering_inventory(parent_inventory, templates, manual, combined, new_keys)
    inventory.update(schema=VERSION, publication_version=PUBLICATION_VERSION,
        generation_template_namespace=GENERATION_NAMESPACE)
    return inventory


def build_report(out, combined, counts, new_keys, manual_keys, status, task_status, templates, locked_count, reserved_count):
    total = lambda field: sum(v[field] for v in counts.values())
    fields = ('old_total', 'manual_modified_added', 'reviewed_original_rectangle', 'saved_human', 'new_candidates', 'total', 'missing')
    table = ''.join(f'<tr><td>{cls}</td>'+''.join(f'<td>{counts[cls][f]}</td>' for f in fields)+'</tr>' for cls in ALL_CLASSES)
    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=14')}*{box-sizing:border-box}body{background:#101720;color:#e7edf5;font:16px PreviewSans,sans-serif;margin:auto;padding:20px;max-width:1090px}a{color:#77ceff}img{display:block;width:100%;height:auto}article{border:1px solid #425269;margin:20px 0;padding:12px;min-width:0}p,h2{overflow-wrap:anywhere}.table-wrap{max-width:100%;overflow-x:auto}td,th{padding:9px;border:1px solid #425269;white-space:nowrap}table{border-collapse:collapse;min-width:1090px}select{font:inherit;padding:6px}[hidden]{display:none!important}</style>
<h1>第十四版草稿 · 待独立核验，尚未发布</h1>'''
    page += (f'<h2>本轮新增严格机器候选 {len(new_keys)} 份，均待人工复核</h2>'
        f'<p>第13版{status["actually_saved"]}份机器候选均有真实图册保存：{status["polygon_changed_from_proposal"]}份轮廓变化、{status["saved_unchanged"]}份保存保持原候选。'
        f'独立预标注任务{task_status["actually_saved"]}份均有真实图册人工保存：{task_status["polygon_changed_from_proposal"]}份相对助手草稿改变、{task_status["saved_unchanged"]}份保存保持草稿。'
        '后者仍有真实人工保存事件；不把助手预标注本身当成人工改绘，也不声称本轮创造人工事件。</p>'
        f'<p>人工保存轮廓 {total("saved_human")} = 位姿来源 {total("saved_pose_human")} + 图册来源 {total("saved_gallery_human")}；'
        f'旧原框复核另列 {total("reviewed_original_rectangle")}，数据库已存复核覆盖 {total("db_stored_human_review")}。'
        f'全22类累计 {total("total")}/2200 个全局不同帧，缺 {total("missing")}；覆盖不是真值率或人工完成率。</p>'
        f'<p>{locked_count}个已计入帧全部锁定；另有{reserved_count}个历史或任务保留原图。旧两份原框未获专项复核授权，不计覆盖、不作模板；旧五份原框保持原声明绑定，旧45份对话侧记和42份未保存候选不变。'
        '所有历史改绘、返回原框、删除、暂缓、确认、隔离、已派发任务与助手拒绝草稿均按整帧保护。</p>'
        f'<p>本轮传播模板 {len(templates)} 份，均为真实保存的多于四点轮廓；新增四点图册来源不是模板，不补造顶点，不递归传播。'
        f'类别19按当前保护及全局最大匹配重新计算原图容量 {counts[19]["capacity_matching_distinct_frames"]}，至少还需新增不同原图 {100-counts[19]["capacity_matching_distinct_frames"]} 份才可能达到100。</p>'
        '<p>仅冻结原始attempt_target：同类、同划分、同传感器及兼容已知视角；初始相关度至少0.55、ECC至少0.84、边界相关度至少0.78、逐顶点往返误差不超过1.5并通过横梁边界检查。保持顶点数和顺序，不放宽门槛，不强行填满。已满100的类别不再扫描。</p>'
        f'<p>用户原话：{html.escape(USER_STATEMENT)}。37份新人工来源为图册保存，不是位姿保存。原生图像、真实原始事件、独立任务完整证据和父级完整证据链均冻结；旧规范记录和资产逐字节保留。'
        '不写生产库、配置、服务、正式数据、单位或训练资源；不自动确认为GT，不生成新请求或ZIP。下一批预标注任务另行准备，不计入本生成器覆盖。</p>')
    page += '<div class="table-wrap"><table><tr><th>类别</th><th>旧覆盖</th><th>新保存图册来源</th><th>旧原框复核</th><th>人工保存轮廓</th><th>新增机器</th><th>累计不同帧</th><th>缺额</th></tr>'+table+'</table></div>'
    page += '<p>筛选类别 <select id="filter"><option value="">全部22类</option>'+''.join(f'<option value="{c}">{c}</option>' for c in ALL_CLASSES)+'</select> <label><input type="checkbox" id="only-new" checked>只看本轮新增机器候选</label></p>'
    for proposal in sorted(combined, key=lambda p: (p['class_id'], p['target_key'])):
        key = proposal['target_key']
        label = ('新增严格机器候选，待复核，不是人工标签' if key in new_keys else
            '真实保存图册人工四点轮廓，不是位姿或模板' if key in manual_keys else '历史卡片原样保留，最新保存另行冻结')
        hidden = '' if key in new_keys else ' hidden'
        page += (f'<article id="target-{key}" data-class="{proposal["class_id"]}" data-batch="{"new" if key in new_keys else "previous"}"{hidden}>'
            f'<h2>类别 {proposal["class_id"]} · {label}</h2><img loading="lazy" src="cases/{key}.png?revision=14" alt="来源对照">'
            f'<p>{html.escape(proposal["source"]["frame_id"])} ／ {key}</p>'
            f'<a href="labelme/{key}.json?revision=14">LabelMe</a> · <a href="masks/{key}.png?revision=14">掩膜</a></article>')
    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 missing Chinese glyph')
        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 subset missing Chinese glyph')
    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=OUTPUT, workers=4, parent=PARENT, requests_directory=REQUESTS):
    out, parent, task_dir = (Path(p).resolve() for p in (output, parent, requests_directory))
    if out.exists() or not out.is_relative_to(PROJECT/'outputs'):
        raise ValueError('Use a new outputs directory; never overwrite frozen publications')
    if not 1 <= workers <= 8:
        raise ValueError('Workers must be 1–8')
    manifest, parent_hashes, task_hashes = verified_inputs(parent, task_dir)
    config_hashes = file_inventory(PROJECT/'configs')
    frozen_code = dict(manifest['frozen_code_sha256'])
    for name in ('pallet/mask_remaining_round4.py', 'pallet/mask_saved_preannotations.py', 'pallet/mask_preannotation.py'):
        frozen_code[name] = digest(PROJECT/name)
    formal, rows, evidence = load_formal_evidence(manifest)
    formal_sha, classification_sha = manifest['formal_manifest_sha256'], manifest['classification_sha256']
    previous, parent_new, old_sources, requests, drafts = (read_jsonl(p) for p in (
        parent/'proposals.jsonl', parent/'new_proposals.jsonl', parent/'all_manual_sources.jsonl',
        task_dir/'requests.jsonl', task_dir/'proposals.jsonl'))
    old_pose_keys, parent_new_keys, requested = (set(indexed(v)) for v in (old_sources, parent_new, requests))
    review_sources = read_jsonl(parent/'reviewed_original_rectangle_source_records.jsonl')
    bindings = historical_review_bindings(parent, review_sources)
    pending = indexed(read_jsonl(parent/'pending_original_rectangle_saves.jsonl'))
    task_keys, task_files = historical_task_inventory(rows, task_dir)
    if (len(rows) != 12344 or len(previous) != 1478 or len(parent_new) != 29 or len(old_pose_keys) != 482 or
            len(review_sources) != 5 or set(pending) != set(PENDING_ORIGINAL_RECTANGLES) or
            len({p['source']['frame_id'] for p in previous}) != len(previous) or
            any(p['source'] != rows[p['target_key']] for p in previous) or
            Counter(r['class_id'] for r in requests) != {4: 10, 5: 7, 7: 10, 14: 10}):
        raise ValueError('Formal / parent1478 / pose482 / task37 / pending2 inventory changed')
    protected = protected_snapshot(formal)
    if len(protected) != 20768 or protected != read_json(parent/'protected_formal_after.json'):
        raise ValueError('Protected formal 20768-file inventory changed')
    out.mkdir(parents=True)
    for name, value in (('parent_file_inventory', parent_hashes), ('request_file_inventory', task_hashes),
            ('configuration_input_hashes', config_hashes), ('protected_formal_before', protected),
            ('historical_task_input_hashes', task_files), ('current_user_instruction', user_instruction(requests, parent_new))):
        write_json(out/(name+'.json'), value)
    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 = snapshot_database(pose_live, pose_snapshot)
    gallery_sha = snapshot_database(gallery_live, gallery_snapshot)
    delta = dict(pose=previous_generation.validate_source_delta(parent/'reviews_snapshot.sqlite3', pose_snapshot, 'annotations', ()),
        gallery=previous_generation.validate_source_delta(parent/'gallery_reviews_snapshot.sqlite3', gallery_snapshot,
            'reviews', parent_new_keys | requested))
    if delta['pose']['new_events'] != 0 or delta['gallery']['new_events'] != 71 or len(delta['gallery']['current_changed_keys']) != 66:
        raise ValueError('Actual unchanged pose / new gallery66 keys71 events contract changed')
    write_json(out/'source_delta_audit.json', delta)
    candidates, source_audit = {}, []
    with closing(readonly(pose_snapshot, True)) as db:
        pose_metadata = dict(db.execute('SELECT key,value FROM metadata'))
        binding = json.loads(pose_metadata['binding'])
        if (binding['formal_manifest_sha256'] != formal_sha or
                binding['formal_annotations_sha256'] != digest(formal/'annotations/frames.jsonl') or
                binding['formal_frames_sha256'] != digest(formal/'frames.jsonl')):
            raise ValueError('Pose formal binding changed')
        current, pose_history_audit = previous_generation.audit_pose_snapshot(db, rows, sha(binding))
        _, _, eligibility = annotation_inventory(db, rows)
        pose_protected = expand.protected_history(db, rows, current)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        pose_event_keys = {r[0] for r in db.execute('SELECT DISTINCT target_key FROM events')}
        for key, old_record in sorted(indexed(old_sources).items()):
            candidates[key] = remaining.validated_manual_source(db, key, rows[key], pose_sha, sha(binding))
            value, _, record = candidates[key]
            if (record['current_payload_raw'] != old_record['current_payload_raw'] or
                    db.execute('SELECT revision,payload FROM events WHERE target_key=? ORDER BY id DESC LIMIT 1', (key,)).fetchone()
                    != (value['revision'], record['current_payload_raw'])):
                raise ValueError('Inherited pose source differs from exact raw latest saved event')
            source_audit.append(dict(target_key=key, class_id=rows[key]['final_class_id'], current_revision=value['revision'],
                current_payload_raw_sha256=record['current_payload_raw_sha256'], exact_current_and_event_validated=True,
                modified_human_contour=True, vertex_count=len(value['polygon']), source_store='pose'))
        old_requests = indexed(read_jsonl(parent/'requests.jsonl'))
        for key, old_record in pending.items():
            fresh = previous_generation.pending_original_rectangle_save(db, key, rows[key], pose_sha, sha(binding), old_requests[key])
            if (fresh['current_payload_raw'] != old_record['current_payload_raw'] or
                    fresh['saved_event_payload_raw'] != old_record['saved_event_payload_raw'] or
                    (rows[key]['final_class_id'], fresh['current_revision']) != PENDING_ORIGINAL_RECTANGLES[key]):
                raise ValueError('Pending original rectangle current or raw historical save changed')
        for record in review_sources:
            key = record['target_key']
            group = next(g for g in bindings if key in g['target_keys'])
            statement = group['statement']
            _, _, fresh = previous_generation.previous_generation.exact_original_rectangle_review(db, key, rows[key],
                pose_sha, sha(binding), statement, expected_statement=statement['statement_zh'])
            if any(fresh[f] != record[f] for f in ('current_payload_raw', 'saved_review_event_payload_raw', 'saved_review_event_id')):
                raise ValueError('Historical original-box current/event evidence changed')
    paths = dict(expand.complete.SOURCE_PUBLICATIONS)
    paths.update({9: remaining.PARENT, 10: expand.OUTPUT, 11: previous_generation.previous_generation.PARENT,
        12: previous_generation.PARENT, 13: parent})
    publications = original.publication_inventory(paths, formal_sha)
    publication_hashes = {v: file_inventory(p) for v, p in paths.items() if p != parent}
    registry = saved.prelabel_registry(task_dir, rows, formal_sha, publications[13], current, pose_event_keys)
    with closing(readonly(gallery_snapshot, True)) as db:
        gallery_metadata = dict(db.execute('SELECT key,value FROM metadata'))
        if gallery_metadata.get('formal_manifest_sha256') != formal_sha:
            raise ValueError('Gallery formal binding changed')
        reviews, events, gallery_sources, gallery_templates, gallery_event_audit = saved.gallery_source_records(
            db, rows, previous, parent, formal_sha, gallery_sha, paths, registry)
    gallery_keys = set(reviews) | {v['target_key'] for v in events}
    gallery_source_by_key, previous_by_key = indexed(gallery_sources), indexed(previous)
    for key, template in gallery_templates.items():
        record, old = gallery_source_by_key[key], current.get(key)
        if old and old.get('state') in ('deleted', 'deferred'):
            raise ValueError('Gallery seed has deleted/deferred pose')
        template.update(revision=old['revision'] if old else 0, current_payload_sha256=sha(old) if old else None,
            saved_human_review=True, new_human_edit_created=False,
            saved_review_unchanged=reviews[key]['polygon'] == previous_by_key[key]['polygon'],
            polygon_modified_from_proposal=reviews[key]['polygon'] != previous_by_key[key]['polygon'],
            current_gallery_payload_raw_sha256=record['current_payload_raw_sha256'],
            saved_review_event_id=record['human_event_id'], saved_review_event_raw_sha256=record['human_event_sha256'])
    quarantine = set(previous_generation.QUARANTINE) | set(manifest.get('preserved_quarantined_targets', []))
    forbidden_manual_frames = {rows[k]['frame_id'] for k in pose_protected | quarantine | set(pending)}
    manual = saved.completed_gallery_proposals(registry, reviews, gallery_source_by_key, rows, previous, current,
        pose_sha, gallery_sha, formal_sha, classification_sha, GENERATION_NAMESPACE, forbidden_manual_frames)
    locked = previous+manual
    reserved_keys = task_keys | pose_protected | gallery_keys | quarantine | set(pending)
    reserved_frames = {rows[k]['frame_id'] for k in reserved_keys} - {p['source']['frame_id'] for p in locked}
    remaining_keys = {k for k in candidates if rows[k]['final_class_id'] in CLASSES}
    templates = generation_templates(candidates, remaining_keys, gallery_templates, formal_sha, classification_sha)
    inherited_seeds = set(manifest['generation_template_keys'])
    if (len(locked) != 1515 or len({p['source']['frame_id'] for p in locked}) != len(locked) or
            len(gallery_templates) != 904 or len(templates) != 679 or
            Counter(t['origin'] for t in templates.values()) != {prior.POSE_ORIGIN: 45, original.GALLERY_ORIGIN: 634} or
            len(manual) != 37 or Counter(p['vertex_count'] for p in manual) != {4: 37} or
            len(current) != 4946 or pose_events != 11926 or len(reviews) != 941 or len(events) != 1023 or
            set(templates) != inherited_seeds | parent_new_keys or set(templates) & requested):
        raise ValueError('Actual locked1515 / pose482 / gallery941 / seed679 contract changed')
    status, status_audit = saved_status(parent_new, reviews, gallery_source_by_key, gallery_sha)
    task_status, task_audit = saved_status(drafts, reviews, gallery_source_by_key, gallery_sha)
    task_status['source_kind'] = 'independent_assistant_preannotation_followed_by_real_gallery_human_save'
    task_status['parent_new_machine_candidates'] = 0
    task_status['completed_preannotation_tasks'] = len(drafts)
    if ((status['polygon_changed_from_proposal'], status['saved_unchanged']) != (20, 9) or
            (task_status['polygon_changed_from_proposal'], task_status['saved_unchanged']) != (36, 1)):
        raise ValueError('Actual separate machine29 and task37 saved-change statistics changed')
    work, outcomes = target_inventory(rows, locked, eligibility, pose_protected, gallery_keys, quarantine, reserved_frames)
    capacity_candidates = [dict(target_key=k, class_id=r['final_class_id'], source=r, metrics={}) for k, r in work]
    potential, _, capacity_assignments, capacity_order = expand.select_locked_diverse(locked, capacity_candidates)
    capacity = Counter(p['class_id'] for p in locked+potential)
    for cls in ALL_CLASSES:
        capacity.setdefault(cls, 0)
    write_json(out/'protected_targets.json', dict(locked_keys=sorted(indexed(locked)),
        locked_frames=sorted(p['source']['frame_id'] for p in locked), quarantine=sorted(quarantine),
        reserved_pending_original_rectangle_keys=sorted(pending),
        reserved_pending_original_rectangle_frames=sorted(rows[k]['frame_id'] for k in pending),
        pending_original_rectangles_excluded_from_coverage=True, reserved_history_or_task_keys=sorted(reserved_keys),
        reserved_history_or_task_frames=sorted(reserved_frames), all_historical_task_keys=sorted(task_keys),
        all_current_task_keys_protected=sorted(requested), previous_machine_task_keys_protected=sorted(parent_new_keys),
        pose_mask_or_state_history_protected=sorted(pose_protected), gallery_saved_or_history=sorted(gallery_keys),
        eligibility=eligibility, counted_manual_source_keys=sorted(candidates),
        reviewed_original_rectangle_keys=sorted(indexed(review_sources)), conversation_dispositions_not_seed_authorization=True))
    write_json(out/'snapshot_manifest.json', dict(formal_manifest_sha256=formal_sha,
        pose=dict(path=pose_snapshot.name, sha256=pose_sha, saved=len(current), events=pose_events,
            metadata=pose_metadata, immutable=True, backup='backups/'+pose_snapshot.name),
        gallery=dict(path=gallery_snapshot.name, sha256=gallery_sha, saved=len(reviews), events=len(events),
            metadata=gallery_metadata, immutable=True, backup='backups/'+gallery_snapshot.name),
        historical_snapshots_retained_under=['parent_evidence', 'preannotation_source_evidence']))
    write_json(out/'review_statement_bindings.json', dict(
        historical_groups=[{k: v for k, v in g.items() if k != 'statement'} for g in bindings],
        no_current_original_rectangle_review_statement_created=True, pending_original_rectangle_keys=sorted(pending),
        current_instruction_path='current_user_instruction.json'))
    write_json(out/'source_inventory.json', dict(schema='canonical_publications_and_independent_prelabel_registry_v1',
        publications={v: {k: x for k, x in p.items() if k not in ('path', 'proposals')} for v, p in publications.items()},
        prelabels={k: dict(base_proposal_id=r['proposal_sha256'], task_manifest_sha256=r['task_manifest_sha256'],
            proposal_publication=r['proposal_publication'], main_gallery_binding=r['binding'],
            main_gallery_proposals_sha256=r['publication_proposals_sha256'], source=r['source'], source_binding=r['source_binding'],
            preannotation_provenance=r['preannotation_provenance']) for k, r in registry.items()},
        canonical_gallery_saved=len(reviews)-len(registry), independent_prelabel_gallery_saved=len(registry)))
    for name, value in (('saved_review_status', status), ('completed_preannotation_status', task_status)):
        write_json(out/(name+'.json'), value)
    for name, values in (('manual_proposals', manual), ('completed_request_manual_proposals', manual),
            ('manual_source_records', [gallery_source_by_key[k] for k in sorted(requested)]),
            ('human_source_audit', source_audit), ('pose_history_audit', pose_history_audit),
            ('remaining_current_manual_source_records', [candidates[k][2] for k in sorted(remaining_keys)]),
            ('gallery_source_records', gallery_sources), ('gallery_event_binding_audit', gallery_event_audit),
            ('saved_review_source_audit', status_audit), ('completed_preannotation_source_audit', task_audit),
            ('refined_annotations', [reviews[k] for k in sorted(reviews)]), ('capacity_frame_assignments', capacity_assignments)):
        write_jsonl(out/(name+'.jsonl'), values)
    shutil.copyfile(task_dir/'requests.jsonl', out/'requests.jsonl')
    for proposal, directory in [(p, parent) for p in previous] + [(p, task_dir) for p in drafts]:
        key, row = proposal['target_key'], proposal['source']
        with Image.open(directory/'images'/f'{key}.png') as image:
            if not np.array_equal(np.asarray(image), evidence.image(row['frame_id'])):
                raise ValueError('Inherited or task image differs from formal native pixels')
    print(json.dumps(dict(state='scanning', workers=workers, locked=len(locked), reserved_frames=len(reserved_frames),
        templates=len(templates), eligible_targets=len(work), saved_human_contours=len(old_sources)+len(reviews),
        class19_capacity=capacity[19], pose_saved=len(current), pose_events=pose_events,
        gallery_saved=len(reviews), gallery_events=len(events))), flush=True)
    attempts, accepted = [], []
    def worker(item):
        return item, attempt_target(*item, templates, evidence)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number, ((key, row), (candidate, trials, outcome)) in enumerate(pool.map(worker, work), 1):
            attempts.extend(trials)
            outcomes.append(dict(outcome, registration_attempts=len(trials)))
            if candidate:
                accepted.append(proposal_from_attempt(key, row, candidate, templates, current, pose_sha, formal_sha, classification_sha))
            if number % 200 == 0:
                print(json.dumps(dict(scanned=number, accepted=len(accepted), attempts=len(attempts))), flush=True)
    new, selections, assignments, scarcity_order = expand.select_locked_diverse(locked, accepted)
    for outcome in outcomes:
        if outcome['target_key'] in selections:
            outcome['reason'] = selections[outcome['target_key']]
    if len(outcomes) != len(rows) or set(indexed(outcomes)) != set(rows):
        raise ValueError('Every formal target requires exactly one outcome')
    combined = sorted(locked+new, key=lambda p: p['target_key'])
    restored = indexed(combined)
    if (any(canonical(restored[p['target_key']]) != canonical(p) for p in previous) or
            reserved_frames & {p['source']['frame_id'] for p in combined}):
        raise ValueError('Inherited canonical proposal changed or reserved frame counted')
    dispositions = read_jsonl(parent/'review_dispositions.jsonl')
    counts = class_counts(previous, manual, new, reviews, old_sources, review_sources, dispositions,
        templates, rows, work, outcomes, capacity)
    total = lambda f: sum(v[f] for v in counts.values())
    if (total('saved_human') != len(old_sources)+len(reviews) or total('db_stored_human_review') != len(old_sources)+len(reviews)+len(review_sources) or
            total('user_reviewed_no_change') != 45 or total('no_new_server_review') != 42+len(new)):
        raise ValueError('Actual saved human / old review5 / old45 / old pending42 coverage changed')
    for name, values in (('proposals', combined), ('new_proposals', new), ('registration_attempts', attempts),
            ('accepted_candidates', accepted), ('outcomes', sorted(outcomes, key=lambda v: v['target_key'])),
            ('frame_assignments', [dict(frame_id=p['source']['frame_id'], class_id=p['class_id'], target_key=p['target_key'],
                locked=True, phase='previously_counted_source') for p in locked]+assignments)):
        write_jsonl(out/(name+'.jsonl'), values)
    for directory in ('cases', 'images', 'masks', 'labelme'):
        (out/directory).mkdir()
    copied = preserve_assets(parent, out, previous, task_dir)
    saved.package_manual_preview(out, manual, evidence)
    previous_generation.package_new_preview(out, new, templates, evidence)
    (out/GENERATION_NAMESPACE/'templates').mkdir(parents=True)
    for key, template in templates.items():
        write_json(out/GENERATION_NAMESPACE/'templates'/f'{key}.json', template)
    inventory = rendering_inventory(read_json(parent/'template_inventory.json'), templates, manual, combined, set(indexed(new)))
    inventory.update(classes=counts, previous_proposals=len(previous), manual_proposals=len(manual),
        modified_manual_proposals=len(manual), reviewed_original_rectangle=len(review_sources), pending_original_rectangle=len(pending),
        all_manual_sources=len(old_sources), snapshot_saved=len(current), snapshot_events=pose_events,
        gallery_snapshot_saved=len(reviews), gallery_snapshot_events=len(events), eligibility=eligibility, target_per_class=100,
        counted_manual_source_keys=sorted(candidates), saved_gallery_manual_source_keys=sorted(requested))
    write_json(out/'template_inventory.json', inventory)
    write_json(out/'preserved_parent_assets.json', copied)
    build_report(out, combined, counts, set(indexed(new)), set(indexed(manual)), status, task_status, templates,
        len(locked), len(reserved_frames))
    contact = Image.new('RGB', (1480, 305*len(ALL_CLASSES)), '#121923')
    for i, cls in enumerate(ALL_CLASSES):
        proposal = next(p for p in new+manual+previous if p['class_id'] == cls)
        with Image.open(out/'cases'/f'{proposal["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, image_hash in evidence.used.items():
        if name in used and used[name] != image_hash:
            raise ValueError('Formal native image hash conflict')
        used[name] = image_hash
    if any(digest(formal/name) != h for name, h 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')
    for live, frozen, table, metadata in ((pose_live, pose_snapshot, 'annotations', pose_metadata),
            (gallery_live, gallery_snapshot, 'reviews', gallery_metadata)):
        verify_history_preserved(live, frozen)
        prior.verify_sources_current(live, frozen, table)
        with closing(readonly(live)) as db:
            if dict(db.execute('SELECT key,value FROM metadata')) != metadata:
                raise ValueError('Live metadata changed during generation')
    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)) or
            file_inventory(parent) != parent_hashes or file_inventory(task_dir) != task_hashes or
            file_inventory(PROJECT/'configs') != config_hashes or
            any(file_inventory(paths[v]) != hashes for v, hashes in publication_hashes.items()) or
            any(digest(PROJECT/name) != h for name, h in frozen_code.items()) or
            any(digest(PROJECT/name) != h for name, h in task_files.items()) or
            any(digest(out/name) != h for name, h in copied.items())):
        raise ValueError('Frozen input/evidence/code/configuration/resource changed')
    for name in ('manual_source_records', 'all_manual_sources', 'pending_original_rectangle_saves',
            'remaining_current_manual_source_records', 'gallery_source_records', 'reviewed_original_rectangle_source_records', 'requests'):
        (out/(name+'.jsonl')).chmod(0o600)
    write_json(out/'source_stability_check.json', dict(state='stable', source_current_stable=True,
        current_payloads_and_events_unchanged=True, metadata_unchanged=True, production_database_writes=0))
    write_json(out/'source_publication_file_inventories.json', publication_hashes)
    write_json(out/'shortfalls.json', {c: dict(missing=counts[c]['missing'], capacity=capacity[c],
        capacity_shortfall=100-capacity[c], templates=counts[c]['templates'], outcomes=counts[c]['outcomes'],
        additional_new_distinct_images_required_at_least=100-capacity[c], manual_template_required=counts[c]['templates'] == 0,
        explanation='Frozen strict gates; whole-frame history/task reservations; no quad seeds or forced fill.') for c in CLASSES})
    result = dict(state='remaining_round4_pending_independent_verification', schema=VERSION, version=VERSION,
        publication_version=PUBLICATION_VERSION, published=False, independent_verification_performed=False,
        parent=str(parent), parent_manifest_sha256=parent_hashes['manifest.json'],
        parent_proposals_sha256=parent_hashes['proposals.jsonl'], parent_verification_sha256=parent_hashes['verification.json'],
        preannotation_manifest_sha256=task_hashes['manifest.json'], preannotation_verification_sha256=task_hashes['verification.json'],
        previous_proposals=len(previous), historical_canonical_preserved=len(previous), base_proposals=len(locked),
        proposals=len(combined), new_proposals=len(new), new_machine_pending=len(new),
        manual_proposals=len(manual), completed_request_manual_proposals=len(manual), modified_manual_proposals=len(manual),
        actual_saved_current_requests=task_status['actually_saved'], manual_by_class=dict(Counter(p['class_id'] for p in manual)),
        new_by_class=dict(Counter(p['class_id'] for p in new)), manual_source_schema=MANUAL_SCHEMA,
        saved_gallery_manual_source_schema=MANUAL_SCHEMA, saved_gallery_manual_source_keys=sorted(requested),
        all_manual_sources=len(old_sources), inherited_manual_sources=len(old_sources), modified_pose_source_records=len(old_sources),
        manual_four_point_sources=len(manual), manual_more_than_four_sources=0,
        all_pose_four_point_sources=sum(r['manual_four_point_source'] for r in old_sources),
        reviewed_original_rectangle=len(review_sources), new_reviewed_original_rectangle=0,
        inherited_reviewed_original_rectangle=len(review_sources), reviewed_original_rectangle_schema=previous_generation.REVIEW_SCHEMA,
        reviewed_original_rectangle_keys=sorted(indexed(review_sources)), new_reviewed_original_rectangle_keys=[],
        pending_original_rectangle=len(pending), pending_original_rectangle_keys=sorted(pending),
        pending_original_rectangles_excluded_from_coverage=True,
        reserved_pending_original_rectangle_frames=sorted(rows[k]['frame_id'] for k in pending),
        reserved_history_or_task_frames=sorted(reserved_frames), counted_manual_source_keys=sorted(candidates),
        requested_manual_keys=sorted(requested), classes=counts, all_classes=list(ALL_CLASSES), remaining_classes=list(CLASSES),
        target_per_class=100, target_total=2200, remaining_target_total=1700, missing=total('missing'),
        saved_human=total('saved_human'), saved_human_contour_source=total('saved_human'),
        saved_pose_human=total('saved_pose_human'), saved_gallery_human=total('saved_gallery_human'),
        db_stored_human_review=total('db_stored_human_review'), pending_machine_raw=total('pending_machine_raw'),
        old_pending_machine=total('old_pending_machine'), user_reviewed_no_change=total('user_reviewed_no_change'),
        no_new_server_review=total('no_new_server_review'), saved_review_status=status, completed_preannotation_status=task_status,
        generation_templates=len(templates), generation_template_namespace=GENERATION_NAMESPACE,
        generation_template_keys=sorted(templates),
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        generation_templates_by_class=dict(Counter(t['row']['final_class_id'] for t in templates.values())),
        method='original', workers=workers, scanned_eligible_targets=len(work), registration_attempts=len(attempts),
        accepted_before_diversity=len(accepted), outcomes=dict(Counter(v['reason'] for v in outcomes)),
        scarcity_order=scarcity_order, capacity_matching_order=capacity_order, capacity_matching_total=sum(capacity.values()),
        class19_additional_new_distinct_images_required_at_least=100-capacity[19],
        formal_manifest_sha256=formal_sha, classification_sha256=classification_sha,
        original_classification_targets=len(rows), original_classification_classes=len(ALL_CLASSES),
        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(events), protected_formal_files=len(protected),
        gallery_all_raw_historical_events_source_binding_validated=len(gallery_event_audit),
        gallery_canonical_current_validated=len(reviews)-len(registry), gallery_prelabel_current_validated=len(registry),
        preserved_quarantined_targets=sorted(quarantine), original_splits_preserved=True, cross_split_template_transfers=0,
        recursive_candidate_propagation=False, thresholds_relaxed=False, forced_fill=False, duplicate_fill=False,
        research_candidates_added=0, assistant_prelabels_created=0, assistant_prelabels_counted_before_human_save=0,
        new_human_edits_created=0, human_events_created=0, human_requests_created=0, production_database_writes=0,
        configuration_writes=0, service_writes=0, automatic_human_confirmation=False,
        automatic_training_eligible=False, robot_command_eligible=False, ground_truth_eligible=False,
        classification_is_candidate_not_truth=True, source_current_stable=True, public_archive_created=False,
        inherited_resources_bytewise_preserved=True, manual_source_snapshots_imported_into_gallery_store=False,
        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'])
    for path in sorted(out.glob('*.jsonl')):
        result[path.stem+'_sha256'] = digest(path)
    for name in ('template_inventory', 'snapshot_manifest', 'font_coverage', 'saved_review_status',
            'completed_preannotation_status', 'current_user_instruction', 'review_statement_bindings', 'source_delta_audit', 'source_inventory'):
        result[name+'_sha256'] = digest(out/(name+'.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, published=False, independent_verification_performed=False,
        historical_canonical_preserved=len(previous), locked_before_machines=len(locked),
        reserved_uncounted_frames=len(reserved_frames), global_distinct_frames=len(combined),
        preserved_source_files=len(copied), protected_formal_files=len(protected), no_production_writes=True))
    (out/'GENERATION_HANDOFF.md').write_text(
        '# Publication 14 draft — pending independent verification\n\n'
        f'{len(combined)} globally distinct frames = parent {len(previous)} + real saved gallery contours {len(manual)} + strict machines {len(new)}.\n\n'
        f'All {len(reviews)} gallery currents and {len(events)} raw historical human events validated. '
        'Independent draft base hashes are NOT canonical publication13 proposal hashes; main gallery bindings remain publication13. '
        'source_inventory.json separates both registries. Complete original task pack in preannotation_source_evidence.\n\n'
        f'Prior machine saves: {status["polygon_changed_from_proposal"]} changed / {status["saved_unchanged"]} unchanged. '
        f'Prelabel task saves: {task_status["polygon_changed_from_proposal"]} changed / {task_status["saved_unchanged"]} unchanged. '
        'These are real gallery saves, not pose events. Saved gallery quads use saved_gallery_mask_entry_v1, '
        'human_source origin mask_gallery_human_revision, self-key gallery_review, and no template or fake pose provenance.\n\n'
        f'all_manual_sources.jsonl retains {len(old_sources)} POSE sources bytewise. '
        f'{len(templates)} direct >4 seeds = pose45 + gallery634; new gallery quads never seed. '
        'Two old original rectangles remain excluded/reserved; five old review-only bindings preserved exactly. '
        'Old45 conversation notes and42 pending candidates untouched. Whole history, prior request and rejected-draft frames protected.\n\n'
        f'{len(attempts)} original strict attempts; {len(accepted)} accepted before global maximum matching. '
        f'Class19 dynamic capacity {capacity[19]}, needs at least {100-capacity[19]} additional distinct images.\n\n'
        'No publication/seal/ZIP/task/UI/configuration/service/production-store writes. Independent verifier must replay attempts, '
        'maximum matching, actual saved raw evidence and asset hashes before publication.\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)
    args = parser.parse_args()
    cv2.setNumThreads(1)
    result = generate(args.output, args.workers)
    print(json.dumps({k: result[k] for k in ('state', 'proposals', 'new_proposals', 'new_by_class',
        'saved_human', 'reviewed_original_rectangle', 'pending_original_rectangle', 'missing',
        'registration_attempts', 'accepted_before_diversity', 'code_sha256')}, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
