"""Publication 15 draft: real saved gallery task v2, immutable history, strict replay.

Writes only a new output directory. Publication, independent verification, task
issuance, ZIP packaging and all production/UI/configuration writes are external.
"""
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_round4 as previous_generation
from . import mask_saved_preannotations_v2 as saved
from .cluster_targets import digest, protected_snapshot
from .mask_quota_saved_extension import file_inventory, indexed, load_formal_evidence
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 .pose_review_geometry import canonical

VERSION = 'remaining_saved_gallery_round5_v1'
PUBLICATION_VERSION = 15
GENERATION_NAMESPACE = 'remaining_round5_generation'
PARENT = previous_generation.OUTPUT
REQUESTS = PROJECT/'outputs/pallet_mask_preannotated_requests_v2'
OLD_REQUESTS = previous_generation.REQUESTS
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_round5_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
history = previous_generation.previous_generation
REVIEW_SCHEMA = history.REVIEW_SCHEMA
QUARANTINE = history.QUARANTINE
user_instruction = previous_generation.user_instruction
historical_review_bindings = previous_generation.historical_review_bindings
target_inventory = previous_generation.target_inventory
saved_status = previous_generation.saved_status
audit_pose_snapshot = history.audit_pose_snapshot
package_new_preview = history.package_new_preview
PARENT_PINS = {
    'manifest.json': '1cf30d3628f69adbc573260ec66c335fd174ffdb89550b6772f8bb2d274b005a',
    'verification.json': 'a60e6c7d8f946532f36f9c84adbfc991f5a14c84ec187b4f2e13aab0339ecb5a',
    'proposals.jsonl': 'b012d4a6570b3f66ab974795b347a2dad517bba6d8fcca37a73a137184d6390d',
    'artifact_hashes.json': '05d75289e9cf38ad389f2ce752fb07f9e828e7bdf493edfc145dd31496279c97',
    'all_manual_sources.jsonl': '2ae591b0996208e5378a78aa760efecdc5bc92eeae9930e2c924d6d338511360',
    'new_proposals.jsonl': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
    'reviewed_original_rectangle_source_records.jsonl': 'e0370bfb8ccd384e14078433d3920ee2e17aa054d39e012cf72905c0c4d2867f',
    'pending_original_rectangle_saves.jsonl': '9369c59877a315f8adc172f680f0722794bdfb978969501c7977d9e26bd7907e',
    'review_statement_bindings.json': '3530ed4a69c89ca41c368afa48f9046d4079c7c0b88e817aa3acfda529de37b1',
}
REQUEST_PINS = {
    'manifest.json': '6a348bb6606ad2d02e66713924798a19348e6b6f2addfbd32919285c8a08ab33',
    'verification.json': '4730c01bc4fd16dbe0f4095336ab8edd0606aed8e021ce7c6a1bedb41f37ee65',
    'proposals.jsonl': 'fb52204df2463bedc21f4b549a320af3de0676f03e42b769a1bf017c86d01673',
    'requests.jsonl': '4e0f47eaad071af61697ded55034f082224846476f3e82bc60e00d9b960ccbaa',
    'input_manifest.json': 'f29dd37276635347cab3be973b00305fceabbaeea4aa08eaca653a2e08a20987',
    'internal/build_decisions.json': 'e1690235be3f1a9e8e2b93f054f4b78c1658f56a504077abb67bd581dd74841a',
    'internal/deferred_sources.json': '3234e2d41b881107575c15c79cc1fec73e58ace4d4d532508e8b5229398c6512',
}


