"""Publication 12: exact saved-review freeze and guarded remaining-class transfer.

Only a new output directory is written. Real saved original-box reviews count as
review coverage, never modified masks or seeds. Historical statements and raw
evidence retain their original bindings; no live store, configuration or service
is written. Publication/independent verification are separate operations.
"""
from __future__ import annotations

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

import cv2
import numpy as np
from PIL import Image

from . import mask_remaining_continue as previous_generation
from . import mask_remaining_expand as expand
from . import mask_remaining_classes as remaining
from . import mask_quota_100_continue as prior
from . import mask_quota_completion as original
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 (QUARANTINE, readonly, read_jsonl,
    snapshot_database, verify_history_preserved, write_jsonl)
from .mask_template_transfer import AUTO_EVENTS, PROJECT, annotation_inventory, write_json
from .pose_review_geometry import canonical, polygon_value

VERSION = 'remaining_saved_review_round2_v1'
PUBLICATION_VERSION = 12
GENERATION_NAMESPACE = 'remaining_round2_generation'
CLASSES, ALL_CLASSES = remaining.CLASSES, tuple(range(1, 23))
TARGET_PER_CLASS = 100
PARENT = PROJECT/'outputs/pallet_mask_remaining_continue_v1'
REQUESTS = PROJECT/'outputs/pallet_mask_remaining_followup_requests_v2'
OUTPUT = PROJECT/'outputs/pallet_mask_remaining_round2_v1'
MANUAL_SCHEMA, FALSE_FLAGS = prior.MANUAL_SCHEMA, prior.FALSE_FLAGS
REVIEW_SCHEMA = REVIEW_ORIGIN = 'reviewed_original_rectangle_entry_v1'
USER_STATEMENT = '已经完成185张候选复核和54张新图的标注，其中没有修改记录的是检验后无需修改的。继续'
ORIGINAL_REVIEWS = {key: (4, 1) for key in (
    '0f62405fc643c71371c8a0ec', '7041455b447a160ccc16d9df', 'db94cf18b0c6e9f8641a9be0')}
HISTORICAL_NAMESPACES = previous_generation.HISTORICAL_NAMESPACES + (previous_generation.GENERATION_NAMESPACE,)
PARENT_PINS = {
    'manifest.json': '37a84183b68c93907bc9f1a4176b7b429629ec197a36f47b67e4f247ec59a301',
    'verification.json': 'b0ce96126faaeceb334cb0f9a09441951f5c8fd2181b875745a13b653a03ef78',
    'proposals.jsonl': '8da4534ba1e947c66fc31d73559a8a3e6ae1cea2531c0869c99df2eb9509ab67',
    'artifact_hashes.json': '89b2e268103a799e35a233ec6c745e552a272360bdb5ca020e2462c13fcd2db7',
    'all_manual_sources.jsonl': '42864f4306a86bb09808e2cc10d0881156fe9a8df1de252a63ff51823f94fc5c',
    'new_proposals.jsonl': 'e2d505640aad83ee390de38f730381f5e721aa0f2d25f6a36cd9d6da8dc9978a',
    'reviewed_original_rectangle_source_records.jsonl': 'e530c97ca7f12d3527216ea3fdf4be6492096e9101d524c4a71fb2a8f314d497',
    'review_completion_statement.json': '21e911b97d1925703a13d74e86d2984b930ab9641b584faf278e94ce84fe81f6',
    'pallet_mask_refined_review.zip': 'a04747bf1cc380718a414b6644c946eb604f097a57e8265427be5b01c6e8c7d5',
}
REQUEST_PINS = {
    'manifest.json': 'd44e8b857b5097c5b11ad3fe22ef4146c5fc8a0cb965c657894bcded14789489',
    'verification.json': '916ea1eaccc81ffd63048d787724b49bdb9be16cb9f5edff8555ec0e782b0a12',
    'independent_verification.json': '916ea1eaccc81ffd63048d787724b49bdb9be16cb9f5edff8555ec0e782b0a12',
    'requests.jsonl': 'ddf7aaf685ace8c7e0061888b1f671c950247247bf093c841e7e5e6bfc29d416',
}
read_json = remaining.read_json
# These imported helpers are stateless and have no publication-specific policy.
class_counts = previous_generation.class_counts
target_inventory = previous_generation.target_inventory
package_manual_preview = previous_generation.package_manual_preview
package_new_preview = previous_generation.package_new_preview


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') != 11 or manifest.get('proposals') != 1296 or
            seal.get('state') != 'passed' or seal.get('publication_version') != 11 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 11 required')
    for n, h in read_json(parent/'artifact_hashes.json').items():
        if parent_hashes.get(n) != h:
            raise ValueError('Frozen parent resource changed: '+n)
    for n, h in manifest['frozen_code_sha256'].items():
        if digest(PROJECT/n) != h:
            raise ValueError('Frozen original engine changed: '+n)
    seal = read_json(requests/'independent_verification.json')
    if (seal.get('state') != 'passed' or seal.get('request_count') != 54 or
            seal.get('manifest_sha256') != request_hashes['manifest.json'] or
            seal.get('requests_sha256') != request_hashes['requests.jsonl'] or
            seal.get('parent_verification_sha256') != parent_hashes['verification.json']):
        raise ValueError('Matching independent 54-request seal required')
    for n, h in seal['public_assets'].items():
        if request_hashes.get(n) != h:
            raise ValueError('Verified request asset changed: '+n)
    return manifest, parent_hashes, request_hashes