def validate_source_delta(parent_snapshot, snapshot, table, allowed_keys):
    result = history.validate_source_delta(parent_snapshot, snapshot, table, allowed_keys)
    with closing(readonly(snapshot, True)) as db:
        events = list(db.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
    added = events[result['historical_events_preserved']:]
    result.update(new_event_target_keys=sorted({k for _, k, _, _ in added}),
        new_event_counts_by_target=dict(sorted(Counter(k for _, k, _, _ in added).items())),
        new_event_raw_records=[dict(event_id=i, target_key=k, revision=r, payload_raw_sha256=saved.raw_sha(raw))
            for i, k, r, raw in added],
        raw_historical_prefix_bytewise_preserved=True,
        all_new_events_within_authorized_current_task_scope=True)
    return result


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') != 14 or manifest.get('proposals') != 1515 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 14 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 14 required')
    if any(parent_hashes.get(n) != h for n, h in read_json(parent/'artifact_hashes.json').items()):
        raise ValueError('Frozen parent artifact changed')
    if any(digest(PROJECT/n) != h for n, h in manifest['frozen_code_sha256'].items()):
        raise ValueError('Frozen original engine changed')
    task, seal = read_json(requests/'manifest.json'), read_json(requests/'verification.json')
    if (seal.get('state') != 'passed' or seal.get('request_count') != 35 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['parent_manifest_sha256'] != parent_hashes['manifest.json'] or
            task['parent_proposals_sha256'] != parent_hashes['proposals.jsonl'] or
            request_hashes['independent_verification.json'] != request_hashes['verification.json']):
        raise ValueError('Matching independent preannotation v2 seal required')
    for assets in (seal['public_assets'], task['public_assets']):
        if any(request_hashes.get(n) != h for n, h in assets.items()):
            raise ValueError('Verified task resource changed')
    return manifest, parent_hashes, request_hashes


def historical_task_inventory(rows, parent, task_directories):
    """Preserve the parent's exact issued/rejected scope plus the completed v2 pack.

    Do not glob new staging directories: upcoming, unissued v3 drafts are not
    historical tasks and cannot reserve or count frames in this publication.
    """
    hashes = read_json(parent/'historical_task_input_hashes.json')
    if any(digest(PROJECT/n) != h for n, h in hashes.items()):
        raise ValueError('Historical issued task or rejected draft changed')
    for directory in task_directories.values():
        for name in ('requests.jsonl', 'internal/shortlist.jsonl', 'internal/deferred_sources.json',
                     'internal/build_decisions.json', 'internal/assistant_decisions.json'):
            path = directory/name
            hashes[str(path.relative_to(PROJECT))] = digest(path)
    keys = set()
    def visit(value):
        if isinstance(value, dict):
            if value.get('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 name in hashes:
        path = PROJECT/name
        visit(read_jsonl(path) if path.suffix == '.jsonl' else read_json(path))
    return keys, hashes


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)
    pose_four = {r['target_key'] for r in sources if r['manual_four_point_source']}
    gallery_four = {k for k, r in reviews.items() if len(r['polygon']) == 4}
    for cls, values in counts.items():
        keys = {p['target_key'] for p in previous+manual+new if p['class_id'] == cls}
        values['manual_four_point_sources'] = len(keys & (pose_four | gallery_four))
    return counts