def review_statement(requests, parent_new):
    return dict(schema='explicit_user_review_no_change_statement_v1', date='2026-10-25',
        statement_zh=USER_STATEMENT, scoped_request_keys=sorted(indexed(requests)),
        scoped_candidate_keys=sorted(indexed(parent_new)),
        reviewed_original_rectangle_keys=sorted(ORIGINAL_REVIEWS),
        scope='Current 54 direct requests and actual 185 prior machine candidates only',
        candidate_count_in_statement=185, actual_parent_batch_saved_count=185,
        candidate_count_authority='actual immutable current/raw latest event evidence',
        old_original_rectangle_statement='parent_evidence/review_completion_statement.json',
        old_original_rectangle_statement_scope_unchanged=True,
        old_45_conversation_dispositions_unchanged=True, old_42_unsaved_not_inferred_reviewed=True,
        creates_human_event=False, confirms_ground_truth=False, authorizes_propagation=False)


def exact_original_rectangle_review(db, key, row, snapshot_sha, binding, statement,
                                    expected_statement=USER_STATEMENT):
    """Bind a scoped statement to exact real saved bytes, without inventing an edit."""
    if (statement.get('statement_zh') != expected_statement or
            key not in statement.get('scoped_request_keys', []) or
            key not in statement.get('reviewed_original_rectangle_keys', [])):
        raise ValueError('Original rectangle lacks an explicit scoped user review statement')
    found = list(db.execute('SELECT revision,payload FROM annotations WHERE target_key=?', (key,)))
    history = list(db.execute('SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,)))
    if len(found) != 1 or not history or len({r for _, r, _ in history}) != len(history):
        raise ValueError('Original rectangle lacks unique current and real event history')
    revision, raw = found[0]; value = json.loads(raw)
    flags = remaining.CONFIRMATION_FLAGS + ('full_six_dof',)
    for v, rev in [(value, revision)]+[(json.loads(s), r) for _, r, s in history]:
        if (v.get('target_key') != key or v.get('revision') != rev or v.get('binding') != binding or
                v.get('state') != 'draft' or not remaining.original_rectangle(v.get('polygon'), row) or
                v.get('event_kind') in AUTO_EVENTS or remaining.machine_marked(v) or
                any(v.get(f, False) is not False for f in flags) or
                any(v.get('pose', {}).get(f, False) is not False for f in flags) or
                any(v.get('source', {}).get(f) != row[f] for f in prior.IDENTITY_FIELDS) or
                any(f in v.get('source', {}) and v['source'][f] != row.get(f)
                    for f in ('final_class_id', 'viewpoint_candidate'))):
            raise ValueError('Original rectangle identity/binding/history/state/flags mismatch')
        polygon_value(v['polygon'], *row['size'])
    event_id, event_revision, event_raw = history[-1]
    if event_revision != revision or max(r for _, r, _ in history) != revision or event_raw != raw:
        raise ValueError('Original rectangle current is not its exact latest real human event')
    event_sha = hashlib.sha256(event_raw.encode('utf-8')).hexdigest()
    pose_review = dict(target_key=key, snapshot_sha256=snapshot_sha, revision=revision,
        payload_sha256=sha(value), saved_review_revision=event_revision,
        saved_review_event_id=event_id, saved_review_event_sha256=event_sha,
        polygon_sha256=sha(value['polygon']), user_statement_sha256=sha(statement))
    metadata = dict(target_key=key, class_id=row['final_class_id'], split=row['split'],
        snapshot_revision=revision, current_payload_sha256=sha(value), origin=REVIEW_ORIGIN,
        vertex_count=4, human_edited=False, saved_pose_review_source=True,
        user_reviewed_no_change=True, original_rectangle_review_only=True,
        propagation_seed_eligible=False, pose_review=pose_review)
    record = dict(schema='reviewed_original_rectangle_source_freeze_v1', target_key=key,
        source=row, source_identity_sha256=sha(row), snapshot_sha256=snapshot_sha,
        current_revision=revision, current_payload_raw=raw, current_payload=value,
        current_payload_sha256=sha(value), current_payload_raw_sha256=hashlib.sha256(raw.encode('utf-8')).hexdigest(),
        saved_review_event_id=event_id, saved_review_revision=event_revision,
        saved_review_event_payload_raw=event_raw, saved_review_event_payload=json.loads(event_raw),
        saved_review_event_sha256=event_sha, polygon_sha256=sha(value['polygon']),
        history_event_ids=[i for i, _, _ in history], human_edited=False,
        saved_pose_review_source=True, original_rectangle_review_only=True, user_reviewed_no_change=True,
        propagation_seed_eligible=False, user_statement_sha256=sha(statement),
        classification_is_candidate_not_truth=True, **dict.fromkeys(FALSE_FLAGS, False))
    return value, metadata, record


def validate_source_delta(parent_snapshot, snapshot, table, allowed_keys):
    """Old current bytes/events remain intact; new writes touch only this task set."""
    with closing(readonly(parent_snapshot, True)) as old, closing(readonly(snapshot, True)) as new:
        before = {k: (r, s) for k, r, s in old.execute(f'SELECT target_key,revision,payload FROM {table}')}
        after = {k: (r, s) for k, r, s in new.execute(f'SELECT target_key,revision,payload FROM {table}')}
        changed = {k for k in before.keys() | after.keys() if before.get(k) != after.get(k)}
        if not changed <= set(allowed_keys):
            raise ValueError('Unexpected current source change outside completed tasks')
        historical = list(old.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        events = list(new.execute('SELECT id,target_key,revision,payload FROM events ORDER BY id'))
        if events[:len(historical)] != historical or any(e[1] not in allowed_keys for e in events[len(historical):]):
            raise ValueError('Historical raw events changed or unrelated new event appeared')
    return dict(current_changed_keys=sorted(changed), new_events=len(events)-len(historical),
        historical_events_preserved=len(historical), unrelated_source_changes=0)


def completed_manual_proposals(requests, candidates, reviewed, rows, previous,
                               pose_sha, formal_sha, classification_sha, quarantine=(), gallery_keys=()):
    indexed(requests)
    frames = {p['source']['frame_id'] for p in previous}; keys = set(indexed(previous))
    result = []
    for request in sorted(requests, key=lambda r: r['target_key']):
        key = request['target_key']; row = rows[key]
        if (key in keys or row['frame_id'] in frames or key in quarantine or key in gallery_keys or
                row['final_class_id'] not in CLASSES or request['class_id'] != row['final_class_id'] or
                request['source'].get('formal_manifest_sha256') != formal_sha or
                any(request['source'].get(f) != row[f] for f in prior.IDENTITY_FIELDS+('viewpoint_candidate',))):
            raise ValueError('Completed request repeats protected frame or has changed source identity')
        if key in reviewed:
            if key in candidates:
                raise ValueError('Original review cannot be a modified manual source')
            value, metadata, _ = reviewed[key]
            p = dict(schema=REVIEW_SCHEMA, generation_schema=VERSION, origin=REVIEW_ORIGIN,
                target_key=key, class_id=row['final_class_id'], source=row, polygon=value['polygon'],
                vertex_count=4, status='user_reviewed_original_rectangle_pending_confirmation',
                human_edited=False, saved_pose_review_source=True, user_reviewed_no_change=True,
                original_rectangle_review_only=True, propagation_seed_eligible=False,
                source_label_kind='REVIEWED_ORIGINAL_RECTANGLE',
                coverage_basis='explicit_user_inspection_no_change_plus_real_saved_pose_review',
                mask_semantics='original_box_accepted_by_explicit_user_review_not_modified_mask_or_GT',
                original_box_review_basis='review_completion_statement.json', human_source=metadata,
                **dict.fromkeys(FALSE_FLAGS, False), metrics={}, pose_snapshot_saved=True,
                target_expected_revision=value['revision'], target_snapshot_payload_sha256=sha(value),
                snapshot_sha256=pose_sha, formal_manifest_sha256=formal_sha,
                class_assignments_sha256=classification_sha, machine_draft=False,
                flags=['reviewed_original_rectangle_coverage_only_not_modified_human_mask_not_GT'])
        elif key in candidates:
            value = candidates[key][0]
            p = remaining.saved_manual_proposal(key, row, candidates[key], pose_sha, formal_sha, classification_sha)
            p.update(generation_schema=VERSION, machine_draft=False)
        else:
            raise ValueError('Completed request lacks exact saved human or explicit original-box review')
        if value['revision'] <= request['existing_pose_revision']:
            raise ValueError('Completed request has no newer saved review revision')
        branch = 'review_source:' if key in reviewed else 'manual_source:' if len(value['polygon']) == 4 else ''
        p.update(manual_source_kind='completed_direct_manual_request',
            rendering_id=GENERATION_NAMESPACE+':'+branch+key,
            manual_request_sha256=sha(request), original_pose_url=request.get('pose_annotation_url'),
            pose_store_written=False, gallery_store_written=False, machine_transfer_performed=False,
            new_human_edit_created=False)
        result.append(p); frames.add(row['frame_id']); keys.add(key)
    return result


def generation_templates(candidates, counted_remaining_keys, gallery, formal_sha, classification_sha):
    """Explicit counted humans and validated saved remaining gallery only; >4 points."""
    templates = {}
    for key in sorted(counted_remaining_keys):
        value, metadata, record = candidates[key]
        row = record['source']
        if row['final_class_id'] not in CLASSES:
            raise ValueError('Old five-class human cannot seed remaining continuation')
        if len(value['polygon']) <= 4:
            continue
        templates[key] = dict(row=row, polygon=value['polygon'], revision=value['revision'],
            human_polygon_revision=record['human_polygon_revision'], human_event_sha256=record['human_event_sha256'],
            current_payload_sha256=record['current_payload_sha256'], human_mask_confirmed=False,
            origin=prior.POSE_ORIGIN, pose_review=metadata['pose_review'])
    for key, template in sorted(gallery.items()):
        if template['row']['final_class_id'] not in CLASSES:
            continue
        if key in templates or len(template['polygon']) <= 4 or template['origin'] != original.GALLERY_ORIGIN:
            raise ValueError('Invalid or overlapping direct remaining gallery seed')
        templates[key] = template
    return {key: dict(t, propagation_seed_eligible=True, rendering_id=GENERATION_NAMESPACE+':'+key,
        formal_manifest_sha256=formal_sha, class_assignments_sha256=classification_sha)
        for key, t in sorted(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 preserve_assets(parent, out, previous):
    shutil.copytree(parent, out/'parent_evidence')
    hashes = {'parent_evidence/'+n: h for n, h in file_inventory(parent).items()}
    for p in previous:
        for directory, extension in (('cases', 'png'), ('images', 'png'), ('masks', 'png'), ('labelme', 'json')):
            n = f'{directory}/{p["target_key"]}.{extension}'
            shutil.copyfile(parent/n, out/n); hashes[n] = digest(parent/n)
    for directory in HISTORICAL_NAMESPACES:
        shutil.copytree(parent/directory, out/directory)
        hashes.update({directory+'/'+n: h for n, h in file_inventory(parent/directory).items()})
    for n in ('vertex2_before_after.png', 'FONT_LICENSE.txt', 'review_dispositions.jsonl',
            'review_disposition_statement.json'):
        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):
    historical = parent_inventory['templates']
    sources = parent_inventory.get('manual_source_renderings', []) + [dict(target_key=p['target_key'],
        polygon=p['polygon'], rendering_id=p['rendering_id'], human_source=p['human_source'],
        propagation_seed_eligible=False, manual_four_point_source=p.get('manual_four_point_source', False),
        original_rectangle_review_only=p.get('original_rectangle_review_only', False), human_edited=p['human_edited'])
        for p in manual if 'human_source' in p]
    all_templates = historical+list(templates.values())
    ids = [t['rendering_id'] for t in all_templates+sources]
    if len(set(ids)) != len(ids):
        raise ValueError('Historical/current rendering namespace collision')
    mapping = dict(parent_inventory['rendering_by_target'])
    mapping.update({p['target_key']: p['rendering_id'] for p in manual})
    mapping.update({p['target_key']: p['rendering_id'] for p in combined if p['target_key'] in new_keys})
    if set(mapping) != set(indexed(combined)) or not set(mapping.values()) <= set(ids):
        raise ValueError('Missing cumulative drawable source')
    return dict(schema=VERSION, publication_version=PUBLICATION_VERSION, templates=all_templates,
        manual_source_renderings=sources, rendering_by_target=mapping,
        generation_template_keys=sorted(templates), generation_templates=len(templates),
        generation_template_namespace=GENERATION_NAMESPACE,
        generation_templates_by_origin=dict(Counter(t['origin'] for t in templates.values())),
        historical_templates=len(historical), historical_templates_not_generation_authorization=True,
        historical_snapshot_provenance=dict(parent='parent_evidence/snapshot_manifest.json'))


def build_report(out, combined, counts, new_keys, manual_keys, pose_keys, reviews,
                 disposition_keys, status, templates):
    total = lambda f: sum(v[f] 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>{c}</td>'+''.join(f'<td>{counts[c][f]}</td>' for f in fields)+'</tr>' for c in ALL_CLASSES)
    cards = []
    for p in sorted(combined, key=lambda v: (v['class_id'], v['target_key'])):
        key = p['target_key']
        if key in new_keys:
            label = '【本轮新增机器候选】待人工复核，不是人工标签或真值'
        elif p.get('original_rectangle_review_only'):
            label = '【原框已复核无需修改】仅计复核覆盖，不是人工改绘或模板；旧记录仍绑定旧声明'
        elif key in manual_keys:
            label = '【本轮完成的人工改绘】真实保存，保持四点或八点，不补造顶点；四点不作模板'
        elif key in reviews:
            label = '【已有真实图册保存复核】历史图像原样保留；最新轮廓及原始事件单独冻结'
            if key in status['unchanged_keys']:
                label += '；复核后保持原候选，不声称新增人工改绘'
        elif key in pose_keys:
            label = '【既有人工改绘来源】历史快照原样保留，最新保存另行审计'
        elif key in disposition_keys:
            label = '【旧45份用户已复核无需修改】旧声明及侧记不变，不伪造保存，不作模板'
        else:
            label = '【旧候选未见新服务器保存】不扩大无需修改声明，不据此推断未查看'
        hidden = ' hidden' if new_keys and key not in new_keys else ''
        kind = 'reviewed_original_rectangle' if p.get('original_rectangle_review_only') else 'manual' if key in manual_keys else 'machine'
        metrics = (f'<p>边界相关度 {p["metrics"]["boundary_ncc"]:.3f}；ECC {p["metrics"]["ecc"]:.3f}，仅为配准诊断，不是正确率。</p>' if key in new_keys else '')
        link = (f'<p><a href="{html.escape(p["original_pose_url"], quote=True)}">打开原位姿人工标注</a></p>' if p.get('original_pose_url') else '')
        cards.append(f'<article id="target-{key}" data-class="{p["class_id"]}" data-kind="{kind}" '
            f'data-batch="{"new" if key in new_keys else "previous"}"{hidden}><h2>类别 {p["class_id"]} · {label}</h2>'
            f'<img loading="lazy" src="cases/{key}.png?revision=12" alt="历史快照或本轮来源对照">'
            f'<p>{html.escape(p["source"]["frame_id"])} ／ {p["source"]["split"]} ／ {key}</p>{metrics}{link}'
            f'<a href="labelme/{key}.json?revision=12">多边形（LabelMe）</a> · <a href="masks/{key}.png?revision=12">掩膜</a></article>')
    page = '''<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>第十二版 · 剩余类别严格续批</title><style>@font-face{font-family:PreviewSans;src:url('report-font.otf?revision=12')}*{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%;max-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;width:100%}select{font:inherit;padding:6px}[hidden]{display:none!important}@media(max-width:700px){body{padding:10px}article{padding:7px}h2{font-size:18px}}</style>
<h1>第十二版 · 剩余类别严格续批（待独立核验，尚未发布）</h1>
'''
    page += (f'<section id="new-batch"><h2>本轮新增机器候选 {len(new_keys)} 份</h2>'
        f'<p>上轮185份候选均有真实保存：132份轮廓变化、53份复核后保持原候选；53份不是新增人工改绘。'
        f'本轮54项请求：51份实际修改（47份四点、4份类别3八点），另3份原框明确复核无需修改。'
        f'人工保存轮廓来源 {total("saved_human")} = 位姿改绘 {total("saved_pose_human")} + 图册保存复核 {total("saved_gallery_human")}；'
        f'另列原框复核 {total("reviewed_original_rectangle")}（旧2＋新3），数据库已存复核合计 {total("db_stored_human_review")}。'
        f'全22类累计覆盖 {total("total")}/2200 个不同帧，缺 {total("missing")}；覆盖不是人工完成率或真值率。</p></section>')
    page += (f'<p>传播模板 {len(templates)} 份，仅含45份超过四点的剩余类别位姿来源和554份真实保存图册复核。'
        '旧五类270份图册来源不用于本轮传播。四点、原框、旧45份对话侧记和42份旧未保存候选不作模板。'
        '类别1、2、3、6、9、10、13、15已达100，禁止继续生成；类别4、7、22无超过四点模板。'
        f'类别19锁定1350帧后重新计算的原图容量为 {counts[19]["capacity_matching_distinct_frames"]}，不强行补满。</p>')
    page += (f'<p>本次用户原话：{html.escape(USER_STATEMENT)}。声明仅绑定本轮54项请求及185份实际候选；'
        '旧两份原框复核与旧45份侧记继续使用各自原声明，完整原始字节保留在parent_evidence，不重写人工历史。</p>')
    page += '''<p>只用冻结原始attempt_target：同类、同划分、同传感器及兼容视角；初始相关度至少0.55、ECC至少0.84、边界相关度至少0.78、逐顶点往返误差不超过1.5并通过横梁边界检查。保持顶点数及顺序，不递归传播、不放宽门槛、不强行填满。1350个已计入帧全部锁定；新增使用全局不同帧最大匹配和组别多样性选择。</p>
<p>旧1296条规范记录、图片、掩膜、LabelMe及全部历史命名空间逐字节保留，完整私有父级证据冻结在parent_evidence。历史卡片不是最新保存图。两库快照、备份、全部历史和20768个正式文件受保护；原始分类分母12344个目标。所有类别均为候选，不确认为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> '
    page += '<label><input type="checkbox" id="only-new"'+(' checked' if new_keys else '')+'>只看本轮新增机器候选</label></p>'+''.join(cards)
    page += """<script>const cls=document.querySelector('#filter'),only=document.querySelector('#only-new');function filter(){document.querySelectorAll('article').forEach(a=>a.hidden=(!!cls.value&&a.dataset.class!==cls.value)||(only.checked&&a.dataset.batch!=='new'));}cls.onchange=only.onchange=filter;filter();</script></html>"""
    (out/'report_zh.html').write_text(page)
    from fontTools import subset
    from fontTools.ttLib import TTFont
    source = Path('/tmp/opencode/pallet_pose_design/NotoSansCJKsc-Regular.otf')
    required = {ord(c) for c in page if '\u3400' <= c <= '\u9fff'}
    with TTFont(source) as font:
        if not required <= set(font.getBestCmap()):
            raise ValueError('Report source font 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, request_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, request_hashes = verified_inputs(parent, request_dir)
    config_hashes = file_inventory(PROJECT/'configs')
    frozen_code = dict(manifest['frozen_code_sha256'])
    frozen_code['pallet/mask_remaining_round2.py'] = digest(__file__)
    formal, rows, evidence = load_formal_evidence(manifest)
    formal_sha, classification_sha = manifest['formal_manifest_sha256'], manifest['classification_sha256']
    previous, parent_new, old_sources, requests = (read_jsonl(p) for p in (
        parent/'proposals.jsonl', parent/'new_proposals.jsonl', parent/'all_manual_sources.jsonl', request_dir/'requests.jsonl'))
    old_pose_keys, parent_new_keys = set(indexed(old_sources)), set(indexed(parent_new))
    old_review_sources = read_jsonl(parent/'reviewed_original_rectangle_source_records.jsonl')
    old_statement = read_json(parent/'review_completion_statement.json')
    if (len(rows) != 12344 or {r['final_class_id'] for r in rows.values()} != set(ALL_CLASSES) or
            len(previous) != 1296 or len(parent_new) != 185 or len(old_pose_keys) != 383 or len(old_review_sources) != 2 or
            len({p['source']['frame_id'] for p in previous}) != 1296 or
            any(p['source'] != rows[p['target_key']] for p in previous) or
            Counter(r['class_id'] for r in requests) != {3: 4, 4: 10, 5: 10, 7: 10, 14: 10, 22: 10}):
        raise ValueError('Formal / canonical1296 / prior185 / completed54 inventory changed')
    protected = protected_snapshot(formal)
    if (len(protected) != 20768 or protected != read_json(parent/'protected_formal_before.json') or
            protected != read_json(parent/'protected_formal_after.json')):
        raise ValueError('Protected formal 20768-file inventory changed')
    out.mkdir(parents=True)
    for n, value in (('parent_file_inventory', parent_hashes), ('request_file_inventory', request_hashes),
            ('configuration_input_hashes', config_hashes), ('protected_formal_before', protected)):
        write_json(out/(n+'.json'), value)
    statement = review_statement(requests, parent_new)
    write_json(out/'review_completion_statement.json', statement)
    write_json(out/'new_review_completion_statement.json', statement)
    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)
    requested = set(indexed(requests))
    delta = dict(pose=validate_source_delta(parent/'reviews_snapshot.sqlite3', pose_snapshot, 'annotations', requested),
        gallery=validate_source_delta(parent/'gallery_reviews_snapshot.sqlite3', gallery_snapshot, 'reviews', parent_new_keys))
    if delta['pose']['new_events'] != 145 or delta['gallery']['new_events'] != 201:
        raise ValueError('Exact completed-task event delta changed')
    write_json(out/'source_delta_audit.json', delta)
    candidates, reviewed, 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.get('formal_manifest_sha256') != formal_sha or
                binding.get('formal_annotations_sha256') != digest(formal/'annotations/frames.jsonl') or
                binding.get('formal_frames_sha256') != digest(formal/'frames.jsonl')):
            raise ValueError('Pose formal binding changed')
        current = remaining.snapshot_rows(db, 'annotations', rows)
        remaining.snapshot_rows(db, 'events', rows)
        _, _, eligibility = annotation_inventory(db, rows)
        pose_protected = expand.protected_history(db, rows, current)
        pose_events = db.execute('SELECT COUNT(*) FROM events').fetchone()[0]
        old_source_by_key = indexed(old_sources)
        for key in sorted(old_pose_keys | requested):
            if key in ORIGINAL_REVIEWS:
                reviewed[key] = exact_original_rectangle_review(db, key, rows[key], pose_sha, sha(binding), statement)
                if (rows[key]['final_class_id'], reviewed[key][0]['revision']) != ORIGINAL_REVIEWS[key]:
                    raise ValueError('New original-box class/revision changed')
                candidate = reviewed[key]
            else:
                candidates[key] = remaining.validated_manual_source(db, key, rows[key], pose_sha, sha(binding))
                candidate = candidates[key]
            value, metadata, record = candidate
            latest = db.execute('SELECT revision,payload FROM events WHERE target_key=? ORDER BY id DESC LIMIT 1', (key,)).fetchone()
            if latest != (value['revision'], record['current_payload_raw']):
                raise ValueError('Manual source current is not exact raw latest event')
            if key in old_pose_keys and record['current_payload_raw'] != old_source_by_key[key]['current_payload_raw']:
                raise ValueError('Inherited modified pose source changed')
            source_audit.append(dict(target_key=key, class_id=rows[key]['final_class_id'],
                current_revision=value['revision'], current_payload_sha256=sha(value),
                current_payload_raw_sha256=record['current_payload_raw_sha256'],
                exact_current_and_event_validated=True, modified_human_contour=key not in reviewed,
                original_rectangle_review_only=key in reviewed, vertex_count=len(value['polygon']),
                historical_canonical_preserved=key in old_pose_keys,
                current_template_selected=rows[key]['final_class_id'] in CLASSES and len(value['polygon']) > 4,
                snapshot_sha256=pose_sha))
        for record in old_review_sources:
            key = record['target_key']
            value, _, fresh = exact_original_rectangle_review(db, key, rows[key], pose_sha, sha(binding),
                old_statement, expected_statement=old_statement['statement_zh'])
            if (fresh['current_payload_raw'] != record['current_payload_raw'] or
                    fresh['saved_review_event_payload_raw'] != record['saved_review_event_payload_raw'] or
                    fresh['saved_review_event_id'] != record['saved_review_event_id'] or
                    record['user_statement_sha256'] != sha(old_statement)):
                raise ValueError('Old original-box evidence/statement binding changed')
            source_audit.append(dict(target_key=key, class_id=rows[key]['final_class_id'],
                current_revision=value['revision'], current_payload_sha256=sha(value),
                current_payload_raw_sha256=fresh['current_payload_raw_sha256'],
                exact_current_and_event_validated=True, modified_human_contour=False,
                original_rectangle_review_only=True, vertex_count=4, historical_canonical_preserved=True,
                current_template_selected=False, snapshot_sha256=pose_sha,
                original_statement_path='parent_evidence/review_completion_statement.json',
                original_statement_sha256=record['user_statement_sha256']))
    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 = remaining.snapshot_rows(db, 'reviews', rows)
        remaining.snapshot_rows(db, 'events', rows)
        events = [json.loads(raw) for raw, in db.execute('SELECT payload FROM events ORDER BY id')]
        gallery_keys = set(reviews) | {v['target_key'] for v in events}
        gallery_sources = []
        for key, revision, raw in db.execute('SELECT target_key,revision,payload FROM reviews ORDER BY target_key'):
            history = list(db.execute('SELECT id,revision,payload FROM events WHERE target_key=? ORDER BY id', (key,)))
            if not history or history[-1][1:] != (revision, raw):
                raise ValueError('Gallery current is not exact raw latest event')
            event_id, _, event_raw = history[-1]
            gallery_sources.append(dict(target_key=key, revision=revision, current_payload_raw=raw,
                current_payload_raw_sha256=hashlib.sha256(raw.encode('utf-8')).hexdigest(), current_payload=json.loads(raw),
                current_payload_sha256=sha(json.loads(raw)), human_event_id=event_id,
                human_event_payload_raw=event_raw, human_event_sha256=hashlib.sha256(event_raw.encode('utf-8')).hexdigest(),
                snapshot_sha256=gallery_sha))
    paths = dict(expand.complete.SOURCE_PUBLICATIONS)
    paths[9] = remaining.PARENT; paths[10] = expand.OUTPUT; paths[11] = parent
    publication_hashes = {v: file_inventory(p) for v, p in paths.items() if p != parent}
    gallery_templates = original.gallery_templates(previous, reviews, events, current, rows, parent,
        formal_sha, gallery_sha, publication_paths=paths)
    previous_by_key, gallery_source_by_key = indexed(previous), indexed(gallery_sources)
    for key, template in gallery_templates.items():
        source = gallery_source_by_key[key]
        template.update(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=source['current_payload_raw_sha256'],
            saved_review_event_id=source['human_event_id'], saved_review_event_raw_sha256=source['human_event_sha256'])
    quarantine = set(QUARANTINE) | set(manifest.get('preserved_quarantined_targets', []))
    manual = completed_manual_proposals(requests, candidates, reviewed, rows, previous,
        pose_sha, formal_sha, classification_sha, quarantine, gallery_keys)
    locked = previous+manual
    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)
    all_sources = [candidates[k][2] for k in sorted(candidates)]
    new_review_sources = [reviewed[k][2] for k in sorted(reviewed)]
    review_sources = old_review_sources+new_review_sources
    if (len(locked) != 1350 or len({p['source']['frame_id'] for p in locked}) != 1350 or
            len(all_sources) != 434 or len(review_sources) != 5 or len(gallery_templates) != 824 or
            len(templates) != 599 or Counter(t['origin'] for t in templates.values()) != {
                prior.POSE_ORIGIN: 45, original.GALLERY_ORIGIN: 554} or
            Counter(p['vertex_count'] for p in manual) != {4: 50, 8: 4} or
            len(current) != 4916 or pose_events != 11698 or len(reviews) != 824 or len(events) != 890):
        raise ValueError('Exact locked1350 / modified434 / review5 / gallery824 / seed599 contract changed')
    saved_status_records = []
    for p in parent_new:
        key = p['target_key']; review = reviews[key]; source = gallery_source_by_key[key]
        if key not in gallery_templates:
            raise ValueError('Completed candidate lacks validated saved >4 gallery source')
        saved_status_records.append(dict(target_key=key, class_id=p['class_id'],
            actual_saved=True, polygon_changed_from_proposal=review['polygon'] != p['polygon'],
            saved_review_unchanged=review['polygon'] == p['polygon'],
            human_new_edit_claim=False, new_human_edit_created=False,
            proposal_sha256=sha(p), proposal_polygon_sha256=sha(p['polygon']),
            current_revision=review['revision'], current_polygon_sha256=sha(review['polygon']),
            current_payload_sha256=sha(review), current_payload_raw_sha256=source['current_payload_raw_sha256'],
            human_event_id=source['human_event_id'], human_event_sha256=source['human_event_sha256'],
            gallery_snapshot_sha256=gallery_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 saved_status_records if r['saved_review_unchanged'])
    status = dict(parent_new_machine_candidates=185, actually_saved=len(saved_status_records), pending=0,
        polygon_changed_from_proposal=185-len(unchanged), saved_unchanged=len(unchanged),
        saved_keys=sorted(parent_new_keys), pending_keys=[], unchanged_keys=unchanged,
        saved_evidence_authority='real frozen current/raw latest events with historical publication/image/source bindings',
        unchanged_review_interpretation_statement_sha256=sha(statement),
        new_dispositions_created=0, new_human_edits_created=0, unsaved_candidates_are_seeds=False)
    if status['polygon_changed_from_proposal'] != 132 or status['saved_unchanged'] != 53:
        raise ValueError('Actual saved132-changed /53-unchanged review evidence changed')
    work, outcomes = target_inventory(rows, locked, eligibility, pose_protected, gallery_keys, quarantine)
    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)
    if capacity[19] > 90:
        raise ValueError('Protected original class19 capacity cannot exceed90')
    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),
        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']))
    for n, values in (('manual_proposals', manual), ('completed_request_manual_proposals', manual),
            ('manual_source_records', [candidates[k][2] for k in sorted(requested-set(reviewed))]),
            ('all_manual_sources', all_sources), ('new_reviewed_original_rectangle_source_records', new_review_sources),
            ('human_source_audit', source_audit), ('remaining_current_manual_source_records', [candidates[k][2] for k in sorted(remaining_keys)]),
            ('gallery_source_records', gallery_sources), ('saved_review_source_audit', saved_status_records),
            ('refined_annotations', [reviews[k] for k in sorted(reviews)]), ('capacity_frame_assignments', capacity_assignments)):
        write_jsonl(out/(n+'.jsonl'), values)
    # Preserve literal old source lines, including their old statement/snapshot hashes.
    with (out/'reviewed_original_rectangle_source_records.jsonl').open('wb') as target:
        target.write((parent/'reviewed_original_rectangle_source_records.jsonl').read_bytes())
        target.write((out/'new_reviewed_original_rectangle_source_records.jsonl').read_bytes())
    for n in ('manual_source_records', 'all_manual_sources', 'reviewed_original_rectangle_source_records',
            'new_reviewed_original_rectangle_source_records', 'remaining_current_manual_source_records', 'gallery_source_records'):
        (out/(n+'.jsonl')).chmod(0o600)
    write_json(out/'review_statement_bindings.json', dict(
        historical=dict(path='parent_evidence/review_completion_statement.json',
            file_sha256=parent_hashes['review_completion_statement.json'], statement_sha256=sha(old_statement),
            target_keys=sorted(indexed(old_review_sources))),
        current=dict(path='review_completion_statement.json', statement_sha256=sha(statement),
            target_keys=sorted(reviewed), scoped_request_keys=sorted(requested))))
    write_json(out/'saved_review_status.json', status)
    shutil.copyfile(request_dir/'requests.jsonl', out/'requests.jsonl')
    for request in requests:
        row = rows[request['target_key']]; native = evidence.image(row['frame_id'])
        if request['source']['image_sha256'] != evidence.used[row['image']]:
            raise ValueError('Completed request native source image changed')
        with Image.open(request_dir/'images'/f'{row["target_key"]}.png') as image:
            if not np.array_equal(np.asarray(image), native):
                raise ValueError('Request full native pixels differ from formal source')
    for p in previous:
        with Image.open(parent/'images'/f'{p["target_key"]}.png') as image:
            if not np.array_equal(np.asarray(image), evidence.image(p['source']['frame_id'])):
                raise ValueError('Inherited image differs from formal native pixels')
    print(json.dumps(dict(state='scanning', workers=workers, locked=len(locked), templates=len(templates),
        eligible_targets=len(work), saved_human_contours=1258, reviewed_original_rectangle=5,
        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):
        raise ValueError('Inherited canonical proposal changed')
    dispositions = read_jsonl(parent/'review_dispositions.jsonl')
    counts = class_counts(previous, manual, new, reviews, all_sources, review_sources, dispositions,
        templates, rows, work, outcomes, capacity)
    total = lambda f: sum(v[f] for v in counts.values())
    if (total('saved_human') != 1258 or total('db_stored_human_review') != 1263 or
            total('user_reviewed_no_change') != 45 or total('no_new_server_review') != 42+len(new)):
        raise ValueError('Saved contour1258 / original review5 / old45 / other42 contract changed')
    for n, 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/(n+'.jsonl'), values)
    for d in ('cases', 'images', 'masks', 'labelme'):
        (out/d).mkdir()
    copied = preserve_assets(parent, out, previous)
    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=1296, manual_proposals=54, modified_manual_proposals=51,
        reviewed_original_rectangle=5, new_reviewed_original_rectangle=3, all_manual_sources=434,
        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))
    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)), set(candidates), reviews,
        set(indexed(dispositions)), status, templates)
    contact = Image.new('RGB', (1480, 305*len(ALL_CLASSES)), '#121923')
    for i, cls in enumerate(ALL_CLASSES):
        p = next(p for p in new+manual+previous if p['class_id'] == cls)
        with Image.open(out/'cases'/f'{p["target_key"]}.png') as image:
            contact.paste(image, (0, i*305))
    contact.save(out/'preview_all_classes.png')
    used = read_json(parent/'used_image_hashes.json')
    for n, h in evidence.used.items():
        if n in used and used[n] != h:
            raise ValueError('Formal native image hash conflict')
        used[n] = h
    if any(digest(formal/n) != h for n, h in used.items()):
        raise ValueError('Used source image changed')
    write_json(out/'used_image_hashes.json', used)
    after = protected_snapshot(formal); write_json(out/'protected_formal_after.json', after)
    if after != protected:
        raise ValueError('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 source 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(request_dir) != request_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/n) != h for n, h in frozen_code.items()) or
            any(digest(out/n) != h for n, h in copied.items())):
        raise ValueError('Frozen inputs / immutable evidence / code / config / assets changed')
    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'],
        manual_template_required=counts[c]['templates'] == 0,
        explanation='Strict original gates; 1350 locked frames; no four-point/original-box propagation; no fake fill.') for c in CLASSES})
    result = dict(state='remaining_round2_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'],
        previous_proposals=1296, manual_proposals=54, completed_request_manual_proposals=54,
        modified_manual_proposals=51, reviewed_original_rectangle=5, new_reviewed_original_rectangle=3,
        inherited_reviewed_original_rectangle=2, reviewed_original_rectangle_schema=REVIEW_SCHEMA,
        manual_source_schema=MANUAL_SCHEMA, inherited_manual_sources=383, all_manual_sources=434,
        base_proposals=1350, proposals=len(combined), new_proposals=len(new), new_machine_pending=len(new),
        new_by_class=dict(Counter(p['class_id'] for p in new)), manual_by_class=dict(Counter(p['class_id'] for p in manual)),
        classes=counts, remaining_classes=list(CLASSES), all_classes=list(ALL_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'), modified_pose_source_records=434,
        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=45,
        no_new_server_review=total('no_new_server_review'), saved_review_status=status,
        counted_manual_source_keys=sorted(candidates), requested_manual_keys=sorted(indexed(manual)),
        reviewed_original_rectangle_keys=sorted(indexed(review_sources)), new_reviewed_original_rectangle_keys=sorted(reviewed),
        manual_four_point_sources=47, manual_more_than_four_sources=4,
        all_pose_four_point_sources=sum(r['manual_four_point_source'] for r in all_sources),
        generation_templates=len(templates), generation_template_namespace=GENERATION_NAMESPACE,
        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())),
        generation_template_keys=sorted(templates), 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()),
        formal_manifest_sha256=formal_sha, classification_sha256=classification_sha,
        original_classification_targets=len(rows), original_classification_classes=22,
        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),
        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, new_human_edits_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, historical_canonical_preserved=1296, 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 n in ('template_inventory', 'snapshot_manifest', 'font_coverage', 'saved_review_status',
            'review_completion_statement', 'new_review_completion_statement', 'review_statement_bindings', 'source_delta_audit'):
        result[n+'_sha256'] = digest(out/(n+'.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=1296, locked_before_machines=1350, global_distinct_frames=len(combined),
        preserved_source_files=len(copied), protected_formal_files=20768, no_production_writes=True))
    (out/'GENERATION_HANDOFF.md').write_text(
        '# Publication 12 draft — pending independent verification\n\n'
        f'{len(combined)} distinct frames = immutable parent1296 + completed54 + new machine{len(new)}. '
        '51 genuinely modified contours = 47 quads + 4 Class3 eight-point contours. '
        '3 new original boxes count only scoped user-reviewed coverage, never modified masks or seeds.\n\n'
        '`all_manual_sources.jsonl`: 434 actual modified pose sources. '
        '`reviewed_original_rectangle_source_records.jsonl`: old2 literal source lines then new3, '
        'with old statement retained at parent_evidence/review_completion_statement.json. '
        '`manual_proposals.jsonl` / `completed_request_manual_proposals.jsonl`: all completed54. '
        '`gallery_source_records.jsonl`: all824 current/raw latest event pairs. '
        '`saved_review_source_audit.jsonl`: exactly185 prior candidates,132 changed and53 saved unchanged.\n\n'
        '599 direct >4 remaining seeds = pose45 + gallery554. Filled classes1,2,3,6,9,10,13,15 '
        'are not scanned; Class3 templates remain inventory only. Old270 five-class gallery sources '
        'are validated but excluded. Quads, original boxes, old45 conversation dispositions and42 '
        'old unsaved candidates never seed. Canonical parent1296, all public assets/namespaces '
        'and complete private parent evidence are byte-preserved. Saved contours1258 and '
        'original-box reviews5 remain separate; stored review coverage1263.\n\n'
        'Independent verifier must replay all attempts, audit exact frozen evidence and compute '
        'global frame matching before publication. No ZIP, new requests, live DB/config/service writes.\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', 'missing', 'registration_attempts', 'accepted_before_diversity', 'code_sha256')}, ensure_ascii=False), flush=True)


if __name__ == '__main__':
    main()