def preserve_assets(parent, out, previous, task_directories):
    hashes = {}
    for source, destination in [(parent, 'parent_evidence')] + [
            (p, 'preannotation_source_evidence/'+n) for n, p in task_directories.items()]:
        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/private evidence copy changed bytes')
    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=15')}*{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>第14版真实新增机器候选 {status["parent_new_machine_candidates"]} 份，本次不伪造候选完成记录。'
        f'独立预标注v2任务{task_status["actually_saved"]}份均有真实图册人工保存：'
        f'{task_status["polygon_changed_from_proposal"]}份相对助手草稿改变、{task_status["saved_unchanged"]}份保存保持草稿。'
        f'{task_status["actually_saved"]}份新人工来源为图册保存，不是位姿保存；不把助手草稿当成人工事件。</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份未保存候选不变。'
        '所有历史改绘、返回原框、删除、暂缓、确认、隔离、已派发任务与助手拒绝草稿均按整帧保护。未派发v3草稿不纳入历史任务或覆盖。</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的类别（包括类别8）不再扫描。</p>'
        f'<p>用户原话：{html.escape(USER_STATEMENT)}。每个原始事件按发布版本、原提案哈希和目标键联合验证；独立预标注v1、v2的完整证据链分别冻结。原生图像、真实事件、父级规范记录、私有快照、旧ZIP与全部历史资源逐字节保留。'
        '不写生产库、配置、服务、正式数据、单位或训练资源；不自动确认为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 p in sorted(combined, key=lambda p: (p['class_id'], p['target_key'])):
        key = p['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="{p["class_id"]}" data-batch="{"new" if key in new_keys else "previous"}"{hidden}>'
            f'<h2>类别 {p["class_id"]} · {label}</h2><img loading="lazy" src="cases/{key}.png?revision=15" alt="来源对照">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {key}</p>'
            f'<a href="labelme/{key}.json?revision=15">LabelMe</a> · <a href="masks/{key}.png?revision=15">掩膜</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)
    task_directories = dict(v1=OLD_REQUESTS, v2=task_dir)
    task_inventories = {n: file_inventory(p) for n, p in task_directories.items()}
    if any(task_inventories['v1'].get(n) != h for n, h in previous_generation.REQUEST_PINS.items()):
        raise ValueError('Frozen independent preannotation v1 pack changed')
    required_disk = 2*sum(p.stat().st_size for p in parent.rglob('*') if p.is_file())
    if shutil.disk_usage(PROJECT/'outputs').free < required_disk:
        raise ValueError('Insufficient disk for full immutable evidence copy; do not delete historical artifacts')
    config_hashes, frozen_code = file_inventory(PROJECT/'configs'), dict(manifest['frozen_code_sha256'])
    for name in ('pallet/mask_remaining_round5.py', 'pallet/mask_saved_preannotations_v2.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, parent, task_directories)
    if (len(rows) != 12344 or len(previous) != 1515 or parent_new 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: 5, 7: 10, 14: 10}):
        raise ValueError('Formal / parent1515 / parent-new0 / pose482 / task35 / 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),
            ('preannotation_input_file_inventories', task_inventories), ('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, gallery_sha = snapshot_database(pose_live, pose_snapshot), snapshot_database(gallery_live, gallery_snapshot)
    delta = dict(pose=validate_source_delta(parent/'reviews_snapshot.sqlite3', pose_snapshot, 'annotations', ()),
        gallery=validate_source_delta(parent/'gallery_reviews_snapshot.sqlite3', gallery_snapshot, 'reviews', requested))
    if (delta['pose']['new_events'] != 0 or delta['gallery']['new_events'] != 38 or
            set(delta['gallery']['current_changed_keys']) != requested):
        raise ValueError('Actual unchanged pose / gallery35 keys38 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 = 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'))
        # These two pending records came from round3 tasks, not either prelabel pack.
        pending_requests = indexed(read_jsonl(parent/'parent_evidence/requests.jsonl'))
        for key, old_record in pending.items():
            fresh = history.pending_original_rectangle_save(db, key, rows[key], pose_sha, sha(binding), pending_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']
            statement = next(g['statement'] for g in bindings if key in g['target_keys'])
            _, _, fresh = history.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: history.previous_generation.PARENT,
        12: history.PARENT, 13: previous_generation.PARENT, 14: 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.combine_registries(*(saved.prelabel_registry(directory, rows, formal_sha,
        publications[version], current, pose_event_keys, version, namespace)
        for namespace, version, directory in (('v1', 13, task_directories['v1']), ('v2', 14, task_dir))))
    current_registry = saved.task_references(registry, 'v2')
    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(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(current_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)
    if (len(locked) != 1550 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) != 35 or Counter(p['vertex_count'] for p in manual) != {4: 35} or
            len(current) != 4946 or pose_events != 11926 or len(reviews) != 976 or len(events) != 1061 or
            set(templates) != set(manifest['generation_template_keys']) or set(templates) & requested):
        raise ValueError('Actual locked1550 / pose482 / gallery976 / seed679 contract changed')
    # Seed inputs/polygons remain unchanged; snapshot provenance and namespace are
    # correctly refreshed. Every eligible target is nevertheless replayed below.
    for key, template in templates.items():
        old = read_json(parent/previous_generation.GENERATION_NAMESPACE/'templates'/f'{key}.json')
        if any(template[f] != old[f] for f in ('row', 'polygon', 'origin', 'revision', 'human_polygon_revision', 'human_event_sha256')):
            raise ValueError('Inherited strict template input 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.update(source_kind='independent_assistant_preannotation_followed_by_real_gallery_human_save',
        parent_new_machine_candidates=0, completed_preannotation_tasks=len(drafts))
    if (status['actually_saved'] != 0 or task_status['polygon_changed_from_proposal'] != 35 or task_status['saved_unchanged'] != 0):
        raise ValueError('Actual empty parent machine set / task35 changed-save 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/v1', 'preannotation_source_evidence/v2']))
    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'))
    canonical_current = sum(r['proposal_registry'] == 'canonical_publication' for r in gallery_sources)
    write_json(out/'source_inventory.json', dict(schema='canonical_and_independent_prelabel_event_registry_v2',
        registry_key_fields=['proposal_publication', 'base_proposal_id', 'target_key'],
        publications={v: {k: x for k, x in p.items() if k not in ('path', 'proposals')} for v, p in publications.items()},
        prelabels=[dict(target_key=r['proposal']['target_key'], base_proposal_id=r['proposal_sha256'],
            namespace=r['namespace'], task_manifest_sha256=r['task_manifest_sha256'],
            task_verification_sha256=r['task_verification_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 _, r in sorted(registry.items())],
        canonical_gallery_saved=canonical_current, independent_prelabel_gallery_saved=len(reviews)-canonical_current,
        independent_task_registry_entries=len(registry), current_task_namespace='v2'))
    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)
    literal_lines = {json.loads(line)['target_key']: line for line in (out/'proposals.jsonl').read_bytes().splitlines(keepends=True)}
    if any(literal_lines[json.loads(line)['target_key']] != line
            for line in (parent/'proposals.jsonl').read_bytes().splitlines(keepends=True)):
        raise ValueError('Historical canonical proposal line bytes changed')
    for directory in ('cases', 'images', 'masks', 'labelme'):
        (out/directory).mkdir()
    copied = preserve_assets(parent, out, previous, task_directories)
    saved.package_manual_preview(out, manual, evidence)
    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(PROJECT/'configs') != config_hashes or
            any(file_inventory(p) != task_inventories[n] for n, p in task_directories.items()) 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_round5_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'],
        preannotation_source_evidence_namespaces=['v1', 'v2'],
        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), actual_parent_new_candidate_count=len(parent_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),
        all_saved_four_point_sources=total('manual_four_point_sources'),
        reviewed_original_rectangle=len(review_sources), new_reviewed_original_rectangle=0,
        inherited_reviewed_original_rectangle=len(review_sources), reviewed_original_rectangle_schema=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=canonical_current, gallery_prelabel_current_validated=len(reviews)-canonical_current,
        gallery_prelabel_registry_entries=len(registry), gallery_source_registry_key_fields=['proposal_publication', 'base_proposal_id', 'target_key'],
        inherited_template_inputs_unchanged=True, all_current_eligible_targets_strictly_replayed=True,
        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', 'preannotation_input_file_inventories'):
        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 15 draft — pending independent verification\n\n'
        f'{len(combined)} globally distinct frames = parent {len(previous)} + real saved gallery v2 contours {len(manual)} + strict machines {len(new)}.\n\n'
        f'All {len(reviews)} gallery currents and {len(events)} raw historical human events validated. '
        'Each event resolves by (publication, base_proposal_id, target_key), canonical publications 1–14 or independent task v1/publication13 and v2/publication14. '
        'Task draft hashes are not replaced with canonical hashes. Both complete original task packs are frozen separately under preannotation_source_evidence/v1 and /v2; full immutable parent chain under parent_evidence.\n\n'
        f'Parent new machines: {len(parent_new)} (empty audit, not fabricated completion). '
        f'Current task v2: {task_status["polygon_changed_from_proposal"]} changed / {task_status["saved_unchanged"]} unchanged, real gallery saves not pose events. '
        'Saved gallery quads use saved_gallery_mask_entry_v1, origin mask_gallery_human_revision, self-key gallery_review with exact raw hashes and task provenance; 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, unchanged polygons/inputs; 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 issued request and rejected-draft frames protected; upcoming unissued v3 staging excluded.\n\n'
        f'{len(work)} current eligible targets replayed with {workers} workers, {len(attempts)} actual 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 actual attempts and matching, validate all raw event identities and current pairs, and check preserved evidence/assets 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()
